@Override
public SkeletalMeshData load(Module module, InputStream stream, List<URL> urls, List<URL> deltas) throws IOException {
try {
MD5 md5 = parse(stream);
SkeletalMeshDataBuilder skeletonBuilder = new SkeletalMeshDataBuilder();
List<Bone> bones = Lists.newArrayListWithCapacity(md5.numJoints);
for (int i = 0; i < md5.numJoints; ++i) {
MD5Joint joint = md5.joints[i];
Bone bone = new Bone(i, joint.name, joint.position, joint.orientation);
bones.add(bone);
if (joint.parent != -1) {
bones.get(joint.parent).addChild(bone);
}
skeletonBuilder.addBone(bone);
}
if (md5.meshes.length > 0) {
// TODO: Support multiple mesh somehow?
MD5Mesh mesh = md5.meshes[0];
for (MD5Weight weight : mesh.weightList) {
skeletonBuilder.addWeight(new BoneWeight(weight.position, weight.bias, weight.jointIndex));
}
List<Vector2f> uvs = Lists.newArrayList();
TIntList vertexStartWeight = new TIntArrayList(mesh.numVertices);
TIntList vertexWeightCount = new TIntArrayList(mesh.numVertices);
for (MD5Vertex vert : mesh.vertexList) {
uvs.add(vert.uv);
vertexStartWeight.add(vert.startWeight);
vertexWeightCount.add(vert.countWeight);
}
skeletonBuilder.setVertexWeights(vertexStartWeight, vertexWeightCount);
skeletonBuilder.setUvs(uvs);
TIntList indices = new TIntArrayList(mesh.indexList.length);
for (int i = 0; i < mesh.numTriangles; ++i) {
indices.add(mesh.indexList[i * 3]);
indices.add(mesh.indexList[i * 3 + 2]);
indices.add(mesh.indexList[i * 3 + 1]);
}
skeletonBuilder.setIndices(indices);
}
return skeletonBuilder.build();
} catch (NumberFormatException e) {
throw new IOException("Error parsing " + module.toString(), e);
}
}