use of com.badlogic.gdx.graphics.g3d.model.NodePart in project bdx by GoranM.
the class GameObject method join.
public void join(HashMap<Mesh, ArrayList<Matrix4f>> map) {
// Collect transformed vertex arrays for each material & calculate number of indices
int VERT_STRIDE = Bdx.VERT_STRIDE;
HashMap<Material, ArrayList<float[]>> tvaMap = new HashMap<Material, ArrayList<float[]>>();
HashMap<Material, Integer> lenMap = new HashMap<Material, Integer>();
Mesh m;
Node node;
Material mat;
MeshPart meshPart;
float[] va, tva;
int numIndices, numVertices, offset, j, len;
Vector3f p = new Vector3f();
Vector3f s = new Vector3f();
Matrix3f o = new Matrix3f();
Vector3f vP = new Vector3f();
Vector3f nP = new Vector3f();
Vector3f vPT = new Vector3f();
Vector3f nPT = new Vector3f();
Vector3f pos = position();
Vector3f sca = scale();
Matrix3f oriInv = orientation().inverted();
for (Map.Entry<Mesh, ArrayList<Matrix4f>> e : map.entrySet()) {
m = e.getKey();
node = m.model.nodes.get(0);
for (Matrix4f t : e.getValue()) {
t.get(p);
p.sub(pos);
p = oriInv.mult(p.div(sca));
t.getRotationScale(o);
o = oriInv.mult(o);
s.set(t.m30, t.m31, t.m32);
if (s.length() == 0) {
s.set(1, 1, 1);
}
s = s.div(sca);
for (NodePart nodePart : node.parts) {
meshPart = nodePart.meshPart;
numIndices = meshPart.size;
numVertices = numIndices * VERT_STRIDE;
offset = meshPart.offset * VERT_STRIDE;
va = meshPart.mesh.getVertices(offset, numVertices, new float[numVertices]);
tva = new float[numVertices];
j = 0;
for (int i = 0; i < numIndices; i++) {
vP.set(va[j], va[j + 1], va[j + 2]);
nP.set(va[j + 3], va[j + 4], va[j + 5]);
vPT.set(o.mult(vP.mul(s)));
vPT.add(p);
nPT.set(o.mult(vP.plus(nP)));
nPT.sub(o.mult(vP));
tva[j] = vPT.x;
tva[j + 1] = vPT.y;
tva[j + 2] = vPT.z;
tva[j + 3] = nPT.x;
tva[j + 4] = nPT.y;
tva[j + 5] = nPT.z;
tva[j + 6] = va[j + 6];
tva[j + 7] = va[j + 7];
j += VERT_STRIDE;
}
mat = m.materials.get(nodePart.material.id);
ArrayList<float[]> l;
if (tvaMap.containsKey(mat)) {
l = tvaMap.get(mat);
len = lenMap.get(mat);
} else {
l = new ArrayList<float[]>();
tvaMap.put(mat, l);
len = 0;
}
l.add(tva);
lenMap.put(mat, len + tva.length);
}
}
}
// Build a unique model out of meshparts for each material
ModelBuilder builder = new ModelBuilder();
builder.begin();
short idx = 0;
MeshPartBuilder mpb;
for (Map.Entry<Material, ArrayList<float[]>> e : tvaMap.entrySet()) {
mat = e.getKey();
len = lenMap.get(mat);
tva = new float[len];
j = 0;
for (float[] verts : e.getValue()) {
numVertices = verts.length;
for (int i = 0; i < numVertices; i++) {
tva[i + j] = verts[i];
}
j += numVertices;
}
mpb = builder.part(mat.name(), GL20.GL_TRIANGLES, Usage.Position | Usage.Normal | Usage.TextureCoordinates, mat);
mpb.vertex(tva);
try {
for (short i = 0; i < len / VERT_STRIDE; i++) {
mpb.index(idx);
idx += 1;
}
} catch (Error error) {
throw new RuntimeException("MODEL ERROR: Models with more than 32767 vertices are not supported. Decrease the number of objects to join.");
}
}
Model finishedModel = builder.end();
// Update mesh
mesh(new Mesh(finishedModel, scene));
// Update dimensionsNoScale and origin
com.badlogic.gdx.graphics.Mesh mesh = finishedModel.meshes.first();
BoundingBox bbox = mesh.calculateBoundingBox();
Vector3 dimensions = bbox.getDimensions(new Vector3());
Vector3 center = bbox.getCenter(new Vector3());
dimensionsNoScale = new Vector3f(dimensions.x, dimensions.y, dimensions.z);
origin = new Vector3f(center.x, center.y, center.z);
// Update body
updateBody();
if (json.get("mesh_name").asString() == null) {
visible = json.get("visible").asBoolean();
}
}
Aggregations