Пример #1
0
        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();
                }
            }
        }
Пример #2
0
        protected void ParseDataObjectMaterial(out Material material)
        {
            string matName;
            ReadHeadOfDataObject(out matName);
            if (matName.Length == 0)
            {
                matName = "material" + lineNumber;
            }
            material = new Material();
            material.Name = matName;
            material.IsReference = false;

            // read material values
            material.Diffuse = ReadRGBA();
            material.SpecularExponent = ReadFloat();
            material.Specular = ReadRGB();
            material.Emissive = ReadRGB();

            // read other data objects
            bool running = true;
            material.Textures = new List<TexEntry>();
            while (running)
            {
                string objectName = GetNextToken();
                if (objectName.Length == 0)
                    ThrowException("Unexpected end of file while parsing mesh material");
                else if (objectName == "}")
                {
                    break; // material finished
                }
                else if (objectName == "TextureFilename" || objectName == "TextureFileName")
                {
                    // some exporters write "TextureFileName" instead.
                    string texname = string.Empty;
                    ParseDataObjectTextureFilename(ref texname);
                    material.Textures.Add(new TexEntry(texname));
                }
                else if (objectName == "NormalmapFilename" || objectName == "NormalmapFileName")
                {
                    // one exporter writes out the normal map in a separate filename tag
                    string texname = string.Empty;
                    ParseDataObjectTextureFilename(ref texname);
                    material.Textures.Add(new TexEntry(texname, true));
                }
                else
                {
                    Debug.WriteLine("Unknown data object in material in x file");
                    ParseUnknownDataObject();
                }
            }
        }