I want to use heightmaps with the stack of
BabylonJS on the client and
cannon-es running on the server and being responsible for the physics.
My ideal workflow would be:
- create heightmaps images (eg. with some editor)
- use it with the BabylonJS on the client (which is simple with
- create heightfield for the
cannon to take care about the physics
I’m having trouble with the latter.
I haven’t found any example of using the image for heightfield creation.
I’m thinking maybe I should take the
Uint8ClampedArray of the heightmap image (which can be done with loading the image to the canvas and getting it from the canvas context) and somehow convert it into the heightfield that
Uint8ClampedArray is one-dimentional array where every 4 entries defines one pixel, like
[0, 0, 0, 255, 0, 0, 0, 255, ... ] defines two pixels.
- The bonus question On the other way around, if I would like to create the heightmap programatically then should I use DynamicTerrain?
I’ll be thankful for any idea and pushing me in the right direction.
I guess you’ve seen this PG already : https://playground.babylonjs.com/#EXL6K#9
Did you try to use a static mesh impostor instead? It may use a bit more memory but performance should be the same…and you could use another Physics Engine.
Youp, I think I’ve seen all the examples existing in the nearest Internet
I don’t think I’m following you with the
static mesh impostor idea. Could you explain in more details?
I use BabylonJS to render heightmap only on the client - not for the physics.
I’ve been going through the BabylonJS implementation and the
CreateGroundFromHeightMap is using
CreateGroundFromHeightMapVertexData so maybe I can use it or at least some parts of its implementation
dumb question maybe but can’t you use babylon on the server too?
That’s not a dumb question at all! I’ve got babylon on the server before
But I would like to avoid this, I take it as the last resort.
I will try my best with the
CreateGroundFromHeightMapVertexData but I’m also curious if @RaananW has something to say in this subject.
If you are asking how to pass the data to cannon, you will need to pass the mesh’s positions (from the vertex data) to the cannon heightmap. We are doing it here - Babylon.js/cannonJSPlugin.ts at master · BabylonJS/Babylon.js (github.com)
It is a grid (plane) with height definition on each defined point.
Sorry for not responding for a while but a lot is going on in my private life.
That’s definitely very interesting but I’m not 100% sure if that’s what I need.
I will try to experiment and let you know