BlueGit

timfa questward

Files

questward / client / src / play / waterDebug.js

waterDebug.js

waterDebug.js 10.7 KiB Download Raw
1/**
2 * Temporary always-on fluid shader debug panel (water + lava tabs).
3 * Export JSON when you land on values you like.
4 */
5
6export const DEFAULT_WATER_DEBUG = Object.freeze({
7 flowX: 0.17,
8 flowY: 0.05,
9 flowZ: 0.91,
10 waveScale: 2.07,
11 fbmAmp: 0.15,
12 texAmp: 0.43,
13 distort: 0.09,
14 normalZ: 0.88,
15 waveGain: 0.62,
16 crestLo: 0.54,
17 crestHi: 0.67,
18 crestPow: 2.05,
19 glitterPow: 13.1,
20 crestGlitter: 1.3,
21 glitterCap: 1,
22 glitterMix: 1.24,
23 shadeLo: 1.06,
24 shadeHi: 1,
25 bodyLo: 0.41,
26 bodyHi: 0.85,
27 litLo: 0.5,
28 litHi: 0.86,
29 litMix: 0.46,
30 edgeDark: 0.37,
31 lightDirX: -0.48,
32 lightDirY: -0.63,
33 lightDirZ: 0.78,
34 normalEps: 0.21,
35 foamStrength: 1,
36 shoreClip: 0.55,
37 /** 0 = baseline deep end; 1 = much darker deep water. */
38 deepDark: 0.4,
39 /** <1 = more abrupt darkening toward deep (pow curve). */
40 deepAbrupt: 0.72
41});
42
43export const DEFAULT_LAVA_DEBUG = Object.freeze({
44 flowX: 0.08,
45 flowY: 0.04,
46 flowZ: 0.27,
47 waveScale: 1.83,
48 fbmAmp: 0.43,
49 texAmp: 0.3,
50 distort: 0.3,
51 normalZ: 1.07,
52 waveGain: 0.52,
53 crestLo: 0.57,
54 crestHi: 0.71,
55 crestPow: 1.15,
56 glitterPow: 7.5,
57 crestGlitter: 1.19,
58 glitterCap: 0.31,
59 glitterMix: 0.51,
60 shadeLo: 0.88,
61 shadeHi: 1.07,
62 bodyLo: 0.05,
63 bodyHi: 1,
64 litLo: 0.59,
65 litHi: 0.74,
66 litMix: 1,
67 edgeDark: 0.55,
68 lightDirX: -0.48,
69 lightDirY: -0.63,
70 lightDirZ: 0.78,
71 normalEps: 0.09,
72 bloomStrength: 0.9,
73 shoreClip: 0.55
74});
75
76const WATER_SLIDERS = [
77 { key: "flowX", label: "Flow X", min: 0, max: 1, step: 0.01 },
78 { key: "flowY", label: "Flow Y", min: 0, max: 1, step: 0.01 },
79 { key: "flowZ", label: "Flow Z", min: 0, max: 1.5, step: 0.01 },
80 { key: "waveScale", label: "Wave scale", min: 0.2, max: 2.5, step: 0.01 },
81 { key: "fbmAmp", label: "FBM amp", min: 0.1, max: 1.5, step: 0.01 },
82 { key: "glitterPow", label: "Glitter pow", min: 1, max: 16, step: 0.1 },
83 { key: "glitterMix", label: "Glitter mix", min: 0, max: 1.5, step: 0.01 },
84 { key: "bodyLo", label: "Body lo", min: 0, max: 0.5, step: 0.01 },
85 { key: "bodyHi", label: "Body hi", min: 0.3, max: 1, step: 0.01 },
86 { key: "litMix", label: "Lit mix", min: 0, max: 1, step: 0.01 },
87 { key: "deepDark", label: "Deep darker", min: 0, max: 1, step: 0.01 },
88 { key: "deepAbrupt", label: "Deep abrupt", min: 0.35, max: 1.4, step: 0.01 },
89 { key: "foamStrength", label: "Foam", min: 0, max: 1, step: 0.01 },
90 { key: "shoreClip", label: "Shore clip", min: 0.1, max: 0.9, step: 0.01 },
91 { key: "edgeDark", label: "Edge dark", min: 0, max: 1, step: 0.01 }
92];
93
94const LAVA_SLIDERS = [
95 { key: "flowX", label: "Flow X", min: 0, max: 1, step: 0.01 },
96 { key: "flowY", label: "Flow Y", min: 0, max: 1, step: 0.01 },
97 { key: "flowZ", label: "Flow Z", min: 0, max: 1.5, step: 0.01 },
98 { key: "waveScale", label: "Wave scale", min: 0.2, max: 2.5, step: 0.01 },
99 { key: "fbmAmp", label: "FBM amp", min: 0.1, max: 1.5, step: 0.01 },
100 { key: "distort", label: "Distort", min: 0, max: 0.5, step: 0.01 },
101 { key: "glitterPow", label: "Heat pow", min: 1, max: 16, step: 0.1 },
102 { key: "glitterMix", label: "Heat mix", min: 0, max: 1.5, step: 0.01 },
103 { key: "glitterCap", label: "Heat cap", min: 0.1, max: 1, step: 0.01 },
104 { key: "bodyLo", label: "Body lo", min: 0, max: 0.5, step: 0.01 },
105 { key: "bodyHi", label: "Body hi", min: 0.3, max: 1, step: 0.01 },
106 { key: "litMix", label: "Lit mix", min: 0, max: 1, step: 0.01 },
107 { key: "bloomStrength", label: "Bloom", min: 0, max: 2, step: 0.01 },
108 { key: "shoreClip", label: "Shore clip", min: 0.1, max: 0.9, step: 0.01 },
109 { key: "edgeDark", label: "Edge dark", min: 0, max: 1, step: 0.01 }
110];
111
112let waterParams = { ...DEFAULT_WATER_DEBUG };
113let lavaParams = { ...DEFAULT_LAVA_DEBUG };
114let activeTab = "water";
115let panelEl = null;
116
117export function getWaterDebugParams ()
118{
119 return { ...waterParams };
120}
121
122export function getLavaDebugParams ()
123{
124 return { ...lavaParams };
125}
126
127function uniformBagFrom (p, extras = {})
128{
129 return {
130 u_flowX: p.flowX,
131 u_flowY: p.flowY,
132 u_flowZ: p.flowZ,
133 u_waveScale: p.waveScale,
134 u_fbmAmp: p.fbmAmp,
135 u_texAmp: p.texAmp,
136 u_distort: p.distort,
137 u_normalZ: p.normalZ,
138 u_waveGain: p.waveGain,
139 u_crestLo: p.crestLo,
140 u_crestHi: p.crestHi,
141 u_crestPow: p.crestPow,
142 u_glitterPow: p.glitterPow,
143 u_crestGlitter: p.crestGlitter,
144 u_glitterCap: p.glitterCap,
145 u_glitterMix: p.glitterMix,
146 u_shadeLo: p.shadeLo,
147 u_shadeHi: p.shadeHi,
148 u_bodyLo: p.bodyLo,
149 u_bodyHi: p.bodyHi,
150 u_litLo: p.litLo,
151 u_litHi: p.litHi,
152 u_litMix: p.litMix,
153 u_edgeDark: p.edgeDark,
154 u_lightDirX: p.lightDirX,
155 u_lightDirY: p.lightDirY,
156 u_lightDirZ: p.lightDirZ,
157 u_normalEps: p.normalEps,
158 ...extras
159 };
160}
161
162/** Uniform bag for the water ground fragment shader. */
163export function waterDebugUniforms ()
164{
165 const p = waterParams;
166 return uniformBagFrom(p, {
167 u_foamStrength: p.foamStrength,
168 u_shoreClip: p.shoreClip,
169 u_deepDark: p.deepDark,
170 u_deepAbrupt: p.deepAbrupt
171 });
172}
173
174/** Uniform bag for lava ground / bloom shaders. */
175export function lavaDebugUniforms ()
176{
177 const p = lavaParams;
178 return uniformBagFrom(p, {
179 u_bloomStrength: p.bloomStrength,
180 u_shoreClip: p.shoreClip
181 });
182}
183
184export function setWaterDebugParams (next)
185{
186 waterParams = { ...DEFAULT_WATER_DEBUG, ...waterParams, ...next };
187 syncInputs();
188}
189
190export function setLavaDebugParams (next)
191{
192 lavaParams = { ...DEFAULT_LAVA_DEBUG, ...lavaParams, ...next };
193 syncInputs();
194}
195
196function activeParams ()
197{
198 return activeTab === "lava" ? lavaParams : waterParams;
199}
200
201function activeSliders ()
202{
203 return activeTab === "lava" ? LAVA_SLIDERS : WATER_SLIDERS;
204}
205
206function syncInputs ()
207{
208 if (!panelEl)
209 { return; }
210 for (const { key } of activeSliders())
211 {
212 const input = panelEl.querySelector(`input[data-fluid-key="${key}"]`);
213 const valueEl = panelEl.querySelector(`[data-fluid-value="${key}"]`);
214 const params = activeParams();
215 if (input)
216 { input.value = String(params[key]); }
217 if (valueEl)
218 { valueEl.textContent = Number(params[key]).toFixed(3); }
219 }
220 for (const btn of panelEl.querySelectorAll("[data-fluid-tab]"))
221 {
222 const on = btn.getAttribute("data-fluid-tab") === activeTab;
223 btn.style.opacity = on ? "1" : "0.55";
224 btn.style.borderBottom = on ? "2px solid #9ec5ff" : "2px solid transparent";
225 }
226}
227
228function exportJson ()
229{
230 const isLava = activeTab === "lava";
231 const text = JSON.stringify(isLava ? getLavaDebugParams() : getWaterDebugParams(), null, 2);
232 void navigator.clipboard?.writeText(text).catch(() => { });
233 const blob = new Blob([text], { type: "application/json" });
234 const url = URL.createObjectURL(blob);
235 const a = document.createElement("a");
236 a.href = url;
237 a.download = isLava ? "lava-debug.json" : "water-debug.json";
238 a.click();
239 URL.revokeObjectURL(url);
240 const status = panelEl?.querySelector("[data-fluid-export-status]");
241 if (status)
242 {
243 status.textContent = "Exported";
244 setTimeout(() =>
245 {
246 if (status.textContent === "Exported")
247 { status.textContent = ""; }
248 }, 2000);
249 }
250}
251
252function rebuildSliderRows ()
253{
254 const scroll = panelEl?.querySelector(".fluid-debug-scroll");
255 if (!scroll)
256 { return; }
257 scroll.replaceChildren();
258 const params = activeParams();
259 for (const spec of activeSliders())
260 {
261 const row = document.createElement("label");
262 row.style.cssText = "display:grid;grid-template-columns:1fr 48px;gap:2px 6px;align-items:center;";
263 row.innerHTML = `
264 <span style="grid-column:1/-1;opacity:0.85">${spec.label}</span>
265 <input type="range" data-fluid-key="${spec.key}" min="${spec.min}" max="${spec.max}" step="${spec.step}" value="${params[spec.key]}">
266 <span data-fluid-value="${spec.key}" style="text-align:right;opacity:0.9">${Number(params[spec.key]).toFixed(3)}</span>
267 `;
268 const input = row.querySelector("input");
269 input.addEventListener("input", () =>
270 {
271 const bag = activeTab === "lava" ? lavaParams : waterParams;
272 bag[spec.key] = Number(input.value);
273 const valueEl = row.querySelector(`[data-fluid-value="${spec.key}"]`);
274 if (valueEl)
275 { valueEl.textContent = Number(bag[spec.key]).toFixed(3); }
276 });
277 scroll.appendChild(row);
278 }
279}
280
281function setTab (tab)
282{
283 activeTab = tab === "lava" ? "lava" : "water";
284 rebuildSliderRows();
285 syncInputs();
286}
287
288/**
289 * Always-visible temporary debug dock (pointer-events on panel only).
290 */
291export function initWaterDebugPanel ()
292{
293 if (panelEl)
294 { return panelEl; }
295
296 panelEl = document.createElement("aside");
297 panelEl.id = "fluid-debug-panel";
298 panelEl.innerHTML = `
299 <header style="display:flex;align-items:center;gap:8px;flex-wrap:wrap;">
300 <nav style="display:flex;gap:6px;">
301 <button type="button" data-fluid-tab="water">Water</button>
302 <button type="button" data-fluid-tab="lava">Lava</button>
303 </nav>
304 <span data-fluid-export-status style="margin-left:auto;opacity:0.75"></span>
305 </header>
306 <div class="fluid-debug-scroll"></div>
307 <footer style="display:flex;gap:6px;">
308 <button type="button" data-fluid-reset>Reset</button>
309 <button type="button" data-fluid-export>Export</button>
310 </footer>
311 `;
312 Object.assign(panelEl.style, {
313 position: "fixed",
314 top: "8px",
315 right: "8px",
316 width: "250px",
317 maxHeight: "calc(100vh - 16px)",
318 zIndex: "9999",
319 display: "flex",
320 flexDirection: "column",
321 gap: "4px",
322 padding: "8px",
323 background: "rgba(12, 16, 22, 0.92)",
324 color: "#e8eef5",
325 font: "11px/1.25 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace",
326 border: "1px solid rgba(255,255,255,0.18)",
327 borderRadius: "6px",
328 boxShadow: "0 8px 24px rgba(0,0,0,0.45)",
329 pointerEvents: "auto"
330 });
331
332 const scroll = panelEl.querySelector(".fluid-debug-scroll");
333 Object.assign(scroll.style, {
334 overflowY: "auto",
335 flex: "1 1 auto",
336 display: "flex",
337 flexDirection: "column",
338 gap: "3px",
339 paddingRight: "2px"
340 });
341
342 for (const btn of panelEl.querySelectorAll("button"))
343 {
344 Object.assign(btn.style, {
345 background: "transparent",
346 color: "inherit",
347 border: "1px solid rgba(255,255,255,0.22)",
348 borderRadius: "4px",
349 padding: "3px 8px",
350 cursor: "pointer",
351 font: "inherit"
352 });
353 }
354 for (const btn of panelEl.querySelectorAll("[data-fluid-tab]"))
355 {
356 btn.style.border = "none";
357 btn.style.borderRadius = "0";
358 btn.style.padding = "2px 4px";
359 btn.addEventListener("click", (event) =>
360 {
361 event.preventDefault();
362 event.stopPropagation();
363 setTab(btn.getAttribute("data-fluid-tab"));
364 });
365 }
366
367 panelEl.querySelector("[data-fluid-reset]").addEventListener("click", (event) =>
368 {
369 event.preventDefault();
370 event.stopPropagation();
371 if (activeTab === "lava")
372 { lavaParams = { ...DEFAULT_LAVA_DEBUG }; }
373 else
374 { waterParams = { ...DEFAULT_WATER_DEBUG }; }
375 rebuildSliderRows();
376 syncInputs();
377 });
378 panelEl.querySelector("[data-fluid-export]").addEventListener("click", (event) =>
379 {
380 event.preventDefault();
381 event.stopPropagation();
382 exportJson();
383 });
384
385 for (const type of ["mousedown", "mouseup", "click", "wheel", "contextmenu", "pointerdown"])
386 {
387 panelEl.addEventListener(type, (event) =>
388 {
389 event.stopPropagation();
390 });
391 }
392
393 rebuildSliderRows();
394 syncInputs();
395 document.body.appendChild(panelEl);
396 return panelEl;
397}