public void UniGLTFSimpleSceneTest()
        {
            var go = CreateSimpleScene();

            // export
            var gltf = new glTF();

            string json = null;

            using (var exporter = new gltfExporter(gltf))
            {
                exporter.Prepare(go);
                exporter.Export(MeshExportSettings.Default, AssetTextureUtil.IsTextureEditorAsset, AssetTextureUtil.GetTextureBytesWithMime);

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

                json = gltf.ToJson();
            }

            // parse
            var parser = new GltfParser();

            parser.ParseJson(json, new SimpleStorage(new ArraySegment <byte>()));

            // import
            using (var context = new ImporterContext(parser))
            {
                context.Load();
                AssertAreEqual(go.transform, context.Root.transform);
            }
        }
Esempio n. 2
0
        public void UniGLTFSimpleSceneTest()
        {
            var go      = CreateSimpelScene();
            var context = new ImporterContext();

            try
            {
                // export
                var gltf = new glTF();
                using (var exporter = new gltfExporter(gltf))
                {
                    exporter.Prepare(go);
                    exporter.Export();

                    // import
                    context.ParseJson(gltf.ToJson(), new SimpleStorage(new ArraySegment <byte>()));
                    //Debug.LogFormat("{0}", context.Json);
                    gltfImporter.Load(context);

                    AssertAreEqual(go.transform, context.Root.transform);
                }
            }
            finally
            {
                //Debug.LogFormat("Destory, {0}", go.name);
                GameObject.DestroyImmediate(go);
                context.Destroy(true);
            }
        }
Esempio n. 3
0
        public void WriteTo(string path)
        {
            var buffer = glTF.buffers[0];

            var json = glTF.ToJson();

            using (var s = new FileStream(path, FileMode.Create))
            {
                GlbHeader.WriteTo(s);

                var pos = s.Position;
                s.Position += 4; // skip total size

                int size = 12;

                {
                    var chunk = new GlbChunk(json);
                    size += chunk.WriteTo(s);
                }
                {
                    var chunk = new GlbChunk(buffer.GetBytes());
                    size += chunk.WriteTo(s);
                }

                s.Position = pos;
                var bytes = BitConverter.GetBytes(size);
                s.Write(bytes, 0, bytes.Length);
            }

            Debug.Log(json);
        }
Esempio n. 4
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);
                }
        }
Esempio n. 5
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))
                {
                    exporter.Prepare(go);
                    exporter.Export(UniGLTF.MeshExportSettings.Default, AssetTextureUtil.IsTextureEditorAsset, AssetTextureUtil.GetTextureBytesWithMime);

                    json = gltf.ToJson();
                }

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

                // import
                {
                    var parser = new GltfParser();
                    parser.ParseJson(json, new SimpleStorage(new ArraySegment <byte>(new byte[1024 * 1024])));

                    using (var context = new ImporterContext(parser))
                    {
                        context.Load();

                        Assert.AreEqual(1, context.Root.transform.GetChildren().Count());

                        {
                            var child = context.Root.transform.GetChild(0);
                            Assert.IsNull(child.GetSharedMesh());
                        }
                    }
                }
            }
            finally
            {
                GameObject.DestroyImmediate(go);
            }
        }
Esempio n. 6
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);
            }
        }
Esempio n. 7
0
        public void UniGLTFSimpleSceneTest()
        {
            var             go      = CreateSimpleScene();
            ImporterContext context = default;

            try
            {
                // export
                var gltf = new glTF();

                string json = null;
                using (var exporter = new gltfExporter(gltf))
                {
                    exporter.Prepare(go);
                    exporter.Export(MeshExportSettings.Default);

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

                    json = gltf.ToJson();
                }

                // parse
                var parser = new GltfParser();
                parser.ParseJson(json, new SimpleStorage(new ArraySegment <byte>()));

                // import
                context = new ImporterContext(parser);
                context.Load();

                AssertAreEqual(go.transform, context.Root.transform);
            }
            finally
            {
                //Debug.LogFormat("Destroy, {0}", go.name);
                GameObject.DestroyImmediate(go);
                if (context != null)
                {
                    var editor = new EditorImporterContext(context);
                    editor.EditorDestroyRootAndAssets();
                }
            }
        }
