Example #1
0
        private MeshId ExportMesh(string name, GameObject[] primitives)
        {
            // check if this set of primitives is already a mesh
            MeshId existingMeshId = null;
            var    key            = new PrimKey();

            foreach (var prim in primitives)
            {
                var filter   = prim.GetComponent <MeshFilter>();
                var renderer = prim.GetComponent <MeshRenderer>();
                key.Mesh     = filter.sharedMesh;
                key.Material = renderer.sharedMaterial;

                MeshId tempMeshId;
                if (_primOwner.TryGetValue(key, out tempMeshId) && (existingMeshId == null || tempMeshId == existingMeshId))
                {
                    existingMeshId = tempMeshId;
                }
                else
                {
                    existingMeshId = null;
                    break;
                }
            }

            // if so, return that mesh id
            if (existingMeshId != null)
            {
                return(existingMeshId);
            }

            // if not, create new mesh and return its id
            var mesh = new GLTF.Schema.GLTFMesh();

            if (ExportNames)
            {
                mesh.Name = name;
            }

            mesh.Primitives = new List <MeshPrimitive>(primitives.Length);
            foreach (var prim in primitives)
            {
                mesh.Primitives.AddRange(ExportPrimitive(prim));
            }

            var id = new MeshId
            {
                Id   = _root.Meshes.Count,
                Root = _root
            };

            _root.Meshes.Add(mesh);

            return(id);
        }
