Exemplo n.º 1
0
        private void read_MAT_ENTRY(int chunk_len)
        {
            long chunk_end = filePos() + chunk_len;

            Material3ds mat = new Material3ds();
            addMaterial(mat);

            if (mDecode != null)
            {
                mDecode.enter();
            }

            while (filePos() < chunk_end)
            {
                Head head = read_HEAD();
                switch (head.id)
                {
                    case CHUNK_MAT_NAME:
                        mat.mName = ReadName();
                        break;
                    case CHUNK_MAT_AMBIENT:
                        mat.mAmbient = readColor(head.length - 6);
                        break;
                    case CHUNK_MAT_SPECULAR:
                        mat.mSpecular = readColor(head.length - 6);
                        break;
                    case CHUNK_MAT_DIFFUSE:
                        mat.mDiffuse = readColor(head.length - 6);
                        break;
                    case CHUNK_MAT_MAPNAME:
                        mat.mMapName = ReadName();
                        break;
                    case CHUNK_MAT_MAP:
                        read_MAT_ENTRY(head.length - 6);
                        break;
                    //case CHUNK_MAT_SHININESS:
                    //mat._shininess = readFloat();
                    //break;
                    case CHUNK_MAT_TRANSPARENCY:
                        mat._transparency = readPercentage(head.length - 6);
                        mat._transparency = 1.0f - mat._transparency;	// values come form 3ds inverted, so we need to invert it here again. 0 is full transparent, 1 is opaque.
                        break;
                    default:
                        SkipChunk(head.length - 6);
                        break;
                }
            }

            if (mDecode != null)
            {
                mDecode.leave();
            }
        }
Exemplo n.º 2
0
        public override Scene ImportFile(FileStream fileStream, string fileName)
        {
            Scene3ds scene3ds = new Scene3ds(fileStream, null, Scene3ds.DECODE_ALL);

            Scene result = new Scene();

            // Compute normals for our scene
            computeNormals(scene3ds, result);

            Material defaultMaterial = new Material
            {
                Name          = "Default",
                FileName      = fileName,
                DiffuseColor  = ColorsRgbF.Gray,
                SpecularColor = ColorsRgbF.White,
                Shininess     = 64
            };

            for (int i = 0; i < scene3ds.Materials.Count(); ++i)
            {
                Material3ds material3ds = scene3ds.Materials.ElementAt(i);
                Material    material    = new Material
                {
                    Name          = material3ds.name(),
                    FileName      = fileName,
                    DiffuseColor  = material3ds.diffuse().ToColorRgbF(),
                    AmbientColor  = material3ds.ambient().ToColorRgbF(),
                    SpecularColor = material3ds.specular().ToColorRgbF(),
                    Transparency  = material3ds.transparency()
                };
                if (material3ds.DiffuseTexture != null)
                {
                    material.DiffuseTextureName = Path.Combine(Path.GetDirectoryName(fileName), material3ds.DiffuseTexture.MapName);
                }
                if ((int)material3ds._shininess != 0)
                {
                    material.Shininess = (int)material3ds._shininess;
                }
                result.Materials.Add(material);
            }

            for (int i = 0; i < scene3ds.Meshes.Count(); ++i)
            {
                Mesh3ds mesh3ds = scene3ds.Meshes.ElementAt(i);

                Mesh resultMesh = result.Meshes[i];

                //resultMesh.Material = defaultMaterial;

                // TODO: Is this right?
                if (mesh3ds.faceMats() == 0)
                {
                    resultMesh.Material = defaultMaterial;

                    for (int fi = 0; fi < mesh3ds.faces(); ++fi)
                    {
                        Face3ds f = mesh3ds.face(fi);


                        resultMesh.Indices.Add(f.P0);
                        resultMesh.Indices.Add(f.P1);
                        resultMesh.Indices.Add(f.P2);
                    }
                }
                else
                {
                    // TODO: Split by face materials so that each mesh only has one material.
                    Debug.Assert(mesh3ds.faceMats() == 1);
                    for (int fm = 0; fm < mesh3ds.faceMats(); ++fm)
                    {
                        // Get current material's face.
                        FaceMat3ds fmat     = mesh3ds.faceMat(fm);
                        Material   material = result.Materials[fmat.material()];
                        resultMesh.Material = material;

                        for (int fi = 0; fi < fmat.faces(); ++fi)
                        {
                            int     idx = fmat.face(fi);
                            Face3ds f   = mesh3ds.face(idx);


                            resultMesh.Indices.Add(f.P0);
                            resultMesh.Indices.Add(f.P1);
                            resultMesh.Indices.Add(f.P2);
                        }
                    }
                }
            }

            return(result);
        }
Exemplo n.º 3
0
 // Add material at end of material array
 private void addMaterial(Material3ds m)
 {
     _materials.Add(m);
 }