Skip to main content

A Voxelize world handles the chunk loading and rendering, as well as any 3D objects. This class extends the ThreeJS Scene class. This means that you can add any ThreeJS objects to the world, and they will be rendered. The world also implements NetIntercept, which means it intercepts chunk-related packets from the server and constructs chunk meshes from them. You can optionally disable this by setting shouldGenerateChunkMeshes to false in the options.

There are a couple components that are by default created by the world that holds data:

  • World.registry: A block registry that handles block textures and block instances.
  • World.chunks: A chunk manager that stores all the chunks in the world.
  • World.physics: A physics engine that handles voxel AABB physics simulation of client-side physics.
  • World.loader: An asset loader that handles loading textures and other assets.
  • World.sky: A sky that can render the sky and the sun.
  • World.clouds: A clouds that renders the cubical clouds.

One thing to keep in mind that there are no specific setters like setVoxelByVoxel or setVoxelRotationByVoxel. This is because, instead, you should use updateVoxel and updateVoxels to update voxels.

Example

const world = new VOXELIZE.World();

// Update the voxel at `(0, 0, 0)` to a voxel type `12` in the world across the network.
world.updateVoxel(0, 0, 0, 12)

// Register the interceptor with the network.
network.register(world);

// Register an image to block sides.
world.applyBlockTexture("Test", VOXELIZE.ALL_FACES, "https://example.com/test.png");

// Update the world every frame.
world.update(controls.position);

World

Type parameters

NameType
Tany

Hierarchy

  • Scene

    World

Implements

Constructors

constructor

new World<T>(options?): World<T>

Create a new Voxelize world.

Type parameters

NameType
Tany

Parameters

NameTypeDescription
optionsPartial<WorldOptions>The options to create the world.

Returns

World<T>

Overrides

Scene.constructor

Properties

chunks

chunks: Chunks

The manager that holds all chunk-related data, such as chunk meshes and voxel data.


clouds

clouds: Clouds

The clouds that renders the cubical clouds.


isInitialized

isInitialized: boolean = false

Whether or not this world is connected to the server and initialized with data from the server.


loader

loader: Loader

An asset loader to load in things like textures, images, GIFs and audio buffers.


options

options: WorldOptions

The options to create the world.


physics

physics: Engine

The voxel physics engine using @voxelize/physics-engine.


registry

registry: Registry

The block registry that holds all block data, such as texture and block properties.


sky

sky: Sky

The sky that renders the sky and the sun.

Accessors

deleteRadius

get deleteRadius(): number

Returns

number


renderRadius

get renderRadius(): number

Returns

number

set renderRadius(radius): void

Parameters

NameType
radiusnumber

Returns

void


time

get time(): number

Returns

number

set time(time): void

Parameters

NameType
timenumber

Returns

void

Methods

addBlockEntityUpdateListener

addBlockEntityUpdateListener(listener): () => void

Parameters

NameType
listenerBlockEntityUpdateListener<T>

Returns

fn

▸ (): void

Returns

void


addBlockUpdateListener

addBlockUpdateListener(listener): () => void

Parameters

NameType
listenerBlockUpdateListener

Returns

fn

▸ (): void

Returns

void


addChunkInitListener

addChunkInitListener(coords, listener): () => void

Add a listener to a chunk. This listener will be called when this chunk is loaded and ready to be rendered. This is useful for, for example, teleporting the player to the top of the chunk when the player just joined.

Parameters

NameTypeDescription
coordsCoords2The chunk coordinates to listen to.
listener(chunk: Chunk) => voidThe listener to add.

Returns

fn

▸ (): void

Returns

void


applyBlockFrames

applyBlockFrames(idOrName, faceNames, keyframes, fadeFrames?): Promise<void>

Apply a set of keyframes to a block. This will load the keyframes from the sources and start the animation to play the keyframes on the block's texture atlas.

Parameters

