From 9339733ff6bac9f8a40c28e1453eeff34c139308 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Fri, 2 Apr 2021 17:38:46 +0900 Subject: [PATCH 1/4] IAnimationImporter, MeshImporter, NodeImporter --- .../Runtime/UniGLTF/IO/IAnimationImporter.cs | 2 +- .../UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs | 8 ++++---- Assets/UniGLTF/Runtime/UniGLTF/IO/MeshImporter.cs | 12 ++++++------ Assets/UniGLTF/Runtime/UniGLTF/IO/NodeImporter.cs | 14 +++++++------- .../Runtime/UniGLTF/IO/RootAnimationImporter.cs | 2 +- 5 files changed, 19 insertions(+), 19 deletions(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/IAnimationImporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/IAnimationImporter.cs index 1d3057d66..09e77a7c0 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/IAnimationImporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/IAnimationImporter.cs @@ -5,6 +5,6 @@ namespace UniGLTF { public interface IAnimationImporter { - List Import(glTF gltf, GameObject root, Axises invertAxis); + List Import(glTF gltf, GameObject root, List nodes, List clips, Axises invertAxis); } } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs index 50cff20c5..289ef7357 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs @@ -110,7 +110,7 @@ namespace UniGLTF using (MeasureTime("AnimationImporter")) { - AnimationClips.AddRange(AnimationImporter.Import(GLTF, Root, InvertAxis)); + AnimationClips.AddRange(AnimationImporter.Import(GLTF, Root, null, null, InvertAxis)); } await OnLoadHierarchy(awaitCaller, MeasureTime); @@ -149,12 +149,12 @@ namespace UniGLTF nodes.Add(NodeImporter.BuildHierarchy(GLTF, i, Nodes, Meshes)); } - NodeImporter.FixCoordinate(this, nodes, inverter); + NodeImporter.FixCoordinate(GLTF, nodes, inverter); // skinning for (int i = 0; i < nodes.Count; ++i) { - NodeImporter.SetupSkinning(this, nodes, i, inverter); + NodeImporter.SetupSkinning(GLTF, nodes, i, inverter); } if (Root == null) @@ -202,7 +202,7 @@ namespace UniGLTF { using (MeasureTime("BuildMesh")) { - var meshWithMaterials = await MeshImporter.BuildMeshAsync(awaitCaller, MaterialFactory, x); + var meshWithMaterials = await MeshImporter.BuildMeshAsync(awaitCaller, MaterialFactory.GetMaterial, x); var mesh = meshWithMaterials.Mesh; // mesh name diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshImporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshImporter.cs index cd6d73f85..469b46ad0 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshImporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshImporter.cs @@ -225,7 +225,7 @@ namespace UniGLTF for (int j = 0; j < jointsLength; ++j) { var bw = new BoneWeight(); - + var joints = joints0(j); var weights = weights0(j); @@ -240,7 +240,7 @@ namespace UniGLTF bw.boneIndex3 = joints.w; bw.weight3 = weights.w; - + bw = NormalizeBoneWeight(bw); m_boneWeights.Add(bw); @@ -389,7 +389,7 @@ namespace UniGLTF for (int j = 0; j < jointsLength; ++j) { var bw = new BoneWeight(); - + var joints = joints0(j); var weights = weights0(j); @@ -406,7 +406,7 @@ namespace UniGLTF bw.weight3 = weights.w; bw = NormalizeBoneWeight(bw); - + m_boneWeights.Add(bw); } } @@ -648,7 +648,7 @@ namespace UniGLTF } } - public static async Task BuildMeshAsync(IAwaitCaller awaitCaller, MaterialFactory ctx, MeshImporter.MeshContext meshContext) + public static async Task BuildMeshAsync(IAwaitCaller awaitCaller, Func ctx, MeshImporter.MeshContext meshContext) { var (mesh, recalculateTangents) = _BuildMesh(meshContext); @@ -663,7 +663,7 @@ namespace UniGLTF var result = new MeshWithMaterials { Mesh = mesh, - Materials = meshContext.MaterialIndices.Select(x => ctx.GetMaterial(x)).ToArray() + Materials = meshContext.MaterialIndices.Select(ctx).ToArray() }; await awaitCaller.NextFrame(); diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/NodeImporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/NodeImporter.cs index 8461da81c..41a143d38 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/NodeImporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/NodeImporter.cs @@ -131,9 +131,9 @@ namespace UniGLTF // // fix node's coordinate. z-back to z-forward // - public static void FixCoordinate(ImporterContext context, List nodes, IAxisInverter inverter) + public static void FixCoordinate(glTF gltf, List nodes, IAxisInverter inverter) { - if (context.GLTF.rootnodes == null) + if (gltf.rootnodes == null) { return; } @@ -142,7 +142,7 @@ namespace UniGLTF Position = x.Transform.position, Rotation = x.Transform.rotation, }); - foreach (var x in context.GLTF.rootnodes) + foreach (var x in gltf.rootnodes) { // fix nodes coordinate // reverse Z in global @@ -158,7 +158,7 @@ namespace UniGLTF } } - public static void SetupSkinning(ImporterContext context, List nodes, int i, IAxisInverter inverter) + public static void SetupSkinning(glTF gltf, List nodes, int i, IAxisInverter inverter) { var x = nodes[i]; var skinnedMeshRenderer = x.Transform.GetComponent(); @@ -170,12 +170,12 @@ namespace UniGLTF if (mesh == null) throw new Exception(); if (skinnedMeshRenderer == null) throw new Exception(); - if (x.SkinIndex.Value < context.GLTF.skins.Count) + if (x.SkinIndex.Value < gltf.skins.Count) { // calculate internal values(boundingBox etc...) when sharedMesh assigned ? skinnedMeshRenderer.sharedMesh = null; - var skin = context.GLTF.skins[x.SkinIndex.Value]; + var skin = gltf.skins[x.SkinIndex.Value]; var joints = skin.joints.Select(y => nodes[y].Transform).ToArray(); if (joints.Any()) { @@ -184,7 +184,7 @@ namespace UniGLTF if (skin.inverseBindMatrices != -1) { - var bindPoses = context.GLTF.GetArrayFromAccessor(skin.inverseBindMatrices) + var bindPoses = gltf.GetArrayFromAccessor(skin.inverseBindMatrices) .Select(inverter.InvertMat4) .ToArray() ; diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/RootAnimationImporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/RootAnimationImporter.cs index 7c062da4b..f58bf4f87 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/RootAnimationImporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/RootAnimationImporter.cs @@ -6,7 +6,7 @@ namespace UniGLTF { public sealed class RootAnimationImporter : IAnimationImporter { - public List Import(glTF gltf, GameObject root, Axises invertAxis) + public List Import(glTF gltf, GameObject root, List _nodes, List _clips, Axises invertAxis) { var animationClips = new List(); if (gltf.animations != null && gltf.animations.Any()) From d28f700997b96b0f2c88a4e2251f717dac5f6a80 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Fri, 2 Apr 2021 18:14:27 +0900 Subject: [PATCH 2/4] =?UTF-8?q?=E7=A7=BB=E5=8B=95=20VRMShaders.TextureExpo?= =?UTF-8?q?rter?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #726 separate AssetTextureUtil --- .../Editor/UniGLTF/AssetTextureUtil.cs | 55 +++++++++++ .../Editor/UniGLTF/AssetTextureUtil.cs.meta | 11 +++ .../Editor/UniGLTF/GltfExportWindow.cs | 2 +- .../UniGLTF/IO/MaterialIO/MaterialExporter.cs | 4 +- .../UniGLTF/IO/TextureIO/TextureExporter.cs | 98 ++++++------------- .../Runtime/UniGLTF/IO/gltfExporter.cs | 6 +- Assets/UniGLTF/Tests/UniGLTF/MaterialTests.cs | 4 +- Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs | 8 +- Assets/UniGLTF/Tests/UniGLTF/UniGLTFTests.cs | 6 +- .../Tests/VRM.Samples.Editor.Tests.asmdef | 4 +- .../Editor/Tests/VRMImportExportTests.cs | 2 +- .../Editor/Tests/VRMMaterialTests.cs | 5 +- .../VRM.Samples/Scripts/VRMRuntimeExporter.cs | 2 +- Assets/VRM.Samples/VRM.Samples.asmdef | 3 +- Assets/VRM/Editor/Format/VRMEditorExporter.cs | 2 +- Assets/VRM/Runtime/IO/VRMExporter.cs | 4 +- Assets/VRM/Runtime/IO/VRMMaterialExporter.cs | 2 +- Assets/VRM/Tests/MToonTest.cs | 3 +- Assets/VRM/Tests/VRM.Tests.asmdef | 3 +- Assets/VRM/Tests/VRMLoadTests.cs | 2 +- .../VRM10/Runtime/Migration/MigrationMToon.cs | 20 +++- 21 files changed, 148 insertions(+), 98 deletions(-) create mode 100644 Assets/UniGLTF/Editor/UniGLTF/AssetTextureUtil.cs create mode 100644 Assets/UniGLTF/Editor/UniGLTF/AssetTextureUtil.cs.meta diff --git a/Assets/UniGLTF/Editor/UniGLTF/AssetTextureUtil.cs b/Assets/UniGLTF/Editor/UniGLTF/AssetTextureUtil.cs new file mode 100644 index 000000000..d22efde76 --- /dev/null +++ b/Assets/UniGLTF/Editor/UniGLTF/AssetTextureUtil.cs @@ -0,0 +1,55 @@ +using System.Reflection; +using UnityEditor; +using UnityEngine; + +namespace UniGLTF +{ + public static class AssetTextureUtil + { + /// + /// TextureImporter.maxTextureSize が元のテクスチャーより小さいか否かの判定 + /// + /// + /// + public static bool CopyIfMaxTextureSizeIsSmaller(Texture src) + { + var textureImporter = AssetImporter.GetAtPath(UnityPath.FromAsset(src).Value) as TextureImporter; + + // private メソッド TextureImporter.GetWidthAndHeight を無理やり呼ぶ + var getSizeMethod = typeof(TextureImporter).GetMethod("GetWidthAndHeight", BindingFlags.NonPublic | BindingFlags.Instance); + if (textureImporter != null && getSizeMethod != null) + { + var args = new object[2] { 0, 0 }; + getSizeMethod.Invoke(textureImporter, args); + var originalWidth = (int)args[0]; + var originalHeight = (int)args[1]; + var originalSize = Mathf.Max(originalWidth, originalHeight); + if (textureImporter.maxTextureSize < originalSize) + { + return true; + } + } + + return false; + } + + /// + /// 元の Asset が存在して、 TextureImporter に設定された画像サイズが小さくない + /// + /// + /// + /// + public static bool UseAsset(Texture texture) + { + if (texture != null && !string.IsNullOrEmpty(UnityEditor.AssetDatabase.GetAssetPath(texture))) + { + if (CopyIfMaxTextureSizeIsSmaller(texture)) + { + return false; + } + return true; + } + return false; + } + } +} diff --git a/Assets/UniGLTF/Editor/UniGLTF/AssetTextureUtil.cs.meta b/Assets/UniGLTF/Editor/UniGLTF/AssetTextureUtil.cs.meta new file mode 100644 index 000000000..003d5df03 --- /dev/null +++ b/Assets/UniGLTF/Editor/UniGLTF/AssetTextureUtil.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 6efbb66c1ac168848beead1fe3b16263 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/UniGLTF/Editor/UniGLTF/GltfExportWindow.cs b/Assets/UniGLTF/Editor/UniGLTF/GltfExportWindow.cs index 2e60f932f..6ed78c1b1 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/GltfExportWindow.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/GltfExportWindow.cs @@ -35,7 +35,7 @@ namespace UniGLTF using (var exporter = new gltfExporter(gltf, inverseAxis)) { exporter.Prepare(go); - exporter.Export(settings); + exporter.Export(settings, AssetTextureUtil.UseAsset); } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/MaterialExporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/MaterialExporter.cs index ab9a3bedd..b31901418 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/MaterialExporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/MaterialExporter.cs @@ -1,7 +1,7 @@ -using System; -using System.Linq; +using System.Linq; using UniGLTF.UniUnlit; using UnityEngine; +using VRMShaders; namespace UniGLTF diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureExporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureExporter.cs index f126e8262..086bc7f6b 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureExporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureExporter.cs @@ -1,13 +1,9 @@ using System; using System.Collections.Generic; using UnityEngine; -using System.Reflection; -using VRMShaders; -#if UNITY_EDITOR -using UnityEditor; -#endif -namespace UniGLTF + +namespace VRMShaders { /// /// glTF にエクスポートする Texture2D を蓄えて index を確定させる。 @@ -15,12 +11,29 @@ namespace UniGLTF /// public class TextureExporter { + Func m_useAsset; + + public TextureExporter(Func useAsset) + { + m_useAsset = useAsset; + } + + public enum ConvertTypes + { + // 無変換 + None, + // Unity Standard様式 から glTF PBR様式への変換 + OcclusionMetallicRoughness, + // Assetを使うときはそのバイト列を無変換で、それ以外は DXT5nm 形式からのデコードを行う + Normal, + } + struct ExportKey { public readonly Texture Src; - public readonly glTFTextureTypes TextureType; + public readonly ConvertTypes TextureType; - public ExportKey(Texture src, glTFTextureTypes type) + public ExportKey(Texture src, ConvertTypes type) { if (src == null) { @@ -45,7 +58,7 @@ namespace UniGLTF /// /// /// - public int GetTextureIndex(Texture src, glTFTextureTypes textureType) + public int GetTextureIndex(Texture src, ConvertTypes textureType) { if (src == null) { @@ -54,53 +67,6 @@ namespace UniGLTF return m_exportMap[new ExportKey(src, textureType)]; } - /// - /// TextureImporter.maxTextureSize が元のテクスチャーより小さいか否かの判定 - /// - /// - /// - static bool CopyIfMaxTextureSizeIsSmaller(Texture src) - { -#if UNITY_EDITOR - var textureImporter = AssetImporter.GetAtPath(UnityPath.FromAsset(src).Value) as TextureImporter; - var getSizeMethod = typeof(TextureImporter).GetMethod("GetWidthAndHeight", BindingFlags.NonPublic | BindingFlags.Instance); - if (textureImporter != null && getSizeMethod != null) - { - var args = new object[2] { 0, 0 }; - getSizeMethod.Invoke(textureImporter, args); - var originalWidth = (int)args[0]; - var originalHeight = (int)args[1]; - var originalSize = Mathf.Max(originalWidth, originalHeight); - if (textureImporter.maxTextureSize < originalSize) - { - return true; - } - } -#endif - return false; - } - - /// - /// 元の Asset が存在して、 TextureImporter に設定された画像サイズが小さくない - /// - /// - /// - /// - static bool UseAsset(Texture2D texture2D) - { -#if UNITY_EDITOR - if (texture2D != null && !string.IsNullOrEmpty(UnityEditor.AssetDatabase.GetAssetPath(texture2D))) - { - if (CopyIfMaxTextureSizeIsSmaller(texture2D)) - { - return false; - } - return true; - } -#endif - return false; - } - /// /// sRGBなテクスチャーを処理し、index を確定させる /// @@ -114,7 +80,7 @@ namespace UniGLTF } // cache - if (m_exportMap.TryGetValue(new ExportKey(src, glTFTextureTypes.SRGB), out var index)) + if (m_exportMap.TryGetValue(new ExportKey(src, ConvertTypes.None), out var index)) { return index; } @@ -122,7 +88,7 @@ namespace UniGLTF // get Texture2D index = Exported.Count; var texture2D = src as Texture2D; - if (UseAsset(texture2D)) + if (m_useAsset(texture2D)) { // do nothing } @@ -131,7 +97,7 @@ namespace UniGLTF texture2D = TextureConverter.CopyTexture(src, TextureImportTypes.sRGB, null); } Exported.Add(texture2D); - m_exportMap.Add(new ExportKey(src, glTFTextureTypes.SRGB), index); + m_exportMap.Add(new ExportKey(src, ConvertTypes.None), index); return index; } @@ -161,11 +127,11 @@ namespace UniGLTF } // cache - if (metallicSmoothTexture != null && m_exportMap.TryGetValue(new ExportKey(metallicSmoothTexture, glTFTextureTypes.OcclusionMetallicRoughness), out var index)) + if (metallicSmoothTexture != null && m_exportMap.TryGetValue(new ExportKey(metallicSmoothTexture, ConvertTypes.OcclusionMetallicRoughness), out var index)) { return index; } - if (occlusionTexture != null && m_exportMap.TryGetValue(new ExportKey(occlusionTexture, glTFTextureTypes.OcclusionMetallicRoughness), out index)) + if (occlusionTexture != null && m_exportMap.TryGetValue(new ExportKey(occlusionTexture, ConvertTypes.OcclusionMetallicRoughness), out index)) { return index; } @@ -179,11 +145,11 @@ namespace UniGLTF Exported.Add(texture2D); if (metallicSmoothTexture != null) { - m_exportMap.Add(new ExportKey(metallicSmoothTexture, glTFTextureTypes.OcclusionMetallicRoughness), index); + m_exportMap.Add(new ExportKey(metallicSmoothTexture, ConvertTypes.OcclusionMetallicRoughness), index); } if (occlusionTexture != null && occlusionTexture != metallicSmoothTexture) { - m_exportMap.Add(new ExportKey(occlusionTexture, glTFTextureTypes.OcclusionMetallicRoughness), index); + m_exportMap.Add(new ExportKey(occlusionTexture, ConvertTypes.OcclusionMetallicRoughness), index); } return index; @@ -202,7 +168,7 @@ namespace UniGLTF } // cache - if (m_exportMap.TryGetValue(new ExportKey(src, glTFTextureTypes.Normal), out var index)) + if (m_exportMap.TryGetValue(new ExportKey(src, ConvertTypes.Normal), out var index)) { return index; } @@ -210,7 +176,7 @@ namespace UniGLTF // get Texture2D index = Exported.Count; var texture2D = src as Texture2D; - if (UseAsset(texture2D)) + if (m_useAsset(texture2D)) { // EditorAsset を使うので変換不要 } @@ -221,7 +187,7 @@ namespace UniGLTF } Exported.Add(texture2D); - m_exportMap.Add(new ExportKey(src, glTFTextureTypes.Normal), index); + m_exportMap.Add(new ExportKey(src, ConvertTypes.Normal), index); return index; } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs index 5aa7c151e..1bdb2f060 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using System.Linq; using UnityEngine; - +using VRMShaders; namespace UniGLTF { @@ -173,7 +173,7 @@ namespace UniGLTF } - public virtual void Export(MeshExportSettings meshExportSettings) + public virtual void Export(MeshExportSettings meshExportSettings, Func useAsset) { var bytesBuffer = new ArrayByteBuffer(new byte[50 * 1024 * 1024]); var bufferIndex = glTF.AddBuffer(bytesBuffer); @@ -185,7 +185,7 @@ namespace UniGLTF #region Materials and Textures Materials = Nodes.SelectMany(x => x.GetSharedMaterials()).Where(x => x != null).Distinct().ToList(); - TextureManager = new TextureExporter(); + TextureManager = new TextureExporter(useAsset); var materialExporter = CreateMaterialExporter(); glTF.materials = Materials.Select(x => materialExporter.ExportMaterial(x, TextureManager)).ToList(); diff --git a/Assets/UniGLTF/Tests/UniGLTF/MaterialTests.cs b/Assets/UniGLTF/Tests/UniGLTF/MaterialTests.cs index ebbc590e3..039c67c9c 100644 --- a/Assets/UniGLTF/Tests/UniGLTF/MaterialTests.cs +++ b/Assets/UniGLTF/Tests/UniGLTF/MaterialTests.cs @@ -18,7 +18,7 @@ namespace UniGLTF filterMode = FilterMode.Bilinear, }; - var textureManager = new TextureExporter(); + var textureManager = new TextureExporter(AssetTextureUtil.UseAsset); var srcMaterial = new Material(Shader.Find("Standard")); var offset = new Vector2(0.3f, 0.2f); @@ -242,7 +242,7 @@ namespace UniGLTF material.SetColor("_EmissionColor", new Color(0, 1, 2, 1)); material.EnableKeyword("_EMISSION"); var materialExporter = new MaterialExporter(); - var textureExportManager = new TextureExporter(); + var textureExportManager = new TextureExporter(AssetTextureUtil.UseAsset); var gltfMaterial = materialExporter.ExportMaterial(material, textureExportManager); Assert.AreEqual(gltfMaterial.emissiveFactor, new float[] { 0, 0.5f, 1 }); diff --git a/Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs b/Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs index 91c960e2a..4371fcdd7 100644 --- a/Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs +++ b/Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs @@ -15,7 +15,7 @@ namespace UniGLTF wrapMode = TextureWrapMode.Clamp, filterMode = FilterMode.Trilinear, }; - var textureManager = new TextureExporter(); + var textureManager = new TextureExporter(AssetTextureUtil.UseAsset); var material = new Material(Shader.Find("Standard")); material.mainTexture = tex0; @@ -58,16 +58,16 @@ namespace UniGLTF var occlusion = new Texture2D(4, 4, TextureFormat.ARGB32, false, true); { - var exporter = new TextureExporter(); + var exporter = new TextureExporter(AssetTextureUtil.UseAsset); Assert.AreEqual(-1, exporter.ExportMetallicSmoothnessOcclusion(null, 0, null)); } { - var exporter = new TextureExporter(); + var exporter = new TextureExporter(AssetTextureUtil.UseAsset); Assert.AreEqual(0, exporter.ExportMetallicSmoothnessOcclusion(null, 0, occlusion)); Assert.AreEqual(1, exporter.ExportMetallicSmoothnessOcclusion(metallic, 0, null)); } { - var exporter = new TextureExporter(); + var exporter = new TextureExporter(AssetTextureUtil.UseAsset); Assert.AreEqual(0, exporter.ExportMetallicSmoothnessOcclusion(metallic, 0, occlusion)); Assert.AreEqual(0, exporter.ExportMetallicSmoothnessOcclusion(null, 0, occlusion)); Assert.AreEqual(0, exporter.ExportMetallicSmoothnessOcclusion(metallic, 0, null)); diff --git a/Assets/UniGLTF/Tests/UniGLTF/UniGLTFTests.cs b/Assets/UniGLTF/Tests/UniGLTF/UniGLTFTests.cs index 9cf2512a1..cd73aa0c3 100644 --- a/Assets/UniGLTF/Tests/UniGLTF/UniGLTFTests.cs +++ b/Assets/UniGLTF/Tests/UniGLTF/UniGLTFTests.cs @@ -106,7 +106,7 @@ namespace UniGLTF using (var exporter = new gltfExporter(gltf)) { exporter.Prepare(go); - exporter.Export(MeshExportSettings.Default); + exporter.Export(MeshExportSettings.Default, AssetTextureUtil.UseAsset); // remove empty buffer gltf.buffers.Clear(); @@ -298,7 +298,7 @@ namespace UniGLTF using (var exporter = new gltfExporter(gltf)) { exporter.Prepare(CreateSimpleScene()); - exporter.Export(MeshExportSettings.Default); + exporter.Export(MeshExportSettings.Default, AssetTextureUtil.UseAsset); } var expected = gltf.ToJson().ParseAsJson(); @@ -534,7 +534,7 @@ namespace UniGLTF using (var exporter = new gltfExporter(gltf)) { exporter.Prepare(go); - exporter.Export(UniGLTF.MeshExportSettings.Default); + exporter.Export(UniGLTF.MeshExportSettings.Default, AssetTextureUtil.UseAsset); json = gltf.ToJson(); } diff --git a/Assets/VRM.Samples/Editor/Tests/VRM.Samples.Editor.Tests.asmdef b/Assets/VRM.Samples/Editor/Tests/VRM.Samples.Editor.Tests.asmdef index 31fbd23fc..fad13ad55 100644 --- a/Assets/VRM.Samples/Editor/Tests/VRM.Samples.Editor.Tests.asmdef +++ b/Assets/VRM.Samples/Editor/Tests/VRM.Samples.Editor.Tests.asmdef @@ -6,7 +6,9 @@ "MeshUtility", "UniGLTF", "UniVRM.Editor", - "VRM.Tests" + "VRM.Tests", + "VRMShaders", + "UniGLTF.Editor" ], "optionalUnityReferences": [ "TestAssemblies" diff --git a/Assets/VRM.Samples/Editor/Tests/VRMImportExportTests.cs b/Assets/VRM.Samples/Editor/Tests/VRMImportExportTests.cs index e484e372f..ee23d0e99 100644 --- a/Assets/VRM.Samples/Editor/Tests/VRMImportExportTests.cs +++ b/Assets/VRM.Samples/Editor/Tests/VRMImportExportTests.cs @@ -169,7 +169,7 @@ namespace VRM.Samples */ importedJson.RemoveValue(Utf8String.From("/bufferViews/*/byteStride")); - var vrm = VRMExporter.Export(UniGLTF.MeshExportSettings.Default, context.Root); + var vrm = VRMExporter.Export(UniGLTF.MeshExportSettings.Default, context.Root, AssetTextureUtil.UseAsset); // TODO: Check contents in JSON /*var exportJson = */ diff --git a/Assets/VRM.Samples/Editor/Tests/VRMMaterialTests.cs b/Assets/VRM.Samples/Editor/Tests/VRMMaterialTests.cs index cf75d926a..7c98c6829 100644 --- a/Assets/VRM.Samples/Editor/Tests/VRMMaterialTests.cs +++ b/Assets/VRM.Samples/Editor/Tests/VRMMaterialTests.cs @@ -1,6 +1,7 @@ using NUnit.Framework; +using UniGLTF; using UnityEngine; - +using VRMShaders; namespace VRM.Samples { @@ -10,7 +11,7 @@ namespace VRM.Samples { var material = Resources.Load(resourceName); var exporter = new VRMMaterialExporter(); - var textureManager = new UniGLTF.TextureExporter(); + var textureManager = new TextureExporter(AssetTextureUtil.UseAsset); var exported = exporter.ExportMaterial(material, textureManager); // parse glTFExtensionExport to glTFExtensionImport diff --git a/Assets/VRM.Samples/Scripts/VRMRuntimeExporter.cs b/Assets/VRM.Samples/Scripts/VRMRuntimeExporter.cs index 16119365e..23d5ad165 100644 --- a/Assets/VRM.Samples/Scripts/VRMRuntimeExporter.cs +++ b/Assets/VRM.Samples/Scripts/VRMRuntimeExporter.cs @@ -95,7 +95,7 @@ namespace VRM.Samples return; } - var vrm = VRMExporter.Export(UniGLTF.MeshExportSettings.Default, m_model); + var vrm = VRMExporter.Export(UniGLTF.MeshExportSettings.Default, m_model, _ => false); var bytes = vrm.ToGlbBytes(); File.WriteAllBytes(path, bytes); Debug.LogFormat("export to {0}", path); diff --git a/Assets/VRM.Samples/VRM.Samples.asmdef b/Assets/VRM.Samples/VRM.Samples.asmdef index 70aae1594..de2c46ae4 100644 --- a/Assets/VRM.Samples/VRM.Samples.asmdef +++ b/Assets/VRM.Samples/VRM.Samples.asmdef @@ -3,7 +3,8 @@ "references": [ "VRM", "UniHumanoid", - "UniGLTF" + "UniGLTF", + "VRMShaders" ], "optionalUnityReferences": [], "includePlatforms": [], diff --git a/Assets/VRM/Editor/Format/VRMEditorExporter.cs b/Assets/VRM/Editor/Format/VRMEditorExporter.cs index 96e236ab0..40c5e8cdc 100644 --- a/Assets/VRM/Editor/Format/VRMEditorExporter.cs +++ b/Assets/VRM/Editor/Format/VRMEditorExporter.cs @@ -224,7 +224,7 @@ namespace VRM using (var exporter = new VRMExporter(gltf)) { exporter.Prepare(target); - exporter.Export(settings.MeshExportSettings); + exporter.Export(settings.MeshExportSettings, AssetTextureUtil.UseAsset); } var bytes = gltf.ToGlbBytes(); File.WriteAllBytes(path, bytes); diff --git a/Assets/VRM/Runtime/IO/VRMExporter.cs b/Assets/VRM/Runtime/IO/VRMExporter.cs index c4d5c80bb..a1a5ce748 100644 --- a/Assets/VRM/Runtime/IO/VRMExporter.cs +++ b/Assets/VRM/Runtime/IO/VRMExporter.cs @@ -14,13 +14,13 @@ namespace VRM return new VRMMaterialExporter(); } - public static glTF Export(MeshExportSettings configuration, GameObject go) + public static glTF Export(MeshExportSettings configuration, GameObject go, Func useAsset) { var gltf = new glTF(); using (var exporter = new VRMExporter(gltf)) { exporter.Prepare(go); - exporter.Export(configuration); + exporter.Export(configuration, useAsset); } return gltf; } diff --git a/Assets/VRM/Runtime/IO/VRMMaterialExporter.cs b/Assets/VRM/Runtime/IO/VRMMaterialExporter.cs index 0b49c6b90..22c3ab11b 100644 --- a/Assets/VRM/Runtime/IO/VRMMaterialExporter.cs +++ b/Assets/VRM/Runtime/IO/VRMMaterialExporter.cs @@ -4,7 +4,7 @@ using System.Linq; using UniGLTF; using UniGLTF.ShaderPropExporter; using UnityEngine; - +using VRMShaders; namespace VRM { diff --git a/Assets/VRM/Tests/MToonTest.cs b/Assets/VRM/Tests/MToonTest.cs index b8326d7e0..86d79422d 100644 --- a/Assets/VRM/Tests/MToonTest.cs +++ b/Assets/VRM/Tests/MToonTest.cs @@ -1,6 +1,7 @@ using NUnit.Framework; using UniGLTF; using UnityEngine; +using VRMShaders; namespace VRM { @@ -15,7 +16,7 @@ namespace VRM filterMode = FilterMode.Bilinear, }; - var textureManager = new TextureExporter(); + var textureManager = new TextureExporter(AssetTextureUtil.UseAsset); var srcMaterial = new Material(Shader.Find("VRM/MToon")); var offset = new Vector2(0.3f, 0.2f); diff --git a/Assets/VRM/Tests/VRM.Tests.asmdef b/Assets/VRM/Tests/VRM.Tests.asmdef index 6f70ecbf9..36d8d3e11 100644 --- a/Assets/VRM/Tests/VRM.Tests.asmdef +++ b/Assets/VRM/Tests/VRM.Tests.asmdef @@ -5,7 +5,8 @@ "UniGLTF", "MeshUtility", "MeshUtility.Editor", - "VRMShaders" + "VRMShaders", + "UniGLTF.Editor" ], "optionalUnityReferences": [ "TestAssemblies" diff --git a/Assets/VRM/Tests/VRMLoadTests.cs b/Assets/VRM/Tests/VRMLoadTests.cs index 49173e7f3..ce4d2fce5 100644 --- a/Assets/VRM/Tests/VRMLoadTests.cs +++ b/Assets/VRM/Tests/VRMLoadTests.cs @@ -105,7 +105,7 @@ namespace VRM try { // export - var vrm = VRMExporter.Export(UniGLTF.MeshExportSettings.Default, go); + var vrm = VRMExporter.Export(UniGLTF.MeshExportSettings.Default, go, AssetTextureUtil.UseAsset); // re import if (vrm != null) diff --git a/Assets/VRM10/Runtime/Migration/MigrationMToon.cs b/Assets/VRM10/Runtime/Migration/MigrationMToon.cs index f500548cc..1c55b1580 100644 --- a/Assets/VRM10/Runtime/Migration/MigrationMToon.cs +++ b/Assets/VRM10/Runtime/Migration/MigrationMToon.cs @@ -124,7 +124,10 @@ namespace UniVRM10 break; default: - throw new NotImplementedException($"{kv.Key}: {kv.Value}"); +#if VRM_DEVELOP + Debug.LogWarning($"vectorProperties: {kv.Key}: {kv.Value}"); +#endif + break; } } @@ -225,7 +228,10 @@ namespace UniVRM10 break; default: - throw new NotImplementedException($"floatProperties: {kv.Key} is unknown"); +#if VRM_DEVELOP + Debug.LogWarning($"floatProperties: {kv.Key} is unknown"); +#endif + break; } } @@ -247,7 +253,10 @@ namespace UniVRM10 case "_OutlineWidthTexture": map.OutlineWidthTexture = index; break; case "_UvAnimMaskTexture": map.UvAnimMaskTexture = index; break; default: - throw new NotImplementedException($"textureProperties: {kv.Key} is unknown"); +#if VRM_DEVELOP + Debug.LogWarning($"textureProperties: {kv.Key} is unknown"); +#endif + break; } } @@ -313,7 +322,10 @@ namespace UniVRM10 { index = mtoon.TextureIndexMap.MainTex.Value }; - var value = mtoon.OffsetScale["_MainTex"]; + if (!mtoon.OffsetScale.TryGetValue("_MainTex", out float[] value)) + { + value = new float[] { 0, 0, 1, 1 }; + } glTF_KHR_texture_transform.Serialize( gltfMaterial.pbrMetallicRoughness.baseColorTexture, (value[0], value[1]), From 27fa5dbc60ff5723c9322abe2d79ae25944f7562 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Fri, 2 Apr 2021 18:35:02 +0900 Subject: [PATCH 3/4] VRMShaders.TextureExporter, AssetTextureUtil --- Assets/UniGLTF/Editor/UniGLTF.Editor.asmdef | 3 ++- .../Editor/UniGLTF/GltfExportWindow.cs | 1 + Assets/UniGLTF/Tests/UniGLTF.Tests.asmdef | 3 ++- Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs | 23 ------------------- Assets/UniGLTF/Tests/UniGLTF/UniGLTFTests.cs | 2 +- .../Tests/VRM.Samples.Editor.Tests.asmdef | 2 +- .../Editor/Tests/VRMImportExportTests.cs | 1 + Assets/VRM/Editor/Format/VRMEditorExporter.cs | 1 + Assets/VRM/Editor/VRM.Editor.asmdef | 3 ++- Assets/VRM/Tests/VRM.Tests.asmdef | 2 +- Assets/VRM/Tests/VRMLoadTests.cs | 1 + Assets/VRMShaders/Editor.meta | 8 +++++++ .../Editor}/AssetTextureUtil.cs | 5 ++-- .../Editor}/AssetTextureUtil.cs.meta | 0 .../Editor/VRMShaders.Editor.asmdef | 3 +++ .../Editor/VRMShaders.Editor.asmdef.meta | 7 ++++++ .../Runtime}/TextureExporter.cs | 0 .../Runtime}/TextureExporter.cs.meta | 0 .../Tests/MetallicRoughnessConverterTests.cs | 23 +++++++++++++++++++ .../VRMShaders/Tests/VRMShaders.Tests.asmdef | 3 ++- 20 files changed, 59 insertions(+), 32 deletions(-) create mode 100644 Assets/VRMShaders/Editor.meta rename Assets/{UniGLTF/Editor/UniGLTF => VRMShaders/Editor}/AssetTextureUtil.cs (92%) rename Assets/{UniGLTF/Editor/UniGLTF => VRMShaders/Editor}/AssetTextureUtil.cs.meta (100%) create mode 100644 Assets/VRMShaders/Editor/VRMShaders.Editor.asmdef create mode 100644 Assets/VRMShaders/Editor/VRMShaders.Editor.asmdef.meta rename Assets/{UniGLTF/Runtime/UniGLTF/IO/TextureIO => VRMShaders/Runtime}/TextureExporter.cs (100%) rename Assets/{UniGLTF/Runtime/UniGLTF/IO/TextureIO => VRMShaders/Runtime}/TextureExporter.cs.meta (100%) diff --git a/Assets/UniGLTF/Editor/UniGLTF.Editor.asmdef b/Assets/UniGLTF/Editor/UniGLTF.Editor.asmdef index 60c094749..403b405c9 100644 --- a/Assets/UniGLTF/Editor/UniGLTF.Editor.asmdef +++ b/Assets/UniGLTF/Editor/UniGLTF.Editor.asmdef @@ -4,7 +4,8 @@ "UniGLTF", "MeshUtility", "MeshUtility.Editor", - "VRMShaders" + "VRMShaders", + "VRMShaders.Editor" ], "optionalUnityReferences": [], "includePlatforms": [ diff --git a/Assets/UniGLTF/Editor/UniGLTF/GltfExportWindow.cs b/Assets/UniGLTF/Editor/UniGLTF/GltfExportWindow.cs index 6ed78c1b1..da1bdea89 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/GltfExportWindow.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/GltfExportWindow.cs @@ -5,6 +5,7 @@ using System.Reflection; using UniGLTF.Animation; using UnityEditor; using UnityEngine; +using VRMShaders; namespace UniGLTF { diff --git a/Assets/UniGLTF/Tests/UniGLTF.Tests.asmdef b/Assets/UniGLTF/Tests/UniGLTF.Tests.asmdef index 71ff882a1..bb5646ef0 100644 --- a/Assets/UniGLTF/Tests/UniGLTF.Tests.asmdef +++ b/Assets/UniGLTF/Tests/UniGLTF.Tests.asmdef @@ -3,7 +3,8 @@ "references": [ "UniGLTF", "UniGLTF.Editor", - "VRMShaders" + "VRMShaders", + "VRMShaders.Editor" ], "optionalUnityReferences": [ "TestAssemblies" diff --git a/Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs b/Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs index 4371fcdd7..f18311fa1 100644 --- a/Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs +++ b/Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs @@ -50,28 +50,5 @@ namespace UniGLTF var (bytes, mime) = GltfTextureExporter.GetBytesWithMime(readonlyTexture); Assert.NotNull(bytes); } - - [Test] - public void ExportMetallicSmoothnessOcclusion_Test() - { - var metallic = new Texture2D(4, 4, TextureFormat.ARGB32, false, true); - var occlusion = new Texture2D(4, 4, TextureFormat.ARGB32, false, true); - - { - var exporter = new TextureExporter(AssetTextureUtil.UseAsset); - Assert.AreEqual(-1, exporter.ExportMetallicSmoothnessOcclusion(null, 0, null)); - } - { - var exporter = new TextureExporter(AssetTextureUtil.UseAsset); - Assert.AreEqual(0, exporter.ExportMetallicSmoothnessOcclusion(null, 0, occlusion)); - Assert.AreEqual(1, exporter.ExportMetallicSmoothnessOcclusion(metallic, 0, null)); - } - { - var exporter = new TextureExporter(AssetTextureUtil.UseAsset); - Assert.AreEqual(0, exporter.ExportMetallicSmoothnessOcclusion(metallic, 0, occlusion)); - Assert.AreEqual(0, exporter.ExportMetallicSmoothnessOcclusion(null, 0, occlusion)); - Assert.AreEqual(0, exporter.ExportMetallicSmoothnessOcclusion(metallic, 0, null)); - } - } } } diff --git a/Assets/UniGLTF/Tests/UniGLTF/UniGLTFTests.cs b/Assets/UniGLTF/Tests/UniGLTF/UniGLTFTests.cs index cd73aa0c3..f50eb2131 100644 --- a/Assets/UniGLTF/Tests/UniGLTF/UniGLTFTests.cs +++ b/Assets/UniGLTF/Tests/UniGLTF/UniGLTFTests.cs @@ -4,7 +4,7 @@ using System.Collections.Generic; using System.Linq; using UniJSON; using UnityEngine; - +using VRMShaders; namespace UniGLTF { diff --git a/Assets/VRM.Samples/Editor/Tests/VRM.Samples.Editor.Tests.asmdef b/Assets/VRM.Samples/Editor/Tests/VRM.Samples.Editor.Tests.asmdef index fad13ad55..813b970b4 100644 --- a/Assets/VRM.Samples/Editor/Tests/VRM.Samples.Editor.Tests.asmdef +++ b/Assets/VRM.Samples/Editor/Tests/VRM.Samples.Editor.Tests.asmdef @@ -8,7 +8,7 @@ "UniVRM.Editor", "VRM.Tests", "VRMShaders", - "UniGLTF.Editor" + "VRMShaders.Editor" ], "optionalUnityReferences": [ "TestAssemblies" diff --git a/Assets/VRM.Samples/Editor/Tests/VRMImportExportTests.cs b/Assets/VRM.Samples/Editor/Tests/VRMImportExportTests.cs index ee23d0e99..adb843443 100644 --- a/Assets/VRM.Samples/Editor/Tests/VRMImportExportTests.cs +++ b/Assets/VRM.Samples/Editor/Tests/VRMImportExportTests.cs @@ -5,6 +5,7 @@ using UniJSON; using UnityEngine; using MeshUtility; using System; +using VRMShaders; namespace VRM.Samples { diff --git a/Assets/VRM/Editor/Format/VRMEditorExporter.cs b/Assets/VRM/Editor/Format/VRMEditorExporter.cs index 40c5e8cdc..4f4e02c46 100644 --- a/Assets/VRM/Editor/Format/VRMEditorExporter.cs +++ b/Assets/VRM/Editor/Format/VRMEditorExporter.cs @@ -6,6 +6,7 @@ using System.Text; using UniGLTF; using UnityEditor; using UnityEngine; +using VRMShaders; namespace VRM { diff --git a/Assets/VRM/Editor/VRM.Editor.asmdef b/Assets/VRM/Editor/VRM.Editor.asmdef index e9422cc38..0cdfce423 100644 --- a/Assets/VRM/Editor/VRM.Editor.asmdef +++ b/Assets/VRM/Editor/VRM.Editor.asmdef @@ -8,7 +8,8 @@ "UniUnlit", "UniGLTF", "UniGLTF.Editor", - "VRMShaders" + "VRMShaders", + "VRMShaders.Editor" ], "optionalUnityReferences": [], "includePlatforms": [ diff --git a/Assets/VRM/Tests/VRM.Tests.asmdef b/Assets/VRM/Tests/VRM.Tests.asmdef index 36d8d3e11..de7e2bdab 100644 --- a/Assets/VRM/Tests/VRM.Tests.asmdef +++ b/Assets/VRM/Tests/VRM.Tests.asmdef @@ -6,7 +6,7 @@ "MeshUtility", "MeshUtility.Editor", "VRMShaders", - "UniGLTF.Editor" + "VRMShaders.Editor" ], "optionalUnityReferences": [ "TestAssemblies" diff --git a/Assets/VRM/Tests/VRMLoadTests.cs b/Assets/VRM/Tests/VRMLoadTests.cs index ce4d2fce5..e1704c2cf 100644 --- a/Assets/VRM/Tests/VRMLoadTests.cs +++ b/Assets/VRM/Tests/VRMLoadTests.cs @@ -4,6 +4,7 @@ using System.IO; using NUnit.Framework; using UniGLTF; using UnityEngine; +using VRMShaders; namespace VRM { diff --git a/Assets/VRMShaders/Editor.meta b/Assets/VRMShaders/Editor.meta new file mode 100644 index 000000000..ac317874a --- /dev/null +++ b/Assets/VRMShaders/Editor.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 58204522f08a6fe4f846a5817a37f902 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/UniGLTF/Editor/UniGLTF/AssetTextureUtil.cs b/Assets/VRMShaders/Editor/AssetTextureUtil.cs similarity index 92% rename from Assets/UniGLTF/Editor/UniGLTF/AssetTextureUtil.cs rename to Assets/VRMShaders/Editor/AssetTextureUtil.cs index d22efde76..2a608107b 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/AssetTextureUtil.cs +++ b/Assets/VRMShaders/Editor/AssetTextureUtil.cs @@ -2,7 +2,7 @@ using System.Reflection; using UnityEditor; using UnityEngine; -namespace UniGLTF +namespace VRMShaders { public static class AssetTextureUtil { @@ -13,7 +13,8 @@ namespace UniGLTF /// public static bool CopyIfMaxTextureSizeIsSmaller(Texture src) { - var textureImporter = AssetImporter.GetAtPath(UnityPath.FromAsset(src).Value) as TextureImporter; + var path = AssetDatabase.GetAssetPath(src); + var textureImporter = AssetImporter.GetAtPath(path) as TextureImporter; // private メソッド TextureImporter.GetWidthAndHeight を無理やり呼ぶ var getSizeMethod = typeof(TextureImporter).GetMethod("GetWidthAndHeight", BindingFlags.NonPublic | BindingFlags.Instance); diff --git a/Assets/UniGLTF/Editor/UniGLTF/AssetTextureUtil.cs.meta b/Assets/VRMShaders/Editor/AssetTextureUtil.cs.meta similarity index 100% rename from Assets/UniGLTF/Editor/UniGLTF/AssetTextureUtil.cs.meta rename to Assets/VRMShaders/Editor/AssetTextureUtil.cs.meta diff --git a/Assets/VRMShaders/Editor/VRMShaders.Editor.asmdef b/Assets/VRMShaders/Editor/VRMShaders.Editor.asmdef new file mode 100644 index 000000000..9f771ac30 --- /dev/null +++ b/Assets/VRMShaders/Editor/VRMShaders.Editor.asmdef @@ -0,0 +1,3 @@ +{ + "name": "VRMShaders.Editor" +} diff --git a/Assets/VRMShaders/Editor/VRMShaders.Editor.asmdef.meta b/Assets/VRMShaders/Editor/VRMShaders.Editor.asmdef.meta new file mode 100644 index 000000000..8c9bfda5c --- /dev/null +++ b/Assets/VRMShaders/Editor/VRMShaders.Editor.asmdef.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 7da8a75dcade2144aab699032d7d7987 +AssemblyDefinitionImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureExporter.cs b/Assets/VRMShaders/Runtime/TextureExporter.cs similarity index 100% rename from Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureExporter.cs rename to Assets/VRMShaders/Runtime/TextureExporter.cs diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureExporter.cs.meta b/Assets/VRMShaders/Runtime/TextureExporter.cs.meta similarity index 100% rename from Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureExporter.cs.meta rename to Assets/VRMShaders/Runtime/TextureExporter.cs.meta diff --git a/Assets/VRMShaders/Tests/MetallicRoughnessConverterTests.cs b/Assets/VRMShaders/Tests/MetallicRoughnessConverterTests.cs index 1f79152f9..caef40d25 100644 --- a/Assets/VRMShaders/Tests/MetallicRoughnessConverterTests.cs +++ b/Assets/VRMShaders/Tests/MetallicRoughnessConverterTests.cs @@ -89,5 +89,28 @@ namespace VRMShaders Is.EqualTo(new Color32(255, 0, 0, 255))); } } + + [Test] + public void ExportMetallicSmoothnessOcclusion_Test() + { + var metallic = new Texture2D(4, 4, TextureFormat.ARGB32, false, true); + var occlusion = new Texture2D(4, 4, TextureFormat.ARGB32, false, true); + + { + var exporter = new TextureExporter(AssetTextureUtil.UseAsset); + Assert.AreEqual(-1, exporter.ExportMetallicSmoothnessOcclusion(null, 0, null)); + } + { + var exporter = new TextureExporter(AssetTextureUtil.UseAsset); + Assert.AreEqual(0, exporter.ExportMetallicSmoothnessOcclusion(null, 0, occlusion)); + Assert.AreEqual(1, exporter.ExportMetallicSmoothnessOcclusion(metallic, 0, null)); + } + { + var exporter = new TextureExporter(AssetTextureUtil.UseAsset); + Assert.AreEqual(0, exporter.ExportMetallicSmoothnessOcclusion(metallic, 0, occlusion)); + Assert.AreEqual(0, exporter.ExportMetallicSmoothnessOcclusion(null, 0, occlusion)); + Assert.AreEqual(0, exporter.ExportMetallicSmoothnessOcclusion(metallic, 0, null)); + } + } } } diff --git a/Assets/VRMShaders/Tests/VRMShaders.Tests.asmdef b/Assets/VRMShaders/Tests/VRMShaders.Tests.asmdef index 18b3c3304..a2a91ba59 100644 --- a/Assets/VRMShaders/Tests/VRMShaders.Tests.asmdef +++ b/Assets/VRMShaders/Tests/VRMShaders.Tests.asmdef @@ -1,7 +1,8 @@ { "name": "VRMShaders.Tests", "references": [ - "VRMShaders" + "VRMShaders", + "VRMShaders.Editor" ], "optionalUnityReferences": [ "TestAssemblies" From 002e8c6060fef6dc4bbab270b1dc04e212ecea81 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Mon, 5 Apr 2021 13:11:27 +0900 Subject: [PATCH 4/4] rename AssetTextureUtil.UseAsset to AssetTextureUtil.IsTextureEditorAsset --- .../Editor/UniGLTF/GltfExportWindow.cs | 2 +- Assets/UniGLTF/Tests/UniGLTF/MaterialTests.cs | 4 +-- Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs | 2 +- Assets/UniGLTF/Tests/UniGLTF/UniGLTFTests.cs | 6 ++--- .../Editor/Tests/VRMImportExportTests.cs | 2 +- .../Editor/Tests/VRMMaterialTests.cs | 2 +- Assets/VRM/Editor/Format/VRMEditorExporter.cs | 2 +- Assets/VRM/Tests/MToonTest.cs | 2 +- Assets/VRM/Tests/VRMLoadTests.cs | 2 +- Assets/VRMShaders/Editor/AssetTextureUtil.cs | 27 ++++++++++++++----- .../Tests/MetallicRoughnessConverterTests.cs | 6 ++--- 11 files changed, 36 insertions(+), 21 deletions(-) diff --git a/Assets/UniGLTF/Editor/UniGLTF/GltfExportWindow.cs b/Assets/UniGLTF/Editor/UniGLTF/GltfExportWindow.cs index da1bdea89..4842878dc 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/GltfExportWindow.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/GltfExportWindow.cs @@ -36,7 +36,7 @@ namespace UniGLTF using (var exporter = new gltfExporter(gltf, inverseAxis)) { exporter.Prepare(go); - exporter.Export(settings, AssetTextureUtil.UseAsset); + exporter.Export(settings, AssetTextureUtil.IsTextureEditorAsset ); } diff --git a/Assets/UniGLTF/Tests/UniGLTF/MaterialTests.cs b/Assets/UniGLTF/Tests/UniGLTF/MaterialTests.cs index 039c67c9c..9592d923b 100644 --- a/Assets/UniGLTF/Tests/UniGLTF/MaterialTests.cs +++ b/Assets/UniGLTF/Tests/UniGLTF/MaterialTests.cs @@ -18,7 +18,7 @@ namespace UniGLTF filterMode = FilterMode.Bilinear, }; - var textureManager = new TextureExporter(AssetTextureUtil.UseAsset); + var textureManager = new TextureExporter(AssetTextureUtil.IsTextureEditorAsset ); var srcMaterial = new Material(Shader.Find("Standard")); var offset = new Vector2(0.3f, 0.2f); @@ -242,7 +242,7 @@ namespace UniGLTF material.SetColor("_EmissionColor", new Color(0, 1, 2, 1)); material.EnableKeyword("_EMISSION"); var materialExporter = new MaterialExporter(); - var textureExportManager = new TextureExporter(AssetTextureUtil.UseAsset); + var textureExportManager = new TextureExporter(AssetTextureUtil.IsTextureEditorAsset ); var gltfMaterial = materialExporter.ExportMaterial(material, textureExportManager); Assert.AreEqual(gltfMaterial.emissiveFactor, new float[] { 0, 0.5f, 1 }); diff --git a/Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs b/Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs index f18311fa1..9911887c2 100644 --- a/Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs +++ b/Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs @@ -15,7 +15,7 @@ namespace UniGLTF wrapMode = TextureWrapMode.Clamp, filterMode = FilterMode.Trilinear, }; - var textureManager = new TextureExporter(AssetTextureUtil.UseAsset); + var textureManager = new TextureExporter(AssetTextureUtil.IsTextureEditorAsset ); var material = new Material(Shader.Find("Standard")); material.mainTexture = tex0; diff --git a/Assets/UniGLTF/Tests/UniGLTF/UniGLTFTests.cs b/Assets/UniGLTF/Tests/UniGLTF/UniGLTFTests.cs index f50eb2131..e4d38cde1 100644 --- a/Assets/UniGLTF/Tests/UniGLTF/UniGLTFTests.cs +++ b/Assets/UniGLTF/Tests/UniGLTF/UniGLTFTests.cs @@ -106,7 +106,7 @@ namespace UniGLTF using (var exporter = new gltfExporter(gltf)) { exporter.Prepare(go); - exporter.Export(MeshExportSettings.Default, AssetTextureUtil.UseAsset); + exporter.Export(MeshExportSettings.Default, AssetTextureUtil.IsTextureEditorAsset ); // remove empty buffer gltf.buffers.Clear(); @@ -298,7 +298,7 @@ namespace UniGLTF using (var exporter = new gltfExporter(gltf)) { exporter.Prepare(CreateSimpleScene()); - exporter.Export(MeshExportSettings.Default, AssetTextureUtil.UseAsset); + exporter.Export(MeshExportSettings.Default, AssetTextureUtil.IsTextureEditorAsset ); } var expected = gltf.ToJson().ParseAsJson(); @@ -534,7 +534,7 @@ namespace UniGLTF using (var exporter = new gltfExporter(gltf)) { exporter.Prepare(go); - exporter.Export(UniGLTF.MeshExportSettings.Default, AssetTextureUtil.UseAsset); + exporter.Export(UniGLTF.MeshExportSettings.Default, AssetTextureUtil.IsTextureEditorAsset ); json = gltf.ToJson(); } diff --git a/Assets/VRM.Samples/Editor/Tests/VRMImportExportTests.cs b/Assets/VRM.Samples/Editor/Tests/VRMImportExportTests.cs index adb843443..e3e25f9a1 100644 --- a/Assets/VRM.Samples/Editor/Tests/VRMImportExportTests.cs +++ b/Assets/VRM.Samples/Editor/Tests/VRMImportExportTests.cs @@ -170,7 +170,7 @@ namespace VRM.Samples */ importedJson.RemoveValue(Utf8String.From("/bufferViews/*/byteStride")); - var vrm = VRMExporter.Export(UniGLTF.MeshExportSettings.Default, context.Root, AssetTextureUtil.UseAsset); + var vrm = VRMExporter.Export(UniGLTF.MeshExportSettings.Default, context.Root, AssetTextureUtil.IsTextureEditorAsset ); // TODO: Check contents in JSON /*var exportJson = */ diff --git a/Assets/VRM.Samples/Editor/Tests/VRMMaterialTests.cs b/Assets/VRM.Samples/Editor/Tests/VRMMaterialTests.cs index 7c98c6829..2f2a63438 100644 --- a/Assets/VRM.Samples/Editor/Tests/VRMMaterialTests.cs +++ b/Assets/VRM.Samples/Editor/Tests/VRMMaterialTests.cs @@ -11,7 +11,7 @@ namespace VRM.Samples { var material = Resources.Load(resourceName); var exporter = new VRMMaterialExporter(); - var textureManager = new TextureExporter(AssetTextureUtil.UseAsset); + var textureManager = new TextureExporter(AssetTextureUtil.IsTextureEditorAsset ); var exported = exporter.ExportMaterial(material, textureManager); // parse glTFExtensionExport to glTFExtensionImport diff --git a/Assets/VRM/Editor/Format/VRMEditorExporter.cs b/Assets/VRM/Editor/Format/VRMEditorExporter.cs index 4f4e02c46..14756193c 100644 --- a/Assets/VRM/Editor/Format/VRMEditorExporter.cs +++ b/Assets/VRM/Editor/Format/VRMEditorExporter.cs @@ -225,7 +225,7 @@ namespace VRM using (var exporter = new VRMExporter(gltf)) { exporter.Prepare(target); - exporter.Export(settings.MeshExportSettings, AssetTextureUtil.UseAsset); + exporter.Export(settings.MeshExportSettings, AssetTextureUtil.IsTextureEditorAsset ); } var bytes = gltf.ToGlbBytes(); File.WriteAllBytes(path, bytes); diff --git a/Assets/VRM/Tests/MToonTest.cs b/Assets/VRM/Tests/MToonTest.cs index 86d79422d..747e624b5 100644 --- a/Assets/VRM/Tests/MToonTest.cs +++ b/Assets/VRM/Tests/MToonTest.cs @@ -16,7 +16,7 @@ namespace VRM filterMode = FilterMode.Bilinear, }; - var textureManager = new TextureExporter(AssetTextureUtil.UseAsset); + var textureManager = new TextureExporter(AssetTextureUtil.IsTextureEditorAsset ); var srcMaterial = new Material(Shader.Find("VRM/MToon")); var offset = new Vector2(0.3f, 0.2f); diff --git a/Assets/VRM/Tests/VRMLoadTests.cs b/Assets/VRM/Tests/VRMLoadTests.cs index e1704c2cf..dbec0820a 100644 --- a/Assets/VRM/Tests/VRMLoadTests.cs +++ b/Assets/VRM/Tests/VRMLoadTests.cs @@ -106,7 +106,7 @@ namespace VRM try { // export - var vrm = VRMExporter.Export(UniGLTF.MeshExportSettings.Default, go, AssetTextureUtil.UseAsset); + var vrm = VRMExporter.Export(UniGLTF.MeshExportSettings.Default, go, AssetTextureUtil.IsTextureEditorAsset ); // re import if (vrm != null) diff --git a/Assets/VRMShaders/Editor/AssetTextureUtil.cs b/Assets/VRMShaders/Editor/AssetTextureUtil.cs index 2a608107b..a4423e84f 100644 --- a/Assets/VRMShaders/Editor/AssetTextureUtil.cs +++ b/Assets/VRMShaders/Editor/AssetTextureUtil.cs @@ -7,11 +7,11 @@ namespace VRMShaders public static class AssetTextureUtil { /// - /// TextureImporter.maxTextureSize が元のテクスチャーより小さいか否かの判定 + /// TextureImporter.maxTextureSize が オリジナルの画像Sizeより小さいか /// /// /// - public static bool CopyIfMaxTextureSizeIsSmaller(Texture src) + public static bool IsMaxTextureSizeSmallerThanOriginalTextureSize(Texture2D src) { var path = AssetDatabase.GetAssetPath(src); var textureImporter = AssetImporter.GetAtPath(path) as TextureImporter; @@ -35,21 +35,36 @@ namespace VRMShaders } /// - /// 元の Asset が存在して、 TextureImporter に設定された画像サイズが小さくない + /// Export するときに オリジナルのテクスチャーアセット(png/jpg)を使用するか否か。 + /// 条件は、 + /// + /// * TextureAsset が存在する + /// * TextureImporter の maxSize + /// /// /// /// /// - public static bool UseAsset(Texture texture) + public static bool IsTextureEditorAsset(Texture texture) { - if (texture != null && !string.IsNullOrEmpty(UnityEditor.AssetDatabase.GetAssetPath(texture))) + if (texture is Texture2D texture2D && !string.IsNullOrEmpty(UnityEditor.AssetDatabase.GetAssetPath(texture2D))) { - if (CopyIfMaxTextureSizeIsSmaller(texture)) + // exists Texture2D asset + if (IsMaxTextureSizeSmallerThanOriginalTextureSize(texture2D)) { + // Texture Inspector の MaxSize 設定で、テクスチャをオリジナルサイズよりも小さいサイズで Texture 化する指示を行っているため + // glTF Exporter もそれにしたがって、解釈をする + // + // 4096x4096 のような巨大なテクスチャーがそのまま出力されることを、Unityの TextureImporter.maxSize により防止する + // return false; } + + // use Texture2D asset. EncodeToPng return true; } + + // not Texture2D or not exists Texture2D asset. EncodeToPng return false; } } diff --git a/Assets/VRMShaders/Tests/MetallicRoughnessConverterTests.cs b/Assets/VRMShaders/Tests/MetallicRoughnessConverterTests.cs index caef40d25..a0cca736a 100644 --- a/Assets/VRMShaders/Tests/MetallicRoughnessConverterTests.cs +++ b/Assets/VRMShaders/Tests/MetallicRoughnessConverterTests.cs @@ -97,16 +97,16 @@ namespace VRMShaders var occlusion = new Texture2D(4, 4, TextureFormat.ARGB32, false, true); { - var exporter = new TextureExporter(AssetTextureUtil.UseAsset); + var exporter = new TextureExporter(AssetTextureUtil.IsTextureEditorAsset ); Assert.AreEqual(-1, exporter.ExportMetallicSmoothnessOcclusion(null, 0, null)); } { - var exporter = new TextureExporter(AssetTextureUtil.UseAsset); + var exporter = new TextureExporter(AssetTextureUtil.IsTextureEditorAsset ); Assert.AreEqual(0, exporter.ExportMetallicSmoothnessOcclusion(null, 0, occlusion)); Assert.AreEqual(1, exporter.ExportMetallicSmoothnessOcclusion(metallic, 0, null)); } { - var exporter = new TextureExporter(AssetTextureUtil.UseAsset); + var exporter = new TextureExporter(AssetTextureUtil.IsTextureEditorAsset ); Assert.AreEqual(0, exporter.ExportMetallicSmoothnessOcclusion(metallic, 0, occlusion)); Assert.AreEqual(0, exporter.ExportMetallicSmoothnessOcclusion(null, 0, occlusion)); Assert.AreEqual(0, exporter.ExportMetallicSmoothnessOcclusion(metallic, 0, null));