From 36b130a8ce30e578cc67c42e25c1dbea0b23014e Mon Sep 17 00:00:00 2001 From: ousttrue Date: Sat, 26 Jan 2019 15:28:15 +0900 Subject: [PATCH 1/4] Add Test for #140 --- Assets/VRM/UniGLTF/Editor/UniGLTFTests.cs | 54 +++++++++++++++++++++-- 1 file changed, 51 insertions(+), 3 deletions(-) diff --git a/Assets/VRM/UniGLTF/Editor/UniGLTFTests.cs b/Assets/VRM/UniGLTF/Editor/UniGLTFTests.cs index 5df2d0a1d..ba9550f84 100644 --- a/Assets/VRM/UniGLTF/Editor/UniGLTFTests.cs +++ b/Assets/VRM/UniGLTF/Editor/UniGLTFTests.cs @@ -461,7 +461,7 @@ namespace UniGLTF var model = new glTFSkin() { name = "b", - joints = new int[] {1}, + joints = new int[] { 1 }, }; var json = model.ToJson(); @@ -482,7 +482,7 @@ namespace UniGLTF var model = new glTFSkin() { name = "", - joints = new int[] {1}, + joints = new int[] { 1 }, }; var json = model.ToJson(); @@ -523,7 +523,7 @@ namespace UniGLTF var model = new glTFSkin() { name = "b", - joints = new int[] {}, + joints = new int[] { }, }; var c = new JsonSchemaValidationContext("") @@ -574,5 +574,53 @@ namespace UniGLTF ); Assert.AreEqual("[version.String] null", ex.Message); } + + [Test] + public void SameMeshButDifferentMaterialExport() + { + var go = new GameObject("same_mesh"); + try + { + var shader = Shader.Find("Unlit/Color"); + + var cubeA = GameObject.CreatePrimitive(PrimitiveType.Cube); + { + cubeA.transform.SetParent(go.transform); + var material = new Material(shader); + material.color = Color.red; + cubeA.GetComponent().sharedMaterial = material; + } + + { + var cubeB = GameObject.Instantiate(cubeA); + cubeB.transform.SetParent(go.transform); + var material = new Material(shader); + material.color = Color.blue; + cubeB.GetComponent().sharedMaterial = material; + + Assert.AreEqual(cubeB.GetComponent().sharedMesh, cubeA.GetComponent().sharedMesh); + } + + // export + var gltf = new glTF(); + using (var exporter = new gltfExporter(gltf)) + { + exporter.Prepare(go); + exporter.Export(); + } + + Assert.AreEqual(2, gltf.meshes.Count); + + var red = gltf.materials[gltf.meshes[0].primitives[0].material]; + Assert.AreEqual(new float[] { 1, 0, 0, 1 }, red.pbrMetallicRoughness.baseColorFactor); + + var blue = gltf.materials[gltf.meshes[1].primitives[0].material]; + Assert.AreEqual(new float[] { 0, 0, 1, 1 }, red.pbrMetallicRoughness.baseColorFactor); + } + finally + { + GameObject.DestroyImmediate(go); + } + } } } From f7888de0baf71e6f225b8254de320666a68f4381 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Sat, 26 Jan 2019 15:42:13 +0900 Subject: [PATCH 2/4] Fixed test #140 --- Assets/VRM/UniGLTF/Editor/UniGLTFTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Assets/VRM/UniGLTF/Editor/UniGLTFTests.cs b/Assets/VRM/UniGLTF/Editor/UniGLTFTests.cs index ba9550f84..d2a0e7dc3 100644 --- a/Assets/VRM/UniGLTF/Editor/UniGLTFTests.cs +++ b/Assets/VRM/UniGLTF/Editor/UniGLTFTests.cs @@ -615,7 +615,7 @@ namespace UniGLTF Assert.AreEqual(new float[] { 1, 0, 0, 1 }, red.pbrMetallicRoughness.baseColorFactor); var blue = gltf.materials[gltf.meshes[1].primitives[0].material]; - Assert.AreEqual(new float[] { 0, 0, 1, 1 }, red.pbrMetallicRoughness.baseColorFactor); + Assert.AreEqual(new float[] { 0, 0, 1, 1 }, blue.pbrMetallicRoughness.baseColorFactor); } finally { From dc1e1dab98a9567d476d4f7a7f121eb909acf04a Mon Sep 17 00:00:00 2001 From: ousttrue Date: Sat, 26 Jan 2019 15:56:25 +0900 Subject: [PATCH 3/4] Add test for #140 --- Assets/VRM/UniGLTF/Editor/UniGLTFTests.cs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Assets/VRM/UniGLTF/Editor/UniGLTFTests.cs b/Assets/VRM/UniGLTF/Editor/UniGLTFTests.cs index d2a0e7dc3..3ed2ce06f 100644 --- a/Assets/VRM/UniGLTF/Editor/UniGLTFTests.cs +++ b/Assets/VRM/UniGLTF/Editor/UniGLTFTests.cs @@ -603,10 +603,13 @@ namespace UniGLTF // export var gltf = new glTF(); + var json = default(string); using (var exporter = new gltfExporter(gltf)) { exporter.Prepare(go); exporter.Export(); + + json = gltf.ToJson(); } Assert.AreEqual(2, gltf.meshes.Count); @@ -616,6 +619,18 @@ namespace UniGLTF var blue = gltf.materials[gltf.meshes[1].primitives[0].material]; Assert.AreEqual(new float[] { 0, 0, 1, 1 }, blue.pbrMetallicRoughness.baseColorFactor); + + // import + var context = new ImporterContext(); + context.ParseJson(json, new SimpleStorage(new ArraySegment(new byte[1024 * 1024]))); + //Debug.LogFormat("{0}", context.Json); + context.Load(); + + var importedRed = context.Root.transform.GetChild(0); + Assert.AreEqual(Color.red, importedRed.GetComponent().sharedMaterial.color); + + var importedBlue = context.Root.transform.GetChild(1); + Assert.AreEqual(Color.blue, importedBlue.GetComponent().sharedMaterial.color); } finally { From 1ccef789c4fffe156cd2cb84a4dab8444cf25a7e Mon Sep 17 00:00:00 2001 From: ousttrue Date: Sat, 26 Jan 2019 16:12:10 +0900 Subject: [PATCH 4/4] Fix exporter logic that getting node.mesh index #140 --- Assets/VRM/UniGLTF/Editor/UniGLTFTests.cs | 14 ++++++++++++-- Assets/VRM/UniGLTF/Scripts/IO/gltfExporter.cs | 14 +++++++------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/Assets/VRM/UniGLTF/Editor/UniGLTFTests.cs b/Assets/VRM/UniGLTF/Editor/UniGLTFTests.cs index 3ed2ce06f..a2b9de381 100644 --- a/Assets/VRM/UniGLTF/Editor/UniGLTFTests.cs +++ b/Assets/VRM/UniGLTF/Editor/UniGLTFTests.cs @@ -587,6 +587,7 @@ namespace UniGLTF { cubeA.transform.SetParent(go.transform); var material = new Material(shader); + material.name = "red"; material.color = Color.red; cubeA.GetComponent().sharedMaterial = material; } @@ -596,6 +597,7 @@ namespace UniGLTF cubeB.transform.SetParent(go.transform); var material = new Material(shader); material.color = Color.blue; + material.name = "blue"; cubeB.GetComponent().sharedMaterial = material; Assert.AreEqual(cubeB.GetComponent().sharedMesh, cubeA.GetComponent().sharedMesh); @@ -620,6 +622,10 @@ namespace UniGLTF var blue = gltf.materials[gltf.meshes[1].primitives[0].material]; Assert.AreEqual(new float[] { 0, 0, 1, 1 }, blue.pbrMetallicRoughness.baseColorFactor); + Assert.AreEqual(2, gltf.nodes.Count); + + Assert.AreNotEqual(gltf.nodes[0].mesh, gltf.nodes[1].mesh); + // import var context = new ImporterContext(); context.ParseJson(json, new SimpleStorage(new ArraySegment(new byte[1024 * 1024]))); @@ -627,10 +633,14 @@ namespace UniGLTF context.Load(); var importedRed = context.Root.transform.GetChild(0); - Assert.AreEqual(Color.red, importedRed.GetComponent().sharedMaterial.color); + var importedRedMaterial = importedRed.GetComponent().sharedMaterial; + Assert.AreEqual("red", importedRedMaterial.name); + Assert.AreEqual(Color.red, importedRedMaterial.color); var importedBlue = context.Root.transform.GetChild(1); - Assert.AreEqual(Color.blue, importedBlue.GetComponent().sharedMaterial.color); + var importedBlueMaterial = importedBlue.GetComponent().sharedMaterial; + Assert.AreEqual("blue", importedBlueMaterial.name); + Assert.AreEqual(Color.blue, importedBlueMaterial.color); } finally { diff --git a/Assets/VRM/UniGLTF/Scripts/IO/gltfExporter.cs b/Assets/VRM/UniGLTF/Scripts/IO/gltfExporter.cs index ed6d0f396..9a955dd81 100644 --- a/Assets/VRM/UniGLTF/Scripts/IO/gltfExporter.cs +++ b/Assets/VRM/UniGLTF/Scripts/IO/gltfExporter.cs @@ -151,7 +151,7 @@ namespace UniGLTF } #region Export - static glTFNode ExportNode(Transform x, List nodes, List meshes, List skins) + static glTFNode ExportNode(Transform x, List nodes, List renderers, List skins) { var node = new glTFNode { @@ -164,16 +164,16 @@ namespace UniGLTF if (x.gameObject.activeInHierarchy) { - var meshFilter = x.GetComponent(); - if (meshFilter != null) + var meshRenderer = x.GetComponent(); + if (meshRenderer != null) { - node.mesh = meshes.IndexOf(meshFilter.sharedMesh); + node.mesh = renderers.IndexOf(meshRenderer); } var skinnredMeshRenderer = x.GetComponent(); if (skinnredMeshRenderer != null) { - node.mesh = meshes.IndexOf(skinnredMeshRenderer.sharedMesh); + node.mesh = renderers.IndexOf(skinnredMeshRenderer); node.skin = skins.IndexOf(skinnredMeshRenderer); } } @@ -244,14 +244,14 @@ namespace UniGLTF Meshes = unityMeshes.Select(x => x.Mesh).ToList(); #endregion - #region Skins + #region Nodes and Skins var unitySkins = Nodes .Select(x => x.GetComponent()).Where(x => x != null && x.bones != null && x.bones.Length > 0) .ToList(); - gltf.nodes = Nodes.Select(x => ExportNode(x, Nodes, unityMeshes.Select(y => y.Mesh).ToList(), unitySkins)).ToList(); + gltf.nodes = Nodes.Select(x => ExportNode(x, Nodes, unityMeshes.Select(y => y.Rendererer).ToList(), unitySkins)).ToList(); gltf.scenes = new List { new gltfScene