use of org.terasology.engine.rendering.gltf.model.GLTFAccessor in project Terasology by MovingBlocks.
the class GLTFMeshFormat method load.
@Override
public MeshData 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);
GLTFMesh gltfMesh = gltf.getMeshes().get(0);
checkPrimitivePresent(urn, gltfMesh);
GLTFPrimitive gltfPrimitive = gltfMesh.getPrimitives().get(0);
List<byte[]> loadedBuffers = loadBinaryBuffers(urn, gltf);
StandardMeshData meshData = new StandardMeshData();
for (MeshAttributeSemantic semantic : MeshAttributeSemantic.values()) {
GLTFAccessor gltfAccessor = getAccessor(semantic, gltfPrimitive, gltf);
if (gltfAccessor != null && gltfAccessor.getBufferView() != null) {
GLTFBufferView bufferView = gltf.getBufferViews().get(gltfAccessor.getBufferView());
switch(semantic) {
case Position:
GLTFAttributeMapping.readVec3FBuffer(loadedBuffers.get(bufferView.getBuffer()), gltfAccessor, bufferView, meshData.position);
break;
case Normal:
GLTFAttributeMapping.readVec3FBuffer(loadedBuffers.get(bufferView.getBuffer()), gltfAccessor, bufferView, meshData.normal);
break;
case Texcoord_0:
GLTFAttributeMapping.readVec2FBuffer(loadedBuffers.get(bufferView.getBuffer()), gltfAccessor, bufferView, meshData.uv0);
break;
case Texcoord_1:
GLTFAttributeMapping.readVec2FBuffer(loadedBuffers.get(bufferView.getBuffer()), gltfAccessor, bufferView, meshData.uv1);
break;
case Color_0:
GLTFAttributeMapping.readColor4FBuffer(loadedBuffers.get(bufferView.getBuffer()), gltfAccessor, bufferView, meshData.color0);
break;
}
}
}
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);
TIntArrayList indices = new TIntArrayList();
readBuffer(loadedBuffers.get(indicesBuffer.getBuffer()), indicesAccessor, indicesBuffer, indices);
for (int x = 0; x < indices.size(); x++) {
meshData.indices.put(indices.get(x));
}
return meshData;
}
}
use of org.terasology.engine.rendering.gltf.model.GLTFAccessor in project Terasology by MovingBlocks.
the class GLTFAnimationFormat method getFloats.
private TFloatList getFloats(GLTF gltf, List<byte[]> loadedBuffers, int accessorIndex) throws IOException {
GLTFAccessor accessor = gltf.getAccessors().get(accessorIndex);
GLTFBufferView bufferView = gltf.getBufferViews().get(accessor.getBufferView());
TFloatList floats = new TFloatArrayList();
readBuffer(loadedBuffers.get(bufferView.getBuffer()), accessor, bufferView, floats);
return floats;
}
use of org.terasology.engine.rendering.gltf.model.GLTFAccessor in project Terasology by MovingBlocks.
the class GLTFCommonFormat method readFloatBuffer.
protected TFloatList readFloatBuffer(MeshAttributeSemantic semantic, GLTFPrimitive gltfPrimitive, GLTF gltf, List<byte[]> loadedBuffers) throws IOException {
GLTFAccessor gltfAccessor = getAccessor(semantic, gltfPrimitive, gltf);
if (gltfAccessor != null && gltfAccessor.getBufferView() != null) {
GLTFBufferView bufferView = gltf.getBufferViews().get(gltfAccessor.getBufferView());
TFloatList floats = new TFloatArrayList();
readBuffer(loadedBuffers.get(bufferView.getBuffer()), gltfAccessor, bufferView, floats);
return floats;
}
return new TFloatArrayList();
}
use of org.terasology.engine.rendering.gltf.model.GLTFAccessor in project Terasology by MovingBlocks.
the class GLTFCommonFormat method loadMat4fList.
protected List<Matrix4f> loadMat4fList(int inverseBindMatrices, GLTF gltf, List<byte[]> loadedBuffers) {
GLTFAccessor accessor = gltf.getAccessors().get(inverseBindMatrices);
GLTFBufferView bufferView = gltf.getBufferViews().get(accessor.getBufferView());
byte[] buffer = loadedBuffers.get(bufferView.getBuffer());
TFloatList values = new TFloatArrayList();
readBuffer(buffer, accessor, bufferView, values);
List<Matrix4f> matricies = Lists.newArrayList();
for (int i = 0; i < values.size(); i += 16) {
Matrix4f mat = new Matrix4f(values.get(i), values.get(i + 1), values.get(i + 2), values.get(i + 3), values.get(i + 4), values.get(i + 5), values.get(i + 6), values.get(i + 7), values.get(i + 8), values.get(i + 9), values.get(i + 10), values.get(i + 11), values.get(i + 12), values.get(i + 13), values.get(i + 14), values.get(i + 15));
matricies.add(mat);
}
return matricies;
}
use of org.terasology.engine.rendering.gltf.model.GLTFAccessor in project Terasology by MovingBlocks.
the class GLTFCommonFormat method readIntBuffer.
protected TIntList readIntBuffer(MeshAttributeSemantic semantic, GLTFPrimitive gltfPrimitive, GLTF gltf, List<byte[]> loadedBuffers) throws IOException {
GLTFAccessor gltfAccessor = getAccessor(semantic, gltfPrimitive, gltf);
if (gltfAccessor != null && gltfAccessor.getBufferView() != null) {
GLTFBufferView bufferView = gltf.getBufferViews().get(gltfAccessor.getBufferView());
TIntList ints = new TIntArrayList();
readBuffer(loadedBuffers.get(bufferView.getBuffer()), gltfAccessor, bufferView, ints);
return ints;
}
throw new IOException("Cannot load gltf without " + semantic);
}
Aggregations