diff --git a/Assets/UniGLTF/Editor/MeshUtility/TextureSaver.cs b/Assets/UniGLTF/Editor/MeshUtility/TextureSaver.cs index 90172a28a6..a4c050b565 100644 --- a/Assets/UniGLTF/Editor/MeshUtility/TextureSaver.cs +++ b/Assets/UniGLTF/Editor/MeshUtility/TextureSaver.cs @@ -2,7 +2,7 @@ using UnityEditor; using UnityEngine; using VRMShaders; -using ColorSpace = UniGLTF.ColorSpace; +using ColorSpace = VRMShaders.ColorSpace; namespace MeshUtility { diff --git a/Assets/UniGLTF/Runtime/Extensions/ColorConversionExtensions.cs b/Assets/UniGLTF/Runtime/Extensions/ColorConversionExtensions.cs index 0391b4fea7..1e6daabe7e 100644 --- a/Assets/UniGLTF/Runtime/Extensions/ColorConversionExtensions.cs +++ b/Assets/UniGLTF/Runtime/Extensions/ColorConversionExtensions.cs @@ -1,5 +1,6 @@ using System; using UnityEngine; +using ColorSpace = VRMShaders.ColorSpace; namespace UniGLTF { @@ -10,13 +11,13 @@ public static float[] ToFloat4(this Color src, ColorSpace srcColorSpace, ColorSp var dst = src.ConvertColorSpace(srcColorSpace, dstColorSpace); return new float[] {dst.r, dst.g, dst.b, dst.a}; } - + public static float[] ToFloat3(this Color src, ColorSpace srcColorSpace, ColorSpace dstColorSpace) { var dst = src.ConvertColorSpace(srcColorSpace, dstColorSpace); return new float[] {dst.r, dst.g, dst.b}; } - + public static Color ToColor4(this float[] src, ColorSpace srcColorSpace, ColorSpace dstColorSpace) { if (src == null || src.Length < 4) @@ -27,7 +28,7 @@ public static Color ToColor4(this float[] src, ColorSpace srcColorSpace, ColorSp return new Color(src[0], src[1], src[2], src[3]).ConvertColorSpace(srcColorSpace, dstColorSpace); } - + public static Color ToColor3(this float[] src, ColorSpace srcColorSpace, ColorSpace dstColorSpace) { if (src == null || src.Length < 3) @@ -64,4 +65,4 @@ private static Color ConvertColorSpace(this Color srcColor, ColorSpace srcColorS } } } -} \ No newline at end of file +} diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs index 4bd9b196e2..34683b75b1 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs @@ -37,14 +37,19 @@ public IAnimationImporter AnimationImporter public TextureFactory TextureFactory { get; } public MaterialFactory MaterialFactory { get; } - public ImporterContext(GltfParser parser, IReadOnlyDictionary externalObjectMap = null) + public ImporterContext( + GltfParser parser, + IReadOnlyDictionary externalObjectMap = null, + ITextureDeserializer textureDeserializer = null) { Parser = parser; TextureDescriptorGenerator = new GltfTextureDescriptorGenerator(Parser); MaterialDescriptorGenerator = new GltfMaterialDescriptorGenerator(); externalObjectMap = externalObjectMap ?? new Dictionary(); - TextureFactory = new TextureFactory(externalObjectMap + textureDeserializer = textureDeserializer ?? new UnityTextureDeserializer(); + + TextureFactory = new TextureFactory(textureDeserializer, externalObjectMap .Where(x => x.Value is Texture) .ToDictionary(x => x.Key, x => (Texture) x.Value)); MaterialFactory = new MaterialFactory(externalObjectMap diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfPbrMaterialImporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfPbrMaterialImporter.cs index b3659431e6..6d28cae78a 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfPbrMaterialImporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfPbrMaterialImporter.cs @@ -1,5 +1,6 @@ using UnityEngine; using VRMShaders; +using ColorSpace = VRMShaders.ColorSpace; namespace UniGLTF { diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfUnlitMaterialImporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfUnlitMaterialImporter.cs index 0322c9f595..18579b39da 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfUnlitMaterialImporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfUnlitMaterialImporter.cs @@ -1,6 +1,6 @@ using UnityEngine; using VRMShaders; - +using ColorSpace = VRMShaders.ColorSpace; namespace UniGLTF { diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/MaterialExporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/MaterialExporter.cs index b8c6b32161..fdb43e9a61 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/MaterialExporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/MaterialExporter.cs @@ -2,7 +2,7 @@ using UniGLTF.UniUnlit; using UnityEngine; using VRMShaders; - +using ColorSpace = VRMShaders.ColorSpace; namespace UniGLTF { diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureExporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureExporter.cs index 3f3cbc6377..165ebe058c 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureExporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureExporter.cs @@ -1,6 +1,7 @@ using System; using UnityEngine; using VRMShaders; +using ColorSpace = VRMShaders.ColorSpace; namespace UniGLTF { diff --git a/Assets/UniGLTF/Runtime/UniGLTF/Resources/Shaders/NormalMapEncoder.shader b/Assets/UniGLTF/Runtime/UniGLTF/Resources/Shaders/NormalMapEncoder.shader deleted file mode 100644 index 8c77bb20b8..0000000000 --- a/Assets/UniGLTF/Runtime/UniGLTF/Resources/Shaders/NormalMapEncoder.shader +++ /dev/null @@ -1,68 +0,0 @@ -Shader "UniGLTF/NormalMapEncoder" -{ - Properties - { - _MainTex("Texture", 2D) = "white" {} - } - SubShader - { - // No culling or depth - Cull Off ZWrite Off ZTest Always - - Pass - { - CGPROGRAM - #pragma vertex vert - #pragma fragment frag - - #include "UnityCG.cginc" - - struct appdata - { - float4 vertex : POSITION; - float2 uv : TEXCOORD0; - }; - - struct v2f - { - float2 uv : TEXCOORD0; - float4 vertex : SV_POSITION; - }; - - v2f vert(appdata v) - { - v2f o; - o.vertex = UnityObjectToClipPos(v.vertex); - o.uv = v.uv; - return o; - } - - sampler2D _MainTex; - - fixed4 frag(v2f i) : SV_Target - { - half4 col = tex2D(_MainTex, i.uv); - - half4 packedNormal; - packedNormal.x = col.x; - packedNormal.y = col.y; - packedNormal.z = col.z; - packedNormal.w = 1.0; - - // normalize as normal vector - float3 normal; - normal.xy = packedNormal.xy * 2 - 1; - if (dot(normal.xy, normal.xy) > 1) - { - normal.xy = normalize(normal.xy); - } - normal.z = sqrt(1 - saturate(dot(normal.xy, normal.xy))); - - packedNormal.xyz = normal.xyz * 0.5 + 0.5; - return packedNormal; - } - ENDCG - } - } -} - diff --git a/Assets/UniGLTF/Runtime/UniGLTF/Resources/Shaders/NormalMapEncoder.shader.meta b/Assets/UniGLTF/Runtime/UniGLTF/Resources/Shaders/NormalMapEncoder.shader.meta deleted file mode 100644 index d0ef10022d..0000000000 --- a/Assets/UniGLTF/Runtime/UniGLTF/Resources/Shaders/NormalMapEncoder.shader.meta +++ /dev/null @@ -1,9 +0,0 @@ -fileFormatVersion: 2 -guid: 3e39586253f31b34f87fa7e133449b1e -ShaderImporter: - externalObjects: {} - defaultTextures: [] - nonModifiableTextures: [] - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/VRM/Runtime/IO/VRMExporter.cs b/Assets/VRM/Runtime/IO/VRMExporter.cs index 2b1e05359c..c04c50bd52 100644 --- a/Assets/VRM/Runtime/IO/VRMExporter.cs +++ b/Assets/VRM/Runtime/IO/VRMExporter.cs @@ -3,8 +3,8 @@ using UniGLTF; using UniJSON; using UnityEngine; -using ColorSpace = UniGLTF.ColorSpace; - +using VRMShaders; +using ColorSpace = VRMShaders.ColorSpace; namespace VRM { diff --git a/Assets/VRM/Runtime/IO/VRMImporterContext.cs b/Assets/VRM/Runtime/IO/VRMImporterContext.cs index bab16713b5..f2fcca636e 100644 --- a/Assets/VRM/Runtime/IO/VRMImporterContext.cs +++ b/Assets/VRM/Runtime/IO/VRMImporterContext.cs @@ -20,8 +20,11 @@ public NotVrm0Exception() public VRM.glTF_VRM_extensions VRM { get; private set; } - public VRMImporterContext(GltfParser parser, IReadOnlyDictionary externalObjectMap = null) - : base(parser, externalObjectMap) + public VRMImporterContext( + GltfParser parser, + IReadOnlyDictionary externalObjectMap = null, + ITextureDeserializer textureDeserializer = null) + : base(parser, externalObjectMap, textureDeserializer) { // parse VRM part if (glTF_VRM_extensions.TryDeserialize(GLTF.extensions, out glTF_VRM_extensions vrm)) diff --git a/Assets/VRM/Runtime/IO/VRMMaterialExporter.cs b/Assets/VRM/Runtime/IO/VRMMaterialExporter.cs index 75a52b895c..16dce2440b 100644 --- a/Assets/VRM/Runtime/IO/VRMMaterialExporter.cs +++ b/Assets/VRM/Runtime/IO/VRMMaterialExporter.cs @@ -5,7 +5,7 @@ using UniGLTF.ShaderPropExporter; using UnityEngine; using VRMShaders; -using ColorSpace = UniGLTF.ColorSpace; +using ColorSpace = VRMShaders.ColorSpace; namespace VRM { diff --git a/Assets/VRM10/Runtime/IO/Material/Vrm10MToonMaterialExporter.cs b/Assets/VRM10/Runtime/IO/Material/Vrm10MToonMaterialExporter.cs index 5c1d8e5f75..f85090c39d 100644 --- a/Assets/VRM10/Runtime/IO/Material/Vrm10MToonMaterialExporter.cs +++ b/Assets/VRM10/Runtime/IO/Material/Vrm10MToonMaterialExporter.cs @@ -4,7 +4,7 @@ using UniGLTF.Extensions.VRMC_materials_mtoon; using UnityEngine; using VRMShaders; -using ColorSpace = UniGLTF.ColorSpace; +using ColorSpace = VRMShaders.ColorSpace; using OutlineWidthMode = MToon.OutlineWidthMode; using RenderMode = MToon.RenderMode; diff --git a/Assets/VRM10/Runtime/IO/Material/Vrm10MToonMaterialImporter.cs b/Assets/VRM10/Runtime/IO/Material/Vrm10MToonMaterialImporter.cs index d7e2273b17..762f45b05e 100644 --- a/Assets/VRM10/Runtime/IO/Material/Vrm10MToonMaterialImporter.cs +++ b/Assets/VRM10/Runtime/IO/Material/Vrm10MToonMaterialImporter.cs @@ -4,7 +4,7 @@ using UniGLTF.Extensions.VRMC_materials_mtoon; using UnityEngine; using VRMShaders; -using ColorSpace = UniGLTF.ColorSpace; +using ColorSpace = VRMShaders.ColorSpace; using OutlineWidthMode = UniGLTF.Extensions.VRMC_materials_mtoon.OutlineWidthMode; namespace UniVRM10 diff --git a/Assets/VRM10/Runtime/IO/Vrm10Importer.cs b/Assets/VRM10/Runtime/IO/Vrm10Importer.cs index 34cc7e62a3..67cfbb8212 100644 --- a/Assets/VRM10/Runtime/IO/Vrm10Importer.cs +++ b/Assets/VRM10/Runtime/IO/Vrm10Importer.cs @@ -21,8 +21,11 @@ public class Vrm10Importer : UniGLTF.ImporterContext IReadOnlyDictionary m_externalMap; - public Vrm10Importer(UniGLTF.GltfParser parser, IReadOnlyDictionary externalObjectMap = null) - : base(parser, externalObjectMap) + public Vrm10Importer( + UniGLTF.GltfParser parser, + IReadOnlyDictionary externalObjectMap = null, + ITextureDeserializer textureDeserializer = null) + : base(parser, externalObjectMap, textureDeserializer) { TextureDescriptorGenerator = new Vrm10TextureDescriptorGenerator(parser); MaterialDescriptorGenerator = new Vrm10MaterialDescriptorGenerator(); diff --git a/Assets/VRM10/Runtime/Migration/MigrationMToon.cs b/Assets/VRM10/Runtime/Migration/MigrationMToon.cs index aefa2f3806..206a817997 100644 --- a/Assets/VRM10/Runtime/Migration/MigrationMToon.cs +++ b/Assets/VRM10/Runtime/Migration/MigrationMToon.cs @@ -5,7 +5,7 @@ using UniGLTF.Extensions.VRMC_materials_mtoon; using UniJSON; using UnityEngine; -using ColorSpace = UniGLTF.ColorSpace; +using ColorSpace = VRMShaders.ColorSpace; using RenderMode = MToon.RenderMode; namespace UniVRM10 diff --git a/Assets/VRMShaders/GLTF/IO/Editor/EditorTextureSerializer.cs b/Assets/VRMShaders/GLTF/IO/Editor/EditorTextureSerializer.cs index 22386ca1f9..3def724ccb 100644 --- a/Assets/VRMShaders/GLTF/IO/Editor/EditorTextureSerializer.cs +++ b/Assets/VRMShaders/GLTF/IO/Editor/EditorTextureSerializer.cs @@ -1,10 +1,8 @@ using System; using System.IO; using System.Reflection; -using UniGLTF; using UnityEditor; using UnityEngine; -using ColorSpace = UniGLTF.ColorSpace; namespace VRMShaders { diff --git a/Assets/VRMShaders/GLTF/IO/Runtime/ColorSpace.cs b/Assets/VRMShaders/GLTF/IO/Runtime/ColorSpace.cs index 68d124b6e2..26332a1547 100644 --- a/Assets/VRMShaders/GLTF/IO/Runtime/ColorSpace.cs +++ b/Assets/VRMShaders/GLTF/IO/Runtime/ColorSpace.cs @@ -1,8 +1,8 @@ -namespace UniGLTF +namespace VRMShaders { public enum ColorSpace { sRGB, Linear, } -} \ No newline at end of file +} diff --git a/Assets/VRMShaders/GLTF/IO/Runtime/ITextureDeserializer.cs b/Assets/VRMShaders/GLTF/IO/Runtime/ITextureDeserializer.cs new file mode 100644 index 0000000000..7551898b95 --- /dev/null +++ b/Assets/VRMShaders/GLTF/IO/Runtime/ITextureDeserializer.cs @@ -0,0 +1,21 @@ +using System; +using System.Threading.Tasks; +using UnityEngine; + +namespace VRMShaders +{ + /// + /// 単純に Texture2D アセットを生成する機能 + /// + public interface ITextureDeserializer + { + /// + /// imageData をもとに Texture2D を生成する + /// + /// データ + /// Texture2D の mipmap が生成されるべきか否か + /// Texture2D の色空間 + /// + Task LoadTextureAsync(byte[] imageData, bool useMipmap, ColorSpace colorSpace); + } +} diff --git a/Assets/VRMShaders/GLTF/IO/Runtime/ITextureDeserializer.cs.meta b/Assets/VRMShaders/GLTF/IO/Runtime/ITextureDeserializer.cs.meta new file mode 100644 index 0000000000..2522b75ace --- /dev/null +++ b/Assets/VRMShaders/GLTF/IO/Runtime/ITextureDeserializer.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 47b889c85e814e7eae6f5b7f760c7ef3 +timeCreated: 1622107590 \ No newline at end of file diff --git a/Assets/VRMShaders/GLTF/IO/Runtime/ITextureExporter.cs b/Assets/VRMShaders/GLTF/IO/Runtime/ITextureExporter.cs index c0931199bc..0e8f302384 100644 --- a/Assets/VRMShaders/GLTF/IO/Runtime/ITextureExporter.cs +++ b/Assets/VRMShaders/GLTF/IO/Runtime/ITextureExporter.cs @@ -13,7 +13,7 @@ public interface ITextureExporter /// /// Export する Texture2D のリスト。これが gltf.textures になる /// - IReadOnlyList<(Texture2D, UniGLTF.ColorSpace)> Exported { get; } + IReadOnlyList<(Texture2D, ColorSpace)> Exported { get; } /// /// 指定の Texture を、 sRGB 色空間の値を持つ Texture に出力するように指示する。 diff --git a/Assets/VRMShaders/GLTF/IO/Runtime/ITextureSerializer.cs b/Assets/VRMShaders/GLTF/IO/Runtime/ITextureSerializer.cs index de5e775549..8da1bf82b3 100644 --- a/Assets/VRMShaders/GLTF/IO/Runtime/ITextureSerializer.cs +++ b/Assets/VRMShaders/GLTF/IO/Runtime/ITextureSerializer.cs @@ -1,6 +1,6 @@ using UnityEngine; -namespace UniGLTF +namespace VRMShaders { /// /// Texture2D を入力として byte[] を得る機能 diff --git a/Assets/VRMShaders/GLTF/IO/Runtime/NormalConverter.cs b/Assets/VRMShaders/GLTF/IO/Runtime/NormalConverter.cs index 99b8f32815..d87b40ff4b 100644 --- a/Assets/VRMShaders/GLTF/IO/Runtime/NormalConverter.cs +++ b/Assets/VRMShaders/GLTF/IO/Runtime/NormalConverter.cs @@ -1,6 +1,4 @@ using UnityEngine; -using ColorSpace = UniGLTF.ColorSpace; - namespace VRMShaders { @@ -19,28 +17,7 @@ private static Material Decoder } } - private static Material m_encoder; - private static Material Encoder - { - get - { - if (m_encoder == null) - { - m_encoder = new Material(Shader.Find("UniGLTF/NormalMapEncoder")); - } - return m_encoder; - } - } - - // GLTF data to Unity texture - // ConvertToNormalValueFromRawColorWhenCompressionIsRequired - public static Texture2D Import(Texture2D texture) - { - return TextureConverter.CopyTexture(texture, ColorSpace.Linear, false, Encoder); - } - // Unity texture to GLTF data - // ConvertToRawColorWhenNormalValueIsCompressed public static Texture2D Export(Texture texture) { return TextureConverter.CopyTexture(texture, ColorSpace.Linear, false, Decoder); diff --git a/Assets/VRMShaders/GLTF/IO/Runtime/OcclusionMetallicRoughnessConverter.cs b/Assets/VRMShaders/GLTF/IO/Runtime/OcclusionMetallicRoughnessConverter.cs index 83eaf32ecd..3d4635081c 100644 --- a/Assets/VRMShaders/GLTF/IO/Runtime/OcclusionMetallicRoughnessConverter.cs +++ b/Assets/VRMShaders/GLTF/IO/Runtime/OcclusionMetallicRoughnessConverter.cs @@ -1,8 +1,6 @@ using System; using System.Linq; using UnityEngine; -using ColorSpace = UniGLTF.ColorSpace; - namespace VRMShaders { diff --git a/Assets/VRMShaders/GLTF/IO/Runtime/RuntimeTextureSerializer.cs b/Assets/VRMShaders/GLTF/IO/Runtime/RuntimeTextureSerializer.cs index 526e953682..9bbd12e3f0 100644 --- a/Assets/VRMShaders/GLTF/IO/Runtime/RuntimeTextureSerializer.cs +++ b/Assets/VRMShaders/GLTF/IO/Runtime/RuntimeTextureSerializer.cs @@ -2,7 +2,7 @@ using UnityEngine; using VRMShaders; -namespace UniGLTF +namespace VRMShaders { public sealed class RuntimeTextureSerializer : ITextureSerializer { diff --git a/Assets/VRMShaders/GLTF/IO/Runtime/TextureConverter.cs b/Assets/VRMShaders/GLTF/IO/Runtime/TextureConverter.cs index d9d19b82f2..e53fdd1599 100644 --- a/Assets/VRMShaders/GLTF/IO/Runtime/TextureConverter.cs +++ b/Assets/VRMShaders/GLTF/IO/Runtime/TextureConverter.cs @@ -1,8 +1,6 @@ using System; using System.Linq; using UnityEngine; -using ColorSpace = UniGLTF.ColorSpace; - namespace VRMShaders { diff --git a/Assets/VRMShaders/GLTF/IO/Runtime/TextureExporter.cs b/Assets/VRMShaders/GLTF/IO/Runtime/TextureExporter.cs index 4c46755d86..c4bdb9b2e7 100644 --- a/Assets/VRMShaders/GLTF/IO/Runtime/TextureExporter.cs +++ b/Assets/VRMShaders/GLTF/IO/Runtime/TextureExporter.cs @@ -1,9 +1,6 @@ using System; using System.Collections.Generic; -using UniGLTF; using UnityEngine; -using ColorSpace = UniGLTF.ColorSpace; - namespace VRMShaders { diff --git a/Assets/VRMShaders/GLTF/IO/Runtime/TextureFactory.cs b/Assets/VRMShaders/GLTF/IO/Runtime/TextureFactory.cs index ac1258a008..d3bc48f0c6 100644 --- a/Assets/VRMShaders/GLTF/IO/Runtime/TextureFactory.cs +++ b/Assets/VRMShaders/GLTF/IO/Runtime/TextureFactory.cs @@ -3,40 +3,40 @@ using UnityEngine; using System.Linq; using System.Threading.Tasks; -using ColorSpace = UniGLTF.ColorSpace; - namespace VRMShaders { public class TextureFactory : IDisposable { - private readonly Dictionary m_temporaryTextures = new Dictionary(); - private readonly Dictionary m_textureCache = new Dictionary(); - private readonly IReadOnlyDictionary m_externalMap; + private readonly ITextureDeserializer _textureDeserializer; + private readonly IReadOnlyDictionary _externalMap; + private readonly Dictionary _temporaryTextures = new Dictionary(); + private readonly Dictionary _textureCache = new Dictionary(); /// /// Importer が動的に生成した Texture /// - public IReadOnlyDictionary ConvertedTextures => m_textureCache; + public IReadOnlyDictionary ConvertedTextures => _textureCache; /// - /// + /// 外部から渡された、すでに存在する Texture (ex. Extracted Editor Asset) /// - public IReadOnlyDictionary ExternalTextures => m_externalMap; + public IReadOnlyDictionary ExternalTextures => _externalMap; - public TextureFactory(IReadOnlyDictionary externalTextures) + public TextureFactory(ITextureDeserializer textureDeserializer, IReadOnlyDictionary externalTextures) { - m_externalMap = externalTextures; + _textureDeserializer = textureDeserializer; + _externalMap = externalTextures; } public void Dispose() { - foreach (var kv in m_temporaryTextures) + foreach (var kv in _temporaryTextures) { DestroyResource(kv.Value); } - m_temporaryTextures.Clear(); - m_textureCache.Clear(); + _temporaryTextures.Clear(); + _textureCache.Clear(); } /// @@ -46,7 +46,7 @@ public void Dispose() public void TransferOwnership(Func take) { var transferredAssets = new HashSet(); - foreach (var x in m_textureCache) + foreach (var x in _textureCache) { if (take(x.Value)) { @@ -56,41 +56,24 @@ public void TransferOwnership(Func take) foreach (var key in transferredAssets) { - m_textureCache.Remove(key); + _textureCache.Remove(key); } } - async Task LoadTextureAsync(GetTextureBytesAsync getTextureBytesAsync, bool useMipmap, ColorSpace colorSpace) - { - var imageBytes = await getTextureBytesAsync(); - - var texture = new Texture2D(2, 2, TextureFormat.ARGB32, useMipmap, colorSpace == ColorSpace.Linear); - if (imageBytes != null) - { - texture.LoadImage(imageBytes); - } - - return texture; - } - /// - /// テクスチャーをロード、必要であれば変換して返す。 - /// 同じものはキャッシュを返す + /// テクスチャ生成情報を基に、テクスチャ生成を行う。 + /// SubAssetKey が同じ場合はキャッシュを返す。 /// - /// 変換の有無を判断する: METALLIC_GLOSS_PROP - /// METALLIC_GLOSS_PROPの追加パラメーター - /// gltf の texture index - /// public async Task GetTextureAsync(TextureDescriptor texDesc) { var subAssetKey = texDesc.SubAssetKey; - if (m_externalMap != null && m_externalMap.TryGetValue(subAssetKey, out var externalTexture)) + if (_externalMap != null && _externalMap.TryGetValue(subAssetKey, out var externalTexture)) { return externalTexture; } - if (m_textureCache.TryGetValue(subAssetKey, out var cachedTexture)) + if (_textureCache.TryGetValue(subAssetKey, out var cachedTexture)) { return cachedTexture; } @@ -99,14 +82,16 @@ public async Task GetTextureAsync(TextureDescriptor texDesc) { case TextureImportTypes.NormalMap: { - // Runtime/SubAsset 用に変換する - var rawTexture = await LoadTextureAsync(texDesc.Index0, texDesc.Sampler.EnableMipMap, ColorSpace.Linear); - var convertedTexture = NormalConverter.Import(rawTexture); - convertedTexture.name = subAssetKey.Name; - convertedTexture.SetSampler(texDesc.Sampler); - m_textureCache.Add(subAssetKey, convertedTexture); - DestroyResource(rawTexture); - return convertedTexture; + // no conversion. Unity's normal map is same with glTF's. + // + // > contrary to Unity’s usual convention of using Y as “up” + // https://docs.unity3d.com/2018.4/Documentation/Manual/StandardShaderMaterialParameterNormalMap.html + var data0 = await texDesc.Index0(); + var rawTexture = await _textureDeserializer.LoadTextureAsync(data0, texDesc.Sampler.EnableMipMap, ColorSpace.Linear); + rawTexture.name = subAssetKey.Name; + rawTexture.SetSampler(texDesc.Sampler); + _textureCache.Add(subAssetKey, rawTexture); + return rawTexture; } case TextureImportTypes.StandardMap: @@ -116,18 +101,20 @@ public async Task GetTextureAsync(TextureDescriptor texDesc) if (texDesc.Index0 != null) { - metallicRoughnessTexture = await LoadTextureAsync(texDesc.Index0, texDesc.Sampler.EnableMipMap, ColorSpace.Linear); + var data0 = await texDesc.Index0(); + metallicRoughnessTexture = await _textureDeserializer.LoadTextureAsync(data0, texDesc.Sampler.EnableMipMap, ColorSpace.Linear); } if (texDesc.Index1 != null) { - occlusionTexture = await LoadTextureAsync(texDesc.Index1, texDesc.Sampler.EnableMipMap, ColorSpace.Linear); + var data1 = await texDesc.Index1(); + occlusionTexture = await _textureDeserializer.LoadTextureAsync(data1, texDesc.Sampler.EnableMipMap, ColorSpace.Linear); } var combinedTexture = OcclusionMetallicRoughnessConverter.Import(metallicRoughnessTexture, texDesc.MetallicFactor, texDesc.RoughnessFactor, occlusionTexture); combinedTexture.name = subAssetKey.Name; combinedTexture.SetSampler(texDesc.Sampler); - m_textureCache.Add(subAssetKey, combinedTexture); + _textureCache.Add(subAssetKey, combinedTexture); DestroyResource(metallicRoughnessTexture); DestroyResource(occlusionTexture); return combinedTexture; @@ -135,18 +122,20 @@ public async Task GetTextureAsync(TextureDescriptor texDesc) case TextureImportTypes.sRGB: { - var rawTexture = await LoadTextureAsync(texDesc.Index0, texDesc.Sampler.EnableMipMap, ColorSpace.sRGB); + var data0 = await texDesc.Index0(); + var rawTexture = await _textureDeserializer.LoadTextureAsync(data0, texDesc.Sampler.EnableMipMap, ColorSpace.sRGB); rawTexture.name = subAssetKey.Name; rawTexture.SetSampler(texDesc.Sampler); - m_textureCache.Add(subAssetKey, rawTexture); + _textureCache.Add(subAssetKey, rawTexture); return rawTexture; } case TextureImportTypes.Linear: { - var rawTexture = await LoadTextureAsync(texDesc.Index0, texDesc.Sampler.EnableMipMap, ColorSpace.Linear); + var data0 = await texDesc.Index0(); + var rawTexture = await _textureDeserializer.LoadTextureAsync(data0, texDesc.Sampler.EnableMipMap, ColorSpace.Linear); rawTexture.name = subAssetKey.Name; rawTexture.SetSampler(texDesc.Sampler); - m_textureCache.Add(subAssetKey, rawTexture); + _textureCache.Add(subAssetKey, rawTexture); return rawTexture; } default: diff --git a/Assets/VRMShaders/GLTF/IO/Runtime/TextureImportTypes.cs b/Assets/VRMShaders/GLTF/IO/Runtime/TextureImportTypes.cs index d4d1d3c3ef..214eb3f2ed 100644 --- a/Assets/VRMShaders/GLTF/IO/Runtime/TextureImportTypes.cs +++ b/Assets/VRMShaders/GLTF/IO/Runtime/TextureImportTypes.cs @@ -1,6 +1,5 @@ using System; using UnityEngine; -using ColorSpace = UniGLTF.ColorSpace; namespace VRMShaders { diff --git a/Assets/VRMShaders/GLTF/IO/Runtime/UnityTextureDeserializer.cs b/Assets/VRMShaders/GLTF/IO/Runtime/UnityTextureDeserializer.cs new file mode 100644 index 0000000000..ad84c9bc5d --- /dev/null +++ b/Assets/VRMShaders/GLTF/IO/Runtime/UnityTextureDeserializer.cs @@ -0,0 +1,23 @@ +using System; +using System.Threading.Tasks; +using UnityEngine; + +namespace VRMShaders +{ + /// + /// Unity の ImageConversion.LoadImage を用いて PNG/JPG の読み込みを実現する + /// + public sealed class UnityTextureDeserializer : ITextureDeserializer + { + public async Task LoadTextureAsync(byte[] imageData, bool useMipmap, ColorSpace colorSpace) + { + var texture = new Texture2D(2, 2, TextureFormat.ARGB32, useMipmap, colorSpace == ColorSpace.Linear); + if (imageData != null) + { + texture.LoadImage(imageData); + } + + return texture; + } + } +} diff --git a/Assets/VRMShaders/GLTF/IO/Runtime/UnityTextureDeserializer.cs.meta b/Assets/VRMShaders/GLTF/IO/Runtime/UnityTextureDeserializer.cs.meta new file mode 100644 index 0000000000..d2b98c8a0e --- /dev/null +++ b/Assets/VRMShaders/GLTF/IO/Runtime/UnityTextureDeserializer.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 170beedd62e24506acc42b7c04464e84 +timeCreated: 1622107784 \ No newline at end of file diff --git a/Assets/VRMShaders/GLTF/IO/Tests/CopyTextureTests.cs b/Assets/VRMShaders/GLTF/IO/Tests/CopyTextureTests.cs index 7d7fa051d0..daeeeec00c 100644 --- a/Assets/VRMShaders/GLTF/IO/Tests/CopyTextureTests.cs +++ b/Assets/VRMShaders/GLTF/IO/Tests/CopyTextureTests.cs @@ -2,7 +2,6 @@ using NUnit.Framework; using UnityEditor; using UnityEngine; -using ColorSpace = UniGLTF.ColorSpace; namespace VRMShaders { diff --git a/Assets/VRMShaders/GLTF/IO/Tests/TextureBytesTests.cs b/Assets/VRMShaders/GLTF/IO/Tests/TextureBytesTests.cs index 6f51639859..8ce7bc3ea0 100644 --- a/Assets/VRMShaders/GLTF/IO/Tests/TextureBytesTests.cs +++ b/Assets/VRMShaders/GLTF/IO/Tests/TextureBytesTests.cs @@ -1,7 +1,6 @@ using NUnit.Framework; using UnityEditor; using UnityEngine; -using ColorSpace = UniGLTF.ColorSpace; namespace VRMShaders {