I noticed a crash while using collision checks for some objects.
Seems that in the _testTriangle function some undefined points where passed which is causing a crash:
We would need to see what this implies but I guess is a fair point (somehow). And then, may be not. Collisions can be costly and I suppose a good practice would be to setâem by mesh or using a collision map. Nonetheless, we could eventually make it a Feature requests.
Letâs first see what the team has to say about it? cc @sebavan
Hmmm we always have to be cautious when adding checks to checkCollisions because it needs to be as light as possible to have good performance. One option would be to change the check from
for (let i = 0; i < pts.length; i += 3) {
to
for (let i = 0; i+2 < pts.length; i += 3) {
but thatâs still an extra op per loop. Another would be to check the meshâs geometry at the moment checkCollisions is set to true and try to correct it if there are any problems, or just not set the collisions in this case. What do you think @Evgeni_Popov ?
I donât think changing the loop is the right solution, as we shouldnât end up in this block of code in the first place. The code tests !indices || indices.length === 0 to see if the mesh is non-indexed, but this is the wrong check: we can have indices.length === 0 for an indexed mesh (as shown in PG). Weâd need to take Mesh.isUnIndexed into account, but itâs not available in this piece of code.
I would update the AbstractMesh._collideForSubMesh function like this:
public _collideForSubMesh(subMesh: SubMesh, transformMatrix: Matrix, collider: Collider): AbstractMesh {
this._generatePointsArray();
if (!this._positions) {
return this;
}
const indices = this.getIndices();
if (!subMesh.getMesh().isUnIndexed && (!indices || indices.length === 0)) {
return this;
}
...
(I added the last 4 lines)
But itâs not working, because isUnIndexed is a property of Mesh, not AbstractMesh⌠We could add isUnIndexed in AbstractMesh, but I donât know if itâs the right thing to do.
Letâs see what @sebavan thinks when he comes back from vacation.
What decides if isUnIndexed is true?
Maybe those parts exist on AbstractMesh?
Edit;
console.log on original PG shows isUnIndexed is false for all meshes.
To be honest this kinda just looks like a faulty mesh⌠the array of points should always be divisible by 3 in a triangular mesh⌠needs to be cleaned up in your DCC / CAD program of choice.
Meshes are always indexed by default. They become unindexed when you call Mesh.convertToUnIndexedMesh.
No, not when in indexed mode. Think of a cube, which has 8 vertices for example. Itâs only in the unindexed mode that the number of vertices must be divisible by 3.
A mesh with vertices but without any face is allowed, so I think we should handle it gracefully. The problem here is that the collision code always assumes that in this case the mesh is unindexed, which is wrong.
I used the term âarray of pointsâ as an overall description, my bad for the misunderstanding.
Whether the mesh have indices or not, the result should divide by 3.
8 indices would be 24 âpointsâ
Yes, itâs the size of the index buffer which must be dividable by 3 for an indexed mesh, not the number of vertices. In the present case, the number of vertices is 100 and the size of the index buffer is 0, so the mesh is valid .