Exemplo n.º 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);
                }
        }
Exemplo n.º 2
0
        protected override void ExportPath(string path)
        {
            var ext   = Path.GetExtension(path).ToLower();
            var isGlb = false;

            switch (ext)
            {
            case ".glb": isGlb = true; break;

            case ".gltf": isGlb = false; break;

            default: throw new System.Exception();
            }

            var progress = 0;

            EditorUtility.DisplayProgressBar("export gltf", path, progress);
            try
            {
                var data = new ExportingGltfData();
                using (var exporter = new gltfExporter(data, Settings, new EditorProgress()))
                {
                    exporter.Prepare(State.ExportRoot);
                    exporter.Export(new EditorTextureSerializer());
                }

                if (isGlb)
                {
                    var bytes = data.ToGlbBytes();
                    File.WriteAllBytes(path, bytes);
                }
                else
                {
                    var(json, buffer0) = data.ToGltf(path);

                    {
                        // write JSON without BOM
                        var encoding = new System.Text.UTF8Encoding(false);
                        File.WriteAllText(path, json, encoding);
                    }

                    {
                        // write to buffer0 local folder
                        var dir        = Path.GetDirectoryName(path);
                        var bufferPath = Path.Combine(dir, buffer0.uri);
                        File.WriteAllBytes(bufferPath, data.BinBytes.ToArray());
                    }
                }

                if (path.StartsWithUnityAssetPath())
                {
                    AssetDatabase.ImportAsset(path.ToUnityRelativePath());
                    AssetDatabase.Refresh();
                }
            }
            finally
            {
                EditorUtility.ClearProgressBar();
            }
        }
Exemplo n.º 3
0
        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);

                // 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);
            }
        }
Exemplo n.º 4
0
        private static void ExportFromMenu()
        {
            var go   = Selection.activeObject as GameObject;
            var path = EditorUtility.SaveFilePanel(
                "Save glb",
                "",
                go.name + ".glb",
                "glb");

            if (string.IsNullOrEmpty(path))
            {
                return;
            }

            var gltf = new glTF();

            using (var exporter = new gltfExporter(gltf))
            {
                exporter.Prepare(go);
                exporter.Export();
            }
            var bytes = gltf.ToGlbBytes();

            File.WriteAllBytes(path, bytes);

            if (path.StartsWithUnityAssetPath())
            {
                AssetDatabase.ImportAsset(path.ToUnityRelativePath());
                AssetDatabase.Refresh();
            }
        }
Exemplo n.º 5
0
        private static void ExportFromMenu()
        {
            var go = Selection.activeObject as GameObject;

            if (go.transform.position == Vector3.zero &&
                go.transform.rotation == Quaternion.identity &&
                go.transform.localScale == Vector3.one)
            {
                var path = EditorUtility.SaveFilePanel(
                    "Save glb", "", go.name + ".glb", "glb");
                if (string.IsNullOrEmpty(path))
                {
                    return;
                }

                var gltf = new glTF();
                using (var exporter = new gltfExporter(gltf))
                {
                    exporter.Prepare(go);
                    exporter.Export();
                }
                var bytes = gltf.ToGlbBytes();
                File.WriteAllBytes(path, bytes);

                if (path.StartsWithUnityAssetPath())
                {
                    AssetDatabase.ImportAsset(path.ToUnityRelativePath());
                    AssetDatabase.Refresh();
                }
            }
            else
            {
                EditorUtility.DisplayDialog("Error", "The Root transform should have Default translation, rotation and scale.", "ok");
            }
        }
Exemplo n.º 6
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);
            }
        }
Exemplo n.º 7
0
        public void UniGLTFSimpleSceneTest()
        {
            var go = CreateSimpleScene();

            // export
            var data = new ExportingGltfData();

            string json = null;

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

                // remove empty buffer
                data.GLTF.buffers.Clear();

                json = data.GLTF.ToJson();
            }

            // parse
            using (var parsed = GltfData.CreateFromExportForTest(data))
                using (var context = new ImporterContext(parsed))
                    using (var loaded = context.Load())
                    {
                        AssertAreEqual(go.transform, loaded.transform);
                    }
        }
