Exemplo n.º 1
0
        /// <summary>
        /// Loads via a web call the gltf file and then constructs a scene
        /// </summary>
        /// <param name="sceneIndex">Index into scene to load. -1 means load default</param>
        /// <param name="isMultithreaded">Whether to do loading operation on a thread</param>
        /// <returns></returns>
        public IEnumerator Load(int sceneIndex = -1, bool isMultithreaded = false)
        {
            if (_loadType == LoadType.Uri)
            {
                var www = UnityWebRequest.Get(_gltfUrl);

                yield return(www.Send());

                if (www.responseCode >= 400 || www.responseCode == 0)
                {
                    throw new WebRequestException(www);
                }

                _gltfData = www.downloadHandler.data;
            }
            else if (_loadType == LoadType.Stream)
            {
                // todo optimization: add stream support to parsing layer
                int streamLength = (int)_gltfStream.Length;
                _gltfData = new byte[streamLength];
                _gltfStream.Read(_gltfData, 0, streamLength);
            }
            else
            {
                throw new Exception("Invalid load type specified: " + _loadType);
            }

            _root = GLTFParser.ParseJson(_gltfData);
            yield return(ImportScene(sceneIndex, isMultithreaded));
        }
Exemplo n.º 2
0
 public IExtension Clone(GLTFRoot gltfRoot)
 {
     return(new MSFT_LODExtension(MeshIds));
 }
        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);
        }
Exemplo n.º 4
0
 public IExtension Clone(GLTFRoot root)
 {
     return(new ExtTextureTransformExtension(Offset, Scale, TexCoord));
 }
 public OcclusionTextureInfo(OcclusionTextureInfo occulisionTextureInfo, GLTFRoot gltfRoot) : base(occulisionTextureInfo, gltfRoot)
 {
     Strength = occulisionTextureInfo.Strength;
 }
Exemplo n.º 6
0
 public AccessorId(AccessorId id, GLTFRoot newRoot) : base(id, newRoot)
 {
 }
Exemplo n.º 7
0
 public SkinId(SkinId id, GLTFRoot newRoot) : base(id, newRoot)
 {
 }
Exemplo n.º 8
0
 public SamplerId(SamplerId id, GLTFRoot newRoot) : base(id, newRoot)
 {
 }
Exemplo n.º 9
0
 public LightId(LightId id, GLTFRoot newRoot) : base(id, newRoot)
 {
 }
Exemplo n.º 10
0
 public AnimationSamplerId(AnimationSamplerId sampler, GLTFRoot root)
 {
     Id            = sampler.Id;
     GLTFAnimation = sampler.GLTFAnimation;
     Root          = root;
 }
Exemplo n.º 11
0
 public IExtension Clone(GLTFRoot gltfRoot)
 {
     return(new UnityMobileDiffuseExtension(new TextureInfo(MainTex, gltfRoot)));
 }
 public IExtension Clone(GLTFRoot gltfRoot)
 {
     return(new KHR_MaterialsUnlitExtension(this, gltfRoot));
 }
 public KHR_MaterialsUnlitExtension(KHR_MaterialsUnlitExtension ext, GLTFRoot root) : base(ext, root)
 {
 }
        public override IExtension Deserialize(GLTFRoot root, JProperty extensionToken)
        {
            List <HT_node_colliderExtension.Collider> Colliders = new List <HT_node_colliderExtension.Collider>();

            if (!extensionToken.HasValues)
            {
                return(new HT_node_colliderExtension(Colliders));
            }

            foreach (JToken value in extensionToken.Values())
            {
                HT_node_colliderExtension.ColliderType type = HT_node_colliderExtension.COLLIDERTYPE_DEFAULT;
                bool    isTrigger = HT_node_colliderExtension.ISTRIGGER_DEFAULT;
                Vector3 center    = HT_node_colliderExtension.CENTER_DEFAULT;
                Vector3 size      = HT_node_colliderExtension.SIZE_DEFAULT;
                float   radius    = HT_node_colliderExtension.RADIUS_DEFAULT;
                float   height    = HT_node_colliderExtension.HEIGHT_DEFAULT;
                HT_node_colliderExtension.CapsuleDirection direction = HT_node_colliderExtension.DIRECTION_DEFAULT;

                JToken typeToken = value[TYPE];
                type = typeToken != null ? (HT_node_colliderExtension.ColliderType)typeToken.DeserializeAsInt() : type;

                JToken isTriggerToken = value[ISTRIGGER];
                isTrigger = isTriggerToken != null?isTriggerToken.DeserializeAsBool() : isTrigger;

                JToken centerToken = value[CENTER];
                center = centerToken != null?centerToken.DeserializeAsVector3() : center;

                if (type == HT_node_colliderExtension.ColliderType.Box)
                {
                    JToken sizeToken = value[SIZE];
                    size = sizeToken != null?sizeToken.DeserializeAsVector3() : size;

                    Colliders.Add(new HT_node_colliderExtension.BoxCollider(isTrigger, center, size));
                }

                if (type == HT_node_colliderExtension.ColliderType.Sphere)
                {
                    JToken radiusToken = value[RADIUS];
                    radius = radiusToken != null?radiusToken.DeserializeAsFloat() : radius;

                    Colliders.Add(new HT_node_colliderExtension.SphereCollider(isTrigger, center, radius));
                }

                if (type == HT_node_colliderExtension.ColliderType.Capsule)
                {
                    JToken radiusToken = value[RADIUS];
                    radius = radiusToken != null?radiusToken.DeserializeAsFloat() : radius;

                    JToken heightToken = value[HEIGHT];
                    height = heightToken != null?heightToken.DeserializeAsFloat() : height;

                    JToken directionToken = value[DIRECTION];
                    direction = directionToken != null ? (HT_node_colliderExtension.CapsuleDirection)directionToken.DeserializeAsInt() : direction;

                    Colliders.Add(new HT_node_colliderExtension.CapsuleCollider(isTrigger, center, radius, height, direction));
                }
            }

            return(new HT_node_colliderExtension(Colliders));
        }
 public IExtension Clone(GLTFRoot gltfRoot)
 {
     return(new KHR_mesh_quantizationExtension(Translation, Scale));
 }
Exemplo n.º 16
0
 public GLTFId(GLTFId <T> gltfId, GLTFRoot newRoot)
 {
     Id   = gltfId.Id;
     Root = newRoot;
 }
Exemplo n.º 17
0
 public NodeId(NodeId id, GLTFRoot newRoot) : base(id, newRoot)
 {
 }
Exemplo n.º 18
0
 public abstract Extension Deserialize(GLTFRoot root, JProperty extensionToken);
Exemplo n.º 19
0
 public SceneId(SceneId id, GLTFRoot newRoot) : base(id, newRoot)
 {
 }
Exemplo n.º 20
0
        // Taken from: http://answers.unity3d.com/comments/190515/view.html
        // Official support for Mesh.RecalculateTangents should be coming in 5.6
        // https://feedback.unity3d.com/suggestions/recalculatetangents

        /*private MeshPrimitiveAttributes CalculateAndSetTangents(MeshPrimitiveAttributes attributes)
         * {
         *      var triangleCount = attributes.Triangles.Length;
         *      var vertexCount = attributes.Vertices.Length;
         *
         *      var tan1 = new Vector3[vertexCount];
         *      var tan2 = new Vector3[vertexCount];
         *
         *      attributes.Tangents = new Vector4[vertexCount];
         *
         *      for (long a = 0; a < triangleCount; a += 3)
         *      {
         *              long i1 = attributes.Triangles[a + 0];
         *              long i2 = attributes.Triangles[a + 1];
         *              long i3 = attributes.Triangles[a + 2];
         *
         *              var v1 = attributes.Vertices[i1];
         *              var v2 = attributes.Vertices[i2];
         *              var v3 = attributes.Vertices[i3];
         *
         *              var w1 = attributes.Uv[i1];
         *              var w2 = attributes.Uv[i2];
         *              var w3 = attributes.Uv[i3];
         *
         *              var x1 = v2.X - v1.X;
         *              var x2 = v3.X - v1.X;
         *              var y1 = v2.Y - v1.Y;
         *              var y2 = v3.Y - v1.Y;
         *              var z1 = v2.Z - v1.Z;
         *              var z2 = v3.Z - v1.Z;
         *
         *              var s1 = w2.X - w1.X;
         *              var s2 = w3.X - w1.X;
         *              var t1 = w2.Y - w1.Y;
         *              var t2 = w3.Y - w1.Y;
         *
         *              var r = 1.0f / (s1 * t2 - s2 * t1);
         *
         *              var sdir = new Vector3((t2 * x1 - t1 * x2) * r, (t2 * y1 - t1 * y2) * r, (t2 * z1 - t1 * z2) * r);
         *              var tdir = new Vector3((s1 * x2 - s2 * x1) * r, (s1 * y2 - s2 * y1) * r, (s1 * z2 - s2 * z1) * r);
         *
         *              tan1[i1] += sdir;
         *              tan1[i2] += sdir;
         *              tan1[i3] += sdir;
         *
         *              tan2[i1] += tdir;
         *              tan2[i2] += tdir;
         *              tan2[i3] += tdir;
         *      }
         *
         *
         *      for (long a = 0; a < vertexCount; ++a)
         *      {
         *              var n = attributes.Normals[a];
         *              var t = tan1[a];
         *
         *              Vector3.OrthoNormalize(ref n, ref t);
         *
         *              attributes.Tangents[a].X = t.X;
         *              attributes.Tangents[a].Y = t.Y;
         *              attributes.Tangents[a].Z = t.Z;
         *
         *              attributes.Tangents[a].W = (Vector3.Dot(Vector3.Cross(n, t), tan2[a]) < 0.0f) ? -1.0f : 1.0f;
         *      }
         *
         *      return attributes;
         * }*/

        public static MeshPrimitive Deserialize(GLTFRoot root, JsonReader reader)
        {
            var primitive = new MeshPrimitive();

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

                switch (curProp)
                {
                case "attributes":
                    primitive.Attributes = reader.ReadAsDictionary(() => new AccessorId
                    {
                        Id   = reader.ReadAsInt32().Value,
                        Root = root
                    });
                    break;

                case "indices":
                    primitive.Indices = AccessorId.Deserialize(root, reader);
                    break;

                case "material":
                    primitive.Material = MaterialId.Deserialize(root, reader);
                    break;

                case "mode":
                    primitive.Mode = (DrawMode)reader.ReadAsInt32().Value;
                    break;

                case "targets":
                    primitive.Targets = reader.ReadList(() =>
                    {
                        return(reader.ReadAsDictionary(() => new AccessorId
                        {
                            Id = reader.ReadAsInt32().Value,
                            Root = root
                        },
                                                       skipStartObjectRead: true));
                    });
                    break;

                case "extras":
                    // GLTF does not support morph target names, serialize in extras for now
                    // https://github.com/KhronosGroup/glTF/issues/1036
                    if (reader.Read() && reader.TokenType == JsonToken.StartObject)
                    {
                        while (reader.Read() && reader.TokenType == JsonToken.PropertyName)
                        {
                            var extraProperty = reader.Value.ToString();
                            switch (extraProperty)
                            {
                            case "targetNames":
                                primitive.TargetNames = reader.ReadStringList();
                                break;
                            }
                        }
                    }

                    break;

                default:
                    primitive.DefaultPropertyDeserializer(root, reader);
                    break;
                }
            }

            return(primitive);
        }
