/// <summary> /// Reads in the vertex positions and vertex indices for the mesh /// </summary> private void InitializeMesh() { // This will turn into the ModelMeshPart //geom = new GeometryContent(); // mesh.Geometry.Add(geom); int numVerts = -1; int i = -1; try { numVerts = tokens.NextInt(); // read the verts and create one boneweight collection for each vert // which will represent that vertex's skinning info (which bones influence it // and the weights of each bone's influence on the vertex) for (i = 0; i < numVerts; i++) { skinInfo.Add(new BoneWeightCollection()); Vector3 v = tokens.NextVector3(); // Reflect each vertex across z axis to convert it from left hand to right // hand v.Z *= -1; mesh.Positions.Add(v); } } catch (Exception e) { throw new Exception("Error trying to parse the verts. The model claims to have " + numVerts + " verts. We were able to parse " + i + " verts"); } // Add the indices that describe the order in which // the vertices are rendered int numFaces = -1; i = -1; try { numFaces = tokens.NextInt(); faces = new Face[numFaces]; for (i = 0; i < numFaces; i++) { int numVertsPerFace = tokens.NextInt(); faces[i].VertexIndices = new int[numVertsPerFace]; for (int j = 0; j < numVertsPerFace; j++) { faces[i].VertexIndices[j] = tokens.NextInt(); } tokens.SkipToken(); } } catch (Exception e) { string error = "Error trying to parse the faces. The model claims to have " + numFaces + " faces. We were able to parse " + i + " faces"; error += "\nThe parser parsed " + numVerts + " verts. Should there really be that many?"; throw new Exception(error); } }
// template Material // { // ColorRGBA faceColor; // FLOAT power; // ColorRGB specularColor; // ColorRGB emissiveColor; // [...] // } /// <summary> /// Imports a material, which defines the textures that a mesh uses and the way in which /// light reflects off the mesh /// </summary> private MaterialContent ImportMaterial() { ExternalReference <TextureContent> texRef = null; BasicMaterialContent basicMaterial = new BasicMaterialContent(); MaterialContent returnMaterial = basicMaterial; // make sure name isn't null string materialName = tokens.ReadName(); if (materialName == null) { materialName = ""; } // Diffuse color describes how diffuse (directional) light // reflects off the mesh Vector4 diffuseColor4 = tokens.NextVector4(); Vector3 diffuseColor = new Vector3( diffuseColor4.X, diffuseColor4.Y, diffuseColor4.Z); // Old code //Vector3 diffuseColor = new Vector3(tokens.NextFloat(), // tokens.NextFloat(), tokens.NextFloat()); // Specular power is inversely exponentially proportional to the // strength of specular light float specularPower = tokens.NextFloat(); // Specular color describes how specular (directional and shiny) // light reflects off the mesh Vector3 specularColor = tokens.NextVector3(); Vector3 emissiveColor = tokens.NextVector3(); // Import any textures associated with this material for (string token = tokens.NextToken(); token != "}";) { // Milkshape exports with capital N on name if (token == "TextureFilename" || token == "TextureFileName") { // Get the absolute path of the texture string fileName = tokens.SkipName().NextString(); if (fileName.TrimStart(' ', '"').TrimEnd(' ', '"') != "") { string path = GetAbsolutePath(fileName); if (Path.IsPathRooted(fileName) && !System.IO.File.Exists(path)) { context.Logger.LogWarning("", new ContentIdentity(), "An absolute texture path that does not exist is stored in an .X file: " + path + "\n Attempting to find texture via relative path."); path = GetAbsolutePath(Path.GetFileName(fileName)); } texRef = new ExternalReference <TextureContent>(path); } tokens.SkipToken(); } else if (token == "EffectInstance") { returnMaterial = ImportCustomMaterial(); } else if (token == "{") { tokens.SkipNode(); } token = tokens.NextToken(); } if (returnMaterial is BasicMaterialContent) { basicMaterial.Texture = texRef; basicMaterial.DiffuseColor = diffuseColor; basicMaterial.EmissiveColor = emissiveColor; basicMaterial.SpecularColor = specularColor; basicMaterial.SpecularPower = specularPower; } returnMaterial.Name = materialName; if (returnMaterial.Name != null) { if (materials.ContainsKey(returnMaterial.Name)) { materials.Remove(returnMaterial.Name); } materials.Add(returnMaterial.Name, returnMaterial); } return(returnMaterial); }