Exemplo n.º 1
0
            // template MeshNormals
            // {
            //     DWORD nNormals;
            //     array Vector normals[nNormals];
            //     DWORD nFaceNormals;
            //     array MeshFace faceNormals[nFaceNormals];
            // }
            /// <summary>
            /// Imports the normals associated with the current mesh.
            /// </summary>
            private void ImportNormals()
            {
                tokens.ReadName();
                hasNormals = true;

                int numNormals = tokens.NextInt();

                if (numNormals == mesh.Positions.Count)
                {
                    normals = new Vector3[numNormals];
                }
                else
                {
                    hasNormals = false;
                }

                for (int i = 0; i < numNormals; i++)
                {
                    Vector3 norm = tokens.NextVector3();
                    if (numNormals == mesh.Positions.Count)
                    {
                        normals[i]    = norm;
                        normals[i].Z *= -1;
                    }
                }

                int numFaces = tokens.NextInt();

                for (int i = 0; i < numFaces; i++)
                {
                    int numNormalsPerFace = tokens.NextInt();
                    tokens.SkipTokens(2 * numNormalsPerFace + 1);
                }
                // end of mesh normals
                tokens.SkipToken();
            }
Exemplo 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);
        }