BabylonJS / Extensions

Extensions for Babylon.js
175 stars 156 forks source link

Dynamic terrain.feature.uv color data #157

Closed jbousquie closed 5 years ago

jbousquie commented 5 years ago

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

    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.