| 1 | /** |
| 2 | * Bake seamless water noise packs: |
| 3 | * R/G = soft Perlin body layers |
| 4 | * B/A = ridged “caustic vein” sprites for crossed glitter overlays |
| 5 | */ |
| 6 | import { join } from "node:path"; |
| 7 | import { makeRgba, writeRgbaPng } from "../../server/src/assets/placeholderPng.js"; |
| 8 | |
| 9 | const SIZE = 64; |
| 10 | const root = join(import.meta.dirname, "..", "assets"); |
| 11 | |
| 12 | function wrapInt (v, period) |
| 13 | { |
| 14 | const m = ((v % period) + period) % period; |
| 15 | return m | 0; |
| 16 | } |
| 17 | |
| 18 | function hash2 (ix, iy, seed) |
| 19 | { |
| 20 | const n = Math.sin(ix * 127.1 + iy * 311.7 + seed * 74.7) * 43758.5453123; |
| 21 | return n - Math.floor(n); |
| 22 | } |
| 23 | |
| 24 | function valueNoise (x, y, period, seed) |
| 25 | { |
| 26 | const x0 = Math.floor(x); |
| 27 | const y0 = Math.floor(y); |
| 28 | const fx = x - x0; |
| 29 | const fy = y - y0; |
| 30 | const ux = fx * fx * (3 - 2 * fx); |
| 31 | const uy = fy * fy * (3 - 2 * fy); |
| 32 | const a = hash2(wrapInt(x0, period), wrapInt(y0, period), seed); |
| 33 | const b = hash2(wrapInt(x0 + 1, period), wrapInt(y0, period), seed); |
| 34 | const c = hash2(wrapInt(x0, period), wrapInt(y0 + 1, period), seed); |
| 35 | const d = hash2(wrapInt(x0 + 1, period), wrapInt(y0 + 1, period), seed); |
| 36 | return a + (b - a) * ux + (c - a) * uy + (a - b - c + d) * ux * uy; |
| 37 | } |
| 38 | |
| 39 | function fbm (x, y, period, seed, octaves = 4) |
| 40 | { |
| 41 | let amp = 0.5; |
| 42 | let freq = 1; |
| 43 | let sum = 0; |
| 44 | let norm = 0; |
| 45 | for (let i = 0; i < octaves; i++) |
| 46 | { |
| 47 | const p = Math.max(2, Math.round(period * freq)); |
| 48 | sum += valueNoise(x * freq, y * freq, p, seed + i * 19.1) * amp; |
| 49 | norm += amp; |
| 50 | amp *= 0.5; |
| 51 | freq *= 2; |
| 52 | } |
| 53 | return sum / norm; |
| 54 | } |
| 55 | |
| 56 | function normalizeLayer (values) |
| 57 | { |
| 58 | let min = Infinity; |
| 59 | let max = -Infinity; |
| 60 | for (let i = 0; i < values.length; i++) |
| 61 | { |
| 62 | if (values[i] < min) |
| 63 | { min = values[i]; } |
| 64 | if (values[i] > max) |
| 65 | { max = values[i]; } |
| 66 | } |
| 67 | const span = Math.max(1e-6, max - min); |
| 68 | const out = new Uint8Array(values.length); |
| 69 | for (let i = 0; i < values.length; i++) |
| 70 | { out[i] = Math.round(((values[i] - min) / span) * 255); } |
| 71 | return out; |
| 72 | } |
| 73 | |
| 74 | function bakeSoft (seed, cells) |
| 75 | { |
| 76 | const values = new Float32Array(SIZE * SIZE); |
| 77 | for (let y = 0; y < SIZE; y++) |
| 78 | { |
| 79 | for (let x = 0; x < SIZE; x++) |
| 80 | { values[y * SIZE + x] = fbm((x / SIZE) * cells, (y / SIZE) * cells, cells, seed); } |
| 81 | } |
| 82 | return normalizeLayer(values); |
| 83 | } |
| 84 | |
| 85 | /** Ridged / vein caustic: bright thin networks on dark. */ |
| 86 | function bakeCaustic (seed, cells) |
| 87 | { |
| 88 | const values = new Float32Array(SIZE * SIZE); |
| 89 | for (let y = 0; y < SIZE; y++) |
| 90 | { |
| 91 | for (let x = 0; x < SIZE; x++) |
| 92 | { |
| 93 | const nx = (x / SIZE) * cells; |
| 94 | const ny = (y / SIZE) * cells; |
| 95 | const n1 = fbm(nx, ny, cells, seed, 5); |
| 96 | const n2 = fbm(nx + 3.1, ny - 1.7, cells, seed + 17.3, 4); |
| 97 | const ridge = 1 - Math.abs(n1 * 2 - 1); |
| 98 | const ridge2 = 1 - Math.abs(n2 * 2 - 1); |
| 99 | const veins = Math.pow(ridge, 3.2) * 0.75 + Math.pow(ridge * ridge2, 2.4) * 0.55; |
| 100 | values[y * SIZE + x] = veins; |
| 101 | } |
| 102 | } |
| 103 | return normalizeLayer(values); |
| 104 | } |
| 105 | |
| 106 | function writePackedNoise (file, softSeeds, causticSeeds) |
| 107 | { |
| 108 | const r = bakeSoft(softSeeds[0], 4); |
| 109 | const g = bakeSoft(softSeeds[1], 5); |
| 110 | const b = bakeCaustic(causticSeeds[0], 7); |
| 111 | const a = bakeCaustic(causticSeeds[1], 8); |
| 112 | const image = makeRgba(SIZE, SIZE, [0, 0, 0, 255]); |
| 113 | for (let i = 0; i < SIZE * SIZE; i++) |
| 114 | { |
| 115 | const o = i * 4; |
| 116 | image.pixels[o] = r[i]; |
| 117 | image.pixels[o + 1] = g[i]; |
| 118 | image.pixels[o + 2] = b[i]; |
| 119 | image.pixels[o + 3] = a[i]; |
| 120 | } |
| 121 | writeRgbaPng(file, SIZE, SIZE, image.pixels); |
| 122 | } |
| 123 | |
| 124 | writePackedNoise( |
| 125 | join(root, "grounds", "water", "water.png"), |
| 126 | [11.3, 27.9], |
| 127 | [101.4, 133.8] |
| 128 | ); |
| 129 | writePackedNoise( |
| 130 | join(root, "grounds", "deep-water", "deep-water.png"), |
| 131 | [53.7, 67.1], |
| 132 | [157.2, 191.6] |
| 133 | ); |
| 134 | |
| 135 | console.log(`Wrote ${SIZE}x${SIZE} RGBA soft+caustic packs for water + deep-water.`); |