Example #1
0
        static Scene.Node NodeFromGLTFScene(glTF.glTF gltf, byte[] bytes, int index)
        {
            var scene = gltf.scenes[index];
            var group = new Scene.Group();

            group.Name     = scene.name;
            group.Children = new List <Scene.Node>();
            for (int i = 0, c = scene.nodes.Length; i < c; ++i)
            {
                group.Children.Add(NodeFromGLTFNode(gltf, bytes, i));
            }
            return(group);
        }
Example #2
0
        static Vector2[] Vector2FromBinary(glTF.glTF gltf, byte[] bytes, int index)
        {
            var vectorlist = new List <Vector2>();
            var bufferview = gltf.accessors[index];
            int i          = gltf.bufferViews[bufferview.bufferview].byteOffset;
            int byteoffset = i;

            for (; i < gltf.bufferViews[bufferview.bufferview].byteLength + byteoffset; i += 8)
            {
                var x = BitConverter.ToSingle(bytes, i);
                var y = BitConverter.ToSingle(bytes, i + 4);
                vectorlist.Add(new Vector2(x, y));
            }
            return(vectorlist.ToArray());
        }
Example #3
0
        static Vector3[] Vector3FromBinary(glTF.glTF gltf, byte[] bytes, int index)
        {
            var vectorlist  = new List <Vector3>();
            var bufferview  = gltf.accessors[index];
            int i           = gltf.bufferViews[bufferview.bufferview].byteOffset;
            int bufferlimit = bufferview.count != -1 ? bufferview.count * 12 : gltf.bufferViews[bufferview.bufferview].byteLength;

            bufferlimit += i;
            for (; i < bufferlimit; i += 12)
            {
                var x = BitConverter.ToSingle(bytes, i);
                var y = BitConverter.ToSingle(bytes, i + 4);
                var z = BitConverter.ToSingle(bytes, i + 8);
                vectorlist.Add(new Vector3(x, y, z));
            }
            return(vectorlist.ToArray());
        }
Example #4
0
        static int[] TrianglesFromBinary(glTF.glTF gltf, byte[] bytes, int index)
        {
            var trianglelist = new List <int>();
            var accessor     = gltf.accessors[index];
            int valuesize    = ByteCountForComponentType(accessor.componentType);
            var bufferview   = gltf.bufferViews[accessor.bufferview];
            var byteOffset   = accessor.byteOffset + bufferview.byteOffset;
            var result       = new int[accessor.count];

            for (int i = 0; i < accessor.count; ++i)
            {
                int offset = byteOffset + (i * valuesize);
                if (valuesize == 1)
                {
                    result[i] = bytes[offset];
                }
                if (valuesize == 2)
                {
                    result[i] = BitConverter.ToUInt16(bytes, offset);
                }
                if (valuesize == 4)
                {
                    result[i] = (int)BitConverter.ToUInt32(bytes, offset);
                }
            }

            /*
             * for (int i = 0, c = accessor.count / 3; i < c; i += 3)
             * {
             *  //result[accessor.count + i + 0] = result[i + 0];
             *  //result[accessor.count + i + 1] = result[i + 2];
             *  //result[accessor.count + i + 2] = result[i + 1];
             *  int tmp = result[i + 1];
             *  result[i + 1] = result[i + 2];
             *  result[i + 2] = tmp;
             * }
             */
            return(result);
        }
Example #5
0
        static Scene.Material MaterialFromGLTFMaterial(glTF.glTF gltf, byte[] bytes, int index)
        {
            var material = gltf.materials[index];
            var result   = new Scene.Material();

            result.Name            = material.name;
            result.BackfaceCulling = !material.doubleSided;

            // TODO: result.Image



            /*
             * var unityMaterial = new Material(Shader.Find("Cognitics/ModelStandard"));
             * unityMaterial.name = material.name;
             * unityMaterial.SetFloat("_Glossiness", material.pbrMetallicRoughness.roughnessFactor);
             * unityMaterial.SetFloat("_Metallic", material.pbrMetallicRoughness.metallicFactor);
             * if (material.pbrMetallicRoughness.baseColorFactor != null)
             * {
             *  var materialcolor = material.pbrMetallicRoughness.baseColorFactor;
             *  var color = new Color(materialcolor[0], materialcolor[1], materialcolor[2], materialcolor[3]);
             *  unityMaterial.SetColor("_Color", color);
             * }
             * if(material.pbrMetallicRoughness.baseColorTexture != null)
             * {
             *  int i = material.pbrMetallicRoughness.baseColorTexture.index;
             *  int source = gltf.textures[i].source;
             *  var texture = new Texture2D(1, 1);
             *  // TODO: We don't deal with textures for this demo. This will need to be figured out for an implementation.
             * }
             */
            //result.material = unityMaterial;



            return(result);
        }
Example #6
0
        static Scene.Node NodeFromGLTFNode(glTF.glTF gltf, byte[] bytes, int index)
        {
            var node = gltf.nodes[index];

            Scene.Node result = new Scene.Group();

            /*
             * Scene.Node result = null;
             * if (node.mesh < 0)
             *  result = new Scene.Group();
             * else
             *  result = new Scene.Mesh();
             */

            result.Name = node.name;
            if (node.translation != null)
            {
                result.position = new Vector3(node.translation[0], node.translation[1], node.translation[2]);
            }
            if (node.rotation != null)
            {
                // assign rotation
            }

            // TODO: assign translation to result.Matrix
            // TODO: assign rotation to result.Matrix

            if (node.mesh >= 0)
            {
                result.Children = new List <Scene.Node>();
                var mesh = gltf.meshes[node.mesh];

                foreach (var meshprimitive in gltf.meshes[index].primitives)
                {
                    var meshtoadd = new Scene.Mesh();
                    meshtoadd.Name = gltf.meshes[node.mesh].name;
                    if (meshprimitive.attributes.POSITION != -1)
                    {
                        meshtoadd.Vertices = Vector3FromBinary(gltf, bytes, meshprimitive.attributes.POSITION);
                    }
                    if (meshprimitive.attributes.NORMAL != -1)
                    {
                        meshtoadd.Normals = Vector3FromBinary(gltf, bytes, meshprimitive.attributes.NORMAL);
                    }
                    if (meshprimitive.attributes.TEXCOORD_0 != -1)
                    {
                        meshtoadd.UVs1 = Vector2FromBinary(gltf, bytes, meshprimitive.attributes.TEXCOORD_0);
                    }
                    if (meshprimitive.indices != -1)
                    {
                        meshtoadd.Triangles = TrianglesFromBinary(gltf, bytes, meshprimitive.indices);
                    }
                    if (meshprimitive.material != -1)
                    {
                        meshtoadd.MaterialIndex = meshprimitive.material;
                    }
                    result.Children.Add(meshtoadd);
                }
            }
            if (node.children != null)
            {
                for (int i = 0, c = node.children.Length; i < c; ++i)
                {
                    result.Children.Add(NodeFromGLTFNode(gltf, bytes, i));
                }
            }
            return(result);
        }