Exemplo n.º 1
0
 void CloseWorkingGroup()
 {
     foreach (var working_mesh_index in WorkingGroup.MeshMap.Keys)
     {
         var working_mesh = WorkingGroup.MeshMap[working_mesh_index];
         var mesh         = new Scene.Mesh();
         mesh.MaterialIndex = working_mesh.MaterialIndex;
         var working_material = Scene.Materials[mesh.MaterialIndex];
         mesh.Name      = working_material.Name;
         mesh.Vertices  = working_mesh.Vertices.ToArray();
         mesh.Triangles = working_mesh.Triangles.ToArray();
         mesh.Normals   = working_mesh.Normals.ToArray();
         mesh.Colors    = working_mesh.Colors.ToArray();
         mesh.UVs1      = working_mesh.UVs.ToArray();
         WorkingGroup.Parent.AddChild(mesh);
     }
     WorkingGroup = null;
 }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
0
        void HandleMesh(OpenFlight.Node node, Scene.Node parent)
        {
            var flt_mesh = Parser.ParseMesh(node.Record);

            OpenFlight.LocalVertexPool local_vertex_pool = null;
            OpenFlight.MeshPrimitive   mesh_primitive    = null;
            foreach (var record in node.Records)
            {
                switch (record.Opcode)
                {
                case OpenFlight.Opcode.LocalVertexPool:
                    local_vertex_pool = Parser.ParseLocalVertexPool(record);
                    break;

                case OpenFlight.Opcode.MeshPrimitive:
                    mesh_primitive = Parser.ParseMeshPrimitive(record);
                    break;
                }
            }
            if (local_vertex_pool == null)
            {
                return;
            }
            if (mesh_primitive == null)
            {
                return;
            }
            if (!local_vertex_pool.HasPosition)
            {
                return;
            }
            if (mesh_primitive.Type == 4)
            {
                UnityEngine.Debug.LogFormat("{0}: indexed poly is not currently supported.", node.Record.ToString());
                return;
            }

            var mesh = new Scene.Mesh();

            parent.AddChild(mesh);
            var material = MaterialForMesh(flt_mesh);

            mesh.MaterialIndex = Scene.Materials.IndexOf(material);
            mesh.Name          = flt_mesh.ID;

            var count = local_vertex_pool.Coordinates.Length;

            mesh.Vertices = new UnityEngine.Vector3[count];
            for (int i = 0; i < count; ++i)
            {
                // TODO: transform
                mesh.Vertices[i].x = (float)local_vertex_pool.Coordinates[i].X;
                mesh.Vertices[i].y = (float)local_vertex_pool.Coordinates[i].Z;
                mesh.Vertices[i].z = (float)local_vertex_pool.Coordinates[i].Y;
            }
            if (local_vertex_pool.HasNormal)
            {
                mesh.Normals = new UnityEngine.Vector3[count];
                for (int i = 0; i < count; ++i)
                {
                    mesh.Normals[i].x = local_vertex_pool.Normals[i].I;
                    mesh.Normals[i].y = local_vertex_pool.Normals[i].K;
                    mesh.Normals[i].z = local_vertex_pool.Normals[i].J;
                }
            }
            if (local_vertex_pool.HasBaseUV)
            {
                mesh.UVs1 = new UnityEngine.Vector2[count];
                for (int i = 0; i < count; ++i)
                {
                    mesh.UVs1[i].x = local_vertex_pool.UVBase[i].U;
                    mesh.UVs1[i].y = local_vertex_pool.UVBase[i].V;
                }
            }
            if (local_vertex_pool.HasRGBAColor)
            {
                mesh.Colors = new UnityEngine.Color32[count];
                for (int i = 0; i < count; ++i)
                {
                    mesh.Colors[i].r = local_vertex_pool.Colors[i].R;
                    mesh.Colors[i].g = local_vertex_pool.Colors[i].G;
                    mesh.Colors[i].b = local_vertex_pool.Colors[i].B;
                    mesh.Colors[i].a = local_vertex_pool.Colors[i].A;
                }
            }

            var index_count = mesh_primitive.Indices.Length;

            if (mesh_primitive.Type == 1)   // triangle strip
            {
                mesh.Triangles = new int[(index_count - 2) * 3];
                for (int i = 0; i + 2 < index_count; ++i)
                {
                    if (i % 2 == 0)
                    {
                        mesh.Triangles[(i * 3) + 0] = mesh_primitive.Indices[i + 1];
                        mesh.Triangles[(i * 3) + 1] = mesh_primitive.Indices[i + 0];
                        mesh.Triangles[(i * 3) + 2] = mesh_primitive.Indices[i + 2];
                    }
                    else
                    {
                        mesh.Triangles[(i * 3) + 0] = mesh_primitive.Indices[i + 0];
                        mesh.Triangles[(i * 3) + 1] = mesh_primitive.Indices[i + 1];
                        mesh.Triangles[(i * 3) + 2] = mesh_primitive.Indices[i + 2];
                    }
                }
            }
            if (mesh_primitive.Type == 2)   // triangle fan
            {
                mesh.Triangles = new int[(index_count - 2) * 3];
                for (int i = 0; i + 2 < index_count; ++i)
                {
                    mesh.Triangles[(i * 3) + 0] = mesh_primitive.Indices[0];
                    mesh.Triangles[(i * 3) + 1] = mesh_primitive.Indices[i + 1];
                    mesh.Triangles[(i * 3) + 2] = mesh_primitive.Indices[i + 2];
                }
            }
            if (mesh_primitive.Type == 3)   // quad strip
            {
                mesh.Triangles = new int[((index_count / 2) - 1) * 6];
                for (int i = 0; i + 1 < index_count; i += 2)
                {
                    mesh.Triangles[(i * 6) + 0] = mesh_primitive.Indices[i + 0];
                    mesh.Triangles[(i * 6) + 1] = mesh_primitive.Indices[i + 1];
                    mesh.Triangles[(i * 6) + 2] = mesh_primitive.Indices[i + 2];
                    mesh.Triangles[(i * 6) + 3] = mesh_primitive.Indices[i + 2];
                    mesh.Triangles[(i * 6) + 4] = mesh_primitive.Indices[i + 1];
                    mesh.Triangles[(i * 6) + 5] = mesh_primitive.Indices[i + 3];
                }
            }
        }