protected void ReadMaterials(XmlNode material_array) { if (material_array == null) { return; } int materialArray_size = int.Parse(material_array.Attributes["size"].Value); if (materialArray_size < 1) { return; } XmlNodeList materials = material_array.SelectNodes("material"); foreach (XmlNode material in materials) { string name = material.Attributes["name"].Value; int index = int.Parse(material.Attributes["index"].Value); bool[] lights = new bool[4]; for (int i = 0; i < 4; i++) { lights[i] = material.Attributes["light" + i].Value.Equals("on"); } string face = material.Attributes["face"].Value; ModelBase.MaterialDef.PolygonDrawingFace polygonDrawingFace; switch (face) { case "front": polygonDrawingFace = ModelBase.MaterialDef.PolygonDrawingFace.Front; break; case "back": polygonDrawingFace = ModelBase.MaterialDef.PolygonDrawingFace.Back; break; case "both": polygonDrawingFace = ModelBase.MaterialDef.PolygonDrawingFace.FrontAndBack; break; default: goto case "front"; } int alpha = int.Parse(material.Attributes["alpha"].Value); bool wire_mode = material.Attributes["wire_mode"].Value.Equals("on"); string polygon_mode = material.Attributes["polygon_mode"].Value; ModelBase.MaterialDef.PolygonMode polygonMode; switch (polygon_mode) { case "modulate": polygonMode = ModelBase.MaterialDef.PolygonMode.Modulation; break; case "decal": polygonMode = ModelBase.MaterialDef.PolygonMode.Decal; break; case "toon_highlight": polygonMode = ModelBase.MaterialDef.PolygonMode.Toon_HighlightShading; break; case "shadow": polygonMode = ModelBase.MaterialDef.PolygonMode.Shadow; break; default: goto case "modulate"; } int polygon_id = int.Parse(material.Attributes["polygon_id"].Value); bool fog_flag = material.Attributes["fog_flag"].Value.Equals("on"); bool depth_test_decal = material.Attributes["depth_test_decal"].Value.Equals("on"); bool translucent_update_depth = material.Attributes["translucent_update_depth"].Value.Equals("on"); bool render_1_pixel = material.Attributes["render_1_pixel"].Value.Equals("on"); bool far_clipping = material.Attributes["far_clipping"].Value.Equals("on"); byte[] diffuse = Array.ConvertAll <string, byte>( material.Attributes["diffuse"].Value.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries), Convert.ToByte); byte[] ambient = Array.ConvertAll <string, byte>( material.Attributes["ambient"].Value.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries), Convert.ToByte); byte[] specular = Array.ConvertAll <string, byte>( material.Attributes["specular"].Value.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries), Convert.ToByte); byte[] emission = Array.ConvertAll <string, byte>( material.Attributes["emission"].Value.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries), Convert.ToByte); bool shininess_table_flag = material.Attributes["shininess_table_flag"].Value.Equals("on"); int tex_image_idx = int.Parse(material.Attributes["tex_image_idx"].Value); int tex_palette_idx = int.Parse(material.Attributes["tex_palette_idx"].Value); ModelBase.MaterialDef.TexTiling[] tex_tiling = new ModelBase.MaterialDef.TexTiling[2]; string[] tmpsplit = (material.Attributes["tex_tiling"] != null) ? material.Attributes["tex_tiling"].Value.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries) : null; if (tex_image_idx >= 0) { for (int i = 0; i < 2; i++) { switch (tmpsplit[i]) { case "clamp": tex_tiling[i] = ModelBase.MaterialDef.TexTiling.Clamp; break; case "repeat": tex_tiling[i] = ModelBase.MaterialDef.TexTiling.Repeat; break; case "flip": tex_tiling[i] = ModelBase.MaterialDef.TexTiling.Flip; break; default: goto case "repeat"; } } } float[] tex_scale = (tex_image_idx >= 0) ? Array.ConvertAll( material.Attributes["tex_scale"].Value.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries), Convert.ToSingle) : null; float tex_rotate = (tex_image_idx >= 0) ? float.Parse(material.Attributes["tex_rotate"].Value, Helper.USA) : 0f; float[] tex_translate = (tex_image_idx >= 0) ? Array.ConvertAll( material.Attributes["tex_translate"].Value.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries), Convert.ToSingle) : null; string tex_gen_mode = (tex_image_idx >= 0) ? material.Attributes["tex_gen_mode"].Value : null; ModelBase.TexGenMode texGenMode; switch (tex_gen_mode) { case "none": texGenMode = ModelBase.TexGenMode.None; break; case null: goto case "none"; case "tex": texGenMode = ModelBase.TexGenMode.Tex; break; case "nrm": texGenMode = ModelBase.TexGenMode.Normal; break; case "pos": texGenMode = ModelBase.TexGenMode.Pos; break; default: goto case "none"; } string tex_gen_st_src = (material.Attributes["tex_gen_st_src"] != null) ? material.Attributes["tex_gen_st_src"].Value : null; string tex_effect_mtx = (material.Attributes["tex_effect_mtx"] != null) ? material.Attributes["tex_effect_mtx"].Value : null; ModelBase.MaterialDef matDef = new ModelBase.MaterialDef(name, index); matDef.m_TextureDefID = (tex_image_idx >= 0) ? m_Model.m_Textures.Values.ElementAt(tex_image_idx).m_ID : null; matDef.m_Lights = lights; matDef.m_PolygonDrawingFace = polygonDrawingFace; matDef.m_Alpha = (int)((alpha / 31f) * 255f); matDef.m_WireMode = wire_mode; matDef.m_PolygonMode = polygonMode; matDef.m_FogFlag = fog_flag; matDef.m_DepthTestDecal = depth_test_decal; matDef.m_RenderOnePixelPolygons = render_1_pixel; matDef.m_FarClipping = far_clipping; matDef.m_Diffuse = Color.FromArgb( (int)(diffuse[0] / 31f * 255f), (int)(diffuse[1] / 31f * 255f), (int)(diffuse[2] / 31f * 255f)); matDef.m_Ambient = Color.FromArgb( (int)(ambient[0] / 31f * 255f), (int)(ambient[1] / 31f * 255f), (int)(ambient[2] / 31f * 255f)); matDef.m_Specular = Color.FromArgb( (int)(specular[0] / 31f * 255f), (int)(specular[1] / 31f * 255f), (int)(specular[2] / 31f * 255f)); matDef.m_Emission = Color.FromArgb( (int)(emission[0] / 31f * 255f), (int)(emission[1] / 31f * 255f), (int)(emission[2] / 31f * 255f)); matDef.m_TexTiling = tex_tiling; matDef.m_TextureScale = (tex_scale != null) ? new Vector2(tex_scale[0], tex_scale[1]) : Vector2.One; matDef.m_TextureRotation = tex_rotate; matDef.m_TextureTranslation = (tex_translate != null) ? new Vector2(tex_translate[0], tex_translate[1]) : Vector2.Zero; matDef.m_TexGenMode = texGenMode; m_Model.m_Materials.Add(matDef.m_ID, matDef); } }
protected void ReadMaterials(XmlNode material_array) { if (material_array == null) return; int materialArray_size = int.Parse(material_array.Attributes["size"].Value); if (materialArray_size < 1) return; XmlNodeList materials = material_array.SelectNodes("material"); foreach (XmlNode material in materials) { string name = material.Attributes["name"].Value; int index = int.Parse(material.Attributes["index"].Value); bool[] lights = new bool[4]; for (int i = 0; i < 4; i++) lights[i] = material.Attributes["light" + i].Value.Equals("on"); string face = material.Attributes["face"].Value; ModelBase.MaterialDef.PolygonDrawingFace polygonDrawingFace; switch (face) { case "front": polygonDrawingFace = ModelBase.MaterialDef.PolygonDrawingFace.Front; break; case "back": polygonDrawingFace = ModelBase.MaterialDef.PolygonDrawingFace.Back; break; case "both": polygonDrawingFace = ModelBase.MaterialDef.PolygonDrawingFace.FrontAndBack; break; default: goto case "front"; } int alpha = int.Parse(material.Attributes["alpha"].Value); bool wire_mode = material.Attributes["wire_mode"].Value.Equals("on"); string polygon_mode = material.Attributes["polygon_mode"].Value; ModelBase.MaterialDef.PolygonMode polygonMode; switch (polygon_mode) { case "modulate": polygonMode = ModelBase.MaterialDef.PolygonMode.Modulation; break; case "decal": polygonMode = ModelBase.MaterialDef.PolygonMode.Decal; break; case "toon_highlight": polygonMode = ModelBase.MaterialDef.PolygonMode.Toon_HighlightShading; break; case "shadow": polygonMode = ModelBase.MaterialDef.PolygonMode.Shadow; break; default: goto case "modulate"; } int polygon_id = int.Parse(material.Attributes["polygon_id"].Value); bool fog_flag = material.Attributes["fog_flag"].Value.Equals("on"); bool depth_test_decal = material.Attributes["depth_test_decal"].Value.Equals("on"); bool translucent_update_depth = material.Attributes["translucent_update_depth"].Value.Equals("on"); bool render_1_pixel = material.Attributes["render_1_pixel"].Value.Equals("on"); bool far_clipping = material.Attributes["far_clipping"].Value.Equals("on"); byte[] diffuse = Array.ConvertAll<string, byte>( material.Attributes["diffuse"].Value.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries), Convert.ToByte); byte[] ambient = Array.ConvertAll<string, byte>( material.Attributes["ambient"].Value.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries), Convert.ToByte); byte[] specular = Array.ConvertAll<string, byte>( material.Attributes["specular"].Value.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries), Convert.ToByte); byte[] emission = Array.ConvertAll<string, byte>( material.Attributes["emission"].Value.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries), Convert.ToByte); bool shininess_table_flag = material.Attributes["shininess_table_flag"].Value.Equals("on"); int tex_image_idx = int.Parse(material.Attributes["tex_image_idx"].Value); int tex_palette_idx = int.Parse(material.Attributes["tex_palette_idx"].Value); ModelBase.MaterialDef.TexTiling[] tex_tiling = new ModelBase.MaterialDef.TexTiling[2]; string[] tmpsplit = (material.Attributes["tex_tiling"] != null) ? material.Attributes["tex_tiling"].Value.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries) : null; if (tex_image_idx >= 0) { for (int i = 0; i < 2; i++) { switch (tmpsplit[i]) { case "clamp": tex_tiling[i] = ModelBase.MaterialDef.TexTiling.Clamp; break; case "repeat": tex_tiling[i] = ModelBase.MaterialDef.TexTiling.Repeat; break; case "flip": tex_tiling[i] = ModelBase.MaterialDef.TexTiling.Flip; break; default: goto case "repeat"; } } } float[] tex_scale = (tex_image_idx >= 0) ? Array.ConvertAll( material.Attributes["tex_scale"].Value.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries), Convert.ToSingle) : null; float tex_rotate = (tex_image_idx >= 0) ? float.Parse(material.Attributes["tex_rotate"].Value, Helper.USA) : 0f; float[] tex_translate = (tex_image_idx >= 0) ? Array.ConvertAll( material.Attributes["tex_translate"].Value.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries), Convert.ToSingle) : null; string tex_gen_mode = (tex_image_idx >= 0) ? material.Attributes["tex_gen_mode"].Value : null; ModelBase.TexGenMode texGenMode; switch (tex_gen_mode) { case "none": texGenMode = ModelBase.TexGenMode.None; break; case null: goto case "none"; case "tex": texGenMode = ModelBase.TexGenMode.Tex; break; case "nrm": texGenMode = ModelBase.TexGenMode.Normal; break; case "pos": texGenMode = ModelBase.TexGenMode.Pos; break; default: goto case "none"; } string tex_gen_st_src = (material.Attributes["tex_gen_st_src"] != null) ? material.Attributes["tex_gen_st_src"].Value : null; string tex_effect_mtx = (material.Attributes["tex_effect_mtx"] != null) ? material.Attributes["tex_effect_mtx"].Value : null; ModelBase.MaterialDef matDef = new ModelBase.MaterialDef(name, index); matDef.m_TextureDefID = (tex_image_idx >= 0) ? m_Model.m_Textures.Values.ElementAt(tex_image_idx).m_ID : null; matDef.m_Lights = lights; matDef.m_PolygonDrawingFace = polygonDrawingFace; matDef.m_Alpha = (int)((alpha / 31f) * 255f); matDef.m_WireMode = wire_mode; matDef.m_PolygonMode = polygonMode; matDef.m_FogFlag = fog_flag; matDef.m_DepthTestDecal = depth_test_decal; matDef.m_RenderOnePixelPolygons = render_1_pixel; matDef.m_FarClipping = far_clipping; matDef.m_Diffuse = Color.FromArgb( (int)(diffuse[0] / 31f * 255f), (int)(diffuse[1] / 31f * 255f), (int)(diffuse[2] / 31f * 255f)); matDef.m_Ambient = Color.FromArgb( (int)(ambient[0] / 31f * 255f), (int)(ambient[1] / 31f * 255f), (int)(ambient[2] / 31f * 255f)); matDef.m_Specular = Color.FromArgb( (int)(specular[0] / 31f * 255f), (int)(specular[1] / 31f * 255f), (int)(specular[2] / 31f * 255f)); matDef.m_Emission = Color.FromArgb( (int)(emission[0] / 31f * 255f), (int)(emission[1] / 31f * 255f), (int)(emission[2] / 31f * 255f)); matDef.m_TexTiling = tex_tiling; matDef.m_TextureScale = (tex_scale != null) ? new Vector2(tex_scale[0], tex_scale[1]) : Vector2.One; matDef.m_TextureRotation = tex_rotate; matDef.m_TextureTranslation = (tex_translate != null) ? new Vector2(tex_translate[0], tex_translate[1]) : Vector2.Zero; matDef.m_TexGenMode = texGenMode; m_Model.m_Materials.Add(matDef.m_ID, matDef); } }