void ImportMaterialLibraries(ObjLine materialLine, BabylonScene scene)
        {
            for (int i = 1; i < materialLine.Tokens.Length; i++)
            {
                string fileName = materialLine.Tokens[i];

                var mtlDocument = new Document <MtlLine>(File.ReadAllText(fileName));

                StandardMaterial currentMaterial = null;

                foreach (var lines in mtlDocument.Blocks)
                {
                    foreach (MtlLine line in lines)
                    {
                        switch (line.Header)
                        {
                        case MtlHeader.Material:
                            currentMaterial = new StandardMaterial(line.Tokens[1]);
                            currentMaterial.BackFaceCulling = false;
                            materials.Add(currentMaterial);
                            break;

                        case MtlHeader.DiffuseColor:
                            currentMaterial.Diffuse = line.ToColor();
                            break;

                        case MtlHeader.DiffuseTexture:
                            currentMaterial.Diffuse        = new Color3(1, 1, 1);
                            currentMaterial.DiffuseTexture = line.Tokens[1].Replace("//", @"\");
                            break;

                        case MtlHeader.Alpha:
                            currentMaterial.Alpha = line.ToFloat();
                            break;

                        case MtlHeader.EmissiveColor:
                            currentMaterial.Emissive = line.ToColor();
                            break;

                        case MtlHeader.SpecularColor:
                            currentMaterial.Specular = line.ToColor();
                            break;

                        case MtlHeader.SpecularPower:
                            currentMaterial.SpecularPower = line.ToFloat();
                            break;
                        }
                    }
                }
            }

            foreach (var material in materials)
            {
                material.CreateBabylonMaterial(scene);
            }
        }
Esempio n. 2
0
        void AppendIndex(int index, ObjLine line)
        {
            string[] indices = line.Tokens[index].Split('/');

            // Required: Position
            int positionIndex = int.Parse(indices[0], CultureInfo.InvariantCulture) - positionsIndexOffset;
            int texCoordIndex = -1;
            int normalIndex   = -1;

            // Optional: Texture coordinate
            if (indices.Length > 1 && indices[1].Equals(string.Empty) == false)
            {
                texCoordIndex = int.Parse(indices[1]) - textureCoordinatesIndexOffset;
            }

            // Optional: Normal
            if (indices.Length > 2 && indices[2].Equals(string.Empty) == false)
            {
                normalIndex = int.Parse(indices[2]) - normalsIndexOffset;
            }

            // Build vertex
            var vertex = new PositionNormalTextured {
                Position = positions[positionIndex]
            };

            if (texCoordIndex >= 0)
            {
                vertex.TextureCoordinates = textureCoordinates[texCoordIndex];
            }

            if (normalIndex >= 0)
            {
                vertex.Normal = normals[normalIndex];
            }

            // check if the vertex does not already exists
            string hash = vertex.ToString();
            int    vertexIndex;

            if (!registeredVertices.TryGetValue(hash, out vertexIndex))
            {
                stagingVertices.Add(vertex);
                vertexIndex = stagingVertices.Count - 1;
                registeredVertices.Add(hash, vertexIndex);
            }

            stagingIndices.Add(vertexIndex);
        }
Esempio n. 3
0
        public void Element_Line_Valid()
        {
            var obj = new ObjFile();

            obj.Groups.Add(new ObjGroup("b"));
            obj.Vertices.Add(new ObjVertex(0, 0, 0));
            obj.Vertices.Add(new ObjVertex(0, 0, 0));
            obj.Vertices.Add(new ObjVertex(0, 0, 0));
            obj.Vertices.Add(new ObjVertex(0, 0, 0));
            var line = new ObjLine();

            line.Vertices.Add(new ObjTriplet(2, 0, 0));
            line.Vertices.Add(new ObjTriplet(3, 0, 0));
            line.Vertices.Add(new ObjTriplet(4, 0, 0));
            obj.Lines.Add(line);
            obj.Groups[0].Lines.Add(line);
            line.ObjectName                     = "a";
            line.LevelOfDetail                  = 2;
            line.MapName                        = "c";
            line.MaterialName                   = "d";
            line.SmoothingGroupNumber           = 10;
            line.IsBevelInterpolationEnabled    = true;
            line.IsColorInterpolationEnabled    = true;
            line.IsDissolveInterpolationEnabled = true;

            string text     = WriteObj(obj);
            string expected =
                @"v 0.000000 0.000000 0.000000
v 0.000000 0.000000 0.000000
v 0.000000 0.000000 0.000000
v 0.000000 0.000000 0.000000
g b
o a
lod 2
usemap c
usemtl d
s 10
bevel on
c_interp on
d_interp on
l 2 3 4
";

            AssertExtensions.TextEqual(expected, text);
        }
Esempio n. 4
0
        void AppendFace(ObjLine line)
        {
            // Line
            if (line.Tokens.Length == 3)
            {
                AppendIndex(1, line);
                AppendIndex(2, line);
                AppendIndex(2, line);

                return;
            }

            // Triangle
            if (line.Tokens.Length == 4)
            {
                for (int index = 1; index <= 3; index++)
                {
                    AppendIndex(index, line);
                }

                return;
            }

            // Quad
            if (line.Tokens.Length == 5)
            {
                for (int index = 1; index <= 3; index++)
                {
                    AppendIndex(index, line);
                }

                AppendIndex(1, line);
                AppendIndex(3, line);
                AppendIndex(4, line);
            }
        }