NameTypeDefault valueDescription
idOrNamestring | numberundefinedThe ID or name of the block.
faceNamesstring | string[]undefinedThe face name or names to apply the texture to.
keyframes[number, string | Color | HTMLImageElement][]undefinedThe keyframes to apply to the texture.
fadeFramesnumber0The number of frames to fade between each keyframe.

Returns

Promise<void>


applyBlockGif

applyBlockGif(idOrName, faceNames, source, interval?): Promise<void>

Apply a GIF animation to a block. This will load the GIF from the source and start the animation using applyBlockFrames internally.

Parameters

NameTypeDefault valueDescription
idOrNamestringundefinedThe ID or name of the block.
faceNamesstring | string[]undefinedThe face name or names to apply the texture to.
sourcestringundefinedThe source of the GIF. Note that this must be a GIF file ending with .gif.
intervalnumber66.666667The interval between each frame of the GIF in milliseconds. Defaults to 66.666667ms.

Returns

Promise<void>


applyBlockTexture

applyBlockTexture(idOrName, faceNames, source): void

Apply a texture to a face or faces of a block. This will automatically load the image from the source and draw it onto the block's texture atlas.

Parameters

NameTypeDescription
idOrNamestring | numberThe ID or name of the block.
faceNamesstring | string[]The face names to apply the texture to.
sourcestring | Color | Texture | HTMLImageElementThe source of the texture.

Returns

void


applyBlockTextureAt

applyBlockTextureAt(idOrName, faceName, source, voxel): CustomChunkShaderMaterial

Parameters

NameType
idOrNamestring | number
faceNamestring
sourcestring | Color | Texture | HTMLImageElement
voxelCoords3

Returns

CustomChunkShaderMaterial


applyBlockTextures

applyBlockTextures(data): Promise<void[]>

Apply multiple block textures at once. See applyBlockTexture for more information.

Parameters

NameTypeDescription
data{ faceNames: string | string[] ; idOrName: string | number ; source: string | Color }[]The data to apply the block textures.

Returns

Promise<void[]>

A promise that resolves when all the textures are applied.


customizeBlockDynamic

customizeBlockDynamic(idOrName, fn): void

Parameters

NameType
idOrNamestring | number
fn(pos: Coords3) => { aabbs: AABB[] ; faces: { corners: { pos: [number, number, number] ; uv: number[] }[] ; dir: [number, number, number] ; independent: boolean ; isolated: boolean ; name: string ; range: UV }[] ; isTransparent: [boolean, boolean, boolean, boolean, boolean, boolean] }

Returns

void


customizeMaterialShaders

customizeMaterialShaders(idOrName, faceName?, data?): CustomChunkShaderMaterial

Parameters

NameTypeDefault value
idOrNamestring | numberundefined
faceNamestringnull
dataObjectundefined
data.fragmentShaderstringundefined
data.uniforms?Objectundefined
data.vertexShaderstringundefined

Returns

CustomChunkShaderMaterial


floodLight

floodLight(queue, color, min?, max?): void

Parameters

NameType
queueLightNode[]
colorLightColor
min?Coords3
max?Coords3

Returns

void


getBlockAABBsAt

getBlockAABBsAt(vx, vy, vz): AABB[]

Parameters

NameType
vxnumber
vynumber
vznumber

Returns

AABB[]


getBlockAABBsByIdAt

getBlockAABBsByIdAt(id, vx, vy, vz): AABB[]

Parameters

NameType
idnumber
vxnumber
vynumber
vznumber

Returns

AABB[]


getBlockAABBsForDynamicPatterns

getBlockAABBsForDynamicPatterns(vx, vy, vz, dynamicPatterns): AABB[]

Parameters

NameType
vxnumber
vynumber
vznumber
dynamicPatternsBlockDynamicPattern[]

Returns

AABB[]


getBlockAt

getBlockAt(px, py, pz): Block

Get the block type data by a 3D world position.

Parameters

NameTypeDescription
pxnumberThe x coordinate of the position.
pynumberThe y coordinate of the position.
pznumberThe z coordinate of the position.

