How to create a physics aggregate from GPU vertex data?

Hey there!

So I have this PG where I create a bunch of vertex data with a compute shader:

Now I want some physics, so I need the vertex data on the CPU.

My first solution is to read from the GPU buffers to create a vertex data object and then apply it to my mesh. Then I can create a physics aggregate using what I found in the doc. (In the PG that’s the branch with keepDataOnGPU=false)

However, if I understand correctly, vertexData.applyToMesh will send the data back to the GPU, which I don’t want because it is slow, and don’t need because the data is already there.

So my question is: Is there a way to download the data from the GPU, use it to create a physics aggregate and not send it back?

CC @Cedric

1 Like

First, I’d separate PhysicsAggregate into PhysicsBody and PhysicsShape (see this post). But that’s where you’d be stuck. The interface for creating a shape of type MESH only exposes methods that take meshes as input.

It’s only in the plugin that mesh vertices are captured by the MeshAccumulate() class to parse through the mesh vertices (and indices) with _addMesh(). This adds to the this._vertices array. You could extend that class with something like _addVertices(vertices,indices), but you’d also have to tie it into PhysicsShape.

A pretty daunting task, though it might be a worthwhile PR.

1 Like

Another possibility, maybe, is to mesh.setEnabled(false);

Will that turn off sending the mesh’s vertex data to the GPU and still allow mesh.getVerticesData()?

Very interesting! It seems the _addMesh could be used with VertexData instead of Mesh so maybe there could be a way to create a shape from the raw data instead of using the mesh :thinking:

From what I see here, the enabled status of the mesh does not affect whereas data is sent to the GPU or not :confused:

I will wait for more answers, but yeah I will probably need to extend the plugin to allow this ^^

I’d extend the options for Mesh and convex hul and add a vertex array (and index array) here : Babylon.js/packages/dev/core/src/Physics/v2/Plugins/havokPlugin.ts at 2780b5955d4afc003b1ccd6350d669044de4ced6 · BabylonJS/Babylon.js · GitHub

then, add a static method in this class : Babylon.js/packages/dev/core/src/Physics/v2/physicsShape.ts at 2780b5955d4afc003b1ccd6350d669044de4ced6 · BabylonJS/Babylon.js · GitHub

to create a physics shape from those buffers.

Thanks for the pointers, I will try to make a PR for this in the next days ^^

2 Likes