Skip to content

Commit

Permalink
Merge pull request #963 from Santarh/refactorTextureExporter
Browse files Browse the repository at this point in the history
Even if the texture instance is the same, if the color space required by the glTF specification is different, it will be output as a different texture.
  • Loading branch information
ousttrue authored May 21, 2021
2 parents c1930ca + 82106f6 commit 49e1ea3
Show file tree
Hide file tree
Showing 21 changed files with 165 additions and 154 deletions.
22 changes: 11 additions & 11 deletions Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/MaterialExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ public enum glTFBlendMode

public interface IMaterialExporter
{
glTFMaterial ExportMaterial(Material m, TextureExporter textureExporter);
glTFMaterial ExportMaterial(Material m, ITextureExporter textureExporter);
}

public class MaterialExporter : IMaterialExporter
{
public virtual glTFMaterial ExportMaterial(Material m, TextureExporter textureExporter)
public virtual glTFMaterial ExportMaterial(Material m, ITextureExporter textureExporter)
{
var material = CreateMaterial(m);

Expand All @@ -34,7 +34,7 @@ public virtual glTFMaterial ExportMaterial(Material m, TextureExporter textureEx
return material;
}

static void Export_Color(Material m, TextureExporter textureManager, glTFMaterial material)
static void Export_Color(Material m, ITextureExporter textureManager, glTFMaterial material)
{
if (m.HasProperty("_Color"))
{
Expand All @@ -43,7 +43,7 @@ static void Export_Color(Material m, TextureExporter textureManager, glTFMateria

if (m.HasProperty("_MainTex"))
{
var index = textureManager.ExportSRGB(m.GetTexture("_MainTex"));
var index = textureManager.ExportAsSRgb(m.GetTexture("_MainTex"));
if (index != -1)
{
material.pbrMetallicRoughness.baseColorTexture = new glTFMaterialBaseColorTextureInfo()
Expand All @@ -60,9 +60,9 @@ static void Export_Color(Material m, TextureExporter textureManager, glTFMateria
/// Occlusion, Metallic, Roughness
/// </summary>
/// <param name="m"></param>
/// <param name="textureManager"></param>
/// <param name="textureExporter"></param>
/// <param name="material"></param>
static void Export_OcclusionMetallicRoughness(Material m, TextureExporter textureManager, glTFMaterial material)
static void Export_OcclusionMetallicRoughness(Material m, ITextureExporter textureExporter, glTFMaterial material)
{
Texture metallicSmoothTexture = default;
float smoothness = 1.0f;
Expand All @@ -88,7 +88,7 @@ static void Export_OcclusionMetallicRoughness(Material m, TextureExporter textur
}
}

int index = textureManager.ExportMetallicSmoothnessOcclusion(metallicSmoothTexture, smoothness, occlusionTexture);
int index = textureExporter.ExportAsGltfMetallicSmoothnessOcclusionCombined(metallicSmoothTexture, smoothness, occlusionTexture);

if (index != -1 && metallicSmoothTexture != null)
{
Expand Down Expand Up @@ -127,11 +127,11 @@ static void Export_OcclusionMetallicRoughness(Material m, TextureExporter textur
}
}

static void Export_Normal(Material m, TextureExporter textureManager, glTFMaterial material)
static void Export_Normal(Material m, ITextureExporter textureExporter, glTFMaterial material)
{
if (m.HasProperty("_BumpMap"))
{
var index = textureManager.ExportNormal(m.GetTexture("_BumpMap"));
var index = textureExporter.ExportAsNormal(m.GetTexture("_BumpMap"));
if (index != -1)
{
material.normalTexture = new glTFMaterialNormalTextureInfo()
Expand All @@ -149,7 +149,7 @@ static void Export_Normal(Material m, TextureExporter textureManager, glTFMateri
}
}

static void Export_Emission(Material m, TextureExporter textureManager, glTFMaterial material)
static void Export_Emission(Material m, ITextureExporter textureExporter, glTFMaterial material)
{
if (m.IsKeywordEnabled("_EMISSION") == false)
return;
Expand All @@ -166,7 +166,7 @@ static void Export_Emission(Material m, TextureExporter textureManager, glTFMate

if (m.HasProperty("_EmissionMap"))
{
var index = textureManager.ExportSRGB(m.GetTexture("_EmissionMap"));
var index = textureExporter.ExportAsSRgb(m.GetTexture("_EmissionMap"));
if (index != -1)
{
material.emissiveTexture = new glTFMaterialEmissiveTextureInfo()
Expand Down
14 changes: 9 additions & 5 deletions Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ public List<Material> Materials
private set;
}

public TextureExporter TextureManager;
public TextureExporter TextureExporter
{
get;
private set;
}

protected virtual IMaterialExporter CreateMaterialExporter()
{
Expand Down Expand Up @@ -238,10 +242,10 @@ public virtual void Export(MeshExportSettings meshExportSettings, ITextureSerial
#region Materials and Textures
Materials = uniqueUnityMeshes.SelectMany(x => x.Renderer.sharedMaterials).Where(x => x != null).Distinct().ToList();

TextureManager = new TextureExporter(textureSerializer);
TextureExporter = new TextureExporter(textureSerializer);

var materialExporter = CreateMaterialExporter();
glTF.materials = Materials.Select(x => materialExporter.ExportMaterial(x, TextureManager)).ToList();
glTF.materials = Materials.Select(x => materialExporter.ExportMaterial(x, TextureExporter)).ToList();
#endregion

#region Meshes
Expand Down Expand Up @@ -360,9 +364,9 @@ public virtual void Export(MeshExportSettings meshExportSettings, ITextureSerial
ExportExtensions(textureSerializer);

// Extension で Texture が増える場合があるので最後に呼ぶ
for (int i = 0; i < TextureManager.Exported.Count; ++i)
for (int i = 0; i < TextureExporter.Exported.Count; ++i)
{
var (unityTexture, colorSpace) = TextureManager.Exported[i];
var (unityTexture, colorSpace) = TextureExporter.Exported[i];
glTF.PushGltfTexture(bufferIndex, unityTexture, colorSpace, textureSerializer);
}
}
Expand Down
8 changes: 4 additions & 4 deletions Assets/UniGLTF/Tests/UniGLTF/MaterialTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public void TextureTransformTest()
filterMode = FilterMode.Bilinear,
};

var textureManager = new TextureExporter(new EditorTextureSerializer());
var textureExporter = new TextureExporter(new EditorTextureSerializer());
var srcMaterial = new Material(Shader.Find("Standard"));

var offset = new Vector2(0.3f, 0.2f);
Expand All @@ -29,7 +29,7 @@ public void TextureTransformTest()
srcMaterial.mainTextureScale = scale;

var materialExporter = new MaterialExporter();
var gltfMaterial = materialExporter.ExportMaterial(srcMaterial, textureManager);
var gltfMaterial = materialExporter.ExportMaterial(srcMaterial, textureExporter);
gltfMaterial.pbrMetallicRoughness.baseColorTexture.extensions = gltfMaterial.pbrMetallicRoughness.baseColorTexture.extensions.Deserialize();

Assert.IsTrue(glTF_KHR_texture_transform.TryGet(gltfMaterial.pbrMetallicRoughness.baseColorTexture, out glTF_KHR_texture_transform t));
Expand Down Expand Up @@ -242,8 +242,8 @@ public void MaterialExportTest()
material.SetColor("_EmissionColor", new Color(0, 1, 2, 1));
material.EnableKeyword("_EMISSION");
var materialExporter = new MaterialExporter();
var textureExportManager = new TextureExporter(new EditorTextureSerializer());
var gltfMaterial = materialExporter.ExportMaterial(material, textureExportManager);
var textureExporter = new TextureExporter(new EditorTextureSerializer());
var gltfMaterial = materialExporter.ExportMaterial(material, textureExporter);

Assert.AreEqual(gltfMaterial.emissiveFactor, new float[] { 0, 0.5f, 1 });
}
Expand Down
6 changes: 3 additions & 3 deletions Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ public void TextureExportTest()
wrapMode = TextureWrapMode.Clamp,
filterMode = FilterMode.Trilinear,
};
var textureManager = new TextureExporter(new EditorTextureSerializer());
var textureExporter = new TextureExporter(new EditorTextureSerializer());

var material = new Material(Shader.Find("Standard"));
material.mainTexture = tex0;

var materialExporter = new MaterialExporter();
materialExporter.ExportMaterial(material, textureManager);
materialExporter.ExportMaterial(material, textureExporter);

var (convTex0, colorSpace) = textureManager.Exported[0];
var (convTex0, colorSpace) = textureExporter.Exported[0];
var sampler = TextureSamplerUtil.Export(convTex0);

Assert.AreEqual(glWrap.CLAMP_TO_EDGE, sampler.wrapS);
Expand Down
4 changes: 2 additions & 2 deletions Assets/VRM.Samples/Editor/Tests/VRMMaterialTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ static UniGLTF.glTFMaterial ExportLoaded(string resourceName)
{
var material = Resources.Load<Material>(resourceName);
var exporter = new VRMMaterialExporter();
var textureManager = new TextureExporter(new EditorTextureSerializer());
var exported = exporter.ExportMaterial(material, textureManager);
var textureExporter = new TextureExporter(new EditorTextureSerializer());
var exported = exporter.ExportMaterial(material, textureExporter);

// parse glTFExtensionExport to glTFExtensionImport
exported.extensions = exported.extensions.Deserialize();
Expand Down
4 changes: 2 additions & 2 deletions Assets/VRM/Runtime/IO/VRMExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public override void ExportExtensions(ITextureSerializer textureSerializer)
VRM.meta.title = meta.Title;
if (meta.Thumbnail != null)
{
VRM.meta.texture = TextureManager.ExportSRGB(meta.Thumbnail);
VRM.meta.texture = TextureExporter.ExportAsSRgb(meta.Thumbnail);
}

// ussage permission
Expand Down Expand Up @@ -199,7 +199,7 @@ public override void ExportExtensions(ITextureSerializer textureSerializer)
// materials
foreach (var m in Materials)
{
VRM.materialProperties.Add(VRMMaterialExporter.CreateFromMaterial(m, TextureManager));
VRM.materialProperties.Add(VRMMaterialExporter.CreateFromMaterial(m, TextureExporter));
}

// Serialize VRM
Expand Down
6 changes: 3 additions & 3 deletions Assets/VRM/Runtime/IO/VRMMaterialExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ static glTFMaterial Export_VRMMToon(Material m)
// "Queue",
};

public static glTF_VRM_Material CreateFromMaterial(Material m, TextureExporter textureExporter)
public static glTF_VRM_Material CreateFromMaterial(Material m, ITextureExporter textureExporter)
{
var material = new glTF_VRM_Material
{
Expand Down Expand Up @@ -181,8 +181,8 @@ public static glTF_VRM_Material CreateFromMaterial(Material m, TextureExporter t
if (texture != null)
{
var value = kv.Key == "_BumpMap"
? textureExporter.ExportNormal(texture)
: textureExporter.ExportSRGB(texture)
? textureExporter.ExportAsNormal(texture)
: textureExporter.ExportAsSRgb(texture)
;
if (value == -1)
{
Expand Down
4 changes: 2 additions & 2 deletions Assets/VRM/Tests/MToonTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public void TextureTransformTest()
filterMode = FilterMode.Bilinear,
};

var textureManager = new TextureExporter(new EditorTextureSerializer());
var textureExporter = new TextureExporter(new EditorTextureSerializer());
var srcMaterial = new Material(Shader.Find("VRM/MToon"));

var offset = new Vector2(0.3f, 0.2f);
Expand All @@ -29,7 +29,7 @@ public void TextureTransformTest()
srcMaterial.mainTextureScale = scale;

var materialExporter = new VRMMaterialExporter();
var vrmMaterial = VRMMaterialExporter.CreateFromMaterial(srcMaterial, textureManager);
var vrmMaterial = VRMMaterialExporter.CreateFromMaterial(srcMaterial, textureExporter);
Assert.AreEqual(vrmMaterial.vectorProperties["_MainTex"], new float[] { 0.3f, 0.2f, 0.5f, 0.6f });

var materialImporter = new VRMMaterialImporter(new glTF_VRM_extensions
Expand Down
18 changes: 9 additions & 9 deletions Assets/VRM10/Runtime/IO/Material/Vrm10MToonMaterialExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace UniVRM10
{
public static class Vrm10MToonMaterialExporter
{
public static bool TryExportMaterialAsMToon(Material src, TextureExporter textureExporter, out glTFMaterial dst)
public static bool TryExportMaterialAsMToon(Material src, ITextureExporter textureExporter, out glTFMaterial dst)
{
try
{
Expand Down Expand Up @@ -41,21 +41,21 @@ public static bool TryExportMaterialAsMToon(Material src, TextureExporter textur
baseColorFactor = def.Color.LitColor.ToFloat4(ColorSpace.sRGB, ColorSpace.Linear),
baseColorTexture = new glTFMaterialBaseColorTextureInfo
{
index = textureExporter.ExportSRGB(def.Color.LitMultiplyTexture),
index = textureExporter.ExportAsSRgb(def.Color.LitMultiplyTexture),
},
},

normalTexture = new glTFMaterialNormalTextureInfo
{
index = textureExporter.ExportNormal(def.Lighting.Normal.NormalTexture),
index = textureExporter.ExportAsNormal(def.Lighting.Normal.NormalTexture),
scale = def.Lighting.Normal.NormalScaleValue,
},

// Emission
emissiveFactor = def.Emission.EmissionColor.ToFloat3(ColorSpace.Linear, ColorSpace.Linear),
emissiveTexture = new glTFMaterialEmissiveTextureInfo
{
index = textureExporter.ExportSRGB(def.Emission.EmissionMultiplyTexture),
index = textureExporter.ExportAsSRgb(def.Emission.EmissionMultiplyTexture),
},
};

Expand All @@ -75,7 +75,7 @@ public static bool TryExportMaterialAsMToon(Material src, TextureExporter textur
ShadeColorFactor = def.Color.ShadeColor.ToFloat3(ColorSpace.sRGB, ColorSpace.Linear),
ShadeMultiplyTexture = new TextureInfo
{
Index = textureExporter.ExportSRGB(def.Color.ShadeMultiplyTexture),
Index = textureExporter.ExportAsSRgb(def.Color.ShadeMultiplyTexture),
},
ShadingToonyFactor = def.Lighting.LitAndShadeMixing.ShadingToonyValue,
ShadingShiftFactor = def.Lighting.LitAndShadeMixing.ShadingShiftValue,
Expand All @@ -87,14 +87,14 @@ public static bool TryExportMaterialAsMToon(Material src, TextureExporter textur
// Rim Lighting
MatcapTexture = new TextureInfo
{
Index = textureExporter.ExportSRGB(def.MatCap.AdditiveTexture),
Index = textureExporter.ExportAsSRgb(def.MatCap.AdditiveTexture),
},
ParametricRimColorFactor = def.Rim.RimColor.ToFloat3(ColorSpace.Linear, ColorSpace.Linear),
ParametricRimFresnelPowerFactor = def.Rim.RimFresnelPowerValue,
ParametricRimLiftFactor = def.Rim.RimLiftValue,
RimMultiplyTexture = new TextureInfo
{
Index = textureExporter.ExportSRGB(def.Rim.RimMultiplyTexture),
Index = textureExporter.ExportAsSRgb(def.Rim.RimMultiplyTexture),
},
RimLightingMixFactor = def.Rim.RimLightingMixValue,

Expand All @@ -103,15 +103,15 @@ public static bool TryExportMaterialAsMToon(Material src, TextureExporter textur
OutlineWidthFactor = def.Outline.OutlineWidthValue * centimeterToMeter,
OutlineWidthMultiplyTexture = new TextureInfo
{
Index = textureExporter.ExportLinear(def.Outline.OutlineWidthMultiplyTexture),
Index = textureExporter.ExportAsLinear(def.Outline.OutlineWidthMultiplyTexture),
},
OutlineColorFactor = def.Outline.OutlineColor.ToFloat3(ColorSpace.sRGB, ColorSpace.Linear),
OutlineLightingMixFactor = ExportOutlineLightingMixFactor(def.Outline.OutlineColorMode, def.Outline.OutlineLightingMixValue),

// UV Anim
UvAnimationMaskTexture = new TextureInfo
{
Index = textureExporter.ExportLinear(def.TextureOption.UvAnimationMaskTexture),
Index = textureExporter.ExportAsLinear(def.TextureOption.UvAnimationMaskTexture),
},
UvAnimationScrollXSpeedFactor = def.TextureOption.UvAnimationScrollXSpeedValue,
UvAnimationScrollYSpeedFactor = def.TextureOption.UvAnimationScrollYSpeedValue * invertY,
Expand Down
2 changes: 1 addition & 1 deletion Assets/VRM10/Runtime/IO/Vrm10Exporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ void ExportExpression(UniGLTF.Extensions.VRMC_vrm.VRMC_vrm vrm, VRM10Controller
int? thumbnailTextureIndex = default;
if (meta.Thumbnail != null)
{
thumbnailTextureIndex = m_textureExporter.ExportSRGB(meta.Thumbnail);
thumbnailTextureIndex = m_textureExporter.ExportAsSRgb(meta.Thumbnail);
}
return thumbnailTextureIndex;
}
Expand Down
2 changes: 1 addition & 1 deletion Assets/VRM10/Runtime/IO/Vrm10MaterialExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace UniVRM10
{
public class Vrm10MaterialExporter : MaterialExporter
{
public override glTFMaterial ExportMaterial(Material m, TextureExporter textureExporter)
public override glTFMaterial ExportMaterial(Material m, ITextureExporter textureExporter)
{
if (Vrm10MToonMaterialExporter.TryExportMaterialAsMToon(m, textureExporter, out var dst))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public bool CanExportAsEditorAssetFile(Texture texture)

public (byte[] bytes, string mime) ExportBytesWithMime(Texture2D texture, ColorSpace textureColorSpace)
{
if (TryGetBytesWithMime(texture, out byte[] bytes, out string mime))
if (CanExportAsEditorAssetFile(texture) && TryGetBytesWithMime(texture, out byte[] bytes, out string mime))
{
return (bytes, mime);
}
Expand Down
38 changes: 38 additions & 0 deletions Assets/VRMShaders/GLTF/IO/Runtime/ITextureExporter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.Collections.Generic;
using UnityEngine;

namespace VRMShaders
{
/// <summary>
/// Texture を用途別に変換の要不要を判断して gltf.textures の index に対応させる機能。
///
/// glTF 拡張で Texture の用途を増やす必要がある場合は、この interface を継承して実装すればよい。
/// </summary>
public interface ITextureExporter
{
/// <summary>
/// Export する Texture2D のリスト。これが gltf.textures になる
/// </summary>
IReadOnlyList<(Texture2D, UniGLTF.ColorSpace)> Exported { get; }

/// <summary>
/// 指定の Texture を、 sRGB 色空間の値を持つ Texture に出力するように指示する。
/// </summary>
int ExportAsSRgb(Texture src);

/// <summary>
/// 指定の Texture を、 Linear の値を持つ Texture に出力するように指示する。
/// </summary>
int ExportAsLinear(Texture src);

/// <summary>
/// 指定の Metallic, Roughness, Occlusion 情報を、 glTF 仕様に準拠した 1 枚の合成テクスチャとして出力するように指示する。
/// </summary>
int ExportAsGltfMetallicSmoothnessOcclusionCombined(Texture metallicSmoothTexture, float smoothness, Texture occlusionTexture);

/// <summary>
/// 指定の Texture を、glTF 仕様に準拠した Normal Texture に出力するように指示する。
/// </summary>
int ExportAsNormal(Texture src);
}
}
Loading

0 comments on commit 49e1ea3

Please sign in to comment.