use of gnu.trove.list.TFloatList in project Terasology by MovingBlocks.
the class ColladaMeshFormat method load.
@Override
public MeshData load(ResourceUrn urn, List<AssetDataFile> inputs) throws IOException {
logger.info("Loading mesh for " + urn);
ColladaLoader loader = new ColladaLoader();
try (BufferedInputStream stream = inputs.get(0).openStream()) {
loader.parseMeshData(stream);
} catch (ColladaParseException e) {
throw new IOException("Error loading collada mesh for " + urn, e);
}
MeshData data = new MeshData();
TFloatList colorsMesh = data.getColors();
TFloatList verticesMesh = data.getVertices();
TFloatList texCoord0Mesh = data.getTexCoord0();
TFloatList normalsMesh = data.getNormals();
TIntList indicesMesh = data.getIndices();
// Scale vertices coordinates by unitsPerMeter
for (int i = 0; i < loader.getVertices().size(); i++) {
float originalVertexValue = loader.getVertices().get(i);
float adjustedVertexValue = (float) (originalVertexValue * loader.getUnitsPerMeter());
verticesMesh.add(adjustedVertexValue);
}
colorsMesh.addAll(loader.getColors());
texCoord0Mesh.addAll(loader.getTexCoord0());
normalsMesh.addAll(loader.getNormals());
indicesMesh.addAll(loader.getIndices());
if (data.getVertices() == null) {
throw new IOException("No vertices define");
}
if (((null == data.getColors()) || (0 == data.getColors().size())) && ((null == data.getTexCoord0()) || (0 == data.getTexCoord0().size()))) {
throw new IOException("There must be either texture coordinates or vertex colors provided.");
}
if ((null != data.getTexCoord0()) && (0 != data.getTexCoord0().size())) {
if (data.getTexCoord0().size() / 2 != data.getVertices().size() / 3) {
throw new IOException("The number of tex coords (" + data.getTexCoord0().size() / 2 + ") does not match the number of vertices (" + data.getVertices().size() / 3 + ").");
}
}
if ((null != data.getColors()) && (0 != data.getColors().size())) {
if (data.getColors().size() / 4 != data.getVertices().size() / 3) {
throw new IOException("The number of vertex colors (" + data.getColors().size() / 4 + ") does not match the number of vertices (" + data.getVertices().size() / 3 + ").");
}
}
return data;
}
use of gnu.trove.list.TFloatList in project Terasology by MovingBlocks.
the class ObjMeshFormat method processData.
private MeshData processData(List<Vector3f> rawVertices, List<Vector3f> rawNormals, List<Vector2f> rawTexCoords, List<Vector3i[]> rawIndices) throws IOException {
MeshData result = new MeshData();
TFloatList vertices = result.getVertices();
TFloatList texCoord0 = result.getTexCoord0();
TFloatList normals = result.getNormals();
TIntList indices = result.getIndices();
int vertCount = 0;
for (Vector3i[] face : rawIndices) {
for (Vector3i indexSet : face) {
if (indexSet.x > rawVertices.size()) {
throw new IOException("Vertex index out of range: " + indexSet.x);
}
Vector3f vertex = rawVertices.get(indexSet.x - 1);
vertices.add(vertex.x);
vertices.add(vertex.y);
vertices.add(vertex.z);
if (indexSet.y != -1) {
if (indexSet.y > rawTexCoords.size()) {
throw new IOException("TexCoord index out of range: " + indexSet.y);
}
Vector2f texCoord = rawTexCoords.get(indexSet.y - 1);
texCoord0.add(texCoord.x);
texCoord0.add(1 - texCoord.y);
}
if (indexSet.z != -1) {
if (indexSet.z > rawNormals.size()) {
throw new IOException("Normal index out of range: " + indexSet.z);
}
Vector3f normal = rawNormals.get(indexSet.z - 1);
normals.add(normal.x);
normals.add(normal.y);
normals.add(normal.z);
}
}
for (int i = 0; i < face.length - 2; ++i) {
indices.add(vertCount);
indices.add(vertCount + i + 1);
indices.add(vertCount + i + 2);
}
vertCount += face.length;
}
return result;
}
use of gnu.trove.list.TFloatList in project Terasology by MovingBlocks.
the class SkeletalMeshDataBuilder method addMesh.
public SkeletalMeshDataBuilder addMesh(Bone bone, MeshData data) {
TFloatList meshVertices = data.getVertices();
TIntList meshIndices = data.getIndices();
TFloatList texCoord0 = data.getTexCoord0();
int weightsStart = weights.size();
addBone(bone);
for (int i = 0; i < meshVertices.size() / 3; i++) {
float x = meshVertices.get(i * 3);
float y = meshVertices.get(i * 3 + 1);
float z = meshVertices.get(i * 3 + 2);
Vector3f pos = new Vector3f(x, y, z);
BoneWeight weight = new BoneWeight(pos, 1, bone.getIndex());
// TODO Meshes may contain normal vectors and we may copy them to the weight here
// - but they are recalculated later on in either case. needs some rework
addWeight(weight);
vertexStartWeights.add(weightsStart + i);
vertexWeightCounts.add(1);
uvs.add(new Vector2f(texCoord0.get(i * 2), texCoord0.get(i * 2 + 1)));
}
for (int i = 0; i < meshIndices.size(); i++) {
indices.add(meshIndices.get(i) + weightsStart);
}
return this;
}
use of gnu.trove.list.TFloatList in project Terasology by MovingBlocks.
the class ModifiableValueTypeHandler method deserialize.
@Override
public Optional<ModifiableValue> deserialize(PersistedData data) {
if (data.isArray()) {
PersistedDataArray vals = data.getAsArray();
if (vals.isNumberArray()) {
TFloatList floatList = vals.getAsFloatArray();
ModifiableValue modifiableValue = new ModifiableValue(floatList.get(0));
if (floatList.size() == 4) {
modifiableValue.setPreModifier(floatList.get(1));
modifiableValue.setMultiplier(floatList.get(2));
modifiableValue.setPostModifier(floatList.get(3));
}
return Optional.of(modifiableValue);
}
}
return Optional.empty();
}
use of gnu.trove.list.TFloatList 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;
}
Aggregations