API

Creating A Tiled Ground

Tiled Ground

A tiled ground is created differently from a ground mesh. It still lies in the xz plane. The bottom-left corner of the tiled ground is given by the values for xmin and zmin, and the top-right corner by xmax and zmax. The tiled ground is subdivided into tile regions: across into w tiles and up into h tiles. In the same way, every tile can be further subdivided into w by h sections. Creating a tiled ground relies on the use of MultiMaterials.

MeshBuilder

Usage:

import { MeshBuilder } from "@babylonjs/core/Meshes/meshBuilder";
var tiledGround = MeshBuilder.CreateTiledGround("tiled ground", options, scene);
optionvaluedefault value
xmin
(number) map min x coordinate value
-1
zmin
(number) map min z coordinate value
-1
xmax
(number) map max x coordinate value
1
zmax
(number) map max z coordinate value
1
subdivisions
object ( {w: number, h: number} ) number of subdivisions (tiles) on the height and the width of the map
{w: 6, h: 6}
precision
( {w: number, h: number} ) number of subdivisions on the height and the width of each tile
{w: 2, h: 2}
updatable
(boolean) true if the mesh is updatable
false

Steps to Create Tiling

  • Create Tiled Ground
  • Create materials needed for each tile
  • Create a multi material and push each material onto its subMaterials array
  • Set the multi material as the material for the Tiled Ground
  • Set the subMeshes property of the Tiled Ground to an empty array
  • Create and set values for these variables
const verticesCount = tiledGround.getTotalVertices();
const tileIndicesLength = tiledGround.getIndices().length / (subdivisions.w * subdivisions.h);
  • Fill the subMeshes array of the Tiled Ground using
import { SubMesh } from "@babylonjs/core/Meshes/subMesh";
let base = 0;
for (let row = 0; row < grid.h; row++) {
for (let col = 0; col < grid.w; col++) {
new SubMesh(row % 2 ^ col % 2, 0, verticesCount, base, tileIndicesLength, tiledGround);
base += tileIndicesLength;
}
}

Examples

Chess Board: Create a Chess Board

Using these two materials:

grass rock

Create a Chess Board With Grass And Rock

Forming a large scale map using open source map tiles: Create a Large Scale Map

Mesh

Usage :

import { MeshBuilder } from "@babylonjs/core/Meshes/meshBuilder";
const tiledGround = MeshBuilder.CreateTiledGround("Tiled Ground", { xmin, zmin, xmax, zmax, subdivisions, precision }, scene);