|
9 | 9 | using SuperBMD.Geometry.Enums; |
10 | 10 | using SuperBMD.Util; |
11 | 11 | using SuperBMD.Rigging; |
| 12 | +using OpenTK; |
12 | 13 |
|
13 | 14 | namespace SuperBMD.BMD |
14 | 15 | { |
@@ -164,6 +165,68 @@ public static SHP1 Create(Scene scene, Dictionary<string, int> boneNames, Vertex |
164 | 165 | return shp1; |
165 | 166 | } |
166 | 167 |
|
| 168 | + public void FillScene(Scene scene, VertexData vertData, List<Rigging.Bone> flatSkeleton, List<Matrix4> inverseBindMatrices) |
| 169 | + { |
| 170 | + for (int i = 0; i < Shapes.Count; i++) |
| 171 | + { |
| 172 | + Mesh mesh = new Mesh($"mesh_{ i }", PrimitiveType.Triangle); |
| 173 | + mesh.MaterialIndex = i; |
| 174 | + |
| 175 | + int vertexID = 0; |
| 176 | + Shape curShape = Shapes[i]; |
| 177 | + foreach (Packet pack in curShape.Packets) |
| 178 | + { |
| 179 | + foreach (Primitive prim in pack.Primitives) |
| 180 | + { |
| 181 | + List<Vertex> triVertices = J3DUtility.PrimitiveToTriangles(prim); |
| 182 | + |
| 183 | + for (int triIndex = 0; triIndex < triVertices.Count / 3; triIndex += 3) |
| 184 | + { |
| 185 | + Face newFace = new Face(new int[] { vertexID, vertexID + 1, vertexID + 2 }); |
| 186 | + mesh.Faces.Add(newFace); |
| 187 | + |
| 188 | + for (int triVertIndex = 0; triVertIndex < 3; triVertIndex++) |
| 189 | + { |
| 190 | + Vertex vert = triVertices[triIndex + triVertIndex]; |
| 191 | + |
| 192 | + for (int j = 0; j < vert.VertexWeight.WeightCount; j++) |
| 193 | + { |
| 194 | + Rigging.Bone curWeightBone = flatSkeleton[vert.VertexWeight.BoneIndices[j]]; |
| 195 | + |
| 196 | + int assBoneIndex = mesh.Bones.FindIndex(x => x.Name == curWeightBone.Name); |
| 197 | + |
| 198 | + if (assBoneIndex == -1) |
| 199 | + { |
| 200 | + Assimp.Bone newBone = new Assimp.Bone(); |
| 201 | + newBone.Name = curWeightBone.Name; |
| 202 | + mesh.Bones.Add(newBone); |
| 203 | + assBoneIndex = mesh.Bones.IndexOf(newBone); |
| 204 | + } |
| 205 | + |
| 206 | + mesh.Bones[assBoneIndex].VertexWeights.Add(new VertexWeight(vertexID, vert.VertexWeight.Weights[j])); |
| 207 | + } |
| 208 | + |
| 209 | + OpenTK.Vector4 openTKVec = new Vector4(vertData.Positions[(int)vert.GetAttributeIndex(GXVertexAttribute.Position)], 1); |
| 210 | + Vector3D vertVec = new Vector3D(openTKVec.X, openTKVec.Y, openTKVec.Z); |
| 211 | + |
| 212 | + if (vert.VertexWeight.WeightCount == 1) |
| 213 | + { |
| 214 | + Vector4 trans = OpenTK.Vector4.Transform(openTKVec, inverseBindMatrices[vert.VertexWeight.BoneIndices[0]].Inverted()); |
| 215 | + vertVec = new Vector3D(trans.X, trans.Y, trans.Z); |
| 216 | + } |
| 217 | + |
| 218 | + mesh.Vertices.Add(vertVec); |
| 219 | + |
| 220 | + vertexID++; |
| 221 | + } |
| 222 | + } |
| 223 | + } |
| 224 | + } |
| 225 | + |
| 226 | + scene.Meshes.Add(mesh); |
| 227 | + } |
| 228 | + } |
| 229 | + |
167 | 230 | public void Write(EndianBinaryWriter writer) |
168 | 231 | { |
169 | 232 | List<Tuple<ShapeVertexDescriptor, int>> descriptorOffsets; // Contains the offsets for each unique vertex descriptor |
|
0 commit comments