Render3D. VoxelMap

VoxelMap - A grid of blocks, a 3D tile map: it draws itself, and objects with collideLevel collide with it

  • pos3D is its corner, as a 2D tile layer's is, and each cell is one world unit, so cell (x, y, z) fills pos3D + (x..x+1, y..y+1, z..z+1); it stays upright and unscaled at the root
  • A block's type is a number from 1 to 255, 0 is empty; a type shows that tile of the sheet on every face unless setBlockType gives it its own faces, or makes it see-through or transparent
  • Faces between blocks are left out, and the map is drawn in chunks of 16 cells a side, a chunk built again only when a block in or beside it changes
  • Objects with collideLevel collide with it, see EngineObject3D.collideWithVoxel, and raycast finds the block a ray hits and the face it comes in through

Constructor

new VoxelMap(pos3Dopt, mapSizeopt, tileInfoopt)

Create a voxel map, it draws itself and joins the level's collision

Parameters:
NameTypeAttributesDescription
pos3DVector3<optional>

Its corner

mapSizeVector3<optional>

Cells along X, Y and Z

tileInfoTileInfo<optional>

The sheet's first tile, as for a TileLayer, a block's type counts tiles from it; give the tiles a border of their own edge pixels, 2 or more, and that padding, or the blurred mipmaps of the distance blend each tile with the ones beside it and show seams along the block edges

Example
const map = new VoxelMap(vec3(), vec3(32, 16, 32), tile(0, 16));
map.setBlockType(1, {top: 0, side: 1, bottom: 2}); // grass
map.setVoxel(vec3(3, 0, 5), 1);

Extends

  • EngineObject3D

Members

ambientOcclusion

Properties
TypeDescription
boolean

Darken the corners where blocks meet, rebuild() after changing it

blockTypes :Array.<({faces: Array.<number>, seeThrough: boolean, transparent: boolean}|undefined)>

Type:
  • Array.<({faces: Array.<number>, seeThrough: boolean, transparent: boolean}|undefined)>

chunkCenters :Array.<Vector3>

Type:
  • Array.<Vector3>

chunkMeshes :Array.<(Mesh|undefined)>

Type:
  • Array.<(Mesh|undefined)>

chunkTransparentMeshes :Array.<(Mesh|undefined)>

Type:
  • Array.<(Mesh|undefined)>

data

Properties
TypeDescription
Uint8Array

The block type of each cell, x + mapSize.x * (y + mapSize.y * z), 0 empty; call rebuild() after changing it directly

mapSize

Properties
TypeDescription
Vector3

Cells along X, Y and Z

tileInfo

Properties
TypeDescription
TileInfo

The sheet's first tile, rebuild() after changing it or resizing its texture

Methods

buildChunks()

Build the chunks that changed, the map calls it before it draws

destroy(immediateopt)

Destroy the map, it leaves the level's collision and lets go of its meshes

Parameters:
NameTypeAttributesDescription
immediateboolean<optional>

getVoxel(cell) → {number}

The block type at a cell, 0 for empty or outside the map

Parameters:
NameTypeDescription
cellVector3
Returns:
Type: 
number

raycast(ray, maxDistanceopt, testopt) → {VoxelHit|undefined}

The first block a ray hits, walking the grid cell by cell: its distance along the ray, its cell, the normal of the face it comes in through, which a new block goes against, and its type

  • A ray that starts inside a block hits it at 0, its normal back along the ray
Parameters:
NameTypeAttributesDescription
rayRay3D

Its distance is in the ray's own units, as the other raycasts

maxDistancenumber<optional>
testfunction<optional>

(type, cell) says which blocks count, every block by default

Returns:
Type: 
VoxelHit | undefined

rebuild()

Build every chunk again, after changing data directly or ambientOcclusion

render3D()

Draw the solid and see-through blocks, the transparent ones draw through its child

setBlockType(type, faces)

Give a block type its own faces, or make it see-through or transparent

Parameters:
NameTypeDescription
typenumber

1 to 255

faces

setVoxel(cell, type)

Set the block at a cell, 0 clears it; a cell outside the map is ignored

Parameters:
NameTypeDescription
cellVector3
typenumber

0 to 255

update()

Keeps an eye on its placement, called automatically each frame