From 95cb9da77e9e042da849b7c77508893b99ce4f82 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Fri, 21 May 2021 13:08:09 +0900 Subject: [PATCH] =?UTF-8?q?SamplerParam.WrapModes=20=E3=82=92=20WrapModesU?= =?UTF-8?q?=20=E3=81=A8=20WrapModesV=20=E3=81=AB=E5=B1=95=E9=96=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../IO/TextureIO/GltfTextureImporter.cs | 87 ++++--------------- .../GLTF/IO/Runtime/SamplerParam.cs | 28 +++++- .../GLTF/IO/Runtime/SamplerWrapTypes.cs | 10 --- .../GLTF/IO/Runtime/SamplerWrapTypes.cs.meta | 11 --- .../GLTF/IO/Runtime/TextureFactory.cs | 40 +-------- 5 files changed, 43 insertions(+), 133 deletions(-) delete mode 100644 Assets/VRMShaders/GLTF/IO/Runtime/SamplerWrapTypes.cs delete mode 100644 Assets/VRMShaders/GLTF/IO/Runtime/SamplerWrapTypes.cs.meta diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureImporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureImporter.cs index 3df38300c..e2b7fbb90 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureImporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureImporter.cs @@ -42,7 +42,7 @@ namespace UniGLTF var param = new TextureImportParam(name, gltfImage.GetExt(), gltfImage.uri, offset, scale, sampler, TextureImportTypes.sRGB, default, default, getTextureBytesAsync, default, default, default, default, default); return (key, param); } - + public static (SubAssetKey, TextureImportParam Param) CreateLinear(GltfParser parser, int textureIndex, Vector2 offset, Vector2 scale) { var gltfTexture = parser.GLTF.textures[textureIndex]; @@ -102,91 +102,36 @@ namespace UniGLTF if (gltfTexture.sampler < 0 || gltfTexture.sampler >= gltf.samplers.Count) { // default - return new SamplerParam - { - FilterMode = FilterMode.Bilinear, - WrapModes = new (SamplerWrapType, TextureWrapMode)[] { }, - }; + return SamplerParam.Default; } var gltfSampler = gltf.samplers[gltfTexture.sampler]; return new SamplerParam { - WrapModes = GetUnityWrapMode(gltfSampler).ToArray(), + WrapModesU = GetUnityWrapMode(gltfSampler.wrapS), + WrapModesV = GetUnityWrapMode(gltfSampler.wrapT), FilterMode = ImportFilterMode(gltfSampler.minFilter), }; } - public static IEnumerable<(SamplerWrapType, TextureWrapMode)> GetUnityWrapMode(glTFTextureSampler sampler) + public static TextureWrapMode GetUnityWrapMode(glWrap wrap) { - if (sampler.wrapS == sampler.wrapT) + switch (wrap) { - switch (sampler.wrapS) - { - case glWrap.NONE: // default - yield return (SamplerWrapType.All, TextureWrapMode.Repeat); - break; + case glWrap.NONE: // default + return TextureWrapMode.Repeat; - case glWrap.CLAMP_TO_EDGE: - yield return (SamplerWrapType.All, TextureWrapMode.Clamp); - break; + case glWrap.CLAMP_TO_EDGE: + return TextureWrapMode.Clamp; - case glWrap.REPEAT: - yield return (SamplerWrapType.All, TextureWrapMode.Repeat); - break; + case glWrap.REPEAT: + return TextureWrapMode.Repeat; - case glWrap.MIRRORED_REPEAT: - yield return (SamplerWrapType.All, TextureWrapMode.Mirror); - break; + case glWrap.MIRRORED_REPEAT: + return TextureWrapMode.Mirror; - default: - throw new NotImplementedException(); - } - } - else - { - switch (sampler.wrapS) - { - case glWrap.NONE: // default - yield return (SamplerWrapType.U, TextureWrapMode.Repeat); - break; - - case glWrap.CLAMP_TO_EDGE: - yield return (SamplerWrapType.U, TextureWrapMode.Clamp); - break; - - case glWrap.REPEAT: - yield return (SamplerWrapType.U, TextureWrapMode.Repeat); - break; - - case glWrap.MIRRORED_REPEAT: - yield return (SamplerWrapType.U, TextureWrapMode.Mirror); - break; - - default: - throw new NotImplementedException(); - } - switch (sampler.wrapT) - { - case glWrap.NONE: // default - yield return (SamplerWrapType.V, TextureWrapMode.Repeat); - break; - - case glWrap.CLAMP_TO_EDGE: - yield return (SamplerWrapType.V, TextureWrapMode.Clamp); - break; - - case glWrap.REPEAT: - yield return (SamplerWrapType.V, TextureWrapMode.Repeat); - break; - - case glWrap.MIRRORED_REPEAT: - yield return (SamplerWrapType.V, TextureWrapMode.Mirror); - break; - - default: - throw new NotImplementedException(); - } + default: + throw new NotImplementedException(); } } diff --git a/Assets/VRMShaders/GLTF/IO/Runtime/SamplerParam.cs b/Assets/VRMShaders/GLTF/IO/Runtime/SamplerParam.cs index 74a867e7f..74ab2e418 100644 --- a/Assets/VRMShaders/GLTF/IO/Runtime/SamplerParam.cs +++ b/Assets/VRMShaders/GLTF/IO/Runtime/SamplerParam.cs @@ -5,7 +5,31 @@ namespace VRMShaders { public struct SamplerParam { - public (SamplerWrapType, TextureWrapMode)[] WrapModes; - public FilterMode FilterMode; + public TextureWrapMode WrapModesU; + + public TextureWrapMode WrapModesV; + + public FilterMode FilterMode; + + public static SamplerParam Default => new SamplerParam + { + FilterMode = FilterMode.Bilinear, + WrapModesU = TextureWrapMode.Repeat, + WrapModesV = TextureWrapMode.Repeat, + }; + } + + public static class SamplerParamExtensions + { + public static void SetSampler(this Texture2D texture, in SamplerParam param) + { + if (texture == null) + { + return; + } + texture.wrapModeU = param.WrapModesU; + texture.wrapModeV = param.WrapModesV; + texture.filterMode = param.FilterMode; + } } } diff --git a/Assets/VRMShaders/GLTF/IO/Runtime/SamplerWrapTypes.cs b/Assets/VRMShaders/GLTF/IO/Runtime/SamplerWrapTypes.cs deleted file mode 100644 index cb47b57e5..000000000 --- a/Assets/VRMShaders/GLTF/IO/Runtime/SamplerWrapTypes.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace VRMShaders -{ - public enum SamplerWrapType - { - All, - U, - V, - W, - } -} \ No newline at end of file diff --git a/Assets/VRMShaders/GLTF/IO/Runtime/SamplerWrapTypes.cs.meta b/Assets/VRMShaders/GLTF/IO/Runtime/SamplerWrapTypes.cs.meta deleted file mode 100644 index f40105f81..000000000 --- a/Assets/VRMShaders/GLTF/IO/Runtime/SamplerWrapTypes.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 0e15e75e4b0c0c24999dcdee7dc17002 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/VRMShaders/GLTF/IO/Runtime/TextureFactory.cs b/Assets/VRMShaders/GLTF/IO/Runtime/TextureFactory.cs index cfc8384fa..cf4db3dec 100644 --- a/Assets/VRMShaders/GLTF/IO/Runtime/TextureFactory.cs +++ b/Assets/VRMShaders/GLTF/IO/Runtime/TextureFactory.cs @@ -138,51 +138,13 @@ namespace VRMShaders texture.LoadImage(imageBytes); } - SetSampler(texture, param); + texture.SetSampler(param.Sampler); cacheInfo = new TextureLoadInfo(texture, used, false); m_textureCache.Add(name, cacheInfo); return cacheInfo; } - public static void SetSampler(Texture2D texture, TextureImportParam param) - { - if (texture == null) - { - return; - } - - if (param.Sampler.WrapModes != null) - { - foreach (var (key, value) in param.Sampler.WrapModes) - { - switch (key) - { - case SamplerWrapType.All: - texture.wrapMode = value; - break; - - case SamplerWrapType.U: - texture.wrapModeU = value; - break; - - case SamplerWrapType.V: - texture.wrapModeV = value; - break; - - case SamplerWrapType.W: - texture.wrapModeW = value; - break; - - default: - throw new NotImplementedException(); - } - } - } - - texture.filterMode = param.Sampler.FilterMode; - } - /// /// テクスチャーをロード、必要であれば変換して返す。 /// 同じものはキャッシュを返す