private int[] GetMeshTriangles(Mesh meshData, uint vertCount)
        {
            IndexFormat format = GetIndexFormat(meshData);

            int[] indices = new int[meshData.IndexCount];

            switch (format)
            {
                case IndexFormat.UInt16:
                    for (int i = 0; i < meshData.IndexCount; i++)
                    {
                        // We reverse the indices otherwise our normals will be inverted.
                        indices[meshData.IndexCount - 1 - i] = Convert.ToInt32(vertCount + BitConverter.ToUInt16(meshData.IndexData, i * 2));
                    }
                    break;
                case IndexFormat.UInt32:
                    for (int i = 0; i < meshData.IndexCount; i++)
                    {
                        // We reverse the indices otherwise our normals will be inverted.
                        indices[meshData.IndexCount - 1 - i] = Convert.ToInt32(vertCount + BitConverter.ToUInt32(meshData.IndexData, i * 4));
                    }
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }

            return indices;
        }
        private Material CreateMeshMaterial(Mesh mesh)
        {
            Texture2D diffuseTex    = textureManager.GetTexture(mesh.BaseDiffuse);
            Texture2D packedSpecTex = textureManager.GetTexture(mesh.SpecMap);
            Texture2D normalTex     = textureManager.GetTexture(mesh.BumpMap);

            Material material = new Material(source);

            if (diffuseTex != null)
            {
                material.SetTexture("_MainTex", diffuseTex);
                material.SetTextureScale("_MainTex", new Vector2(1, -1));
            }
            if (packedSpecTex != null)
            {
                material.SetTexture("_PackedSpecular", packedSpecTex);
                material.SetTextureScale("_PackedSpecular", new Vector2(1, -1));
            }
            if (normalTex != null)
            {
                material.SetTexture("_BumpMap", normalTex);
                material.SetTextureScale("_BumpMap", new Vector2(1, -1));
            }

            return(material);
        }
        private void ReadVertexStream(Vector2[] targetArray, int arrayPos, Mesh meshData, VertexLayout.EntryInfo info)
        {
            Mesh.VertexStream stream = meshData.VertexStreams[info.streamIndex];

            for (int i = 0; i < meshData.VertexCount; i++)
            {
                targetArray[i + arrayPos] = ReadVector2(info.offset, stream, i, info.dataType);
            }
        }
        /// <summary>
        /// Imports the mesh's vertex data.
        /// </summary>
        private void GetMeshVerts(Vector3[] verts, int arrayPos, Mesh meshData, VertexLayout vertexLayout)
        {
            VertexLayout.EntryInfo info;

            bool vertsExist = vertexLayout.GetEntryInfo(VertexLayout.Entry.DataUsages.Position, 0, out info);

            Assert.IsTrue(vertsExist);

            ReadVertexStream(verts, arrayPos, meshData, info);
        }
        private void ProcessMesh(int arrayPos, Dme modelData, Mesh meshData, Vector3[] verts, Vector2[] uvs)
        {
            uint materialHash = modelData.Materials[(int)meshData.MaterialIndex].MaterialDefinitionHash;
            MaterialDefinition materialDefinition = materialDefinitionManager.GetMaterial(materialHash);

            uint vertexLayoutHash = materialDefinition.DrawStyles[DRAWSTYLE_INDEX].VertexLayoutNameHash;
            VertexLayout vertexLayout = materialDefinitionManager.GetVertexLayout(vertexLayoutHash);

            GetMeshVerts(verts, arrayPos, meshData, vertexLayout);
            GetMeshUVs(uvs, arrayPos, meshData, vertexLayout);
        }
        private void GetMeshNormals(Vector3[] normals, int arrayPos, Mesh meshData, VertexLayout vertexLayout)
        {
            VertexLayout.EntryInfo info;

            bool normalExists = vertexLayout.GetEntryInfo(VertexLayout.Entry.DataUsages.Normal, 0, out info);

            if (normalExists)
            {
                ReadVertexStream(normals, arrayPos, meshData, info);
            }
        }
        /// <summary>
        /// Imports the mesh's texture coordinates.
        /// </summary>
        private void GetMeshUVs(Vector2[] uvs, int offset, Mesh meshData, VertexLayout vertexLayout)
        {
            VertexLayout.EntryInfo info;

            bool uvsExist = vertexLayout.GetEntryInfo(VertexLayout.Entry.DataUsages.Texcoord, 0, out info);

            if (uvsExist)
            {
                ReadVertexStream(uvs, offset, meshData, info);
            }
        }
 private IndexFormat GetIndexFormat(Mesh meshData)
 {
     switch (meshData.IndexSize)
     {
         case 2:
             return IndexFormat.UInt16;
         case 4:
             return IndexFormat.UInt32;
         default:
             throw new ArgumentOutOfRangeException("Unknown Index Format/Size: " + meshData.IndexSize);
     }
 }
        public MeshData GenerateMeshData(Dme modelData)
        {
            MeshData meshData = new MeshData();

            uint totalVerts = 0;

            // Verts and UVs
            foreach (Mesh mesh in modelData.Meshes)
            {
                totalVerts += mesh.VertexCount;
            }

            meshData.verts = new Vector3[totalVerts];
            meshData.uvs = new Vector2[totalVerts];

            int arrayPos = 0;
            foreach (Mesh mesh in modelData.Meshes)
            {
                ProcessMesh(arrayPos, modelData, mesh, meshData.verts, meshData.uvs);
                arrayPos += (int)mesh.VertexCount;
            }

            // Triangles
            meshData.triangles = new int[modelData.Meshes.Count][];
            uint vertCount = 0;
            for (int i = 0; i < modelData.Meshes.Count; i++)
            {
                Mesh mesh = modelData.Meshes[i];

                meshData.triangles[i] = GetMeshTriangles(mesh, vertCount);

                vertCount += mesh.VertexCount;
            }

            meshData.IndexFormat = GetIndexFormat(modelData.Meshes[0]);

            return meshData;
        }