I’m trying to create a dynamic terrain from an array that I’ll load in. All of the examples are meshes that are created from math, not loaded. I can’t figure out a format that will work?
My marix is 20 X 20 X 20, the process that I’m using to create the matrix will be like this
Even if I do, What do I set the following values:
const mapSubX = 500;
const mapSubZ = 300;
const terrainSub = 100;
As the demos are all created from math, the formula uses those to generate the array. What would I set those to if I had pre-made the terrain?
I originally tried the hightmap.jpg, however I didn’t want to have the extra baggage of storing an image file in static for each new map created. The array isn’t going to be very large, so I’d just like to load it into as part of the scene data.
EDIT: After I solved the problem, I figured out the format. It’s in the docs, but difficult to describe.
[x,y,z,x,y,z,x,y,z,x,y,z,x,y,z,x,y,z,x,y,z,x,y,z]
It’s a flat array of xyz values, so you divide the matrix by three, then you devide by mapSubX
.
terrainSub
is the same as the number of rows (assuming a square) that you will slice the triplets in the list. So array/terrainSub/3.
Final pattern that does this:
ground_dimensions = 5000
ground_subdivisions = 19 // number of rows in my 2x2 square matrix
const ground = BABYLON.MeshBuilder.CreateGround("ground", {height: ground_dimensions, width: ground_dimensions, subdivisions: ground_subdivisions, updatable: true})
var ground_arr = ground.getVerticesData(BABYLON.VertexBuffer.PositionKind);
var altiudes = JSON.parse(data['biome'][0]['grid']) //my input data form ajax
for (let i=0;i<altiudes.length;i++){ground_arr[(i*3)+1] = altiudes[i]*10} // 10 is an arbitrary bump in altitude, depends on size of your map
console.log("altiudes: ",altiudes)
console.log('positions:', ground_arr)
ground.updateVerticesData(BABYLON.VertexBuffer.PositionKind, ground_arr);