| 1 | /** |
| 2 | * Bake seamless multi-octave value-noise layers into water ground PNGs. |
| 3 | * R/G/B = three independent tiling fields for sliding additive caustics. |
| 4 | */ |
| 5 | import { join } from "node:path"; |
| 6 | import { makeRgba, writeRgbaPng } from "../../server/src/assets/placeholderPng.js"; |
| 7 | |
| 8 | const SIZE = 64; |
| 9 | const root = join(import.meta.dirname, "..", "assets"); |
| 10 | |
| 11 | function wrapInt (v, period) |
| 12 | { |
| 13 | const m = ((v % period) + period) % period; |
| 14 | return m | 0; |
| 15 | } |
| 16 | |
| 17 | function hash2 (ix, iy, seed) |
| 18 | { |
| 19 | const n = Math.sin(ix * 127.1 + iy * 311.7 + seed * 74.7) * 43758.5453123; |
| 20 | return n - Math.floor(n); |
| 21 | } |
| 22 | |
| 23 | function valueNoise (x, y, period, seed) |
| 24 | { |
| 25 | const x0 = Math.floor(x); |
| 26 | const y0 = Math.floor(y); |
| 27 | const fx = x - x0; |
| 28 | const fy = y - y0; |
| 29 | const ux = fx * fx * (3 - 2 * fx); |
| 30 | const uy = fy * fy * (3 - 2 * fy); |
| 31 | const a = hash2(wrapInt(x0, period), wrapInt(y0, period), seed); |
| 32 | const b = hash2(wrapInt(x0 + 1, period), wrapInt(y0, period), seed); |
| 33 | const c = hash2(wrapInt(x0, period), wrapInt(y0 + 1, period), seed); |
| 34 | const d = hash2(wrapInt(x0 + 1, period), wrapInt(y0 + 1, period), seed); |
| 35 | return a + (b - a) * ux + (c - a) * uy + (a - b - c + d) * ux * uy; |
| 36 | } |
| 37 | |
| 38 | function fbm (x, y, period, seed, octaves = 4) |
| 39 | { |
| 40 | let amp = 0.5; |
| 41 | let freq = 1; |
| 42 | let sum = 0; |
| 43 | let norm = 0; |
| 44 | for (let i = 0; i < octaves; i++) |
| 45 | { |
| 46 | const p = Math.max(2, Math.round(period * freq)); |
| 47 | sum += valueNoise(x * freq, y * freq, p, seed + i * 19.1) * amp; |
| 48 | norm += amp; |
| 49 | amp *= 0.5; |
| 50 | freq *= 2; |
| 51 | } |
| 52 | return sum / norm; |
| 53 | } |
| 54 | |
| 55 | function bakeLayer (seed, cells) |
| 56 | { |
| 57 | const values = new Float32Array(SIZE * SIZE); |
| 58 | let min = Infinity; |
| 59 | let max = -Infinity; |
| 60 | for (let y = 0; y < SIZE; y++) |
| 61 | { |
| 62 | for (let x = 0; x < SIZE; x++) |
| 63 | { |
| 64 | const v = fbm((x / SIZE) * cells, (y / SIZE) * cells, cells, seed); |
| 65 | values[y * SIZE + x] = v; |
| 66 | if (v < min) |
| 67 | { min = v; } |
| 68 | if (v > max) |
| 69 | { max = v; } |
| 70 | } |
| 71 | } |
| 72 | const span = Math.max(1e-6, max - min); |
| 73 | const out = new Uint8Array(SIZE * SIZE); |
| 74 | for (let i = 0; i < values.length; i++) |
| 75 | { out[i] = Math.round(((values[i] - min) / span) * 255); } |
| 76 | return out; |
| 77 | } |
| 78 | |
| 79 | function writePackedNoise (file, seeds) |
| 80 | { |
| 81 | const r = bakeLayer(seeds[0], 4); |
| 82 | const g = bakeLayer(seeds[1], 5); |
| 83 | const b = bakeLayer(seeds[2], 6); |
| 84 | const image = makeRgba(SIZE, SIZE, [0, 0, 0, 255]); |
| 85 | for (let i = 0; i < SIZE * SIZE; i++) |
| 86 | { |
| 87 | const o = i * 4; |
| 88 | image.pixels[o] = r[i]; |
| 89 | image.pixels[o + 1] = g[i]; |
| 90 | image.pixels[o + 2] = b[i]; |
| 91 | image.pixels[o + 3] = 255; |
| 92 | } |
| 93 | writeRgbaPng(file, SIZE, SIZE, image.pixels); |
| 94 | } |
| 95 | |
| 96 | writePackedNoise(join(root, "grounds", "water", "water.png"), [11.3, 27.9, 41.2]); |
| 97 | writePackedNoise(join(root, "grounds", "deep-water", "deep-water.png"), [53.7, 67.1, 89.4]); |
| 98 | |
| 99 | console.log(`Wrote ${SIZE}x${SIZE} RGB tiling Perlin packs for water + deep-water.`); |