From df45b69521f0bcfea01f05623a80c354db40a3be Mon Sep 17 00:00:00 2001 From: ousttrue Date: Mon, 24 May 2021 14:36:27 +0900 Subject: [PATCH] =?UTF-8?q?TextureSamplerUtil=20=E3=81=AE=E6=95=B4?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../IO/TextureIO/GltfTextureImporter.cs | 75 ++----------------- .../IO/TextureIO/TextureSamplerUtil.cs | 70 ++++++++++++++++- .../GLTF/IO/Runtime/SamplerParam.cs | 3 + 3 files changed, 75 insertions(+), 73 deletions(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureImporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureImporter.cs index e2b7fbb90..d39aed972 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureImporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureImporter.cs @@ -1,6 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; using System.Threading.Tasks; using UnityEngine; using VRMShaders; @@ -36,7 +34,7 @@ namespace UniGLTF var gltfTexture = parser.GLTF.textures[textureIndex]; var gltfImage = parser.GLTF.images[gltfTexture.source]; var name = TextureImportName.GetUnityObjectName(TextureImportTypes.sRGB, gltfTexture.name, gltfImage.uri); - var sampler = CreateSampler(parser.GLTF, textureIndex); + var sampler = TextureSamplerUtil.CreateSampler(parser.GLTF, textureIndex); GetTextureBytesAsync getTextureBytesAsync = () => Task.FromResult(ToArray(parser.GLTF.GetImageBytesFromTextureIndex(parser.Storage, textureIndex))); var key = new SubAssetKey(typeof(Texture2D), name); var param = new TextureImportParam(name, gltfImage.GetExt(), gltfImage.uri, offset, scale, sampler, TextureImportTypes.sRGB, default, default, getTextureBytesAsync, default, default, default, default, default); @@ -48,7 +46,7 @@ namespace UniGLTF var gltfTexture = parser.GLTF.textures[textureIndex]; var gltfImage = parser.GLTF.images[gltfTexture.source]; var name = TextureImportName.GetUnityObjectName(TextureImportTypes.Linear, gltfTexture.name, gltfImage.uri); - var sampler = CreateSampler(parser.GLTF, textureIndex); + var sampler = TextureSamplerUtil.CreateSampler(parser.GLTF, textureIndex); GetTextureBytesAsync getTextureBytesAsync = () => Task.FromResult(ToArray(parser.GLTF.GetImageBytesFromTextureIndex(parser.Storage, textureIndex))); var key = new SubAssetKey(typeof(Texture2D), name); var param = new TextureImportParam(name, gltfImage.GetExt(), gltfImage.uri, offset, scale, sampler, TextureImportTypes.Linear, default, default, getTextureBytesAsync, default, default, default, default, default); @@ -60,7 +58,7 @@ namespace UniGLTF var gltfTexture = parser.GLTF.textures[textureIndex]; var gltfImage = parser.GLTF.images[gltfTexture.source]; var name = TextureImportName.GetUnityObjectName(TextureImportTypes.NormalMap, gltfTexture.name, gltfImage.uri); - var sampler = CreateSampler(parser.GLTF, textureIndex); + var sampler = TextureSamplerUtil.CreateSampler(parser.GLTF, textureIndex); GetTextureBytesAsync getTextureBytesAsync = () => Task.FromResult(ToArray(parser.GLTF.GetImageBytesFromTextureIndex(parser.Storage, textureIndex))); var key = new SubAssetKey(typeof(Texture2D), name); var param = new TextureImportParam(name, gltfImage.GetExt(), gltfImage.uri, offset, scale, sampler, TextureImportTypes.NormalMap, default, default, getTextureBytesAsync, default, default, default, default, default); @@ -77,7 +75,7 @@ namespace UniGLTF { var gltfTexture = parser.GLTF.textures[metallicRoughnessTextureIndex.Value]; name = TextureImportName.GetUnityObjectName(TextureImportTypes.StandardMap, gltfTexture.name, parser.GLTF.images[gltfTexture.source].uri); - sampler = CreateSampler(parser.GLTF, metallicRoughnessTextureIndex.Value); + sampler = TextureSamplerUtil.CreateSampler(parser.GLTF, metallicRoughnessTextureIndex.Value); getMetallicRoughnessAsync = () => Task.FromResult(ToArray(parser.GLTF.GetImageBytesFromTextureIndex(parser.Storage, metallicRoughnessTextureIndex.Value))); } @@ -89,72 +87,11 @@ namespace UniGLTF { name = TextureImportName.GetUnityObjectName(TextureImportTypes.StandardMap, gltfTexture.name, parser.GLTF.images[gltfTexture.source].uri); } - sampler = CreateSampler(parser.GLTF, occlusionTextureIndex.Value); + sampler = TextureSamplerUtil.CreateSampler(parser.GLTF, occlusionTextureIndex.Value); getOcclusionAsync = () => Task.FromResult(ToArray(parser.GLTF.GetImageBytesFromTextureIndex(parser.Storage, occlusionTextureIndex.Value))); } return new TextureImportParam(name, ".png", null, offset, scale, sampler, TextureImportTypes.StandardMap, metallicFactor, roughnessFactor, getMetallicRoughnessAsync, getOcclusionAsync, default, default, default, default); } - - public static SamplerParam CreateSampler(glTF gltf, int index) - { - var gltfTexture = gltf.textures[index]; - if (gltfTexture.sampler < 0 || gltfTexture.sampler >= gltf.samplers.Count) - { - // default - return SamplerParam.Default; - } - - var gltfSampler = gltf.samplers[gltfTexture.sampler]; - return new SamplerParam - { - WrapModesU = GetUnityWrapMode(gltfSampler.wrapS), - WrapModesV = GetUnityWrapMode(gltfSampler.wrapT), - FilterMode = ImportFilterMode(gltfSampler.minFilter), - }; - } - - public static TextureWrapMode GetUnityWrapMode(glWrap wrap) - { - switch (wrap) - { - case glWrap.NONE: // default - return TextureWrapMode.Repeat; - - case glWrap.CLAMP_TO_EDGE: - return TextureWrapMode.Clamp; - - case glWrap.REPEAT: - return TextureWrapMode.Repeat; - - case glWrap.MIRRORED_REPEAT: - return TextureWrapMode.Mirror; - - 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(); - } - } } -} \ No newline at end of file +} diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureSamplerUtil.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureSamplerUtil.cs index 8c6b1de09..25a2bd7ee 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureSamplerUtil.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureSamplerUtil.cs @@ -1,13 +1,12 @@ using System; -using System.Collections.Generic; using UnityEngine; - +using VRMShaders; namespace UniGLTF { public static class TextureSamplerUtil { - #region Export + #region FilterMode // MagFilter は 2種類だけ public static glFilter ExportMagFilter(Texture texture) { @@ -43,6 +42,30 @@ namespace UniGLTF } } + 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(); + } + } + #endregion + + #region WrapMode public static glWrap ExportWrapMode(TextureWrapMode wrapMode) { switch (wrapMode) @@ -61,6 +84,28 @@ namespace UniGLTF } } + public static TextureWrapMode ImportWrapMode(glWrap wrap) + { + switch (wrap) + { + case glWrap.NONE: // default + return TextureWrapMode.Repeat; + + case glWrap.CLAMP_TO_EDGE: + return TextureWrapMode.Clamp; + + case glWrap.REPEAT: + return TextureWrapMode.Repeat; + + case glWrap.MIRRORED_REPEAT: + return TextureWrapMode.Mirror; + + default: + throw new NotImplementedException(); + } + } + #endregion + public static glTFTextureSampler Export(Texture texture) { var magFilter = ExportMagFilter(texture); @@ -75,6 +120,23 @@ namespace UniGLTF wrapT = wrapT, }; } - #endregion + + public static SamplerParam CreateSampler(glTF gltf, int index) + { + var gltfTexture = gltf.textures[index]; + if (gltfTexture.sampler < 0 || gltfTexture.sampler >= gltf.samplers.Count) + { + // default + return SamplerParam.Default; + } + + var gltfSampler = gltf.samplers[gltfTexture.sampler]; + return new SamplerParam + { + WrapModesU = ImportWrapMode(gltfSampler.wrapS), + WrapModesV = ImportWrapMode(gltfSampler.wrapT), + FilterMode = ImportFilterMode(gltfSampler.minFilter), + }; + } } } diff --git a/Assets/VRMShaders/GLTF/IO/Runtime/SamplerParam.cs b/Assets/VRMShaders/GLTF/IO/Runtime/SamplerParam.cs index 74ab2e418..a44aa7e2a 100644 --- a/Assets/VRMShaders/GLTF/IO/Runtime/SamplerParam.cs +++ b/Assets/VRMShaders/GLTF/IO/Runtime/SamplerParam.cs @@ -10,12 +10,15 @@ namespace VRMShaders public TextureWrapMode WrapModesV; public FilterMode FilterMode; + + public bool EnableMipMap; public static SamplerParam Default => new SamplerParam { FilterMode = FilterMode.Bilinear, WrapModesU = TextureWrapMode.Repeat, WrapModesV = TextureWrapMode.Repeat, + EnableMipMap = false, }; }