public void AddWeightData(SceneNodeContainer snc, PolygonObject polyOb, CAWeightTag weightTag, IEnumerable <int> range) { if (weightTag == null || polyOb == null) { return; } List <VertexWeightList> weightMap = new List <VertexWeightList>(); List <float4x4> bindingMatrices = new List <float4x4>(); foreach (int j in range) { using (CPolygon poly = polyOb.GetPolygonAt(j)) { foreach (int iVert in GetPolyPointIndices(poly, polyOb)) { var vertexWeights = new List <VertexWeight>(); for (int iJoint = 0; iJoint < weightTag.GetJointCount(); iJoint++) { double weight = weightTag.GetWeight(iJoint, iVert); // Leave out zero weights. This will save space for the sparse weight table. if (Math.Abs(weight) > double.Epsilon) { vertexWeights.Add(new VertexWeight { JointIndex = iJoint, Weight = (float)weight }); } } vertexWeights.Sort((vw1, vw2) => ((int)(vw1.Weight - vw2.Weight))); weightMap.Add(new VertexWeightList { VertexWeights = vertexWeights }); } } } for (int iJoint = 0; iJoint < weightTag.GetJointCount(); iJoint++) { // Add Binding Matrix JointRestState jointRestState = weightTag.GetJointRestState(iJoint); float4x4 mat = (float4x4)(jointRestState.m_oMi * weightTag.GetGeomMg()); bindingMatrices.Add(mat); } _weightObjects.Add(new WeightObject() { SceneNodeContainer = snc, WeightTag = weightTag, WeightMap = weightMap, BindingMatrices = bindingMatrices }); }
private static MeshComponent GetMesh(PolygonObject polyOb, float3[] normalsOb, UVWTag uvwTag, IEnumerable <int> range) { List <float3> normals = new List <float3>(); ushort nNewVerts = 0; VertexList verts = new VertexList(); List <float2> uvs = new List <float2>(); List <ushort> tris = new List <ushort>(); foreach (int i in range) { int iNorm = i * 4; double3 a, b, c, d; using (CPolygon poly = polyOb.GetPolygonAt(i)) { a = polyOb.GetPointAt(poly.a); b = polyOb.GetPointAt(poly.b); c = polyOb.GetPointAt(poly.c); d = polyOb.GetPointAt(poly.d); } float2 uvA = new float2(0, 0); float2 uvB = new float2(0, 1); float2 uvC = new float2(1, 1); float2 uvD = new float2(1, 0); if (uvwTag != null) { using (UVWStruct uvw = uvwTag.GetSlow(i)) { uvA = new float2((float)uvw.a.x, 1.0f - (float)uvw.a.y); uvB = new float2((float)uvw.b.x, 1.0f - (float)uvw.b.y); uvC = new float2((float)uvw.c.x, 1.0f - (float)uvw.c.y); uvD = new float2((float)uvw.d.x, 1.0f - (float)uvw.d.y); } } verts.Add((float3)a); verts.Add((float3)b); verts.Add((float3)c); uvs.Add(uvA); uvs.Add(uvB); uvs.Add(uvC); float3 faceNormal = CalcFaceNormal((float3)a, (float3)b, (float3)c); float3 normalD; if (normalsOb != null) { normals.Add(AdjustNormal(normalsOb[iNorm++], faceNormal)); normals.Add(AdjustNormal(normalsOb[iNorm++], faceNormal)); normals.Add(AdjustNormal(normalsOb[iNorm++], faceNormal)); normalD = AdjustNormal(normalsOb[iNorm++], faceNormal); } else { normals.Add(faceNormal); normals.Add(faceNormal); normals.Add(faceNormal); normalD = faceNormal; } tris.AddRange(new ushort[] { nNewVerts, (ushort)(nNewVerts + 2), (ushort)(nNewVerts + 1) }); if (c != d) { // The Polyogon is not a triangle, but a quad. Add the second triangle. verts.Add((float3)d); uvs.Add(uvD); normals.Add(normalD); tris.AddRange(new ushort[] { nNewVerts, (ushort)(nNewVerts + 3), (ushort)(nNewVerts + 2) }); nNewVerts += 1; } nNewVerts += 3; } Debug.WriteLine(verts.Count); return(new MeshComponent() { Normals = normals.ToArray(), Vertices = verts.ToArray(), Triangles = tris.ToArray(), UVs = uvs.ToArray(), BoundingBox = new AABBf(verts.Min, verts.Max) }); }