/// <summary> /// write a mesh entry of the scene /// </summary> protected void WriteMesh(Mesh mesh) { }
protected void ParseDataObjectSkinMeshHeader(ref Mesh mesh) { ReadHeadOfDataObject(); ReadInt(); ReadInt(); ReadInt(); CheckForClosingBrace(); }
protected void ParseDataObjectSkinWeights(ref Mesh mesh) { ReadHeadOfDataObject(); string transformNodeName; GetNextTokenAsString(out transformNodeName); Bone bone = new Bone(); mesh.Bones.Add(bone); bone.Name = transformNodeName; // read vertex weights uint numWeights = ReadInt(); for (uint a = 0; a < numWeights; a++) { BoneWeight weight = new BoneWeight(); weight.Vertex = ReadInt(); bone.Weights.Add(weight); } // read vertex weights for (int a = 0; a < numWeights; a++) { BoneWeight bw = bone.Weights[a]; bw.Weight = ReadFloat(); bone.Weights[a] = bw; } // read matrix offset bone.OffsetMatrix = new Matrix4x4(); bone.OffsetMatrix.M11 = ReadFloat(); bone.OffsetMatrix.M21 = ReadFloat(); bone.OffsetMatrix.M31 = ReadFloat(); bone.OffsetMatrix.M41 = ReadFloat(); bone.OffsetMatrix.M12 = ReadFloat(); bone.OffsetMatrix.M22 = ReadFloat(); bone.OffsetMatrix.M33 = ReadFloat(); bone.OffsetMatrix.M42 = ReadFloat(); bone.OffsetMatrix.M13 = ReadFloat(); bone.OffsetMatrix.M23 = ReadFloat(); bone.OffsetMatrix.M33 = ReadFloat(); bone.OffsetMatrix.M43 = ReadFloat(); bone.OffsetMatrix.M14 = ReadFloat(); bone.OffsetMatrix.M24 = ReadFloat(); bone.OffsetMatrix.M33 = ReadFloat(); bone.OffsetMatrix.M44 = ReadFloat(); CheckForSemicolon(); CheckForClosingBrace(); }
protected void ParseDataObjectMeshVertexColors(ref Mesh mesh) { ReadHeadOfDataObject(); if (mesh.NumColorSets + 1 > AI_MAX_NUMBER_OF_COLOR_SETS) ThrowException("Too many colorsets"); Color4D[] colors; uint numColors = ReadInt(); if (numColors != mesh.Positions.Count) ThrowException("Vertex color count does not match vertex count"); //colors.resize( numColors, aiColor4D( 0, 0, 0, 1)); colors = new Color4D[numColors]; for (uint a = 0; a < numColors; a++) { uint index = ReadInt(); if (index >= mesh.Positions.Count) ThrowException("Vertex color index out of bounds"); colors[(int)index] = ReadRGBA(); // HACK: (thom) Maxon Cinema XPort plugin puts a third separator here, kwxPort puts a comma. // Ignore gracefully. if (!isBinaryFormat) { FindNextNoneWhiteSpace(); if (buffer[p] == ';' || buffer[p] == ',') p++; } } mesh.Colors[(int)mesh.NumColorSets] = new List<Color4D>(colors); CheckForClosingBrace(); }
protected void ParseDataObjectMeshTextureCoords(ref Mesh mesh) { ReadHeadOfDataObject(); if (mesh.NumTextures + 1 > AI_MAX_NUMBER_OF_TEXTURECOORDS) { ThrowException("Too many sets of texture coordinates"); } uint numCoords = ReadInt(); if (numCoords != mesh.Positions.Count) { ThrowException("Texture coord count does not match vertex count"); } List<Vector2D> coords = new List<Vector2D>((int)numCoords); for (int a = 0; a < numCoords; a++) { coords.Add(ReadVector2()); } mesh.TexCoords[mesh.NumTextures++] = coords; CheckForClosingBrace(); }
protected void ParseDataObjectMeshNormals(ref Mesh mesh) { ReadHeadOfDataObject(); // read count uint numNormals = ReadInt(); mesh.Normals = new List<Vector3D>((int)numNormals); // read normal vectors for (uint a = 0; a < numNormals; a++) mesh.Normals.Add(ReadVector3()); // read normal indices uint numFaces = ReadInt(); if (numFaces != mesh.PosFaces.Count) ThrowException("Normal face count does not match vertex face count."); for (uint a = 0; a < numFaces; a++) { uint numIndices = ReadInt(); Face face = new Face(); face.Indices = new List<uint>(); for (uint b = 0; b < numIndices; b++) face.Indices.Add(ReadInt()); mesh.NormalFaces.Add(face); TestForSeparator(); } CheckForClosingBrace(); }
protected void ParseDataObjectMeshMaterialList(ref Mesh mesh) { ReadHeadOfDataObject(); // read material count /*unsigned int numMaterials =*/ ReadInt(); // read non triangulated face material index count uint numMatIndices = ReadInt(); // some models have a material index count of 1... to be able to read them we // replicate this single material index on every face if (numMatIndices != mesh.PosFaces.Count && numMatIndices != 1) ThrowException("Per-Face material index count does not match face count."); // read per-face material indices for (uint a = 0; a < numMatIndices; a++) mesh.FaceMaterials.Add(ReadInt()); // in version 03.02, the face indices end with two semicolons. // commented out version check, as version 03.03 exported from blender also has 2 semicolons if (!isBinaryFormat) // && MajorVersion == 3 && MinorVersion <= 2) { if (p < end && buffer[p] == ';') ++p; } // if there was only a single material index, replicate it on all faces while (mesh.FaceMaterials.Count < mesh.PosFaces.Count) mesh.FaceMaterials.Add(mesh.FaceMaterials[0]); // read following data objects bool running = true; while (running) { string objectName = GetNextToken(); if (objectName.Length == 0) ThrowException("Unexpected end of file while parsing mesh material list."); else if (objectName == "}") break; // material list finished else if (objectName == "{") { // template materials string matName = GetNextToken(); Material material = new Material(); material.IsReference = true; material.Name = matName; mesh.Materials.Add(material); CheckForClosingBrace(); // skip } } else if (objectName == "Material") { Material material; ParseDataObjectMaterial(out material); mesh.Materials.Add(material); } else if (objectName == ";") { // ignore } else { Debug.WriteLine("Unknown data object in material list in x file"); ParseUnknownDataObject(); } } }
protected void ParseDataObjectMesh(out Mesh mesh) { mesh = new Mesh(); string name; ReadHeadOfDataObject(out name); // read vertex count uint numVertices = ReadInt(); mesh.Positions = new List<Vector3D>((int)numVertices); // read vertices for (int a = 0; a < numVertices; a++) mesh.Positions.Add(ReadVector3()); // read position faces uint numPosFaces = ReadInt(); mesh.PosFaces = new List<Face>((int)numPosFaces); for (uint a = 0; a < numPosFaces; a++) { uint numIndices = ReadInt(); if (numIndices < 3) ThrowException(string.Format("Invalid index count {0} for face {1}.", numIndices, a)); // read indices Face face = new Face(); face.Indices = new List<uint>(); for (uint b = 0; b < numIndices; b++) face.Indices.Add(ReadInt()); mesh.PosFaces.Add(face); TestForSeparator(); } // here, other data objects may follow bool running = true; while (running) { string objectName = GetNextToken(); if (objectName.Length == 0) ThrowException("Unexpected end of file while parsing mesh structure"); else if (objectName == "}") break; // mesh finished else if (objectName == "MeshNormals") ParseDataObjectMeshNormals(ref mesh); else if (objectName == "MeshTextureCoords") ParseDataObjectMeshTextureCoords(ref mesh); else if (objectName == "MeshVertexColors") ParseDataObjectMeshVertexColors(ref mesh); else if (objectName == "MeshMaterialList") ParseDataObjectMeshMaterialList(ref mesh); else if (objectName == "VertexDuplicationIndices") ParseUnknownDataObject(); // we'll ignore vertex duplication indices else if (objectName == "XSkinMeshHeader") ParseDataObjectSkinMeshHeader(ref mesh); else if (objectName == "SkinWeights") ParseDataObjectSkinWeights(ref mesh); else { Debug.WriteLine("Unknown data object in mesh in x file"); ParseUnknownDataObject(); } } }