BlueGit

timfa questward

Files

questward / client / src / play / waterDebug.js

waterDebug.js

waterDebug.js 7.5 KiB Download Raw
1/**
2 * Temporary always-on water shader debug panel (simplex FBM tune).
3 * Export JSON when you land on values you like.
4 */
5
6export const DEFAULT_WATER_DEBUG = Object.freeze({
7 flowX: 0.33,
8 flowY: 0.21,
9 flowZ: 0.52,
10 waveScale: 0.85,
11 fbmAmp: 0.75,
12 texAmp: 0.18,
13 distort: 0.14,
14 normalZ: 0.9,
15 waveGain: 0.42,
16 crestLo: 0.72,
17 crestHi: 0.96,
18 crestPow: 2.2,
19 glitterPow: 7.5,
20 crestGlitter: 0.55,
21 glitterCap: 0.72,
22 glitterMix: 0.8,
23 shadeLo: 0.9,
24 shadeHi: 1.06,
25 bodyLo: 0.12,
26 bodyHi: 0.72,
27 litLo: 0.52,
28 litHi: 0.9,
29 litMix: 0.4,
30 edgeDark: 0.4,
31 lightDirX: -0.55,
32 lightDirY: -0.5,
33 lightDirZ: 0.75,
34 normalEps: 0.1,
35 foamStrength: 0.95,
36 shoreClip: 0.42
37});
38
39const SLIDERS = [
40 { key: "flowX", label: "Flow X", min: 0, max: 1, step: 0.01 },
41 { key: "flowY", label: "Flow Y", min: 0, max: 1, step: 0.01 },
42 { key: "flowZ", label: "Flow Z (time)", min: 0, max: 1.5, step: 0.01 },
43 { key: "waveScale", label: "Wave scale", min: 0.2, max: 2.5, step: 0.01 },
44 { key: "fbmAmp", label: "FBM amp", min: 0.1, max: 1.5, step: 0.01 },
45 { key: "texAmp", label: "Tex amp", min: 0, max: 0.6, step: 0.01 },
46 { key: "distort", label: "Distort", min: 0, max: 0.5, step: 0.01 },
47 { key: "normalEps", label: "Normal ε", min: 0.02, max: 0.3, step: 0.01 },
48 { key: "normalZ", label: "Normal Z", min: 0.3, max: 1.5, step: 0.01 },
49 { key: "waveGain", label: "Wave gain", min: 0.1, max: 1, step: 0.01 },
50 { key: "crestLo", label: "Crest lo", min: 0.3, max: 0.95, step: 0.01 },
51 { key: "crestHi", label: "Crest hi", min: 0.5, max: 1, step: 0.01 },
52 { key: "crestPow", label: "Crest pow", min: 0.5, max: 5, step: 0.05 },
53 { key: "glitterPow", label: "Glitter pow", min: 1, max: 16, step: 0.1 },
54 { key: "crestGlitter", label: "Crest glitter", min: 0, max: 1.5, step: 0.01 },
55 { key: "glitterCap", label: "Glitter cap", min: 0.1, max: 1, step: 0.01 },
56 { key: "glitterMix", label: "Glitter mix", min: 0, max: 1.5, step: 0.01 },
57 { key: "shadeLo", label: "Shade lo", min: 0.5, max: 1.1, step: 0.01 },
58 { key: "shadeHi", label: "Shade hi", min: 0.9, max: 1.4, step: 0.01 },
59 { key: "bodyLo", label: "Body lo", min: 0, max: 0.5, step: 0.01 },
60 { key: "bodyHi", label: "Body hi", min: 0.3, max: 1, step: 0.01 },
61 { key: "litLo", label: "Lit lo", min: 0.2, max: 0.8, step: 0.01 },
62 { key: "litHi", label: "Lit hi", min: 0.5, max: 1, step: 0.01 },
63 { key: "litMix", label: "Lit mix", min: 0, max: 1, step: 0.01 },
64 { key: "edgeDark", label: "Edge dark", min: 0, max: 1, step: 0.01 },
65 { key: "lightDirX", label: "Light dir X", min: -1, max: 1, step: 0.01 },
66 { key: "lightDirY", label: "Light dir Y", min: -1, max: 1, step: 0.01 },
67 { key: "lightDirZ", label: "Light dir Z", min: 0.1, max: 1.5, step: 0.01 },
68 { key: "foamStrength", label: "Foam strength", min: 0, max: 1, step: 0.01 },
69 { key: "shoreClip", label: "Shore clip", min: 0.1, max: 0.9, step: 0.01 }
70];
71
72let params = { ...DEFAULT_WATER_DEBUG };
73let panelEl = null;
74
75export function getWaterDebugParams ()
76{
77 return { ...params };
78}
79
80/** Uniform bag for the water ground fragment shader. */
81export function waterDebugUniforms ()
82{
83 const p = params;
84 return {
85 u_flowX: p.flowX,
86 u_flowY: p.flowY,
87 u_flowZ: p.flowZ,
88 u_waveScale: p.waveScale,
89 u_fbmAmp: p.fbmAmp,
90 u_texAmp: p.texAmp,
91 u_distort: p.distort,
92 u_normalZ: p.normalZ,
93 u_waveGain: p.waveGain,
94 u_crestLo: p.crestLo,
95 u_crestHi: p.crestHi,
96 u_crestPow: p.crestPow,
97 u_glitterPow: p.glitterPow,
98 u_crestGlitter: p.crestGlitter,
99 u_glitterCap: p.glitterCap,
100 u_glitterMix: p.glitterMix,
101 u_shadeLo: p.shadeLo,
102 u_shadeHi: p.shadeHi,
103 u_bodyLo: p.bodyLo,
104 u_bodyHi: p.bodyHi,
105 u_litLo: p.litLo,
106 u_litHi: p.litHi,
107 u_litMix: p.litMix,
108 u_edgeDark: p.edgeDark,
109 u_lightDirX: p.lightDirX,
110 u_lightDirY: p.lightDirY,
111 u_lightDirZ: p.lightDirZ,
112 u_normalEps: p.normalEps,
113 u_foamStrength: p.foamStrength,
114 u_shoreClip: p.shoreClip
115 };
116}
117
118export function setWaterDebugParams (next)
119{
120 params = { ...DEFAULT_WATER_DEBUG, ...params, ...next };
121 syncInputs();
122}
123
124function syncInputs ()
125{
126 if (!panelEl)
127 { return; }
128 for (const { key } of SLIDERS)
129 {
130 const input = panelEl.querySelector(`input[data-water-key="${key}"]`);
131 const valueEl = panelEl.querySelector(`[data-water-value="${key}"]`);
132 if (input)
133 { input.value = String(params[key]); }
134 if (valueEl)
135 { valueEl.textContent = Number(params[key]).toFixed(3); }
136 }
137}
138
139function exportJson ()
140{
141 const text = JSON.stringify(getWaterDebugParams(), null, 2);
142 void navigator.clipboard?.writeText(text).catch(() => {});
143 const blob = new Blob([text], { type: "application/json" });
144 const url = URL.createObjectURL(blob);
145 const a = document.createElement("a");
146 a.href = url;
147 a.download = "water-debug.json";
148 a.click();
149 URL.revokeObjectURL(url);
150 const status = panelEl?.querySelector("[data-water-export-status]");
151 if (status)
152 {
153 status.textContent = "Exported (+ copied if allowed)";
154 setTimeout(() =>
155 {
156 if (status.textContent.startsWith("Exported"))
157 { status.textContent = ""; }
158 }, 2000);
159 }
160}
161
162/**
163 * Always-visible temporary debug dock (pointer-events on panel only).
164 */
165export function initWaterDebugPanel ()
166{
167 if (panelEl)
168 { return panelEl; }
169
170 panelEl = document.createElement("aside");
171 panelEl.id = "water-debug-panel";
172 panelEl.innerHTML = `
173 <header>
174 <strong>Water debug</strong>
175 <span data-water-export-status></span>
176 </header>
177 <div class="water-debug-scroll"></div>
178 <footer>
179 <button type="button" data-water-reset>Reset</button>
180 <button type="button" data-water-export>Export JSON</button>
181 </footer>
182 `;
183 Object.assign(panelEl.style, {
184 position: "fixed",
185 top: "8px",
186 right: "8px",
187 width: "280px",
188 maxHeight: "calc(100vh - 16px)",
189 zIndex: "9999",
190 display: "flex",
191 flexDirection: "column",
192 gap: "6px",
193 padding: "8px",
194 background: "rgba(12, 16, 22, 0.92)",
195 color: "#e8eef5",
196 font: "12px/1.3 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace",
197 border: "1px solid rgba(255,255,255,0.18)",
198 borderRadius: "6px",
199 boxShadow: "0 8px 24px rgba(0,0,0,0.45)",
200 pointerEvents: "auto"
201 });
202
203 const scroll = panelEl.querySelector(".water-debug-scroll");
204 Object.assign(scroll.style, {
205 overflowY: "auto",
206 flex: "1 1 auto",
207 display: "flex",
208 flexDirection: "column",
209 gap: "4px",
210 paddingRight: "2px"
211 });
212
213 for (const spec of SLIDERS)
214 {
215 const row = document.createElement("label");
216 row.style.cssText = "display:grid;grid-template-columns:1fr 52px;gap:4px 6px;align-items:center;";
217 row.innerHTML = `
218 <span style="grid-column:1/-1;opacity:0.85">${spec.label}</span>
219 <input type="range" data-water-key="${spec.key}" min="${spec.min}" max="${spec.max}" step="${spec.step}" value="${params[spec.key]}">
220 <span data-water-value="${spec.key}" style="text-align:right;opacity:0.9">${Number(params[spec.key]).toFixed(3)}</span>
221 `;
222 const input = row.querySelector("input");
223 input.addEventListener("input", () =>
224 {
225 params[spec.key] = Number(input.value);
226 const valueEl = row.querySelector(`[data-water-value="${spec.key}"]`);
227 if (valueEl)
228 { valueEl.textContent = Number(params[spec.key]).toFixed(3); }
229 });
230 scroll.appendChild(row);
231 }
232
233 panelEl.querySelector("[data-water-reset]").addEventListener("click", (event) =>
234 {
235 event.preventDefault();
236 event.stopPropagation();
237 params = { ...DEFAULT_WATER_DEBUG };
238 syncInputs();
239 });
240 panelEl.querySelector("[data-water-export]").addEventListener("click", (event) =>
241 {
242 event.preventDefault();
243 event.stopPropagation();
244 exportJson();
245 });
246
247 for (const type of ["mousedown", "mouseup", "click", "wheel", "contextmenu", "pointerdown"])
248 {
249 panelEl.addEventListener(type, (event) =>
250 {
251 event.stopPropagation();
252 });
253 }
254
255 document.body.appendChild(panelEl);
256 return panelEl;
257}