| 1 | /** |
| 2 | * Temporary always-on nature tint debug panel (blendable “nature” grounds). |
| 3 | */ |
| 4 | |
| 5 | export const DEFAULT_NATURE_TINT = Object.freeze({ |
| 6 | /** World-space noise frequency (tiles). */ |
| 7 | scale: 0.08, |
| 8 | /** 0..1 how hard to reshape noise around mid-grey. */ |
| 9 | contrast: 1, |
| 10 | /** Warm/cool recolor strength. */ |
| 11 | tint: 0.12, |
| 12 | /** Brightness variation strength. */ |
| 13 | darken: 0.08 |
| 14 | }); |
| 15 | |
| 16 | const SLIDERS = [ |
| 17 | { key: "scale", label: "Noise scale", min: 0.01, max: 0.4, step: 0.005 }, |
| 18 | { key: "contrast", label: "Contrast", min: 0.4, max: 2.5, step: 0.05 }, |
| 19 | { key: "tint", label: "Tint", min: 0, max: 0.45, step: 0.01 }, |
| 20 | { key: "darken", label: "Darken", min: 0, max: 0.4, step: 0.01 } |
| 21 | ]; |
| 22 | |
| 23 | let params = { ...DEFAULT_NATURE_TINT }; |
| 24 | let panelEl = null; |
| 25 | let onChange = null; |
| 26 | |
| 27 | export function getNatureTintParams () |
| 28 | { |
| 29 | return { ...params }; |
| 30 | } |
| 31 | |
| 32 | /** Shader uniforms for atlas nature tint (ground only). */ |
| 33 | export function natureTintUniforms () |
| 34 | { |
| 35 | const p = params; |
| 36 | return { |
| 37 | u_useNature: 1, |
| 38 | u_natureTint: p.tint, |
| 39 | u_natureDark: p.darken, |
| 40 | u_natureContrast: p.contrast |
| 41 | }; |
| 42 | } |
| 43 | |
| 44 | export function setNatureTintParams (next) |
| 45 | { |
| 46 | const prevScale = params.scale; |
| 47 | params = { ...DEFAULT_NATURE_TINT, ...params, ...next }; |
| 48 | syncInputs(); |
| 49 | onChange?.({ |
| 50 | rebuildNoise: params.scale !== prevScale || next?.scale != null |
| 51 | }); |
| 52 | } |
| 53 | |
| 54 | function syncInputs () |
| 55 | { |
| 56 | if (!panelEl) |
| 57 | { return; } |
| 58 | for (const { key } of SLIDERS) |
| 59 | { |
| 60 | const input = panelEl.querySelector(`input[data-nature-key="${key}"]`); |
| 61 | const valueEl = panelEl.querySelector(`[data-nature-value="${key}"]`); |
| 62 | if (input) |
| 63 | { input.value = String(params[key]); } |
| 64 | if (valueEl) |
| 65 | { valueEl.textContent = Number(params[key]).toFixed(3); } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * @param {{ onChange?: (info: { rebuildNoise: boolean }) => void }} [opts] |
| 71 | */ |
| 72 | export function initNatureTintDebugPanel (opts = {}) |
| 73 | { |
| 74 | if (panelEl) |
| 75 | { return panelEl; } |
| 76 | onChange = opts.onChange ?? null; |
| 77 | |
| 78 | panelEl = document.createElement("aside"); |
| 79 | panelEl.id = "nature-tint-debug-panel"; |
| 80 | panelEl.innerHTML = ` |
| 81 | <header style="display:flex;align-items:center;gap:8px;"> |
| 82 | <strong>Nature tint</strong> |
| 83 | </header> |
| 84 | <div class="nature-tint-scroll"></div> |
| 85 | <footer style="display:flex;gap:6px;"> |
| 86 | <button type="button" data-nature-reset>Reset</button> |
| 87 | </footer> |
| 88 | `; |
| 89 | Object.assign(panelEl.style, { |
| 90 | position: "fixed", |
| 91 | top: "8px", |
| 92 | right: "8px", |
| 93 | width: "240px", |
| 94 | zIndex: "9999", |
| 95 | display: "flex", |
| 96 | flexDirection: "column", |
| 97 | gap: "4px", |
| 98 | padding: "8px", |
| 99 | background: "rgba(12, 16, 22, 0.92)", |
| 100 | color: "#e8eef5", |
| 101 | font: "11px/1.25 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace", |
| 102 | border: "1px solid rgba(255,255,255,0.18)", |
| 103 | borderRadius: "6px", |
| 104 | boxShadow: "0 8px 24px rgba(0,0,0,0.45)", |
| 105 | pointerEvents: "auto" |
| 106 | }); |
| 107 | |
| 108 | const scroll = panelEl.querySelector(".nature-tint-scroll"); |
| 109 | Object.assign(scroll.style, { |
| 110 | display: "flex", |
| 111 | flexDirection: "column", |
| 112 | gap: "3px" |
| 113 | }); |
| 114 | |
| 115 | for (const spec of SLIDERS) |
| 116 | { |
| 117 | const row = document.createElement("label"); |
| 118 | row.style.cssText = "display:grid;grid-template-columns:1fr 48px;gap:2px 6px;align-items:center;"; |
| 119 | row.innerHTML = ` |
| 120 | <span style="grid-column:1/-1;opacity:0.85">${spec.label}</span> |
| 121 | <input type="range" data-nature-key="${spec.key}" min="${spec.min}" max="${spec.max}" step="${spec.step}" value="${params[spec.key]}"> |
| 122 | <span data-nature-value="${spec.key}" style="text-align:right;opacity:0.9">${Number(params[spec.key]).toFixed(3)}</span> |
| 123 | `; |
| 124 | const input = row.querySelector("input"); |
| 125 | input.addEventListener("input", () => |
| 126 | { |
| 127 | const prevScale = params.scale; |
| 128 | params[spec.key] = Number(input.value); |
| 129 | const valueEl = row.querySelector(`[data-nature-value="${spec.key}"]`); |
| 130 | if (valueEl) |
| 131 | { valueEl.textContent = Number(params[spec.key]).toFixed(3); } |
| 132 | onChange?.({ rebuildNoise: spec.key === "scale" && params.scale !== prevScale }); |
| 133 | }); |
| 134 | scroll.appendChild(row); |
| 135 | } |
| 136 | |
| 137 | const resetBtn = panelEl.querySelector("[data-nature-reset]"); |
| 138 | Object.assign(resetBtn.style, { |
| 139 | background: "transparent", |
| 140 | color: "inherit", |
| 141 | border: "1px solid rgba(255,255,255,0.22)", |
| 142 | borderRadius: "4px", |
| 143 | padding: "3px 8px", |
| 144 | cursor: "pointer", |
| 145 | font: "inherit" |
| 146 | }); |
| 147 | resetBtn.addEventListener("click", (event) => |
| 148 | { |
| 149 | event.preventDefault(); |
| 150 | event.stopPropagation(); |
| 151 | params = { ...DEFAULT_NATURE_TINT }; |
| 152 | syncInputs(); |
| 153 | onChange?.({ rebuildNoise: true }); |
| 154 | }); |
| 155 | |
| 156 | for (const type of ["mousedown", "mouseup", "click", "wheel", "contextmenu", "pointerdown"]) |
| 157 | { |
| 158 | panelEl.addEventListener(type, (event) => |
| 159 | { |
| 160 | event.stopPropagation(); |
| 161 | }); |
| 162 | } |
| 163 | |
| 164 | document.body.appendChild(panelEl); |
| 165 | return panelEl; |
| 166 | } |