コード例 #1
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);
        }
コード例 #2
0
        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);
        }
コード例 #3
0
        public GLTFRoot(GLTFRoot gltfRoot) : base(gltfRoot)
        {
            if (gltfRoot.ExtensionsUsed != null)
            {
                ExtensionsUsed = gltfRoot.ExtensionsUsed.ToList();
            }

            if (gltfRoot.ExtensionsRequired != null)
            {
                ExtensionsRequired = gltfRoot.ExtensionsRequired.ToList();
            }

            if (gltfRoot.Accessors != null)
            {
                Accessors = new List <Accessor>(gltfRoot.Accessors.Count);
                foreach (Accessor accessor in gltfRoot.Accessors)
                {
                    Accessors.Add(new Accessor(accessor, this));
                }
            }

            if (gltfRoot.Animations != null)
            {
                Animations = new List <GLTFAnimation>(gltfRoot.Animations.Count);
                foreach (GLTFAnimation animation in gltfRoot.Animations)
                {
                    Animations.Add(new GLTFAnimation(animation, this));
                }
            }

            if (gltfRoot.Asset != null)
            {
                Asset = new Asset(gltfRoot.Asset);
            }

            if (gltfRoot.Buffers != null)
            {
                Buffers = new List <GLTFBuffer>(gltfRoot.Buffers.Count);
                foreach (GLTFBuffer buffer in gltfRoot.Buffers)
                {
                    Buffers.Add(new GLTFBuffer(buffer, this));
                }
            }

            if (gltfRoot.BufferViews != null)
            {
                BufferViews = new List <BufferView>(gltfRoot.BufferViews.Count);
                foreach (BufferView bufferView in gltfRoot.BufferViews)
                {
                    BufferViews.Add(new BufferView(bufferView, this));
                }
            }

            if (gltfRoot.Cameras != null)
            {
                Cameras = new List <GLTFCamera>(gltfRoot.Cameras.Count);
                foreach (GLTFCamera camera in gltfRoot.Cameras)
                {
                    Cameras.Add(new GLTFCamera(camera, this));
                }
            }

            if (gltfRoot.Images != null)
            {
                Images = new List <GLTFImage>(gltfRoot.Images.Count);
                foreach (GLTFImage image in gltfRoot.Images)
                {
                    Images.Add(new GLTFImage(image, this));
                }
            }

            if (gltfRoot.Materials != null)
            {
                Materials = new List <GLTFMaterial>(gltfRoot.Materials.Count);
                foreach (GLTFMaterial material in gltfRoot.Materials)
                {
                    Materials.Add(new GLTFMaterial(material, this));
                }
            }

            if (gltfRoot.Meshes != null)
            {
                Meshes = new List <GLTFMesh>(gltfRoot.Meshes.Count);
                foreach (GLTFMesh mesh in gltfRoot.Meshes)
                {
                    Meshes.Add(new GLTFMesh(mesh, this));
                }
            }

            if (gltfRoot.Nodes != null)
            {
                Nodes = new List <Node>(gltfRoot.Nodes.Count);
                foreach (Node node in gltfRoot.Nodes)
                {
                    Nodes.Add(new Node(node, this));
                }
            }

            if (gltfRoot.Samplers != null)
            {
                Samplers = new List <Sampler>(gltfRoot.Samplers.Count);
                foreach (Sampler sampler in gltfRoot.Samplers)
                {
                    Samplers.Add(new Sampler(sampler, this));
                }
            }

            if (gltfRoot.Scene != null)
            {
                Scene = new SceneId(gltfRoot.Scene, this);
            }

            if (gltfRoot.Scenes != null)
            {
                Scenes = new List <GLTFScene>(gltfRoot.Scenes.Count);
                foreach (GLTFScene scene in gltfRoot.Scenes)
                {
                    Scenes.Add(new GLTFScene(scene, this));
                }
            }

            if (gltfRoot.Skins != null)
            {
                Skins = new List <Skin>(gltfRoot.Skins.Count);
                foreach (Skin skin in gltfRoot.Skins)
                {
                    Skins.Add(new Skin(skin, this));
                }
            }

            if (gltfRoot.Textures != null)
            {
                Textures = new List <GLTFTexture>(gltfRoot.Textures.Count);
                foreach (GLTFTexture texture in gltfRoot.Textures)
                {
                    Textures.Add(new GLTFTexture(texture, this));
                }
            }
        }
コード例 #4
0
 public SceneId(SceneId id, GLTFRoot newRoot) : base(id, newRoot)
 {
 }