Exemplo n.º 8
0
        private static void ExportFromMenu(bool isGlb, MeshExportSettings settings)
        {
            var go = Selection.activeObject as GameObject;

            var ext = isGlb ? "glb" : "gltf";

            if (go.transform.position == Vector3.zero &&
                go.transform.rotation == Quaternion.identity &&
                go.transform.localScale == Vector3.one)
            {
                var path = EditorUtility.SaveFilePanel(
                    $"Save {ext}", "", go.name + $".{ext}", $"{ext}");
                if (string.IsNullOrEmpty(path))
                {
                    return;
                }

                var gltf = new glTF();
                using (var exporter = new gltfExporter(gltf))
                {
                    exporter.Prepare(go);
                    exporter.Export(settings);
                }

                if (isGlb)
                {
                    var bytes = gltf.ToGlbBytes();
                    File.WriteAllBytes(path, bytes);
                }
                else
                {
                    var(json, buffers) = gltf.ToGltf(path);
                    // without BOM
                    var encoding = new System.Text.UTF8Encoding(false);
                    File.WriteAllText(path, json, encoding);
                    // write to local folder
                    var dir = Path.GetDirectoryName(path);
                    foreach (var b in buffers)
                    {
                        var bufferPath = Path.Combine(dir, b.uri);
                        File.WriteAllBytes(bufferPath, b.GetBytes().ToArray());
                    }
                }

                if (path.StartsWithUnityAssetPath())
                {
                    AssetDatabase.ImportAsset(path.ToUnityRelativePath());
                    AssetDatabase.Refresh();
                }
            }
            else
            {
                EditorUtility.DisplayDialog("Error", "The Root transform should have Default translation, rotation and scale.", "ok");
            }
        }
Exemplo n.º 9
0
        protected override void ExportPath(string path)
        {
            var ext   = Path.GetExtension(path).ToLower();
            var isGlb = false;

            switch (ext)
            {
            case ".glb": isGlb = true; break;

            case ".gltf": isGlb = false; break;

            default: throw new System.Exception();
            }

            var gltf = new glTF();

            using (var exporter = new gltfExporter(gltf, m_settings.InverseAxis))
            {
                exporter.Prepare(State.ExportRoot);
                var settings = new MeshExportSettings
                {
                    ExportOnlyBlendShapePosition    = m_settings.DropNormal,
                    UseSparseAccessorForMorphTarget = m_settings.Sparse,
                    DivideVertexBuffer = m_settings.DivideVertexBuffer,
                };
                exporter.Export(settings, new EditorTextureSerializer());
            }

            if (isGlb)
            {
                var bytes = gltf.ToGlbBytes();
                File.WriteAllBytes(path, bytes);
            }
            else
            {
                var(json, buffers) = gltf.ToGltf(path);
                // without BOM
                var encoding = new System.Text.UTF8Encoding(false);
                File.WriteAllText(path, json, encoding);
                // write to local folder
                var dir = Path.GetDirectoryName(path);
                foreach (var b in buffers)
                {
                    var bufferPath = Path.Combine(dir, b.uri);
                    File.WriteAllBytes(bufferPath, b.GetBytes().ToArray());
                }
            }

            if (path.StartsWithUnityAssetPath())
            {
                AssetDatabase.ImportAsset(path.ToUnityRelativePath());
                AssetDatabase.Refresh();
            }
        }
Exemplo n.º 10
0
        public static glTF Export(GameObject go)
        {
            var gltf = new glTF();

            using (var exporter = new gltfExporter(gltf))
            {
                exporter.Prepare(go);
                exporter.Export();
            }
            return(gltf);
        }
Exemplo n.º 11
0
        static Byte[] Export(GameObject root)
        {
            var gltf = new glTF();

            using (var exporter = new gltfExporter(gltf))
            {
                exporter.Prepare(root);
                exporter.Export(MeshExportSettings.Default, AssetTextureUtil.IsTextureEditorAsset, AssetTextureUtil.GetTextureBytesWithMime);
                return(gltf.ToGlbBytes());
            }
        }
Exemplo n.º 12
0
        static Byte[] Export(GameObject root)
        {
            var data = new ExportingGltfData();

            using (var exporter = new gltfExporter(data, new GltfExportSettings()))
            {
                exporter.Prepare(root);
                exporter.Export(new EditorTextureSerializer());
            }
            return(data.ToGlbBytes());
        }
Exemplo n.º 13
0
        static Byte[] Export(GameObject root)
        {
            var gltf = new glTF();

            using (var exporter = new gltfExporter(gltf, new GltfExportSettings()))
            {
                exporter.Prepare(root);
                exporter.Export(new GltfExportSettings(), new EditorTextureSerializer());
                return(gltf.ToGlbBytes());
            }
        }
Exemplo n.º 14
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);
            }
        }
