Skip to content

Commit 1b2780c

Browse files
committed
fix: correct 2D simplex noise gradient table to match Gustavson reference
The 2D noise was using a custom 8-vector grad2 table with permMod8, but the 70.0 scaling factor is calibrated for 12 grad3 vectors. Switched to canonical approach: reuse grad3 with permMod12 (taking only x,y components). Also removed misleading CRT scanline comment in fx.ts.
1 parent fe24129 commit 1b2780c

2 files changed

Lines changed: 11 additions & 11 deletions

File tree

src/engine/fx.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,7 @@ function renderCrt(
194194
const strength = clamp(layer.fxStrength, 0, 1)
195195
if (strength <= 0.001) return
196196

197-
// --- Scanlines with brightness-dependent visibility ---
198-
// Gaussian-inspired scanline profile: darker lines between rows
197+
// --- Scanlines ---
199198
const scanlineSpacing = 3
200199
const scanlineAlpha = strength * 0.18
201200
ctx.fillStyle = `rgba(0,0,0,${scanlineAlpha})`

src/engine/noise.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ const G2 = (3 - Math.sqrt(3)) / 6
33
const F3 = 1 / 3
44
const G3 = 1 / 6
55

6-
const grad2 = new Float64Array([1,1, -1,1, 1,-1, -1,-1, 1,0, -1,0, 0,1, 0,-1])
6+
// 12 gradient vectors for 3D simplex noise (Gustavson's Java reference)
7+
// 2D noise reuses these, taking only x,y components via permMod12
78
const grad3 = new Float64Array([
89
1,1,0, -1,1,0, 1,-1,0, -1,-1,0,
910
1,0,1, -1,0,1, 1,0,-1, -1,0,-1,
@@ -32,10 +33,10 @@ const p = new Uint8Array([
3233

3334
// Double the permutation table for overflow-free access
3435
const perm = new Uint8Array(512)
35-
const permMod8 = new Uint8Array(512)
36+
const permMod12 = new Uint8Array(512)
3637
for (let i = 0; i < 512; i++) {
3738
perm[i] = p[i & 255]
38-
permMod8[i] = perm[i] % 8
39+
permMod12[i] = perm[i] % 12
3940
}
4041

4142
export function simplex2(x: number, y: number): number {
@@ -64,23 +65,23 @@ export function simplex2(x: number, y: number): number {
6465

6566
let t0 = 0.5 - x0 * x0 - y0 * y0
6667
if (t0 >= 0) {
67-
const gi0 = permMod8[ii + perm[jj]] * 2
68+
const gi0 = permMod12[ii + perm[jj]] * 3
6869
t0 *= t0
69-
n0 = t0 * t0 * (grad2[gi0] * x0 + grad2[gi0 + 1] * y0)
70+
n0 = t0 * t0 * (grad3[gi0] * x0 + grad3[gi0 + 1] * y0)
7071
}
7172

7273
let t1 = 0.5 - x1 * x1 - y1 * y1
7374
if (t1 >= 0) {
74-
const gi1 = permMod8[ii + i1 + perm[jj + j1]] * 2
75+
const gi1 = permMod12[ii + i1 + perm[jj + j1]] * 3
7576
t1 *= t1
76-
n1 = t1 * t1 * (grad2[gi1] * x1 + grad2[gi1 + 1] * y1)
77+
n1 = t1 * t1 * (grad3[gi1] * x1 + grad3[gi1 + 1] * y1)
7778
}
7879

7980
let t2 = 0.5 - x2 * x2 - y2 * y2
8081
if (t2 >= 0) {
81-
const gi2 = permMod8[ii + 1 + perm[jj + 1]] * 2
82+
const gi2 = permMod12[ii + 1 + perm[jj + 1]] * 3
8283
t2 *= t2
83-
n2 = t2 * t2 * (grad2[gi2] * x2 + grad2[gi2 + 1] * y2)
84+
n2 = t2 * t2 * (grad3[gi2] * x2 + grad3[gi2 + 1] * y2)
8485
}
8586

8687
return 70.0 * (n0 + n1 + n2)

0 commit comments

Comments
 (0)