From 27f9a9908f644696b901b2648bec4b0fe33979fe Mon Sep 17 00:00:00 2001 From: ousttrue Date: Thu, 11 Mar 2021 19:53:22 +0900 Subject: [PATCH 01/28] fix normal use original texture --- .../UniGLTF/IO/TextureLoader/TextureFactory.cs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/TextureFactory.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/TextureFactory.cs index de8a409f1..f8310b717 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/TextureFactory.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/TextureFactory.cs @@ -55,15 +55,16 @@ namespace UniGLTF if (param.TextureType == GetTextureParam.NORMAL_PROP) { cacheName = param.GltflName; + if (m_textureCache.TryGetValue(cacheName, out TextureLoadInfo normalInfo)) + { + external = normalInfo.Texture; + return true; + } } - if (m_externalMap.TryGetValue(cacheName, out external)) { - if (!m_textureCache.ContainsKey(cacheName)) - { - m_textureCache.Add(cacheName, new TextureLoadInfo(external, used, true)); - } - return external; + m_textureCache.Add(cacheName, new TextureLoadInfo(external, used, true)); + return true; } } external = default; From f35a51b645ea44c61d88d241f65415a1a9d4449f Mon Sep 17 00:00:00 2001 From: ousttrue Date: Thu, 11 Mar 2021 19:53:55 +0900 Subject: [PATCH 02/28] fix roughnessConversion #388 --- .../MetallicRoughnessConverter.cs | 61 ++++++++++++------- 1 file changed, 38 insertions(+), 23 deletions(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/MetallicRoughnessConverter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/MetallicRoughnessConverter.cs index 4f6a9ad90..ee078b880 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/MetallicRoughnessConverter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/MetallicRoughnessConverter.cs @@ -1,14 +1,27 @@ +#define SIMPLE_CONV using UnityEngine; namespace UniGLTF { + /// + /// + /// * https://github.com/dwango/UniVRM/issues/212. + /// * https://blogs.unity3d.com/jp/2016/01/25/ggx-in-unity-5-3/ + /// * https://github.com/vrm-c/UniVRM/issues/388 + /// + /// Occlusion(glTF): src.r + /// Roughness(glTF): src.g -> Smoothness(Unity): dst.a (bake smoothnessOrRoughness) + /// Metallic(glTF) : src.b -> Metallic(Unity) : dst.r + /// public class MetallicRoughnessConverter : ITextureConverter { - private float _smoothnessOrRoughness; + private readonly float _smoothnessOrRoughness; + private readonly float _smoothnessOrRoughnessInverse; public MetallicRoughnessConverter(float smoothnessOrRoughness) { _smoothnessOrRoughness = smoothnessOrRoughness; + _smoothnessOrRoughnessInverse = 1.0f / _smoothnessOrRoughness; } public Texture2D GetImportTexture(Texture2D texture) @@ -25,43 +38,45 @@ namespace UniGLTF public Color32 Import(Color32 src) { - // Roughness(glTF): dst.g -> Smoothness(Unity): src.a (with conversion) - // Metallic(glTF) : dst.b -> Metallic(Unity) : src.r - - var pixelRoughnessFactor = (src.g * _smoothnessOrRoughness) / 255.0f; // roughness - var pixelSmoothness = 1.0f - Mathf.Sqrt(pixelRoughnessFactor); - - return new Color32 + var dst = new Color32 { r = src.b, g = 0, b = 0, - // Bake roughness values into a texture. - // See: https://github.com/dwango/UniVRM/issues/212. - a = (byte)Mathf.Clamp(pixelSmoothness * 255, 0, 255), }; + + // Bake _smoothnessOrRoughness into a texture. +#if SIMPLE_CONV + dst.a = (byte)(255 - src.g * _smoothnessOrRoughness); +#else + var pixelRoughnessFactor = (src.g * _smoothnessOrRoughness) / 255.0f; // roughness + var pixelSmoothness = 1.0f - Mathf.Sqrt(pixelRoughnessFactor); + dst.a = (byte)Mathf.Clamp(pixelSmoothness * 255, 0, 255); +#endif + return dst; } public Color32 Export(Color32 src) { - // Smoothness(Unity): src.a -> Roughness(glTF): dst.g (with conversion) - // Metallic(Unity) : src.r -> Metallic(glTF) : dst.b - var pixelSmoothness = (src.a * _smoothnessOrRoughness) / 255.0f; // smoothness - // https://blogs.unity3d.com/jp/2016/01/25/ggx-in-unity-5-3/ - var pixelRoughnessFactorSqrt = (1.0f - pixelSmoothness); - var pixelRoughnessFactor = pixelRoughnessFactorSqrt * pixelRoughnessFactorSqrt; - - return new Color32 + var dst = new Color32 { r = 0, - // Bake smoothness values into a texture. - // See: https://github.com/dwango/UniVRM/issues/212. - g = (byte)Mathf.Clamp(pixelRoughnessFactor * 255, 0, 255), b = src.r, a = 255, }; + + // Bake divide _smoothnessOrRoughness from a texture. +#if SIMPLE_CONV + dst.g = (byte)(255 - src.a); +#else + var pixelSmoothness = (src.a * _smoothnessOrRoughness) / 255.0f; // smoothness + var pixelRoughnessFactorSqrt = (1.0f - pixelSmoothness); + var pixelRoughnessFactor = pixelRoughnessFactorSqrt * pixelRoughnessFactorSqrt; + dst.g = (byte)Mathf.Clamp(pixelRoughnessFactor * 255, 0, 255); +#endif + + return dst; } } - } From d589f6bb46aee54c59feaa18d4ddf52dd5f5196f Mon Sep 17 00:00:00 2001 From: ousttrue Date: Fri, 12 Mar 2021 18:28:46 +0900 Subject: [PATCH 03/28] OcclusionMetallicRoughnessConverter --- .../ScriptedImporter/ScriptedImporterImpl.cs | 3 +- .../ScriptedImporter/TextureExtractor.cs | 8 +- .../Runtime/UniGLTF/IO/MaterialExporter.cs | 58 ++-- .../IO/MaterialLoader/PBRMaterialItem.cs | 274 +++++++++--------- .../IO/MaterialLoader/UnlitMaterialItem.cs | 2 +- .../MetallicRoughnessConverter.cs | 82 ------ .../IO/TextureConverter/OcclusionConverter.cs | 42 --- .../OcclusionConverter.cs.meta | 11 - .../OcclusionMetallicRoughnessConverter.cs | 65 +++++ ...clusionMetallicRoughnessConverter.cs.meta} | 2 +- .../IO/TextureLoader/GetTextureParam.cs | 58 ++-- .../IO/TextureLoader/TextureFactory.cs | 27 +- Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs | 16 +- Assets/VRM/Runtime/IO/VRMImporterContext.cs | 2 +- 14 files changed, 285 insertions(+), 365 deletions(-) delete mode 100644 Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/MetallicRoughnessConverter.cs delete mode 100644 Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionConverter.cs delete mode 100644 Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionConverter.cs.meta create mode 100644 Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionMetallicRoughnessConverter.cs rename Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/{MetallicRoughnessConverter.cs.meta => OcclusionMetallicRoughnessConverter.cs.meta} (83%) diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterImpl.cs b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterImpl.cs index a2dba9f0c..666defaaf 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterImpl.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterImpl.cs @@ -64,8 +64,7 @@ namespace UniGLTF { switch (texParam.TextureType) { - case GetTextureParam.METALLIC_GLOSS_PROP: - case GetTextureParam.OCCLUSION_PROP: + case GetTextureParam.TextureTypes.StandardMap: break; default: diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/TextureExtractor.cs b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/TextureExtractor.cs index dd14e8894..a74736131 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/TextureExtractor.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/TextureExtractor.cs @@ -70,8 +70,7 @@ namespace UniGLTF switch (param.TextureType) { - case GetTextureParam.METALLIC_GLOSS_PROP: - case GetTextureParam.OCCLUSION_PROP: + case GetTextureParam.TextureTypes.StandardMap: { // write converted texture targetPath = $"{m_path}/{param.ConvertedName}.png"; @@ -141,8 +140,7 @@ namespace UniGLTF { switch (param.TextureType) { - case GetTextureParam.OCCLUSION_PROP: - case GetTextureParam.METALLIC_GLOSS_PROP: + case GetTextureParam.TextureTypes.StandardMap: #if VRM_DEVELOP Debug.Log($"{targetPath} => linear"); #endif @@ -150,7 +148,7 @@ namespace UniGLTF targetTextureImporter.SaveAndReimport(); break; - case GetTextureParam.NORMAL_PROP: + case GetTextureParam.TextureTypes.NormalMap: #if VRM_DEVELOP Debug.Log($"{targetPath} => normalmap"); #endif diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialExporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialExporter.cs index 3bc1861d3..ee06e8c59 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialExporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialExporter.cs @@ -27,10 +27,9 @@ namespace UniGLTF // common params material.name = m.name; Export_Color(m, textureManager, material); - Export_Metallic(m, textureManager, material); - Export_Normal(m, textureManager, material); - Export_Occlusion(m, textureManager, material); Export_Emission(m, textureManager, material); + Export_Normal(m, textureManager, material); + Export_PBR(m, textureManager, material); return material; } @@ -57,7 +56,13 @@ namespace UniGLTF } } - static void Export_Metallic(Material m, TextureExportManager textureManager, glTFMaterial material) + /// + /// Occlusion, Metallic, Roughness + /// + /// + /// + /// + static void Export_PBR(Material m, TextureExportManager textureManager, glTFMaterial material) { int index = -1; if (m.HasProperty("_MetallicGlossMap")) @@ -69,7 +74,7 @@ namespace UniGLTF } // Bake smoothness values into a texture. - var converter = new MetallicRoughnessConverter(smoothness); + var converter = new OcclusionMetallicRoughnessConverter(smoothness); index = textureManager.ConvertAndGetIndex(m.GetTexture("_MetallicGlossMap"), converter); if (index != -1) { @@ -102,6 +107,27 @@ namespace UniGLTF } } } + // static void Export_Occlusion(Material m, TextureExportManager textureManager, glTFMaterial material) + // { + // if (m.HasProperty("_OcclusionMap")) + // { + // var index = textureManager.ConvertAndGetIndex(m.GetTexture("_OcclusionMap"), new OcclusionConverter()); + // if (index != -1) + // { + // material.occlusionTexture = new glTFMaterialOcclusionTextureInfo() + // { + // index = index, + // }; + + // Export_MainTextureTransform(m, material.occlusionTexture); + // } + + // if (index != -1 && m.HasProperty("_OcclusionStrength")) + // { + // material.occlusionTexture.strength = m.GetFloat("_OcclusionStrength"); + // } + // } + // } static void Export_Normal(Material m, TextureExportManager textureManager, glTFMaterial material) { @@ -125,28 +151,6 @@ namespace UniGLTF } } - static void Export_Occlusion(Material m, TextureExportManager textureManager, glTFMaterial material) - { - if (m.HasProperty("_OcclusionMap")) - { - var index = textureManager.ConvertAndGetIndex(m.GetTexture("_OcclusionMap"), new OcclusionConverter()); - if (index != -1) - { - material.occlusionTexture = new glTFMaterialOcclusionTextureInfo() - { - index = index, - }; - - Export_MainTextureTransform(m, material.occlusionTexture); - } - - if (index != -1 && m.HasProperty("_OcclusionStrength")) - { - material.occlusionTexture.strength = m.GetFloat("_OcclusionStrength"); - } - } - } - static void Export_Emission(Material m, TextureExportManager textureManager, glTFMaterial material) { if (m.IsKeywordEnabled("_EMISSION") == false) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/PBRMaterialItem.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/PBRMaterialItem.cs index 4ea932bb5..e79c9997c 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/PBRMaterialItem.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/PBRMaterialItem.cs @@ -45,21 +45,16 @@ namespace UniGLTF public static GetTextureParam BaseColorTexture(glTF gltf, glTFMaterial src) { - return GetTextureParam.Create(gltf, src.pbrMetallicRoughness.baseColorTexture.index); + return GetTextureParam.CreateSRGB(gltf, src.pbrMetallicRoughness.baseColorTexture.index); } - public static GetTextureParam MetallicRoughnessTexture(glTF gltf, glTFMaterial src) + public static GetTextureParam StandardTexture(glTF gltf, glTFMaterial src) { - return GetTextureParam.CreateMetallic(gltf, + return GetTextureParam.CreateStandard(gltf, src.pbrMetallicRoughness.metallicRoughnessTexture.index, src.pbrMetallicRoughness.metallicFactor); } - public static GetTextureParam OcclusionTexture(glTF gltf, glTFMaterial src) - { - return GetTextureParam.CreateOcclusion(gltf, src.occlusionTexture.index); - } - public static GetTextureParam NormalTexture(glTF gltf, glTFMaterial src) { return GetTextureParam.CreateNormal(gltf, src.normalTexture.index); @@ -69,156 +64,153 @@ namespace UniGLTF { if (getTexture == null) { - getTexture = (_x, _y, _z) => Task.FromResult(null); + getTexture = (IAwaitCaller _awaitCaller, glTF _gltf, GetTextureParam _param) => Task.FromResult(null); } - // PBR material - var material = default(Material); - if (i >= 0 && i < gltf.materials.Count) + if (i < 0 || i >= gltf.materials.Count) { - var src = gltf.materials[i]; - material = MaterialFactory.CreateMaterial(i, src, ShaderName); - if (src.pbrMetallicRoughness != null) + return MaterialFactory.CreateMaterial(i, null, ShaderName); + } + + var src = gltf.materials[i]; + var material = MaterialFactory.CreateMaterial(i, src, ShaderName); + var standardParam = StandardTexture(gltf, src); + if (src.pbrMetallicRoughness != null) + { + if (src.pbrMetallicRoughness.baseColorFactor != null && src.pbrMetallicRoughness.baseColorFactor.Length == 4) { - if (src.pbrMetallicRoughness.baseColorFactor != null && src.pbrMetallicRoughness.baseColorFactor.Length == 4) - { - var color = src.pbrMetallicRoughness.baseColorFactor; - material.color = (new Color(color[0], color[1], color[2], color[3])).gamma; - } - - if (src.pbrMetallicRoughness.baseColorTexture != null && src.pbrMetallicRoughness.baseColorTexture.index != -1) - { - material.mainTexture = await getTexture(awaitCaller, gltf, BaseColorTexture(gltf, src)); - - // Texture Offset and Scale - MaterialFactory.SetTextureOffsetAndScale(material, src.pbrMetallicRoughness.baseColorTexture, "_MainTex"); - } - - if (src.pbrMetallicRoughness.metallicRoughnessTexture != null && src.pbrMetallicRoughness.metallicRoughnessTexture.index != -1) - { - material.EnableKeyword("_METALLICGLOSSMAP"); - - var texture = await getTexture(awaitCaller, gltf, MetallicRoughnessTexture(gltf, src)); - if (texture != null) - { - material.SetTexture(GetTextureParam.METALLIC_GLOSS_PROP, texture); - } - - material.SetFloat("_Metallic", 1.0f); - // Set 1.0f as hard-coded. See: https://github.com/dwango/UniVRM/issues/212. - material.SetFloat("_GlossMapScale", 1.0f); - - // Texture Offset and Scale - MaterialFactory.SetTextureOffsetAndScale(material, src.pbrMetallicRoughness.metallicRoughnessTexture, "_MetallicGlossMap"); - } - else - { - material.SetFloat("_Metallic", src.pbrMetallicRoughness.metallicFactor); - material.SetFloat("_Glossiness", 1.0f - src.pbrMetallicRoughness.roughnessFactor); - } + var color = src.pbrMetallicRoughness.baseColorFactor; + material.color = (new Color(color[0], color[1], color[2], color[3])).gamma; } - if (src.normalTexture != null && src.normalTexture.index != -1) + if (src.pbrMetallicRoughness.baseColorTexture != null && src.pbrMetallicRoughness.baseColorTexture.index != -1) { - material.EnableKeyword("_NORMALMAP"); - var texture = await getTexture(awaitCaller, gltf, NormalTexture(gltf, src)); + material.mainTexture = await getTexture(awaitCaller, gltf, BaseColorTexture(gltf, src)); + + // Texture Offset and Scale + MaterialFactory.SetTextureOffsetAndScale(material, src.pbrMetallicRoughness.baseColorTexture, "_MainTex"); + } + + if (src.pbrMetallicRoughness.metallicRoughnessTexture != null && src.pbrMetallicRoughness.metallicRoughnessTexture.index != -1) + { + material.EnableKeyword("_METALLICGLOSSMAP"); + + var texture = await getTexture(awaitCaller, gltf, standardParam); if (texture != null) { - material.SetTexture(GetTextureParam.NORMAL_PROP, texture); - material.SetFloat("_BumpScale", src.normalTexture.scale); + material.SetTexture(GetTextureParam.METALLIC_GLOSS_PROP, texture); + } + + material.SetFloat("_Metallic", 1.0f); + // Set 1.0f as hard-coded. See: https://github.com/dwango/UniVRM/issues/212. + material.SetFloat("_GlossMapScale", 1.0f); + + // Texture Offset and Scale + MaterialFactory.SetTextureOffsetAndScale(material, src.pbrMetallicRoughness.metallicRoughnessTexture, "_MetallicGlossMap"); + } + else + { + material.SetFloat("_Metallic", src.pbrMetallicRoughness.metallicFactor); + material.SetFloat("_Glossiness", 1.0f - src.pbrMetallicRoughness.roughnessFactor); + } + } + + if (src.normalTexture != null && src.normalTexture.index != -1) + { + material.EnableKeyword("_NORMALMAP"); + var texture = await getTexture(awaitCaller, gltf, NormalTexture(gltf, src)); + if (texture != null) + { + material.SetTexture(GetTextureParam.NORMAL_PROP, texture); + material.SetFloat("_BumpScale", src.normalTexture.scale); + } + + // Texture Offset and Scale + MaterialFactory.SetTextureOffsetAndScale(material, src.normalTexture, "_BumpMap"); + } + + if (src.occlusionTexture != null && src.occlusionTexture.index != -1) + { + var texture = await getTexture(awaitCaller, gltf, standardParam); + if (texture != null) + { + material.SetTexture(GetTextureParam.OCCLUSION_PROP, texture); + material.SetFloat("_OcclusionStrength", src.occlusionTexture.strength); + } + + // Texture Offset and Scale + MaterialFactory.SetTextureOffsetAndScale(material, src.occlusionTexture, "_OcclusionMap"); + } + + if (src.emissiveFactor != null + || (src.emissiveTexture != null && src.emissiveTexture.index != -1)) + { + material.EnableKeyword("_EMISSION"); + material.globalIlluminationFlags &= ~MaterialGlobalIlluminationFlags.EmissiveIsBlack; + + if (src.emissiveFactor != null && src.emissiveFactor.Length == 3) + { + material.SetColor("_EmissionColor", new Color(src.emissiveFactor[0], src.emissiveFactor[1], src.emissiveFactor[2])); + } + + if (src.emissiveTexture != null && src.emissiveTexture.index != -1) + { + var texture = await getTexture(awaitCaller, gltf, GetTextureParam.CreateSRGB(gltf, src.emissiveTexture.index)); + if (texture != null) + { + material.SetTexture("_EmissionMap", texture); } // Texture Offset and Scale - MaterialFactory.SetTextureOffsetAndScale(material, src.normalTexture, "_BumpMap"); + MaterialFactory.SetTextureOffsetAndScale(material, src.emissiveTexture, "_EmissionMap"); } - - if (src.occlusionTexture != null && src.occlusionTexture.index != -1) - { - var texture = await getTexture(awaitCaller, gltf, OcclusionTexture(gltf, src)); - if (texture != null) - { - material.SetTexture(GetTextureParam.OCCLUSION_PROP, texture); - material.SetFloat("_OcclusionStrength", src.occlusionTexture.strength); - } - - // Texture Offset and Scale - MaterialFactory.SetTextureOffsetAndScale(material, src.occlusionTexture, "_OcclusionMap"); - } - - if (src.emissiveFactor != null - || (src.emissiveTexture != null && src.emissiveTexture.index != -1)) - { - material.EnableKeyword("_EMISSION"); - material.globalIlluminationFlags &= ~MaterialGlobalIlluminationFlags.EmissiveIsBlack; - - if (src.emissiveFactor != null && src.emissiveFactor.Length == 3) - { - material.SetColor("_EmissionColor", new Color(src.emissiveFactor[0], src.emissiveFactor[1], src.emissiveFactor[2])); - } - - if (src.emissiveTexture != null && src.emissiveTexture.index != -1) - { - var texture = await getTexture(awaitCaller, gltf, GetTextureParam.Create(gltf, src.emissiveTexture.index)); - if (texture != null) - { - material.SetTexture("_EmissionMap", texture); - } - - // Texture Offset and Scale - MaterialFactory.SetTextureOffsetAndScale(material, src.emissiveTexture, "_EmissionMap"); - } - } - - BlendMode blendMode = BlendMode.Opaque; - // https://forum.unity.com/threads/standard-material-shader-ignoring-setfloat-property-_mode.344557/#post-2229980 - switch (src.alphaMode) - { - case "BLEND": - blendMode = BlendMode.Fade; - material.SetOverrideTag("RenderType", "Transparent"); - material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha); - material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha); - material.SetInt("_ZWrite", 0); - material.DisableKeyword("_ALPHATEST_ON"); - material.EnableKeyword("_ALPHABLEND_ON"); - material.DisableKeyword("_ALPHAPREMULTIPLY_ON"); - material.renderQueue = 3000; - break; - - case "MASK": - blendMode = BlendMode.Cutout; - material.SetOverrideTag("RenderType", "TransparentCutout"); - material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One); - material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero); - material.SetInt("_ZWrite", 1); - material.SetFloat("_Cutoff", src.alphaCutoff); - material.EnableKeyword("_ALPHATEST_ON"); - material.DisableKeyword("_ALPHABLEND_ON"); - material.DisableKeyword("_ALPHAPREMULTIPLY_ON"); - material.renderQueue = 2450; - - break; - - default: // OPAQUE - blendMode = BlendMode.Opaque; - material.SetOverrideTag("RenderType", ""); - material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One); - material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero); - material.SetInt("_ZWrite", 1); - material.DisableKeyword("_ALPHATEST_ON"); - material.DisableKeyword("_ALPHABLEND_ON"); - material.DisableKeyword("_ALPHAPREMULTIPLY_ON"); - material.renderQueue = -1; - break; - } - - material.SetFloat("_Mode", (float)blendMode); } - else + + BlendMode blendMode = BlendMode.Opaque; + // https://forum.unity.com/threads/standard-material-shader-ignoring-setfloat-property-_mode.344557/#post-2229980 + switch (src.alphaMode) { - material = MaterialFactory.CreateMaterial(i, null, ShaderName); + case "BLEND": + blendMode = BlendMode.Fade; + material.SetOverrideTag("RenderType", "Transparent"); + material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha); + material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha); + material.SetInt("_ZWrite", 0); + material.DisableKeyword("_ALPHATEST_ON"); + material.EnableKeyword("_ALPHABLEND_ON"); + material.DisableKeyword("_ALPHAPREMULTIPLY_ON"); + material.renderQueue = 3000; + break; + + case "MASK": + blendMode = BlendMode.Cutout; + material.SetOverrideTag("RenderType", "TransparentCutout"); + material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One); + material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero); + material.SetInt("_ZWrite", 1); + material.SetFloat("_Cutoff", src.alphaCutoff); + material.EnableKeyword("_ALPHATEST_ON"); + material.DisableKeyword("_ALPHABLEND_ON"); + material.DisableKeyword("_ALPHAPREMULTIPLY_ON"); + material.renderQueue = 2450; + + break; + + default: // OPAQUE + blendMode = BlendMode.Opaque; + material.SetOverrideTag("RenderType", ""); + material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One); + material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero); + material.SetInt("_ZWrite", 1); + material.DisableKeyword("_ALPHATEST_ON"); + material.DisableKeyword("_ALPHABLEND_ON"); + material.DisableKeyword("_ALPHAPREMULTIPLY_ON"); + material.renderQueue = -1; + break; } + material.SetFloat("_Mode", (float)blendMode); + return material; } } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/UnlitMaterialItem.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/UnlitMaterialItem.cs index 2cca1aab4..8960e8b27 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/UnlitMaterialItem.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/UnlitMaterialItem.cs @@ -21,7 +21,7 @@ namespace UniGLTF // texture if (src.pbrMetallicRoughness.baseColorTexture != null) { - material.mainTexture = await getTexture(awaitCaller, gltf, GetTextureParam.Create(gltf, src.pbrMetallicRoughness.baseColorTexture.index)); + material.mainTexture = await getTexture(awaitCaller, gltf, GetTextureParam.CreateSRGB(gltf, src.pbrMetallicRoughness.baseColorTexture.index)); // Texture Offset and Scale MaterialFactory.SetTextureOffsetAndScale(material, src.pbrMetallicRoughness.baseColorTexture, "_MainTex"); diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/MetallicRoughnessConverter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/MetallicRoughnessConverter.cs deleted file mode 100644 index ee078b880..000000000 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/MetallicRoughnessConverter.cs +++ /dev/null @@ -1,82 +0,0 @@ -#define SIMPLE_CONV -using UnityEngine; - -namespace UniGLTF -{ - /// - /// - /// * https://github.com/dwango/UniVRM/issues/212. - /// * https://blogs.unity3d.com/jp/2016/01/25/ggx-in-unity-5-3/ - /// * https://github.com/vrm-c/UniVRM/issues/388 - /// - /// Occlusion(glTF): src.r - /// Roughness(glTF): src.g -> Smoothness(Unity): dst.a (bake smoothnessOrRoughness) - /// Metallic(glTF) : src.b -> Metallic(Unity) : dst.r - /// - public class MetallicRoughnessConverter : ITextureConverter - { - private readonly float _smoothnessOrRoughness; - private readonly float _smoothnessOrRoughnessInverse; - - public MetallicRoughnessConverter(float smoothnessOrRoughness) - { - _smoothnessOrRoughness = smoothnessOrRoughness; - _smoothnessOrRoughnessInverse = 1.0f / _smoothnessOrRoughness; - } - - public Texture2D GetImportTexture(Texture2D texture) - { - var converted = TextureConverter.Convert(texture, glTFTextureTypes.Metallic, Import, null); - return converted; - } - - public Texture2D GetExportTexture(Texture2D texture) - { - var converted = TextureConverter.Convert(texture, glTFTextureTypes.Metallic, Export, null); - return converted; - } - - public Color32 Import(Color32 src) - { - var dst = new Color32 - { - r = src.b, - g = 0, - b = 0, - }; - - // Bake _smoothnessOrRoughness into a texture. -#if SIMPLE_CONV - dst.a = (byte)(255 - src.g * _smoothnessOrRoughness); -#else - var pixelRoughnessFactor = (src.g * _smoothnessOrRoughness) / 255.0f; // roughness - var pixelSmoothness = 1.0f - Mathf.Sqrt(pixelRoughnessFactor); - dst.a = (byte)Mathf.Clamp(pixelSmoothness * 255, 0, 255); -#endif - return dst; - } - - public Color32 Export(Color32 src) - { - - var dst = new Color32 - { - r = 0, - b = src.r, - a = 255, - }; - - // Bake divide _smoothnessOrRoughness from a texture. -#if SIMPLE_CONV - dst.g = (byte)(255 - src.a); -#else - var pixelSmoothness = (src.a * _smoothnessOrRoughness) / 255.0f; // smoothness - var pixelRoughnessFactorSqrt = (1.0f - pixelSmoothness); - var pixelRoughnessFactor = pixelRoughnessFactorSqrt * pixelRoughnessFactorSqrt; - dst.g = (byte)Mathf.Clamp(pixelRoughnessFactor * 255, 0, 255); -#endif - - return dst; - } - } -} diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionConverter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionConverter.cs deleted file mode 100644 index 2e4768f3a..000000000 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionConverter.cs +++ /dev/null @@ -1,42 +0,0 @@ -using UnityEngine; - -namespace UniGLTF -{ - public class OcclusionConverter : ITextureConverter - { - - public Texture2D GetImportTexture(Texture2D texture) - { - var converted = TextureConverter.Convert(texture, glTFTextureTypes.Occlusion, Import, null); - return converted; - } - - public Texture2D GetExportTexture(Texture2D texture) - { - var converted = TextureConverter.Convert(texture, glTFTextureTypes.Occlusion, Export, null); - return converted; - } - - public Color32 Import(Color32 src) - { - return new Color32 - { - r = 0, - g = src.r, - b = 0, - a = 255, - }; - } - - public Color32 Export(Color32 src) - { - return new Color32 - { - r = src.g, - g = 0, - b = 0, - a = 255, - }; - } - } -} \ No newline at end of file diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionConverter.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionConverter.cs.meta deleted file mode 100644 index 2a97dd0d2..000000000 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionConverter.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 268e807d3ea7f0f45882d20bf318cf8b -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionMetallicRoughnessConverter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionMetallicRoughnessConverter.cs new file mode 100644 index 000000000..2888f0f35 --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionMetallicRoughnessConverter.cs @@ -0,0 +1,65 @@ +using UnityEngine; + +namespace UniGLTF +{ + /// + /// + /// * https://github.com/dwango/UniVRM/issues/212. + /// * https://blogs.unity3d.com/jp/2016/01/25/ggx-in-unity-5-3/ + /// * https://github.com/vrm-c/UniVRM/issues/388 + /// + /// glTF = Unity + /// Occlusion: src.r -> dst.g + /// Roughness: src.g -> dst.a (bake smoothnessOrRoughness) + /// Metallic : src.b -> dst.r + /// + public class OcclusionMetallicRoughnessConverter : ITextureConverter + { + private readonly float _smoothnessOrRoughness; + + public OcclusionMetallicRoughnessConverter(float smoothnessOrRoughness) + { + _smoothnessOrRoughness = smoothnessOrRoughness; + } + + public Texture2D GetImportTexture(Texture2D texture) + { + var converted = TextureConverter.Convert(texture, glTFTextureTypes.Metallic, Import, null); + return converted; + } + + public Texture2D GetExportTexture(Texture2D texture) + { + var converted = TextureConverter.Convert(texture, glTFTextureTypes.Metallic, Export, null); + return converted; + } + + public Color32 Import(Color32 src) + { + var dst = new Color32 + { + r = src.b, // Metallic + g = src.r, // Occlusion + b = 0, // not used + // Roughness to Smoothness. Bake _smoothnessOrRoughness into a texture. + a = (byte)(255 - src.g * _smoothnessOrRoughness), + }; + + return dst; + } + + public Color32 Export(Color32 src) + { + var dst = new Color32 + { + r = src.g, // Occlusion + // Roughness from Smoothness. Bake divide _smoothnessOrRoughness from a texture. + g = (byte)(255 - src.a), + b = src.r, // Metallic + a = 255, // not used + }; + + return dst; + } + } +} diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/MetallicRoughnessConverter.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionMetallicRoughnessConverter.cs.meta similarity index 83% rename from Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/MetallicRoughnessConverter.cs.meta rename to Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionMetallicRoughnessConverter.cs.meta index 7a089ba89..1c32f4f89 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/MetallicRoughnessConverter.cs.meta +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionMetallicRoughnessConverter.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: ae1f69c4441f3a74292499f75467c057 +guid: 55d0c8cd2f5154f488e47f7bb1e6fa60 MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/GetTextureParam.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/GetTextureParam.cs index b5334756d..931d75522 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/GetTextureParam.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/GetTextureParam.cs @@ -2,14 +2,25 @@ namespace UniGLTF { + /// + /// STANDARD(Pbr) texture = occlusion + metallic + smoothness + /// public struct GetTextureParam { public const string NORMAL_PROP = "_BumpMap"; public const string NORMAL_SUFFIX = ".normal"; + public const string METALLIC_GLOSS_PROP = "_MetallicGlossMap"; - public const string METALLIC_GLOSS_SUFFIX = ".metallicRoughness"; public const string OCCLUSION_PROP = "_OcclusionMap"; - public const string OCCLUSION_SUFFIX = ".occlusion"; + public const string STANDARD_SUFFIX = ".standard"; + + public enum TextureTypes + { + sRGB, + NormalMap, + // Occlusion + Metallic + Smoothness + StandardMap, + } public static string RemoveSuffix(string src) { @@ -17,13 +28,9 @@ namespace UniGLTF { return src.Substring(0, src.Length - NORMAL_SUFFIX.Length); } - else if (src.EndsWith(METALLIC_GLOSS_SUFFIX)) + else if (src.EndsWith(STANDARD_SUFFIX)) { - return src.Substring(0, src.Length - METALLIC_GLOSS_SUFFIX.Length); - } - else if (src.EndsWith(OCCLUSION_SUFFIX)) - { - return src.Substring(0, src.Length - OCCLUSION_SUFFIX.Length); + return src.Substring(0, src.Length - STANDARD_SUFFIX.Length); } else { @@ -41,15 +48,14 @@ namespace UniGLTF { switch (TextureType) { - case METALLIC_GLOSS_PROP: return $"{m_name}{METALLIC_GLOSS_SUFFIX}"; - case OCCLUSION_PROP: return $"{m_name}{OCCLUSION_SUFFIX}"; - case NORMAL_PROP: return $"{m_name}{NORMAL_SUFFIX}"; + case TextureTypes.StandardMap: return $"{m_name}{STANDARD_SUFFIX}"; + case TextureTypes.NormalMap: return $"{m_name}{NORMAL_SUFFIX}"; default: return m_name; } } } - public readonly string TextureType; + public readonly TextureTypes TextureType; public readonly float MetallicFactor; public readonly ushort? Index0; public readonly ushort? Index1; @@ -59,11 +65,11 @@ namespace UniGLTF public readonly ushort? Index5; /// - /// この2種類は変換済みをExtract + /// この種類は RGB チャンネルの組み換えが必用 /// - public bool ExtractConverted => TextureType == OCCLUSION_PROP || TextureType == METALLIC_GLOSS_PROP; + public bool ExtractConverted => TextureType == TextureTypes.StandardMap; - public GetTextureParam(string name, string textureType, float metallicFactor, int i0, int i1, int i2, int i3, int i4, int i5) + public GetTextureParam(string name, TextureTypes textureType, float metallicFactor, int i0, int i1, int i2, int i3, int i4, int i5) { if (string.IsNullOrEmpty(name)) { @@ -81,10 +87,10 @@ namespace UniGLTF Index5 = (ushort)i5; } - public static GetTextureParam Create(glTF gltf, int textureIndex) + public static GetTextureParam CreateSRGB(glTF gltf, int textureIndex) { var name = gltf.textures[textureIndex].name; - return new GetTextureParam(name, default, default, textureIndex, default, default, default, default, default); + return new GetTextureParam(name, TextureTypes.sRGB, default, textureIndex, default, default, default, default, default); } public static GetTextureParam Create(glTF gltf, int index, string prop) @@ -95,32 +101,24 @@ namespace UniGLTF return CreateNormal(gltf, index); case OCCLUSION_PROP: - return CreateOcclusion(gltf, index); - case METALLIC_GLOSS_PROP: - return CreateMetallic(gltf, index, 1); + return CreateStandard(gltf, index, 1); default: - return Create(gltf, index); + return CreateSRGB(gltf, index); } } public static GetTextureParam CreateNormal(glTF gltf, int textureIndex) { var name = gltf.textures[textureIndex].name; - return new GetTextureParam(name, NORMAL_PROP, default, textureIndex, default, default, default, default, default); + return new GetTextureParam(name, TextureTypes.NormalMap, default, textureIndex, default, default, default, default, default); } - public static GetTextureParam CreateMetallic(glTF gltf, int textureIndex, float metallicFactor) + public static GetTextureParam CreateStandard(glTF gltf, int textureIndex, float metallicFactor) { var name = gltf.textures[textureIndex].name; - return new GetTextureParam(name, METALLIC_GLOSS_PROP, metallicFactor, textureIndex, default, default, default, default, default); - } - - public static GetTextureParam CreateOcclusion(glTF gltf, int textureIndex) - { - var name = gltf.textures[textureIndex].name; - return new GetTextureParam(name, OCCLUSION_PROP, default, textureIndex, default, default, default, default, default); + return new GetTextureParam(name, TextureTypes.StandardMap, metallicFactor, textureIndex, default, default, default, default, default); } } } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/TextureFactory.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/TextureFactory.cs index f8310b717..c70bea707 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/TextureFactory.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/TextureFactory.cs @@ -52,7 +52,7 @@ namespace UniGLTF if (param.Index0.HasValue && m_externalMap != null) { var cacheName = param.ConvertedName; - if (param.TextureType == GetTextureParam.NORMAL_PROP) + if (param.TextureType == GetTextureParam.TextureTypes.NormalMap) { cacheName = param.GltflName; if (m_textureCache.TryGetValue(cacheName, out TextureLoadInfo normalInfo)) @@ -165,7 +165,7 @@ namespace UniGLTF switch (param.TextureType) { - case GetTextureParam.NORMAL_PROP: + case GetTextureParam.TextureTypes.NormalMap: { var baseTexture = await GetOrCreateBaseTexture(awaitCaller, gltf, param.Index0.Value, false); var converted = new NormalConverter().GetImportTexture(baseTexture.Texture); @@ -175,26 +175,25 @@ namespace UniGLTF return info.Texture; } - case GetTextureParam.METALLIC_GLOSS_PROP: + case GetTextureParam.TextureTypes.StandardMap: { - // Bake roughnessFactor values into a texture. var baseTexture = await GetOrCreateBaseTexture(awaitCaller, gltf, param.Index0.Value, false); - var converted = new MetallicRoughnessConverter(param.MetallicFactor).GetImportTexture(baseTexture.Texture); + var converted = new OcclusionMetallicRoughnessConverter(param.MetallicFactor).GetImportTexture(baseTexture.Texture); converted.name = param.ConvertedName; var info = new TextureLoadInfo(converted, true, false); m_textureCache.Add(converted.name, info); return info.Texture; } - case GetTextureParam.OCCLUSION_PROP: - { - var baseTexture = await GetOrCreateBaseTexture(awaitCaller, gltf, param.Index0.Value, false); - var converted = new OcclusionConverter().GetImportTexture(baseTexture.Texture); - converted.name = param.ConvertedName; - var info = new TextureLoadInfo(converted, true, false); - m_textureCache.Add(converted.name, info); - return info.Texture; - } + // case GetTextureParam.OCCLUSION_PROP: + // { + // var baseTexture = await GetOrCreateBaseTexture(awaitCaller, gltf, param.Index0.Value, false); + // var converted = new OcclusionConverter().GetImportTexture(baseTexture.Texture); + // converted.name = param.ConvertedName; + // var info = new TextureLoadInfo(converted, true, false); + // m_textureCache.Add(converted.name, info); + // return info.Texture; + // } default: { diff --git a/Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs b/Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs index edc28a891..08a4a114f 100644 --- a/Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs +++ b/Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs @@ -14,7 +14,7 @@ namespace UniGLTF wrapMode = TextureWrapMode.Clamp, filterMode = FilterMode.Trilinear, }; - var textureManager = new TextureExportManager(new Texture[] {tex0}); + var textureManager = new TextureExportManager(new Texture[] { tex0 }); var material = new Material(Shader.Find("Standard")); material.mainTexture = tex0; @@ -39,7 +39,7 @@ namespace UniGLTF { { var smoothness = 1.0f; - var conv = new MetallicRoughnessConverter(smoothness); + var conv = new OcclusionMetallicRoughnessConverter(smoothness); Assert.That( conv.Export(new Color32(255, 255, 255, 255)), // r <- 0 : (Unused) @@ -51,7 +51,7 @@ namespace UniGLTF { var smoothness = 0.5f; - var conv = new MetallicRoughnessConverter(smoothness); + var conv = new OcclusionMetallicRoughnessConverter(smoothness); Assert.That( conv.Export(new Color32(255, 255, 255, 255)), // r <- 0 : (Unused) @@ -63,7 +63,7 @@ namespace UniGLTF { var smoothness = 0.0f; - var conv = new MetallicRoughnessConverter(smoothness); + var conv = new OcclusionMetallicRoughnessConverter(smoothness); Assert.That( conv.Export(new Color32(255, 255, 255, 255)), // r <- 0 : (Unused) @@ -79,7 +79,7 @@ namespace UniGLTF { { var roughnessFactor = 1.0f; - var conv = new MetallicRoughnessConverter(roughnessFactor); + var conv = new OcclusionMetallicRoughnessConverter(roughnessFactor); Assert.That( conv.Import(new Color32(255, 255, 255, 255)), // r <- 255 : Same metallic (src.r) @@ -91,7 +91,7 @@ namespace UniGLTF { var roughnessFactor = 1.0f; - var conv = new MetallicRoughnessConverter(roughnessFactor); + var conv = new OcclusionMetallicRoughnessConverter(roughnessFactor); Assert.That( conv.Import(new Color32(255, 63, 255, 255)), // r <- 255 : Same metallic (src.r) @@ -103,7 +103,7 @@ namespace UniGLTF { var roughnessFactor = 0.5f; - var conv = new MetallicRoughnessConverter(roughnessFactor); + var conv = new OcclusionMetallicRoughnessConverter(roughnessFactor); Assert.That( conv.Import(new Color32(255, 255, 255, 255)), // r <- 255 : Same metallic (src.r) @@ -115,7 +115,7 @@ namespace UniGLTF { var roughnessFactor = 0.0f; - var conv = new MetallicRoughnessConverter(roughnessFactor); + var conv = new OcclusionMetallicRoughnessConverter(roughnessFactor); Assert.That( conv.Import(new Color32(255, 255, 255, 255)), // r <- 255 : Same metallic (src.r) diff --git a/Assets/VRM/Runtime/IO/VRMImporterContext.cs b/Assets/VRM/Runtime/IO/VRMImporterContext.cs index 3c59f3ecd..15af9f0b8 100644 --- a/Assets/VRM/Runtime/IO/VRMImporterContext.cs +++ b/Assets/VRM/Runtime/IO/VRMImporterContext.cs @@ -292,7 +292,7 @@ namespace VRM meta.Title = gltfMeta.title; if (gltfMeta.texture >= 0) { - meta.Thumbnail = await TextureFactory.GetTextureAsync(awaitCaller, GLTF, GetTextureParam.Create(GLTF, gltfMeta.texture)); + meta.Thumbnail = await TextureFactory.GetTextureAsync(awaitCaller, GLTF, GetTextureParam.CreateSRGB(GLTF, gltfMeta.texture)); } meta.AllowedUser = gltfMeta.allowedUser; meta.ViolentUssage = gltfMeta.violentUssage; From 4413128f28bc8344f14d464f327d90182e9a45aa Mon Sep 17 00:00:00 2001 From: ousttrue Date: Fri, 12 Mar 2021 18:45:46 +0900 Subject: [PATCH 04/28] remove ITextureConverter.GetImportTexture --- .../UniGLTF/IO/TextureConverter/ITextureConverter.cs | 1 - .../OcclusionMetallicRoughnessConverter.cs | 10 +++++++--- .../UniGLTF/IO/TextureLoader/TextureFactory.cs | 2 +- Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs | 12 ++++-------- 4 files changed, 12 insertions(+), 13 deletions(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/ITextureConverter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/ITextureConverter.cs index 0117fba4a..29bca8ca8 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/ITextureConverter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/ITextureConverter.cs @@ -4,7 +4,6 @@ namespace UniGLTF { public interface ITextureConverter { - Texture2D GetImportTexture(Texture2D texture); Texture2D GetExportTexture(Texture2D texture); } } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionMetallicRoughnessConverter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionMetallicRoughnessConverter.cs index 2888f0f35..c13377de0 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionMetallicRoughnessConverter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionMetallicRoughnessConverter.cs @@ -22,9 +22,13 @@ namespace UniGLTF _smoothnessOrRoughness = smoothnessOrRoughness; } - public Texture2D GetImportTexture(Texture2D texture) + public static Texture2D GetImportTexture(Texture2D texture, float smoothnessOrRoughness) { - var converted = TextureConverter.Convert(texture, glTFTextureTypes.Metallic, Import, null); + TextureConverter.ColorConversion convert = src => + { + return Import(src, smoothnessOrRoughness); + }; + var converted = TextureConverter.Convert(texture, glTFTextureTypes.Metallic, convert, null); return converted; } @@ -34,7 +38,7 @@ namespace UniGLTF return converted; } - public Color32 Import(Color32 src) + public static Color32 Import(Color32 src, float _smoothnessOrRoughness) { var dst = new Color32 { diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/TextureFactory.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/TextureFactory.cs index c70bea707..d90a6a4af 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/TextureFactory.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/TextureFactory.cs @@ -178,7 +178,7 @@ namespace UniGLTF case GetTextureParam.TextureTypes.StandardMap: { var baseTexture = await GetOrCreateBaseTexture(awaitCaller, gltf, param.Index0.Value, false); - var converted = new OcclusionMetallicRoughnessConverter(param.MetallicFactor).GetImportTexture(baseTexture.Texture); + var converted = OcclusionMetallicRoughnessConverter.GetImportTexture(baseTexture.Texture, param.MetallicFactor); converted.name = param.ConvertedName; var info = new TextureLoadInfo(converted, true, false); m_textureCache.Add(converted.name, info); diff --git a/Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs b/Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs index 08a4a114f..a8018e826 100644 --- a/Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs +++ b/Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs @@ -79,9 +79,8 @@ namespace UniGLTF { { var roughnessFactor = 1.0f; - var conv = new OcclusionMetallicRoughnessConverter(roughnessFactor); Assert.That( - conv.Import(new Color32(255, 255, 255, 255)), + OcclusionMetallicRoughnessConverter.Import(new Color32(255, 255, 255, 255), roughnessFactor), // r <- 255 : Same metallic (src.r) // g <- 0 : (Unused) // b <- 0 : (Unused) @@ -91,9 +90,8 @@ namespace UniGLTF { var roughnessFactor = 1.0f; - var conv = new OcclusionMetallicRoughnessConverter(roughnessFactor); Assert.That( - conv.Import(new Color32(255, 63, 255, 255)), + OcclusionMetallicRoughnessConverter.Import(new Color32(255, 63, 255, 255), roughnessFactor), // r <- 255 : Same metallic (src.r) // g <- 0 : (Unused) // b <- 0 : (Unused) @@ -103,9 +101,8 @@ namespace UniGLTF { var roughnessFactor = 0.5f; - var conv = new OcclusionMetallicRoughnessConverter(roughnessFactor); Assert.That( - conv.Import(new Color32(255, 255, 255, 255)), + OcclusionMetallicRoughnessConverter.Import(new Color32(255, 255, 255, 255), roughnessFactor), // r <- 255 : Same metallic (src.r) // g <- 0 : (Unused) // b <- 0 : (Unused) @@ -115,9 +112,8 @@ namespace UniGLTF { var roughnessFactor = 0.0f; - var conv = new OcclusionMetallicRoughnessConverter(roughnessFactor); Assert.That( - conv.Import(new Color32(255, 255, 255, 255)), + OcclusionMetallicRoughnessConverter.Import(new Color32(255, 255, 255, 255), roughnessFactor), // r <- 255 : Same metallic (src.r) // g <- 0 : (Unused) // b <- 0 : (Unused) From 3d85986f2ec20f684aefeeb7976a2c3237de9001 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Fri, 12 Mar 2021 18:57:23 +0900 Subject: [PATCH 05/28] MetallicFactor and RoughnessFactor --- .../IO/MaterialLoader/PBRMaterialItem.cs | 3 +- .../OcclusionMetallicRoughnessConverter.cs | 34 ++++++++++--------- .../IO/TextureLoader/GetTextureParam.cs | 16 +++++---- .../IO/TextureLoader/TextureFactory.cs | 2 +- Assets/VRM/Runtime/IO/VRMMaterialImporter.cs | 2 +- 5 files changed, 31 insertions(+), 26 deletions(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/PBRMaterialItem.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/PBRMaterialItem.cs index e79c9997c..c2fc530fd 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/PBRMaterialItem.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/PBRMaterialItem.cs @@ -52,7 +52,8 @@ namespace UniGLTF { return GetTextureParam.CreateStandard(gltf, src.pbrMetallicRoughness.metallicRoughnessTexture.index, - src.pbrMetallicRoughness.metallicFactor); + src.pbrMetallicRoughness.metallicFactor, + src.pbrMetallicRoughness.roughnessFactor); } public static GetTextureParam NormalTexture(glTF gltf, glTFMaterial src) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionMetallicRoughnessConverter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionMetallicRoughnessConverter.cs index c13377de0..e8749cdfb 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionMetallicRoughnessConverter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionMetallicRoughnessConverter.cs @@ -4,14 +4,18 @@ namespace UniGLTF { /// /// - /// * https://github.com/dwango/UniVRM/issues/212. - /// * https://blogs.unity3d.com/jp/2016/01/25/ggx-in-unity-5-3/ - /// * https://github.com/vrm-c/UniVRM/issues/388 + /// * https://github.com/vrm-c/UniVRM/issues/781 + /// + /// Unity = glTF + /// Occlusion: unity.g = glTF.r + /// Roughness: unity.a = 1 - glTF.g * roughnessFactor + /// Metallic : unity.r = glTF.b * metallicFactor /// /// glTF = Unity - /// Occlusion: src.r -> dst.g - /// Roughness: src.g -> dst.a (bake smoothnessOrRoughness) - /// Metallic : src.b -> dst.r + /// Occlusion: glTF.r = unity.g + /// Roughness: glTF.g = 1 - unity.a * smoothness + /// Metallic : glTF.b = unity.r + /// /// public class OcclusionMetallicRoughnessConverter : ITextureConverter { @@ -22,11 +26,11 @@ namespace UniGLTF _smoothnessOrRoughness = smoothnessOrRoughness; } - public static Texture2D GetImportTexture(Texture2D texture, float smoothnessOrRoughness) + public static Texture2D GetImportTexture(Texture2D texture, float metallicFactor, float roughnessFactor) { TextureConverter.ColorConversion convert = src => { - return Import(src, smoothnessOrRoughness); + return Import(src, metallicFactor, roughnessFactor); }; var converted = TextureConverter.Convert(texture, glTFTextureTypes.Metallic, convert, null); return converted; @@ -38,15 +42,14 @@ namespace UniGLTF return converted; } - public static Color32 Import(Color32 src, float _smoothnessOrRoughness) + public static Color32 Import(Color32 src, float metallicFactor, float roughnessFactor) { var dst = new Color32 { - r = src.b, // Metallic + r = (byte)(src.b * metallicFactor), // Metallic g = src.r, // Occlusion - b = 0, // not used - // Roughness to Smoothness. Bake _smoothnessOrRoughness into a texture. - a = (byte)(255 - src.g * _smoothnessOrRoughness), + b = 0, // not used + a = (byte)(255 - src.g * roughnessFactor), // Roughness to Smoothness }; return dst; @@ -56,9 +59,8 @@ namespace UniGLTF { var dst = new Color32 { - r = src.g, // Occlusion - // Roughness from Smoothness. Bake divide _smoothnessOrRoughness from a texture. - g = (byte)(255 - src.a), + r = src.g, // Occlusion + g = (byte)(255 - src.a), // Roughness from Smoothness b = src.r, // Metallic a = 255, // not used }; diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/GetTextureParam.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/GetTextureParam.cs index 931d75522..c888366fa 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/GetTextureParam.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/GetTextureParam.cs @@ -57,6 +57,7 @@ namespace UniGLTF public readonly TextureTypes TextureType; public readonly float MetallicFactor; + public readonly float RoughnessFactor; public readonly ushort? Index0; public readonly ushort? Index1; public readonly ushort? Index2; @@ -69,7 +70,7 @@ namespace UniGLTF /// public bool ExtractConverted => TextureType == TextureTypes.StandardMap; - public GetTextureParam(string name, TextureTypes textureType, float metallicFactor, int i0, int i1, int i2, int i3, int i4, int i5) + public GetTextureParam(string name, TextureTypes textureType, float metallicFactor, float roughnessFactor, int i0, int i1, int i2, int i3, int i4, int i5) { if (string.IsNullOrEmpty(name)) { @@ -79,6 +80,7 @@ namespace UniGLTF TextureType = textureType; MetallicFactor = metallicFactor; + RoughnessFactor = roughnessFactor; Index0 = (ushort)i0; Index1 = (ushort)i1; Index2 = (ushort)i2; @@ -90,10 +92,10 @@ namespace UniGLTF public static GetTextureParam CreateSRGB(glTF gltf, int textureIndex) { var name = gltf.textures[textureIndex].name; - return new GetTextureParam(name, TextureTypes.sRGB, default, textureIndex, default, default, default, default, default); + return new GetTextureParam(name, TextureTypes.sRGB, default, default, textureIndex, default, default, default, default, default); } - public static GetTextureParam Create(glTF gltf, int index, string prop) + public static GetTextureParam Create(glTF gltf, int index, string prop, float metallicFactor, float roughnessFactor) { switch (prop) { @@ -102,7 +104,7 @@ namespace UniGLTF case OCCLUSION_PROP: case METALLIC_GLOSS_PROP: - return CreateStandard(gltf, index, 1); + return CreateStandard(gltf, index, metallicFactor, roughnessFactor); default: return CreateSRGB(gltf, index); @@ -112,13 +114,13 @@ namespace UniGLTF public static GetTextureParam CreateNormal(glTF gltf, int textureIndex) { var name = gltf.textures[textureIndex].name; - return new GetTextureParam(name, TextureTypes.NormalMap, default, textureIndex, default, default, default, default, default); + return new GetTextureParam(name, TextureTypes.NormalMap, default, default, textureIndex, default, default, default, default, default); } - public static GetTextureParam CreateStandard(glTF gltf, int textureIndex, float metallicFactor) + public static GetTextureParam CreateStandard(glTF gltf, int textureIndex, float metallicFactor, float roughnessFactor) { var name = gltf.textures[textureIndex].name; - return new GetTextureParam(name, TextureTypes.StandardMap, metallicFactor, textureIndex, default, default, default, default, default); + return new GetTextureParam(name, TextureTypes.StandardMap, metallicFactor, roughnessFactor, textureIndex, default, default, default, default, default); } } } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/TextureFactory.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/TextureFactory.cs index d90a6a4af..ff666a8dc 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/TextureFactory.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/TextureFactory.cs @@ -178,7 +178,7 @@ namespace UniGLTF case GetTextureParam.TextureTypes.StandardMap: { var baseTexture = await GetOrCreateBaseTexture(awaitCaller, gltf, param.Index0.Value, false); - var converted = OcclusionMetallicRoughnessConverter.GetImportTexture(baseTexture.Texture, param.MetallicFactor); + var converted = OcclusionMetallicRoughnessConverter.GetImportTexture(baseTexture.Texture, param.MetallicFactor, param.RoughnessFactor); converted.name = param.ConvertedName; var info = new TextureLoadInfo(converted, true, false); m_textureCache.Add(converted.name, info); diff --git a/Assets/VRM/Runtime/IO/VRMMaterialImporter.cs b/Assets/VRM/Runtime/IO/VRMMaterialImporter.cs index 28cacb2e9..6ea9985bf 100644 --- a/Assets/VRM/Runtime/IO/VRMMaterialImporter.cs +++ b/Assets/VRM/Runtime/IO/VRMMaterialImporter.cs @@ -73,7 +73,7 @@ namespace VRM } foreach (var kv in item.textureProperties) { - var param = GetTextureParam.Create(gltf, kv.Value, kv.Key); + var param = GetTextureParam.Create(gltf, kv.Value, kv.Key, 1, 1); var texture = await getTexture(awaitCaller, gltf, param); if (texture != null) { From dba9460c408a3198ae36fa814692b871a9b5d393 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Fri, 12 Mar 2021 19:37:39 +0900 Subject: [PATCH 06/28] not catch --- .../UniGLTF/ScriptedImporter/GlbScriptedImporter.cs | 9 +-------- .../UniGLTF/ScriptedImporter/GltfScriptedImporter.cs | 9 +-------- 2 files changed, 2 insertions(+), 16 deletions(-) diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/GlbScriptedImporter.cs b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/GlbScriptedImporter.cs index ae92a0387..31d2ad748 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/GlbScriptedImporter.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/GlbScriptedImporter.cs @@ -12,14 +12,7 @@ namespace UniGLTF public override void OnImportAsset(AssetImportContext ctx) { - try - { - ScriptedImporterImpl.Import(this, ctx, m_reverseAxis); - } - catch (System.Exception ex) - { - Debug.LogError(ex); - } + ScriptedImporterImpl.Import(this, ctx, m_reverseAxis); } } } diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/GltfScriptedImporter.cs b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/GltfScriptedImporter.cs index fe4fc605b..89297f485 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/GltfScriptedImporter.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/GltfScriptedImporter.cs @@ -12,14 +12,7 @@ namespace UniGLTF public override void OnImportAsset(AssetImportContext ctx) { - try - { - ScriptedImporterImpl.Import(this, ctx, m_reverseAxis); - } - catch (System.Exception ex) - { - Debug.LogError(ex); - } + ScriptedImporterImpl.Import(this, ctx, m_reverseAxis); } } } From d2e560aca4022b495b1949932f86e052d9a7ae08 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Fri, 12 Mar 2021 19:39:25 +0900 Subject: [PATCH 07/28] OcclusionMetallicRoughnessConverter.Convert #781 --- .../IO/MaterialLoader/PBRMaterialItem.cs | 14 +++- .../OcclusionMetallicRoughnessConverter.cs | 75 ++++++++++++++++--- .../IO/TextureLoader/GetTextureParam.cs | 30 +++++--- .../IO/TextureLoader/TextureFactory.cs | 17 ++--- Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs | 8 +- 5 files changed, 105 insertions(+), 39 deletions(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/PBRMaterialItem.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/PBRMaterialItem.cs index c2fc530fd..38968b4e6 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/PBRMaterialItem.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/PBRMaterialItem.cs @@ -50,10 +50,18 @@ namespace UniGLTF public static GetTextureParam StandardTexture(glTF gltf, glTFMaterial src) { + var metallicFactor = 1.0f; + var roughnessFactor = 1.0f; + if (src.pbrMetallicRoughness != null) + { + metallicFactor = src.pbrMetallicRoughness.metallicFactor; + roughnessFactor = src.pbrMetallicRoughness.roughnessFactor; + } return GetTextureParam.CreateStandard(gltf, - src.pbrMetallicRoughness.metallicRoughnessTexture.index, - src.pbrMetallicRoughness.metallicFactor, - src.pbrMetallicRoughness.roughnessFactor); + src.pbrMetallicRoughness?.metallicRoughnessTexture?.index, + src.occlusionTexture?.index, + metallicFactor, + roughnessFactor); } public static GetTextureParam NormalTexture(glTF gltf, glTFMaterial src) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionMetallicRoughnessConverter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionMetallicRoughnessConverter.cs index e8749cdfb..cd4b5c0c5 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionMetallicRoughnessConverter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionMetallicRoughnessConverter.cs @@ -1,3 +1,5 @@ +using System; +using System.Linq; using UnityEngine; namespace UniGLTF @@ -26,14 +28,67 @@ namespace UniGLTF _smoothnessOrRoughness = smoothnessOrRoughness; } - public static Texture2D GetImportTexture(Texture2D texture, float metallicFactor, float roughnessFactor) + public delegate Color32 ColorConversion(Color32 metallicRoughness, Color32 occlusion); + + public static Texture2D Convert(Texture2D metallicRoughnessTexture, Texture2D occlusionTexture, Material convertMaterial, + float metallicFactor, float roughnessFactor) { - TextureConverter.ColorConversion convert = src => + if (metallicRoughnessTexture != null && occlusionTexture != null) { - return Import(src, metallicFactor, roughnessFactor); - }; - var converted = TextureConverter.Convert(texture, glTFTextureTypes.Metallic, convert, null); - return converted; + if (metallicRoughnessTexture != occlusionTexture) + { + var copyMetallicRoughness = TextureConverter.CopyTexture(metallicRoughnessTexture, RenderTextureReadWrite.Linear, convertMaterial); + var metallicRoughnessPixels = copyMetallicRoughness.GetPixels32(); + var copyOcclusion = TextureConverter.CopyTexture(occlusionTexture, RenderTextureReadWrite.Linear, convertMaterial); + var occlusionPixels = copyOcclusion.GetPixels32(); + if (metallicRoughnessPixels.Length != occlusionPixels.Length) + { + throw new NotImplementedException(); + } + for (int i = 0; i < metallicRoughnessPixels.Length; ++i) + { + metallicRoughnessPixels[i] = Import(metallicRoughnessPixels[i], metallicFactor, roughnessFactor, occlusionPixels[i]); + } + copyMetallicRoughness.SetPixels32(metallicRoughnessPixels); + copyMetallicRoughness.Apply(); + copyMetallicRoughness.name = metallicRoughnessTexture.name; + return copyMetallicRoughness; + } + else + { + var copyMetallicRoughness = TextureConverter.CopyTexture(metallicRoughnessTexture, RenderTextureReadWrite.Linear, convertMaterial); + var metallicRoughnessPixels = copyMetallicRoughness.GetPixels32(); + for (int i = 0; i < metallicRoughnessPixels.Length; ++i) + { + metallicRoughnessPixels[i] = Import(metallicRoughnessPixels[i], metallicFactor, roughnessFactor, metallicRoughnessPixels[i]); + } + copyMetallicRoughness.SetPixels32(metallicRoughnessPixels); + copyMetallicRoughness.Apply(); + copyMetallicRoughness.name = metallicRoughnessTexture.name; + return copyMetallicRoughness; + } + } + else if (metallicRoughnessTexture != null) + { + var copyTexture = TextureConverter.CopyTexture(metallicRoughnessTexture, RenderTextureReadWrite.Linear, convertMaterial); + copyTexture.SetPixels32(copyTexture.GetPixels32().Select(x => Import(x, metallicFactor, roughnessFactor, default)).ToArray()); + copyTexture.Apply(); + copyTexture.name = metallicRoughnessTexture.name; + return copyTexture; + } + else if (occlusionTexture != null) + { + throw new NotImplementedException("occlusion only"); + } + else + { + throw new ArgumentNullException("no texture"); + } + } + + public static Texture2D GetImportTexture(Texture2D metallicRoughnessTexture, float metallicFactor, float roughnessFactor, Texture2D occlusionTexture) + { + return Convert(metallicRoughnessTexture, occlusionTexture, null, metallicFactor, roughnessFactor); } public Texture2D GetExportTexture(Texture2D texture) @@ -42,14 +97,14 @@ namespace UniGLTF return converted; } - public static Color32 Import(Color32 src, float metallicFactor, float roughnessFactor) + public static Color32 Import(Color32 metallicRoughness, float metallicFactor, float roughnessFactor, Color32 occlusion) { var dst = new Color32 { - r = (byte)(src.b * metallicFactor), // Metallic - g = src.r, // Occlusion + r = (byte)(metallicRoughness.b * metallicFactor), // Metallic + g = occlusion.r, // Occlusion b = 0, // not used - a = (byte)(255 - src.g * roughnessFactor), // Roughness to Smoothness + a = (byte)(255 - metallicRoughness.g * roughnessFactor), // Roughness to Smoothness }; return dst; diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/GetTextureParam.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/GetTextureParam.cs index c888366fa..eb4e5264a 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/GetTextureParam.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/GetTextureParam.cs @@ -70,7 +70,7 @@ namespace UniGLTF /// public bool ExtractConverted => TextureType == TextureTypes.StandardMap; - public GetTextureParam(string name, TextureTypes textureType, float metallicFactor, float roughnessFactor, int i0, int i1, int i2, int i3, int i4, int i5) + public GetTextureParam(string name, TextureTypes textureType, float metallicFactor, float roughnessFactor, int? i0, int? i1, int? i2, int? i3, int? i4, int? i5) { if (string.IsNullOrEmpty(name)) { @@ -81,12 +81,12 @@ namespace UniGLTF TextureType = textureType; MetallicFactor = metallicFactor; RoughnessFactor = roughnessFactor; - Index0 = (ushort)i0; - Index1 = (ushort)i1; - Index2 = (ushort)i2; - Index3 = (ushort)i3; - Index4 = (ushort)i4; - Index5 = (ushort)i5; + Index0 = (ushort?)i0; + Index1 = (ushort?)i1; + Index2 = (ushort?)i2; + Index3 = (ushort?)i3; + Index4 = (ushort?)i4; + Index5 = (ushort?)i5; } public static GetTextureParam CreateSRGB(glTF gltf, int textureIndex) @@ -104,7 +104,7 @@ namespace UniGLTF case OCCLUSION_PROP: case METALLIC_GLOSS_PROP: - return CreateStandard(gltf, index, metallicFactor, roughnessFactor); + return CreateStandard(gltf, index, default, metallicFactor, roughnessFactor); default: return CreateSRGB(gltf, index); @@ -117,10 +117,18 @@ namespace UniGLTF return new GetTextureParam(name, TextureTypes.NormalMap, default, default, textureIndex, default, default, default, default, default); } - public static GetTextureParam CreateStandard(glTF gltf, int textureIndex, float metallicFactor, float roughnessFactor) + public static GetTextureParam CreateStandard(glTF gltf, int? metallicRoughnessTextureIndex, int? occlusionTextureIndex, float metallicFactor, float roughnessFactor) { - var name = gltf.textures[textureIndex].name; - return new GetTextureParam(name, TextureTypes.StandardMap, metallicFactor, roughnessFactor, textureIndex, default, default, default, default, default); + string name = default; + if (metallicRoughnessTextureIndex.HasValue) + { + name = gltf.textures[metallicRoughnessTextureIndex.Value].name; + } + else if (occlusionTextureIndex.HasValue) + { + name = gltf.textures[occlusionTextureIndex.Value].name; + } + return new GetTextureParam(name, TextureTypes.StandardMap, metallicFactor, roughnessFactor, metallicRoughnessTextureIndex, occlusionTextureIndex, default, default, default, default); } } } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/TextureFactory.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/TextureFactory.cs index ff666a8dc..87e922785 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/TextureFactory.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/TextureFactory.cs @@ -178,23 +178,18 @@ namespace UniGLTF case GetTextureParam.TextureTypes.StandardMap: { var baseTexture = await GetOrCreateBaseTexture(awaitCaller, gltf, param.Index0.Value, false); - var converted = OcclusionMetallicRoughnessConverter.GetImportTexture(baseTexture.Texture, param.MetallicFactor, param.RoughnessFactor); + TextureLoadInfo occlusionBaseTexture = default; + if (param.Index1.HasValue) + { + occlusionBaseTexture = await GetOrCreateBaseTexture(awaitCaller, gltf, param.Index1.Value, false); + } + var converted = OcclusionMetallicRoughnessConverter.GetImportTexture(baseTexture.Texture, param.MetallicFactor, param.RoughnessFactor, occlusionBaseTexture.Texture); converted.name = param.ConvertedName; var info = new TextureLoadInfo(converted, true, false); m_textureCache.Add(converted.name, info); return info.Texture; } - // case GetTextureParam.OCCLUSION_PROP: - // { - // var baseTexture = await GetOrCreateBaseTexture(awaitCaller, gltf, param.Index0.Value, false); - // var converted = new OcclusionConverter().GetImportTexture(baseTexture.Texture); - // converted.name = param.ConvertedName; - // var info = new TextureLoadInfo(converted, true, false); - // m_textureCache.Add(converted.name, info); - // return info.Texture; - // } - default: { var baseTexture = await GetOrCreateBaseTexture(awaitCaller, gltf, param.Index0.Value, true); diff --git a/Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs b/Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs index a8018e826..01f6cba6c 100644 --- a/Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs +++ b/Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs @@ -80,7 +80,7 @@ namespace UniGLTF { var roughnessFactor = 1.0f; Assert.That( - OcclusionMetallicRoughnessConverter.Import(new Color32(255, 255, 255, 255), roughnessFactor), + OcclusionMetallicRoughnessConverter.Import(new Color32(255, 255, 255, 255), 1.0f, roughnessFactor, default), // r <- 255 : Same metallic (src.r) // g <- 0 : (Unused) // b <- 0 : (Unused) @@ -91,7 +91,7 @@ namespace UniGLTF { var roughnessFactor = 1.0f; Assert.That( - OcclusionMetallicRoughnessConverter.Import(new Color32(255, 63, 255, 255), roughnessFactor), + OcclusionMetallicRoughnessConverter.Import(new Color32(255, 63, 255, 255), 1.0f, roughnessFactor, default), // r <- 255 : Same metallic (src.r) // g <- 0 : (Unused) // b <- 0 : (Unused) @@ -102,7 +102,7 @@ namespace UniGLTF { var roughnessFactor = 0.5f; Assert.That( - OcclusionMetallicRoughnessConverter.Import(new Color32(255, 255, 255, 255), roughnessFactor), + OcclusionMetallicRoughnessConverter.Import(new Color32(255, 255, 255, 255), 1.0f, roughnessFactor, default), // r <- 255 : Same metallic (src.r) // g <- 0 : (Unused) // b <- 0 : (Unused) @@ -113,7 +113,7 @@ namespace UniGLTF { var roughnessFactor = 0.0f; Assert.That( - OcclusionMetallicRoughnessConverter.Import(new Color32(255, 255, 255, 255), roughnessFactor), + OcclusionMetallicRoughnessConverter.Import(new Color32(255, 255, 255, 255), 1.0f, roughnessFactor, default), // r <- 255 : Same metallic (src.r) // g <- 0 : (Unused) // b <- 0 : (Unused) From fd12e0d1f52372d9be9270b277973a3f227983c2 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Fri, 12 Mar 2021 20:35:07 +0900 Subject: [PATCH 08/28] remove ITextureConverter. static class OcclusionMetallicRoughnessConverter --- .../Runtime/UniGLTF/IO/MaterialExporter.cs | 32 ++---------- .../IO/TextureConverter/ITextureConverter.cs | 9 ---- .../ITextureConverter.cs.meta | 11 ---- .../IO/TextureConverter/NormalConverter.cs | 2 +- .../OcclusionMetallicRoughnessConverter.cs | 52 +++++++------------ .../UniGLTF/IO/TextureExportManager.cs | 7 +-- .../IO/TextureLoader/TextureFactory.cs | 2 +- Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs | 17 +++--- 8 files changed, 36 insertions(+), 96 deletions(-) delete mode 100644 Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/ITextureConverter.cs delete mode 100644 Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/ITextureConverter.cs.meta diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialExporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialExporter.cs index ee06e8c59..cb444690b 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialExporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialExporter.cs @@ -1,6 +1,4 @@ -using System.Collections.Generic; -using UniGLTF.UniUnlit; -using UniJSON; +using UniGLTF.UniUnlit; using UnityEngine; @@ -67,15 +65,14 @@ namespace UniGLTF int index = -1; if (m.HasProperty("_MetallicGlossMap")) { - float smoothness = 0.0f; + float smoothness = 1.0f; if (m.HasProperty("_GlossMapScale")) { smoothness = m.GetFloat("_GlossMapScale"); } // Bake smoothness values into a texture. - var converter = new OcclusionMetallicRoughnessConverter(smoothness); - index = textureManager.ConvertAndGetIndex(m.GetTexture("_MetallicGlossMap"), converter); + index = textureManager.ConvertAndGetIndex(m.GetTexture("_MetallicGlossMap"), x => OcclusionMetallicRoughnessConverter.GetExportTexture(x, smoothness)); if (index != -1) { material.pbrMetallicRoughness.metallicRoughnessTexture = @@ -107,33 +104,12 @@ namespace UniGLTF } } } - // static void Export_Occlusion(Material m, TextureExportManager textureManager, glTFMaterial material) - // { - // if (m.HasProperty("_OcclusionMap")) - // { - // var index = textureManager.ConvertAndGetIndex(m.GetTexture("_OcclusionMap"), new OcclusionConverter()); - // if (index != -1) - // { - // material.occlusionTexture = new glTFMaterialOcclusionTextureInfo() - // { - // index = index, - // }; - - // Export_MainTextureTransform(m, material.occlusionTexture); - // } - - // if (index != -1 && m.HasProperty("_OcclusionStrength")) - // { - // material.occlusionTexture.strength = m.GetFloat("_OcclusionStrength"); - // } - // } - // } static void Export_Normal(Material m, TextureExportManager textureManager, glTFMaterial material) { if (m.HasProperty("_BumpMap")) { - var index = textureManager.ConvertAndGetIndex(m.GetTexture("_BumpMap"), new NormalConverter()); + var index = textureManager.ConvertAndGetIndex(m.GetTexture("_BumpMap"), new NormalConverter().GetExportTexture); if (index != -1) { material.normalTexture = new glTFMaterialNormalTextureInfo() diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/ITextureConverter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/ITextureConverter.cs deleted file mode 100644 index 29bca8ca8..000000000 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/ITextureConverter.cs +++ /dev/null @@ -1,9 +0,0 @@ -using UnityEngine; - -namespace UniGLTF -{ - public interface ITextureConverter - { - Texture2D GetExportTexture(Texture2D texture); - } -} diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/ITextureConverter.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/ITextureConverter.cs.meta deleted file mode 100644 index 2baf4c012..000000000 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/ITextureConverter.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 55c6531d46a98a845b19cbe1f938eab1 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/NormalConverter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/NormalConverter.cs index 4ccb6e2e1..31eba372d 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/NormalConverter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/NormalConverter.cs @@ -2,7 +2,7 @@ using UnityEngine; namespace UniGLTF { - public class NormalConverter : ITextureConverter + public class NormalConverter { private Material m_decoder; private Material GetDecoder() diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionMetallicRoughnessConverter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionMetallicRoughnessConverter.cs index cd4b5c0c5..29e2cc461 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionMetallicRoughnessConverter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionMetallicRoughnessConverter.cs @@ -19,27 +19,18 @@ namespace UniGLTF /// Metallic : glTF.b = unity.r /// /// - public class OcclusionMetallicRoughnessConverter : ITextureConverter + public static class OcclusionMetallicRoughnessConverter { - private readonly float _smoothnessOrRoughness; - - public OcclusionMetallicRoughnessConverter(float smoothnessOrRoughness) - { - _smoothnessOrRoughness = smoothnessOrRoughness; - } - - public delegate Color32 ColorConversion(Color32 metallicRoughness, Color32 occlusion); - - public static Texture2D Convert(Texture2D metallicRoughnessTexture, Texture2D occlusionTexture, Material convertMaterial, - float metallicFactor, float roughnessFactor) + public static Texture2D Import(Texture2D metallicRoughnessTexture, + float metallicFactor, float roughnessFactor, Texture2D occlusionTexture) { if (metallicRoughnessTexture != null && occlusionTexture != null) { if (metallicRoughnessTexture != occlusionTexture) { - var copyMetallicRoughness = TextureConverter.CopyTexture(metallicRoughnessTexture, RenderTextureReadWrite.Linear, convertMaterial); + var copyMetallicRoughness = TextureConverter.CopyTexture(metallicRoughnessTexture, RenderTextureReadWrite.Linear, null); var metallicRoughnessPixels = copyMetallicRoughness.GetPixels32(); - var copyOcclusion = TextureConverter.CopyTexture(occlusionTexture, RenderTextureReadWrite.Linear, convertMaterial); + var copyOcclusion = TextureConverter.CopyTexture(occlusionTexture, RenderTextureReadWrite.Linear, null); var occlusionPixels = copyOcclusion.GetPixels32(); if (metallicRoughnessPixels.Length != occlusionPixels.Length) { @@ -47,7 +38,7 @@ namespace UniGLTF } for (int i = 0; i < metallicRoughnessPixels.Length; ++i) { - metallicRoughnessPixels[i] = Import(metallicRoughnessPixels[i], metallicFactor, roughnessFactor, occlusionPixels[i]); + metallicRoughnessPixels[i] = ImportPixel(metallicRoughnessPixels[i], metallicFactor, roughnessFactor, occlusionPixels[i]); } copyMetallicRoughness.SetPixels32(metallicRoughnessPixels); copyMetallicRoughness.Apply(); @@ -56,11 +47,11 @@ namespace UniGLTF } else { - var copyMetallicRoughness = TextureConverter.CopyTexture(metallicRoughnessTexture, RenderTextureReadWrite.Linear, convertMaterial); + var copyMetallicRoughness = TextureConverter.CopyTexture(metallicRoughnessTexture, RenderTextureReadWrite.Linear, null); var metallicRoughnessPixels = copyMetallicRoughness.GetPixels32(); for (int i = 0; i < metallicRoughnessPixels.Length; ++i) { - metallicRoughnessPixels[i] = Import(metallicRoughnessPixels[i], metallicFactor, roughnessFactor, metallicRoughnessPixels[i]); + metallicRoughnessPixels[i] = ImportPixel(metallicRoughnessPixels[i], metallicFactor, roughnessFactor, metallicRoughnessPixels[i]); } copyMetallicRoughness.SetPixels32(metallicRoughnessPixels); copyMetallicRoughness.Apply(); @@ -70,8 +61,8 @@ namespace UniGLTF } else if (metallicRoughnessTexture != null) { - var copyTexture = TextureConverter.CopyTexture(metallicRoughnessTexture, RenderTextureReadWrite.Linear, convertMaterial); - copyTexture.SetPixels32(copyTexture.GetPixels32().Select(x => Import(x, metallicFactor, roughnessFactor, default)).ToArray()); + var copyTexture = TextureConverter.CopyTexture(metallicRoughnessTexture, RenderTextureReadWrite.Linear, null); + copyTexture.SetPixels32(copyTexture.GetPixels32().Select(x => ImportPixel(x, metallicFactor, roughnessFactor, default)).ToArray()); copyTexture.Apply(); copyTexture.name = metallicRoughnessTexture.name; return copyTexture; @@ -86,18 +77,7 @@ namespace UniGLTF } } - public static Texture2D GetImportTexture(Texture2D metallicRoughnessTexture, float metallicFactor, float roughnessFactor, Texture2D occlusionTexture) - { - return Convert(metallicRoughnessTexture, occlusionTexture, null, metallicFactor, roughnessFactor); - } - - public Texture2D GetExportTexture(Texture2D texture) - { - var converted = TextureConverter.Convert(texture, glTFTextureTypes.Metallic, Export, null); - return converted; - } - - public static Color32 Import(Color32 metallicRoughness, float metallicFactor, float roughnessFactor, Color32 occlusion) + public static Color32 ImportPixel(Color32 metallicRoughness, float metallicFactor, float roughnessFactor, Color32 occlusion) { var dst = new Color32 { @@ -110,12 +90,18 @@ namespace UniGLTF return dst; } - public Color32 Export(Color32 src) + public static Texture2D GetExportTexture(Texture2D texture, float smoothness) + { + var converted = TextureConverter.Convert(texture, glTFTextureTypes.Metallic, x => Export(x, smoothness), null); + return converted; + } + + public static Color32 Export(Color32 src, float smoothness) { var dst = new Color32 { r = src.g, // Occlusion - g = (byte)(255 - src.a), // Roughness from Smoothness + g = (byte)(255 - src.a * smoothness), // Roughness from Smoothness b = src.r, // Metallic a = 255, // not used }; diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureExportManager.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureExportManager.cs index f8208be4b..3b1ad9829 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureExportManager.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureExportManager.cs @@ -1,4 +1,5 @@ -using System.Collections; +using System; +using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; @@ -70,7 +71,7 @@ namespace UniGLTF return index; } - public int ConvertAndGetIndex(Texture texture, ITextureConverter converter) + public int ConvertAndGetIndex(Texture texture, Func converter) { if (texture == null) { @@ -84,7 +85,7 @@ namespace UniGLTF return -1; } - m_exportTextures[index] = converter.GetExportTexture(texture as Texture2D); + m_exportTextures[index] = converter(texture as Texture2D); return index; } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/TextureFactory.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/TextureFactory.cs index 87e922785..2d496e2d3 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/TextureFactory.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/TextureFactory.cs @@ -183,7 +183,7 @@ namespace UniGLTF { occlusionBaseTexture = await GetOrCreateBaseTexture(awaitCaller, gltf, param.Index1.Value, false); } - var converted = OcclusionMetallicRoughnessConverter.GetImportTexture(baseTexture.Texture, param.MetallicFactor, param.RoughnessFactor, occlusionBaseTexture.Texture); + var converted = OcclusionMetallicRoughnessConverter.Import(baseTexture.Texture, param.MetallicFactor, param.RoughnessFactor, occlusionBaseTexture.Texture); converted.name = param.ConvertedName; var info = new TextureLoadInfo(converted, true, false); m_textureCache.Add(converted.name, info); diff --git a/Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs b/Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs index 01f6cba6c..ba559e61a 100644 --- a/Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs +++ b/Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs @@ -39,9 +39,8 @@ namespace UniGLTF { { var smoothness = 1.0f; - var conv = new OcclusionMetallicRoughnessConverter(smoothness); Assert.That( - conv.Export(new Color32(255, 255, 255, 255)), + OcclusionMetallicRoughnessConverter.Export(new Color32(255, 255, 255, 255), smoothness), // r <- 0 : (Unused) // g <- 0 : ((1 - src.a(as float) * smoothness) ^ 2)(as uint8) // b <- 255 : Same metallic (src.r) @@ -51,9 +50,8 @@ namespace UniGLTF { var smoothness = 0.5f; - var conv = new OcclusionMetallicRoughnessConverter(smoothness); Assert.That( - conv.Export(new Color32(255, 255, 255, 255)), + OcclusionMetallicRoughnessConverter.Export(new Color32(255, 255, 255, 255), smoothness), // r <- 0 : (Unused) // g <- 63 : ((1 - src.a(as float) * smoothness) ^ 2)(as uint8) // b <- 255 : Same metallic (src.r) @@ -63,9 +61,8 @@ namespace UniGLTF { var smoothness = 0.0f; - var conv = new OcclusionMetallicRoughnessConverter(smoothness); Assert.That( - conv.Export(new Color32(255, 255, 255, 255)), + OcclusionMetallicRoughnessConverter.Export(new Color32(255, 255, 255, 255), smoothness), // r <- 0 : (Unused) // g <- 255 : ((1 - src.a(as float) * smoothness) ^ 2)(as uint8) // b <- 255 : Same metallic (src.r) @@ -80,7 +77,7 @@ namespace UniGLTF { var roughnessFactor = 1.0f; Assert.That( - OcclusionMetallicRoughnessConverter.Import(new Color32(255, 255, 255, 255), 1.0f, roughnessFactor, default), + OcclusionMetallicRoughnessConverter.ImportPixel(new Color32(255, 255, 255, 255), 1.0f, roughnessFactor, default), // r <- 255 : Same metallic (src.r) // g <- 0 : (Unused) // b <- 0 : (Unused) @@ -91,7 +88,7 @@ namespace UniGLTF { var roughnessFactor = 1.0f; Assert.That( - OcclusionMetallicRoughnessConverter.Import(new Color32(255, 63, 255, 255), 1.0f, roughnessFactor, default), + OcclusionMetallicRoughnessConverter.ImportPixel(new Color32(255, 63, 255, 255), 1.0f, roughnessFactor, default), // r <- 255 : Same metallic (src.r) // g <- 0 : (Unused) // b <- 0 : (Unused) @@ -102,7 +99,7 @@ namespace UniGLTF { var roughnessFactor = 0.5f; Assert.That( - OcclusionMetallicRoughnessConverter.Import(new Color32(255, 255, 255, 255), 1.0f, roughnessFactor, default), + OcclusionMetallicRoughnessConverter.ImportPixel(new Color32(255, 255, 255, 255), 1.0f, roughnessFactor, default), // r <- 255 : Same metallic (src.r) // g <- 0 : (Unused) // b <- 0 : (Unused) @@ -113,7 +110,7 @@ namespace UniGLTF { var roughnessFactor = 0.0f; Assert.That( - OcclusionMetallicRoughnessConverter.Import(new Color32(255, 255, 255, 255), 1.0f, roughnessFactor, default), + OcclusionMetallicRoughnessConverter.ImportPixel(new Color32(255, 255, 255, 255), 1.0f, roughnessFactor, default), // r <- 255 : Same metallic (src.r) // g <- 0 : (Unused) // b <- 0 : (Unused) From 07afd49193c6b8cb4209e5e3074b0b6e3387404c Mon Sep 17 00:00:00 2001 From: ousttrue Date: Fri, 12 Mar 2021 20:39:17 +0900 Subject: [PATCH 09/28] static class NormalConverter --- .../ScriptedImporter/EditorMaterial.cs | 4 +- .../Runtime/UniGLTF/IO/MaterialExporter.cs | 2 +- .../IO/TextureConverter/NormalConverter.cs | 43 ++++++++++--------- .../IO/TextureLoader/TextureFactory.cs | 2 +- 4 files changed, 26 insertions(+), 25 deletions(-) diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/EditorMaterial.cs b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/EditorMaterial.cs index 0ad9a47bc..1d977f8dd 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/EditorMaterial.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/EditorMaterial.cs @@ -25,8 +25,8 @@ namespace UniGLTF } } - static bool s_foldMaterials; - static bool s_foldTextures; + static bool s_foldMaterials = true; + static bool s_foldTextures = true; public static void OnGUIMaterial(ScriptedImporter importer, GltfParser parser) { diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialExporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialExporter.cs index cb444690b..190231c35 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialExporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialExporter.cs @@ -109,7 +109,7 @@ namespace UniGLTF { if (m.HasProperty("_BumpMap")) { - var index = textureManager.ConvertAndGetIndex(m.GetTexture("_BumpMap"), new NormalConverter().GetExportTexture); + var index = textureManager.ConvertAndGetIndex(m.GetTexture("_BumpMap"), NormalConverter.Export); if (index != -1) { material.normalTexture = new glTFMaterialNormalTextureInfo() diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/NormalConverter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/NormalConverter.cs index 31eba372d..29a78c695 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/NormalConverter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/NormalConverter.cs @@ -2,45 +2,46 @@ using UnityEngine; namespace UniGLTF { - public class NormalConverter + public static class NormalConverter { - private Material m_decoder; - private Material GetDecoder() + private static Material m_decoder; + private static Material Decoder { - if (m_decoder == null) + get { - m_decoder = new Material(Shader.Find("UniGLTF/NormalMapDecoder")); + if (m_decoder == null) + { + m_decoder = new Material(Shader.Find("UniGLTF/NormalMapDecoder")); + } + return m_decoder; } - return m_decoder; } - private Material m_encoder; - private Material GetEncoder() + private static Material m_encoder; + private static Material Encoder { - if (m_encoder == null) + get { - m_encoder = new Material(Shader.Find("UniGLTF/NormalMapEncoder")); + if (m_encoder == null) + { + m_encoder = new Material(Shader.Find("UniGLTF/NormalMapEncoder")); + } + return m_encoder; } - return m_encoder; } // GLTF data to Unity texture // ConvertToNormalValueFromRawColorWhenCompressionIsRequired - public Texture2D GetImportTexture(Texture2D texture) + public static Texture2D Import(Texture2D texture) { - var mat = GetEncoder(); - var converted = TextureConverter.Convert(texture, glTFTextureTypes.Normal, null, mat); - return converted; + return TextureConverter.Convert(texture, glTFTextureTypes.Normal, null, Encoder); } // Unity texture to GLTF data // ConvertToRawColorWhenNormalValueIsCompressed - public Texture2D GetExportTexture(Texture2D texture) + public static Texture2D Export(Texture2D texture) { - var mat = GetDecoder(); - var converted = TextureConverter.Convert(texture, glTFTextureTypes.Normal, null, mat); - return converted; + return TextureConverter.Convert(texture, glTFTextureTypes.Normal, null, Decoder); } } - -} \ No newline at end of file +} diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/TextureFactory.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/TextureFactory.cs index 2d496e2d3..af060fa95 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/TextureFactory.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/TextureFactory.cs @@ -168,7 +168,7 @@ namespace UniGLTF case GetTextureParam.TextureTypes.NormalMap: { var baseTexture = await GetOrCreateBaseTexture(awaitCaller, gltf, param.Index0.Value, false); - var converted = new NormalConverter().GetImportTexture(baseTexture.Texture); + var converted = NormalConverter.Import(baseTexture.Texture); converted.name = param.ConvertedName; var info = new TextureLoadInfo(converted, true, false); m_textureCache.Add(converted.name, info); From fd2650f86f4837672f0cace943094db47321f1fc Mon Sep 17 00:00:00 2001 From: ousttrue Date: Fri, 12 Mar 2021 20:40:01 +0900 Subject: [PATCH 10/28] rename --- .../TextureConverter/OcclusionMetallicRoughnessConverter.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionMetallicRoughnessConverter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionMetallicRoughnessConverter.cs index 29e2cc461..2e27b6105 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionMetallicRoughnessConverter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionMetallicRoughnessConverter.cs @@ -90,13 +90,13 @@ namespace UniGLTF return dst; } - public static Texture2D GetExportTexture(Texture2D texture, float smoothness) + public static Texture2D Export(Texture2D texture, float smoothness) { - var converted = TextureConverter.Convert(texture, glTFTextureTypes.Metallic, x => Export(x, smoothness), null); + var converted = TextureConverter.Convert(texture, glTFTextureTypes.Metallic, x => ExportPixel(x, smoothness), null); return converted; } - public static Color32 Export(Color32 src, float smoothness) + public static Color32 ExportPixel(Color32 src, float smoothness) { var dst = new Color32 { From 59d84cb78b2c92f4da730ba2ca3e002d5ae8b2a4 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Fri, 12 Mar 2021 21:14:36 +0900 Subject: [PATCH 11/28] glTFTextureTypes, Export_PBR --- .../Editor/UniGLTF/AssetTextureLoader.cs | 8 +-- .../Runtime/UniGLTF/Format/glTFMaterial.cs | 27 ++----- .../Runtime/UniGLTF/IO/MaterialExporter.cs | 70 ++++++++++++++----- .../IO/TextureConverter/NormalConverter.cs | 2 +- .../OcclusionMetallicRoughnessConverter.cs | 4 +- .../IO/TextureConverter/TextureConverter.cs | 2 +- .../UniGLTF/IO/TextureExportManager.cs | 4 +- .../UniGLTF/Runtime/UniGLTF/IO/TextureIO.cs | 48 ++++++++----- .../IO/TextureLoader/GltfTextureLoader.cs | 10 ++- Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs | 6 +- Assets/VRM/Runtime/IO/VRMExporter.cs | 4 +- 11 files changed, 108 insertions(+), 77 deletions(-) diff --git a/Assets/UniGLTF/Editor/UniGLTF/AssetTextureLoader.cs b/Assets/UniGLTF/Editor/UniGLTF/AssetTextureLoader.cs index caf03659d..818307d5d 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/AssetTextureLoader.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/AssetTextureLoader.cs @@ -8,10 +8,7 @@ namespace UniGLTF public static Task LoadTaskAsync(UnityPath m_assetPath, glTF gltf, int textureIndex) { - var textureType = TextureIO.GetglTFTextureType(gltf, textureIndex); - var colorSpace = TextureIO.GetColorSpace(textureType); - var isLinear = colorSpace == RenderTextureReadWrite.Linear; - var sampler = gltf.GetSamplerFromTextureIndex(textureIndex); + var colorSpace = TextureIO.GetColorSpace(gltf, textureIndex); // // texture from assets @@ -25,7 +22,7 @@ namespace UniGLTF else { importer.maxTextureSize = 8192; - importer.sRGBTexture = !isLinear; + importer.sRGBTexture = colorSpace == RenderTextureReadWrite.sRGB; importer.SaveAndReimport(); } @@ -50,6 +47,7 @@ namespace UniGLTF importer.SaveAndReimport(); } + var sampler = gltf.GetSamplerFromTextureIndex(textureIndex); if (sampler != null) { TextureSamplerUtil.SetSampler(Texture, sampler); diff --git a/Assets/UniGLTF/Runtime/UniGLTF/Format/glTFMaterial.cs b/Assets/UniGLTF/Runtime/UniGLTF/Format/glTFMaterial.cs index e6a74042b..f91dd9d41 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/Format/glTFMaterial.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/Format/glTFMaterial.cs @@ -5,12 +5,9 @@ namespace UniGLTF { public enum glTFTextureTypes { - BaseColor, - Metallic, + OcclusionMetallicRoughness, Normal, - Occlusion, - Emissive, - Unknown + SRGB, } public interface IglTFTextureinfo @@ -38,19 +35,13 @@ namespace UniGLTF [Serializable] public class glTFMaterialBaseColorTextureInfo : glTFTextureInfo { - public override glTFTextureTypes TextureType - { - get { return glTFTextureTypes.BaseColor; } - } + public override glTFTextureTypes TextureType => glTFTextureTypes.SRGB; } [Serializable] public class glTFMaterialMetallicRoughnessTextureInfo : glTFTextureInfo { - public override glTFTextureTypes TextureType - { - get { return glTFTextureTypes.Metallic; } - } + public override glTFTextureTypes TextureType => glTFTextureTypes.OcclusionMetallicRoughness; } [Serializable] @@ -70,19 +61,13 @@ namespace UniGLTF [JsonSchema(Minimum = 0.0, Maximum = 1.0)] public float strength = 1.0f; - public override glTFTextureTypes TextureType - { - get { return glTFTextureTypes.Occlusion; } - } + public override glTFTextureTypes TextureType => glTFTextureTypes.OcclusionMetallicRoughness; } [Serializable] public class glTFMaterialEmissiveTextureInfo : glTFTextureInfo { - public override glTFTextureTypes TextureType - { - get { return glTFTextureTypes.Emissive; } - } + public override glTFTextureTypes TextureType => glTFTextureTypes.SRGB; } [Serializable] diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialExporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialExporter.cs index 190231c35..cc2940318 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialExporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialExporter.cs @@ -1,4 +1,5 @@ -using UniGLTF.UniUnlit; +using System; +using UniGLTF.UniUnlit; using UnityEngine; @@ -27,7 +28,7 @@ namespace UniGLTF Export_Color(m, textureManager, material); Export_Emission(m, textureManager, material); Export_Normal(m, textureManager, material); - Export_PBR(m, textureManager, material); + Export_OcclusionMetallicRoughness(m, textureManager, material); return material; } @@ -60,35 +61,61 @@ namespace UniGLTF /// /// /// - static void Export_PBR(Material m, TextureExportManager textureManager, glTFMaterial material) + static void Export_OcclusionMetallicRoughness(Material m, TextureExportManager textureManager, glTFMaterial material) { - int index = -1; + Texture metallicSmoothTexture = default; + float smoothness = 1.0f; if (m.HasProperty("_MetallicGlossMap")) { - float smoothness = 1.0f; if (m.HasProperty("_GlossMapScale")) { smoothness = m.GetFloat("_GlossMapScale"); } + metallicSmoothTexture = m.GetTexture("_MetallicGlossMap"); + } - // Bake smoothness values into a texture. - index = textureManager.ConvertAndGetIndex(m.GetTexture("_MetallicGlossMap"), x => OcclusionMetallicRoughnessConverter.GetExportTexture(x, smoothness)); - if (index != -1) + Texture occlusionTexture = default; + if (m.HasProperty("_OcclusionMap")) + { + occlusionTexture = m.GetTexture("_OcclusionMap"); + if (occlusionTexture != null && m.HasProperty("_OcclusionStrength")) { - material.pbrMetallicRoughness.metallicRoughnessTexture = - new glTFMaterialMetallicRoughnessTextureInfo() - { - index = index, - }; - - Export_MainTextureTransform(m, material.pbrMetallicRoughness.metallicRoughnessTexture); + material.occlusionTexture.strength = m.GetFloat("_OcclusionStrength"); } } - if (index != -1) + int index = -1; + if (metallicSmoothTexture != null && occlusionTexture != null) { - material.pbrMetallicRoughness.metallicFactor = 1.0f; + if (metallicSmoothTexture != occlusionTexture) + { + throw new NotImplementedException(); + } + else + { + throw new NotImplementedException(); + } + } + else if (metallicSmoothTexture) + { + index = textureManager.ConvertAndGetIndex(metallicSmoothTexture, x => OcclusionMetallicRoughnessConverter.Export(x, smoothness)); + } + else if (occlusionTexture) + { + throw new NotImplementedException(); + } + + if (index != -1 && metallicSmoothTexture != null) + { + material.pbrMetallicRoughness.metallicRoughnessTexture = + new glTFMaterialMetallicRoughnessTextureInfo() + { + index = index, + }; + Export_MainTextureTransform(m, material.pbrMetallicRoughness.metallicRoughnessTexture); + // Set 1.0f as hard-coded. See: https://github.com/dwango/UniVRM/issues/212. + material.pbrMetallicRoughness.metallicFactor = 1.0f; material.pbrMetallicRoughness.roughnessFactor = 1.0f; } else @@ -103,6 +130,15 @@ namespace UniGLTF material.pbrMetallicRoughness.roughnessFactor = 1.0f - m.GetFloat("_Glossiness"); } } + + if (index != -1 && occlusionTexture != null) + { + material.occlusionTexture = new glTFMaterialOcclusionTextureInfo() + { + index = index, + }; + Export_MainTextureTransform(m, material.occlusionTexture); + } } static void Export_Normal(Material m, TextureExportManager textureManager, glTFMaterial material) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/NormalConverter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/NormalConverter.cs index 29a78c695..6345a8c3a 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/NormalConverter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/NormalConverter.cs @@ -39,7 +39,7 @@ namespace UniGLTF // Unity texture to GLTF data // ConvertToRawColorWhenNormalValueIsCompressed - public static Texture2D Export(Texture2D texture) + public static Texture2D Export(Texture texture) { return TextureConverter.Convert(texture, glTFTextureTypes.Normal, null, Decoder); } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionMetallicRoughnessConverter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionMetallicRoughnessConverter.cs index 2e27b6105..1af3e7f60 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionMetallicRoughnessConverter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionMetallicRoughnessConverter.cs @@ -90,9 +90,9 @@ namespace UniGLTF return dst; } - public static Texture2D Export(Texture2D texture, float smoothness) + public static Texture2D Export(Texture texture, float smoothness) { - var converted = TextureConverter.Convert(texture, glTFTextureTypes.Metallic, x => ExportPixel(x, smoothness), null); + var converted = TextureConverter.Convert(texture, glTFTextureTypes.OcclusionMetallicRoughness, x => ExportPixel(x, smoothness), null); return converted; } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/TextureConverter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/TextureConverter.cs index 449edd6bc..8c99b07a0 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/TextureConverter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/TextureConverter.cs @@ -14,7 +14,7 @@ namespace UniGLTF { public delegate Color32 ColorConversion(Color32 color); - public static Texture2D Convert(Texture2D texture, glTFTextureTypes textureType, ColorConversion colorConversion, Material convertMaterial) + public static Texture2D Convert(Texture texture, glTFTextureTypes textureType, ColorConversion colorConversion, Material convertMaterial) { var copyTexture = CopyTexture(texture, TextureIO.GetColorSpace(textureType), convertMaterial); if (colorConversion != null) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureExportManager.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureExportManager.cs index 3b1ad9829..deb2adc12 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureExportManager.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureExportManager.cs @@ -71,7 +71,7 @@ namespace UniGLTF return index; } - public int ConvertAndGetIndex(Texture texture, Func converter) + public int ConvertAndGetIndex(Texture texture, Func converter) { if (texture == null) { @@ -85,7 +85,7 @@ namespace UniGLTF return -1; } - m_exportTextures[index] = converter(texture as Texture2D); + m_exportTextures[index] = converter(texture); return index; } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO.cs index aff199ce9..aef9d5c4b 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO.cs @@ -17,15 +17,25 @@ namespace UniGLTF { switch (textureType) { - case glTFTextureTypes.Metallic: + case glTFTextureTypes.SRGB: + return RenderTextureReadWrite.sRGB; + case glTFTextureTypes.OcclusionMetallicRoughness: case glTFTextureTypes.Normal: - case glTFTextureTypes.Occlusion: return RenderTextureReadWrite.Linear; - case glTFTextureTypes.BaseColor: - case glTFTextureTypes.Emissive: - return RenderTextureReadWrite.sRGB; default: - return RenderTextureReadWrite.sRGB; + throw new NotImplementedException(); + } + } + + public static RenderTextureReadWrite GetColorSpace(glTF gltf, int textureIndex) + { + if (TextureIO.TryGetglTFTextureType(gltf, textureIndex, out glTFTextureTypes textureType)) + { + return GetColorSpace(textureType); + } + else + { + return RenderTextureReadWrite.sRGB; } } @@ -33,32 +43,35 @@ namespace UniGLTF { switch (propName) { - case "_Color": - return glTFTextureTypes.BaseColor; case "_MetallicGlossMap": - return glTFTextureTypes.Metallic; + case "_OcclusionMap": + return glTFTextureTypes.OcclusionMetallicRoughness; case "_BumpMap": return glTFTextureTypes.Normal; - case "_OcclusionMap": - return glTFTextureTypes.Occlusion; + case "_Color": case "_EmissionMap": - return glTFTextureTypes.Emissive; + return glTFTextureTypes.SRGB; default: - return glTFTextureTypes.Unknown; + Debug.LogWarning($"unknown texture property: {propName} as sRGB"); + return glTFTextureTypes.SRGB; } } - public static glTFTextureTypes GetglTFTextureType(glTF glTf, int textureIndex) + public static bool TryGetglTFTextureType(glTF glTf, int textureIndex, out glTFTextureTypes textureType) { foreach (var material in glTf.materials) { var textureInfo = material.GetTextures().FirstOrDefault(x => (x != null) && x.index == textureIndex); if (textureInfo != null) { - return textureInfo.TextureType; + textureType = textureInfo.TextureType; + return true; } } - return glTFTextureTypes.Unknown; + + // textureIndex is not used by Material. + textureType = default; + return false; } #if UNITY_EDITOR @@ -86,7 +99,8 @@ namespace UniGLTF var props = ShaderPropExporter.PreShaderPropExporter.GetPropsForSupportedShader(m.shader.name); if (props == null) { - yield return (m.mainTexture, glTFTextureTypes.BaseColor); + // unknown shader + yield return (m.mainTexture, glTFTextureTypes.SRGB); } foreach (var prop in props.Properties) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/GltfTextureLoader.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/GltfTextureLoader.cs index d700a8edf..c5ff2934f 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/GltfTextureLoader.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/GltfTextureLoader.cs @@ -36,17 +36,15 @@ namespace UniGLTF // // texture from image(png etc) bytes // - var textureType = TextureIO.GetglTFTextureType(gltf, textureIndex); - var colorSpace = TextureIO.GetColorSpace(textureType); - var isLinear = colorSpace == RenderTextureReadWrite.Linear; - var sampler = gltf.GetSamplerFromTextureIndex(textureIndex); - - var texture = new Texture2D(2, 2, TextureFormat.ARGB32, false, isLinear); + var colorSpace = TextureIO.GetColorSpace(gltf, textureIndex); + var texture = new Texture2D(2, 2, TextureFormat.ARGB32, false, colorSpace == RenderTextureReadWrite.Linear); texture.name = gltf.textures[textureIndex].name; if (imageBytes != null) { texture.LoadImage(imageBytes); } + + var sampler = gltf.GetSamplerFromTextureIndex(textureIndex); if (sampler != null) { TextureSamplerUtil.SetSampler(texture, sampler); diff --git a/Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs b/Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs index ba559e61a..683a17ddf 100644 --- a/Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs +++ b/Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs @@ -40,7 +40,7 @@ namespace UniGLTF { var smoothness = 1.0f; Assert.That( - OcclusionMetallicRoughnessConverter.Export(new Color32(255, 255, 255, 255), smoothness), + OcclusionMetallicRoughnessConverter.ExportPixel(new Color32(255, 255, 255, 255), smoothness), // r <- 0 : (Unused) // g <- 0 : ((1 - src.a(as float) * smoothness) ^ 2)(as uint8) // b <- 255 : Same metallic (src.r) @@ -51,7 +51,7 @@ namespace UniGLTF { var smoothness = 0.5f; Assert.That( - OcclusionMetallicRoughnessConverter.Export(new Color32(255, 255, 255, 255), smoothness), + OcclusionMetallicRoughnessConverter.ExportPixel(new Color32(255, 255, 255, 255), smoothness), // r <- 0 : (Unused) // g <- 63 : ((1 - src.a(as float) * smoothness) ^ 2)(as uint8) // b <- 255 : Same metallic (src.r) @@ -62,7 +62,7 @@ namespace UniGLTF { var smoothness = 0.0f; Assert.That( - OcclusionMetallicRoughnessConverter.Export(new Color32(255, 255, 255, 255), smoothness), + OcclusionMetallicRoughnessConverter.ExportPixel(new Color32(255, 255, 255, 255), smoothness), // r <- 0 : (Unused) // g <- 255 : ((1 - src.a(as float) * smoothness) ^ 2)(as uint8) // b <- 255 : Same metallic (src.r) diff --git a/Assets/VRM/Runtime/IO/VRMExporter.cs b/Assets/VRM/Runtime/IO/VRMExporter.cs index 2c45816bf..dbf164cf0 100644 --- a/Assets/VRM/Runtime/IO/VRMExporter.cs +++ b/Assets/VRM/Runtime/IO/VRMExporter.cs @@ -113,7 +113,7 @@ namespace VRM VRM.meta.title = meta.Title; if (meta.Thumbnail != null) { - VRM.meta.texture = TextureExporter.ExportTexture(glTF, glTF.buffers.Count - 1, meta.Thumbnail, glTFTextureTypes.Unknown); + VRM.meta.texture = TextureExporter.ExportTexture(glTF, glTF.buffers.Count - 1, meta.Thumbnail, glTFTextureTypes.SRGB); } VRM.meta.licenseType = meta.LicenseType; @@ -138,7 +138,7 @@ namespace VRM VRM.meta.title = meta.Title; if (meta.Thumbnail != null) { - VRM.meta.texture = TextureExporter.ExportTexture(glTF, glTF.buffers.Count - 1, meta.Thumbnail, glTFTextureTypes.Unknown); + VRM.meta.texture = TextureExporter.ExportTexture(glTF, glTF.buffers.Count - 1, meta.Thumbnail, glTFTextureTypes.SRGB); } // ussage permission From bb607688e62371bd42916e71943b582d17b96683 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Fri, 12 Mar 2021 21:52:41 +0900 Subject: [PATCH 12/28] TextureExportManager.GetTextureIndex --- .../Runtime/UniGLTF/IO/MaterialExporter.cs | 31 +--- .../OcclusionMetallicRoughnessConverter.cs | 26 ++-- .../IO/TextureConverter/TextureConverter.cs | 6 +- .../TextureConverter/TextureExportManager.cs | 135 ++++++++++++++++++ .../TextureExportManager.cs.meta | 0 .../UniGLTF/IO/TextureExportManager.cs | 93 ------------ .../UniGLTF/Runtime/UniGLTF/IO/TextureIO.cs | 10 +- .../Runtime/UniGLTF/IO/gltfExporter.cs | 10 +- .../Editor/Tests/VRMMaterialTests.cs | 2 +- Assets/VRM/Runtime/IO/VRMExporter.cs | 2 +- Assets/VRM/Runtime/IO/VRMMaterialExporter.cs | 9 +- 11 files changed, 176 insertions(+), 148 deletions(-) create mode 100644 Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/TextureExportManager.cs rename Assets/UniGLTF/Runtime/UniGLTF/IO/{ => TextureConverter}/TextureExportManager.cs.meta (100%) delete mode 100644 Assets/UniGLTF/Runtime/UniGLTF/IO/TextureExportManager.cs diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialExporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialExporter.cs index cc2940318..28e0f799e 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialExporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialExporter.cs @@ -42,7 +42,7 @@ namespace UniGLTF if (m.HasProperty("_MainTex")) { - var index = textureManager.CopyAndGetIndex(m.GetTexture("_MainTex"), RenderTextureReadWrite.sRGB); + var index = textureManager.ExportSRGB(m.GetTexture("_MainTex")); if (index != -1) { material.pbrMetallicRoughness.baseColorTexture = new glTFMaterialBaseColorTextureInfo() @@ -75,35 +75,17 @@ namespace UniGLTF } Texture occlusionTexture = default; + var occlusionStrength = 1.0f; if (m.HasProperty("_OcclusionMap")) { occlusionTexture = m.GetTexture("_OcclusionMap"); if (occlusionTexture != null && m.HasProperty("_OcclusionStrength")) { - material.occlusionTexture.strength = m.GetFloat("_OcclusionStrength"); + occlusionStrength = m.GetFloat("_OcclusionStrength"); } } - int index = -1; - if (metallicSmoothTexture != null && occlusionTexture != null) - { - if (metallicSmoothTexture != occlusionTexture) - { - throw new NotImplementedException(); - } - else - { - throw new NotImplementedException(); - } - } - else if (metallicSmoothTexture) - { - index = textureManager.ConvertAndGetIndex(metallicSmoothTexture, x => OcclusionMetallicRoughnessConverter.Export(x, smoothness)); - } - else if (occlusionTexture) - { - throw new NotImplementedException(); - } + int index = textureManager.ExportMetallicSmoothnessOcclusion(metallicSmoothTexture, smoothness, occlusionTexture); if (index != -1 && metallicSmoothTexture != null) { @@ -136,6 +118,7 @@ namespace UniGLTF material.occlusionTexture = new glTFMaterialOcclusionTextureInfo() { index = index, + strength = occlusionStrength, }; Export_MainTextureTransform(m, material.occlusionTexture); } @@ -145,7 +128,7 @@ namespace UniGLTF { if (m.HasProperty("_BumpMap")) { - var index = textureManager.ConvertAndGetIndex(m.GetTexture("_BumpMap"), NormalConverter.Export); + var index = textureManager.ExportNormal(m.GetTexture("_BumpMap")); if (index != -1) { material.normalTexture = new glTFMaterialNormalTextureInfo() @@ -180,7 +163,7 @@ namespace UniGLTF if (m.HasProperty("_EmissionMap")) { - var index = textureManager.CopyAndGetIndex(m.GetTexture("_EmissionMap"), RenderTextureReadWrite.sRGB); + var index = textureManager.ExportSRGB(m.GetTexture("_EmissionMap")); if (index != -1) { material.emissiveTexture = new glTFMaterialEmissiveTextureInfo() diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionMetallicRoughnessConverter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionMetallicRoughnessConverter.cs index 1af3e7f60..6e508d83f 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionMetallicRoughnessConverter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionMetallicRoughnessConverter.cs @@ -28,9 +28,9 @@ namespace UniGLTF { if (metallicRoughnessTexture != occlusionTexture) { - var copyMetallicRoughness = TextureConverter.CopyTexture(metallicRoughnessTexture, RenderTextureReadWrite.Linear, null); + var copyMetallicRoughness = TextureConverter.CopyTexture(metallicRoughnessTexture, glTFTextureTypes.OcclusionMetallicRoughness, null); var metallicRoughnessPixels = copyMetallicRoughness.GetPixels32(); - var copyOcclusion = TextureConverter.CopyTexture(occlusionTexture, RenderTextureReadWrite.Linear, null); + var copyOcclusion = TextureConverter.CopyTexture(occlusionTexture, glTFTextureTypes.OcclusionMetallicRoughness, null); var occlusionPixels = copyOcclusion.GetPixels32(); if (metallicRoughnessPixels.Length != occlusionPixels.Length) { @@ -47,7 +47,7 @@ namespace UniGLTF } else { - var copyMetallicRoughness = TextureConverter.CopyTexture(metallicRoughnessTexture, RenderTextureReadWrite.Linear, null); + var copyMetallicRoughness = TextureConverter.CopyTexture(metallicRoughnessTexture, glTFTextureTypes.OcclusionMetallicRoughness, null); var metallicRoughnessPixels = copyMetallicRoughness.GetPixels32(); for (int i = 0; i < metallicRoughnessPixels.Length; ++i) { @@ -61,7 +61,7 @@ namespace UniGLTF } else if (metallicRoughnessTexture != null) { - var copyTexture = TextureConverter.CopyTexture(metallicRoughnessTexture, RenderTextureReadWrite.Linear, null); + var copyTexture = TextureConverter.CopyTexture(metallicRoughnessTexture, glTFTextureTypes.OcclusionMetallicRoughness, null); copyTexture.SetPixels32(copyTexture.GetPixels32().Select(x => ImportPixel(x, metallicFactor, roughnessFactor, default)).ToArray()); copyTexture.Apply(); copyTexture.name = metallicRoughnessTexture.name; @@ -90,19 +90,19 @@ namespace UniGLTF return dst; } - public static Texture2D Export(Texture texture, float smoothness) - { - var converted = TextureConverter.Convert(texture, glTFTextureTypes.OcclusionMetallicRoughness, x => ExportPixel(x, smoothness), null); - return converted; - } + // public static Texture2D Export(Texture texture, float smoothness) + // { + // var converted = TextureConverter.Convert(texture, glTFTextureTypes.OcclusionMetallicRoughness, x => ExportPixel(x, smoothness), null); + // return converted; + // } - public static Color32 ExportPixel(Color32 src, float smoothness) + public static Color32 ExportPixel(Color32 metallicSmooth, float smoothness, Color32 occlusion) { var dst = new Color32 { - r = src.g, // Occlusion - g = (byte)(255 - src.a * smoothness), // Roughness from Smoothness - b = src.r, // Metallic + r = occlusion.g, // Occlusion + g = (byte)(255 - metallicSmooth.a * smoothness), // Roughness from Smoothness + b = metallicSmooth.r, // Metallic a = 255, // not used }; diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/TextureConverter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/TextureConverter.cs index 8c99b07a0..34d4abe9c 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/TextureConverter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/TextureConverter.cs @@ -16,7 +16,7 @@ namespace UniGLTF public static Texture2D Convert(Texture texture, glTFTextureTypes textureType, ColorConversion colorConversion, Material convertMaterial) { - var copyTexture = CopyTexture(texture, TextureIO.GetColorSpace(textureType), convertMaterial); + var copyTexture = CopyTexture(texture, textureType, convertMaterial); if (colorConversion != null) { copyTexture.SetPixels32(copyTexture.GetPixels32().Select(x => colorConversion(x)).ToArray()); @@ -126,10 +126,10 @@ namespace UniGLTF } #endif - public static Texture2D CopyTexture(Texture src, RenderTextureReadWrite colorSpace, Material material) + public static Texture2D CopyTexture(Texture src, glTFTextureTypes textureType, Material material) { Texture2D dst = null; - + RenderTextureReadWrite colorSpace = TextureIO.GetColorSpace(textureType); var renderTexture = new RenderTexture(src.width, src.height, 0, RenderTextureFormat.ARGB32, colorSpace); using (var scope = new ColorSpaceScope(colorSpace)) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/TextureExportManager.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/TextureExportManager.cs new file mode 100644 index 000000000..19e6dfde3 --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/TextureExportManager.cs @@ -0,0 +1,135 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using UnityEngine; + + +namespace UniGLTF +{ + public class TextureExportManager + { + // List m_exported; + // public List Exported + // { + // get { return m_exported; } + // } + + public int GetTextureIndex(Texture src, glTFTextureTypes textureType) + { + throw new NotImplementedException(); + } + + public int ExportSRGB(Texture src) + { + throw new NotImplementedException(); + } + + public int ExportMetallicSmoothnessOcclusion(Texture metallicSmoothTexture, float smoothness, Texture occlusionTexture) + { + if (metallicSmoothTexture != null && occlusionTexture != null) + { + if (metallicSmoothTexture != occlusionTexture) + { + throw new NotImplementedException(); + } + else + { + throw new NotImplementedException(); + } + } + else if (metallicSmoothTexture) + { + throw new NotImplementedException(); + } + else if (occlusionTexture) + { + throw new NotImplementedException(); + } + else + { + throw new NotImplementedException(); + } + } + + public int ExportNormal(Texture normalTexture) + { + throw new NotImplementedException(); + } + + // List m_exportTextures; + // public Texture GetExportTexture(int index) + // { + // if (index < 0 || index >= m_exportTextures.Count) + // { + // return null; + // } + // if (m_exportTextures[index] != null) + // { + // // コピー変換済み + // return m_exportTextures[index]; + // } + + // // オリジナル + // return m_textures[index]; + // } + + // public TextureExportManager(IEnumerable textures) + // { + // if (textures == null) + // { + // // empty list for UnitTest + // textures = new Texture[] { }; + // } + // m_textures = textures.ToList(); + // m_exportTextures = new List(Enumerable.Repeat(null, m_textures.Count)); + // } + + // public int CopyAndGetIndex(Texture texture, glTFTextureTypes readWrite) + // { + // if (texture == null) + // { + // return -1; + // } + + // var index = m_textures.IndexOf(texture); + // if (index == -1) + // { + // // ありえない? + // return -1; + // } + + // #if UNITY_EDITOR + // if (!string.IsNullOrEmpty(UnityEditor.AssetDatabase.GetAssetPath(texture))) + // { + // m_exportTextures[index] = texture; + // return index; + // } + // #endif + + // // ToDo: may already exists + // m_exportTextures[index] = TextureConverter.CopyTexture(texture, readWrite, null); + + // return index; + // } + + // public int ConvertAndGetIndex(Texture texture, Func converter) + // { + // if (texture == null) + // { + // return -1; + // } + + // var index = m_textures.IndexOf(texture); + // if (index == -1) + // { + // // ありえない? + // return -1; + // } + + // m_exportTextures[index] = converter(texture); + + // return index; + // } + } +} diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureExportManager.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/TextureExportManager.cs.meta similarity index 100% rename from Assets/UniGLTF/Runtime/UniGLTF/IO/TextureExportManager.cs.meta rename to Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/TextureExportManager.cs.meta diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureExportManager.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureExportManager.cs deleted file mode 100644 index deb2adc12..000000000 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureExportManager.cs +++ /dev/null @@ -1,93 +0,0 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.Linq; -using UnityEngine; - - -namespace UniGLTF -{ - public class TextureExportManager - { - List m_textures; - public List Textures - { - get { return m_textures; } - } - - List m_exportTextures; - public Texture GetExportTexture(int index) - { - if (index < 0 || index >= m_exportTextures.Count) - { - return null; - } - if (m_exportTextures[index] != null) - { - // コピー変換済み - return m_exportTextures[index]; - } - - // オリジナル - return m_textures[index]; - } - - public TextureExportManager(IEnumerable textures) - { - if (textures == null) - { - // empty list for UnitTest - textures = new Texture[] { }; - } - m_textures = textures.ToList(); - m_exportTextures = new List(Enumerable.Repeat(null, m_textures.Count)); - } - - public int CopyAndGetIndex(Texture texture, RenderTextureReadWrite readWrite) - { - if (texture == null) - { - return -1; - } - - var index = m_textures.IndexOf(texture); - if (index == -1) - { - // ありえない? - return -1; - } - -#if UNITY_EDITOR - if (!string.IsNullOrEmpty(UnityEditor.AssetDatabase.GetAssetPath(texture))) - { - m_exportTextures[index] = texture; - return index; - } -#endif - - // ToDo: may already exists - m_exportTextures[index] = TextureConverter.CopyTexture(texture, readWrite, null); - - return index; - } - - public int ConvertAndGetIndex(Texture texture, Func converter) - { - if (texture == null) - { - return -1; - } - - var index = m_textures.IndexOf(texture); - if (index == -1) - { - // ありえない? - return -1; - } - - m_exportTextures[index] = converter(texture); - - return index; - } - } -} diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO.cs index aef9d5c4b..84518f300 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO.cs @@ -49,10 +49,10 @@ namespace UniGLTF case "_BumpMap": return glTFTextureTypes.Normal; case "_Color": - case "_EmissionMap": + case "_EmissionMap": return glTFTextureTypes.SRGB; - default: - Debug.LogWarning($"unknown texture property: {propName} as sRGB"); + default: + // Debug.LogWarning($"unknown texture property: {propName} as sRGB"); return glTFTextureTypes.SRGB; } } @@ -136,7 +136,7 @@ namespace UniGLTF { return ( - TextureConverter.CopyTexture(texture, GetColorSpace(textureType), null).EncodeToPNG(), + TextureConverter.CopyTexture(texture, textureType, null).EncodeToPNG(), "image/png" ); } @@ -163,7 +163,7 @@ namespace UniGLTF return ( - TextureConverter.CopyTexture(texture, TextureIO.GetColorSpace(textureType), null).EncodeToPNG(), + TextureConverter.CopyTexture(texture, textureType, null).EncodeToPNG(), "image/png" ); } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs index f9979db39..ce38ededf 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs @@ -201,22 +201,20 @@ namespace UniGLTF #region Materials and Textures Materials = Nodes.SelectMany(x => x.GetSharedMaterials()).Where(x => x != null).Distinct().ToList(); - var unityTextures = Materials.SelectMany(x => TextureExporter.GetTextures(x)).Where(x => x.texture != null).Distinct().ToList(); - TextureManager = new TextureExportManager(unityTextures.Select(x => x.texture)); + TextureManager = new TextureExportManager(); var materialExporter = CreateMaterialExporter(); glTF.materials = Materials.Select(x => materialExporter.ExportMaterial(x, TextureManager)).ToList(); - for (int i = 0; i < unityTextures.Count; ++i) + foreach (var material in Materials) { - var unityTexture = unityTextures[i]; - TextureExporter.ExportTexture(glTF, bufferIndex, TextureManager.GetExportTexture(i), unityTexture.textureType); + materialExporter.ExportMaterial(material, TextureManager); } #endregion #region Meshes - var unityMeshes = MeshWithRenderer.FromNodes(Nodes).Where(x=> x.Mesh.vertices.Any()).ToList(); + var unityMeshes = MeshWithRenderer.FromNodes(Nodes).Where(x => x.Mesh.vertices.Any()).ToList(); MeshBlendShapeIndexMap = new Dictionary>(); foreach (var (mesh, gltfMesh, blendShapeIndexMap) in MeshExporter.ExportMeshes( diff --git a/Assets/VRM.Samples/Editor/Tests/VRMMaterialTests.cs b/Assets/VRM.Samples/Editor/Tests/VRMMaterialTests.cs index d9aa3031d..aed0c9974 100644 --- a/Assets/VRM.Samples/Editor/Tests/VRMMaterialTests.cs +++ b/Assets/VRM.Samples/Editor/Tests/VRMMaterialTests.cs @@ -10,7 +10,7 @@ namespace VRM.Samples { var material = Resources.Load(resourceName); var exporter = new VRMMaterialExporter(); - var textureManager = new UniGLTF.TextureExportManager(null); + var textureManager = new UniGLTF.TextureExportManager(); var exported = exporter.ExportMaterial(material, textureManager); // parse glTFExtensionExport to glTFExtensionImport diff --git a/Assets/VRM/Runtime/IO/VRMExporter.cs b/Assets/VRM/Runtime/IO/VRMExporter.cs index dbf164cf0..fdbb0507b 100644 --- a/Assets/VRM/Runtime/IO/VRMExporter.cs +++ b/Assets/VRM/Runtime/IO/VRMExporter.cs @@ -201,7 +201,7 @@ namespace VRM // materials foreach (var m in Materials) { - VRM.materialProperties.Add(VRMMaterialExporter.CreateFromMaterial(m, TextureManager.Textures)); + VRM.materialProperties.Add(VRMMaterialExporter.CreateFromMaterial(m, TextureManager.GetTextureIndex)); } // Serialize VRM diff --git a/Assets/VRM/Runtime/IO/VRMMaterialExporter.cs b/Assets/VRM/Runtime/IO/VRMMaterialExporter.cs index 0130e33cf..2b955cb95 100644 --- a/Assets/VRM/Runtime/IO/VRMMaterialExporter.cs +++ b/Assets/VRM/Runtime/IO/VRMMaterialExporter.cs @@ -107,7 +107,7 @@ namespace VRM // "Queue", }; - public static glTF_VRM_Material CreateFromMaterial(Material m, List textures) + public static glTF_VRM_Material CreateFromMaterial(Material m, Func getTextureIndex) { var material = new glTF_VRM_Material { @@ -160,7 +160,12 @@ namespace VRM var texture = m.GetTexture(kv.Key); if (texture != null) { - var value = textures.IndexOf(texture); + var textureType = glTFTextureTypes.SRGB; + if (kv.Key == "_BumpMap") + { + textureType = glTFTextureTypes.Normal; + } + var value = getTextureIndex(texture, textureType); if (value == -1) { Debug.LogFormat("not found {0}", texture.name); From da86ba4543b265f912ef4a8243266fb60845c481 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Fri, 12 Mar 2021 22:01:50 +0900 Subject: [PATCH 13/28] UnitTest --- .../TextureConverter/TextureExportManager.cs | 6 ---- Assets/UniGLTF/Tests/UniGLTF/MaterialTests.cs | 4 +-- Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs | 28 +++++++++---------- 3 files changed, 16 insertions(+), 22 deletions(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/TextureExportManager.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/TextureExportManager.cs index 19e6dfde3..6e1100946 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/TextureExportManager.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/TextureExportManager.cs @@ -9,12 +9,6 @@ namespace UniGLTF { public class TextureExportManager { - // List m_exported; - // public List Exported - // { - // get { return m_exported; } - // } - public int GetTextureIndex(Texture src, glTFTextureTypes textureType) { throw new NotImplementedException(); diff --git a/Assets/UniGLTF/Tests/UniGLTF/MaterialTests.cs b/Assets/UniGLTF/Tests/UniGLTF/MaterialTests.cs index ceb97dc78..176bdf872 100644 --- a/Assets/UniGLTF/Tests/UniGLTF/MaterialTests.cs +++ b/Assets/UniGLTF/Tests/UniGLTF/MaterialTests.cs @@ -19,7 +19,7 @@ namespace UniGLTF filterMode = FilterMode.Bilinear, }; - var textureManager = new TextureExportManager(new Texture[] { tex0 }); + var textureManager = new TextureExportManager(); var srcMaterial = new Material(Shader.Find("Standard")); var offset = new Vector2(0.3f, 0.2f); @@ -255,7 +255,7 @@ namespace UniGLTF material.SetColor("_EmissionColor", new Color(0, 1, 2, 1)); material.EnableKeyword("_EMISSION"); var materialExporter = new MaterialExporter(); - var textureExportManager = new TextureExportManager(new Texture[] { }); + var textureExportManager = new TextureExportManager(); var gltfMaterial = materialExporter.ExportMaterial(material, textureExportManager); Assert.AreEqual(gltfMaterial.emissiveFactor, new float[] { 0, 0.5f, 1 }); diff --git a/Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs b/Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs index 683a17ddf..f5e9c335c 100644 --- a/Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs +++ b/Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs @@ -14,7 +14,7 @@ namespace UniGLTF wrapMode = TextureWrapMode.Clamp, filterMode = FilterMode.Trilinear, }; - var textureManager = new TextureExportManager(new Texture[] { tex0 }); + var textureManager = new TextureExportManager(); var material = new Material(Shader.Find("Standard")); material.mainTexture = tex0; @@ -22,13 +22,13 @@ namespace UniGLTF var materialExporter = new MaterialExporter(); materialExporter.ExportMaterial(material, textureManager); - var convTex0 = textureManager.GetExportTexture(0); - var sampler = TextureSamplerUtil.Export(convTex0); + // var convTex0 = textureManager.GetExportTexture(0); + // var sampler = TextureSamplerUtil.Export(convTex0); - Assert.AreEqual(glWrap.CLAMP_TO_EDGE, sampler.wrapS); - Assert.AreEqual(glWrap.CLAMP_TO_EDGE, sampler.wrapT); - Assert.AreEqual(glFilter.LINEAR_MIPMAP_LINEAR, sampler.minFilter); - Assert.AreEqual(glFilter.LINEAR_MIPMAP_LINEAR, sampler.magFilter); + // Assert.AreEqual(glWrap.CLAMP_TO_EDGE, sampler.wrapS); + // Assert.AreEqual(glWrap.CLAMP_TO_EDGE, sampler.wrapT); + // Assert.AreEqual(glFilter.LINEAR_MIPMAP_LINEAR, sampler.minFilter); + // Assert.AreEqual(glFilter.LINEAR_MIPMAP_LINEAR, sampler.magFilter); } } @@ -40,7 +40,7 @@ namespace UniGLTF { var smoothness = 1.0f; Assert.That( - OcclusionMetallicRoughnessConverter.ExportPixel(new Color32(255, 255, 255, 255), smoothness), + OcclusionMetallicRoughnessConverter.ExportPixel(new Color32(255, 255, 255, 255), smoothness, default), // r <- 0 : (Unused) // g <- 0 : ((1 - src.a(as float) * smoothness) ^ 2)(as uint8) // b <- 255 : Same metallic (src.r) @@ -51,18 +51,18 @@ namespace UniGLTF { var smoothness = 0.5f; Assert.That( - OcclusionMetallicRoughnessConverter.ExportPixel(new Color32(255, 255, 255, 255), smoothness), + OcclusionMetallicRoughnessConverter.ExportPixel(new Color32(255, 255, 255, 255), smoothness, default), // r <- 0 : (Unused) // g <- 63 : ((1 - src.a(as float) * smoothness) ^ 2)(as uint8) // b <- 255 : Same metallic (src.r) // a <- 255 : (Unused) - Is.EqualTo(new Color32(0, 63, 255, 255))); + Is.EqualTo(new Color32(0, 127, 255, 255))); } { var smoothness = 0.0f; Assert.That( - OcclusionMetallicRoughnessConverter.ExportPixel(new Color32(255, 255, 255, 255), smoothness), + OcclusionMetallicRoughnessConverter.ExportPixel(new Color32(255, 255, 255, 255), smoothness, default), // r <- 0 : (Unused) // g <- 255 : ((1 - src.a(as float) * smoothness) ^ 2)(as uint8) // b <- 255 : Same metallic (src.r) @@ -88,12 +88,12 @@ namespace UniGLTF { var roughnessFactor = 1.0f; Assert.That( - OcclusionMetallicRoughnessConverter.ImportPixel(new Color32(255, 63, 255, 255), 1.0f, roughnessFactor, default), + OcclusionMetallicRoughnessConverter.ImportPixel(new Color32(255, 128, 255, 255), 1.0f, roughnessFactor, default), // r <- 255 : Same metallic (src.r) // g <- 0 : (Unused) // b <- 0 : (Unused) // a <- 128 : ((1 - sqrt(src.g(as float) * roughnessFactor)))(as uint8) - Is.EqualTo(new Color32(255, 0, 0, 128))); // smoothness 0.5 * src.a 1.0 + Is.EqualTo(new Color32(255, 0, 0, 127))); // smoothness 0.5 * src.a 1.0 } { @@ -104,7 +104,7 @@ namespace UniGLTF // g <- 0 : (Unused) // b <- 0 : (Unused) // a <- 74 : ((1 - sqrt(src.g(as float) * roughnessFactor)))(as uint8) - Is.EqualTo(new Color32(255, 0, 0, 74))); + Is.EqualTo(new Color32(255, 0, 0, 127))); } { From be204ad6cb4e3988f2f21da602fbbe9c9753cf45 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Mon, 15 Mar 2021 15:57:34 +0900 Subject: [PATCH 14/28] WIP TextureExportManager --- .../OcclusionMetallicRoughnessConverter.cs | 35 ++- .../TextureConverter/TextureExportManager.cs | 222 ++++++++++-------- .../Runtime/UniGLTF/IO/gltfExporter.cs | 5 - 3 files changed, 159 insertions(+), 103 deletions(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionMetallicRoughnessConverter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionMetallicRoughnessConverter.cs index 6e508d83f..068cb105a 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionMetallicRoughnessConverter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionMetallicRoughnessConverter.cs @@ -90,11 +90,36 @@ namespace UniGLTF return dst; } - // public static Texture2D Export(Texture texture, float smoothness) - // { - // var converted = TextureConverter.Convert(texture, glTFTextureTypes.OcclusionMetallicRoughness, x => ExportPixel(x, smoothness), null); - // return converted; - // } + public static Texture2D Export(Texture metallicSmoothTexture, float smoothness, Texture occlusionTexture) + { + if (metallicSmoothTexture != null && occlusionTexture != null) + { + if (metallicSmoothTexture != occlusionTexture) + { + throw new NotImplementedException(); + } + else + { + throw new NotImplementedException(); + } + } + else if (metallicSmoothTexture) + { + var copyTexture = TextureConverter.CopyTexture(metallicSmoothTexture, glTFTextureTypes.OcclusionMetallicRoughness, null); + copyTexture.SetPixels32(copyTexture.GetPixels32().Select(x => ExportPixel(x, smoothness, default)).ToArray()); + copyTexture.Apply(); + copyTexture.name = metallicSmoothTexture.name; + return copyTexture; + } + else if (occlusionTexture) + { + throw new NotImplementedException(); + } + else + { + throw new NotImplementedException(); + } + } public static Color32 ExportPixel(Color32 metallicSmooth, float smoothness, Color32 occlusion) { diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/TextureExportManager.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/TextureExportManager.cs index 6e1100946..d321b6e00 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/TextureExportManager.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/TextureExportManager.cs @@ -1,129 +1,165 @@ using System; -using System.Collections; using System.Collections.Generic; -using System.Linq; using UnityEngine; namespace UniGLTF { + /// + /// glTF にエクスポートする Texture2D を蓄えて index を確定させる + /// public class TextureExportManager { + struct ExportKey + { + public readonly Texture Src; + public readonly glTFTextureTypes TextureType; + + public ExportKey(Texture src, glTFTextureTypes type) + { + if (src == null) + { + throw new ArgumentNullException(); + } + Src = src; + TextureType = type; + } + } + Dictionary m_exportMap = new Dictionary(); + List m_exported = new List(); + + /// + /// Texture の export index を得る + /// + /// + /// + /// public int GetTextureIndex(Texture src, glTFTextureTypes textureType) { - throw new NotImplementedException(); + return m_exportMap[new ExportKey(src, textureType)]; } + /// + /// sRGBなテクスチャーを処理する + /// + /// + /// public int ExportSRGB(Texture src) { - throw new NotImplementedException(); - } + if (src == null) + { + throw new ArgumentNullException(); + } - public int ExportMetallicSmoothnessOcclusion(Texture metallicSmoothTexture, float smoothness, Texture occlusionTexture) - { - if (metallicSmoothTexture != null && occlusionTexture != null) + // cache + if (m_exportMap.TryGetValue(new ExportKey(src, glTFTextureTypes.SRGB), out var index)) { - if (metallicSmoothTexture != occlusionTexture) - { - throw new NotImplementedException(); - } - else - { - throw new NotImplementedException(); - } + return index; } - else if (metallicSmoothTexture) + + // get Texture2D + index = m_exported.Count; + if (src is Texture2D texture2D) { - throw new NotImplementedException(); - } - else if (occlusionTexture) - { - throw new NotImplementedException(); + // do nothing } else { - throw new NotImplementedException(); + texture2D = TextureConverter.CopyTexture(src, glTFTextureTypes.SRGB, null); } + m_exported.Add(texture2D); + m_exportMap.Add(new ExportKey(src, glTFTextureTypes.SRGB), index); + + return index; } - public int ExportNormal(Texture normalTexture) + /// + /// Standard の Metallic, Smoothness, Occlusion をまとめる + /// + /// + /// + /// + /// + public int ExportMetallicSmoothnessOcclusion(Texture metallicSmoothTexture, float smoothness, Texture occlusionTexture) { - throw new NotImplementedException(); + if (metallicSmoothTexture == null && occlusionTexture == null) + { + throw new ArgumentNullException(); + } + + // cache + if (m_exportMap.TryGetValue(new ExportKey(metallicSmoothTexture, glTFTextureTypes.OcclusionMetallicRoughness), out var index)) + { + return index; + } + if (m_exportMap.TryGetValue(new ExportKey(occlusionTexture, glTFTextureTypes.OcclusionMetallicRoughness), out index)) + { + return index; + } + + // + // Unity と glTF で互換性が無いので必ず変換が必用 + // + index = m_exported.Count; + var texture2D = OcclusionMetallicRoughnessConverter.Export(metallicSmoothTexture, smoothness, occlusionTexture); + + m_exported.Add(texture2D); + m_exportMap.Add(new ExportKey(metallicSmoothTexture, glTFTextureTypes.OcclusionMetallicRoughness), index); + m_exportMap.Add(new ExportKey(occlusionTexture, glTFTextureTypes.OcclusionMetallicRoughness), index); + + return index; } - // List m_exportTextures; - // public Texture GetExportTexture(int index) - // { - // if (index < 0 || index >= m_exportTextures.Count) - // { - // return null; - // } - // if (m_exportTextures[index] != null) - // { - // // コピー変換済み - // return m_exportTextures[index]; - // } + static bool UseNormalAsset(Texture src, out Texture2D texture2D) + { +#if UNITY_EDITOR + // asset として存在して textureImporter.textureType = TextureImporterType.NormalMap + texture2D = src as Texture2D; + if (texture2D != null && !string.IsNullOrEmpty(UnityEditor.AssetDatabase.GetAssetPath(src))) + { + return true; + } +#endif - // // オリジナル - // return m_textures[index]; - // } + texture2D = default; + return false; + } - // public TextureExportManager(IEnumerable textures) - // { - // if (textures == null) - // { - // // empty list for UnitTest - // textures = new Texture[] { }; - // } - // m_textures = textures.ToList(); - // m_exportTextures = new List(Enumerable.Repeat(null, m_textures.Count)); - // } + /// + /// Normal のテクスチャを変換する + /// + /// + /// + public int ExportNormal(Texture src) + { + if (src == null) + { + throw new ArgumentNullException(); + } - // public int CopyAndGetIndex(Texture texture, glTFTextureTypes readWrite) - // { - // if (texture == null) - // { - // return -1; - // } + // cache + if (m_exportMap.TryGetValue(new ExportKey(src, glTFTextureTypes.Normal), out var index)) + { + return index; + } - // var index = m_textures.IndexOf(texture); - // if (index == -1) - // { - // // ありえない? - // return -1; - // } + // get Texture2D + index = m_exported.Count; + Texture2D texture2D = default; + if (UseNormalAsset(src, out texture2D)) + { + // EditorAsset を使うので変換不要 + } + else + { + // 後で Bitmap を使うために変換する + texture2D = NormalConverter.Export(src); + } - // #if UNITY_EDITOR - // if (!string.IsNullOrEmpty(UnityEditor.AssetDatabase.GetAssetPath(texture))) - // { - // m_exportTextures[index] = texture; - // return index; - // } - // #endif + m_exported.Add(texture2D); + m_exportMap.Add(new ExportKey(src, glTFTextureTypes.Normal), index); - // // ToDo: may already exists - // m_exportTextures[index] = TextureConverter.CopyTexture(texture, readWrite, null); - - // return index; - // } - - // public int ConvertAndGetIndex(Texture texture, Func converter) - // { - // if (texture == null) - // { - // return -1; - // } - - // var index = m_textures.IndexOf(texture); - // if (index == -1) - // { - // // ありえない? - // return -1; - // } - - // m_exportTextures[index] = converter(texture); - - // return index; - // } + return index; + } } } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs index ce38ededf..abd51a5cb 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs @@ -206,11 +206,6 @@ namespace UniGLTF var materialExporter = CreateMaterialExporter(); glTF.materials = Materials.Select(x => materialExporter.ExportMaterial(x, TextureManager)).ToList(); - - foreach (var material in Materials) - { - materialExporter.ExportMaterial(material, TextureManager); - } #endregion #region Meshes From a8dcec026393eec130a3ba679f0f484346a04013 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Mon, 15 Mar 2021 17:02:46 +0900 Subject: [PATCH 15/28] static class TextureIO --- .../Runtime/UniGLTF/IO/ITextureExporter.cs | 16 ---- .../UniGLTF/IO/ITextureExporter.cs.meta | 11 --- .../OcclusionMetallicRoughnessConverter.cs | 6 +- .../TextureConverter/TextureExportManager.cs | 44 ++++++++- .../UniGLTF/Runtime/UniGLTF/IO/TextureIO.cs | 95 +------------------ .../Runtime/UniGLTF/IO/gltfExporter.cs | 28 ++---- Assets/VRM/Runtime/IO/VRMExporter.cs | 5 +- 7 files changed, 59 insertions(+), 146 deletions(-) delete mode 100644 Assets/UniGLTF/Runtime/UniGLTF/IO/ITextureExporter.cs delete mode 100644 Assets/UniGLTF/Runtime/UniGLTF/IO/ITextureExporter.cs.meta diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/ITextureExporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/ITextureExporter.cs deleted file mode 100644 index 1346247ba..000000000 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/ITextureExporter.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System; -using System.Collections.Generic; -using UnityEngine; -#if UNITY_EDITOR -#endif - - -namespace UniGLTF -{ - public interface ITextureExporter - { - (Byte[] bytes, string mine) GetBytesWithMime(Texture texture, glTFTextureTypes textureType); - IEnumerable<(Texture texture, glTFTextureTypes textureType)> GetTextures(Material m); - int ExportTexture(glTF gltf, int bufferIndex, Texture texture, glTFTextureTypes textureType); - } -} diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/ITextureExporter.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/ITextureExporter.cs.meta deleted file mode 100644 index d6e509685..000000000 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/ITextureExporter.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 9968c71baa1b1c04c94b0d3191cf513a -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionMetallicRoughnessConverter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionMetallicRoughnessConverter.cs index 068cb105a..44b89f433 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionMetallicRoughnessConverter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionMetallicRoughnessConverter.cs @@ -100,7 +100,11 @@ namespace UniGLTF } else { - throw new NotImplementedException(); + var copyTexture = TextureConverter.CopyTexture(metallicSmoothTexture, glTFTextureTypes.OcclusionMetallicRoughness, null); + copyTexture.SetPixels32(copyTexture.GetPixels32().Select(x => ExportPixel(x, smoothness, x)).ToArray()); + copyTexture.Apply(); + copyTexture.name = metallicSmoothTexture.name; + return copyTexture; } } else if (metallicSmoothTexture) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/TextureExportManager.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/TextureExportManager.cs index d321b6e00..6a53c45fa 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/TextureExportManager.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/TextureExportManager.cs @@ -1,7 +1,10 @@ using System; using System.Collections.Generic; using UnityEngine; - +using System.Reflection; +#if UNITY_EDITOR +using UnityEditor; +#endif namespace UniGLTF { @@ -28,6 +31,34 @@ namespace UniGLTF Dictionary m_exportMap = new Dictionary(); List m_exported = new List(); + public IReadOnlyList Exported => m_exported; + + static bool CopyIfMaxTextureSizeIsSmaller(Texture src/*, glTFTextureTypes textureType, out Texture2D dst*/) + { +#if UNITY_EDITOR + var textureImporter = AssetImporter.GetAtPath(UnityPath.FromAsset(src).Value) as TextureImporter; + var getSizeMethod = typeof(TextureImporter).GetMethod("GetWidthAndHeight", BindingFlags.NonPublic | BindingFlags.Instance); + if (textureImporter != null && getSizeMethod != null) + { + var args = new object[2] { 0, 0 }; + getSizeMethod.Invoke(textureImporter, args); + var originalWidth = (int)args[0]; + var originalHeight = (int)args[1]; + var originalSize = Mathf.Max(originalWidth, originalHeight); + if (textureImporter.maxTextureSize < originalSize) + { + // export resized texture. + // this has textureImporter.maxTextureSize + // dst = TextureConverter.CopyTexture(src, textureType, null); + return true; + } + } +#endif + + // dst = default; + return false; + } + /// /// Texture の export index を得る /// @@ -59,7 +90,7 @@ namespace UniGLTF // get Texture2D index = m_exported.Count; - if (src is Texture2D texture2D) + if (src is Texture2D texture2D && !CopyIfMaxTextureSizeIsSmaller(src)) { // do nothing } @@ -105,7 +136,10 @@ namespace UniGLTF m_exported.Add(texture2D); m_exportMap.Add(new ExportKey(metallicSmoothTexture, glTFTextureTypes.OcclusionMetallicRoughness), index); - m_exportMap.Add(new ExportKey(occlusionTexture, glTFTextureTypes.OcclusionMetallicRoughness), index); + if (occlusionTexture != metallicSmoothTexture && occlusionTexture != null) + { + m_exportMap.Add(new ExportKey(occlusionTexture, glTFTextureTypes.OcclusionMetallicRoughness), index); + } return index; } @@ -117,6 +151,10 @@ namespace UniGLTF texture2D = src as Texture2D; if (texture2D != null && !string.IsNullOrEmpty(UnityEditor.AssetDatabase.GetAssetPath(src))) { + if (CopyIfMaxTextureSizeIsSmaller(src)) + { + return false; + } return true; } #endif diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO.cs index 84518f300..13b122e17 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO.cs @@ -1,17 +1,12 @@ using System; -using System.Collections.Generic; using System.Linq; using UnityEngine; -#if UNITY_EDITOR -using System.Reflection; -using UnityEditor; -#endif namespace UniGLTF { - public class TextureIO : ITextureExporter + public class TextureIO { public static RenderTextureReadWrite GetColorSpace(glTFTextureTypes textureType) { @@ -39,24 +34,6 @@ namespace UniGLTF } } - public static glTFTextureTypes GetglTFTextureType(string shaderName, string propName) - { - switch (propName) - { - case "_MetallicGlossMap": - case "_OcclusionMap": - return glTFTextureTypes.OcclusionMetallicRoughness; - case "_BumpMap": - return glTFTextureTypes.Normal; - case "_Color": - case "_EmissionMap": - return glTFTextureTypes.SRGB; - default: - // Debug.LogWarning($"unknown texture property: {propName} as sRGB"); - return glTFTextureTypes.SRGB; - } - } - public static bool TryGetglTFTextureType(glTF glTf, int textureIndex, out glTFTextureTypes textureType) { foreach (var material in glTf.materials) @@ -74,74 +51,12 @@ namespace UniGLTF return false; } -#if UNITY_EDITOR - public static void MarkTextureAssetAsNormalMap(string assetPath) - { - if (string.IsNullOrEmpty(assetPath)) - { - return; - } - - var textureImporter = AssetImporter.GetAtPath(assetPath) as TextureImporter; - if (null == textureImporter) - { - return; - } - - //Debug.LogFormat("[MarkTextureAssetAsNormalMap] {0}", assetPath); - textureImporter.textureType = TextureImporterType.NormalMap; - textureImporter.SaveAndReimport(); - } -#endif - - public virtual IEnumerable<(Texture texture, glTFTextureTypes textureType)> GetTextures(Material m) - { - var props = ShaderPropExporter.PreShaderPropExporter.GetPropsForSupportedShader(m.shader.name); - if (props == null) - { - // unknown shader - yield return (m.mainTexture, glTFTextureTypes.SRGB); - } - - foreach (var prop in props.Properties) - { - - if (prop.ShaderPropertyType == ShaderPropExporter.ShaderPropertyType.TexEnv) - { - yield return (m.GetTexture(prop.Key), GetglTFTextureType(m.shader.name, prop.Key)); - } - } - } - - public virtual (Byte[] bytes, string mine) GetBytesWithMime(Texture texture, glTFTextureTypes textureType) + static (Byte[] bytes, string mine) GetBytesWithMime(Texture2D texture) { #if UNITY_EDITOR var path = UnityPath.FromAsset(texture); if (path.IsUnderAssetsFolder) { - var textureImporter = AssetImporter.GetAtPath(path.Value) as TextureImporter; - var getSizeMethod = typeof(TextureImporter).GetMethod("GetWidthAndHeight", BindingFlags.NonPublic | BindingFlags.Instance); - if (textureImporter != null && getSizeMethod != null) - { - var args = new object[2] { 0, 0 }; - getSizeMethod.Invoke(textureImporter, args); - var originalWidth = (int)args[0]; - var originalHeight = (int)args[1]; - - var originalSize = Mathf.Max(originalWidth, originalHeight); - var requiredMaxSize = textureImporter.maxTextureSize; - - // Resized exporting if MaxSize setting value is smaller than original image size. - if (originalSize > requiredMaxSize) - { - return - ( - TextureConverter.CopyTexture(texture, textureType, null).EncodeToPNG(), - "image/png" - ); - } - } - if (path.Extension == ".png") { return @@ -163,14 +78,14 @@ namespace UniGLTF return ( - TextureConverter.CopyTexture(texture, textureType, null).EncodeToPNG(), + texture.EncodeToPNG(), "image/png" ); } - public int ExportTexture(glTF gltf, int bufferIndex, Texture texture, glTFTextureTypes textureType) + static public int ExportTexture(glTF gltf, int bufferIndex, Texture2D texture) { - var bytesWithMime = GetBytesWithMime(texture, textureType); ; + var bytesWithMime = GetBytesWithMime(texture); // add view var view = gltf.buffers[bufferIndex].Append(bytesWithMime.bytes, glBufferTarget.NONE); diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs index abd51a5cb..7cb16bf6a 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs @@ -54,28 +54,6 @@ namespace UniGLTF return new MaterialExporter(); } - private ITextureExporter _textureExporter; - public ITextureExporter TextureExporter - { - get - { - if (_textureExporter != null) - { - return _textureExporter; - } - else - { - _textureExporter = new TextureIO(); - return _textureExporter; - } - } - set - { - _textureExporter = value; - } - } - - /// /// このエクスポーターがサポートするExtension /// @@ -206,6 +184,12 @@ namespace UniGLTF var materialExporter = CreateMaterialExporter(); glTF.materials = Materials.Select(x => materialExporter.ExportMaterial(x, TextureManager)).ToList(); + + for (int i = 0; i < TextureManager.Exported.Count; ++i) + { + var unityTexture = TextureManager.Exported[i]; + TextureIO.ExportTexture(glTF, bufferIndex, unityTexture); + } #endregion #region Meshes diff --git a/Assets/VRM/Runtime/IO/VRMExporter.cs b/Assets/VRM/Runtime/IO/VRMExporter.cs index fdbb0507b..e7f8237b5 100644 --- a/Assets/VRM/Runtime/IO/VRMExporter.cs +++ b/Assets/VRM/Runtime/IO/VRMExporter.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Linq; using UniGLTF; using UniJSON; @@ -113,7 +112,7 @@ namespace VRM VRM.meta.title = meta.Title; if (meta.Thumbnail != null) { - VRM.meta.texture = TextureExporter.ExportTexture(glTF, glTF.buffers.Count - 1, meta.Thumbnail, glTFTextureTypes.SRGB); + VRM.meta.texture = TextureIO.ExportTexture(glTF, glTF.buffers.Count - 1, meta.Thumbnail); } VRM.meta.licenseType = meta.LicenseType; @@ -138,7 +137,7 @@ namespace VRM VRM.meta.title = meta.Title; if (meta.Thumbnail != null) { - VRM.meta.texture = TextureExporter.ExportTexture(glTF, glTF.buffers.Count - 1, meta.Thumbnail, glTFTextureTypes.SRGB); + VRM.meta.texture = TextureIO.ExportTexture(glTF, glTF.buffers.Count - 1, meta.Thumbnail); } // ussage permission From 755c907791cccd76e420bfa9e2d9f51514fb15eb Mon Sep 17 00:00:00 2001 From: ousttrue Date: Mon, 15 Mar 2021 18:57:29 +0900 Subject: [PATCH 16/28] rename --- .../Runtime/UniGLTF/IO/{TextureLoader.meta => TextureIO.meta} | 2 +- .../UniGLTF/IO/{TextureLoader => TextureIO}/GetTextureParam.cs | 0 .../IO/{TextureLoader => TextureIO}/GetTextureParam.cs.meta | 0 .../IO/{TextureLoader => TextureIO}/GltfTextureLoader.cs | 0 .../IO/{TextureLoader => TextureIO}/GltfTextureLoader.cs.meta | 0 .../UniGLTF/IO/{TextureLoader => TextureIO}/TextureFactory.cs | 0 .../IO/{TextureLoader => TextureIO}/TextureFactory.cs.meta | 0 .../UnityWebRequestTextureLoader.cs | 0 .../UnityWebRequestTextureLoader.cs.meta | 0 9 files changed, 1 insertion(+), 1 deletion(-) rename Assets/UniGLTF/Runtime/UniGLTF/IO/{TextureLoader.meta => TextureIO.meta} (77%) rename Assets/UniGLTF/Runtime/UniGLTF/IO/{TextureLoader => TextureIO}/GetTextureParam.cs (100%) rename Assets/UniGLTF/Runtime/UniGLTF/IO/{TextureLoader => TextureIO}/GetTextureParam.cs.meta (100%) rename Assets/UniGLTF/Runtime/UniGLTF/IO/{TextureLoader => TextureIO}/GltfTextureLoader.cs (100%) rename Assets/UniGLTF/Runtime/UniGLTF/IO/{TextureLoader => TextureIO}/GltfTextureLoader.cs.meta (100%) rename Assets/UniGLTF/Runtime/UniGLTF/IO/{TextureLoader => TextureIO}/TextureFactory.cs (100%) rename Assets/UniGLTF/Runtime/UniGLTF/IO/{TextureLoader => TextureIO}/TextureFactory.cs.meta (100%) rename Assets/UniGLTF/Runtime/UniGLTF/IO/{TextureLoader => TextureIO}/UnityWebRequestTextureLoader.cs (100%) rename Assets/UniGLTF/Runtime/UniGLTF/IO/{TextureLoader => TextureIO}/UnityWebRequestTextureLoader.cs.meta (100%) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO.meta similarity index 77% rename from Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader.meta rename to Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO.meta index 1ef61d2aa..4795a6084 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader.meta +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: a4bd2e8f388fb204186d743f337ddb83 +guid: 2b2634da9b588264c8ea8a81578080b2 folderAsset: yes DefaultImporter: externalObjects: {} diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/GetTextureParam.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GetTextureParam.cs similarity index 100% rename from Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/GetTextureParam.cs rename to Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GetTextureParam.cs diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/GetTextureParam.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GetTextureParam.cs.meta similarity index 100% rename from Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/GetTextureParam.cs.meta rename to Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GetTextureParam.cs.meta diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/GltfTextureLoader.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureLoader.cs similarity index 100% rename from Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/GltfTextureLoader.cs rename to Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureLoader.cs diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/GltfTextureLoader.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureLoader.cs.meta similarity index 100% rename from Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/GltfTextureLoader.cs.meta rename to Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureLoader.cs.meta diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/TextureFactory.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureFactory.cs similarity index 100% rename from Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/TextureFactory.cs rename to Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureFactory.cs diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/TextureFactory.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureFactory.cs.meta similarity index 100% rename from Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/TextureFactory.cs.meta rename to Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureFactory.cs.meta diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/UnityWebRequestTextureLoader.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/UnityWebRequestTextureLoader.cs similarity index 100% rename from Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/UnityWebRequestTextureLoader.cs rename to Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/UnityWebRequestTextureLoader.cs diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/UnityWebRequestTextureLoader.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/UnityWebRequestTextureLoader.cs.meta similarity index 100% rename from Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/UnityWebRequestTextureLoader.cs.meta rename to Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/UnityWebRequestTextureLoader.cs.meta From f6aeef30edc83c57620f5a7eee67bf23671b0991 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Mon, 15 Mar 2021 19:25:06 +0900 Subject: [PATCH 17/28] rename --- .../Editor/UniGLTF/AssetTextureLoader.cs | 2 +- .../Runtime/UniGLTF/IO/MaterialExporter.cs | 12 +- .../IO/TextureConverter/TextureConverter.cs | 2 +- .../UniGLTF/Runtime/UniGLTF/IO/TextureIO.cs | 118 ------------- .../UniGLTF/IO/TextureIO/ColorSpace.cs | 52 ++++++ .../ColorSpace.cs.meta} | 5 +- .../UniGLTF/IO/TextureIO/GltfTextureLoader.cs | 2 +- .../TextureExporter.cs} | 167 +++++++++++++----- .../TextureExporter.cs.meta} | 5 +- .../IO/{ => TextureIO}/TextureSamplerUtil.cs | 0 .../TextureSamplerUtil.cs.meta | 2 +- .../Runtime/UniGLTF/IO/gltfExporter.cs | 6 +- Assets/UniGLTF/Tests/UniGLTF/MaterialTests.cs | 4 +- Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs | 2 +- .../Editor/Tests/VRMMaterialTests.cs | 2 +- Assets/VRM/Runtime/IO/VRMExporter.cs | 4 +- 16 files changed, 201 insertions(+), 184 deletions(-) delete mode 100644 Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO.cs create mode 100644 Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/ColorSpace.cs rename Assets/UniGLTF/Runtime/UniGLTF/IO/{TextureConverter/TextureExportManager.cs.meta => TextureIO/ColorSpace.cs.meta} (69%) rename Assets/UniGLTF/Runtime/UniGLTF/IO/{TextureConverter/TextureExportManager.cs => TextureIO/TextureExporter.cs} (59%) rename Assets/UniGLTF/Runtime/UniGLTF/IO/{TextureIO.cs.meta => TextureIO/TextureExporter.cs.meta} (69%) rename Assets/UniGLTF/Runtime/UniGLTF/IO/{ => TextureIO}/TextureSamplerUtil.cs (100%) rename Assets/UniGLTF/Runtime/UniGLTF/IO/{ => TextureIO}/TextureSamplerUtil.cs.meta (83%) diff --git a/Assets/UniGLTF/Editor/UniGLTF/AssetTextureLoader.cs b/Assets/UniGLTF/Editor/UniGLTF/AssetTextureLoader.cs index 818307d5d..21f3d5e09 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/AssetTextureLoader.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/AssetTextureLoader.cs @@ -8,7 +8,7 @@ namespace UniGLTF public static Task LoadTaskAsync(UnityPath m_assetPath, glTF gltf, int textureIndex) { - var colorSpace = TextureIO.GetColorSpace(gltf, textureIndex); + var colorSpace = gltf.GetColorSpace(textureIndex); // // texture from assets diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialExporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialExporter.cs index 28e0f799e..0f33eb548 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialExporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialExporter.cs @@ -14,12 +14,12 @@ namespace UniGLTF public interface IMaterialExporter { - glTFMaterial ExportMaterial(Material m, TextureExportManager textureManager); + glTFMaterial ExportMaterial(Material m, TextureExporter textureManager); } public class MaterialExporter : IMaterialExporter { - public virtual glTFMaterial ExportMaterial(Material m, TextureExportManager textureManager) + public virtual glTFMaterial ExportMaterial(Material m, TextureExporter textureManager) { var material = CreateMaterial(m); @@ -33,7 +33,7 @@ namespace UniGLTF return material; } - static void Export_Color(Material m, TextureExportManager textureManager, glTFMaterial material) + static void Export_Color(Material m, TextureExporter textureManager, glTFMaterial material) { if (m.HasProperty("_Color")) { @@ -61,7 +61,7 @@ namespace UniGLTF /// /// /// - static void Export_OcclusionMetallicRoughness(Material m, TextureExportManager textureManager, glTFMaterial material) + static void Export_OcclusionMetallicRoughness(Material m, TextureExporter textureManager, glTFMaterial material) { Texture metallicSmoothTexture = default; float smoothness = 1.0f; @@ -124,7 +124,7 @@ namespace UniGLTF } } - static void Export_Normal(Material m, TextureExportManager textureManager, glTFMaterial material) + static void Export_Normal(Material m, TextureExporter textureManager, glTFMaterial material) { if (m.HasProperty("_BumpMap")) { @@ -146,7 +146,7 @@ namespace UniGLTF } } - static void Export_Emission(Material m, TextureExportManager textureManager, glTFMaterial material) + static void Export_Emission(Material m, TextureExporter textureManager, glTFMaterial material) { if (m.IsKeywordEnabled("_EMISSION") == false) return; diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/TextureConverter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/TextureConverter.cs index 34d4abe9c..fbc9ce5eb 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/TextureConverter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/TextureConverter.cs @@ -129,7 +129,7 @@ namespace UniGLTF public static Texture2D CopyTexture(Texture src, glTFTextureTypes textureType, Material material) { Texture2D dst = null; - RenderTextureReadWrite colorSpace = TextureIO.GetColorSpace(textureType); + RenderTextureReadWrite colorSpace = textureType.GetColorSpace(); var renderTexture = new RenderTexture(src.width, src.height, 0, RenderTextureFormat.ARGB32, colorSpace); using (var scope = new ColorSpaceScope(colorSpace)) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO.cs deleted file mode 100644 index 13b122e17..000000000 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO.cs +++ /dev/null @@ -1,118 +0,0 @@ -using System; -using System.Linq; -using UnityEngine; - - -namespace UniGLTF -{ - - public class TextureIO - { - public static RenderTextureReadWrite GetColorSpace(glTFTextureTypes textureType) - { - switch (textureType) - { - case glTFTextureTypes.SRGB: - return RenderTextureReadWrite.sRGB; - case glTFTextureTypes.OcclusionMetallicRoughness: - case glTFTextureTypes.Normal: - return RenderTextureReadWrite.Linear; - default: - throw new NotImplementedException(); - } - } - - public static RenderTextureReadWrite GetColorSpace(glTF gltf, int textureIndex) - { - if (TextureIO.TryGetglTFTextureType(gltf, textureIndex, out glTFTextureTypes textureType)) - { - return GetColorSpace(textureType); - } - else - { - return RenderTextureReadWrite.sRGB; - } - } - - public static bool TryGetglTFTextureType(glTF glTf, int textureIndex, out glTFTextureTypes textureType) - { - foreach (var material in glTf.materials) - { - var textureInfo = material.GetTextures().FirstOrDefault(x => (x != null) && x.index == textureIndex); - if (textureInfo != null) - { - textureType = textureInfo.TextureType; - return true; - } - } - - // textureIndex is not used by Material. - textureType = default; - return false; - } - - static (Byte[] bytes, string mine) GetBytesWithMime(Texture2D texture) - { -#if UNITY_EDITOR - var path = UnityPath.FromAsset(texture); - if (path.IsUnderAssetsFolder) - { - if (path.Extension == ".png") - { - return - ( - System.IO.File.ReadAllBytes(path.FullPath), - "image/png" - ); - } - if (path.Extension == ".jpg") - { - return - ( - System.IO.File.ReadAllBytes(path.FullPath), - "image/jpeg" - ); - } - } -#endif - - return - ( - texture.EncodeToPNG(), - "image/png" - ); - } - - static public int ExportTexture(glTF gltf, int bufferIndex, Texture2D texture) - { - var bytesWithMime = GetBytesWithMime(texture); - - // add view - var view = gltf.buffers[bufferIndex].Append(bytesWithMime.bytes, glBufferTarget.NONE); - var viewIndex = gltf.AddBufferView(view); - - // add image - var imageIndex = gltf.images.Count; - gltf.images.Add(new glTFImage - { - name = GetTextureParam.RemoveSuffix(texture.name), - bufferView = viewIndex, - mimeType = bytesWithMime.mine, - }); - - // add sampler - var samplerIndex = gltf.samplers.Count; - var sampler = TextureSamplerUtil.Export(texture); - gltf.samplers.Add(sampler); - - // add texture - gltf.textures.Add(new glTFTexture - { - sampler = samplerIndex, - source = imageIndex, - }); - - return imageIndex; - } - } -} diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/ColorSpace.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/ColorSpace.cs new file mode 100644 index 000000000..1274075ee --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/ColorSpace.cs @@ -0,0 +1,52 @@ +using System; +using System.Linq; +using UnityEngine; + +namespace UniGLTF +{ + public static class ColorSpace + { + public static RenderTextureReadWrite GetColorSpace(this glTFTextureTypes textureType) + { + switch (textureType) + { + case glTFTextureTypes.SRGB: + return RenderTextureReadWrite.sRGB; + case glTFTextureTypes.OcclusionMetallicRoughness: + case glTFTextureTypes.Normal: + return RenderTextureReadWrite.Linear; + default: + throw new NotImplementedException(); + } + } + + public static bool TryGetglTFTextureType(this glTF glTf, int textureIndex, out glTFTextureTypes textureType) + { + foreach (var material in glTf.materials) + { + var textureInfo = material.GetTextures().FirstOrDefault(x => (x != null) && x.index == textureIndex); + if (textureInfo != null) + { + textureType = textureInfo.TextureType; + return true; + } + } + + // textureIndex is not used by Material. + textureType = default; + return false; + } + + public static RenderTextureReadWrite GetColorSpace(this glTF gltf, int textureIndex) + { + if (TryGetglTFTextureType(gltf, textureIndex, out glTFTextureTypes textureType)) + { + return GetColorSpace(textureType); + } + else + { + return RenderTextureReadWrite.sRGB; + } + } + } +} diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/TextureExportManager.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/ColorSpace.cs.meta similarity index 69% rename from Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/TextureExportManager.cs.meta rename to Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/ColorSpace.cs.meta index 5048e0474..d5bf0544c 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/TextureExportManager.cs.meta +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/ColorSpace.cs.meta @@ -1,8 +1,7 @@ fileFormatVersion: 2 -guid: 435499f173753ac418331fe0f967b789 -timeCreated: 1541561421 -licenseType: Free +guid: 1a3edb24329fd454db97cac12f30c0d8 MonoImporter: + externalObjects: {} serializedVersion: 2 defaultReferences: [] executionOrder: 0 diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureLoader.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureLoader.cs index c5ff2934f..28bd4f201 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureLoader.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureLoader.cs @@ -36,7 +36,7 @@ namespace UniGLTF // // texture from image(png etc) bytes // - var colorSpace = TextureIO.GetColorSpace(gltf, textureIndex); + var colorSpace = gltf.GetColorSpace(textureIndex); var texture = new Texture2D(2, 2, TextureFormat.ARGB32, false, colorSpace == RenderTextureReadWrite.Linear); texture.name = gltf.textures[textureIndex].name; if (imageBytes != null) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/TextureExportManager.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureExporter.cs similarity index 59% rename from Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/TextureExportManager.cs rename to Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureExporter.cs index 6a53c45fa..1c76c1c03 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/TextureExportManager.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureExporter.cs @@ -11,7 +11,7 @@ namespace UniGLTF /// /// glTF にエクスポートする Texture2D を蓄えて index を確定させる /// - public class TextureExportManager + public class TextureExporter { struct ExportKey { @@ -29,11 +29,31 @@ namespace UniGLTF } } Dictionary m_exportMap = new Dictionary(); - List m_exported = new List(); - public IReadOnlyList Exported => m_exported; + /// + /// Export する Texture2D のリスト。これが gltf.textures になる + /// + /// + /// + public readonly List Exported = new List(); - static bool CopyIfMaxTextureSizeIsSmaller(Texture src/*, glTFTextureTypes textureType, out Texture2D dst*/) + /// + /// Texture の export index を得る + /// + /// + /// + /// + public int GetTextureIndex(Texture src, glTFTextureTypes textureType) + { + return m_exportMap[new ExportKey(src, textureType)]; + } + + /// + /// TextureImporter.maxTextureSize が元のテクスチャーより小さいか否かの判定 + /// + /// + /// + static bool CopyIfMaxTextureSizeIsSmaller(Texture src) { #if UNITY_EDITOR var textureImporter = AssetImporter.GetAtPath(UnityPath.FromAsset(src).Value) as TextureImporter; @@ -47,27 +67,32 @@ namespace UniGLTF var originalSize = Mathf.Max(originalWidth, originalHeight); if (textureImporter.maxTextureSize < originalSize) { - // export resized texture. - // this has textureImporter.maxTextureSize - // dst = TextureConverter.CopyTexture(src, textureType, null); return true; } } #endif - - // dst = default; return false; } /// - /// Texture の export index を得る + /// 元の Asset が存在して、 TextureImporter に設定された画像サイズが小さくない /// /// - /// + /// /// - public int GetTextureIndex(Texture src, glTFTextureTypes textureType) + static bool UseAsset(Texture2D texture2D) { - return m_exportMap[new ExportKey(src, textureType)]; +#if UNITY_EDITOR + if (texture2D != null && !string.IsNullOrEmpty(UnityEditor.AssetDatabase.GetAssetPath(texture2D))) + { + if (CopyIfMaxTextureSizeIsSmaller(texture2D)) + { + return false; + } + return true; + } +#endif + return false; } /// @@ -89,8 +114,9 @@ namespace UniGLTF } // get Texture2D - index = m_exported.Count; - if (src is Texture2D texture2D && !CopyIfMaxTextureSizeIsSmaller(src)) + index = Exported.Count; + var texture2D = src as Texture2D; + if (UseAsset(texture2D)) { // do nothing } @@ -98,7 +124,7 @@ namespace UniGLTF { texture2D = TextureConverter.CopyTexture(src, glTFTextureTypes.SRGB, null); } - m_exported.Add(texture2D); + Exported.Add(texture2D); m_exportMap.Add(new ExportKey(src, glTFTextureTypes.SRGB), index); return index; @@ -131,10 +157,10 @@ namespace UniGLTF // // Unity と glTF で互換性が無いので必ず変換が必用 // - index = m_exported.Count; + index = Exported.Count; var texture2D = OcclusionMetallicRoughnessConverter.Export(metallicSmoothTexture, smoothness, occlusionTexture); - m_exported.Add(texture2D); + Exported.Add(texture2D); m_exportMap.Add(new ExportKey(metallicSmoothTexture, glTFTextureTypes.OcclusionMetallicRoughness), index); if (occlusionTexture != metallicSmoothTexture && occlusionTexture != null) { @@ -144,25 +170,6 @@ namespace UniGLTF return index; } - static bool UseNormalAsset(Texture src, out Texture2D texture2D) - { -#if UNITY_EDITOR - // asset として存在して textureImporter.textureType = TextureImporterType.NormalMap - texture2D = src as Texture2D; - if (texture2D != null && !string.IsNullOrEmpty(UnityEditor.AssetDatabase.GetAssetPath(src))) - { - if (CopyIfMaxTextureSizeIsSmaller(src)) - { - return false; - } - return true; - } -#endif - - texture2D = default; - return false; - } - /// /// Normal のテクスチャを変換する /// @@ -182,9 +189,9 @@ namespace UniGLTF } // get Texture2D - index = m_exported.Count; - Texture2D texture2D = default; - if (UseNormalAsset(src, out texture2D)) + index = Exported.Count; + var texture2D = src as Texture2D; + if (UseAsset(texture2D)) { // EditorAsset を使うので変換不要 } @@ -194,10 +201,88 @@ namespace UniGLTF texture2D = NormalConverter.Export(src); } - m_exported.Add(texture2D); + Exported.Add(texture2D); m_exportMap.Add(new ExportKey(src, glTFTextureTypes.Normal), index); return index; } + + /// + /// 画像のバイト列を得る + /// + /// + /// + /// + static (Byte[] bytes, string mine) GetBytesWithMime(Texture2D texture) + { +#if UNITY_EDITOR + var path = UnityPath.FromAsset(texture); + if (path.IsUnderAssetsFolder) + { + if (path.Extension == ".png") + { + return + ( + System.IO.File.ReadAllBytes(path.FullPath), + "image/png" + ); + } + if (path.Extension == ".jpg") + { + return + ( + System.IO.File.ReadAllBytes(path.FullPath), + "image/jpeg" + ); + } + } +#endif + + return + ( + texture.EncodeToPNG(), + "image/png" + ); + } + + /// + /// + /// + /// + /// + /// + /// + static public int ExportTexture(glTF gltf, int bufferIndex, Texture2D texture) + { + var bytesWithMime = GetBytesWithMime(texture); + + // add view + var view = gltf.buffers[bufferIndex].Append(bytesWithMime.bytes, glBufferTarget.NONE); + var viewIndex = gltf.AddBufferView(view); + + // add image + var imageIndex = gltf.images.Count; + gltf.images.Add(new glTFImage + { + name = GetTextureParam.RemoveSuffix(texture.name), + bufferView = viewIndex, + mimeType = bytesWithMime.mine, + }); + + // add sampler + var samplerIndex = gltf.samplers.Count; + var sampler = TextureSamplerUtil.Export(texture); + gltf.samplers.Add(sampler); + + // add texture + var textureIndex = gltf.textures.Count; + gltf.textures.Add(new glTFTexture + { + sampler = samplerIndex, + source = imageIndex, + }); + + return textureIndex; + } } } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureExporter.cs.meta similarity index 69% rename from Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO.cs.meta rename to Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureExporter.cs.meta index 2b85d8e5d..9c239e48f 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO.cs.meta +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureExporter.cs.meta @@ -1,8 +1,7 @@ fileFormatVersion: 2 -guid: eca1330a83e17a14eb99fc6ea1697922 -timeCreated: 1533533316 -licenseType: Free +guid: 65fdfff6cc4b1e14a882259e903fc830 MonoImporter: + externalObjects: {} serializedVersion: 2 defaultReferences: [] executionOrder: 0 diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureSamplerUtil.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureSamplerUtil.cs similarity index 100% rename from Assets/UniGLTF/Runtime/UniGLTF/IO/TextureSamplerUtil.cs rename to Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureSamplerUtil.cs diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureSamplerUtil.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureSamplerUtil.cs.meta similarity index 83% rename from Assets/UniGLTF/Runtime/UniGLTF/IO/TextureSamplerUtil.cs.meta rename to Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureSamplerUtil.cs.meta index f30e2fb67..cfc44057e 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureSamplerUtil.cs.meta +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureSamplerUtil.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: f3929edbda61f9346906bfab93411b98 +guid: 62f23c5ca623a9f4083c25a63b2c82af MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs index 7cb16bf6a..5ac336716 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs @@ -47,7 +47,7 @@ namespace UniGLTF private set; } - public TextureExportManager TextureManager; + public TextureExporter TextureManager; protected virtual IMaterialExporter CreateMaterialExporter() { @@ -180,7 +180,7 @@ namespace UniGLTF #region Materials and Textures Materials = Nodes.SelectMany(x => x.GetSharedMaterials()).Where(x => x != null).Distinct().ToList(); - TextureManager = new TextureExportManager(); + TextureManager = new TextureExporter(); var materialExporter = CreateMaterialExporter(); glTF.materials = Materials.Select(x => materialExporter.ExportMaterial(x, TextureManager)).ToList(); @@ -188,7 +188,7 @@ namespace UniGLTF for (int i = 0; i < TextureManager.Exported.Count; ++i) { var unityTexture = TextureManager.Exported[i]; - TextureIO.ExportTexture(glTF, bufferIndex, unityTexture); + TextureExporter.ExportTexture(glTF, bufferIndex, unityTexture); } #endregion diff --git a/Assets/UniGLTF/Tests/UniGLTF/MaterialTests.cs b/Assets/UniGLTF/Tests/UniGLTF/MaterialTests.cs index 176bdf872..f08778cd1 100644 --- a/Assets/UniGLTF/Tests/UniGLTF/MaterialTests.cs +++ b/Assets/UniGLTF/Tests/UniGLTF/MaterialTests.cs @@ -19,7 +19,7 @@ namespace UniGLTF filterMode = FilterMode.Bilinear, }; - var textureManager = new TextureExportManager(); + var textureManager = new TextureExporter(); var srcMaterial = new Material(Shader.Find("Standard")); var offset = new Vector2(0.3f, 0.2f); @@ -255,7 +255,7 @@ namespace UniGLTF material.SetColor("_EmissionColor", new Color(0, 1, 2, 1)); material.EnableKeyword("_EMISSION"); var materialExporter = new MaterialExporter(); - var textureExportManager = new TextureExportManager(); + var textureExportManager = new TextureExporter(); var gltfMaterial = materialExporter.ExportMaterial(material, textureExportManager); Assert.AreEqual(gltfMaterial.emissiveFactor, new float[] { 0, 0.5f, 1 }); diff --git a/Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs b/Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs index f5e9c335c..6fbca157b 100644 --- a/Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs +++ b/Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs @@ -14,7 +14,7 @@ namespace UniGLTF wrapMode = TextureWrapMode.Clamp, filterMode = FilterMode.Trilinear, }; - var textureManager = new TextureExportManager(); + var textureManager = new TextureExporter(); var material = new Material(Shader.Find("Standard")); material.mainTexture = tex0; diff --git a/Assets/VRM.Samples/Editor/Tests/VRMMaterialTests.cs b/Assets/VRM.Samples/Editor/Tests/VRMMaterialTests.cs index aed0c9974..cf75d926a 100644 --- a/Assets/VRM.Samples/Editor/Tests/VRMMaterialTests.cs +++ b/Assets/VRM.Samples/Editor/Tests/VRMMaterialTests.cs @@ -10,7 +10,7 @@ namespace VRM.Samples { var material = Resources.Load(resourceName); var exporter = new VRMMaterialExporter(); - var textureManager = new UniGLTF.TextureExportManager(); + var textureManager = new UniGLTF.TextureExporter(); var exported = exporter.ExportMaterial(material, textureManager); // parse glTFExtensionExport to glTFExtensionImport diff --git a/Assets/VRM/Runtime/IO/VRMExporter.cs b/Assets/VRM/Runtime/IO/VRMExporter.cs index e7f8237b5..980cef846 100644 --- a/Assets/VRM/Runtime/IO/VRMExporter.cs +++ b/Assets/VRM/Runtime/IO/VRMExporter.cs @@ -112,7 +112,7 @@ namespace VRM VRM.meta.title = meta.Title; if (meta.Thumbnail != null) { - VRM.meta.texture = TextureIO.ExportTexture(glTF, glTF.buffers.Count - 1, meta.Thumbnail); + VRM.meta.texture = TextureExporter.ExportTexture(glTF, glTF.buffers.Count - 1, meta.Thumbnail); } VRM.meta.licenseType = meta.LicenseType; @@ -137,7 +137,7 @@ namespace VRM VRM.meta.title = meta.Title; if (meta.Thumbnail != null) { - VRM.meta.texture = TextureIO.ExportTexture(glTF, glTF.buffers.Count - 1, meta.Thumbnail); + VRM.meta.texture = TextureExporter.ExportTexture(glTF, glTF.buffers.Count - 1, meta.Thumbnail); } // ussage permission From 83ac94423cd9e14831a62fef2c35d558c49423db Mon Sep 17 00:00:00 2001 From: ousttrue Date: Mon, 15 Mar 2021 19:26:10 +0900 Subject: [PATCH 18/28] rename --- .../UniGLTF/IO/{MaterialLoader.meta => MaterialIO.meta} | 2 +- .../GltfTextureEnumerator.cs | 0 .../GltfTextureEnumerator.cs.meta | 0 .../UniGLTF/IO/{ => MaterialIO}/MaterialExporter.cs | 0 .../UniGLTF/IO/{ => MaterialIO}/MaterialExporter.cs.meta | 5 ++--- .../IO/{MaterialLoader => MaterialIO}/MaterialFactory.cs | 0 .../MaterialFactory.cs.meta | 0 .../IO/{MaterialLoader => MaterialIO}/PBRMaterialItem.cs | 0 .../PBRMaterialItem.cs.meta | 0 .../{MaterialLoader => MaterialIO}/UnlitMaterialItem.cs | 0 .../UnlitMaterialItem.cs.meta | 0 Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter.meta | 8 -------- .../IO/{TextureConverter => TextureIO}/NormalConverter.cs | 0 .../NormalConverter.cs.meta | 2 +- .../OcclusionMetallicRoughnessConverter.cs | 0 .../OcclusionMetallicRoughnessConverter.cs.meta | 2 +- .../{TextureConverter => TextureIO}/TextureConverter.cs | 0 .../TextureConverter.cs.meta | 2 +- 18 files changed, 6 insertions(+), 15 deletions(-) rename Assets/UniGLTF/Runtime/UniGLTF/IO/{MaterialLoader.meta => MaterialIO.meta} (77%) rename Assets/UniGLTF/Runtime/UniGLTF/IO/{MaterialLoader => MaterialIO}/GltfTextureEnumerator.cs (100%) rename Assets/UniGLTF/Runtime/UniGLTF/IO/{MaterialLoader => MaterialIO}/GltfTextureEnumerator.cs.meta (100%) rename Assets/UniGLTF/Runtime/UniGLTF/IO/{ => MaterialIO}/MaterialExporter.cs (100%) rename Assets/UniGLTF/Runtime/UniGLTF/IO/{ => MaterialIO}/MaterialExporter.cs.meta (69%) rename Assets/UniGLTF/Runtime/UniGLTF/IO/{MaterialLoader => MaterialIO}/MaterialFactory.cs (100%) rename Assets/UniGLTF/Runtime/UniGLTF/IO/{MaterialLoader => MaterialIO}/MaterialFactory.cs.meta (100%) rename Assets/UniGLTF/Runtime/UniGLTF/IO/{MaterialLoader => MaterialIO}/PBRMaterialItem.cs (100%) rename Assets/UniGLTF/Runtime/UniGLTF/IO/{MaterialLoader => MaterialIO}/PBRMaterialItem.cs.meta (100%) rename Assets/UniGLTF/Runtime/UniGLTF/IO/{MaterialLoader => MaterialIO}/UnlitMaterialItem.cs (100%) rename Assets/UniGLTF/Runtime/UniGLTF/IO/{MaterialLoader => MaterialIO}/UnlitMaterialItem.cs.meta (100%) delete mode 100644 Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter.meta rename Assets/UniGLTF/Runtime/UniGLTF/IO/{TextureConverter => TextureIO}/NormalConverter.cs (100%) rename Assets/UniGLTF/Runtime/UniGLTF/IO/{TextureConverter => TextureIO}/NormalConverter.cs.meta (83%) rename Assets/UniGLTF/Runtime/UniGLTF/IO/{TextureConverter => TextureIO}/OcclusionMetallicRoughnessConverter.cs (100%) rename Assets/UniGLTF/Runtime/UniGLTF/IO/{TextureConverter => TextureIO}/OcclusionMetallicRoughnessConverter.cs.meta (83%) rename Assets/UniGLTF/Runtime/UniGLTF/IO/{TextureConverter => TextureIO}/TextureConverter.cs (100%) rename Assets/UniGLTF/Runtime/UniGLTF/IO/{TextureConverter => TextureIO}/TextureConverter.cs.meta (83%) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO.meta similarity index 77% rename from Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader.meta rename to Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO.meta index eae1397bf..f92034793 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader.meta +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: bafc2eb5323419940a4991a8045bedc3 +guid: 84cb103042e9924459b7dfc465ef4cef folderAsset: yes DefaultImporter: externalObjects: {} diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/GltfTextureEnumerator.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfTextureEnumerator.cs similarity index 100% rename from Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/GltfTextureEnumerator.cs rename to Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfTextureEnumerator.cs diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/GltfTextureEnumerator.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfTextureEnumerator.cs.meta similarity index 100% rename from Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/GltfTextureEnumerator.cs.meta rename to Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfTextureEnumerator.cs.meta diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialExporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/MaterialExporter.cs similarity index 100% rename from Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialExporter.cs rename to Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/MaterialExporter.cs diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialExporter.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/MaterialExporter.cs.meta similarity index 69% rename from Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialExporter.cs.meta rename to Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/MaterialExporter.cs.meta index e441123c7..a3e167778 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialExporter.cs.meta +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/MaterialExporter.cs.meta @@ -1,8 +1,7 @@ fileFormatVersion: 2 -guid: 99662edfbf59e8f458bcba3b62b13050 -timeCreated: 1533622882 -licenseType: Free +guid: 89f71b1d593633c47bfd4cfef050ae0d MonoImporter: + externalObjects: {} serializedVersion: 2 defaultReferences: [] executionOrder: 0 diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/MaterialFactory.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/MaterialFactory.cs similarity index 100% rename from Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/MaterialFactory.cs rename to Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/MaterialFactory.cs diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/MaterialFactory.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/MaterialFactory.cs.meta similarity index 100% rename from Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/MaterialFactory.cs.meta rename to Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/MaterialFactory.cs.meta diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/PBRMaterialItem.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/PBRMaterialItem.cs similarity index 100% rename from Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/PBRMaterialItem.cs rename to Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/PBRMaterialItem.cs diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/PBRMaterialItem.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/PBRMaterialItem.cs.meta similarity index 100% rename from Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/PBRMaterialItem.cs.meta rename to Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/PBRMaterialItem.cs.meta diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/UnlitMaterialItem.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/UnlitMaterialItem.cs similarity index 100% rename from Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/UnlitMaterialItem.cs rename to Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/UnlitMaterialItem.cs diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/UnlitMaterialItem.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/UnlitMaterialItem.cs.meta similarity index 100% rename from Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/UnlitMaterialItem.cs.meta rename to Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/UnlitMaterialItem.cs.meta diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter.meta deleted file mode 100644 index 52d888649..000000000 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter.meta +++ /dev/null @@ -1,8 +0,0 @@ -fileFormatVersion: 2 -guid: 79721770f23ba6748abc1720cd99ad74 -folderAsset: yes -DefaultImporter: - externalObjects: {} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/NormalConverter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/NormalConverter.cs similarity index 100% rename from Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/NormalConverter.cs rename to Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/NormalConverter.cs diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/NormalConverter.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/NormalConverter.cs.meta similarity index 83% rename from Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/NormalConverter.cs.meta rename to Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/NormalConverter.cs.meta index 8e2421c1a..e4d7ac38d 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/NormalConverter.cs.meta +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/NormalConverter.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 5c6170d9d89ac7f43b6860805e5e6214 +guid: 3640a6aad209c3f4d9178b078af8362f MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionMetallicRoughnessConverter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/OcclusionMetallicRoughnessConverter.cs similarity index 100% rename from Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionMetallicRoughnessConverter.cs rename to Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/OcclusionMetallicRoughnessConverter.cs diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionMetallicRoughnessConverter.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/OcclusionMetallicRoughnessConverter.cs.meta similarity index 83% rename from Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionMetallicRoughnessConverter.cs.meta rename to Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/OcclusionMetallicRoughnessConverter.cs.meta index 1c32f4f89..a00fd7507 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionMetallicRoughnessConverter.cs.meta +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/OcclusionMetallicRoughnessConverter.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 55d0c8cd2f5154f488e47f7bb1e6fa60 +guid: 9de35f7c2673d5d48b84d494e71f25ff MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/TextureConverter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureConverter.cs similarity index 100% rename from Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/TextureConverter.cs rename to Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureConverter.cs diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/TextureConverter.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureConverter.cs.meta similarity index 83% rename from Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/TextureConverter.cs.meta rename to Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureConverter.cs.meta index 818d31078..d26c96ec8 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/TextureConverter.cs.meta +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureConverter.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 0342907cb8901f44b8792016ac152fcd +guid: 843bfd183520d064ea63b4716d26b7cc MonoImporter: externalObjects: {} serializedVersion: 2 From 18fd38182d3cd104e5c14b6a0bad8b53ca6644d2 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Mon, 15 Mar 2021 20:01:20 +0900 Subject: [PATCH 19/28] TextureImporterConfigurator --- .../ScriptedImporter/ScriptedImporterImpl.cs | 7 ++ .../ScriptedImporter/TextureExtractor.cs | 30 +------- .../UniGLTF/TextureImporterConfigurator.cs | 74 +++++++++++++++++++ .../TextureImporterConfigurator.cs.meta | 11 +++ .../Runtime/UniGLTF/IO/ImporterContext.cs | 1 + .../UniGLTF/IO/MaterialIO/PBRMaterialItem.cs | 5 +- .../UniGLTF/IO/TextureIO/TextureFactory.cs | 9 ++- Assets/VRM/Editor/Format/VRMImporterMenu.cs | 13 +++- .../Editor/Format/vrmAssetPostprocessor.cs | 5 ++ 9 files changed, 120 insertions(+), 35 deletions(-) create mode 100644 Assets/UniGLTF/Editor/UniGLTF/TextureImporterConfigurator.cs create mode 100644 Assets/UniGLTF/Editor/UniGLTF/TextureImporterConfigurator.cs.meta diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterImpl.cs b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterImpl.cs index 666defaaf..34b1ed741 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterImpl.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterImpl.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Linq; using UnityEditor; @@ -35,6 +36,12 @@ namespace UniGLTF externalObjectMap.Where(x => x.Value != null).Select(x => (x.Value.name, x.Value)).Concat( EnumerateTexturesFromUri(externalObjectMap, parser, UnityPath.FromUnityPath(scriptedImporter.assetPath).Parent)))) { + // settings TextureImporters + foreach (var textureInfo in parser.EnumerateTextures()) + { + TextureImporterConfigurator.Configure(textureInfo, loaded.TextureFactory.ExternalMap); + } + loaded.InvertAxis = reverseAxis; loaded.Load(); loaded.ShowMeshes(); diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/TextureExtractor.cs b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/TextureExtractor.cs index a74736131..3aa84ac95 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/TextureExtractor.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/TextureExtractor.cs @@ -129,39 +129,13 @@ namespace UniGLTF EditorApplication.delayCall += () => { + // Wait for the texture assets to be imported + foreach (var kv in extractor.Textures) { var targetPath = kv.Key; var param = kv.Value; - // TextureImporter - var targetTextureImporter = AssetImporter.GetAtPath(targetPath) as TextureImporter; - if (targetTextureImporter != null) - { - switch (param.TextureType) - { - case GetTextureParam.TextureTypes.StandardMap: -#if VRM_DEVELOP - Debug.Log($"{targetPath} => linear"); -#endif - targetTextureImporter.sRGBTexture = false; - targetTextureImporter.SaveAndReimport(); - break; - - case GetTextureParam.TextureTypes.NormalMap: -#if VRM_DEVELOP - Debug.Log($"{targetPath} => normalmap"); -#endif - targetTextureImporter.textureType = TextureImporterType.NormalMap; - targetTextureImporter.SaveAndReimport(); - break; - } - } - else - { - throw new FileNotFoundException(targetPath); - } - // remap var externalObject = AssetDatabase.LoadAssetAtPath(targetPath); if (externalObject != null) diff --git a/Assets/UniGLTF/Editor/UniGLTF/TextureImporterConfigurator.cs b/Assets/UniGLTF/Editor/UniGLTF/TextureImporterConfigurator.cs new file mode 100644 index 000000000..8dca3330d --- /dev/null +++ b/Assets/UniGLTF/Editor/UniGLTF/TextureImporterConfigurator.cs @@ -0,0 +1,74 @@ +using System; +using System.Collections.Generic; +using UnityEditor; +using UnityEngine; + +namespace UniGLTF +{ + public static class TextureImporterConfigurator + { + public static void ConfigureNormalMap(Texture2D texture) + { + var path = UnityPath.FromAsset(texture); + if (AssetImporter.GetAtPath(path.Value) is TextureImporter textureImporter) + { +#if VRM_DEVELOP + Debug.Log($"{path} => normalmap"); +#endif + textureImporter.textureType = TextureImporterType.NormalMap; + textureImporter.SaveAndReimport(); + } + else + { + throw new System.IO.FileNotFoundException($"{path}"); + } + } + + public static void ConfigureLinear(Texture2D texture) + { + var path = UnityPath.FromAsset(texture); + if (AssetImporter.GetAtPath(path.Value) is TextureImporter textureImporter) + { +#if VRM_DEVELOP + Debug.Log($"{path} => linear"); +#endif + textureImporter.sRGBTexture = false; + textureImporter.SaveAndReimport(); + } + else + { + throw new System.IO.FileNotFoundException($"{path}"); + } + } + + public static void Configure(GetTextureParam textureInfo, IDictionary ExternalMap) + { + switch (textureInfo.TextureType) + { + case GetTextureParam.TextureTypes.NormalMap: + { + if (ExternalMap.TryGetValue(textureInfo.GltflName, out Texture2D external)) + { + ConfigureNormalMap(external); + } + } + break; + + case GetTextureParam.TextureTypes.StandardMap: + { + if (ExternalMap.TryGetValue(textureInfo.ConvertedName, out Texture2D external)) + { + ConfigureLinear(external); + } + } + break; + + case GetTextureParam.TextureTypes.sRGB: + break; + + default: + throw new NotImplementedException(); + } + } + } +} diff --git a/Assets/UniGLTF/Editor/UniGLTF/TextureImporterConfigurator.cs.meta b/Assets/UniGLTF/Editor/UniGLTF/TextureImporterConfigurator.cs.meta new file mode 100644 index 000000000..b14b582e7 --- /dev/null +++ b/Assets/UniGLTF/Editor/UniGLTF/TextureImporterConfigurator.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 193b7c6393807a04f8aafabc23478f8e +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs index 8e05108df..503d77127 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs @@ -56,6 +56,7 @@ namespace UniGLTF }; #endif } + m_textureFactory = new TextureFactory(loadTextureAsync, externalObjectMap); m_materialFactory = new MaterialFactory(GLTF, Storage, externalObjectMap); } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/PBRMaterialItem.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/PBRMaterialItem.cs index 38968b4e6..55de4ddee 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/PBRMaterialItem.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/PBRMaterialItem.cs @@ -83,9 +83,10 @@ namespace UniGLTF var src = gltf.materials[i]; var material = MaterialFactory.CreateMaterial(i, src, ShaderName); - var standardParam = StandardTexture(gltf, src); - if (src.pbrMetallicRoughness != null) + var standardParam = default(GetTextureParam); + if (src.pbrMetallicRoughness != null || src.occlusionTexture != null) { + standardParam = StandardTexture(gltf, src); if (src.pbrMetallicRoughness.baseColorFactor != null && src.pbrMetallicRoughness.baseColorFactor.Length == 4) { var color = src.pbrMetallicRoughness.baseColorFactor; diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureFactory.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureFactory.cs index af060fa95..70872c48c 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureFactory.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureFactory.cs @@ -46,10 +46,11 @@ namespace UniGLTF public delegate Task GetTextureAsyncFunc(IAwaitCaller awaitCaller, glTF gltf, GetTextureParam param); public class TextureFactory : IDisposable { - Dictionary m_externalMap; + public readonly Dictionary ExternalMap; + public bool TryGetExternal(GetTextureParam param, bool used, out Texture2D external) { - if (param.Index0.HasValue && m_externalMap != null) + if (param.Index0.HasValue && ExternalMap != null) { var cacheName = param.ConvertedName; if (param.TextureType == GetTextureParam.TextureTypes.NormalMap) @@ -61,7 +62,7 @@ namespace UniGLTF return true; } } - if (m_externalMap.TryGetValue(cacheName, out external)) + if (ExternalMap.TryGetValue(cacheName, out external)) { m_textureCache.Add(cacheName, new TextureLoadInfo(external, used, true)); return true; @@ -79,7 +80,7 @@ namespace UniGLTF LoadTextureAsync = loadTextureAsync; if (externalMap != null) { - m_externalMap = externalMap + ExternalMap = externalMap .Select(kv => (kv.Item1, kv.Item2 as Texture2D)) .Where(kv => kv.Item2 != null) .ToDictionary(kv => kv.Item1, kv => kv.Item2); diff --git a/Assets/VRM/Editor/Format/VRMImporterMenu.cs b/Assets/VRM/Editor/Format/VRMImporterMenu.cs index 61be120de..9090698c4 100644 --- a/Assets/VRM/Editor/Format/VRMImporterMenu.cs +++ b/Assets/VRM/Editor/Format/VRMImporterMenu.cs @@ -4,6 +4,7 @@ using UnityEngine; using UniGLTF; using System; using System.Collections.Generic; +using System.Linq; namespace VRM { @@ -58,14 +59,24 @@ namespace VRM var parser = new GltfParser(); parser.ParseGlb(File.ReadAllBytes(path)); - Action> onCompleted = _ => + Action> onCompleted = texturePaths => { // // after textures imported // + var map = texturePaths.Select(x => + { + var texture = AssetDatabase.LoadAssetAtPath(x, typeof(Texture2D)); + return (texture.name, texture); + }).ToArray(); + using (var context = new VRMImporterContext(parser)) { var editor = new VRMEditorImporterContext(context, prefabPath); + foreach (var textureInfo in parser.EnumerateTextures()) + { + TextureImporterConfigurator.Configure(textureInfo, map.ToDictionary(x => x.name, x => x.texture as Texture2D)); + } context.Load(); editor.SaveAsAsset(); } diff --git a/Assets/VRM/Editor/Format/vrmAssetPostprocessor.cs b/Assets/VRM/Editor/Format/vrmAssetPostprocessor.cs index 630bffa37..264bf69a6 100644 --- a/Assets/VRM/Editor/Format/vrmAssetPostprocessor.cs +++ b/Assets/VRM/Editor/Format/vrmAssetPostprocessor.cs @@ -49,9 +49,14 @@ namespace VRM var texture = AssetDatabase.LoadAssetAtPath(x, typeof(Texture2D)); return (texture.name, texture); }).ToArray(); + using (var context = new VRMImporterContext(parser, null, map)) { var editor = new VRMEditorImporterContext(context, prefabPath); + foreach (var textureInfo in parser.EnumerateTextures()) + { + TextureImporterConfigurator.Configure(textureInfo, map.ToDictionary(x => x.name, x => x.texture as Texture2D)); + } context.Load(); editor.SaveAsAsset(); } From 6b3856af52cee526628020acf503580a893fe697 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Mon, 15 Mar 2021 20:08:09 +0900 Subject: [PATCH 20/28] fix PBRMaterialItem null check --- .../UniGLTF/Runtime/UniGLTF/IO/MaterialIO/PBRMaterialItem.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/PBRMaterialItem.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/PBRMaterialItem.cs index 55de4ddee..7218863b7 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/PBRMaterialItem.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/PBRMaterialItem.cs @@ -86,7 +86,10 @@ namespace UniGLTF var standardParam = default(GetTextureParam); if (src.pbrMetallicRoughness != null || src.occlusionTexture != null) { - standardParam = StandardTexture(gltf, src); + if (src.pbrMetallicRoughness.metallicRoughnessTexture != null || src.occlusionTexture != null) + { + standardParam = StandardTexture(gltf, src); + } if (src.pbrMetallicRoughness.baseColorFactor != null && src.pbrMetallicRoughness.baseColorFactor.Length == 4) { var color = src.pbrMetallicRoughness.baseColorFactor; From 13e43bd6d62221af5789da16da78aab999ae7fbc Mon Sep 17 00:00:00 2001 From: ousttrue Date: Mon, 15 Mar 2021 20:12:51 +0900 Subject: [PATCH 21/28] return -1 --- .../UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureExporter.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureExporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureExporter.cs index 1c76c1c03..ca73855ef 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureExporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureExporter.cs @@ -104,7 +104,7 @@ namespace UniGLTF { if (src == null) { - throw new ArgumentNullException(); + return -1; } // cache @@ -141,7 +141,7 @@ namespace UniGLTF { if (metallicSmoothTexture == null && occlusionTexture == null) { - throw new ArgumentNullException(); + return -1; } // cache @@ -179,7 +179,7 @@ namespace UniGLTF { if (src == null) { - throw new ArgumentNullException(); + return -1; } // cache From 558f651534f968655bb7e34e31a76a4458d2cf9e Mon Sep 17 00:00:00 2001 From: ousttrue Date: Mon, 15 Mar 2021 20:38:05 +0900 Subject: [PATCH 22/28] fix normalMap extract --- .../UniGLTF/ScriptedImporter/EditorMaterial.cs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/EditorMaterial.cs b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/EditorMaterial.cs index 1d977f8dd..a918fa8b4 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/EditorMaterial.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/EditorMaterial.cs @@ -51,7 +51,17 @@ namespace UniGLTF s_foldTextures = EditorGUILayout.Foldout(s_foldTextures, "Remapped Textures"); if (s_foldTextures) { - DrawRemapGUI(importer, GltfTextureEnumerator.Enumerate(parser.GLTF).Select(x => x.ConvertedName)); + DrawRemapGUI(importer, GltfTextureEnumerator.Enumerate(parser.GLTF).Select(x => + { + switch (x.TextureType) + { + case GetTextureParam.TextureTypes.NormalMap: + return x.GltflName; + + default: + return x.ConvertedName; + } + })); } if (GUILayout.Button("Clear")) From b474ca3ebaea4953d5d82247c63dfff187ac3a1b Mon Sep 17 00:00:00 2001 From: ousttrue Date: Mon, 15 Mar 2021 20:42:13 +0900 Subject: [PATCH 23/28] NotVrm0Exception --- Assets/VRM/Editor/Format/vrmAssetPostprocessor.cs | 9 ++++++++- Assets/VRM/Runtime/IO/VRMImporterContext.cs | 8 +++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/Assets/VRM/Editor/Format/vrmAssetPostprocessor.cs b/Assets/VRM/Editor/Format/vrmAssetPostprocessor.cs index 264bf69a6..bdafda9d5 100644 --- a/Assets/VRM/Editor/Format/vrmAssetPostprocessor.cs +++ b/Assets/VRM/Editor/Format/vrmAssetPostprocessor.cs @@ -25,7 +25,14 @@ namespace VRM var ext = Path.GetExtension(path).ToLower(); if (ext == ".vrm") { - ImportVrm(UnityPath.FromUnityPath(path)); + try + { + ImportVrm(UnityPath.FromUnityPath(path)); + } + catch (VRMImporterContext.NotVrm0Exception) + { + // is not vrm0 + } } } } diff --git a/Assets/VRM/Runtime/IO/VRMImporterContext.cs b/Assets/VRM/Runtime/IO/VRMImporterContext.cs index 15af9f0b8..3f6c7d63d 100644 --- a/Assets/VRM/Runtime/IO/VRMImporterContext.cs +++ b/Assets/VRM/Runtime/IO/VRMImporterContext.cs @@ -10,6 +10,12 @@ namespace VRM { public class VRMImporterContext : ImporterContext { + public class NotVrm0Exception : Exception + { + public NotVrm0Exception() + { } + } + public VRM.glTF_VRM_extensions VRM { get; private set; } public VRMImporterContext(GltfParser parser, @@ -25,7 +31,7 @@ namespace VRM } else { - throw new KeyNotFoundException("not vrm0"); + throw new NotVrm0Exception(); } } From 377d5127ccaea78519dcbaa3224010a51522c700 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Mon, 15 Mar 2021 20:52:19 +0900 Subject: [PATCH 24/28] fix MToon export --- .../Runtime/UniGLTF/IO/TextureIO/TextureExporter.cs | 4 ++++ Assets/VRM/Runtime/IO/VRMExporter.cs | 4 ++-- Assets/VRM/Runtime/IO/VRMMaterialExporter.cs | 12 +++++------- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureExporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureExporter.cs index ca73855ef..486bdece0 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureExporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureExporter.cs @@ -45,6 +45,10 @@ namespace UniGLTF /// public int GetTextureIndex(Texture src, glTFTextureTypes textureType) { + if (src == null) + { + return -1; + } return m_exportMap[new ExportKey(src, textureType)]; } diff --git a/Assets/VRM/Runtime/IO/VRMExporter.cs b/Assets/VRM/Runtime/IO/VRMExporter.cs index 980cef846..a24c3e240 100644 --- a/Assets/VRM/Runtime/IO/VRMExporter.cs +++ b/Assets/VRM/Runtime/IO/VRMExporter.cs @@ -137,7 +137,7 @@ namespace VRM VRM.meta.title = meta.Title; if (meta.Thumbnail != null) { - VRM.meta.texture = TextureExporter.ExportTexture(glTF, glTF.buffers.Count - 1, meta.Thumbnail); + VRM.meta.texture = TextureManager.ExportSRGB(meta.Thumbnail); } // ussage permission @@ -200,7 +200,7 @@ namespace VRM // materials foreach (var m in Materials) { - VRM.materialProperties.Add(VRMMaterialExporter.CreateFromMaterial(m, TextureManager.GetTextureIndex)); + VRM.materialProperties.Add(VRMMaterialExporter.CreateFromMaterial(m, TextureManager)); } // Serialize VRM diff --git a/Assets/VRM/Runtime/IO/VRMMaterialExporter.cs b/Assets/VRM/Runtime/IO/VRMMaterialExporter.cs index 2b955cb95..0b49c6b90 100644 --- a/Assets/VRM/Runtime/IO/VRMMaterialExporter.cs +++ b/Assets/VRM/Runtime/IO/VRMMaterialExporter.cs @@ -107,7 +107,7 @@ namespace VRM // "Queue", }; - public static glTF_VRM_Material CreateFromMaterial(Material m, Func getTextureIndex) + public static glTF_VRM_Material CreateFromMaterial(Material m, TextureExporter textureExporter) { var material = new glTF_VRM_Material { @@ -160,12 +160,10 @@ namespace VRM var texture = m.GetTexture(kv.Key); if (texture != null) { - var textureType = glTFTextureTypes.SRGB; - if (kv.Key == "_BumpMap") - { - textureType = glTFTextureTypes.Normal; - } - var value = getTextureIndex(texture, textureType); + var value = kv.Key == "_BumpMap" + ? textureExporter.ExportNormal(texture) + : textureExporter.ExportSRGB(texture) + ; if (value == -1) { Debug.LogFormat("not found {0}", texture.name); From 2c9bb63ccb5ce6a33e5e5d35e8de65b1f67f1061 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Mon, 15 Mar 2021 21:17:31 +0900 Subject: [PATCH 25/28] impl --- .../OcclusionMetallicRoughnessConverter.cs | 61 ++++++++++++------- 1 file changed, 40 insertions(+), 21 deletions(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/OcclusionMetallicRoughnessConverter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/OcclusionMetallicRoughnessConverter.cs index 44b89f433..72ad2d1af 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/OcclusionMetallicRoughnessConverter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/OcclusionMetallicRoughnessConverter.cs @@ -26,7 +26,20 @@ namespace UniGLTF { if (metallicRoughnessTexture != null && occlusionTexture != null) { - if (metallicRoughnessTexture != occlusionTexture) + if (metallicRoughnessTexture == occlusionTexture) + { + var copyMetallicRoughness = TextureConverter.CopyTexture(metallicRoughnessTexture, glTFTextureTypes.OcclusionMetallicRoughness, null); + var metallicRoughnessPixels = copyMetallicRoughness.GetPixels32(); + for (int i = 0; i < metallicRoughnessPixels.Length; ++i) + { + metallicRoughnessPixels[i] = ImportPixel(metallicRoughnessPixels[i], metallicFactor, roughnessFactor, metallicRoughnessPixels[i]); + } + copyMetallicRoughness.SetPixels32(metallicRoughnessPixels); + copyMetallicRoughness.Apply(); + copyMetallicRoughness.name = metallicRoughnessTexture.name; + return copyMetallicRoughness; + } + else { var copyMetallicRoughness = TextureConverter.CopyTexture(metallicRoughnessTexture, glTFTextureTypes.OcclusionMetallicRoughness, null); var metallicRoughnessPixels = copyMetallicRoughness.GetPixels32(); @@ -45,19 +58,6 @@ namespace UniGLTF copyMetallicRoughness.name = metallicRoughnessTexture.name; return copyMetallicRoughness; } - else - { - var copyMetallicRoughness = TextureConverter.CopyTexture(metallicRoughnessTexture, glTFTextureTypes.OcclusionMetallicRoughness, null); - var metallicRoughnessPixels = copyMetallicRoughness.GetPixels32(); - for (int i = 0; i < metallicRoughnessPixels.Length; ++i) - { - metallicRoughnessPixels[i] = ImportPixel(metallicRoughnessPixels[i], metallicFactor, roughnessFactor, metallicRoughnessPixels[i]); - } - copyMetallicRoughness.SetPixels32(metallicRoughnessPixels); - copyMetallicRoughness.Apply(); - copyMetallicRoughness.name = metallicRoughnessTexture.name; - return copyMetallicRoughness; - } } else if (metallicRoughnessTexture != null) { @@ -94,11 +94,7 @@ namespace UniGLTF { if (metallicSmoothTexture != null && occlusionTexture != null) { - if (metallicSmoothTexture != occlusionTexture) - { - throw new NotImplementedException(); - } - else + if (metallicSmoothTexture == occlusionTexture) { var copyTexture = TextureConverter.CopyTexture(metallicSmoothTexture, glTFTextureTypes.OcclusionMetallicRoughness, null); copyTexture.SetPixels32(copyTexture.GetPixels32().Select(x => ExportPixel(x, smoothness, x)).ToArray()); @@ -106,6 +102,25 @@ namespace UniGLTF copyTexture.name = metallicSmoothTexture.name; return copyTexture; } + else + { + var copyMetallicSmooth = TextureConverter.CopyTexture(metallicSmoothTexture, glTFTextureTypes.OcclusionMetallicRoughness, null); + var metallicSmoothPixels = copyMetallicSmooth.GetPixels32(); + var copyOcclusion = TextureConverter.CopyTexture(occlusionTexture, glTFTextureTypes.OcclusionMetallicRoughness, null); + var occlusionPixels = copyOcclusion.GetPixels32(); + if (metallicSmoothPixels.Length != occlusionPixels.Length) + { + throw new NotImplementedException(); + } + for (int i = 0; i < metallicSmoothPixels.Length; ++i) + { + metallicSmoothPixels[i] = ExportPixel(metallicSmoothPixels[i], smoothness, occlusionPixels[i]); + } + copyMetallicSmooth.SetPixels32(metallicSmoothPixels); + copyMetallicSmooth.Apply(); + copyMetallicSmooth.name = metallicSmoothTexture.name; + return copyMetallicSmooth; + } } else if (metallicSmoothTexture) { @@ -117,11 +132,15 @@ namespace UniGLTF } else if (occlusionTexture) { - throw new NotImplementedException(); + var copyTexture = TextureConverter.CopyTexture(occlusionTexture, glTFTextureTypes.OcclusionMetallicRoughness, null); + copyTexture.SetPixels32(copyTexture.GetPixels32().Select(x => ExportPixel(default, smoothness, x)).ToArray()); + copyTexture.Apply(); + copyTexture.name = occlusionTexture.name; + return copyTexture; } else { - throw new NotImplementedException(); + throw new ArgumentNullException(); } } From 1d38656df2e8bb1109b9addd5e539bfac95d1008 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Wed, 17 Mar 2021 15:42:16 +0900 Subject: [PATCH 26/28] fix rebase --- .../ScriptedImporter/ScriptedImporterImpl.cs | 2 +- .../IO/MaterialIO/GltfTextureEnumerator.cs | 18 +++++++++++++----- Assets/VRM/Editor/Format/VRMImporterMenu.cs | 2 +- .../VRM/Editor/Format/vrmAssetPostprocessor.cs | 2 +- Assets/VRM/Runtime/IO/VRMTextureEnumerator.cs | 4 ++-- 5 files changed, 18 insertions(+), 10 deletions(-) diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterImpl.cs b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterImpl.cs index 34b1ed741..9ab19ef73 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterImpl.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterImpl.cs @@ -37,7 +37,7 @@ namespace UniGLTF EnumerateTexturesFromUri(externalObjectMap, parser, UnityPath.FromUnityPath(scriptedImporter.assetPath).Parent)))) { // settings TextureImporters - foreach (var textureInfo in parser.EnumerateTextures()) + foreach (var textureInfo in GltfTextureEnumerator.Enumerate(parser.GLTF)) { TextureImporterConfigurator.Configure(textureInfo, loaded.TextureFactory.ExternalMap); } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfTextureEnumerator.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfTextureEnumerator.cs index b2550d9d4..074816122 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfTextureEnumerator.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfTextureEnumerator.cs @@ -8,6 +8,7 @@ namespace UniGLTF { public static IEnumerable EnumerateTextures(glTF gltf, glTFMaterial m) { + int? metallicRoughnessTexture = default; if (m.pbrMetallicRoughness != null) { // base color @@ -17,16 +18,16 @@ namespace UniGLTF } // metallic roughness - if (m.pbrMetallicRoughness?.metallicRoughnessTexture != null) + if (m.pbrMetallicRoughness?.metallicRoughnessTexture != null && m.pbrMetallicRoughness.metallicRoughnessTexture.index != -1) { - yield return PBRMaterialItem.MetallicRoughnessTexture(gltf, m); + metallicRoughnessTexture = m.pbrMetallicRoughness?.metallicRoughnessTexture?.index; } } // emission if (m.emissiveTexture != null) { - yield return GetTextureParam.Create(gltf, m.emissiveTexture.index); + yield return GetTextureParam.CreateSRGB(gltf, m.emissiveTexture.index); } // normal @@ -36,9 +37,16 @@ namespace UniGLTF } // occlusion - if (m.occlusionTexture != null) + int? occlusionTexture = default; + if (m.occlusionTexture != null && m.occlusionTexture.index != -1) { - yield return PBRMaterialItem.OcclusionTexture(gltf, m); + occlusionTexture = m.occlusionTexture.index; + } + + // metallicSmooth and occlusion + if (metallicRoughnessTexture.HasValue || occlusionTexture.HasValue) + { + yield return PBRMaterialItem.StandardTexture(gltf, m); } } diff --git a/Assets/VRM/Editor/Format/VRMImporterMenu.cs b/Assets/VRM/Editor/Format/VRMImporterMenu.cs index 9090698c4..b8ceb8717 100644 --- a/Assets/VRM/Editor/Format/VRMImporterMenu.cs +++ b/Assets/VRM/Editor/Format/VRMImporterMenu.cs @@ -73,7 +73,7 @@ namespace VRM using (var context = new VRMImporterContext(parser)) { var editor = new VRMEditorImporterContext(context, prefabPath); - foreach (var textureInfo in parser.EnumerateTextures()) + foreach (var textureInfo in new VRMTextureEnumerator(context.VRM).Enumerate(parser.GLTF)) { TextureImporterConfigurator.Configure(textureInfo, map.ToDictionary(x => x.name, x => x.texture as Texture2D)); } diff --git a/Assets/VRM/Editor/Format/vrmAssetPostprocessor.cs b/Assets/VRM/Editor/Format/vrmAssetPostprocessor.cs index bdafda9d5..2acd8fbb4 100644 --- a/Assets/VRM/Editor/Format/vrmAssetPostprocessor.cs +++ b/Assets/VRM/Editor/Format/vrmAssetPostprocessor.cs @@ -60,7 +60,7 @@ namespace VRM using (var context = new VRMImporterContext(parser, null, map)) { var editor = new VRMEditorImporterContext(context, prefabPath); - foreach (var textureInfo in parser.EnumerateTextures()) + foreach (var textureInfo in new VRMTextureEnumerator(context.VRM).Enumerate(parser.GLTF)) { TextureImporterConfigurator.Configure(textureInfo, map.ToDictionary(x => x.name, x => x.texture as Texture2D)); } diff --git a/Assets/VRM/Runtime/IO/VRMTextureEnumerator.cs b/Assets/VRM/Runtime/IO/VRMTextureEnumerator.cs index 1a6cc2117..c84bc66ad 100644 --- a/Assets/VRM/Runtime/IO/VRMTextureEnumerator.cs +++ b/Assets/VRM/Runtime/IO/VRMTextureEnumerator.cs @@ -22,7 +22,7 @@ namespace VRM foreach (var kv in vrmMaterial.textureProperties) { // SRGB color or normalmap - yield return GetTextureParam.Create(gltf, kv.Value, kv.Key); + yield return GetTextureParam.Create(gltf, kv.Value, kv.Key, default, default); } } else @@ -38,7 +38,7 @@ namespace VRM // thumbnail if (m_vrm.meta != null && m_vrm.meta.texture != -1) { - yield return GetTextureParam.Create(gltf, m_vrm.meta.texture); + yield return GetTextureParam.CreateSRGB(gltf, m_vrm.meta.texture); } } } From 6d211f2ec5fef288a7dc068861f5468e100544c9 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Wed, 17 Mar 2021 15:53:53 +0900 Subject: [PATCH 27/28] test --- Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs b/Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs index 6fbca157b..62a80736e 100644 --- a/Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs +++ b/Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs @@ -22,13 +22,13 @@ namespace UniGLTF var materialExporter = new MaterialExporter(); materialExporter.ExportMaterial(material, textureManager); - // var convTex0 = textureManager.GetExportTexture(0); - // var sampler = TextureSamplerUtil.Export(convTex0); + var convTex0 = textureManager.Exported[0]; + var sampler = TextureSamplerUtil.Export(convTex0); - // Assert.AreEqual(glWrap.CLAMP_TO_EDGE, sampler.wrapS); - // Assert.AreEqual(glWrap.CLAMP_TO_EDGE, sampler.wrapT); - // Assert.AreEqual(glFilter.LINEAR_MIPMAP_LINEAR, sampler.minFilter); - // Assert.AreEqual(glFilter.LINEAR_MIPMAP_LINEAR, sampler.magFilter); + Assert.AreEqual(glWrap.CLAMP_TO_EDGE, sampler.wrapS); + Assert.AreEqual(glWrap.CLAMP_TO_EDGE, sampler.wrapT); + Assert.AreEqual(glFilter.LINEAR_MIPMAP_LINEAR, sampler.minFilter); + Assert.AreEqual(glFilter.LINEAR_MIPMAP_LINEAR, sampler.magFilter); } } From 25b70b97d2ba7cede163da509553ea6cd27bd656 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Wed, 17 Mar 2021 17:50:47 +0900 Subject: [PATCH 28/28] TextureExporter.ExportLinear place holder --- .../Runtime/UniGLTF/Format/glTFMaterial.cs | 1 + .../UniGLTF/IO/TextureIO/GetTextureParam.cs | 1 + .../IO/TextureIO/GltfTextureExporter.cs | 94 +++++++++++++++++++ .../IO/TextureIO/GltfTextureExporter.cs.meta | 11 +++ .../UniGLTF/IO/TextureIO/TextureExporter.cs | 94 +++---------------- .../Runtime/UniGLTF/IO/gltfExporter.cs | 2 +- Assets/VRM/Runtime/IO/VRMExporter.cs | 2 +- 7 files changed, 122 insertions(+), 83 deletions(-) create mode 100644 Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureExporter.cs create mode 100644 Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureExporter.cs.meta diff --git a/Assets/UniGLTF/Runtime/UniGLTF/Format/glTFMaterial.cs b/Assets/UniGLTF/Runtime/UniGLTF/Format/glTFMaterial.cs index f91dd9d41..4fe760250 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/Format/glTFMaterial.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/Format/glTFMaterial.cs @@ -8,6 +8,7 @@ namespace UniGLTF OcclusionMetallicRoughness, Normal, SRGB, + Linear, } public interface IglTFTextureinfo diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GetTextureParam.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GetTextureParam.cs index eb4e5264a..8bfcc14a3 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GetTextureParam.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GetTextureParam.cs @@ -20,6 +20,7 @@ namespace UniGLTF NormalMap, // Occlusion + Metallic + Smoothness StandardMap, + Linear, } public static string RemoveSuffix(string src) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureExporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureExporter.cs new file mode 100644 index 000000000..66c7664ab --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureExporter.cs @@ -0,0 +1,94 @@ +using UnityEngine; + +namespace UniGLTF +{ + public static class GltfTextureExporter + { + + /// + /// 画像のバイト列を得る + /// + /// + /// + /// + static (byte[] bytes, string mine) GetBytesWithMime(Texture2D texture) + { +#if UNITY_EDITOR + var path = UnityPath.FromAsset(texture); + if (path.IsUnderAssetsFolder) + { + if (path.Extension == ".png") + { + return + ( + System.IO.File.ReadAllBytes(path.FullPath), + "image/png" + ); + } + if (path.Extension == ".jpg") + { + return + ( + System.IO.File.ReadAllBytes(path.FullPath), + "image/jpeg" + ); + } + } +#endif + + return + ( + texture.EncodeToPNG(), + "image/png" + ); + } + + /// + /// gltf に texture を足す + /// + /// * textures + /// * samplers + /// * images + /// * bufferViews + /// + /// を更新し、textures の index を返す + /// + /// + /// + /// + /// + /// gltf texture index + public static int PushGltfTexture(this glTF gltf, int bufferIndex, Texture2D texture) + { + var bytesWithMime = GetBytesWithMime(texture); + + // add view + var view = gltf.buffers[bufferIndex].Append(bytesWithMime.bytes, glBufferTarget.NONE); + var viewIndex = gltf.AddBufferView(view); + + // add image + var imageIndex = gltf.images.Count; + gltf.images.Add(new glTFImage + { + name = GetTextureParam.RemoveSuffix(texture.name), + bufferView = viewIndex, + mimeType = bytesWithMime.mine, + }); + + // add sampler + var samplerIndex = gltf.samplers.Count; + var sampler = TextureSamplerUtil.Export(texture); + gltf.samplers.Add(sampler); + + // add texture + var textureIndex = gltf.textures.Count; + gltf.textures.Add(new glTFTexture + { + sampler = samplerIndex, + source = imageIndex, + }); + + return textureIndex; + } + } +} diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureExporter.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureExporter.cs.meta new file mode 100644 index 000000000..df9f76019 --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureExporter.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: edc800243b783ea4392e1d916789c803 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureExporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureExporter.cs index 486bdece0..4cf0c3f5e 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureExporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureExporter.cs @@ -100,7 +100,7 @@ namespace UniGLTF } /// - /// sRGBなテクスチャーを処理する + /// sRGBなテクスチャーを処理し、index を確定させる /// /// /// @@ -135,7 +135,17 @@ namespace UniGLTF } /// - /// Standard の Metallic, Smoothness, Occlusion をまとめる + /// Linearなテクスチャーを処理し、index を確定させる + /// + /// + /// + public int ExportLinear(Texture src) + { + throw new NotImplementedException(); + } + + /// + /// Standard の Metallic, Smoothness, Occlusion をまとめ、index を確定させる /// /// /// @@ -175,7 +185,7 @@ namespace UniGLTF } /// - /// Normal のテクスチャを変換する + /// Normal のテクスチャを変換し index を確定させる /// /// /// @@ -210,83 +220,5 @@ namespace UniGLTF return index; } - - /// - /// 画像のバイト列を得る - /// - /// - /// - /// - static (Byte[] bytes, string mine) GetBytesWithMime(Texture2D texture) - { -#if UNITY_EDITOR - var path = UnityPath.FromAsset(texture); - if (path.IsUnderAssetsFolder) - { - if (path.Extension == ".png") - { - return - ( - System.IO.File.ReadAllBytes(path.FullPath), - "image/png" - ); - } - if (path.Extension == ".jpg") - { - return - ( - System.IO.File.ReadAllBytes(path.FullPath), - "image/jpeg" - ); - } - } -#endif - - return - ( - texture.EncodeToPNG(), - "image/png" - ); - } - - /// - /// - /// - /// - /// - /// - /// - static public int ExportTexture(glTF gltf, int bufferIndex, Texture2D texture) - { - var bytesWithMime = GetBytesWithMime(texture); - - // add view - var view = gltf.buffers[bufferIndex].Append(bytesWithMime.bytes, glBufferTarget.NONE); - var viewIndex = gltf.AddBufferView(view); - - // add image - var imageIndex = gltf.images.Count; - gltf.images.Add(new glTFImage - { - name = GetTextureParam.RemoveSuffix(texture.name), - bufferView = viewIndex, - mimeType = bytesWithMime.mine, - }); - - // add sampler - var samplerIndex = gltf.samplers.Count; - var sampler = TextureSamplerUtil.Export(texture); - gltf.samplers.Add(sampler); - - // add texture - var textureIndex = gltf.textures.Count; - gltf.textures.Add(new glTFTexture - { - sampler = samplerIndex, - source = imageIndex, - }); - - return textureIndex; - } } } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs index 5ac336716..424b02de0 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs @@ -188,7 +188,7 @@ namespace UniGLTF for (int i = 0; i < TextureManager.Exported.Count; ++i) { var unityTexture = TextureManager.Exported[i]; - TextureExporter.ExportTexture(glTF, bufferIndex, unityTexture); + glTF.PushGltfTexture(bufferIndex, unityTexture); } #endregion diff --git a/Assets/VRM/Runtime/IO/VRMExporter.cs b/Assets/VRM/Runtime/IO/VRMExporter.cs index a24c3e240..1245c9196 100644 --- a/Assets/VRM/Runtime/IO/VRMExporter.cs +++ b/Assets/VRM/Runtime/IO/VRMExporter.cs @@ -112,7 +112,7 @@ namespace VRM VRM.meta.title = meta.Title; if (meta.Thumbnail != null) { - VRM.meta.texture = TextureExporter.ExportTexture(glTF, glTF.buffers.Count - 1, meta.Thumbnail); + VRM.meta.texture = glTF.PushGltfTexture(glTF.buffers.Count - 1, meta.Thumbnail); } VRM.meta.licenseType = meta.LicenseType;