Random
Hash Functions
PCG Hash
uint pcg_hash(uint input)
{
uint state = input * 747796405u + 2891336453u;
uint word = ((state >> ((state >> 28u) + 4u)) ^ state) * 277803737u;
return (word >> 22u) ^ word;
}
Noise
Value Noise
Perlin Noise
Simplex Noise
A simple way to generate terrain is to layer different noise functions together. This is a technique called Fractal Brownian Motion (FBM) popularized by Inigo Quilez. I also like the interactive explanation by Patricio Gonzalez Vivo in his The Book of Shaders.
/**
* Generates Fractal Brownian Motion noise by layering multiple octaves of simplex noise
* @param {number} x - X coordinate to sample
* @param {number} y - Y coordinate to sample
* @param {number} scale - Base frequency scale. Higher values = more zoomed out noise
* @param {number} octaves - Number of noise layers to combine
* @param {number} persistance - How quickly amplitude decreases per octave (0-1)
* @param {number} lacunarity - How quickly frequency increases per octave (typically 2)
* @returns {number} Combined noise value between 0 and 1
*/
function noiseFBM(x, y, scale = 1.0, octaves = 4, persistance = 0.5, lacunarity = 2.0) {
let amplitude = 1.0
let frequency = scale
let height = 0.0
let maxAmplitude = 0.0
for (let i = 0; i < octaves; i++) {
height += simplex2(x * frequency, y * frequency) * amplitude
maxAmplitude += amplitude
amplitude *= persistance
frequency *= lacunarity
}
return height / maxAmplitude
}
https://www.reedbeta.com/blog/hash-functions-for-gpu-rendering/
Resources
https://www.youtube.com/watch?v=DxUY42r_6Cg
https://www.reedbeta.com/blog/hash-functions-for-gpu-rendering/
https://thebookofshaders.com/10/