use of org.terasology.engine.rendering.assets.skeletalmesh.BoneWeight in project Terasology by MovingBlocks.
the class GLTFSkeletalMeshFormat method load.
@Override
public SkeletalMeshData load(ResourceUrn urn, List<AssetDataFile> inputs) throws IOException {
try (Reader in = new InputStreamReader(inputs.get(0).openStream())) {
GLTF gltf = gson.fromJson(in, GLTF.class);
checkVersionSupported(urn, gltf);
checkMeshPresent(urn, gltf);
GLTFSkin skin = gltf.getSkins().get(0);
GLTFMesh gltfMesh = gltf.getMeshes().get(0);
checkPrimitivePresent(urn, gltfMesh);
GLTFPrimitive gltfPrimitive = gltfMesh.getPrimitives().get(0);
List<byte[]> loadedBuffers = loadBinaryBuffers(urn, gltf);
SkeletalMeshDataBuilder builder = new SkeletalMeshDataBuilder();
List<Bone> bones = loadBones(gltf, skin, loadedBuffers);
for (Bone bone : bones) {
builder.addBone(bone);
}
List<Vector3f> positions = loadVector3fList(MeshAttributeSemantic.Position, gltfPrimitive, gltf, loadedBuffers);
List<Vector3f> normals = loadVector3fList(MeshAttributeSemantic.Normal, gltfPrimitive, gltf, loadedBuffers);
TIntList joints = readIntBuffer(MeshAttributeSemantic.Joints_0, gltfPrimitive, gltf, loadedBuffers);
TFloatList weights = readFloatBuffer(MeshAttributeSemantic.Weights_0, gltfPrimitive, gltf, loadedBuffers);
List<BoneWeight> boneWeights = new ArrayList<>();
for (int index = 0; index < positions.size(); index++) {
TIntList weightJoints = new TIntArrayList();
TFloatList weightBiases = new TFloatArrayList();
for (int i = 0; i < 4; i++) {
if (weights.get(4 * index + i) > 0) {
weightBiases.add(weights.get(4 * index + i));
weightJoints.add(joints.get(4 * index + i));
}
}
boneWeights.add(new BoneWeight(weightBiases, weightJoints));
}
builder.addVertices(positions);
builder.addNormals(normals);
builder.addWeights(boneWeights);
builder.setUvs(loadVector2fList(MeshAttributeSemantic.Texcoord_0, gltfPrimitive, gltf, loadedBuffers));
GLTFAccessor indicesAccessor = getIndicesAccessor(gltfPrimitive, gltf, urn);
if (indicesAccessor.getBufferView() == null) {
throw new IOException("Missing buffer view for indices accessor in " + urn);
}
GLTFBufferView indicesBuffer = gltf.getBufferViews().get(indicesAccessor.getBufferView());
checkIndicesBuffer(indicesBuffer);
TIntList indicies = new TIntArrayList();
readBuffer(loadedBuffers.get(indicesBuffer.getBuffer()), indicesAccessor, indicesBuffer, indicies);
builder.setIndices(indicies);
if (gltf.getSkins().isEmpty()) {
throw new IOException("Skeletal mesh '" + urn + "' missing skin");
}
return builder.build();
}
}
Aggregations