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); } }
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); }
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); } }