Skip to content

API reference / @jsprismarine/worldgen / DensityFunctionCompiler

Class: DensityFunctionCompiler ​

Defined in: density/compile.ts:411

Turns parsed density function trees into closures, once per world seed.

One compiler serves every function of one import('../RandomState').RandomState: it memoizes by tree identity and hands out the cache slot ids that cache nodes share, which is what makes continents cost one noise evaluation per position even though the router reaches it through five different slots.

Constructors ​

Constructor ​

new DensityFunctionCompiler(environment, options?): DensityFunctionCompiler

Defined in: density/compile.ts:427

Parameters ​

environment ​

CompileEnvironment

options? ​

DensityFunctionCompilerOptions = {}

Returns ​

DensityFunctionCompiler

Accessors ​

cacheCount ​

Get Signature ​

get cacheCount(): number

Defined in: density/compile.ts:437

How many memo slots the trees compiled so far need.

Returns ​

number


closureFallbackCount ​

Get Signature ​

get closureFallbackCount(): number

Defined in: density/compile.ts:452

How many nodes used the closure compiler because code generation was disabled or unsupported.

Returns ​

number


generatedFunctionBytes ​

Get Signature ​

get generatedFunctionBytes(): number

Defined in: density/compile.ts:447

Source characters compiled by the optional code generator; generated source is ASCII.

Returns ​

number


generatedFunctionCount ​

Get Signature ​

get generatedFunctionCount(): number

Defined in: density/compile.ts:442

How many sampler functions the optional closed-node code generator emitted.

Returns ​

number


interpolationCellSize ​

Get Signature ​

get interpolationCellSize(): InterpolationCellSize | undefined

Defined in: density/compile.ts:470

The largest block extent that sits inside one cell of every interpolated node compiled so far, or undefined while none has been.

Vanilla's five overworld nodes all ask for 4 by 8 by 4, so this is that. Taking the greatest common divisor is what makes the answer right for a set of differing sizes: the cells of a node of size s are aligned to multiples of s, and gcd | s, so a block of the divisor's size never straddles two of them.

This covers every tree the compiler has seen, so it shrinks when an unrelated tree with a finer cell compiles. The surface pass compiles chunk_surface_level with a 16 by 1 cell, and a chunk walked in the gcd of both visits eight times as many cells as final density needs. Use DensityFunctionCompiler.interpolationCellSizeOf to size a walk.

Returns ​

InterpolationCellSize | undefined

Methods ​

compile() ​

compile(node): DensitySampler

Defined in: density/compile.ts:547

Compiles a tree, reusing the closure when the same tree was compiled before.

Parameters ​

node ​

DensityFunctionNode

Returns ​

DensitySampler

Throws ​

When the tree contains a node type the compiler does not know, which only happens if a parser and this switch drift apart.


compileVolume() ​

compileVolume(node): DensityVolumeEvaluator

Defined in: density/compile.ts:558

Compiles an opt-in typed-array evaluator with scalar samplers for unsupported nodes.

Parameters ​

node ​

DensityFunctionNode

Returns ​

DensityVolumeEvaluator


interpolationCellSizeOf() ​

interpolationCellSizeOf(node): InterpolationCellSize | undefined

Defined in: density/compile.ts:483

The cell shared by the interpolated nodes reachable from one tree, or undefined when it holds none.

The answer is the same gcd DensityFunctionCompiler.interpolationCellSize takes, over the nodes this tree can evaluate instead of every node the compiler holds, so it does not depend on which other trees were compiled first. Trees share subtrees, so the walk memoizes by node and visits each once.

Parameters ​

node ​

DensityFunctionNode

Returns ​

InterpolationCellSize | undefined


isYIndependent() ​

isYIndependent(node): boolean

Defined in: density/compile.ts:790

Whether every operation in a tree is independent of its y coordinate.

Parameters ​

node ​

DensityFunctionNode

Returns ​

boolean