Jump to content

Dynamic Terrain


Recommended Posts

  • 2 weeks later...

Well, here's the first working prototype of this new feature : http://jerome.bousquie.fr/BJS/test/terrainSP.html

Demo description :

- the wireframe is the visualization of the map, the logical pool of data describing the relief. Normally, it's not rendered and can be far bigger.

- the textured square is the terrain. It's dynamically morphed to the map data when moving according to the camera position. I didn't activate any LOD or camera elevation correction. Just move the cam and zoom in/out.

- the red sphere is the World origin and the red box shows the north, those are just tags in the space.

=> Another logical pool is added beside the map : a Solid Particle Map (SPMap). It's simply a bunch of data describing where some solid particles are in the World. The solid particles in this map have a type (a shape model), a set of coordinates, a set of rotation values and a set of scaling values. So 9 successive float values per particle of a given type.

So the SPMap is simply an array of arrays.

SPMap[particleType1] = [posx1, posy1, posz1, rotx1, rotx1, rotz1, sclx1, scly1, sclz1, posx2, posy2, posz2, etc ...]   <= list of successive values of particle of type1

SPMap[particleType2] = [posx1, posy1, posz1, rotx1, rotx1, rotz1, sclx1, scly1, sclz1, posx2, posy2, posz2, etc ...]  <= list of successive values of particle of type2

SPMap[ etc ]

In this example type1 is a box, type2 a sphere and type3 a tetrahadron. I randomy populate the SPMap with particle values. There could be dozen thousands or millions objects in the map. There are just set at a fixed location, rotation and scaling.

The SPMap feature of the DynamicTerrain allows to render dynamically only the SPMap objects visible on the terrain with a SPS. This SPS can then be quite little (according to the object density in the map actually).

In this example, I just declared a SPS with 100 boxes, 100 spheres and 100 tetrahedrons what is quite few (less than 300 visible objects is easy to manage for the SPS engine; maybe I could have done it even tinier, didn't test) and I passed this SPS with the SPMap to the DynamicTerrain. And that's all.

The DynamicTerrain will update the SPS for you by recycling automatically the particles per type in order to use and show only the needed ones in the terrain according to the current position and obviously to the SPMap data.

Still a little buggy on the edges : from the terrain perspective the map is infinitely repeated and tiled, but not the SPMap for now. [EDITED] fixed !

Doc coming soon with better examples...


What is this for ?

This allows for instance to declare where some objects are located on a map (houses, trees, bridge, buildings... or clouds) and then to render these objects on the terrain with the best possible performance : a small SPS, recycling quickly a light pool of particles.



Link to comment
Share on other sites

Please have a look at this better demo : http://jerome.bousquie.fr/BJS/test/terrainSP2.html

- map : 1000 x 1000 points map

- 66511 random particles fixed in the SPMap (well more than 66K)

- terrain : 100 x 100

- sps : 2000 boxes, 2000 spheres, 2000 tetra (it can works with 500 of each if we don't get in altitude to see in the distance), so 6K real particles maximum

So we can manage 66K objects from the map with only 6K (or less, if no LOD) real objects automatically recycled.


not sure the LOD is really accurate with the particles ... (edit : I'm sure it's wrong actually)

Link to comment
Share on other sites

  • 2 weeks later...

New feature for the DynamicTerrain : per object color and texture.
The previous feature added the ability to set objects in an object map, as many as wanted, by setting their positions, rotations and scalings in the map.
A SPS was used to render these objects in the terrain with a reduced pool of recycled solid particles.

This new feature allows now to pass also to the terrain data about object colors and textures (uvs actually) in the same way than object settings.
Example of randomly population of objects in the map with random colors and uvs :

            if (Math.random() > 0.8) {
               let xp = x;
               let yp = y;
               let zp = z;
               let ry = Math.random() * 3.6;
               let sx = 0.5 + Math.random();
               let sy = 0.5 + Math.random();
               let sz = 0.5 + Math.random();

               let r = Math.abs(xp) / mapSubX + 0.5;
               let g = Math.abs(zp) / mapSubZ + 0.5;
               let b = Math.abs(yp) / elevationScale + 0.1;

               let u = 0.9 * Math.random();
               let v = 0.9 * Math.random();
               let type = index % 3;
               SPmapData[type].push(xp, yp, zp, 0, ry, 0, sx, sy, sz);
               SPcolorData[type].push(r, g, b, 1.0); 
               SPuvData[type].push(u, v, u + 0.1, v + 0.1);

then the terrain creation  with the new parameters SPcolorData and SPuvData :

    var terrainSub = 100;        // terrain subdivisions
    var terrainOptions = {
        terrainSub: terrainSub, 
        mapData: mapData, mapSubX: mapSubX, mapSubZ: mapSubZ, 
        mapColors: mapColors, 
        SPmapData: SPmapData,
        sps: sps,
        SPcolorData: SPcolorData,
        SPuvData: SPuvData
    var terrain = new BABYLON.DynamicTerrain("dt", terrainOptions, scene);
    terrain.mesh.material = terrainMaterial;

That's all ... now each recycled solid particle will be ever given the right settings, colors and uvs when rendering the related object in the terrain.

Documentation to come.


Live example : http://jerome.bousquie.fr/BJS/test/terrainSP.html

Link to comment
Share on other sites

  • 2 years later...

Could this be used for an RPG from a third person view? I seem to be struggling with the basic physics collision options of flat terrains. 


More specifically - I loaded a map using heighmaps, but characters meshes seem to go right through the terrain like it doesnt exist. 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...