noise v1
Smooth noise as a number, in two dimensions. Use it to bend a line, turn a flow, or vary a value across the plate. It is only numbers, so it works on both targets.
use noise v1Noise reads a table, and the piece makes the table from one of its own named randoms. The library rolls nothing by itself, so the same hash always gives the same field, and a change to the library cannot reshuffle a piece.
Names
| call | gives | |
|---|---|---|
noise.table(r) | int[] | the table a field reads, shuffled with rolls from the named random r (255 rolls). Make it once, at setup. |
noise.at(t, p) | num | the noise of table t at the point p (a vec), from -1 to 1. About one rise and one fall per unit. |
noise.octaves(t, p, count: 4, gain: 0.5) | num | noise in layers, from -1 to 1: count layers, each twice as fine as the one before and gain times as strong. More layers give a rougher edge to the same broad shape. |
count and gain may be left out. t is any table noise.table made.
Scale
noise.at changes about once per unit, so scale the point down. Across a 1920 x 2400 plate:
p * k with k | gives |
|---|---|
| 0.0005 to 0.001 | one or two broad swells over the plate |
| 0.002 to 0.005 | a field of medium shapes |
| 0.01 and up | fine grain |
A second field from the same table: read it somewhere else, noise.at(t, p * 0.002 + vec(100, 0)). A field
that does not follow the first at all: a second table from another named random.
Example
plate(1920, 2400)
use noise v1
random layout
let field = noise.table(layout)
// the direction a flow runs at p, in radians
fn flow(p: vec) = noise.octaves(field, p * 0.001, count: 3) * TAU
background(#f3efe6)
for i in 0 ..< 400 {
let p = vec(layout.float(0 ..< 1920), layout.float(0 ..< 2400))
line(p, p + polar(flow(p), 40))
}
stroke(#1b1b1f, 2)Notes
- Make the table at setup. A frame may read it (and call
noise.at), but not make it from a setup named random, since that would roll from it every frame. noise.atstays a little inside -1 and 1 (about 0.95 at most). Scale and shift it to the range wanted:0.5 + 0.5 * noise.at(t, p)runs from about 0 to 1.- Simplex noise on a skewed triangular grid: no square grid shows through.
- What it costs: about 460 bytes of shipped JavaScript for the table,
atandoctavestogether, less for fewer. Only what the piece calls is shipped.
The whole library, 64 lines
// noise v1: smooth noise as a number.
//
// Simplex noise in two dimensions, read from a table the piece makes from its
// own named random: noise.table(r) shuffles 256 numbers with r's rolls, so the same
// hash always gives the same field and nothing here rolls on its own.
// the corners of the skewed grid: (sqrt(3) - 1) / 2 and (3 - sqrt(3)) / 6
let _F2 = 0.3660254037844386
let _G2 = 0.21132486540518713
// eight directions a corner can lean
let _GX = [1, -1, 1, -1, 1, -1, 0, 0]
let _GY = [1, 1, -1, -1, 0, 0, 1, -1]
// The table a field reads: 0 to 255 shuffled by r, written twice so a lookup never wraps.
// Each call rolls 255 numbers from r.
fn table(r: random): int[] {
let t = int[]
for i in 0 ..< 256 { t.add(i) }
for k in 0 ..< 255 {
let i = 255 - k
let j = r.int(0 ..= i)
let v = t[i]
t[i] = t[j]
t[j] = v
}
for i in 0 ..< 256 { t.add(t[i]) }
return t
}
// what one corner adds at offset (x, y) from it, leaning direction g
fn _corner(x: num, y: num, g: int): num {
let f = 0.5 - x * x - y * y
if f <= 0 { return 0 }
let f2 = f * f
return f2 * f2 * (_GX[g] * x + _GY[g] * y)
}
// The noise at p, from -1 to 1: smooth, with about one rise and one fall per unit,
// so scale p down for broad shapes (p * 0.002 across a plate).
fn at(t: int[], p: vec): num {
let s = (p.x + p.y) * _F2
let i = floor(p.x + s), j = floor(p.y + s)
let u = (i + j) * _G2
let x0 = p.x - i + u, y0 = p.y - j + u
let i1 = x0 > y0 ? 1 : 0
let j1 = 1 - i1
let ii = i & 255, jj = j & 255
let a = _corner(x0, y0, t[ii + t[jj]] & 7)
let b = _corner(x0 - i1 + _G2, y0 - j1 + _G2, t[ii + i1 + t[jj + j1]] & 7)
let c = _corner(x0 - 1 + 2 * _G2, y0 - 1 + 2 * _G2, t[ii + 1 + t[jj + 1]] & 7)
return clamp(70 * (a + b + c), -1, 1)
}
// Noise in layers, from -1 to 1: count octaves, each twice as fine as the one before
// and gain times as strong. More octaves give a rougher edge to the same broad shape.
fn octaves(t: int[], p: vec, count: int = 4, gain: num = 0.5): num {
var sum = 0
var amp = 1
var total = 0
var q = p
for k in 0 ..< count {
sum += amp * at(t, q)
total += amp
amp *= gain
// each layer finer, and moved off the last so their zeros do not line up
q = q * 2 + vec(17.3, -9.1)
}
return sum / total
}