Engines/FlatRedBallXNA/FlatRedBall.Content/ModelAnimation/X File/XModelImporter.cs
// 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>
Imports a material, which defines the textures that a mesh uses and the way in which
light reflects off the mesh
Imports a material which defines the textures that a mesh uses and the way in which light reflects off the mesh
private MaterialContent ImportMaterial()
        {
            ExternalReference<TextureContent> texRef = null;
            BasicMaterialContent basicMaterial = new BasicMaterialContent();
            MaterialContent returnMaterial = basicMaterial;
            string materialName = tokens.ReadName();
            if (materialName == null)
                materialName = "";
            Vector4 diffuseColor4 = tokens.NextVector4();
            Vector3 diffuseColor = new Vector3(
                diffuseColor4.X, diffuseColor4.Y, diffuseColor4.Z);
            float specularPower = tokens.NextFloat();
            Vector3 specularColor = tokens.NextVector3();
            Vector3 emissiveColor = tokens.NextVector3();
            for (string token = tokens.NextToken();
                token != "}"; )
            {
                if (token == "TextureFilename" || token=="TextureFileName")
                {
                    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;
        }
private MaterialContent ImportMaterial ( ) { ExternalReference < TextureContent > texRef = null ; BasicMaterialContent basicMaterial = new BasicMaterialContent ( ) ; MaterialContent returnMaterial = basicMaterial ; string materialName = tokens . ReadName ( ) ; if ( materialName == null ) materialName = " " ; Vector4 diffuseColor4 = tokens . NextVector4 ( ) ; Vector3 diffuseColor = new Vector3 ( diffuseColor4 . X , diffuseColor4 . Y , diffuseColor4 . Z ) ; float specularPower = tokens . NextFloat ( ) ; Vector3 specularColor = tokens . NextVector3 ( ) ; Vector3 emissiveColor = tokens . NextVector3 ( ) ; for ( string token = tokens . NextToken ( ) ; token != " " ; ) { if ( token == " " || token == " " ) { 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 ( ) , " " + path + " \n " ) ; path = GetAbsolutePath ( Path . GetFileName ( fileName ) ) ; } texRef = new ExternalReference < TextureContent > ( path ) ; } tokens . SkipToken ( ) ; } else if ( token == " " ) 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 ; }
Imports a material, which defines the textures that a mesh uses and the way in which
light reflects off the mesh
Imports a material which defines the textures that a mesh uses and the way in which light reflects off the mesh
// make sure name isn't null // Diffuse color describes how diffuse (directional) light // reflects off the mesh // Old code //Vector3 diffuseColor = new Vector3(tokens.NextFloat(), //    tokens.NextFloat(), tokens.NextFloat()); // Specular power is inversely exponentially proportional to the // strength of specular light // Specular color describes how specular (directional and shiny) // light reflects off the mesh // Import any textures associated with this material // Milkshape exports with capital N on name // Get the absolute path of the texture