BlueGit

timfa questward

Files

questward / client / scripts / write-fluid-preview-icons.mjs

write-fluid-preview-icons.mjs

write-fluid-preview-icons.mjs 6.9 KiB Download Raw
1/**
2 * Bake 16×16 editor palette icons for procedural fluids.
3 * Renders a 3×3 tile neighborhood and crops the center tile so noise is continuous.
4 *
5 * node client/scripts/write-fluid-preview-icons.mjs
6 */
7import { join } from "node:path";
8import { makeRgba, writeRgbaPng } from "../../server/src/assets/placeholderPng.js";
9import { DEFAULT_WATER_DEBUG, DEFAULT_LAVA_DEBUG } from "../src/play/waterDebug.js";
10
11const TILE = 16;
12const GRID = 3;
13const BAKE = TILE * GRID;
14const TIME = 2.4;
15const root = join(import.meta.dirname, "..", "assets", "grounds");
16
17function clamp (v, lo, hi)
18{
19 return Math.min(hi, Math.max(lo, v));
20}
21
22function mix (a, b, t)
23{
24 return a + (b - a) * t;
25}
26
27function smoothstep (edge0, edge1, x)
28{
29 const t = clamp((x - edge0) / Math.max(1e-6, edge1 - edge0), 0, 1);
30 return t * t * (3 - 2 * t);
31}
32
33/** Compact value-noise FBM (preview-only stand-in for simplex). */
34function hash3 (x, y, z)
35{
36 const n = Math.sin(x * 127.1 + y * 311.7 + z * 74.7) * 43758.5453123;
37 return n - Math.floor(n);
38}
39
40function valueNoise3 (x, y, z)
41{
42 const x0 = Math.floor(x);
43 const y0 = Math.floor(y);
44 const z0 = Math.floor(z);
45 const fx = x - x0;
46 const fy = y - y0;
47 const fz = z - z0;
48 const ux = fx * fx * (3 - 2 * fx);
49 const uy = fy * fy * (3 - 2 * fy);
50 const uz = fz * fz * (3 - 2 * fz);
51 const n000 = hash3(x0, y0, z0);
52 const n100 = hash3(x0 + 1, y0, z0);
53 const n010 = hash3(x0, y0 + 1, z0);
54 const n110 = hash3(x0 + 1, y0 + 1, z0);
55 const n001 = hash3(x0, y0, z0 + 1);
56 const n101 = hash3(x0 + 1, y0, z0 + 1);
57 const n011 = hash3(x0, y0 + 1, z0 + 1);
58 const n111 = hash3(x0 + 1, y0 + 1, z0 + 1);
59 const nx00 = mix(n000, n100, ux);
60 const nx10 = mix(n010, n110, ux);
61 const nx01 = mix(n001, n101, ux);
62 const nx11 = mix(n011, n111, ux);
63 const nxy0 = mix(nx00, nx10, uy);
64 const nxy1 = mix(nx01, nx11, uy);
65 return mix(nxy0, nxy1, uz) * 2 - 1;
66}
67
68function fbm3 (x, y, z)
69{
70 let sum = 0;
71 let amp = 0.5;
72 let px = x;
73 let py = y;
74 let pz = z;
75 for (let i = 0; i < 4; i++)
76 {
77 sum += amp * valueNoise3(px, py, pz);
78 px *= 2;
79 py *= 2;
80 pz *= 2;
81 amp *= 0.5;
82 }
83 return sum;
84}
85
86function sampleHeight (worldX, worldY, p, timeSec)
87{
88 const flowX = timeSec * p.flowX;
89 const flowY = timeSec * p.flowY;
90 const baseX = worldX * p.waveScale + flowX;
91 const baseY = worldY * p.waveScale + flowY;
92 const baseZ = timeSec * p.flowZ;
93 const eps = Math.max(p.normalEps, 0.001);
94 const nC = fbm3(baseX, baseY, baseZ);
95 const nL = fbm3(baseX - eps, baseY, baseZ);
96 const nR = fbm3(baseX + eps, baseY, baseZ);
97 const nD = fbm3(baseX, baseY - eps, baseZ);
98 const nU = fbm3(baseX, baseY + eps, baseZ);
99 const h = nC * p.fbmAmp;
100 const ndotl = clamp(
101 (-(nL - nR) * p.lightDirX + -(nD - nU) * p.lightDirY + p.normalZ * p.lightDirZ)
102 / Math.hypot(nL - nR, nD - nU, p.normalZ),
103 0,
104 1
105 );
106 const wave = clamp(h * p.waveGain + 0.5, 0, 1);
107 const crest = smoothstep(p.crestLo, Math.max(p.crestHi, p.crestLo + 0.01), wave) ** Math.max(p.crestPow, 0.01);
108 let glitter = (ndotl ** Math.max(p.glitterPow, 0.01)) * crest;
109 glitter = Math.max(glitter, (crest ** 2.5) * p.crestGlitter);
110 return { wave, glitter, ndotl };
111}
112
113function rgbBytes (r, g, b)
114{
115 return [
116 Math.round(clamp(r, 0, 1) * 255),
117 Math.round(clamp(g, 0, 1) * 255),
118 Math.round(clamp(b, 0, 1) * 255),
119 255
120 ];
121}
122
123function paintWaterPixel (worldX, worldY, deepAmt, p)
124{
125 const { wave, glitter, ndotl } = sampleHeight(worldX, worldY, p, TIME);
126 const darkW = clamp(p.deepDark, 0, 1);
127 const deepCol = [
128 mix(0.12, mix(0.05, 0.015, darkW), deepAmt),
129 mix(0.34, mix(0.16, 0.055, darkW), deepAmt),
130 mix(0.54, mix(0.34, 0.14, darkW), deepAmt)
131 ];
132 const midCol = [
133 mix(0.24, mix(0.10, 0.04, darkW), deepAmt),
134 mix(0.52, mix(0.30, 0.14, darkW), deepAmt),
135 mix(0.70, mix(0.50, 0.28, darkW), deepAmt)
136 ];
137 const litCol = [
138 mix(0.52, mix(0.26, 0.12, darkW), deepAmt),
139 mix(0.78, mix(0.46, 0.28, darkW), deepAmt),
140 mix(0.90, mix(0.66, 0.46, darkW), deepAmt)
141 ];
142 const sparkCol = [
143 mix(0.98, mix(0.72, 0.48, darkW), deepAmt),
144 mix(1.0, mix(0.88, 0.68, darkW), deepAmt),
145 mix(1.0, mix(0.98, 0.86, darkW), deepAmt)
146 ];
147 const body = smoothstep(p.bodyLo, Math.max(p.bodyHi, p.bodyLo + 0.01), wave);
148 const litAmt = smoothstep(p.litLo, Math.max(p.litHi, p.litLo + 0.01), wave) * p.litMix;
149 let r = mix(deepCol[0], midCol[0], body);
150 let g = mix(deepCol[1], midCol[1], body);
151 let b = mix(deepCol[2], midCol[2], body);
152 r = mix(r, litCol[0], litAmt);
153 g = mix(g, litCol[1], litAmt);
154 b = mix(b, litCol[2], litAmt);
155 const shade = mix(p.shadeLo, p.shadeHi, ndotl);
156 r *= shade;
157 g *= shade;
158 b *= shade;
159 const spark = Math.min(glitter, p.glitterCap) * p.glitterMix;
160 r = mix(r, sparkCol[0], spark);
161 g = mix(g, sparkCol[1], spark);
162 b = mix(b, sparkCol[2], spark);
163 return rgbBytes(r, g, b);
164}
165
166function paintLavaPixel (worldX, worldY, p)
167{
168 const { wave, glitter, ndotl } = sampleHeight(worldX, worldY, p, TIME);
169 let heat = smoothstep(p.bodyLo, Math.max(p.bodyHi, p.bodyLo + 0.01), wave);
170 heat = mix(heat, 1, smoothstep(p.litLo, Math.max(p.litHi, p.litLo + 0.01), wave) * p.litMix * 0.35);
171 heat = clamp(heat + Math.min(glitter, p.glitterCap) * p.glitterMix * 0.45, 0, 1);
172 let r = mix(0.16, 0.78, smoothstep(0, 0.38, heat));
173 let g = mix(0.015, 0.08, smoothstep(0, 0.38, heat));
174 let b = mix(0.01, 0.02, smoothstep(0, 0.38, heat));
175 r = mix(r, 0.98, smoothstep(0.28, 0.68, heat));
176 g = mix(g, 0.38, smoothstep(0.28, 0.68, heat));
177 b = mix(b, 0.04, smoothstep(0.28, 0.68, heat));
178 const yellowAmt = smoothstep(0.55, 0.98, heat);
179 r = mix(r, 1.0, yellowAmt);
180 g = mix(g, 0.9, yellowAmt);
181 b = mix(b, 0.28, yellowAmt);
182 const shade = mix(p.shadeLo, p.shadeHi, ndotl);
183 return rgbBytes(r * shade, g * shade, b * shade);
184}
185
186function bakeCenterTile (paintPixel)
187{
188 const full = makeRgba(BAKE, BAKE, [0, 0, 0, 255]);
189 const origin = 1;
190 for (let py = 0; py < BAKE; py++)
191 {
192 for (let px = 0; px < BAKE; px++)
193 {
194 const worldX = origin + px / TILE;
195 const worldY = origin + py / TILE;
196 full.pixels.set(paintPixel(worldX, worldY), (py * BAKE + px) * 4);
197 }
198 }
199 const out = makeRgba(TILE, TILE, [0, 0, 0, 255]);
200 for (let y = 0; y < TILE; y++)
201 {
202 for (let x = 0; x < TILE; x++)
203 {
204 const sx = TILE + x;
205 const sy = TILE + y;
206 const src = (sy * BAKE + sx) * 4;
207 out.pixels.set(full.pixels.subarray(src, src + 4), (y * TILE + x) * 4);
208 }
209 }
210 return out;
211}
212
213const waterP = { ...DEFAULT_WATER_DEBUG };
214const lavaP = { ...DEFAULT_LAVA_DEBUG };
215
216const waterIcon = bakeCenterTile((x, y) => paintWaterPixel(x, y, 0, waterP));
217writeRgbaPng(join(root, "water", "water-preview.png"), TILE, TILE, waterIcon.pixels);
218
219const deepIcon = bakeCenterTile((x, y) => paintWaterPixel(x, y, Math.pow(1, waterP.deepAbrupt), waterP));
220writeRgbaPng(join(root, "deep-water", "deep-water-preview.png"), TILE, TILE, deepIcon.pixels);
221
222const lavaIcon = bakeCenterTile((x, y) => paintLavaPixel(x, y, lavaP));
223writeRgbaPng(join(root, "lava", "lava-preview.png"), TILE, TILE, lavaIcon.pixels);
224
225console.log("Wrote water / deep-water / lava preview icons (3×3 center crop).");