Esempio n. 1
0
            /// <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 = 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 (int 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);
                }

                // Add the indices that describe the order in which
                // the vertices are rendered
                int numFaces = tokens.NextInt();

                faces = new Face[numFaces];
                for (int 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();
                }
            }
Esempio n. 2
0
        // 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
            Vector3 diffuseColor = new Vector3(tokens.NextFloat(),
                                               tokens.NextFloat(), tokens.NextFloat());

            // We dont care about the alpha component of diffuse light.
            // I don't even understand what this is useful for.
            tokens.NextFloat();
            // Specular power is inversely exponentially proportional to the
            // strength of specular light
            float specularPower = tokens.SkipToken().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);
        }