noise v2
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 v2Noise 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(rolls) | int[] | the table a field reads, shuffled with the rolls of the named random rolls (255 rolls). Make it once, at setup. |
noise.at(field, point) | num | the noise of the table field at point (a vec), from -1 to 1. About one rise and one fall per unit. |
noise.octaves(field, point, layers = 4, rough = 0.5) | num | noise in layers, from -1 to 1: layers layers, each twice as fine as the one before and rough times as strong. More layers give a rougher edge to the same broad shape. |
layers and rough may be left out. field 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:
point * 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(field, point: 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 v2
random layout
let field = noise.table(layout)
// the direction a flow runs at p, in radians
fn flow(p: vec) = noise.octaves(field, point: p * 0.001, layers: 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(field, point: 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 v2: smooth noise as a number.
//
// Simplex noise in two dimensions, read from a table the piece makes from its
// own named random: noise.table(layout) shuffles 256 numbers with its 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 rolls, written twice so a lookup never wraps.
// Each call rolls 255 numbers from rolls.
fn table(rolls: random): int[] {
let t = int[]
for i in 0 ..< 256 { t.add(i) }
for k in 0 ..< 255 {
let i = 255 - k
let j = rolls.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 of field at point, from -1 to 1: smooth, with about one rise and one fall per unit,
// so scale point down for broad shapes (point * 0.002 across a plate).
fn at(field: int[], point: vec): num {
let s = (point.x + point.y) * _F2
let i = floor(point.x + s), j = floor(point.y + s)
let u = (i + j) * _G2
let x0 = point.x - i + u, y0 = point.y - j + u
let i1 = x0 > y0 ? 1 : 0
let j1 = 1 - i1
let ii = i & 255, jj = j & 255
let a = _corner(x0, y: y0, g: field[ii + field[jj]] & 7)
let b = _corner(x0 - i1 + _G2, y: y0 - j1 + _G2, g: field[ii + i1 + field[jj + j1]] & 7)
let c = _corner(x0 - 1 + 2 * _G2, y: y0 - 1 + 2 * _G2, g: field[ii + 1 + field[jj + 1]] & 7)
return clamp(70 * (a + b + c), -1, 1)
}
// Noise in layers, from -1 to 1: layers octaves, each twice as fine as the one before
// and rough times as strong. More layers give a rougher edge to the same broad shape.
fn octaves(field: int[], point: vec, layers: int = 4, rough: num = 0.5): num {
var sum = 0
var amp = 1
var total = 0
var q = point
for k in 0 ..< layers {
sum += amp * at(field, point: q)
total += amp
amp *= rough
// 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
}