public MeshPart(MediaLoader ml, MeshDef.MaterialDef m, MeshDef def, Poseable poseable) { this.poseable = poseable; mat = new Material(m.diffuse, ml.fetch <Texture>(m.texture)); foreach (MeshDef.Tri tri in def.TrianglesFor(m)) { faces.Add(def.lookup(tri)); } }
private static void LoadMaterialLibrary(MeshDef mesh, FileSystem fs, string path) { MeshDef.MaterialDef mat = null; using (var file = fs.open(path)) { foreach (string l in FileUtil.LinesIn(file)) { string line = l.Trim(); if (string.IsNullOrEmpty(line) == false && line[0] != '#') { string[] data = line.Split(" \t".ToCharArray()); if (data[0] == "newmtl") { mat = mesh.addMaterial(data[1].Trim()); } else if (data[0] == "Ka") { mat.ambient = ParseColor(data); } else if (data[0] == "Ke") { mat.emissive = ParseColor(data); } else if (data[0] == "Kd") { mat.diffuse = ParseColor(data); } else if (data[0] == "Ks") { mat.specular = ParseColor(data); } else if (data[0] == "d" || data[0] == "Tr") { mat.alpha = floatParse(data[1]); } else if (data[0] == "Ns") { mat.shininess = floatParse(data[1]); } else if (data[0] == "map_Ka" || data[0] == "map_Kd") { mat.texture = Resolve(path, data[1]); } } } } }
internal static MeshDef Load(FileSystem fs, string path) { MeshDef def = new MeshDef(); using (Stream s = fs.open(path)) { BinaryReader br = new BinaryReader(s); int version = br.ReadInt32(); int materials = br.ReadInt32(); for (int materialid = 0; materialid < materials; ++materialid) { MeshDef.MaterialDef m = def.addMaterial("m" + materialid.ToString()); m.texture = br.ReadString(); m.ambient = vec3.Read(br); m.diffuse = vec3.Read(br); m.specular = vec3.Read(br); m.emissive = vec3.Read(br); m.alpha = br.ReadSingle(); m.shininess = br.ReadSingle(); } int bonecount = br.ReadInt32(); for (int boneid = 0; boneid < bonecount; ++boneid) { MeshDef.Bone bone = def.newBone(); bone.name = br.ReadString(); bone.parent = br.ReadInt32(); bone.pos = vec3.Read(br); vec3 qv = vec3.Read(br); bone.rot = new quat(br.ReadSingle(), qv); } int pointcount = br.ReadInt32(); for (int pointid = 0; pointid < pointcount; ++pointid) { int boneid = br.ReadInt32(); vec3 p = new vec3(br.ReadSingle(), br.ReadSingle(), br.ReadSingle()); def.addPoint(p, boneid); } int uvcount = br.ReadInt32(); for (int uvid = 0; uvid < uvcount; ++uvid) { vec2 v = new vec2(br.ReadSingle(), br.ReadSingle()); def.AddUv(v); } int normalcount = br.ReadInt32(); for (int normalid = 0; normalid < normalcount; ++normalid) { vec3 p = new vec3(br.ReadSingle(), br.ReadSingle(), br.ReadSingle()); def.addNomal(p); } for (int materialid = 0; materialid < materials; ++materialid) { def.selectMaterial("m" + materialid.ToString()); int tricount = br.ReadInt32(); for (int triid = 0; triid < tricount; ++triid) { MeshDef.VertexData[] data = new MeshDef.VertexData[3]; for (int i = 0; i < 3; ++i) { data[i].vertex = br.ReadInt32(); data[i].uv = br.ReadInt32(); data[i].normal = br.ReadInt32(); } def.addTri(new MeshDef.Tri(data)); } } } return(def); }