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:
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)) {
return;
}
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) {
return;
}
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;
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 :
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.