From e3a830bddb2e3fd5414a416d60dd37872ecf000f Mon Sep 17 00:00:00 2001 From: ousttrue Date: Wed, 24 Mar 2021 16:44:44 +0900 Subject: [PATCH] TextureImportParam --- .../Runtime/UniGLTF/IO/ImporterContext.cs | 3 +- .../IO/TextureIO/GltfTextureExporter.cs | 2 +- .../UniGLTF/IO/TextureIO/TextureFactory.cs | 22 ++-- Assets/VRMShaders/Runtime/GetTextureParam.cs | 115 ------------------ .../VRMShaders/Runtime/TextureImportName.cs | 50 ++++++++ .../Runtime/TextureImportName.cs.meta | 11 ++ .../VRMShaders/Runtime/TextureImportParam.cs | 72 +++++++++++ ...ram.cs.meta => TextureImportParam.cs.meta} | 0 8 files changed, 146 insertions(+), 129 deletions(-) delete mode 100644 Assets/VRMShaders/Runtime/GetTextureParam.cs create mode 100644 Assets/VRMShaders/Runtime/TextureImportName.cs create mode 100644 Assets/VRMShaders/Runtime/TextureImportName.cs.meta create mode 100644 Assets/VRMShaders/Runtime/TextureImportParam.cs rename Assets/VRMShaders/Runtime/{GetTextureParam.cs.meta => TextureImportParam.cs.meta} (100%) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs index 641b5552d..4569d0c59 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs @@ -170,9 +170,10 @@ namespace UniGLTF await OnLoadModel(m_awaitCaller, MeasureTime); } - protected virtual async Task OnLoadModel(IAwaitCaller awaitCaller, Func MeasureTime) + protected virtual Task OnLoadModel(IAwaitCaller awaitCaller, Func MeasureTime) { // do nothing + return Task.FromResult(null); } async Task BuildMeshAsync(Func MeasureTime, MeshImporter.MeshContext x, int i) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureExporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureExporter.cs index 7f5eb3e9d..43e2df86e 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureExporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureExporter.cs @@ -71,7 +71,7 @@ namespace UniGLTF var imageIndex = gltf.images.Count; gltf.images.Add(new glTFImage { - name = GetTextureParam.RemoveSuffix(texture.name), + name = TextureImportName.RemoveSuffix(texture.name), bufferView = viewIndex, mimeType = bytesWithMime.mine, }); diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureFactory.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureFactory.cs index 10356c019..ad49acc7f 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureFactory.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureFactory.cs @@ -4,9 +4,7 @@ using UnityEngine; using System.Linq; using System.Threading.Tasks; using VRMShaders; -#if UNITY_EDITOR -using UnityEditor; -#endif + namespace UniGLTF { @@ -170,7 +168,7 @@ namespace UniGLTF texture.name = name; if (imageBytes != null) { - texture.LoadImage(ToArray(imageBytes)); + texture.LoadImage(imageBytes); } SetSampler(texture, param); @@ -279,7 +277,7 @@ namespace UniGLTF { var name = CreateNameExt(parser.GLTF, textureIndex, TextureImportTypes.sRGB); var sampler = CreateSampler(parser.GLTF, textureIndex); - GetTextureBytesAsync getTextureBytesAsync = async () => parser.GLTF.GetImageBytesFromTextureIndex(parser.Storage, textureIndex); + GetTextureBytesAsync getTextureBytesAsync = () => Task.FromResult(ToArray(parser.GLTF.GetImageBytesFromTextureIndex(parser.Storage, textureIndex))); return new GetTextureParam(name, sampler, TextureImportTypes.sRGB, default, default, getTextureBytesAsync, default, default, default, default, default); } @@ -287,13 +285,13 @@ namespace UniGLTF { var name = CreateNameExt(parser.GLTF, textureIndex, TextureImportTypes.NormalMap); var sampler = CreateSampler(parser.GLTF, textureIndex); - GetTextureBytesAsync getTextureBytesAsync = async () => parser.GLTF.GetImageBytesFromTextureIndex(parser.Storage, textureIndex); + GetTextureBytesAsync getTextureBytesAsync = () => Task.FromResult(ToArray(parser.GLTF.GetImageBytesFromTextureIndex(parser.Storage, textureIndex))); return new GetTextureParam(name, sampler, TextureImportTypes.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; + TextureImportName name = default; GetTextureBytesAsync getMetallicRoughnessAsync = default; SamplerParam sampler = default; @@ -301,7 +299,7 @@ namespace UniGLTF { name = CreateNameExt(parser.GLTF, metallicRoughnessTextureIndex.Value, TextureImportTypes.StandardMap); sampler = CreateSampler(parser.GLTF, metallicRoughnessTextureIndex.Value); - getMetallicRoughnessAsync = async () => parser.GLTF.GetImageBytesFromTextureIndex(parser.Storage, metallicRoughnessTextureIndex.Value); + getMetallicRoughnessAsync = () => Task.FromResult(ToArray(parser.GLTF.GetImageBytesFromTextureIndex(parser.Storage, metallicRoughnessTextureIndex.Value))); } GetTextureBytesAsync getOcclusionAsync = default; @@ -311,7 +309,7 @@ namespace UniGLTF name = CreateNameExt(parser.GLTF, occlusionTextureIndex.Value, TextureImportTypes.StandardMap); } sampler = CreateSampler(parser.GLTF, occlusionTextureIndex.Value); - getOcclusionAsync = async () => parser.GLTF.GetImageBytesFromTextureIndex(parser.Storage, occlusionTextureIndex.Value); + getOcclusionAsync = () => Task.FromResult(ToArray(parser.GLTF.GetImageBytesFromTextureIndex(parser.Storage, occlusionTextureIndex.Value))); } return new GetTextureParam(name, sampler, TextureImportTypes.StandardMap, metallicFactor, roughnessFactor, getMetallicRoughnessAsync, getOcclusionAsync, default, default, default, default); @@ -333,7 +331,7 @@ namespace UniGLTF } } - public static GetTextureParam.NameExt CreateNameExt(glTF gltf, int textureIndex, TextureImportTypes textureType) + public static TextureImportName CreateNameExt(glTF gltf, int textureIndex, TextureImportTypes textureType) { if (textureIndex < 0 || textureIndex >= gltf.textures.Count) { @@ -345,8 +343,8 @@ namespace UniGLTF 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); + var convertedName = TextureImportName.Convert(gltfTexture.name, textureType); + return new TextureImportName(gltfTexture.name, convertedName, gltfImage.GetExt(), gltfImage.uri); } public static SamplerParam CreateSampler(glTF gltf, int index) diff --git a/Assets/VRMShaders/Runtime/GetTextureParam.cs b/Assets/VRMShaders/Runtime/GetTextureParam.cs deleted file mode 100644 index 357019a5f..000000000 --- a/Assets/VRMShaders/Runtime/GetTextureParam.cs +++ /dev/null @@ -1,115 +0,0 @@ -using System; -using System.Threading.Tasks; -using UnityEngine; - - -namespace VRMShaders -{ - 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 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, TextureImportTypes textureType) - { - switch (textureType) - { - case TextureImportTypes.StandardMap: return $"{name}{STANDARD_SUFFIX}"; - case TextureImportTypes.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 SamplerParam Sampler; - - public readonly TextureImportTypes 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 == TextureImportTypes.StandardMap; - - public GetTextureParam(NameExt name, SamplerParam sampler, TextureImportTypes 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/VRMShaders/Runtime/TextureImportName.cs b/Assets/VRMShaders/Runtime/TextureImportName.cs new file mode 100644 index 000000000..713ec6a20 --- /dev/null +++ b/Assets/VRMShaders/Runtime/TextureImportName.cs @@ -0,0 +1,50 @@ +namespace VRMShaders +{ + public struct TextureImportName + { + public readonly string GltfName; + public readonly string ConvertedName; + + public readonly string Ext; + public string Uri; + + public TextureImportName(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 const string NORMAL_SUFFIX = ".normal"; + public const string STANDARD_SUFFIX = ".standard"; + public static string Convert(string name, TextureImportTypes textureType) + { + switch (textureType) + { + case TextureImportTypes.StandardMap: return $"{name}{STANDARD_SUFFIX}"; + case TextureImportTypes.NormalMap: return $"{name}{NORMAL_SUFFIX}"; + default: return name; + } + } + 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; + } + } + } +} diff --git a/Assets/VRMShaders/Runtime/TextureImportName.cs.meta b/Assets/VRMShaders/Runtime/TextureImportName.cs.meta new file mode 100644 index 000000000..e57d20721 --- /dev/null +++ b/Assets/VRMShaders/Runtime/TextureImportName.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0d005850c7abbb24582e9788657afd35 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/VRMShaders/Runtime/TextureImportParam.cs b/Assets/VRMShaders/Runtime/TextureImportParam.cs new file mode 100644 index 000000000..c1f752360 --- /dev/null +++ b/Assets/VRMShaders/Runtime/TextureImportParam.cs @@ -0,0 +1,72 @@ +using System; +using System.Threading.Tasks; + + +namespace VRMShaders +{ + /// + /// get bytes for + /// + /// runtime: + /// Texture2D.LoadImage + /// extact: + /// File.WriteAllBytes + /// + /// + public delegate Task GetTextureBytesAsync(); + + public struct GetTextureParam + { + public const string NORMAL_PROP = "_BumpMap"; + + public const string METALLIC_GLOSS_PROP = "_MetallicGlossMap"; + public const string OCCLUSION_PROP = "_OcclusionMap"; + + + public readonly TextureImportName 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 SamplerParam Sampler; + + public readonly TextureImportTypes 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 == TextureImportTypes.StandardMap; + + public GetTextureParam(TextureImportName name, SamplerParam sampler, TextureImportTypes 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/VRMShaders/Runtime/GetTextureParam.cs.meta b/Assets/VRMShaders/Runtime/TextureImportParam.cs.meta similarity index 100% rename from Assets/VRMShaders/Runtime/GetTextureParam.cs.meta rename to Assets/VRMShaders/Runtime/TextureImportParam.cs.meta