diff --git a/Assets/UniGLTF/Editor/UniGLTF/AssetTextureLoader.cs b/Assets/UniGLTF/Editor/UniGLTF/AssetTextureLoader.cs deleted file mode 100644 index 21f3d5e09..000000000 --- a/Assets/UniGLTF/Editor/UniGLTF/AssetTextureLoader.cs +++ /dev/null @@ -1,59 +0,0 @@ -using System.Threading.Tasks; -using UnityEngine; - -namespace UniGLTF -{ - public static class AssetTextureLoader - { - public static Task LoadTaskAsync(UnityPath m_assetPath, - glTF gltf, int textureIndex) - { - var colorSpace = gltf.GetColorSpace(textureIndex); - - // - // texture from assets - // - m_assetPath.ImportAsset(); - var importer = m_assetPath.GetImporter(); - if (importer == null) - { - Debug.LogWarningFormat("fail to get TextureImporter: {0}", m_assetPath); - } - else - { - importer.maxTextureSize = 8192; - importer.sRGBTexture = colorSpace == RenderTextureReadWrite.sRGB; - importer.SaveAndReimport(); - } - - var Texture = m_assetPath.LoadAsset(); - - if (Texture == null) - { - Debug.LogWarningFormat("fail to Load Texture2D: {0}", m_assetPath); - } - - else - { - var maxSize = Mathf.Max(Texture.width, Texture.height); - - importer.maxTextureSize - = maxSize > 4096 ? 8192 : - maxSize > 2048 ? 4096 : - maxSize > 1024 ? 2048 : - maxSize > 512 ? 1024 : - 512; - - importer.SaveAndReimport(); - } - - var sampler = gltf.GetSamplerFromTextureIndex(textureIndex); - if (sampler != null) - { - TextureSamplerUtil.SetSampler(Texture, sampler); - } - - return Task.FromResult(Texture); - } - } -} diff --git a/Assets/UniGLTF/Editor/UniGLTF/AssetTextureLoader.cs.meta b/Assets/UniGLTF/Editor/UniGLTF/AssetTextureLoader.cs.meta deleted file mode 100644 index c32ca741a..000000000 --- a/Assets/UniGLTF/Editor/UniGLTF/AssetTextureLoader.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 748452d292d79304da4c271f584ab985 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/EditorMaterial.cs b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/EditorMaterial.cs index 52d9260da..e33e4512b 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/EditorMaterial.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/EditorMaterial.cs @@ -56,12 +56,12 @@ namespace UniGLTF s_foldTextures = EditorGUILayout.Foldout(s_foldTextures, "Remapped Textures"); if (s_foldTextures) { - DrawRemapGUI(importer, GltfTextureEnumerator.Enumerate(parser.GLTF).Select(x => + DrawRemapGUI(importer, GltfTextureEnumerator.Enumerate(parser).Select(x => { switch (x.TextureType) { case GetTextureParam.TextureTypes.NormalMap: - return x.GltflName; + return x.GltfName; default: return x.ConvertedName; diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterImpl.cs b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterImpl.cs index 1129e72c9..23f2ecdf2 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterImpl.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterImpl.cs @@ -42,7 +42,7 @@ namespace UniGLTF using (var loaded = new ImporterContext(parser, externalObjectMap.Concat(externalTextures))) { // settings TextureImporters - foreach (var textureInfo in GltfTextureEnumerator.Enumerate(parser.GLTF)) + foreach (var textureInfo in GltfTextureEnumerator.Enumerate(parser)) { TextureImporterConfigurator.Configure(textureInfo, loaded.TextureFactory.ExternalMap); } @@ -73,7 +73,7 @@ namespace UniGLTF GltfParser parser, UnityPath dir) { var used = new HashSet(); - foreach (var texParam in GltfTextureEnumerator.Enumerate(parser.GLTF)) + foreach (var texParam in GltfTextureEnumerator.Enumerate(parser)) { switch (texParam.TextureType) { @@ -82,11 +82,9 @@ namespace UniGLTF default: { - var gltfTexture = parser.GLTF.textures.First(y => y.name == texParam.GltflName); - var gltfImage = parser.GLTF.images[gltfTexture.source]; - if (!string.IsNullOrEmpty(gltfImage.uri) && !gltfImage.uri.StartsWith("data:")) + if (!string.IsNullOrEmpty(texParam.Uri) && !texParam.Uri.StartsWith("data:")) { - var child = dir.Child(gltfImage.uri); + var child = dir.Child(texParam.Uri); var asset = AssetDatabase.LoadAssetAtPath(child.Value); if (asset == null) { diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/TextureExtractor.cs b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/TextureExtractor.cs index 2d848a1d2..0d1a06f47 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/TextureExtractor.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/TextureExtractor.cs @@ -40,22 +40,17 @@ namespace UniGLTF return Path.GetExtension(uri).ToLower(); } - public void Extract(GetTextureParam param, bool hasUri) + public void Extract(GetTextureParam param) { if (Textures.Values.Contains(param)) { return; } - var subAsset = m_subAssets.FirstOrDefault(x => x.name == param.ConvertedName); UnityPath targetPath = default; - - if (hasUri && !param.ExtractConverted) + if (!string.IsNullOrEmpty(param.Uri) && !param.ExtractConverted) { - var gltfTexture = GLTF.textures[param.Index0.Value]; - var gltfImage = GLTF.images[gltfTexture.source]; - var ext = GetExt(gltfImage.mimeType, gltfImage.uri); - targetPath = m_textureDirectory.Child($"{param.GltflName}{ext}"); + targetPath = m_textureDirectory.Child(param.GltfFileName); } else { @@ -65,7 +60,8 @@ namespace UniGLTF case GetTextureParam.TextureTypes.StandardMap: { // write converted texture - targetPath = m_textureDirectory.Child($"{param.ConvertedName}.png"); + var subAsset = m_subAssets.FirstOrDefault(x => x.name == param.ConvertedName); + targetPath = m_textureDirectory.Child(param.ConvertedFileName); File.WriteAllBytes(targetPath.FullPath, subAsset.EncodeToPNG().ToArray()); targetPath.ImportAsset(); break; @@ -74,11 +70,8 @@ namespace UniGLTF default: { // write original bytes - var gltfTexture = GLTF.textures[param.Index0.Value]; - var gltfImage = GLTF.images[gltfTexture.source]; - var ext = GetExt(gltfImage.mimeType, gltfImage.uri); - targetPath = m_textureDirectory.Child($"{param.GltflName}{ext}"); - File.WriteAllBytes(targetPath.FullPath, GLTF.GetImageBytes(Storage, gltfTexture.source).ToArray()); + targetPath = m_textureDirectory.Child(param.GltfFileName); + File.WriteAllBytes(targetPath.FullPath, param.Index0().Result.ToArray()); targetPath.ImportAsset(); break; } @@ -112,11 +105,9 @@ namespace UniGLTF Action> onCompleted = null) { var extractor = new TextureExtractor(parser, textureDirectory, subAssets); - foreach (var x in textureEnumerator(extractor.GLTF)) + foreach (var x in textureEnumerator(parser)) { - var gltfTexture = extractor.GLTF.textures[x.Index0.Value]; - var gltfImage = extractor.GLTF.images[gltfTexture.source]; - extractor.Extract(x, !string.IsNullOrEmpty(gltfImage.uri)); + extractor.Extract(x); } EditorApplication.delayCall += () => diff --git a/Assets/UniGLTF/Editor/UniGLTF/TextureImporterConfigurator.cs b/Assets/UniGLTF/Editor/UniGLTF/TextureImporterConfigurator.cs index 8dca3330d..358ae0833 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/TextureImporterConfigurator.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/TextureImporterConfigurator.cs @@ -7,6 +7,26 @@ namespace UniGLTF { public static class TextureImporterConfigurator { + public static void ConfigureSize(Texture2D texture) + { + var path = UnityPath.FromAsset(texture); + if (AssetImporter.GetAtPath(path.Value) is TextureImporter textureImporter) + { + var maxSize = Mathf.Max(texture.width, texture.height); + textureImporter.maxTextureSize + = maxSize > 4096 ? 8192 : + maxSize > 2048 ? 4096 : + maxSize > 1024 ? 2048 : + maxSize > 512 ? 1024 : + 512; + textureImporter.SaveAndReimport(); + } + else + { + throw new System.IO.FileNotFoundException($"{path}"); + } + } + public static void ConfigureNormalMap(Texture2D texture) { var path = UnityPath.FromAsset(texture); @@ -47,8 +67,9 @@ namespace UniGLTF { case GetTextureParam.TextureTypes.NormalMap: { - if (ExternalMap.TryGetValue(textureInfo.GltflName, out Texture2D external)) + if (ExternalMap.TryGetValue(textureInfo.GltfName, out Texture2D external)) { + ConfigureSize(external); ConfigureNormalMap(external); } } @@ -58,12 +79,19 @@ namespace UniGLTF { if (ExternalMap.TryGetValue(textureInfo.ConvertedName, out Texture2D external)) { + ConfigureSize(external); ConfigureLinear(external); } } break; case GetTextureParam.TextureTypes.sRGB: + { + if (ExternalMap.TryGetValue(textureInfo.GltfName, out Texture2D external)) + { + ConfigureSize(external); + } + } break; default: diff --git a/Assets/UniGLTF/Runtime/Extensions/glTFExtensions.cs b/Assets/UniGLTF/Runtime/Extensions/glTFExtensions.cs index ee1ac1353..ceb8e9642 100644 --- a/Assets/UniGLTF/Runtime/Extensions/glTFExtensions.cs +++ b/Assets/UniGLTF/Runtime/Extensions/glTFExtensions.cs @@ -372,6 +372,12 @@ namespace UniGLTF return result; } + public static ArraySegment GetImageBytesFromTextureIndex(this glTF self, IStorage storage, int textureIndex) + { + var imageIndex = self.textures[textureIndex].source; + return self.GetImageBytes(storage, imageIndex); + } + public static ArraySegment GetImageBytes(this glTF self, IStorage storage, int imageIndex) { var image = self.images[imageIndex]; diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs index 5f14f4c27..641b5552d 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs @@ -45,7 +45,7 @@ namespace UniGLTF { m_parser = parser; m_textureFactory = new TextureFactory(GLTF, Storage, externalObjectMap); - m_materialFactory = new MaterialFactory(GLTF, Storage, externalObjectMap); + m_materialFactory = new MaterialFactory(m_parser, externalObjectMap); } #region Source diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfTextureEnumerator.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfTextureEnumerator.cs index 15147905b..6bb3892b6 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfTextureEnumerator.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfTextureEnumerator.cs @@ -2,11 +2,11 @@ namespace UniGLTF { - public delegate IEnumerable TextureEnumerator(glTF gltf); + public delegate IEnumerable TextureEnumerator(GltfParser parser); public static class GltfTextureEnumerator { - public static IEnumerable EnumerateTextures(glTF gltf, glTFMaterial m) + public static IEnumerable EnumerateTextures(GltfParser parser, glTFMaterial m) { int? metallicRoughnessTexture = default; if (m.pbrMetallicRoughness != null) @@ -14,7 +14,7 @@ namespace UniGLTF // base color if (m.pbrMetallicRoughness?.baseColorTexture != null) { - yield return PBRMaterialItem.BaseColorTexture(gltf, m); + yield return PBRMaterialItem.BaseColorTexture(parser, m); } // metallic roughness @@ -27,13 +27,13 @@ namespace UniGLTF // emission if (m.emissiveTexture != null) { - yield return GetTextureParam.CreateSRGB(gltf, m.emissiveTexture.index); + yield return GetTextureParam.CreateSRGB(parser, m.emissiveTexture.index); } // normal if (m.normalTexture != null) { - yield return PBRMaterialItem.NormalTexture(gltf, m); + yield return PBRMaterialItem.NormalTexture(parser, m); } // occlusion @@ -46,16 +46,16 @@ namespace UniGLTF // metallicSmooth and occlusion if (metallicRoughnessTexture.HasValue || occlusionTexture.HasValue) { - yield return PBRMaterialItem.StandardTexture(gltf, m); + yield return PBRMaterialItem.StandardTexture(parser, m); } } - public static IEnumerable Enumerate(glTF gltf) + public static IEnumerable Enumerate(GltfParser parser) { var used = new HashSet(); - foreach (var material in gltf.materials) + foreach (var material in parser.GLTF.materials) { - foreach (var textureInfo in EnumerateTextures(gltf, material)) + foreach (var textureInfo in EnumerateTextures(parser, material)) { if(used.Add(textureInfo)){ yield return textureInfo; diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/MaterialFactory.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/MaterialFactory.cs index 0aa88ea9e..8768b560f 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/MaterialFactory.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/MaterialFactory.cs @@ -8,14 +8,13 @@ namespace UniGLTF { public class MaterialFactory : IDisposable { - glTF m_gltf; - IStorage m_storage; + GltfParser m_parser; Dictionary m_externalMap; public bool TryGetExternal(int index, out Material external) { if (m_externalMap != null) { - var gltfMaterial = m_gltf.materials[index]; + var gltfMaterial = m_parser.GLTF.materials[index]; if (m_externalMap.TryGetValue(gltfMaterial.name, out external)) { return true; @@ -26,11 +25,9 @@ namespace UniGLTF return false; } - public MaterialFactory(glTF gltf, IStorage storage, - IEnumerable<(string, UnityEngine.Object)> externalMap) + public MaterialFactory(GltfParser parser, IEnumerable<(string, UnityEngine.Object)> externalMap) { - m_gltf = gltf; - m_storage = storage; + m_parser = parser; if (externalMap != null) { m_externalMap = externalMap @@ -41,7 +38,7 @@ namespace UniGLTF } } - public delegate Task CreateMaterialAsyncFunc(IAwaitCaller awaitCaller, glTF gltf, int i, GetTextureAsyncFunc getTexture); + public delegate Task CreateMaterialAsyncFunc(IAwaitCaller awaitCaller, GltfParser parser, int i, GetTextureAsyncFunc getTexture); CreateMaterialAsyncFunc m_createMaterialAsync; public CreateMaterialAsyncFunc CreateMaterialAsync { @@ -138,15 +135,15 @@ namespace UniGLTF /// public async Task LoadMaterialsAsync(IAwaitCaller awaitCaller, GetTextureAsyncFunc getTexture) { - if (m_gltf.materials == null || m_gltf.materials.Count == 0) + if (m_parser.GLTF.materials == null || m_parser.GLTF.materials.Count == 0) { // no material. work around. - var material = await CreateMaterialAsync(awaitCaller, m_gltf, 0, getTexture); + var material = await CreateMaterialAsync(awaitCaller, m_parser, 0, getTexture); m_materials.Add(new MaterialLoadInfo(material, false)); return; } - for (int i = 0; i < m_gltf.materials.Count; ++i) + for (int i = 0; i < m_parser.GLTF.materials.Count; ++i) { if (TryGetExternal(i, out Material material)) { @@ -154,7 +151,7 @@ namespace UniGLTF continue; } - material = await CreateMaterialAsync(awaitCaller, m_gltf, i, getTexture); + material = await CreateMaterialAsync(awaitCaller, m_parser, i, getTexture); m_materials.Add(new MaterialLoadInfo(material, false)); } } @@ -196,22 +193,22 @@ namespace UniGLTF } } - public static Task DefaultCreateMaterialAsync(IAwaitCaller awaitCaller, glTF gltf, int i, GetTextureAsyncFunc getTexture) + public static Task DefaultCreateMaterialAsync(IAwaitCaller awaitCaller, GltfParser parser, int i, GetTextureAsyncFunc getTexture) { - if (i < 0 || i >= gltf.materials.Count) + if (i < 0 || i >= parser.GLTF.materials.Count) { UnityEngine.Debug.LogWarning("glTFMaterial is empty"); - return PBRMaterialItem.CreateAsync(awaitCaller, gltf, i, getTexture); + return PBRMaterialItem.CreateAsync(awaitCaller, parser, i, getTexture); } - var x = gltf.materials[i]; + var x = parser.GLTF.materials[i]; if (glTF_KHR_materials_unlit.IsEnable(x)) { - var hasVertexColor = gltf.MaterialHasVertexColor(i); - return UnlitMaterialItem.CreateAsync(awaitCaller, gltf, i, getTexture, hasVertexColor); + var hasVertexColor = parser.GLTF.MaterialHasVertexColor(i); + return UnlitMaterialItem.CreateAsync(awaitCaller, parser, i, getTexture, hasVertexColor); } - return PBRMaterialItem.CreateAsync(awaitCaller, gltf, i, getTexture); + return PBRMaterialItem.CreateAsync(awaitCaller, parser, i, getTexture); } /// @@ -231,8 +228,14 @@ namespace UniGLTF name = "texture_0" } }, + images = new List{ + new glTFImage{ + name = "image_0", + mimeType = "image/png", + } + }, }; - var task = DefaultCreateMaterialAsync(default(ImmediateCaller), gltf, i, null); + var task = DefaultCreateMaterialAsync(default(ImmediateCaller), new GltfParser{GLTF = gltf}, i, null); return task.Result; } } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/PBRMaterialItem.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/PBRMaterialItem.cs index 7218863b7..f6ba9a663 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/PBRMaterialItem.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/PBRMaterialItem.cs @@ -43,12 +43,12 @@ namespace UniGLTF Transparent } - public static GetTextureParam BaseColorTexture(glTF gltf, glTFMaterial src) + public static GetTextureParam BaseColorTexture(GltfParser parser, glTFMaterial src) { - return GetTextureParam.CreateSRGB(gltf, src.pbrMetallicRoughness.baseColorTexture.index); + return GetTextureParam.CreateSRGB(parser, src.pbrMetallicRoughness.baseColorTexture.index); } - public static GetTextureParam StandardTexture(glTF gltf, glTFMaterial src) + public static GetTextureParam StandardTexture(GltfParser parser, glTFMaterial src) { var metallicFactor = 1.0f; var roughnessFactor = 1.0f; @@ -57,38 +57,38 @@ namespace UniGLTF metallicFactor = src.pbrMetallicRoughness.metallicFactor; roughnessFactor = src.pbrMetallicRoughness.roughnessFactor; } - return GetTextureParam.CreateStandard(gltf, + return GetTextureParam.CreateStandard(parser, src.pbrMetallicRoughness?.metallicRoughnessTexture?.index, src.occlusionTexture?.index, metallicFactor, roughnessFactor); } - public static GetTextureParam NormalTexture(glTF gltf, glTFMaterial src) + public static GetTextureParam NormalTexture(GltfParser parser, glTFMaterial src) { - return GetTextureParam.CreateNormal(gltf, src.normalTexture.index); + return GetTextureParam.CreateNormal(parser, src.normalTexture.index); } - public static async Task CreateAsync(IAwaitCaller awaitCaller, glTF gltf, int i, GetTextureAsyncFunc getTexture) + public static async Task CreateAsync(IAwaitCaller awaitCaller, GltfParser parser, int i, GetTextureAsyncFunc getTexture) { if (getTexture == null) { getTexture = (IAwaitCaller _awaitCaller, glTF _gltf, GetTextureParam _param) => Task.FromResult(null); } - if (i < 0 || i >= gltf.materials.Count) + if (i < 0 || i >= parser.GLTF.materials.Count) { return MaterialFactory.CreateMaterial(i, null, ShaderName); } - var src = gltf.materials[i]; + var src = parser.GLTF.materials[i]; var material = MaterialFactory.CreateMaterial(i, src, ShaderName); var standardParam = default(GetTextureParam); if (src.pbrMetallicRoughness != null || src.occlusionTexture != null) { if (src.pbrMetallicRoughness.metallicRoughnessTexture != null || src.occlusionTexture != null) { - standardParam = StandardTexture(gltf, src); + standardParam = StandardTexture(parser, src); } if (src.pbrMetallicRoughness.baseColorFactor != null && src.pbrMetallicRoughness.baseColorFactor.Length == 4) { @@ -98,7 +98,7 @@ namespace UniGLTF if (src.pbrMetallicRoughness.baseColorTexture != null && src.pbrMetallicRoughness.baseColorTexture.index != -1) { - material.mainTexture = await getTexture(awaitCaller, gltf, BaseColorTexture(gltf, src)); + material.mainTexture = await getTexture(awaitCaller, parser.GLTF, BaseColorTexture(parser, src)); // Texture Offset and Scale MaterialFactory.SetTextureOffsetAndScale(material, src.pbrMetallicRoughness.baseColorTexture, "_MainTex"); @@ -108,7 +108,7 @@ namespace UniGLTF { material.EnableKeyword("_METALLICGLOSSMAP"); - var texture = await getTexture(awaitCaller, gltf, standardParam); + var texture = await getTexture(awaitCaller, parser.GLTF, standardParam); if (texture != null) { material.SetTexture(GetTextureParam.METALLIC_GLOSS_PROP, texture); @@ -131,7 +131,7 @@ namespace UniGLTF if (src.normalTexture != null && src.normalTexture.index != -1) { material.EnableKeyword("_NORMALMAP"); - var texture = await getTexture(awaitCaller, gltf, NormalTexture(gltf, src)); + var texture = await getTexture(awaitCaller, parser.GLTF, NormalTexture(parser, src)); if (texture != null) { material.SetTexture(GetTextureParam.NORMAL_PROP, texture); @@ -144,7 +144,7 @@ namespace UniGLTF if (src.occlusionTexture != null && src.occlusionTexture.index != -1) { - var texture = await getTexture(awaitCaller, gltf, standardParam); + var texture = await getTexture(awaitCaller, parser.GLTF, standardParam); if (texture != null) { material.SetTexture(GetTextureParam.OCCLUSION_PROP, texture); @@ -168,7 +168,7 @@ namespace UniGLTF if (src.emissiveTexture != null && src.emissiveTexture.index != -1) { - var texture = await getTexture(awaitCaller, gltf, GetTextureParam.CreateSRGB(gltf, src.emissiveTexture.index)); + var texture = await getTexture(awaitCaller, parser.GLTF, GetTextureParam.CreateSRGB(parser, src.emissiveTexture.index)); if (texture != null) { material.SetTexture("_EmissionMap", texture); diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/UnlitMaterialItem.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/UnlitMaterialItem.cs index 8960e8b27..a610f48ba 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/UnlitMaterialItem.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/UnlitMaterialItem.cs @@ -8,20 +8,20 @@ namespace UniGLTF { public const string ShaderName = "UniGLTF/UniUnlit"; - public static async Task CreateAsync(IAwaitCaller awaitCaller, glTF gltf, int i, GetTextureAsyncFunc getTexture, bool hasVertexColor) + public static async Task CreateAsync(IAwaitCaller awaitCaller, GltfParser parser, int i, GetTextureAsyncFunc getTexture, bool hasVertexColor) { if (getTexture == null) { getTexture = (_x, _y, _z) => Task.FromResult(default); } - var src = gltf.materials[i]; + var src = parser.GLTF.materials[i]; var material = MaterialFactory.CreateMaterial(i, src, ShaderName); // texture if (src.pbrMetallicRoughness.baseColorTexture != null) { - material.mainTexture = await getTexture(awaitCaller, gltf, GetTextureParam.CreateSRGB(gltf, src.pbrMetallicRoughness.baseColorTexture.index)); + material.mainTexture = await getTexture(awaitCaller, parser.GLTF, GetTextureParam.CreateSRGB(parser, src.pbrMetallicRoughness.baseColorTexture.index)); // Texture Offset and Scale MaterialFactory.SetTextureOffsetAndScale(material, src.pbrMetallicRoughness.baseColorTexture, "_MainTex"); diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GetTextureParam.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GetTextureParam.cs index 8bfcc14a3..358e9609d 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GetTextureParam.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GetTextureParam.cs @@ -1,7 +1,13 @@ using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using UnityEngine; namespace UniGLTF { + public delegate Task> GetTextureBytesAsync(); + /// /// STANDARD(Pbr) texture = occlusion + metallic + smoothness /// @@ -38,98 +44,285 @@ namespace UniGLTF return src; } } - - readonly string m_name; - - public string GltflName => m_name; - - public string ConvertedName + + public struct NameExt { - get + public readonly string GltfName; + public readonly string ConvertedName; + + public readonly string Ext; + public string Uri; + + public NameExt(string gltfName, string convertedName, string ext, string uri) { - switch (TextureType) + GltfName = gltfName; + ConvertedName = convertedName; + Ext = ext; + Uri = uri; + } + + public string GltfFileName => $"{GltfName}{Ext}"; + + public string ConvertedFileName => $"{ConvertedName}.png"; + + public static NameExt Create(glTF gltf, int textureIndex, TextureTypes textureType) + { + if (textureIndex < 0 || textureIndex >= gltf.textures.Count) { - case TextureTypes.StandardMap: return $"{m_name}{STANDARD_SUFFIX}"; - case TextureTypes.NormalMap: return $"{m_name}{NORMAL_SUFFIX}"; - default: return m_name; + throw new ArgumentOutOfRangeException(); + } + var gltfTexture = gltf.textures[textureIndex]; + if(gltfTexture.source < 0 || gltfTexture.source >=gltf.images.Count) + { + throw new ArgumentOutOfRangeException(); + } + var gltfImage = gltf.images[gltfTexture.source]; + return new NameExt(gltfTexture.name, Convert(gltfTexture.name, textureType), gltfImage.GetExt(), gltfImage.uri); + } + + static string Convert(string name, TextureTypes textureType) + { + switch (textureType) + { + case TextureTypes.StandardMap: return $"{name}{STANDARD_SUFFIX}"; + case TextureTypes.NormalMap: return $"{name}{NORMAL_SUFFIX}"; + default: return name; + } + } + } + + public readonly NameExt Name; + public string GltfName => Name.GltfName; + public string GltfFileName => Name.GltfFileName; + public string ConvertedName => Name.ConvertedName; + public string ConvertedFileName => Name.ConvertedFileName; + public string Uri => Name.Uri; + + public struct TextureSamplerParam + { + public enum TextureWrapType + { + All, + U, + V, + W, + } + + public (TextureWrapType, TextureWrapMode)[] WrapModes; + public FilterMode FilterMode; + + public static TextureSamplerParam Create(glTF gltf, int index) + { + var gltfTexture = gltf.textures[index]; + if(gltfTexture.sampler<0 || gltfTexture.sampler>= gltf.samplers.Count) + { + // default + return new TextureSamplerParam + { + FilterMode = FilterMode.Bilinear, + WrapModes = new (TextureWrapType, TextureWrapMode)[]{}, + }; + } + + var gltfSampler = gltf.samplers[gltfTexture.sampler]; + return new TextureSamplerParam + { + WrapModes = GetUnityWrapMode(gltfSampler).ToArray(), + FilterMode = ImportFilterMode(gltfSampler.minFilter), + }; + } + + public static IEnumerable<(TextureWrapType, TextureWrapMode)> GetUnityWrapMode(glTFTextureSampler sampler) + { + if (sampler.wrapS == sampler.wrapT) + { + switch (sampler.wrapS) + { + case glWrap.NONE: // default + yield return (TextureWrapType.All, TextureWrapMode.Repeat); + break; + + case glWrap.CLAMP_TO_EDGE: + yield return (TextureWrapType.All, TextureWrapMode.Clamp); + break; + + case glWrap.REPEAT: + yield return (TextureWrapType.All, TextureWrapMode.Repeat); + break; + + case glWrap.MIRRORED_REPEAT: + yield return (TextureWrapType.All, TextureWrapMode.Mirror); + break; + + default: + throw new NotImplementedException(); + } + } + else + { + switch (sampler.wrapS) + { + case glWrap.NONE: // default + yield return (TextureWrapType.U, TextureWrapMode.Repeat); + break; + + case glWrap.CLAMP_TO_EDGE: + yield return (TextureWrapType.U, TextureWrapMode.Clamp); + break; + + case glWrap.REPEAT: + yield return (TextureWrapType.U, TextureWrapMode.Repeat); + break; + + case glWrap.MIRRORED_REPEAT: + yield return (TextureWrapType.U, TextureWrapMode.Mirror); + break; + + default: + throw new NotImplementedException(); + } + switch (sampler.wrapT) + { + case glWrap.NONE: // default + yield return (TextureWrapType.V, TextureWrapMode.Repeat); + break; + + case glWrap.CLAMP_TO_EDGE: + yield return (TextureWrapType.V, TextureWrapMode.Clamp); + break; + + case glWrap.REPEAT: + yield return (TextureWrapType.V, TextureWrapMode.Repeat); + break; + + case glWrap.MIRRORED_REPEAT: + yield return (TextureWrapType.V, TextureWrapMode.Mirror); + break; + + default: + throw new NotImplementedException(); + } + } + } + + public static FilterMode ImportFilterMode(glFilter filterMode) + { + switch (filterMode) + { + case glFilter.NEAREST: + case glFilter.NEAREST_MIPMAP_LINEAR: + case glFilter.NEAREST_MIPMAP_NEAREST: + return FilterMode.Point; + + case glFilter.NONE: + case glFilter.LINEAR: + case glFilter.LINEAR_MIPMAP_NEAREST: + return FilterMode.Bilinear; + + case glFilter.LINEAR_MIPMAP_LINEAR: + return FilterMode.Trilinear; + + default: + throw new NotImplementedException(); } } } + public TextureSamplerParam Sampler; + public readonly TextureTypes TextureType; public readonly float MetallicFactor; public readonly float RoughnessFactor; - public readonly ushort? Index0; - public readonly ushort? Index1; - public readonly ushort? Index2; - public readonly ushort? Index3; - public readonly ushort? Index4; - public readonly ushort? Index5; + + public readonly GetTextureBytesAsync Index0; + public readonly GetTextureBytesAsync Index1; + public readonly GetTextureBytesAsync Index2; + public readonly GetTextureBytesAsync Index3; + public readonly GetTextureBytesAsync Index4; + public readonly GetTextureBytesAsync Index5; /// /// この種類は RGB チャンネルの組み換えが必用 /// 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(NameExt name, TextureSamplerParam sampler, TextureTypes textureType, float metallicFactor, float roughnessFactor, + GetTextureBytesAsync i0, + GetTextureBytesAsync i1, + GetTextureBytesAsync i2, + GetTextureBytesAsync i3, + GetTextureBytesAsync i4, + GetTextureBytesAsync i5) { - if (string.IsNullOrEmpty(name)) - { - throw new ArgumentNullException(); - } - m_name = name; - + Name = name; + Sampler = sampler; 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 = i0; + Index1 = i1; + Index2 = i2; + Index3 = i3; + Index4 = i4; + Index5 = i5; } - public static GetTextureParam CreateSRGB(glTF gltf, int textureIndex) + public static GetTextureParam CreateSRGB(GltfParser parser, int textureIndex) { - var name = gltf.textures[textureIndex].name; - return new GetTextureParam(name, TextureTypes.sRGB, default, default, textureIndex, default, default, default, default, default); + var name = NameExt.Create(parser.GLTF, textureIndex, TextureTypes.sRGB); + var sampler = TextureSamplerParam.Create(parser.GLTF, textureIndex); + GetTextureBytesAsync getTextureBytesAsync = async () => parser.GLTF.GetImageBytesFromTextureIndex(parser.Storage, textureIndex); + return new GetTextureParam(name, sampler, TextureTypes.sRGB, default, default, getTextureBytesAsync, default, default, default, default, default); } - public static GetTextureParam Create(glTF gltf, int index, string prop, float metallicFactor, float roughnessFactor) + public static GetTextureParam CreateNormal(GltfParser parser, int textureIndex) + { + var name = NameExt.Create(parser.GLTF, textureIndex, TextureTypes.NormalMap); + var sampler = TextureSamplerParam.Create(parser.GLTF, textureIndex); + GetTextureBytesAsync getTextureBytesAsync = async () => parser.GLTF.GetImageBytesFromTextureIndex(parser.Storage, textureIndex); + return new GetTextureParam(name, sampler, TextureTypes.NormalMap, default, default, getTextureBytesAsync, default, default, default, default, default); + } + + public static GetTextureParam CreateStandard(GltfParser parser, int? metallicRoughnessTextureIndex, int? occlusionTextureIndex, float metallicFactor, float roughnessFactor) + { + NameExt name = default; + + GetTextureBytesAsync getMetallicRoughnessAsync = default; + TextureSamplerParam sampler = default; + if (metallicRoughnessTextureIndex.HasValue) + { + name = NameExt.Create(parser.GLTF, metallicRoughnessTextureIndex.Value, TextureTypes.StandardMap); + sampler = TextureSamplerParam.Create(parser.GLTF, metallicRoughnessTextureIndex.Value); + getMetallicRoughnessAsync = async () => parser.GLTF.GetImageBytesFromTextureIndex(parser.Storage, metallicRoughnessTextureIndex.Value); + } + + GetTextureBytesAsync getOcclusionAsync = default; + if (occlusionTextureIndex.HasValue) + { + if(string.IsNullOrEmpty(name.GltfName)){ + name = NameExt.Create(parser.GLTF, occlusionTextureIndex.Value, TextureTypes.StandardMap); + } + sampler = TextureSamplerParam.Create(parser.GLTF, occlusionTextureIndex.Value); + getOcclusionAsync = async () => parser.GLTF.GetImageBytesFromTextureIndex(parser.Storage, occlusionTextureIndex.Value); + } + + return new GetTextureParam(name, sampler, TextureTypes.StandardMap, metallicFactor, roughnessFactor, getMetallicRoughnessAsync, getOcclusionAsync, default, default, default, default); + } + + public static GetTextureParam Create(GltfParser parser, int index, string prop, float metallicFactor, float roughnessFactor) { switch (prop) { case NORMAL_PROP: - return CreateNormal(gltf, index); + return CreateNormal(parser, index); case OCCLUSION_PROP: case METALLIC_GLOSS_PROP: - return CreateStandard(gltf, index, default, metallicFactor, roughnessFactor); + return CreateStandard(parser, index, default, metallicFactor, roughnessFactor); default: - return CreateSRGB(gltf, index); + return CreateSRGB(parser, index); } } - - public static GetTextureParam CreateNormal(glTF gltf, int textureIndex) - { - var name = gltf.textures[textureIndex].name; - return new GetTextureParam(name, TextureTypes.NormalMap, default, default, textureIndex, default, default, default, default, default); - } - - public static GetTextureParam CreateStandard(glTF gltf, int? metallicRoughnessTextureIndex, int? occlusionTextureIndex, float metallicFactor, float roughnessFactor) - { - 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/TextureIO/GltfTextureLoader.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureLoader.cs deleted file mode 100644 index 2071151a1..000000000 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureLoader.cs +++ /dev/null @@ -1,10 +0,0 @@ -using System; -using System.Threading.Tasks; -using UnityEngine; - -namespace UniGLTF -{ - public static class GltfTextureLoader - { - } -} diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureLoader.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureLoader.cs.meta deleted file mode 100644 index 28ea0287a..000000000 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureLoader.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: c541795ac7ae51d468dfff535e5333cb -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureFactory.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureFactory.cs index 8cd8391b0..9e418a648 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureFactory.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureFactory.cs @@ -52,12 +52,12 @@ namespace UniGLTF public bool TryGetExternal(GetTextureParam param, bool used, out Texture2D external) { - if (param.Index0.HasValue && ExternalMap != null) + if (param.Index0!=null && ExternalMap != null) { var cacheName = param.ConvertedName; if (param.TextureType == GetTextureParam.TextureTypes.NormalMap) { - cacheName = param.GltflName; + cacheName = param.GltfName; if (m_textureCache.TryGetValue(cacheName, out TextureLoadInfo normalInfo)) { external = normalInfo.Texture; @@ -151,44 +151,69 @@ namespace UniGLTF } } - async Task GetOrCreateBaseTexture(IAwaitCaller awaitCaller, glTF gltf, int textureIndex, bool used) + async Task GetOrCreateBaseTexture(IAwaitCaller awaitCaller, GetTextureParam param, GetTextureBytesAsync getTextureBytesAsync, RenderTextureReadWrite colorSpace, bool used) { - var name = gltf.textures[textureIndex].name; + var name = param.GltfName; if (m_textureCache.TryGetValue(name, out TextureLoadInfo cacheInfo)) { return cacheInfo; } // not found. load new - var imageBytes = await awaitCaller.Run(() => - { - var imageIndex = gltf.textures[textureIndex].source; - var segments = gltf.GetImageBytes(m_storage, imageIndex); - return ToArray(segments); - }); + var imageBytes = await getTextureBytesAsync(); // // texture from image(png etc) bytes // - var colorSpace = gltf.GetColorSpace(textureIndex); var texture = new Texture2D(2, 2, TextureFormat.ARGB32, false, colorSpace == RenderTextureReadWrite.Linear); - texture.name = gltf.textures[textureIndex].name; + texture.name = name; if (imageBytes != null) { - texture.LoadImage(imageBytes); + texture.LoadImage(ToArray(imageBytes)); } - var sampler = gltf.GetSamplerFromTextureIndex(textureIndex); - if (sampler != null) - { - TextureSamplerUtil.SetSampler(texture, sampler); - } + SetSampler(texture, param); cacheInfo = new TextureLoadInfo(texture, used, false); m_textureCache.Add(name, cacheInfo); return cacheInfo; } + public static void SetSampler(Texture2D texture, GetTextureParam param) + { + if (texture == null) + { + return; + } + + foreach (var (key, value) in param.Sampler.WrapModes) + { + switch (key) + { + case GetTextureParam.TextureSamplerParam.TextureWrapType.All: + texture.wrapMode = value; + break; + + case GetTextureParam.TextureSamplerParam.TextureWrapType.U: + texture.wrapModeU = value; + break; + + case GetTextureParam.TextureSamplerParam.TextureWrapType.V: + texture.wrapModeV = value; + break; + + case GetTextureParam.TextureSamplerParam.TextureWrapType.W: + texture.wrapModeW = value; + break; + + default: + throw new NotImplementedException(); + } + } + + texture.filterMode = param.Sampler.FilterMode; + } + /// /// テクスチャーをロード、必要であれば変換して返す。 /// 同じものはキャッシュを返す @@ -212,7 +237,7 @@ namespace UniGLTF { case GetTextureParam.TextureTypes.NormalMap: { - var baseTexture = await GetOrCreateBaseTexture(awaitCaller, gltf, param.Index0.Value, false); + var baseTexture = await GetOrCreateBaseTexture(awaitCaller, param, param.Index0, RenderTextureReadWrite.Linear, false); var converted = NormalConverter.Import(baseTexture.Texture); converted.name = param.ConvertedName; var info = new TextureLoadInfo(converted, true, false); @@ -223,14 +248,14 @@ namespace UniGLTF case GetTextureParam.TextureTypes.StandardMap: { TextureLoadInfo baseTexture = default; - if (param.Index0.HasValue) + if (param.Index0!=null) { - baseTexture = await GetOrCreateBaseTexture(awaitCaller, gltf, param.Index0.Value, false); + baseTexture = await GetOrCreateBaseTexture(awaitCaller, param, param.Index0, RenderTextureReadWrite.Linear, false); } TextureLoadInfo occlusionBaseTexture = default; - if (param.Index1.HasValue) + if (param.Index1!=null) { - occlusionBaseTexture = await GetOrCreateBaseTexture(awaitCaller, gltf, param.Index1.Value, false); + occlusionBaseTexture = await GetOrCreateBaseTexture(awaitCaller, param, param.Index1, RenderTextureReadWrite.Linear, false); } var converted = OcclusionMetallicRoughnessConverter.Import(baseTexture.Texture, param.MetallicFactor, param.RoughnessFactor, occlusionBaseTexture.Texture); converted.name = param.ConvertedName; @@ -241,7 +266,7 @@ namespace UniGLTF default: { - var baseTexture = await GetOrCreateBaseTexture(awaitCaller, gltf, param.Index0.Value, true); + var baseTexture = await GetOrCreateBaseTexture(awaitCaller, param, param.Index0, RenderTextureReadWrite.sRGB, true); return baseTexture.Texture; } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureSamplerUtil.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureSamplerUtil.cs index 63bd0429f..5a3bea1ae 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureSamplerUtil.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureSamplerUtil.cs @@ -7,180 +7,6 @@ namespace UniGLTF { public static class TextureSamplerUtil { - #region WrapMode - public enum TextureWrapType - { - All, -#if UNITY_2017_1_OR_NEWER - U, - V, - W, -#endif - } - - public static KeyValuePair TypeWithMode(TextureWrapType type, TextureWrapMode mode) - { - return new KeyValuePair(type, mode); - } - - public static IEnumerable> GetUnityWrapMode(glTFTextureSampler sampler) - { -#if UNITY_2017_1_OR_NEWER - if (sampler.wrapS == sampler.wrapT) - { - switch (sampler.wrapS) - { - case glWrap.NONE: // default - yield return TypeWithMode(TextureWrapType.All, TextureWrapMode.Repeat); - break; - - case glWrap.CLAMP_TO_EDGE: - yield return TypeWithMode(TextureWrapType.All, TextureWrapMode.Clamp); - break; - - case glWrap.REPEAT: - yield return TypeWithMode(TextureWrapType.All, TextureWrapMode.Repeat); - break; - - case glWrap.MIRRORED_REPEAT: - yield return TypeWithMode(TextureWrapType.All, TextureWrapMode.Mirror); - break; - - default: - throw new NotImplementedException(); - } - } - else - { - switch (sampler.wrapS) - { - case glWrap.NONE: // default - yield return TypeWithMode(TextureWrapType.U, TextureWrapMode.Repeat); - break; - - case glWrap.CLAMP_TO_EDGE: - yield return TypeWithMode(TextureWrapType.U, TextureWrapMode.Clamp); - break; - - case glWrap.REPEAT: - yield return TypeWithMode(TextureWrapType.U, TextureWrapMode.Repeat); - break; - - case glWrap.MIRRORED_REPEAT: - yield return TypeWithMode(TextureWrapType.U, TextureWrapMode.Mirror); - break; - - default: - throw new NotImplementedException(); - } - switch (sampler.wrapT) - { - case glWrap.NONE: // default - yield return TypeWithMode(TextureWrapType.V, TextureWrapMode.Repeat); - break; - - case glWrap.CLAMP_TO_EDGE: - yield return TypeWithMode(TextureWrapType.V, TextureWrapMode.Clamp); - break; - - case glWrap.REPEAT: - yield return TypeWithMode(TextureWrapType.V, TextureWrapMode.Repeat); - break; - - case glWrap.MIRRORED_REPEAT: - yield return TypeWithMode(TextureWrapType.V, TextureWrapMode.Mirror); - break; - - default: - throw new NotImplementedException(); - } -#else - // Unity2017.1より前 - // * wrapSとwrapTの区別が無くてwrapしかない - // * Mirrorが無い - - switch (sampler.wrapS) - { - case glWrap.NONE: // default - yield return TypeWithMode(TextureWrapType.All, TextureWrapMode.Repeat); - break; - - case glWrap.CLAMP_TO_EDGE: - case glWrap.MIRRORED_REPEAT: - yield return TypeWithMode(TextureWrapType.All, TextureWrapMode.Clamp); - break; - - case glWrap.REPEAT: - yield return TypeWithMode(TextureWrapType.All, TextureWrapMode.Repeat); - break; - - default: - throw new NotImplementedException(); -#endif - } - } - #endregion - - public static FilterMode ImportFilterMode(glFilter filterMode) - { - switch (filterMode) - { - case glFilter.NEAREST: - case glFilter.NEAREST_MIPMAP_LINEAR: - case glFilter.NEAREST_MIPMAP_NEAREST: - return FilterMode.Point; - - case glFilter.NONE: - case glFilter.LINEAR: - case glFilter.LINEAR_MIPMAP_NEAREST: - return FilterMode.Bilinear; - - case glFilter.LINEAR_MIPMAP_LINEAR: - return FilterMode.Trilinear; - - default: - throw new NotImplementedException(); - - } - } - - public static void SetSampler(Texture2D texture, glTFTextureSampler sampler) - { - if (texture == null) - { - return; - } - - foreach (var kv in GetUnityWrapMode(sampler)) - { - switch (kv.Key) - { - case TextureWrapType.All: - texture.wrapMode = kv.Value; - break; - -#if UNITY_2017_1_OR_NEWER - case TextureWrapType.U: - texture.wrapModeU = kv.Value; - break; - - case TextureWrapType.V: - texture.wrapModeV = kv.Value; - break; - - case TextureWrapType.W: - texture.wrapModeW = kv.Value; - break; -#endif - - default: - throw new NotImplementedException(); - } - } - - texture.filterMode = ImportFilterMode(sampler.minFilter); - } - #region Export public static glFilter ExportFilterMode(Texture texture) { diff --git a/Assets/UniGLTF/Tests/UniGLTF/GltfLoadTests.cs b/Assets/UniGLTF/Tests/UniGLTF/GltfLoadTests.cs index e27914c53..e0f42e241 100644 --- a/Assets/UniGLTF/Tests/UniGLTF/GltfLoadTests.cs +++ b/Assets/UniGLTF/Tests/UniGLTF/GltfLoadTests.cs @@ -99,7 +99,7 @@ namespace UniGLTF } // should unique - var gltfTextures = GltfTextureEnumerator.Enumerate(parser.GLTF); + var gltfTextures = GltfTextureEnumerator.Enumerate(parser).ToArray(); var distinct = gltfTextures.Distinct().ToArray(); Assert.True(gltfTextures.SequenceEqual(distinct)); } diff --git a/Assets/UniGLTF/Tests/UniGLTF/MaterialTests.cs b/Assets/UniGLTF/Tests/UniGLTF/MaterialTests.cs index f08778cd1..aabad09b7 100644 --- a/Assets/UniGLTF/Tests/UniGLTF/MaterialTests.cs +++ b/Assets/UniGLTF/Tests/UniGLTF/MaterialTests.cs @@ -1,9 +1,7 @@ -using System.Collections.Generic; -using NUnit.Framework; +using NUnit.Framework; using UnityEngine; using UniJSON; using System.Linq; -using System.Threading.Tasks; namespace UniGLTF { diff --git a/Assets/VRM/Editor/Format/VRMImporterMenu.cs b/Assets/VRM/Editor/Format/VRMImporterMenu.cs index 727cdc289..97d26f9f6 100644 --- a/Assets/VRM/Editor/Format/VRMImporterMenu.cs +++ b/Assets/VRM/Editor/Format/VRMImporterMenu.cs @@ -83,7 +83,7 @@ namespace VRM using (var context = new VRMImporterContext(parser, map)) { var editor = new VRMEditorImporterContext(context, prefabPath); - foreach (var textureInfo in new VRMTextureEnumerator(context.VRM).Enumerate(parser.GLTF)) + foreach (var textureInfo in new VRMTextureEnumerator(context.VRM).Enumerate(parser)) { 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 4c5ac52d0..218057dd6 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, map)) { var editor = new VRMEditorImporterContext(context, prefabPath); - foreach (var textureInfo in new VRMTextureEnumerator(context.VRM).Enumerate(parser.GLTF)) + foreach (var textureInfo in new VRMTextureEnumerator(context.VRM).Enumerate(parser)) { TextureImporterConfigurator.Configure(textureInfo, map.ToDictionary(x => x.name, x => x.texture as Texture2D)); } diff --git a/Assets/VRM/Runtime/IO/VRMImporterContext.cs b/Assets/VRM/Runtime/IO/VRMImporterContext.cs index f081bc205..3678fda6d 100644 --- a/Assets/VRM/Runtime/IO/VRMImporterContext.cs +++ b/Assets/VRM/Runtime/IO/VRMImporterContext.cs @@ -297,7 +297,7 @@ namespace VRM meta.Title = gltfMeta.title; if (gltfMeta.texture >= 0) { - meta.Thumbnail = await TextureFactory.GetTextureAsync(awaitCaller, GLTF, GetTextureParam.CreateSRGB(GLTF, gltfMeta.texture)); + meta.Thumbnail = await TextureFactory.GetTextureAsync(awaitCaller, GLTF, GetTextureParam.CreateSRGB(Parser, gltfMeta.texture)); } meta.AllowedUser = gltfMeta.allowedUser; meta.ViolentUssage = gltfMeta.violentUssage; diff --git a/Assets/VRM/Runtime/IO/VRMMaterialImporter.cs b/Assets/VRM/Runtime/IO/VRMMaterialImporter.cs index 6ea9985bf..564863e3f 100644 --- a/Assets/VRM/Runtime/IO/VRMMaterialImporter.cs +++ b/Assets/VRM/Runtime/IO/VRMMaterialImporter.cs @@ -21,7 +21,7 @@ namespace VRM "VRM/UnlitTransparentZWrite", }; - public static async Task CreateAsync(IAwaitCaller awaitCaller, glTF gltf, int m_index, glTF_VRM_Material vrmMaterial, GetTextureAsyncFunc getTexture) + public static async Task CreateAsync(IAwaitCaller awaitCaller, GltfParser parser, int m_index, glTF_VRM_Material vrmMaterial, GetTextureAsyncFunc getTexture) { var item = vrmMaterial; var shaderName = item.shader; @@ -41,7 +41,7 @@ namespace VRM // Debug.LogWarningFormat("unknown shader {0}.", shaderName); // #endif } - return await MaterialFactory.DefaultCreateMaterialAsync(awaitCaller, gltf, m_index, getTexture); + return await MaterialFactory.DefaultCreateMaterialAsync(awaitCaller, parser, m_index, getTexture); } // @@ -49,7 +49,7 @@ namespace VRM // var material = new Material(shader); // use material.name, because material name may renamed in GltfParser. - material.name = gltf.materials[m_index].name; + material.name = parser.GLTF.materials[m_index].name; material.renderQueue = item.renderQueue; foreach (var kv in item.floatProperties) @@ -73,8 +73,8 @@ namespace VRM } foreach (var kv in item.textureProperties) { - var param = GetTextureParam.Create(gltf, kv.Value, kv.Key, 1, 1); - var texture = await getTexture(awaitCaller, gltf, param); + var param = GetTextureParam.Create(parser, kv.Value, kv.Key, 1, 1); + var texture = await getTexture(awaitCaller, parser.GLTF, param); if (texture != null) { material.SetTexture(kv.Key, texture); @@ -115,15 +115,15 @@ namespace VRM m_materials = materials; } - public Task CreateMaterialAsync(IAwaitCaller awaitCaller, glTF gltf, int i, GetTextureAsyncFunc getTexture) + public Task CreateMaterialAsync(IAwaitCaller awaitCaller, GltfParser parser, int i, GetTextureAsyncFunc getTexture) { if (i == 0 && m_materials.Count == 0) { // dummy - return MaterialFactory.DefaultCreateMaterialAsync(awaitCaller, gltf, i, getTexture); + return MaterialFactory.DefaultCreateMaterialAsync(awaitCaller, parser, i, getTexture); } - return MToonMaterialItem.CreateAsync(awaitCaller, gltf, i, m_materials[i], getTexture); + return MToonMaterialItem.CreateAsync(awaitCaller, parser, i, m_materials[i], getTexture); } } } diff --git a/Assets/VRM/Runtime/IO/VRMTextureEnumerator.cs b/Assets/VRM/Runtime/IO/VRMTextureEnumerator.cs index c84bc66ad..e08cb61ee 100644 --- a/Assets/VRM/Runtime/IO/VRMTextureEnumerator.cs +++ b/Assets/VRM/Runtime/IO/VRMTextureEnumerator.cs @@ -11,9 +11,9 @@ namespace VRM m_vrm = vrm; } - public IEnumerable Enumerate(glTF gltf) + public IEnumerable Enumerate(GltfParser parser) { - for (int i = 0; i < gltf.materials.Count; ++i) + for (int i = 0; i < parser.GLTF.materials.Count; ++i) { var vrmMaterial = m_vrm.materialProperties[i]; if (vrmMaterial.shader == MToon.Utils.ShaderName) @@ -22,13 +22,13 @@ namespace VRM foreach (var kv in vrmMaterial.textureProperties) { // SRGB color or normalmap - yield return GetTextureParam.Create(gltf, kv.Value, kv.Key, default, default); + yield return GetTextureParam.Create(parser, kv.Value, kv.Key, default, default); } } else { // PBR or Unlit - foreach (var textureInfo in GltfTextureEnumerator.EnumerateTextures(gltf, gltf.materials[i])) + foreach (var textureInfo in GltfTextureEnumerator.EnumerateTextures(parser, parser.GLTF.materials[i])) { yield return textureInfo; } @@ -38,7 +38,7 @@ namespace VRM // thumbnail if (m_vrm.meta != null && m_vrm.meta.texture != -1) { - yield return GetTextureParam.CreateSRGB(gltf, m_vrm.meta.texture); + yield return GetTextureParam.CreateSRGB(parser, m_vrm.meta.texture); } } }