Splitting dynamic terrain to chunks

I’m working with dynamic terrain and trying to split it to multiple chunks. I want to load zone mapData depending on player’s position. For example when player is at position (0, 0) load zone 1, at position (100, 0) load zone 2 and etc.
I have 400 x 400 terrain and each zone is 100x100 size. So terrain is divided to 16 zones, but it’s working not like I expected. Something is wrongabout my arithmetics and I cant figure out what. I guess I do not understand how dynamic terrain works. What I’m trying to achieve is to make terrain scroll seamlessly. And here is what I get:

Here’s my function:

let lastPosition: BABYLON.Vector3;
let lastTile: string;
let terrainSub: number = 100;
let mapSubX: number = 100;
let mapSubZ: number = 100;
let terrainSize: number = 400;

export function updateDynamicTerrain() {
if (lastPosition && lastPosition.equals(_camera.position)) {

lastPosition = _camera.position.clone();

let currentTileX = Math.floor(lastPosition.x / 100);
let currentTileZ = Math.floor(lastPosition.z / -100);
let currentTile = [currentTileX, currentTileZ].join(’.’);

if (currentTile === lastTile) {

lastTile = currentTile;

var newMapData = new Float32Array(mapSubX * mapSubZ * 3);
let startX = currentTileX * 100;
let startZ = currentTileZ * 100;
for (var z = 0; z < mapSubZ; z++) {
for (var x = 0; x < mapSubX; x++) {
var xRatio = (startX + x) / terrainSize;
var zRatio = (startZ + z) / terrainSize;
var y = _map.getHeight(xRatio % 1, zRatio % 1) * 10;

newMapData[3 * (z * mapSubX + x)] = x;
newMapData[3 * (z * mapSubX + x) + 1] = y;
newMapData[3 * (z * mapSubX + x) + 2] = z;

if (!terrain) {
var params = {
mapData: newMapData,
mapSubX: mapSubX,
mapSubZ: mapSubZ,
terrainSub: terrainSub
terrain = new DynamicTerrain(‘t’, params, _scene);

terrain.mapData = newMapData;
terrain.precomputeNormalsFromMap = true;

Also I tried to replicate it in playground, but it’s harder to understand what’s happening there. Babylon.js Playground

Hello and welcome to the forum !!!

Ping both @MackeyK24 and @jerome who both did amazing infinite terrain demos.

Not sure to understand your code.
Actually, the dynamic is a mesh that “flies” over a data map. This mesh doesn’t need to be big : it’s linked to the camera and updates according to the local underlying map data.

So, with this design, you don’t really need to update the terrain itself by downloaded chunks : it will auto-fit every new data on its position.
On the other hand, you can still update the data map instead (by chunks or any other way) and change the terrain data map on the fly :

1 Like

Also in docs How to Use Dynamic Terrain - Babylon.js Documentation

it seems that the BJS about the DynamicTerrain is not up to date because the section about the usage of a SPS (More Advanced Terrain) is missing

Different page https://doc.babylonjs.com/extensions/DT_Objects

rhooooo … ok , thank you :blush:

You see, my map is going to be very large. Possibly billions and billions of vertices. Height data is taken from custom perlin noise function. I guess browser would crash every time if I would try to precalculate such large data array. I’m trying to precalculate and replace map data of current and all neighboring zones, so exploring experience would be smooth.