Skip to main content

A utility class for all things related to chunks and chunk coordinates.

Example

// Get the chunk coordinates of a voxel, (0, 0) with `chunkSize=16`.
const chunkCoords = ChunkUtils.mapVoxelToChunk([1, 10, 12]);

Methods

getChunkName

getChunkName(coords, concat?): string

Convert a 2D chunk coordinate to a string representation.

Parameters

NameTypeDefault valueDescription
coordsCoords2undefinedThe coordinates to convert.
concatstring`""`

Returns

string

The string representation of the coordinates.


getVoxelName

getVoxelName(coords, concat?): string

Convert a 3D voxel coordinate to a string representation.

Parameters

NameTypeDefault valueDescription
coordsCoords3undefinedThe coordinates to convert.
concatstring`""`

Returns

string

The string representation of the coordinates.


mapChunkToVoxel

mapChunkToVoxel(chunkPos, chunkSize): Coords3

Map a 2D chunk coordinate to the 3D voxel coordinate.

Parameters

NameTypeDescription
chunkPosCoords2The chunk coordinate to map.
chunkSizenumberThe horizontal dimension of a chunk.

Returns

Coords3

The mapped coordinate.


mapVoxelToChunk

mapVoxelToChunk(voxelPos, chunkSize): Coords2

Map a 3D voxel coordinate to the 2D chunk coordinate.

Parameters

NameTypeDescription
voxelPosCoords3The voxel coordinate to map.
chunkSizenumberThe horizontal dimension of a chunk.

Returns

Coords2

The mapped coordinate.


mapVoxelToChunkLocal

mapVoxelToChunkLocal(voxelPos, chunkSize): Coords3

Map a 3D voxel coordinate to the local 3D voxel coordinate in the situated chunk.

Parameters

NameTypeDescription
voxelPosCoords3The voxel coordinate to map.
chunkSizenumberThe horizontal dimension of a chunk.

Returns

Coords3

The mapped coordinate.


mapWorldToVoxel

mapWorldToVoxel(worldPos): Coords3

Map a 3D world coordinate to the 3D voxel coordinate. Since a voxel is exactly 1 unit in size, this is just a floor operation.

Parameters

NameTypeDescription
worldPosCoords3The world coordinate to map.

Returns

Coords3

The mapped coordinate.


parseChunkName

parseChunkName(name, concat?): number[]

Given a chunk representation, parse the chunk coordinates.

Parameters

NameTypeDefault valueDescription
namestringundefinedThe string representation of the chunk.
concatstring`""`

Returns

number[]

The parsed chunk coordinates.


scaleCoordsF

scaleCoordsF(coords, factor): Coords3

Scale and floor a 3D coordinate.

Parameters

NameTypeDescription
coordsCoords3The coordinates to scale and floor.
factornumberThe factor to scale by.

Returns

Coords3

The scaled and floored coordinates.