Exemplo n.º 15
0
        private static void Export(GameObject go, string path, MeshExportSettings settings, Axises inverseAxis)
        {
            var ext   = Path.GetExtension(path).ToLower();
            var isGlb = false;

            switch (ext)
            {
            case ".glb": isGlb = true; break;

            case ".gltf": isGlb = false; break;

            default: throw new System.Exception();
            }

            var gltf = new glTF();

            using (var exporter = new gltfExporter(gltf, inverseAxis))
            {
                exporter.Prepare(go);
                exporter.Export(settings, AssetTextureUtil.IsTextureEditorAsset);
            }


            if (isGlb)
            {
                var bytes = gltf.ToGlbBytes();
                File.WriteAllBytes(path, bytes);
            }
            else
            {
                var(json, buffers) = gltf.ToGltf(path);
                // without BOM
                var encoding = new System.Text.UTF8Encoding(false);
                File.WriteAllText(path, json, encoding);
                // write to local folder
                var dir = Path.GetDirectoryName(path);
                foreach (var b in buffers)
                {
                    var bufferPath = Path.Combine(dir, b.uri);
                    File.WriteAllBytes(bufferPath, b.GetBytes().ToArray());
                }
            }

            if (path.StartsWithUnityAssetPath())
            {
                AssetDatabase.ImportAsset(path.ToUnityRelativePath());
                AssetDatabase.Refresh();
            }
        }
Exemplo n.º 16
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);
            }
        }
Exemplo n.º 17
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    data = new ExportingGltfData();
                var    gltf = data.GLTF;
                string json;
                using (var exporter = new gltfExporter(data, new GltfExportSettings()))
                {
                    exporter.Prepare(go);
                    exporter.Export(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 parsed = GltfData.CreateFromExportForTest(data);
                    using (var context = new ImporterContext(parsed))
                        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);
            }
        }
        private static Texture2D AssignTextureToMaterialPropertyAndExportAndExtract(Texture2D srcTex, string srcImageName, string propertyName)
        {
            // Prepare
            var root = GameObject.CreatePrimitive(PrimitiveType.Cube);
            var mat  = new Material(Shader.Find("Standard"));

            mat.SetTexture(propertyName, srcTex);
            root.GetComponent <MeshRenderer>().sharedMaterial = mat;

            // Export glTF
            var data = new ExportingGltfData();

            using (var exporter = new gltfExporter(data, new GltfExportSettings
            {
                InverseAxis = Axes.X,
                ExportOnlyBlendShapePosition = false,
                UseSparseAccessorForMorphTarget = false,
                DivideVertexBuffer = false,
            }))
            {
                exporter.Prepare(root);
                exporter.Export(new EditorTextureSerializer());
            }
            var gltf = data.GLTF;

            Assert.AreEqual(1, gltf.images.Count);
            var exportedImage = gltf.images[0];

            Assert.AreEqual("image/png", exportedImage.mimeType);
            Assert.AreEqual(srcImageName, exportedImage.name);

            UnityEngine.Object.DestroyImmediate(mat);
            UnityEngine.Object.DestroyImmediate(root);

            var parsed = GltfData.CreateFromGltfDataForTest(gltf, data.BinBytes);

            // Extract Image to Texture2D
            var exportedBytes   = parsed.GetBytesFromBufferView(exportedImage.bufferView).ToArray();
            var exportedTexture = new Texture2D(2, 2, TextureFormat.ARGB32, mipChain: false, linear: false);

            Assert.IsTrue(exportedTexture.LoadImage(exportedBytes)); // Always true ?
            Assert.AreEqual(srcTex.width, exportedTexture.width);
            Assert.AreEqual(srcTex.height, exportedTexture.height);

            return(exportedTexture);
        }
Exemplo n.º 19
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();
                }
            }
        }
Exemplo n.º 20
0
        private static void ExportFromMenu()
        {
            var go   = Selection.activeObject as GameObject;
            var path = EditorUtility.SaveFilePanel(
                "Save glb",
                "",
                go.name + ".glb",
                "glb");

            if (string.IsNullOrEmpty(path))
            {
                return;
            }

            using (var exporter = new gltfExporter(new glTF()))
            {
                exporter.Prepare(go);
                exporter.Export();
                exporter.WriteTo(path);
            }
        }
Exemplo n.º 21
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);
            }
        }
Exemplo n.º 22
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);
        }
Exemplo n.º 23
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    data = new ExportingGltfData();
                var    gltf = data.GLTF;
                string json;
                using (var exporter = new gltfExporter(data, new GltfExportSettings()))
                {
                    exporter.Prepare(root);
                    exporter.Export(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 parsed = GltfData.CreateFromExportForTest(data);
                    using (var context = new ImporterContext(parsed))
                        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);
            }
        }
Exemplo n.º 24
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 data = new ExportingGltfData();
                var gltf = data.GLTF;
                var json = default(string);
                using (var exporter = new gltfExporter(data, new GltfExportSettings()))
                {
                    exporter.Prepare(go);
                    exporter.Export(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 parsed = GltfData.CreateFromExportForTest(data);
                    using (var context = new ImporterContext(parsed))
                        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 parsed = GltfData.CreateFromExportForTest(data);
                    using (var context = new ImporterContext(parsed))
                        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);
            }
        }