Returns

Block

The block at the given position, or null if it does not exist.


getBlockById

getBlockById(id): Block

Get the block type data by a block id.

Parameters

NameTypeDescription
idnumberThe block id.

Returns

Block

The block data for the given id, or null if it does not exist.


getBlockByName

getBlockByName(name): Block

Get the block type data by a block name.

Parameters

NameTypeDescription
namestringThe block name.

Returns

Block

The block data for the given name, or null if it does not exist.


getBlockEntityDataAt

getBlockEntityDataAt(px, py, pz): T

Parameters

NameType
pxnumber
pynumber
pznumber

Returns

T


getBlockFaceMaterial

getBlockFaceMaterial(idOrName, faceName?, voxel?): CustomChunkShaderMaterial

Parameters

NameType
idOrNamestring | number
faceName?string
voxel?Coords3

Returns

CustomChunkShaderMaterial


getBlockFacesByFaceNames

getBlockFacesByFaceNames(id, faceNames): { corners: { pos: [number, number, number] ; uv: number[] }[] ; dir: [number, number, number] ; independent: boolean ; isolated: boolean ; name: string ; range: UV }[]

Parameters

NameType
idnumber
faceNamesstring | RegExp | string[]

Returns

{ corners: { pos: [number, number, number] ; uv: number[] }[] ; dir: [number, number, number] ; independent: boolean ; isolated: boolean ; name: string ; range: UV }[]


getBlockOf

getBlockOf(idOrName): Block

Parameters

NameType
idOrNamestring | number

Returns

Block


getChunkByCoords

getChunkByCoords(cx, cz): Chunk

Get a chunk by its 2D coordinates.

Parameters

NameTypeDescription
cxnumberThe x coordinate of the chunk.
cznumberThe z coordinate of the chunk.

Returns

Chunk

The chunk at the given coordinates, or undefined if it does not exist.


getChunkByName

getChunkByName(name): Chunk

Get a chunk by its name.

Parameters

NameTypeDescription
namestringThe name of the chunk to get.

Returns

Chunk

The chunk with the given name, or undefined if it does not exist.


getChunkByPosition

getChunkByPosition(px, py, pz): Chunk

Get a chunk that contains a given position.

Parameters

NameTypeDescription
pxnumberThe x coordinate of the position.
pynumberThe y coordinate of the position.
pznumberThe z coordinate of the position.

Returns

Chunk

The chunk that contains the position at the given position, or undefined if it does not exist.


getChunkStatus

getChunkStatus(cx, cz): "to request" | "requested" | "processing" | "loaded"

Get the status of a chunk.

Parameters

NameTypeDescription
cxnumberThe x 2D coordinate of the chunk.
cznumberThe z 2D coordinate of the chunk.

Returns

"to request" | "requested" | "processing" | "loaded"

The status of the chunk.


getIsolatedBlockMaterialAt

getIsolatedBlockMaterialAt(voxel, faceName, defaultDimension?): CustomChunkShaderMaterial

Parameters

NameType
voxelCoords3
faceNamestring
defaultDimension?number

Returns

CustomChunkShaderMaterial


getLightColorAt

getLightColorAt(vx, vy, vz): Color

Get a color instance that represents what an object would be like if it were rendered at the given 3D voxel coordinate. This is useful to dynamically shade objects based on their position in the world. Also used in LightShined.

Parameters

NameTypeDescription
vxnumberThe voxel's X position.
vynumberThe voxel's Y position.
vznumberThe voxel's Z position.

Returns

Color

The voxel's light color at the given coordinate.


getMaxHeightAt

getMaxHeightAt(px, pz): number

Get the highest block at a x/z position. Highest block means the first block counting downwards that isn't empty (isEmpty).

Parameters

NameTypeDescription
pxnumberThe x coordinate of the position.
pznumberThe z coordinate of the position.

Returns

number

The highest block at the given position, or 0 if it does not exist.


getPreviousValueAt

getPreviousValueAt(px, py, pz, count?): number

Get the previous value of a voxel by a 3D world position.

Parameters

NameTypeDefault valueDescription
pxnumberundefinedThe x coordinate of the position.
pynumberundefinedThe y coordinate of the position.
pznumberundefinedThe z coordinate of the position.
countnumber1By how much to look back in the history. Defaults to 1.

Returns

number


getSunlightAt

getSunlightAt(px, py, pz): number

Get a voxel sunlight by a 3D world position.

Parameters

NameTypeDescription
pxnumberThe x coordinate of the position.
pynumberThe y coordinate of the position.
pznumberThe z coordinate of the position.

Returns

number

The voxel sunlight at the given position, or 0 if it does not exist.


getTorchLightAt

getTorchLightAt(px, py, pz, color): number

Get a voxel torch light by a 3D world position.

Parameters

NameTypeDescription
pxnumberThe x coordinate of the position.
pynumberThe y coordinate of the position.
pznumberThe z coordinate of the position.
colorLightColorThe color of the torch light.

Returns

number

The voxel torchlight at the given position, or 0 if it does not exist.


getVoxelAt

getVoxelAt(px, py, pz): number

Get a voxel by a 3D world position.

Parameters

NameTypeDescription
pxnumberThe x coordinate of the position.
pynumberThe y coordinate of the position.
pznumberThe z coordinate of the position.

Returns

number

The voxel at the given position, or 0 if it does not exist.


getVoxelRotationAt

getVoxelRotationAt(px, py, pz): BlockRotation

Get a voxel rotation by a 3D world position.

Parameters

NameTypeDescription
pxnumberThe x coordinate of the position.
pynumberThe y coordinate of the position.
pznumberThe z coordinate of the position.

Returns

BlockRotation

The voxel rotation at the given position, or the default rotation if it does not exist.


getVoxelStageAt

getVoxelStageAt(px, py, pz): number

Get a voxel stage by a 3D world position.

Parameters

NameTypeDescription
pxnumberThe x coordinate of the position.
pynumberThe y coordinate of the position.
pznumberThe z coordinate of the position.

Returns

number

The voxel stage at the given position, or 0 if it does not exist.


initialize

initialize(): Promise<void>

Initialize the world with the data received from the server. This includes populating the registry, setting the options, and creating the texture atlas.

Returns

Promise<void>


isChunkInView

isChunkInView(center, target, direction, threshold): boolean

Parameters

NameType
centerCoords2
targetCoords2
directionVector3
thresholdnumber

Returns

boolean


isWithinWorld

isWithinWorld(cx, cz): boolean

Whether or not if this chunk coordinate is within (inclusive) the world's bounds. That is, if this chunk coordinate is within WorldServerOptions.minChunk and WorldServerOptions.maxChunk.

Parameters

NameTypeDescription
cxnumberThe chunk's X position.
cznumberThe chunk's Z position.

Returns

boolean

Whether or not this chunk is within the bounds of the world.


makeBlockMesh

makeBlockMesh(idOrName, options?): Group<Object3DEventMap>

Get a mesh of the model of the given block.

Parameters

NameTypeDescription
idOrNamestring | number-
optionsPartial<{ crumbs: boolean ; material: "basic" | "standard" ; separateFaces: boolean }>The options of creating this block mesh.

Returns

Group<Object3DEventMap>

A 3D mesh (group) of the block model.


meshChunkLocally

meshChunkLocally(cx, cz, level): Promise<void>

Parameters

NameType
cxnumber
cznumber
levelnumber

Returns

Promise<void>


raycastVoxels

raycastVoxels(origin, direction, maxDistance, options?): Object

Raycast through the world of voxels and return the details of the first block intersection.

Parameters

NameTypeDescription
originCoords3The origin of the ray.
directionCoords3The direction of the ray.
maxDistancenumberThe maximum distance of the ray.
optionsObjectThe options for the ray.
options.ignoreFluids?booleanWhether or not to ignore fluids. Defaults to true.
options.ignoreList?number[]A list of blocks to ignore. Defaults to [].
options.ignorePassables?booleanWhether or not to ignore passable blocks. Defaults to false.
options.ignoreSeeThrough?booleanWhether or not to ignore see through blocks. Defaults to false.

Returns

Object

NameType
normalnumber[]
pointnumber[]
voxelnumber[]

removeLight

removeLight(voxel, color): void

Parameters

NameType
voxelCoords3
colorLightColor

Returns

void


setBlockEntityDataAt

setBlockEntityDataAt(px, py, pz, data): void

Parameters

NameType
pxnumber
pynumber
pznumber
dataT

Returns

void


setResolutionOf

setResolutionOf(idOrName, faceNames, resolution): Promise<void>

Apply a resolution to a block. This will set the resolution of the block's texture atlas. Keep in mind that this face or faces must be independent.

Parameters

NameTypeDescription
idOrNamestring | numberThe ID or name of the block.
faceNamesstring | string[]The face name or names to apply the resolution to.
resolutionnumber | { x: number ; y: number }The resolution to apply to the block, in pixels.

Returns

Promise<void>


setSunlightAt

setSunlightAt(px, py, pz, level): void

Parameters

NameType
pxnumber
pynumber
pznumber
levelnumber

Returns

void


setTorchLightAt

setTorchLightAt(px, py, pz, level, color): void

Parameters

NameType
pxnumber
pynumber
pznumber
levelnumber
colorLightColor

Returns

void


setVoxelAt

setVoxelAt(px, py, pz, voxel): void

Parameters

NameType
pxnumber
pynumber
pznumber
voxelnumber

Returns

void


setVoxelRotationAt

setVoxelRotationAt(px, py, pz, rotation): void

Set a voxel rotation at a 3D world position.

Parameters

NameTypeDescription
pxnumberThe x coordinate of the position.
pynumberThe y coordinate of the position.
pznumberThe z coordinate of the position.
rotationBlockRotationThe rotation to set.

Returns

void


setVoxelStageAt

setVoxelStageAt(px, py, pz, stage): void

Parameters

NameType
pxnumber
pynumber
pznumber
stagenumber

Returns

void


update

update(position?, direction?): void

Parameters

NameType
positionVector3
directionVector3

Returns

void


updateSkyAndClouds

updateSkyAndClouds(position): void

Parameters

NameType
positionVector3

Returns

void


updateVoxel

updateVoxel(vx, vy, vz, type, options): void

This sends a block update to the server and updates across the network. Block updates are queued to World.chunks.toUpdate and scaffolded to the server WorldClientOptions.maxUpdatesPerUpdate times per tick. Keep in mind that for rotation and y-rotation, the value should be one of the following:

This ignores blocks that are not defined, and also ignores rotations for blocks that are not Block.rotatable (Same for if block is not Block.yRotatable).

Parameters

NameTypeDescription
vxnumberThe voxel's X position.
vynumberThe voxel's Y position.
vznumberThe voxel's Z position.
typenumberThe type of the voxel.
optionsObjectThe options for the voxel.
options.rotation?numberThe major axis rotation of the voxel.
options.source?"client" | "server"Whether the update is from the client or server. Defaults to "client".
options.stage?numberThe stage of the voxel.
options.yRotation?numberThe Y rotation on the major axis. Applies to blocks with major axis of PY or NY.

Returns

void


updateVoxels

updateVoxels(updates, source?): void

This sends a list of block updates to the server and updates across the network. Block updates are queued to World.chunks.toUpdate and scaffolded to the server WorldClientOptions.maxUpdatesPerUpdate times per tick. Keep in mind that for rotation and y-rotation, the value should be one of the following:

This ignores blocks that are not defined, and also ignores rotations for blocks that are not Block.rotatable (Same for if block is not Block.yRotatable).

Parameters

NameTypeDefault valueDescription
updatesBlockUpdate[]undefinedA list of updates to send to the server.
source"client" | "server""client"-

Returns

void