dymokomi/luce-kernel
Source of release 0.1.1
0.1.1 / src / lib / cellular.lucb
# Cellular noise: Worley's feature points (VEX `wnoise`), MaterialX's cell noise and
# alligator noise (VEX `anoise`). Each unit cell holds one feature point at a hashed
# place; a lookup visits the 27 cells around the point.
## Worley distances (VEX `wnoise`'s outputs): `f1` to the nearest feature point and `f2`
## to the second nearest (Euclidean, not squared), and `seed`, a number in 0..2^24
## naming the nearest point's cell (the same for every position nearest that point).
pub struct Worley:
pub var f1: f32 = 0.0
pub var f2: f32 = 0.0
pub var seed: i32 = 0
## The hash of cell (x, y, z), stream `stream`.
func cell_bits(x: i64, y: i64, z: i64, stream: u32) -> u64:
let mixed = (x *% 0x632BE59BD9B4E019) ^ (y *% 0x2CB92BA72F3D8DD7) ^ (z *% 0x2BC98388FB8FAC03)
return random_bits(mixed, stream)
## A number in `[0, 1)` from the top 24 bits of `bits`.
func unit_of(bits: u64) -> f32:
return (f32)(bits >> 40) * (1.0 / 16777216.0)
## The feature point of cell (x, y, z): three 21-bit fractions of one hash.
func feature(x: i64, y: i64, z: i64) -> f32[4]:
let bits = cell_bits(x, y, z, 21)
let scale: f32 = 1.0 / 2097152.0
return [(f32)x + (f32)(bits >> 43) * scale, (f32)y + (f32)((bits >> 22) & 0x1FFFFF) * scale, (f32)z + (f32)((bits >> 1) & 0x1FFFFF) * scale, 0.0]
## The two nearest feature points' distances from `p` (VEX `wnoise(pos, seed, f1, f2)`;
## departure: distances, not squared distances). Always `f1 <= f2`.
pub func wnoise(p: f32[4]) -> Worley:
let cx = (i64)math32.floor(p[0])
let cy = (i64)math32.floor(p[1])
let cz = (i64)math32.floor(p[2])
var f1: f32 = 1.0e9
var f2: f32 = 1.0e9
var nearest: i64 = 0
for i in 0..<27:
let x = cx + (i64)(i % 3) - 1
let y = cy + (i64)((i // 3) % 3) - 1
let z = cz + (i64)(i // 9) - 1
let d = distance([p[0], p[1], p[2], 0.0], feature(x, y, z))
if d < f1:
f2 = f1
f1 = d
nearest = (i64)i
elif d < f2:
f2 = d
let seed = (i32)(cell_bits(cx + nearest % 3 - 1, cy + (nearest // 3) % 3 - 1, cz + nearest // 9 - 1, 23) >> 40)
return Worley(f1 = f1, f2 = f2, seed = seed)
## A random value in `[0, 1)` constant over each unit cell (MaterialX and VEX
## `mx_cellnoise`).
pub func mx_cellnoise(p: f32[4]) -> f32:
return unit_of(cell_bits((i64)math32.floor(p[0]), (i64)math32.floor(p[1]), (i64)math32.floor(p[2]), 24))
## Alligator noise in `0..1`: each cell's feature point carries a bump of random height
## falling to zero one unit away; the value is the tallest bump less the second, which
## leaves plates with cracks between them.
pub func alligator(p: f32[4]) -> f32:
let cx = (i64)math32.floor(p[0])
let cy = (i64)math32.floor(p[1])
let cz = (i64)math32.floor(p[2])
var first: f32 = 0.0
var second: f32 = 0.0
for i in 0..<27:
let x = cx + (i64)(i % 3) - 1
let y = cy + (i64)((i // 3) % 3) - 1
let z = cz + (i64)(i // 9) - 1
let d = distance([p[0], p[1], p[2], 0.0], feature(x, y, z))
let fall = math32.max(1.0 - d, 0.0)
let bump = unit_of(cell_bits(x, y, z, 25)) * fall * fall * fall
if bump > first:
second = first
first = bump
elif bump > second:
second = bump
return math32.min(first - second, 1.0)
## Fractal alligator noise of `turbulence` octaves (VEX `anoise(pos, turbulence, rough,
## atten)`): octave `i` at `p * 2^i` weighted `rough^i`, the weighted mean raised to
## `atten`.
pub func anoise(p: f32[4], turbulence: i32, rough: f32, atten: f32) -> f32:
var total: f32 = 0.0
var weight: f32 = 0.0
var amplitude: f32 = 1.0
var frequency: f32 = 1.0
let octaves = turbulence if turbulence > 0 else 1
for _ in 0..<octaves:
total += amplitude * alligator(p * frequency)
weight += amplitude
amplitude *= rough
frequency *= 2.0
return math32.pow(math32.clamp(total / weight, 0.0, 1.0), atten)