Esempio n. 8
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))
                {
                    exporter.Prepare(go);
                    exporter.Export();

                    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 context = new ImporterContext();
                    context.ParseJson(json, new SimpleStorage(new ArraySegment <byte>(new byte[1024 * 1024])));
                    //Debug.LogFormat("{0}", context.Json);
                    context.Load();

                    var importedRed         = context.Root.transform.GetChild(0);
                    var importedRedMaterial = importedRed.GetComponent <Renderer>().sharedMaterial;
                    Assert.AreEqual("red", importedRedMaterial.name);
                    Assert.AreEqual(Color.red, importedRedMaterial.color);

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

                // import new version
                {
                    var context = new ImporterContext
                    {
                        UseUniJSONParser = true
                    };
                    context.ParseJson(json, new SimpleStorage(new ArraySegment <byte>(new byte[1024 * 1024])));
                    //Debug.LogFormat("{0}", context.Json);
                    context.Load();

                    var importedRed         = context.Root.transform.GetChild(0);
                    var importedRedMaterial = importedRed.GetComponent <Renderer>().sharedMaterial;
                    Assert.AreEqual("red", importedRedMaterial.name);
                    Assert.AreEqual(Color.red, importedRedMaterial.color);

                    var importedBlue         = context.Root.transform.GetChild(1);
                    var importedBlueMaterial = importedBlue.GetComponent <Renderer>().sharedMaterial;
                    Assert.AreEqual("blue", importedBlueMaterial.name);
                    Assert.AreEqual(Color.blue, importedBlueMaterial.color);
                }
            }
            finally
            {
                GameObject.DestroyImmediate(go);
            }
        }
Esempio n. 9
0
        public void GlTFToJsonTest()
        {
            var gltf = new glTF();

            using (var exporter = new gltfExporter(gltf))
            {
                exporter.Prepare(CreateSimpleScene());
                exporter.Export();
            }

            var expected = gltf.ToJson().ParseAsJson();

            expected.AddKey(Utf8String.From("meshes"));
            expected.AddValue(default(ArraySegment <byte>), ValueNodeType.Array);
            expected["meshes"].AddValue(default(ArraySegment <byte>), ValueNodeType.Object);

            var mesh = expected["meshes"][0];

            mesh.AddKey(Utf8String.From("name"));
            mesh.AddValue(Utf8String.From(JsonString.Quote("test")).Bytes, ValueNodeType.String);
            mesh.AddKey(Utf8String.From("primitives"));
            mesh.AddValue(default(ArraySegment <byte>), ValueNodeType.Array);
            mesh["primitives"].AddValue(default(ArraySegment <byte>), ValueNodeType.Object);

            var primitive = mesh["primitives"][0];

            primitive.AddKey(Utf8String.From("mode"));
            primitive.AddValue(Utf8String.From("0").Bytes, ValueNodeType.Integer);
            primitive.AddKey(Utf8String.From("indices"));
            primitive.AddValue(Utf8String.From("0").Bytes, ValueNodeType.Integer);
            primitive.AddKey(Utf8String.From("material"));
            primitive.AddValue(Utf8String.From("0").Bytes, ValueNodeType.Integer);
            primitive.AddKey(Utf8String.From("attributes"));
            primitive.AddValue(default(ArraySegment <byte>), ValueNodeType.Object);
            primitive["attributes"].AddKey(Utf8String.From("POSITION"));
            primitive["attributes"].AddValue(Utf8String.From("0").Bytes, ValueNodeType.Integer);
            primitive.AddKey(Utf8String.From("targets"));
            primitive.AddValue(default(ArraySegment <byte>), ValueNodeType.Array);
            primitive["targets"].AddValue(default(ArraySegment <byte>), ValueNodeType.Object);
            primitive["targets"][0].AddKey(Utf8String.From("POSITION"));
            primitive["targets"][0].AddValue(Utf8String.From("1").Bytes, ValueNodeType.Integer);
            primitive["targets"].AddValue(default(ArraySegment <byte>), ValueNodeType.Object);
            primitive["targets"][1].AddKey(Utf8String.From("POSITION"));
            primitive["targets"][1].AddValue(Utf8String.From("2").Bytes, ValueNodeType.Integer);
            primitive["targets"][1].AddKey(Utf8String.From("TANGENT"));
            primitive["targets"][1].AddValue(Utf8String.From("0").Bytes, ValueNodeType.Integer);

            gltf.meshes.Add(new glTFMesh("test")
            {
                primitives = new List <glTFPrimitives>
                {
                    new glTFPrimitives
                    {
                        indices    = 0,
                        attributes = new glTFAttributes
                        {
                            POSITION = 0,
                            TANGENT  = -1 // should be removed
                        },
                        targets = new List <gltfMorphTarget>
                        {
                            new gltfMorphTarget
                            {
                                POSITION = 1,
                                TANGENT  = -1 // should be removed
                            },
                            new gltfMorphTarget
                            {
                                POSITION = 2,
                                TANGENT  = 0
                            }
                        }
                    }
                }
            });
            var actual = gltf.ToJson().ParseAsJson();

            Assert.AreEqual(expected, actual);
        }
Esempio n. 10
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);
            }
        }