Example #2
0
        public static GLTFRoot Deserialize(TextReader textReader)
        {
            var jsonText = textReader.ReadToEnd();
            var sr       = textReader as StreamReader;

            sr.BaseStream.Seek(0, SeekOrigin.Begin);
            sr.DiscardBufferedData();

            JObject jo = JObject.Parse(jsonText);

            var jsonReader = new JsonTextReader(textReader);
            var root       = new GLTFRoot();

            if (jsonReader.Read() && jsonReader.TokenType != JsonToken.StartObject)
            {
                throw new Exception("gltf json must be an object");
            }

            while (jsonReader.Read() && jsonReader.TokenType == JsonToken.PropertyName)
            {
                var curProp = jsonReader.Value.ToString();

                switch (curProp)
                {
                case "extensionsUsed":
                    root.ExtensionsUsed = jsonReader.ReadStringList();
                    break;

                case "extensionsRequired":
                    root.ExtensionsRequired = jsonReader.ReadStringList();
                    break;

                case "accessors":
                    root.Accessors = jsonReader.ReadList(() => Accessor.Deserialize(root, jsonReader));
                    break;

                case "animations":
                    root.Animations = jsonReader.ReadList(() => GLTFAnimation.Deserialize(root, jsonReader));
                    break;

                case "asset":
                    var asset = jo["asset"].ToString();
                    root.Asset = JsonUtility.FromJson <Asset>(asset);
                    // will be deleted
                    Asset.Deserialize(root, jsonReader);
                    break;

                case "buffers":
                    root.Buffers = jsonReader.ReadList(() => GLTFBuffer.Deserialize(root, jsonReader));
                    break;

                case "bufferViews":
                    root.BufferViews = jsonReader.ReadList(() => BufferView.Deserialize(root, jsonReader));
                    break;

                case "cameras":
                    root.Cameras = new List <GLTFCamera>();
                    foreach (var camera in jo["cameras"])
                    {
                        GLTFCamera c = JsonUtility.FromJson <GLTFCamera>(camera.ToString());
                        root.Cameras.Append(c);
                    }
                    // will be deleted
                    jsonReader.ReadList(() => GLTFCamera.Deserialize(root, jsonReader));
                    break;

                case "images":
                    root.Images = jsonReader.ReadList(() => GLTFImage.Deserialize(root, jsonReader));
                    break;

                case "materials":
                    root.Materials = jsonReader.ReadList(() => GLTFMaterial.Deserialize(root, jsonReader));
                    break;

                case "meshes":
                    root.Meshes = jsonReader.ReadList(() => GLTFMesh.Deserialize(root, jsonReader));
                    break;

                case "nodes":
                    root.Nodes = jsonReader.ReadList(() => Node.Deserialize(root, jsonReader));
                    break;

                case "samplers":
                    root.Samplers = jsonReader.ReadList(() => Sampler.Deserialize(root, jsonReader));
                    break;

                case "scene":
                    root.Scene = SceneId.Deserialize(root, jsonReader);
                    break;

                case "scenes":
                    root.Scenes = jsonReader.ReadList(() => GLTF.Schema.GLTFScene.Deserialize(root, jsonReader));
                    break;

                case "skins":
                    root.Skins = jsonReader.ReadList(() => Skin.Deserialize(root, jsonReader));
                    break;

                case "textures":
                    root.Textures = jsonReader.ReadList(() => GLTFTexture.Deserialize(root, jsonReader));
                    break;

                default:
                    root.DefaultPropertyDeserializer(root, jsonReader);
                    break;
                }
            }

            return(root);
        }
        public static GLTFRoot Deserialize(TextReader textReader)
        {
            var jsonReader = new JsonTextReader(textReader);
            var root       = new GLTFRoot();

            if (jsonReader.Read() && jsonReader.TokenType != JsonToken.StartObject)
            {
                throw new Exception("gltf json must be an object");
            }

            while (jsonReader.Read() && jsonReader.TokenType == JsonToken.PropertyName)
            {
                var curProp = jsonReader.Value.ToString();

                switch (curProp)
                {
                case "extensionsUsed":
                    root.ExtensionsUsed = jsonReader.ReadStringList();
                    break;

                case "extensionsRequired":
                    root.ExtensionsRequired = jsonReader.ReadStringList();
                    break;

                case "accessors":
                    root.Accessors = jsonReader.ReadList(() => Accessor.Deserialize(root, jsonReader));
                    break;

                case "animations":
                    root.Animations = jsonReader.ReadList(() => GLTFAnimation.Deserialize(root, jsonReader));
                    break;

                case "asset":
                    root.Asset = Asset.Deserialize(root, jsonReader);
                    break;

                case "buffers":
                    root.Buffers = jsonReader.ReadList(() => GLTFBuffer.Deserialize(root, jsonReader));
                    break;

                case "bufferViews":
                    root.BufferViews = jsonReader.ReadList(() => BufferView.Deserialize(root, jsonReader));
                    break;

                case "cameras":
                    root.Cameras = jsonReader.ReadList(() => GLTFCamera.Deserialize(root, jsonReader));
                    break;

                case "images":
                    root.Images = jsonReader.ReadList(() => GLTFImage.Deserialize(root, jsonReader));
                    break;

                case "materials":
                    root.Materials = jsonReader.ReadList(() => GLTFMaterial.Deserialize(root, jsonReader));
                    break;

                case "meshes":
                    root.Meshes = jsonReader.ReadList(() => GLTFMesh.Deserialize(root, jsonReader));
                    break;

                case "nodes":
                    root.Nodes = jsonReader.ReadList(() => Node.Deserialize(root, jsonReader));
                    break;

                case "samplers":
                    root.Samplers = jsonReader.ReadList(() => Sampler.Deserialize(root, jsonReader));
                    break;

                case "scene":
                    root.Scene = SceneId.Deserialize(root, jsonReader);
                    break;

                case "scenes":
                    root.Scenes = jsonReader.ReadList(() => GLTF.Schema.GLTFScene.Deserialize(root, jsonReader));
                    break;

                case "skins":
                    root.Skins = jsonReader.ReadList(() => Skin.Deserialize(root, jsonReader));
                    break;

                case "textures":
                    root.Textures = jsonReader.ReadList(() => GLTFTexture.Deserialize(root, jsonReader));
                    break;

                default:
                    root.DefaultPropertyDeserializer(root, jsonReader);
                    break;
                }
            }

            return(root);
        }