private static MeshDef ParaseChunk(MainChunk main) { MeshDef def = new MeshDef(); foreach (MaterialChunk chunk in main.editor.materials) { MeshDef.MaterialDef mat = def.addMaterial(chunk.name.Value); mat.texture = chunk.texture.Value; //mat.specular = chunk.specular.Color; //mat.ambient = chunk.ambient.Color; //mat.diffuse = chunk.diffuse.Color; } foreach (ObjectChunk chunk in main.editor.objects) { int ibase = def.points.Count; for (int i = 0; i < chunk.trimesh.points.Count; ++i) { def.addPoint(chunk.trimesh.coordsys.translate(chunk.trimesh.points[i]), -1); def.AddUv(chunk.trimesh.mapping[i]); } foreach (FaceMaterialChunk fmc in chunk.trimesh.facematerials) { def.selectMaterial(fmc.name); foreach (int faceindex in fmc.faces) { MeshDef.VertexData[] vd = new MeshDef.VertexData[3]; TriMeshChunk.Poly p = chunk.trimesh.faces[faceindex]; vd[0].normal = -1; vd[0].vertex = vd[0].uv = ibase + p.a; vd[1].normal = -1; vd[1].vertex = vd[1].uv = ibase + p.b; vd[2].normal = -1; vd[2].vertex = vd[2].uv = ibase + p.c; def.addTri(new MeshDef.Tri(vd)); } } } return(def); }
internal static MeshDef ExtractMeshDefinition(Model model) { MeshDef def = new MeshDef(); int id = 0; foreach (Material mat in model.materials) { MeshDef.MaterialDef smat = def.addMaterial("m" + id.ToString()); smat.texture = SmartTexture(mat.diffuseTexture); ++id; } foreach (Bone b in model.bones) { MeshDef.Bone bn = def.newBone(); bn.pos = new vec3(b.x, b.y, b.z); bn.rot = makeQuat(new vec3(b.rx, b.ry, b.rz)); bn.parent = b.parentId; bn.name = b.name; } int vadded = 0; int vbase = 0; int nadded = 0; int nbase = 0; foreach (Mesh me in model.meshes) { vbase += vadded; vadded = 0; nbase += nadded; nadded = 0; def.selectMaterial("m" + me.materialId); foreach (Vertex v in me.vertices) { def.addPoint(v.pos, v.bone); def.AddUv(new vec2(v.u, 1 - v.v)); ++vadded; } foreach (Normal n in me.normals) { def.addNomal(n.norm); ++nadded; } foreach (Tri tr in me.tris) { MeshDef.VertexData[] data = new MeshDef.VertexData[3]; data[0].uv = data[0].vertex = vbase + tr.v1; data[1].uv = data[1].vertex = vbase + tr.v2; data[2].uv = data[2].vertex = vbase + tr.v3; data[0].normal = nbase + tr.n1; data[1].normal = nbase + tr.n2; data[2].normal = nbase + tr.n3; def.addTri(new MeshDef.Tri(data)); } } return(def); }