Exemplo n.º 21
0
 public TextureId(TextureId id, GLTFRoot newRoot) : base(id, newRoot)
 {
 }
Exemplo n.º 22
0
 public CameraId(CameraId id, GLTFRoot newRoot) : base(id, newRoot)
 {
 }
Exemplo n.º 23
0
 public BufferViewId(BufferViewId id, GLTFRoot newRoot) : base(id, newRoot)
 {
 }
Exemplo n.º 24
0
 public ImageId(ImageId id, GLTFRoot newRoot) : base(id, newRoot)
 {
 }
Exemplo n.º 25
0
 public IExtension Clone(GLTFRoot root)
 {
     return(new HT_node_colliderExtension(Colliders));
 }
Exemplo n.º 26
0
 public MaterialId(MaterialId id, GLTFRoot newRoot) : base(id, newRoot)
 {
 }
Exemplo n.º 27
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));
                }
            }
        }
Exemplo n.º 28
0
 public MeshId(MeshId id, GLTFRoot newRoot) : base(id, newRoot)
 {
 }
Exemplo n.º 29
0
        public static Node Deserialize(GLTFRoot root, JsonReader reader)
        {
            var node = new Node();

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

                switch (curProp)
                {
                case "camera":
                    node.Camera = CameraId.Deserialize(root, reader);
                    break;

                case "children":
                    node.Children = NodeId.ReadList(root, reader);
                    break;

                case "skin":
                    node.Skin = SkinId.Deserialize(root, reader);
                    break;

                case "matrix":
                    var list = reader.ReadDoubleList();
                    // gltf has column ordered matricies
                    UnityEngine.Vector4 c1 = new UnityEngine.Vector4((float)list[0], (float)list[1], (float)list[2], (float)list[3]);
                    UnityEngine.Vector4 c2 = new UnityEngine.Vector4((float)list[4], (float)list[5], (float)list[6], (float)list[7]);
                    UnityEngine.Vector4 c3 = new UnityEngine.Vector4((float)list[8], (float)list[9], (float)list[10], (float)list[11]);
                    UnityEngine.Vector4 c4 = new UnityEngine.Vector4((float)list[12], (float)list[13], (float)list[14], (float)list[15]);

                    var mat = new Matrix4x4(c1, c2, c3, c4);

                    node.Matrix = mat;
                    break;

                case "mesh":
                    node.Mesh = MeshId.Deserialize(root, reader);
                    break;

                case "rotation":
                    node.UseTRS   = true;
                    node.Rotation = reader.ReadAsQuaternion();
                    break;

                case "scale":
                    node.UseTRS = true;
                    node.Scale  = reader.ReadAsVector3();
                    break;

                case "translation":
                    node.UseTRS      = true;
                    node.Translation = reader.ReadAsVector3();
                    break;

                case "weights":
                    node.Weights = reader.ReadDoubleList();
                    break;

                default:
                    node.DefaultPropertyDeserializer(root, reader);
                    break;
                }
            }

            return(node);
        }
Exemplo n.º 30
0
 public AnimationId(AnimationId id, GLTFRoot newRoot) : base(id, newRoot)
 {
 }