Class: VoxelWorld

VoxelWorld(options)

Manages voxel data. At the top level, a single VoxelWorld consists of cells. Each cell is a 'chunk' of the world that consists of voxels (i.e. cubes). In order to optimize render times, we merge the geometry of all the voxels within a single cell and make a single render call (as opposed to rendering each individual voxel). In addition, a single cell is essentially a 3D grid that voxels are placed in. Each cell has a length, width, and height dictated by the cellSize variable. This could be set to anything but is perhaps best capped at 128 or 256 (256^3 is 16,777,216 voxels!).

Constructor

new VoxelWorld(options)

Creates a VoxelWorld object with the given options

Parameters:
Name Type Description
options Object

Options to spawn the world with

Properties
Name Type Description
cellSize number

The length, width, and height of each cell

tileSize number

The size of each tile from a texture atlas

tileTextureWidth number

The width of the texture atlas

tileTextureHeight number

The height of the texture atlas

material *

The material that the VoxelWorld should use for its meshes

colorPalette- ColorPalette

The current color palette that the world is using

Properties:
Name Type Description
cellSize number

The length, width, and height of a single cell (or chunk) within the world

cellSliceSize number

The area of a single slice of each cell (cellSize^2)

cells Object

Object consisting of an array for each cell

Source:

Members

(static) faces

Array of objects that represent each face of a single voxel. uvRow is the row of the texture atlas to grab an image from dir is the direction of the face corners consist of vertices and uv coordinates for the texture

Source:

Methods

addCellForVoxel(x, y, z) → {Uint8Array}

Adds a new cell for a voxel at the given x, y, and z coordinates if a cell doesn't already exist to accomodate it.

Parameters:
Name Type Description
x number
y number
z number
Source:
Returns:

Array of voxels for the cell

Type
Uint8Array

computeCellId(x, y, z) → {string}

Computes the id of the cell stored as a key in this.cells based on the given x, y, and z coordinates of a voxel.

Parameters:
Name Type Description
x number
y number
z number
Source:
Returns:

The id of the cell in the form of "(x,y,z)"

Type
string

computeVoxelOffset(x, y, z) → {number}

Returns the offset, or index, to the voxel within the cell array at the given x, y, and z coordinates.

Parameters:
Name Type Description
x number
y number
z number
Source:
Returns:

Index to the voxel within the cell array

Type
number

floodFillVoxels(startX, startY, startZ, normX, normY, normZ, v, isExtruding)

Performs a flood fill starting at a given voxel position and sets that voxel and all voxels of the same type to the given v voxel type. If the voxel is obstructed by another voxel along the given normal, it is left untouched. Otherwise, it will be changed to the given v voxel type. If extruding, then adjacent empty voxels along the given normal will be changed to the given v voxel type instead.

Parameters:
Name Type Description
startX number

The x coordinate of the starting voxel

startY number

The y coordinate of the starting voxel

startZ number

The z coordinate of the starting voxel

normX number

The x normal to check along

normY number

The y normal to check along

normZ number

The z normal to check along

v number

The new voxel to flood fill with

isExtruding boolean

If true, sets adjacent empty voxels along the normal to v. Otherwise, just changes adjacent voxels that share the same color of the starting voxel

Source:

generateGeometryDataForCell(cellX, cellY, cellZ)

Generates geometry data for a cell at the given coordinate. Similar to voxels, each cell is a part of a 3D grid as well.

Parameters:
Name Type Description
cellX number
cellY number
cellZ number
Source:
Example
generateGeometryDataForCell(0, 0, 0);  // Cell created at (0, 0, 0) coordinate
generateGeometryDataForCell(0, 1, 0);  // Cell created above the last one at (0, 1, 0)

getCellForVoxel(x, y, z) → {Uint8Array}

Finds the corresponding voxel array for the cell for the given voxel coordinates.

Parameters:
Name Type Description
x number
y number
z number
Source:
Returns:

Array of voxels.

Type
Uint8Array

getVoxel(x, y, z) → {number}

Gets the corresponding voxel at the given coordinates.

Parameters:
Name Type Description
x number
y number
z number
Source:
Returns:

Number representing the type of voxel

Type
number

intersectRay(start, end) → {Object|Array.<number>|Array.<number>|number}

Algorithm for raycasting specialized for use with voxels. Used to check if the user clicked a voxel in the scene and returns information related to it such as the coordinates of the successful hit. The code itself is based upon this paper: http://www.cse.chalmers.se/edu/year/2010/course/TDA361/grid.pdf

Parameters:
Name Type Description
start *
end *
Source:
Returns:
  • HitResults or null if nothing was hit

    Type
    Object
  • HitResults.position Coordinates of the hit

    Type
    Array.<number>
  • HitResults.normal Normal of the hit

    Type
    Array.<number>
  • HitResults.voxel The type of voxel hit

    Type
    number

removeAllCells(scene)

Removes every cell from the world along with associated meshes.

Parameters:
Name Type Description
scene Scene

The scene object to remove the cells from

Source:

setVoxel(x, y, z, v, addCell)

Sets voxel at given coordinates.

Parameters:
Name Type Default Description
x number
y number
z number
v number

The type of voxel to add

addCell boolean true

If true, a new cell will be created to accomodate the voxel if needed

Source:

updateCellGeometry(scene, x, y, z)

Updates the geometry of the cell with the given coordinates within the scene.

Parameters:
Name Type Description
scene

The scene to add the final mesh to

x number
y number
z number
Source:

updateVoxelGeometry(scene, x, y, z)

Updates the voxel of a cell at the given x, y, and z coordinates. Also, updates any cells that the voxel is adjacent to.

Parameters:
Name Type Description
scene

The scene to add the final mesh to

x number
y number
z number
Source:

updateWorldGeometry(scene)

Updates every single cell within the world. Useful for when loading in a brand new world.

Parameters:
Name Type Description
scene *
Source: