LittleJS 1.28.0

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

Extends EngineObject3D

Constructor

new VoxelMap(pos3D, mapSize, tileInfo)render3dVoxels.js:59

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

pos3D Vector3 optional

Its corner

mapSize Vector3 optional

Cells along X, Y and Z

tileInfo TileInfo 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

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);

Members

Methods

Inherited from EngineObject3D

Inherited from EngineObject

Members

ambientOcclusion : booleanrender3dVoxels.js:80

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

blockTypes : Array<({faces: Array<number>, seeThrough: boolean, transparent: boolean, doubleSided: boolean}|undefined)>render3dVoxels.js:94
chunkCenters : Array<Vector3>render3dVoxels.js:90
chunkMeshes : Array<(Mesh|undefined)>render3dVoxels.js:86
chunksChanged : Set<number>render3dVoxels.js:92
chunkTransparentMeshes : Array<(Mesh|undefined)>render3dVoxels.js:88
data : Uint8Arrayrender3dVoxels.js:78

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

Cells along X, Y and Z

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

tiles : Map<number, Array<Vector2>>render3dVoxels.js:96

Methods

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

destroy(immediate)render3dVoxels.js:526

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

immediate boolean optional
getVoxel(cell) → numberrender3dVoxels.js:111

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

cell Vector3
raycast(ray, maxDistance, test) → VoxelHit|undefinedrender3dVoxels.js:412

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
ray Ray3D

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

maxDistance number optional
test function optional

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

Build every chunk again, after changing data directly or ambientOcclusion

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

setBlockType(type, faces)render3dVoxels.js:171

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

type number

1 to 255

faces number|Array<number>|{top?: number, side: number, bottom?: number}

A tile index for every face, six in the order +x, -x, +y, -y, +z, -z, or the side's with the top and bottom's, which default to the side's

setVoxel(cell, type)render3dVoxels.js:132

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

cell Vector3
type number

0 to 255

Keeps an eye on its placement, called automatically each frame