BoundingBox of a moving Mesh does not update properly

Hey, i have a question. Why do I need to call app.removeChild(renderer) and app.addChild(renderer) in Update everytime i change the values of my vertices to have proper BoundingBoxes. :slight_smile:.
I thought mesh.update() should do the work.

BoundingBoxTest.prototype.update = function (dt) {

this.time += dt;

this.vertexPositions[0] = 2 * Math.cos(this.time * 0.5);
this.vertexPositions[2] = 2 * Math.sin(this.time * 0.5);

this.vertexPositions[3] = 1 + 2 * Math.cos(this.time * 0.5);
this.vertexPositions[5] = 2 * Math.sin(this.time * 0.5);

this.vertexPositions[6] = 2 * Math.cos(this.time * 0.5);
this.vertexPositions[8] = 1 + 2 * Math.sin(this.time * 0.5);


// this.app.root.removeChild(this.arcEntity);

this.mesh.setPositions(this.vertexPositions);
this.mesh.update();

// this.app.root.addChild(this.arcEntity);

};

With removeChild and addChild it works fine.

Hi @Andrushka130,

Hmm, it maybe that the MeshInstance using that Mesh may be caching the aabb for performance reasons.

A bounding box is in world units, and even though the mesh has its own, each mesh instance based on its node/entity world position has a different bounding box.

No need to remove/add the child, you can just update the mesh instance aabb. @mvaligursky is there an official way of forcing a mesh instance to do that?

mesh.update() is the right way to do it and it works correctly. Try adding

console.log(mesh.aabb.center)

in the live editor of this example (e.g. add it somewhere in updateMesh function)
https://playcanvas.github.io/#/graphics/mesh-generation

You will see that the aabb center is changed every frame.

1 Like

agreed with Lexxik, there is no way to force is as it gets updated every frame automatically.

1 Like

Agreed with Lexxik, the boundingBox of the mesh is getting updated properly, but the boundingBox of the MeshInstance does not update and i think the frustum Culling of an camera is using the meshInstance BoundingBox.

I try to explain my project. I am trying to create an VR Framework for my company and i am casting an Arc out of an Controller. The arc is only displayed when i look at the ground (0,0,0) because the boundingBox of my MeshInstance of the Arc is not getting updated.

If i disable frustum Culling and render everything it works properly. But i want to enable frustum Culling of course, so if i enable frustumCulling and use removeChild and addChild the meshInstance boundingBox is getting updated and my ray is displayed properly and i have no problems.

I hope my thesis make sense :smiley: and thank you for your fast replies.

Yes, that makes sense. I would probably do it using a custom AABB. When you set a custom AABB on a render component, it will be assigned to the mesh instance. The default AABB would not be used. Then you could do something like:

const aabb = new pc.BoundingBox();

entity.render.customAabb = aabb;

update(dt) {
    aabb.compute(this.vertexPositions, this.vertexPositions.length / 3);
}
1 Like

put a breakpoint in MeshInstance.get aabb to see what happens there for your mesh. Hack something on your mesh, say mesh.hack = true; and set up conditional breakpoint for this, you should see if the world space aabb gets updated there correctly.

@mvaligursky do you mean the world aabb on mesh instance? I don’t think it would update, because those will both be equal after the first frame:

image

I found another solution:

this.meshInstance.aabb = this.mesh.aabb.clone();

this works as well. I set my meshInstance.aabb every frame

No it does not update correctly.

Perhaps you could create an issue here with a simple repro: Issues · playcanvas/engine · GitHub
and I’ll investigate / fix if needed.

1 Like

Created a ticket for follow up:

Here is another sample project, which illustrates this issue:
https://playcanvas.com/project/1147948/overview/mesh-instance-aabb-update

Yellow → Mesh aabb
Magenta → MeshInstance aabb

I also add this sample to the github issue

1 Like

@LucaHeft
thanks, a workaround for now is to use

this.arcEntity.setLocalPosition(this.arcEntity.getLocalPosition());

which makes the version change, forcing the update

@Andrushka130 - try the same workaround instead of adding / removing the entity from parent

And the engine fix: [Fix] World space aabb gets updated when local space aabb of mesh changes by mvaligursky · Pull Request #5726 · playcanvas/engine · GitHub
(will get released at the next engine patch release)

2 Likes

yes , thank you :slight_smile: