public void AddMaterial(DxsMaterial material) { if (!_materials.ContainsKey(material.MaterialID)) { _materials.Add(material.MaterialID, material); } }
/// <summary> /// Parses a vertex found in a polygon's vertex list. Parses texture and /// lightmap coordinates based on the polygon's material type. /// </summary> /// <param name="xmlNode">Xml Poly node</param> /// <param name="dxsPrim">Dxs Primitive this Poly belongs to</param> /// <param name="dxsPoly">Dxs Poly the vertex belongs to</param> private void ParseVertex(XmlNode xmlNode, DxsPrimitive dxsPrim, DxsPoly dxsPoly) { //Get the vertex ID so we can look up the position info int vid = int.Parse(xmlNode.Attributes["vid"].Value); //Copy the master vertex (position + vertex color) DxsVertex vertex = new DxsVertex(dxsPrim.MasterVertexList[vid]); //Get the material of the polygon DxsMaterial material = dxsPrim.Materials[dxsPoly.MaterialID]; //If the first layer is a texture, parse texture coordinates if (material.Layers.Count > 0 && material.Layers[0].MaterialType == MaterialType.Texture) { float u0 = float.Parse(xmlNode.Attributes["u0"].Value); float v0 = float.Parse(xmlNode.Attributes["v0"].Value); vertex.TextureCoordinate = new Vector2(u0, v0); } //If one of the layers (last one) is a lightmap, parse its texture coordinates for (int i = 1; i < material.Layers.Count; i++) { if (material.Layers[i].MaterialType == MaterialType.Lightmap) { float ui = float.Parse(xmlNode.Attributes["u" + i].Value); float vi = float.Parse(xmlNode.Attributes["v" + i].Value); vertex.LightMapCoordinate = new Vector2(ui, vi); break; } } //Add vertex to the poly dxsPoly.Vertices.Add(vertex); }
/// <summary> /// Parse all the material nodes attached to the /// specified xml node. /// </summary> /// <param name="xmlNode">XmlNode that is parent to material nodes</param> private void ParseMaterials(XmlNode xmlNode) { for (int i = 0; i < xmlNode.ChildNodes.Count; i++) { //Get the material node XmlNode matNode = xmlNode.ChildNodes[i]; //Parse attributes int id = int.Parse(matNode.Attributes["id"].Value); String name = matNode.Attributes["name"].Value; bool used = bool.Parse(matNode.Attributes["used"].Value); //Only create a new material if its used if (used) { DxsMaterial dxsMaterial = new DxsMaterial(name, id); //Parse the layers of the material for (int j = 0; j < matNode.ChildNodes.Count; j++) { XmlNode child = matNode.ChildNodes[j]; //Ensure this is a layer node if (child.Name.Equals("layer")) { ParseLayer(child, dxsMaterial); } } //Finally add it to the master list materialList.Add(dxsMaterial.MaterialID, dxsMaterial); } } }
/// <summary> /// Parses a layer node that corresponds to the specified material. /// </summary> /// <param name="xmlNode">Xml layer node</param> /// <param name="dxsMaterial">Material associated with the layer</param> private void ParseLayer(XmlNode xmlNode, DxsMaterial dxsMaterial) { //Get the material and blend types, this tells us how to parse the data String type = xmlNode.Attributes["type"].Value; String blend = xmlNode.Attributes["blend"].Value; DxsLayer layer = new DxsLayer(); //First set the blending types switch (blend) { case "replace": layer.Blend = BlendType.Replace; break; case "add": layer.Blend = BlendType.Add; break; case "modulate": layer.Blend = BlendType.Modulate; break; case "alphablend": layer.Blend = BlendType.Alphablend; break; case "inactive": layer.Blend = BlendType.Inactive; break; } //Next the type of material switch (type) { case "color": layer.MaterialType = MaterialType.Color; XmlNode child = xmlNode.FirstChild; byte r = byte.Parse(child.Attributes["r"].Value); byte g = byte.Parse(child.Attributes["b"].Value); byte b = byte.Parse(child.Attributes["g"].Value); byte a = byte.Parse(child.Attributes["a"].Value); layer.Color = new Color(r, g, b, a); break; case "texture": layer.MaterialType = MaterialType.Texture; child = xmlNode.FirstChild; layer.TextureFile = child.Attributes["file"].Value; break; case "lightmap": layer.MaterialType = MaterialType.Lightmap; child = xmlNode.FirstChild; layer.TextureFile = child.Attributes["file"].Value; break; } dxsMaterial.Layers.Add(layer); }