From 7cf9895b0259fb632ce540b4a4db5a1df74ff7ad Mon Sep 17 00:00:00 2001 From: ousttrue Date: Wed, 24 Mar 2021 15:52:51 +0900 Subject: [PATCH] VRMShaders.GetTextureParam --- Assets/UniGLTF/Editor/UniGLTF.Editor.asmdef | 3 +- Assets/UniGLTF/Runtime/UniGLTF.asmdef | 3 +- .../IO/MaterialIO/GltfTextureEnumerator.cs | 2 +- .../UniGLTF/IO/MaterialIO/PBRMaterialItem.cs | 8 +- .../IO/MaterialIO/UnlitMaterialItem.cs | 2 +- .../UniGLTF/IO/TextureIO/GetTextureParam.cs | 328 ------------------ .../UniGLTF/IO/TextureIO/TextureFactory.cs | 199 ++++++++++- Assets/UniGLTF/Tests/UniGLTF.Tests.asmdef | 3 +- Assets/VRM/Editor/VRM.Editor.asmdef | 3 +- Assets/VRM/Runtime/IO/VRMImporterContext.cs | 2 +- Assets/VRM/Runtime/IO/VRMMaterialImporter.cs | 2 +- Assets/VRM/Runtime/IO/VRMTextureEnumerator.cs | 4 +- Assets/VRM/Runtime/VRM.asmdef | 3 +- Assets/VRMShaders/Runtime.meta | 8 + Assets/VRMShaders/Runtime/GetTextureParam.cs | 138 ++++++++ .../Runtime}/GetTextureParam.cs.meta | 0 Assets/VRMShaders/Runtime/VRMShaders.asmdef | 3 + .../VRMShaders/Runtime/VRMShaders.asmdef.meta | 7 + 18 files changed, 371 insertions(+), 347 deletions(-) delete mode 100644 Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GetTextureParam.cs create mode 100644 Assets/VRMShaders/Runtime.meta create mode 100644 Assets/VRMShaders/Runtime/GetTextureParam.cs rename Assets/{UniGLTF/Runtime/UniGLTF/IO/TextureIO => VRMShaders/Runtime}/GetTextureParam.cs.meta (100%) create mode 100644 Assets/VRMShaders/Runtime/VRMShaders.asmdef create mode 100644 Assets/VRMShaders/Runtime/VRMShaders.asmdef.meta diff --git a/Assets/UniGLTF/Editor/UniGLTF.Editor.asmdef b/Assets/UniGLTF/Editor/UniGLTF.Editor.asmdef index 45229522d..60c094749 100644 --- a/Assets/UniGLTF/Editor/UniGLTF.Editor.asmdef +++ b/Assets/UniGLTF/Editor/UniGLTF.Editor.asmdef @@ -3,7 +3,8 @@ "references": [ "UniGLTF", "MeshUtility", - "MeshUtility.Editor" + "MeshUtility.Editor", + "VRMShaders" ], "optionalUnityReferences": [], "includePlatforms": [ diff --git a/Assets/UniGLTF/Runtime/UniGLTF.asmdef b/Assets/UniGLTF/Runtime/UniGLTF.asmdef index d2f68d054..c43bea13c 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF.asmdef +++ b/Assets/UniGLTF/Runtime/UniGLTF.asmdef @@ -2,7 +2,8 @@ "name": "UniGLTF", "references": [ "UniUnlit", - "ShaderProperty.Runtime" + "ShaderProperty.Runtime", + "VRMShaders" ], "optionalUnityReferences": [], "includePlatforms": [], diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfTextureEnumerator.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfTextureEnumerator.cs index 6bb3892b6..a5d450fe2 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfTextureEnumerator.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfTextureEnumerator.cs @@ -27,7 +27,7 @@ namespace UniGLTF // emission if (m.emissiveTexture != null) { - yield return GetTextureParam.CreateSRGB(parser, m.emissiveTexture.index); + yield return TextureFactory.CreateSRGB(parser, m.emissiveTexture.index); } // normal diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/PBRMaterialItem.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/PBRMaterialItem.cs index f6ba9a663..0ba8aef78 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/PBRMaterialItem.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/PBRMaterialItem.cs @@ -45,7 +45,7 @@ namespace UniGLTF public static GetTextureParam BaseColorTexture(GltfParser parser, glTFMaterial src) { - return GetTextureParam.CreateSRGB(parser, src.pbrMetallicRoughness.baseColorTexture.index); + return TextureFactory.CreateSRGB(parser, src.pbrMetallicRoughness.baseColorTexture.index); } public static GetTextureParam StandardTexture(GltfParser parser, glTFMaterial src) @@ -57,7 +57,7 @@ namespace UniGLTF metallicFactor = src.pbrMetallicRoughness.metallicFactor; roughnessFactor = src.pbrMetallicRoughness.roughnessFactor; } - return GetTextureParam.CreateStandard(parser, + return TextureFactory.CreateStandard(parser, src.pbrMetallicRoughness?.metallicRoughnessTexture?.index, src.occlusionTexture?.index, metallicFactor, @@ -66,7 +66,7 @@ namespace UniGLTF public static GetTextureParam NormalTexture(GltfParser parser, glTFMaterial src) { - return GetTextureParam.CreateNormal(parser, src.normalTexture.index); + return TextureFactory.CreateNormal(parser, src.normalTexture.index); } public static async Task CreateAsync(IAwaitCaller awaitCaller, GltfParser parser, int i, GetTextureAsyncFunc getTexture) @@ -168,7 +168,7 @@ namespace UniGLTF if (src.emissiveTexture != null && src.emissiveTexture.index != -1) { - var texture = await getTexture(awaitCaller, parser.GLTF, GetTextureParam.CreateSRGB(parser, src.emissiveTexture.index)); + var texture = await getTexture(awaitCaller, parser.GLTF, TextureFactory.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 a610f48ba..f04643b4f 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/UnlitMaterialItem.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/UnlitMaterialItem.cs @@ -21,7 +21,7 @@ namespace UniGLTF // texture if (src.pbrMetallicRoughness.baseColorTexture != null) { - material.mainTexture = await getTexture(awaitCaller, parser.GLTF, GetTextureParam.CreateSRGB(parser, src.pbrMetallicRoughness.baseColorTexture.index)); + material.mainTexture = await getTexture(awaitCaller, parser.GLTF, TextureFactory.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 deleted file mode 100644 index 358e9609d..000000000 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GetTextureParam.cs +++ /dev/null @@ -1,328 +0,0 @@ -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 - /// - 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 OCCLUSION_PROP = "_OcclusionMap"; - public const string STANDARD_SUFFIX = ".standard"; - - public enum TextureTypes - { - sRGB, - NormalMap, - // Occlusion + Metallic + Smoothness - StandardMap, - Linear, - } - - public static string RemoveSuffix(string src) - { - if (src.EndsWith(NORMAL_SUFFIX)) - { - return src.Substring(0, src.Length - NORMAL_SUFFIX.Length); - } - else if (src.EndsWith(STANDARD_SUFFIX)) - { - return src.Substring(0, src.Length - STANDARD_SUFFIX.Length); - } - else - { - return src; - } - } - - public struct NameExt - { - 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) - { - 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) - { - 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 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(NameExt name, TextureSamplerParam sampler, TextureTypes textureType, float metallicFactor, float roughnessFactor, - GetTextureBytesAsync i0, - GetTextureBytesAsync i1, - GetTextureBytesAsync i2, - GetTextureBytesAsync i3, - GetTextureBytesAsync i4, - GetTextureBytesAsync i5) - { - Name = name; - Sampler = sampler; - TextureType = textureType; - MetallicFactor = metallicFactor; - RoughnessFactor = roughnessFactor; - Index0 = i0; - Index1 = i1; - Index2 = i2; - Index3 = i3; - Index4 = i4; - Index5 = i5; - } - - public static GetTextureParam CreateSRGB(GltfParser parser, int textureIndex) - { - 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 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(parser, index); - - case OCCLUSION_PROP: - case METALLIC_GLOSS_PROP: - return CreateStandard(parser, index, default, metallicFactor, roughnessFactor); - - default: - return CreateSRGB(parser, index); - } - } - } -} diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureFactory.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureFactory.cs index 9e418a648..b541d893b 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureFactory.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureFactory.cs @@ -190,19 +190,19 @@ namespace UniGLTF { switch (key) { - case GetTextureParam.TextureSamplerParam.TextureWrapType.All: + case GetTextureParam.TextureWrapType.All: texture.wrapMode = value; break; - case GetTextureParam.TextureSamplerParam.TextureWrapType.U: + case GetTextureParam.TextureWrapType.U: texture.wrapModeU = value; break; - case GetTextureParam.TextureSamplerParam.TextureWrapType.V: + case GetTextureParam.TextureWrapType.V: texture.wrapModeV = value; break; - case GetTextureParam.TextureSamplerParam.TextureWrapType.W: + case GetTextureParam.TextureWrapType.W: texture.wrapModeW = value; break; @@ -273,5 +273,196 @@ namespace UniGLTF throw new NotImplementedException(); } } + + public static GetTextureParam CreateSRGB(GltfParser parser, int textureIndex) + { + var name = CreateNameExt(parser.GLTF, textureIndex, GetTextureParam.TextureTypes.sRGB); + var sampler = CreateSampler(parser.GLTF, textureIndex); + GetTextureBytesAsync getTextureBytesAsync = async () => parser.GLTF.GetImageBytesFromTextureIndex(parser.Storage, textureIndex); + return new GetTextureParam(name, sampler, GetTextureParam.TextureTypes.sRGB, default, default, getTextureBytesAsync, default, default, default, default, default); + } + + public static GetTextureParam CreateNormal(GltfParser parser, int textureIndex) + { + var name = CreateNameExt(parser.GLTF, textureIndex, GetTextureParam.TextureTypes.NormalMap); + var sampler = CreateSampler(parser.GLTF, textureIndex); + GetTextureBytesAsync getTextureBytesAsync = async () => parser.GLTF.GetImageBytesFromTextureIndex(parser.Storage, textureIndex); + return new GetTextureParam(name, sampler, GetTextureParam.TextureTypes.NormalMap, default, default, getTextureBytesAsync, default, default, default, default, default); + } + + public static GetTextureParam CreateStandard(GltfParser parser, int? metallicRoughnessTextureIndex, int? occlusionTextureIndex, float metallicFactor, float roughnessFactor) + { + GetTextureParam.NameExt name = default; + + GetTextureBytesAsync getMetallicRoughnessAsync = default; + GetTextureParam.TextureSamplerParam sampler = default; + if (metallicRoughnessTextureIndex.HasValue) + { + name = CreateNameExt(parser.GLTF, metallicRoughnessTextureIndex.Value, GetTextureParam.TextureTypes.StandardMap); + sampler = CreateSampler(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 = CreateNameExt(parser.GLTF, occlusionTextureIndex.Value, GetTextureParam.TextureTypes.StandardMap); + } + sampler = CreateSampler(parser.GLTF, occlusionTextureIndex.Value); + getOcclusionAsync = async () => parser.GLTF.GetImageBytesFromTextureIndex(parser.Storage, occlusionTextureIndex.Value); + } + + return new GetTextureParam(name, sampler, GetTextureParam.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 GetTextureParam.NORMAL_PROP: + return CreateNormal(parser, index); + + case GetTextureParam.OCCLUSION_PROP: + case GetTextureParam.METALLIC_GLOSS_PROP: + return CreateStandard(parser, index, default, metallicFactor, roughnessFactor); + + default: + return CreateSRGB(parser, index); + } + } + + public static GetTextureParam.NameExt CreateNameExt(glTF gltf, int textureIndex, GetTextureParam.TextureTypes textureType) + { + if (textureIndex < 0 || textureIndex >= gltf.textures.Count) + { + 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]; + var convertedName = GetTextureParam.NameExt.Convert(gltfTexture.name, textureType); + return new GetTextureParam.NameExt(gltfTexture.name, convertedName, gltfImage.GetExt(), gltfImage.uri); + } + + public static GetTextureParam.TextureSamplerParam CreateSampler(glTF gltf, int index) + { + var gltfTexture = gltf.textures[index]; + if (gltfTexture.sampler < 0 || gltfTexture.sampler >= gltf.samplers.Count) + { + // default + return new GetTextureParam.TextureSamplerParam + { + FilterMode = FilterMode.Bilinear, + WrapModes = new (GetTextureParam.TextureWrapType, TextureWrapMode)[] { }, + }; + } + + var gltfSampler = gltf.samplers[gltfTexture.sampler]; + return new GetTextureParam.TextureSamplerParam + { + WrapModes = GetUnityWrapMode(gltfSampler).ToArray(), + FilterMode = ImportFilterMode(gltfSampler.minFilter), + }; + } + + public static IEnumerable<(GetTextureParam.TextureWrapType, TextureWrapMode)> GetUnityWrapMode(glTFTextureSampler sampler) + { + if (sampler.wrapS == sampler.wrapT) + { + switch (sampler.wrapS) + { + case glWrap.NONE: // default + yield return (GetTextureParam.TextureWrapType.All, TextureWrapMode.Repeat); + break; + + case glWrap.CLAMP_TO_EDGE: + yield return (GetTextureParam.TextureWrapType.All, TextureWrapMode.Clamp); + break; + + case glWrap.REPEAT: + yield return (GetTextureParam.TextureWrapType.All, TextureWrapMode.Repeat); + break; + + case glWrap.MIRRORED_REPEAT: + yield return (GetTextureParam.TextureWrapType.All, TextureWrapMode.Mirror); + break; + + default: + throw new NotImplementedException(); + } + } + else + { + switch (sampler.wrapS) + { + case glWrap.NONE: // default + yield return (GetTextureParam.TextureWrapType.U, TextureWrapMode.Repeat); + break; + + case glWrap.CLAMP_TO_EDGE: + yield return (GetTextureParam.TextureWrapType.U, TextureWrapMode.Clamp); + break; + + case glWrap.REPEAT: + yield return (GetTextureParam.TextureWrapType.U, TextureWrapMode.Repeat); + break; + + case glWrap.MIRRORED_REPEAT: + yield return (GetTextureParam.TextureWrapType.U, TextureWrapMode.Mirror); + break; + + default: + throw new NotImplementedException(); + } + switch (sampler.wrapT) + { + case glWrap.NONE: // default + yield return (GetTextureParam.TextureWrapType.V, TextureWrapMode.Repeat); + break; + + case glWrap.CLAMP_TO_EDGE: + yield return (GetTextureParam.TextureWrapType.V, TextureWrapMode.Clamp); + break; + + case glWrap.REPEAT: + yield return (GetTextureParam.TextureWrapType.V, TextureWrapMode.Repeat); + break; + + case glWrap.MIRRORED_REPEAT: + yield return (GetTextureParam.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(); + } + } + } } diff --git a/Assets/UniGLTF/Tests/UniGLTF.Tests.asmdef b/Assets/UniGLTF/Tests/UniGLTF.Tests.asmdef index 2feff781c..71ff882a1 100644 --- a/Assets/UniGLTF/Tests/UniGLTF.Tests.asmdef +++ b/Assets/UniGLTF/Tests/UniGLTF.Tests.asmdef @@ -2,7 +2,8 @@ "name": "UniGLTF.Tests", "references": [ "UniGLTF", - "UniGLTF.Editor" + "UniGLTF.Editor", + "VRMShaders" ], "optionalUnityReferences": [ "TestAssemblies" diff --git a/Assets/VRM/Editor/VRM.Editor.asmdef b/Assets/VRM/Editor/VRM.Editor.asmdef index a56ec1e28..e9422cc38 100644 --- a/Assets/VRM/Editor/VRM.Editor.asmdef +++ b/Assets/VRM/Editor/VRM.Editor.asmdef @@ -7,7 +7,8 @@ "MeshUtility.Editor", "UniUnlit", "UniGLTF", - "UniGLTF.Editor" + "UniGLTF.Editor", + "VRMShaders" ], "optionalUnityReferences": [], "includePlatforms": [ diff --git a/Assets/VRM/Runtime/IO/VRMImporterContext.cs b/Assets/VRM/Runtime/IO/VRMImporterContext.cs index 3678fda6d..ce18012ee 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(Parser, gltfMeta.texture)); + meta.Thumbnail = await TextureFactory.GetTextureAsync(awaitCaller, GLTF, TextureFactory.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 564863e3f..af75fc1d5 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(parser, kv.Value, kv.Key, 1, 1); + var param = TextureFactory.Create(parser, kv.Value, kv.Key, 1, 1); var texture = await getTexture(awaitCaller, parser.GLTF, param); if (texture != null) { diff --git a/Assets/VRM/Runtime/IO/VRMTextureEnumerator.cs b/Assets/VRM/Runtime/IO/VRMTextureEnumerator.cs index e08cb61ee..6ff2cc8a7 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(parser, kv.Value, kv.Key, default, default); + yield return TextureFactory.Create(parser, 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.CreateSRGB(parser, m_vrm.meta.texture); + yield return TextureFactory.CreateSRGB(parser, m_vrm.meta.texture); } } } diff --git a/Assets/VRM/Runtime/VRM.asmdef b/Assets/VRM/Runtime/VRM.asmdef index 4b1ba93da..3d408ffc8 100644 --- a/Assets/VRM/Runtime/VRM.asmdef +++ b/Assets/VRM/Runtime/VRM.asmdef @@ -6,7 +6,8 @@ "UniHumanoid", "ShaderProperty.Runtime", "MeshUtility", - "UniGLTF" + "UniGLTF", + "VRMShaders" ], "optionalUnityReferences": [], "includePlatforms": [], diff --git a/Assets/VRMShaders/Runtime.meta b/Assets/VRMShaders/Runtime.meta new file mode 100644 index 000000000..a4e70ac3d --- /dev/null +++ b/Assets/VRMShaders/Runtime.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: fcad5106da772b9478acad653777c2c8 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/VRMShaders/Runtime/GetTextureParam.cs b/Assets/VRMShaders/Runtime/GetTextureParam.cs new file mode 100644 index 000000000..95b36d83f --- /dev/null +++ b/Assets/VRMShaders/Runtime/GetTextureParam.cs @@ -0,0 +1,138 @@ +using System; +using System.Threading.Tasks; +using UnityEngine; + + +namespace UniGLTF +{ + public delegate Task> GetTextureBytesAsync(); + + /// + /// 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 OCCLUSION_PROP = "_OcclusionMap"; + public const string STANDARD_SUFFIX = ".standard"; + + public enum TextureTypes + { + sRGB, + NormalMap, + // Occlusion + Metallic + Smoothness + StandardMap, + Linear, + } + + public static string RemoveSuffix(string src) + { + if (src.EndsWith(NORMAL_SUFFIX)) + { + return src.Substring(0, src.Length - NORMAL_SUFFIX.Length); + } + else if (src.EndsWith(STANDARD_SUFFIX)) + { + return src.Substring(0, src.Length - STANDARD_SUFFIX.Length); + } + else + { + return src; + } + } + + public struct NameExt + { + 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) + { + GltfName = gltfName; + ConvertedName = convertedName; + Ext = ext; + Uri = uri; + } + + public string GltfFileName => $"{GltfName}{Ext}"; + + public string ConvertedFileName => $"{ConvertedName}.png"; + + public 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 enum TextureWrapType + { + All, + U, + V, + W, + } + + public struct TextureSamplerParam + { + public (TextureWrapType, TextureWrapMode)[] WrapModes; + public FilterMode FilterMode; + } + + public TextureSamplerParam Sampler; + + public readonly TextureTypes TextureType; + public readonly float MetallicFactor; + public readonly float RoughnessFactor; + + 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(NameExt name, TextureSamplerParam sampler, TextureTypes textureType, float metallicFactor, float roughnessFactor, + GetTextureBytesAsync i0, + GetTextureBytesAsync i1, + GetTextureBytesAsync i2, + GetTextureBytesAsync i3, + GetTextureBytesAsync i4, + GetTextureBytesAsync i5) + { + Name = name; + Sampler = sampler; + TextureType = textureType; + MetallicFactor = metallicFactor; + RoughnessFactor = roughnessFactor; + Index0 = i0; + Index1 = i1; + Index2 = i2; + Index3 = i3; + Index4 = i4; + Index5 = i5; + } + } +} diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GetTextureParam.cs.meta b/Assets/VRMShaders/Runtime/GetTextureParam.cs.meta similarity index 100% rename from Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GetTextureParam.cs.meta rename to Assets/VRMShaders/Runtime/GetTextureParam.cs.meta diff --git a/Assets/VRMShaders/Runtime/VRMShaders.asmdef b/Assets/VRMShaders/Runtime/VRMShaders.asmdef new file mode 100644 index 000000000..6bc7e93fb --- /dev/null +++ b/Assets/VRMShaders/Runtime/VRMShaders.asmdef @@ -0,0 +1,3 @@ +{ + "name": "VRMShaders" +} diff --git a/Assets/VRMShaders/Runtime/VRMShaders.asmdef.meta b/Assets/VRMShaders/Runtime/VRMShaders.asmdef.meta new file mode 100644 index 000000000..d694d87ec --- /dev/null +++ b/Assets/VRMShaders/Runtime/VRMShaders.asmdef.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: da3e51d19d51a544fa14d43fee843098 +AssemblyDefinitionImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: