Density function

Density functions make up mathematical expressions to obtain a number from a position, stored as JSON files within a data pack in the path data/<namespace>/worldgen/density_function.
== JSON Format ==
A density function can be a constant number or an object.
* : Root object.
** : The ID of the density function type.
** Other additional fields depend on the value of , described below.
If the is constant, a shorthand format is:
* : A constant number. Value between −1000000.0 and 1000000.0 (both inclusive).
== Maker functions ==
=== interpolated ===
Interpolates at each block in one cell based on the input density function value of some cells around. The size of each cell is size_horizontal * 4 and size_vertical * 4. Used often in combination with flat_cache.
* : Root object.
** : The ID of the density function type (in this case, interpolated).
** : The input density function. Can be an ID of a density function, or a density function in the form of a JSON object or a constant number.
=== flat_cache ===
Calculate the value per 4*4 column (Value at each block in one column is the same). And it is calculated only once per column, at Y=0. Used often in combination with interpolated.
* : Root object.
** : The ID of the density function type (in this case, flat_cache).
** : The input density function. Can be an ID of a density function, or a density function in the form of a JSON object or a constant number.
=== cache_2d ===
Only computes the input density once per horizonal position.
* : Root object.
** : The ID of the density function type (in this case, cache_2d).
** : The input density function. Can be an ID of a density function, or a density function in the form of a JSON object or a constant number.
=== cache_once ===
If this density function is referenced twice, it is only computed once per block position.
* : Root object.
** : The ID of the density function type (in this case, cache_once).
** : The input density function. Can be an ID of a density function, or a density function in the form of a JSON object or a constant number.
=== cache_all_in_cell ===
Used by the game onto final_density and should not be referenced in data packs.
* : Root object.
** : The ID of the density function type (in this case, cache_all_in_cell).
** : The input density function. Can be an ID of a density function, or a density function in the form of a JSON object or a constant number.
== Functions with one argument ==
=== abs ===
Calculates the absolute value of the input density function.
* : Root object.
** : The ID of the density function type (in this case, abs).
** : The input density function. Can be an ID of a density function, or a density function in the form of a JSON object or a constant number.
=== square ===
Squares the input. (x^2)
* : Root object.
** : The ID of the density function type (in this case, square).
** : The input density function. Can be an ID of a density function, or a density function in the form of a JSON object or a constant number.
=== cube ===
Cubes the input (x^3).
* : Root object.
** : The ID of the density function type (in this case, cube).
** : The input density function. Can be an ID of a density function, or a density function in the form of a JSON object or a constant number.
=== half_negative ===
If the input is negative, returns half of the input. Otherwise returns the input. (x < 0 ? x/2 : x)
* : Root object.
** : The ID of the density function type (in this case, half_negative).
** : The input density function. Can be an ID of a density function, or a density function in the form of a JSON object or a constant number.
=== quarter_negative ===
If the input is negative, returns a quarter of the input. Otherwise returns the input. (x < 0 ? x/4 : x)
* : Root object.
** : The ID of the density function type (in this case, quarter_negative).
** : A density function. Can be an ID of a density function, or a density function in the form of a JSON object or a constant number.
=== squeeze ===
First clamps the input between −1 and 1, then transforms it using x/2 - x*x*x/24.
* : Root object.
** : The ID of the density function type (in this case, squeeze).
** : The input density function. Can be an ID of a density function, or a density function in the form of a JSON object or a constant number.
== Functions with two arguments ==
=== add ===
Adds two density functions together.
* : Root object.
** : The ID of the density function type (in this case, add).
** : A density function. Can be an ID of a density function, or a density function in the form of a JSON object or a constant number.
** : A density function. Can be an ID of a density function, or a density function in the form of a JSON object or a constant number.
=== mul ===
Multiplies two inputs.
* : Root object.
** : The ID of the density function type (in this case, mul).
** : A density function. Can be an ID of a density function, or a density function in the form of a JSON object or a constant number.
** : A density function. Can be an ID of a density function, or a density function in the form of a JSON object or a constant number.
=== min ===
Returns the mininum of two inputs.
* : Root object.
** : The ID of the density function type (in this case, min).
** : A density function. Can be an ID of a density function, or a density function in the form of a JSON object or a constant number.
** : A density function. Can be an ID of a density function, or a density function in the form of a JSON object or a constant number.
=== max ===
Returns the maximum of two inputs.
* : Root object.
** : The ID of the density function type (in this case, max).
** : A density function. Can be an ID of a density function, or a density function in the form of a JSON object or a constant number.
** : A density function. Can be an ID of a density function, or a density function in the form of a JSON object or a constant number.
== Other density functions ==
=== blend_alpha ===
Used in vanilla for smooth transition between legacy and new chunks.
* : Root object.
** : The ID of the density function type (in this case, blend_alpha).
=== blend_offset ===
Used in vanilla for smooth transition between legacy and new chunks.
* : Root object.
** : The ID of the density function type (in this case, blend_offset).
=== blend_density ===
Used in vanilla for smooth transition between legacy and new chunks.
* : Root object.
** : The ID of the density function type (in this case, blend_density).
** : The input density function. Can be an ID of a density function, or a density function in the form of a JSON object or a constant number.
=== beardifier ===
Adds beards for structures (see the terrain_adaptation field in Configured Structure Feature). Its value is added to the final_density in noise setting by the game. Should not be referenced in data packs.
* : Root object.
** : The ID of the density function type (in this case, beardifier).
=== old_blended_noise ===
Samples a legacy noise. 
* : Root object.
** : The ID of the density function type (in this case, old_blended_noise).
** : Value between 0.001 and 1000.0 (both inclusive).
** : Value between 0.001 and 1000.0 (both inclusive).
** : Value between 0.001 and 1000.0 (both inclusive).
** : Value between 0.001 and 1000.0 (both inclusive).
** : Value between 1.0 and 8.0 (both inclusive).
=== noise ===
Samples a noise.
* : Root object.
** : The ID of the density function type (in this case, noise).
** : An ID of a noise.
** : Scales the X and Z before sampling.
** : Scales the Y before sampling.
=== end_islands ===
Samples at current position using a noise algorithm used for end islands. Its minimum value is −0.84375 and its maximum value is 0.5625.
* : Root object.
** : The ID of the density function type (in this case, end_islands).
=== weird_scaled_sampler ===
According to the input value, scales and enhances (or weakens) some regions of the specified noise, and then returns the absolute value.
* : Root object.
** : The ID of the density function type (in this case, weird_scaled_sampler).
** : Can be type_1（The minimum scale is 0.75, and the maximum is 2.0）or type_2（The minimum scale is 0.5, and the maximum is 3.0.
** : An ID of a noise.
** : The input density function. Can be an ID of a density function, or a density function in the form of a JSON object or a constant number.
=== shifted_noise ===
Similar to noise, but first shifts the input coordinates.
* : Root object.
** : The ID of the density function type (in this case, shifted_noise).
** : An ID of a noise.
** : Scales the X and Z before sampling.
** : Scales the Y before sampling.
** : Density function used to offset the X value. Can be an ID of a density function, or a density function in the form of a JSON object or a constant number.
** : Density function used to offset the Y value. Can be an ID of a density function, or a density function in the form of a JSON object or a constant number.
** : Density function used to offset the Z value. Can be an ID of a density function, or a density function in the form of a JSON object or a constant number.
=== range_choice ===
Computes the input value, and depending on that result returns one of two other density functions. Basically an if-then-else statement.
* : Root object.
** : The ID of the density function type (in this case, range_choice).
** : The input density function. Can be an ID of a density function, or a density function in the form of a JSON object or a constant number.
** : The lower bound of the range. Value between −1000000.0 and 1000000.0 (both inclusive).
** : The upper bound of the range. Value between −1000000.0 and 1000000.0 (both inclusive).
** : Density function whose value is returned when the input is inside the range. Can be an ID of a density function, or a density function in the form of a JSON object or a constant number.
** : Density function whose value is returned when the input is outside the range. Can be an ID of a density function, or a density function in the form of a JSON object or a constant number.
=== shift_a ===
Samples a noise at (x/4, 0, z/4), then multiplies it by 4.
* : Root object.
** : The ID of the density function type (in this case, shift_a).
** : An ID of a noise.
=== shift_b ===
Samples a noise at (z/4, x/4, 0), then multiplies it by 4.
* : Root object.
** : The ID of the density function type (in this case, shift_b).
** : An ID of a noise.
=== shift ===
Samples a noise at (x/4, y/4, z/4), then multiplies it by 4.
* : Root object.
** : The ID of the density function type (in this case, shift).
** : An ID of a noise.
=== clamp ===
Clamps the input between two values.
* : Root object.
** : The ID of the density function type (in this case, clamp).
** : The input density function. Can only be a density function in the form of a JSON object or a constant number.
** : The lower bound. Value between −1000000.0 and 1000000.0 (both inclusive).
** : The upper bound. Value between −1000000.0 and 1000000.0 (both inclusive).
=== spline ===
Computes a cubic spline.
* : Root object.
** : The ID of the density function type (in this case, spline).
** : The spline. Can be either a number or an object.
*** : The density function used to get a location on the spline. Can be an ID of a density function, or a density function in the form of a JSON object or a constant number.
*** : (Cannot be empty) List of points of the cubic spline.
**** : The location of this point.
**** : The value of this point. Can be either a number or a spline object.
**** : The slope at this point.
=== constant ===
A constant value.
* : Root object.
** : The ID of the density function type (in this case, constant).
** : A constant value. Value between −1000000.0 and 1000000.0 (both inclusive).
=== y_clamped_gradient ===
Clamps the Y coordinate between from_y and to_y and then linearly maps it to a range.
* : Root object.
** : The ID of the density function type (in this case, y_clamped_gradient).
** : The value to be mapped to from_value. Value between −4064 and 4062 (both inclusive).
** : The value to be mapped to to_value. Value between −4064 and 4062 (both inclusive).
** : The value to map from_y to. Value between −1000000.0 and 1000000.0 (both inclusive).
** : The value to map to_y to. Value between −1000000.0 and 1000000.0 (both inclusive).
== Removed density functions ==
=== slide ===
Removed in 22w12a
* : Root object.
** : The ID of the density function type (in this case, slide).
** : The input density function. Can be an ID of a density function, or a density function in the form of a JSON object or a constant number.
=== The legacy "spline" ===
Removed in 22w11a
* : Root object.
** : The ID of the density function type (in this case, spline).
** : The spline. Can be either a number or an object.
*** : The density function used to get a location on the spline. Can be an ID of a density function, or a density function in the form of a JSON object or a constant number.
*** : (Cannot be empty) List of points of the cubic spline.
**** : The location of this point.
**** : The value of this point. Can be either a number or a spline object.
**** : The slope at this point.
** : The min value of the output. Value between −1000000.0 and 1000000.0 (both inclusive).
** : The max value of the output. Value between −1000000.0 and 1000000.0 (both inclusive).
=== terrain_shaper_spline ===
Removed in 22w11a
Calculate the spline from the noise settings.
* : Root object.
** : The ID of the density function type (in this case, terrain_shaper_spline).
** : Can be offset, factor, orjaggedness.
** : The min value of the output. Value between −1000000.0 and 1000000.0 (both inclusive).
** : The max value of the output. Value between −1000000.0 and 1000000.0 (both inclusive).
** : Can be an ID of a density function, or a density function in the form of a JSON object or a constant number.
** : Can be an ID of a density function, or a density function in the form of a JSON object or a constant number.
** : Can be an ID of a density function, or a density function in the form of a JSON object or a constant number.
== History ==
== External links ==
This article references Guide of density function by Misode and Technical changelog by Misode.
pt:Função de densidade
zh:密度函数