public void readBinaryFile( string file ) { // TODO: Reset data. BinaryReader br = new BinaryReader(new FileStream(file, FileMode.Open)); // Mesh count. int meshCount = br.ReadInt32(); // Read all meshes. for( int currMesh = 0; currMesh < meshCount; ++currMesh ) { MayaMesh mesh = new MayaMesh(); // Mesh name. mesh.name = br.ReadString(); //Mesh id. mesh.id = br.ReadInt32(); // Vertex count. int vertexCount = br.ReadInt32(); // Read all vertices. for( int currVert = 0; currVert < vertexCount; ++currVert ) { Vertex vert = new Vertex(); // Read position. vert.position.x = br.ReadSingle(); vert.position.y = br.ReadSingle(); vert.position.z = br.ReadSingle(); // Read normal. vert.normal.x = br.ReadSingle(); vert.normal.y = br.ReadSingle(); vert.normal.z = br.ReadSingle(); // Read texcoord. vert.texcoord.x = br.ReadSingle(); vert.texcoord.y = br.ReadSingle(); mesh.vertices.Add(vert); } // Read index count. int indexCount = br.ReadInt32(); // Read all indices. for( int currIdx = 0; currIdx < indexCount; ++currIdx ) { mesh.indices.Add(br.ReadUInt32()); } allMeshes.Add(mesh); } // Read xform count. int xformCount = br.ReadInt32(); for( int currXform = 0; currXform < xformCount; ++currXform ) { MayaXform xform = new MayaXform(); // Read name. xform.name = br.ReadString(); // Read id. xform.id = br.ReadInt32(); // Read parent id. int parentId = br.ReadInt32(); if( parentId != -1 ) { xform.parent = allXforms[parentId]; } // Read matrix. for( int x = 0; x < 4; ++x ) { for( int y = 0; y < 4; ++y ) { xform.matrix[x,y] = br.ReadSingle(); } } // Read source xform bool. xform.isSourceXform = br.ReadBoolean(); // Read mesh id. int meshId = br.ReadInt32(); if( meshId != -1 ) { xform.mesh = allMeshes[meshId]; } // Read children count. int childCount = br.ReadInt32(); // Read all children ids. for( int currChild = 0; currChild < childCount; ++currChild ) { int childId = br.ReadInt32(); xform.children.Add(allXforms[childId]); } allXforms.Add(xform); } br.Close(); }
private void computeMeshData() { foreach( MayaMesh mesh in allMeshes ) { // Get the Maya mesh MFnMesh mayaMesh = new MFnMesh(mesh.mayaObjectPath); // Does the maya mesh have UVs? MStringArray uvSetNames = new MStringArray(); mayaMesh.getUVSetNames(uvSetNames); bool hasUvs = (uvSetNames.length > 0) && (mayaMesh.numUVs(uvSetNames[0]) > 0); // Iterate through all of the vertices and build the data. MItMeshFaceVertex it = new MItMeshFaceVertex(mesh.mayaObjectPath); while( !it.isDone ) { // Create a new vertex and populate its data. Vertex vert = new Vertex(); // Get the local position relative to the world origin. MPoint mayaPos = it.position(MSpace.Space.kObject); vert.position = new Vector3((float)mayaPos.x, (float)mayaPos.y, (float)mayaPos.z); //vert.position = new Vector3((float)mayaPos.x - mesh.sourceXForm.mayaWorldPosition.x, //(float)mayaPos.y - mesh.sourceXForm.mayaWorldPosition.y, //(float)mayaPos.z - mesh.sourceXForm.mayaWorldPosition.z); // Get the normal. MVector mayaNrm = new MVector(); it.getNormal(mayaNrm, MSpace.Space.kObject); vert.normal = new Vector3((float)mayaNrm.x, (float)mayaNrm.y, (float)mayaNrm.z); // Texcoords. if( hasUvs && it.hasUVsProperty ) { float[] mayaUvs = new float[2]; it.getUV(mayaUvs, uvSetNames[0]); vert.texcoord = new Vector2(mayaUvs[0], mayaUvs[1]); } // Append the vertex. mesh.vertices.Add(vert); it.next(); } // Get all index data. MIntArray mia1 = new MIntArray(); MIntArray mia2 = new MIntArray(); mayaMesh.getTriangleOffsets(mia1, mia2); foreach( int idx in mia2 ) { mesh.indices.Add((uint)idx); } } }