예제 #1
0
        public void UniGLTFSimpleSceneTest()
        {
            var go = CreateSimpleScene();

            // export
            var gltf = new glTF();

            string json = null;

            using (var exporter = new gltfExporter(gltf, new GltfExportSettings()))
            {
                exporter.Prepare(go);
                exporter.Export(new GltfExportSettings(), new EditorTextureSerializer());

                // remove empty buffer
                gltf.buffers.Clear();

                json = gltf.ToJson();
            }

            // parse
            var data = new JsonWithStorageParser(json).Parse();

            // import
            using (var context = new ImporterContext(data))
                using (var loaded = context.Load())
                {
                    AssertAreEqual(go.transform, loaded.transform);
                }
        }
예제 #2
0
        public void MeshHasNoRendererTest()
        {
            var go = new GameObject("mesh_has_no_renderer");

            try
            {
                {
                    var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
                    cube.transform.SetParent(go.transform);
                    UnityEngine.Object.DestroyImmediate(cube.GetComponent <MeshRenderer>());
                }

                // export
                var    gltf = new glTF();
                string json;
                using (var exporter = new gltfExporter(gltf, new GltfExportSettings()))
                {
                    exporter.Prepare(go);
                    exporter.Export(new UniGLTF.GltfExportSettings(), new EditorTextureSerializer());

                    json = gltf.ToJson();
                }

                Assert.AreEqual(0, gltf.meshes.Count);
                Assert.AreEqual(1, gltf.nodes.Count);
                Assert.AreEqual(-1, gltf.nodes[0].mesh);

                // import
                {
                    var storage = new SimpleStorage(new ArraySegment <byte>(new byte[1024 * 1024]));
                    var data    = new JsonWithStorageParser(json, storage).Parse();

                    using (var context = new ImporterContext(data))
                        using (var loaded = context.Load())
                        {
                            Assert.AreEqual(1, loaded.transform.GetChildren().Count());
                            {
                                var child = loaded.transform.GetChild(0);
                                Assert.IsNull(child.GetSharedMesh());
                            }
                        }
                }
            }
            finally
            {
                GameObject.DestroyImmediate(go);
            }
        }
예제 #3
0
        public void ExportingNullMeshTest()
        {
            var validator = ScriptableObject.CreateInstance <MeshExportValidator>();
            var root      = new GameObject("root");

            try
            {
                {
                    var child = GameObject.CreatePrimitive(PrimitiveType.Cube);
                    child.transform.SetParent(root.transform);
                    // remove MeshFilter
                    Component.DestroyImmediate(child.GetComponent <MeshFilter>());
                }

                {
                    var child = GameObject.CreatePrimitive(PrimitiveType.Cube);
                    child.transform.SetParent(root.transform);
                    // set null
                    child.GetComponent <MeshFilter>().sharedMesh = null;
                }

                // validate
                validator.SetRoot(root, new GltfExportSettings(), new DefualtBlendShapeExportFilter());
                var vs = validator.Validate(root);
                Assert.True(vs.All(x => x.CanExport));

                // export
                var    gltf = new glTF();
                string json;
                using (var exporter = new gltfExporter(gltf, new GltfExportSettings()))
                {
                    exporter.Prepare(root);
                    exporter.Export(new UniGLTF.GltfExportSettings(), new EditorTextureSerializer());

                    json = gltf.ToJson();
                }

                Assert.AreEqual(0, gltf.meshes.Count);
                Assert.AreEqual(2, gltf.nodes.Count);
                Assert.AreEqual(-1, gltf.nodes[0].mesh);
                Assert.AreEqual(-1, gltf.nodes[1].mesh);

                // import
                {
                    var storage = new SimpleStorage(new ArraySegment <byte>(new byte[1024 * 1024]));
                    var data    = new JsonWithStorageParser(json, storage).Parse();

                    using (var context = new ImporterContext(data))
                        using (var loaded = context.Load())
                        {
                            Assert.AreEqual(2, loaded.transform.GetChildren().Count());

                            {
                                var child = loaded.transform.GetChild(0);
                                Assert.IsNull(child.GetSharedMesh());
                            }

                            {
                                var child = loaded.transform.GetChild(1);
                                Assert.IsNull(child.GetSharedMesh());
                            }
                        }
                }
            }
            finally
            {
                GameObject.DestroyImmediate(root);
                ScriptableObject.DestroyImmediate(validator);
            }
        }
예제 #4
0
        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.name  = "red";
                    material.color = Color.red;
                    cubeA.GetComponent <Renderer>().sharedMaterial = material;
                }

                {
                    var cubeB = GameObject.Instantiate(cubeA);
                    cubeB.transform.SetParent(go.transform);
                    var material = new Material(shader);
                    material.color = Color.blue;
                    material.name  = "blue";
                    cubeB.GetComponent <Renderer>().sharedMaterial = material;

                    Assert.AreEqual(cubeB.GetComponent <MeshFilter>().sharedMesh, cubeA.GetComponent <MeshFilter>().sharedMesh);
                }

                // export
                var gltf = new glTF();
                var json = default(string);
                using (var exporter = new gltfExporter(gltf, new GltfExportSettings()))
                {
                    exporter.Prepare(go);
                    exporter.Export(new UniGLTF.GltfExportSettings(), new EditorTextureSerializer());

                    json = gltf.ToJson();
                }

                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 }, blue.pbrMetallicRoughness.baseColorFactor);

                Assert.AreEqual(2, gltf.nodes.Count);

                Assert.AreNotEqual(gltf.nodes[0].mesh, gltf.nodes[1].mesh);

                // import
                {
                    var storage = new SimpleStorage(new ArraySegment <byte>(new byte[1024 * 1024]));
                    var data    = new JsonWithStorageParser(json, storage).Parse();

                    using (var context = new ImporterContext(data))
                        using (var loaded = context.Load())
                        {
                            var importedRed         = loaded.transform.GetChild(0);
                            var importedRedMaterial = importedRed.GetComponent <Renderer>().sharedMaterial;
                            Assert.AreEqual("red", importedRedMaterial.name);
                            Assert.AreEqual(Color.red, importedRedMaterial.color);

                            var importedBlue         = loaded.transform.GetChild(1);
                            var importedBlueMaterial = importedBlue.GetComponent <Renderer>().sharedMaterial;
                            Assert.AreEqual("blue", importedBlueMaterial.name);
                            Assert.AreEqual(Color.blue, importedBlueMaterial.color);
                        }
                }

                // import new version
                {
                    var storage = new SimpleStorage(new ArraySegment <byte>(new byte[1024 * 1024]));
                    var data    = new JsonWithStorageParser(json, storage).Parse();

                    //Debug.LogFormat("{0}", context.Json);
                    using (var context = new ImporterContext(data))
                        using (var loaded = context.Load())
                        {
                            var importedRed         = loaded.transform.GetChild(0);
                            var importedRedMaterial = importedRed.GetComponent <Renderer>().sharedMaterial;
                            Assert.AreEqual("red", importedRedMaterial.name);
                            Assert.AreEqual(Color.red, importedRedMaterial.color);

                            var importedBlue         = loaded.transform.GetChild(1);
                            var importedBlueMaterial = importedBlue.GetComponent <Renderer>().sharedMaterial;
                            Assert.AreEqual("blue", importedBlueMaterial.name);
                            Assert.AreEqual(Color.blue, importedBlueMaterial.color);
                        }
                }
            }
            finally
            {
                GameObject.DestroyImmediate(go);
            }
        }