BlueGit

timfa questward

Try sliding baked Perlin caustics instead of Gerstner water.

Pack three tiling noise layers in RGB, scroll them at different angles additively, and at coasts brighten light caustics while dark pixels retreat into the shore neighbor sprite.

timfa Tim Falken · pushed by timfa

Authored 2026-09-04 · 2 months ago

Hash
47448bc83b881ab377cab15a111bd62d9f7cb89f
Parent
925fc84f
6 files changed +163 −107
client/assets/grounds/deep-water/deep-water.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/grounds/water/water.png Binary file +0 −0

The contents of this binary file are not shown.

client/scripts/write-placeholder-assets.mjs +2 −17
@@ -53,24 +53,9 @@ overlay(join(root, "items", "lantern", "lantern-equip.png"), [200, 130, 48, 230]
53 53 torchBloom(join(root, "items", "lantern", "lantern-equip-bloom.png"));
54 54 torchBloom(join(root, "blocks", "torch", "torch-bloom.png"));
55 55
56 function writeWaterNoise (file, seed)
57 {
58 const size = 16;
59 const image = makeRgba(size, size, [0, 0, 0, 255]);
60 for (let y = 0; y < size; y++)
61 {
62 for (let x = 0; x < size; x++)
63 {
64 const n = Math.abs(Math.sin((x + seed * 3.1) * 12.9898 + (y + seed) * 78.233)) * 43758.5453;
65 const v = Math.floor((n - Math.floor(n)) * 255);
66 fillRect(image, x, y, 1, 1, [v, v, v, 255]);
67 }
68 }
69 writeRgbaPng(file, size, size, image.pixels);
70 }
56 // Water caustic noise (64×64 RGB tiling Perlin) — regenerate via:
57 // node client/scripts/write-water-caustics-noise.mjs
71 58
72 writeWaterNoise(join(root, "grounds", "water", "water.png"), 1);
73 writeWaterNoise(join(root, "grounds", "deep-water", "deep-water.png"), 2);
74 59
75 60 {
76 61 const image = makeRgba(24, 8, [0, 0, 0, 0]);
@@ -53,24 +53,9 @@ overlay(join(root, "items", "lantern", "lantern-equip.png"), [200, 130, 48, 230]
53torchBloom(join(root, "items", "lantern", "lantern-equip-bloom.png"));
53torchBloom(join(root, "items", "lantern", "lantern-equip-bloom.png"));
54torchBloom(join(root, "blocks", "torch", "torch-bloom.png"));
54torchBloom(join(root, "blocks", "torch", "torch-bloom.png"));
55
55
56function writeWaterNoise (file, seed)
56// Water caustic noise (64×64 RGB tiling Perlin) — regenerate via:
57{
57// node client/scripts/write-water-caustics-noise.mjs
58 const size = 16;
59 const image = makeRgba(size, size, [0, 0, 0, 255]);
60 for (let y = 0; y < size; y++)
61 {
62 for (let x = 0; x < size; x++)
63 {
64 const n = Math.abs(Math.sin((x + seed * 3.1) * 12.9898 + (y + seed) * 78.233)) * 43758.5453;
65 const v = Math.floor((n - Math.floor(n)) * 255);
66 fillRect(image, x, y, 1, 1, [v, v, v, 255]);
67 }
68 }
69 writeRgbaPng(file, size, size, image.pixels);
70}
71
58
72writeWaterNoise(join(root, "grounds", "water", "water.png"), 1);
73writeWaterNoise(join(root, "grounds", "deep-water", "deep-water.png"), 2);
74
59
75{
60{
76 const image = makeRgba(24, 8, [0, 0, 0, 0]);
61 const image = makeRgba(24, 8, [0, 0, 0, 0]);
client/scripts/write-water-caustics-noise.mjs added +99 −0
@@ -0,0 +1,99 @@
1 /**
2 * Bake seamless multi-octave value-noise layers into water ground PNGs.
3 * R/G/B = three independent tiling fields for sliding additive caustics.
4 */
5 import { join } from "node:path";
6 import { makeRgba, writeRgbaPng } from "../../server/src/assets/placeholderPng.js";
7
8 const SIZE = 64;
9 const root = join(import.meta.dirname, "..", "assets");
10
11 function wrapInt (v, period)
12 {
13 const m = ((v % period) + period) % period;
14 return m | 0;
15 }
16
17 function hash2 (ix, iy, seed)
18 {
19 const n = Math.sin(ix * 127.1 + iy * 311.7 + seed * 74.7) * 43758.5453123;
20 return n - Math.floor(n);
21 }
22
23 function valueNoise (x, y, period, seed)
24 {
25 const x0 = Math.floor(x);
26 const y0 = Math.floor(y);
27 const fx = x - x0;
28 const fy = y - y0;
29 const ux = fx * fx * (3 - 2 * fx);
30 const uy = fy * fy * (3 - 2 * fy);
31 const a = hash2(wrapInt(x0, period), wrapInt(y0, period), seed);
32 const b = hash2(wrapInt(x0 + 1, period), wrapInt(y0, period), seed);
33 const c = hash2(wrapInt(x0, period), wrapInt(y0 + 1, period), seed);
34 const d = hash2(wrapInt(x0 + 1, period), wrapInt(y0 + 1, period), seed);
35 return a + (b - a) * ux + (c - a) * uy + (a - b - c + d) * ux * uy;
36 }
37
38 function fbm (x, y, period, seed, octaves = 4)
39 {
40 let amp = 0.5;
41 let freq = 1;
42 let sum = 0;
43 let norm = 0;
44 for (let i = 0; i < octaves; i++)
45 {
46 const p = Math.max(2, Math.round(period * freq));
47 sum += valueNoise(x * freq, y * freq, p, seed + i * 19.1) * amp;
48 norm += amp;
49 amp *= 0.5;
50 freq *= 2;
51 }
52 return sum / norm;
53 }
54
55 function bakeLayer (seed, cells)
56 {
57 const values = new Float32Array(SIZE * SIZE);
58 let min = Infinity;
59 let max = -Infinity;
60 for (let y = 0; y < SIZE; y++)
61 {
62 for (let x = 0; x < SIZE; x++)
63 {
64 const v = fbm((x / SIZE) * cells, (y / SIZE) * cells, cells, seed);
65 values[y * SIZE + x] = v;
66 if (v < min)
67 { min = v; }
68 if (v > max)
69 { max = v; }
70 }
71 }
72 const span = Math.max(1e-6, max - min);
73 const out = new Uint8Array(SIZE * SIZE);
74 for (let i = 0; i < values.length; i++)
75 { out[i] = Math.round(((values[i] - min) / span) * 255); }
76 return out;
77 }
78
79 function writePackedNoise (file, seeds)
80 {
81 const r = bakeLayer(seeds[0], 4);
82 const g = bakeLayer(seeds[1], 5);
83 const b = bakeLayer(seeds[2], 6);
84 const image = makeRgba(SIZE, SIZE, [0, 0, 0, 255]);
85 for (let i = 0; i < SIZE * SIZE; i++)
86 {
87 const o = i * 4;
88 image.pixels[o] = r[i];
89 image.pixels[o + 1] = g[i];
90 image.pixels[o + 2] = b[i];
91 image.pixels[o + 3] = 255;
92 }
93 writeRgbaPng(file, SIZE, SIZE, image.pixels);
94 }
95
96 writePackedNoise(join(root, "grounds", "water", "water.png"), [11.3, 27.9, 41.2]);
97 writePackedNoise(join(root, "grounds", "deep-water", "deep-water.png"), [53.7, 67.1, 89.4]);
98
99 console.log(`Wrote ${SIZE}x${SIZE} RGB tiling Perlin packs for water + deep-water.`);
@@ -0,0 +1,99 @@
1/**
2 * Bake seamless multi-octave value-noise layers into water ground PNGs.
3 * R/G/B = three independent tiling fields for sliding additive caustics.
4 */
5import { join } from "node:path";
6import { makeRgba, writeRgbaPng } from "../../server/src/assets/placeholderPng.js";
7
8const SIZE = 64;
9const root = join(import.meta.dirname, "..", "assets");
10
11function wrapInt (v, period)
12{
13 const m = ((v % period) + period) % period;
14 return m | 0;
15}
16
17function hash2 (ix, iy, seed)
18{
19 const n = Math.sin(ix * 127.1 + iy * 311.7 + seed * 74.7) * 43758.5453123;
20 return n - Math.floor(n);
21}
22
23function valueNoise (x, y, period, seed)
24{
25 const x0 = Math.floor(x);
26 const y0 = Math.floor(y);
27 const fx = x - x0;
28 const fy = y - y0;
29 const ux = fx * fx * (3 - 2 * fx);
30 const uy = fy * fy * (3 - 2 * fy);
31 const a = hash2(wrapInt(x0, period), wrapInt(y0, period), seed);
32 const b = hash2(wrapInt(x0 + 1, period), wrapInt(y0, period), seed);
33 const c = hash2(wrapInt(x0, period), wrapInt(y0 + 1, period), seed);
34 const d = hash2(wrapInt(x0 + 1, period), wrapInt(y0 + 1, period), seed);
35 return a + (b - a) * ux + (c - a) * uy + (a - b - c + d) * ux * uy;
36}
37
38function fbm (x, y, period, seed, octaves = 4)
39{
40 let amp = 0.5;
41 let freq = 1;
42 let sum = 0;
43 let norm = 0;
44 for (let i = 0; i < octaves; i++)
45 {
46 const p = Math.max(2, Math.round(period * freq));
47 sum += valueNoise(x * freq, y * freq, p, seed + i * 19.1) * amp;
48 norm += amp;
49 amp *= 0.5;
50 freq *= 2;
51 }
52 return sum / norm;
53}
54
55function bakeLayer (seed, cells)
56{
57 const values = new Float32Array(SIZE * SIZE);
58 let min = Infinity;
59 let max = -Infinity;
60 for (let y = 0; y < SIZE; y++)
61 {
62 for (let x = 0; x < SIZE; x++)
63 {
64 const v = fbm((x / SIZE) * cells, (y / SIZE) * cells, cells, seed);
65 values[y * SIZE + x] = v;
66 if (v < min)
67 { min = v; }
68 if (v > max)
69 { max = v; }
70 }
71 }
72 const span = Math.max(1e-6, max - min);
73 const out = new Uint8Array(SIZE * SIZE);
74 for (let i = 0; i < values.length; i++)
75 { out[i] = Math.round(((values[i] - min) / span) * 255); }
76 return out;
77}
78
79function writePackedNoise (file, seeds)
80{
81 const r = bakeLayer(seeds[0], 4);
82 const g = bakeLayer(seeds[1], 5);
83 const b = bakeLayer(seeds[2], 6);
84 const image = makeRgba(SIZE, SIZE, [0, 0, 0, 255]);
85 for (let i = 0; i < SIZE * SIZE; i++)
86 {
87 const o = i * 4;
88 image.pixels[o] = r[i];
89 image.pixels[o + 1] = g[i];
90 image.pixels[o + 2] = b[i];
91 image.pixels[o + 3] = 255;
92 }
93 writeRgbaPng(file, SIZE, SIZE, image.pixels);
94}
95
96writePackedNoise(join(root, "grounds", "water", "water.png"), [11.3, 27.9, 41.2]);
97writePackedNoise(join(root, "grounds", "deep-water", "deep-water.png"), [53.7, 67.1, 89.4]);
98
99console.log(`Wrote ${SIZE}x${SIZE} RGB tiling Perlin packs for water + deep-water.`);
client/src/render/worldView.js +61 −89
@@ -134,82 +134,44 @@ const WATER_GROUND_FRAG = [
134 134 "uniform float u_shoreEdgeS;",
135 135 "uniform float u_shoreEdgeW;",
136 136 "uniform vec4 u_shoreUvRect;",
137 "float hash21(vec2 p) {",
138 " return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453123);",
137 "vec2 rot2(vec2 p, float a) {",
138 " float c = cos(a);",
139 " float s = sin(a);",
140 " return vec2(c * p.x - s * p.y, s * p.x + c * p.y);",
139 141 "}",
140 "float vnoise(vec2 p) {",
141 " vec2 i = floor(p);",
142 " vec2 f = fract(p);",
143 " f = f * f * (3.0 - 2.0 * f);",
144 " float a = hash21(i);",
145 " float b = hash21(i + vec2(1.0, 0.0));",
146 " float c = hash21(i + vec2(0.0, 1.0));",
147 " float d = hash21(i + vec2(1.0, 1.0));",
148 " return mix(mix(a, b, f.x), mix(c, d, f.x), f.y);",
142 // Wrap UV into (0,1) so CLAMP_TO_EDGE textures still tile when scrolled.
143 "vec2 wrapUv(vec2 uv) {",
144 " return fract(uv) * 0.998 + 0.001;",
149 145 "}",
150 // dir: wave travel direction; steep Q in (0..1); amp; len wavelength in tiles; speed.
151 "void gerstner(vec2 world, vec2 dir, float steep, float amp, float len, float speed, float t,",
152 " inout float h, inout float dx, inout float dy) {",
153 " float invLen = 1.0 / max(len, 0.001);",
154 " float d2 = max(dot(dir, dir), 1e-5);",
155 " vec2 d = dir * inversesqrt(d2);",
156 " float k = 6.2831853 * invLen;",
157 " float phase = k * dot(d, world) - speed * t;",
158 " float s = sin(phase);",
159 " float c = cos(phase);",
160 " h += amp * c;",
161 " float qak = steep * amp * k;",
162 " dx -= d.x * qak * s;",
163 " dy -= d.y * qak * s;",
164 "}",
165 // Shore-normal traveling crest (foam line parallel to coast), not along-shore dots.
166 "void coastEdge(",
167 " float enabled, float dist, float along, float phaseOff, float useShore,",
168 " vec2 shoreNu, inout float foam, inout float shoreMix, inout vec3 shoreRgb",
146 "float sampleShore(",
147 " float enabled, float useShore, vec2 shoreNu,",
148 " inout vec3 shoreRgb, inout float shoreW",
169 149 ") {",
170 " if (enabled < 0.5) { return; }",
171 " float n = vnoise(vec2(along * 1.1 + phaseOff, floor(u_time * 0.2) + phaseOff));",
172 " float frontWobble = 0.035 * sin(along * 6.28318 * 0.85 + phaseOff + u_time * 0.35);",
173 " float travel = fract(u_time * 0.32 + along * 0.12 + phaseOff * 0.07 + n * 0.25);",
174 " float crestDist = mix(0.30, 0.015, smoothstep(0.0, 0.72, travel)) + frontWobble;",
175 " float wash = smoothstep(0.72, 1.0, travel);",
176 " float line = exp(-pow((dist - crestDist) * 26.0, 2.0));",
177 " float sheet = (1.0 - smoothstep(0.0, crestDist + 0.05, dist)) * (1.0 - wash) * 0.22;",
178 " foam = max(foam, line * (0.75 + 0.25 * (1.0 - wash)) + sheet);",
179 " float retreat = wash * smoothstep(0.11, 0.0, dist);",
180 " if (retreat > 0.04 && useShore > 0.5 && u_hasShore > 0.5) {",
181 " vec2 shoreUv = u_shoreUvRect.xy + shoreNu * (u_shoreUvRect.zw - u_shoreUvRect.xy);",
182 " vec4 shore = texture2D(u_secondary, shoreUv);",
183 " if (shore.a > 0.1) {",
184 " shoreRgb = shore.rgb;",
185 " shoreMix = max(shoreMix, retreat);",
186 " }",
187 " }",
150 " if (enabled < 0.5 || useShore < 0.5 || u_hasShore < 0.5) { return 0.0; }",
151 " vec2 shoreUv = u_shoreUvRect.xy + shoreNu * (u_shoreUvRect.zw - u_shoreUvRect.xy);",
152 " vec4 shore = texture2D(u_secondary, shoreUv);",
153 " if (shore.a <= 0.1) { return 0.0; }",
154 " shoreRgb = shore.rgb;",
155 " shoreW = 1.0;",
156 " return 1.0;",
188 157 "}",
189 158 "void main() {",
190 159 " vec2 px = floor(v_texCoord * 16.0) + 0.5;",
191 160 " vec2 puv = px / 16.0;",
192 161 " vec2 world = vec2(u_tileX, u_tileY) + puv;",
193 162 " float t = u_time;",
194 " float nSlow = vnoise(world * 0.55 + vec2(t * 0.04, -t * 0.03));",
195 " float nFine = vnoise(world * 2.4 + vec2(-t * 0.11, t * 0.09));",
196 " float texN = texture2D(u_texture, fract(world * 0.37)).r;",
197 " float h = 0.0;",
198 " float dx = 0.0;",
199 " float dy = 0.0;",
200 // Cardinal + diagonal crossings so no single oval stretch dominates.
201 " gerstner(world, vec2(1.0, 0.0), 0.42, 0.20, 2.8, 1.05, t, h, dx, dy);",
202 " gerstner(world, vec2(0.0, 1.0), 0.42, 0.18, 2.6, 1.12, t, h, dx, dy);",
203 " gerstner(world, vec2(1.0, 0.35), 0.50, 0.16, 3.1, 1.18, t, h, dx, dy);",
204 " gerstner(world, vec2(-0.55, 1.0), 0.52, 0.15, 1.9, 1.48, t, h, dx, dy);",
205 " gerstner(world, vec2(0.75, -0.85), 0.40, 0.11, 1.1, 2.05, t, h, dx, dy);",
206 " gerstner(world, vec2(-0.95, -0.35), 0.45, 0.12, 1.55, 1.72, t, h, dx, dy);",
207 " gerstner(world, vec2(0.4, -1.0), 0.38, 0.09, 0.95, 2.25, t, h, dx, dy);",
208 " h += (nSlow - 0.5) * 0.10 + (nFine - 0.5) * 0.05 + (texN - 0.5) * 0.04;",
209 " float slopeLit = clamp(0.55 + dx * 0.55 + dy * 0.28, 0.0, 1.0);",
210 " float crest = smoothstep(0.12, 0.72, h);",
211 " float trough = smoothstep(-0.05, -0.65, h);",
212 // Soft shallow↔deep blend toward water neighbors (full half-tile ramp).
163 // Three baked Perlin channels, scrolled at different angles / speeds, additive caustics.
164 " vec2 uvR = wrapUv(rot2(world * 0.42, 0.35) + vec2(t * 0.028, t * 0.019));",
165 " vec2 uvG = wrapUv(rot2(world * 0.51, -1.05) + vec2(-t * 0.022, t * 0.031));",
166 " vec2 uvB = wrapUv(rot2(world * 0.63, 1.55) + vec2(t * 0.035, -t * 0.017));",
167 " float nR = texture2D(u_texture, uvR).r;",
168 " float nG = texture2D(u_texture, uvG).g;",
169 " float nB = texture2D(u_texture, uvB).b;",
170 " float caustic = nR * 0.45 + nG * 0.40 + nB * 0.35;",
171 " caustic += nR * nG * 0.55 + nG * nB * 0.35;",
172 " caustic = clamp(caustic * 0.72, 0.0, 1.0);",
173 " caustic = pow(caustic, 1.35);",
174 // Soft shallow↔deep blend toward water neighbors (half-tile ramp).
213 175 " float deepMix = u_deep;",
214 176 " float blendW = 0.0;",
215 177 " float blendAcc = 0.0;",
@@ -231,30 +193,40 @@ const WATER_GROUND_FRAG = [
231 193 " }",
232 194 " if (blendW > 0.001) {",
233 195 " float nbrDeep = clamp(blendAcc / blendW, 0.0, 1.0);",
234 " float w = clamp(blendW, 0.0, 1.0);",
235 " deepMix = mix(u_deep, nbrDeep, w);",
196 " deepMix = mix(u_deep, nbrDeep, clamp(blendW, 0.0, 1.0));",
236 197 " }",
237 " vec3 shallowDeep = mix(vec3(0.16, 0.42, 0.62), vec3(0.09, 0.24, 0.40), deepMix);",
238 " vec3 shallowLit = mix(vec3(0.34, 0.66, 0.82), vec3(0.20, 0.45, 0.66), deepMix);",
239 " vec3 troughCol = mix(vec3(0.10, 0.28, 0.44), vec3(0.05, 0.14, 0.28), deepMix);",
240 " vec3 crestCol = mix(vec3(0.72, 0.88, 0.96), vec3(0.48, 0.70, 0.88), deepMix);",
241 " vec3 rgb = mix(shallowDeep, shallowLit, slopeLit);",
242 " rgb = mix(rgb, troughCol, trough * 0.85);",
243 " rgb = mix(rgb, crestCol, crest * (0.35 + 0.55 * slopeLit));",
244 " vec3 foamCol = vec3(0.82, 0.94, 1.0);",
245 " float foam = 0.0;",
246 " float shoreMix = 0.0;",
198 " vec3 deepCol = mix(vec3(0.12, 0.36, 0.56), vec3(0.06, 0.18, 0.34), deepMix);",
199 " vec3 midCol = mix(vec3(0.22, 0.52, 0.70), vec3(0.12, 0.34, 0.54), deepMix);",
200 " vec3 litCol = mix(vec3(0.55, 0.82, 0.94), vec3(0.34, 0.60, 0.82), deepMix);",
201 " vec3 rgb = mix(deepCol, midCol, smoothstep(0.15, 0.55, caustic));",
202 " rgb = mix(rgb, litCol, smoothstep(0.45, 0.92, caustic));",
203 // Coast: bright caustic → push light-blue; dark → retreat into shore neighbor.
204 " float edgeProx = 0.0;",
205 " float shoreW = 0.0;",
247 206 " vec3 shoreRgb = rgb;",
248 " coastEdge(u_edgeN, puv.y, puv.x, u_tileX * 0.7 + 0.1, u_shoreEdgeN,",
249 " vec2(puv.x, 1.0 - puv.y), foam, shoreMix, shoreRgb);",
250 " coastEdge(u_edgeS, 1.0 - puv.y, puv.x, u_tileX * 0.5 + 1.7, u_shoreEdgeS,",
251 " vec2(puv.x, 1.0 - puv.y), foam, shoreMix, shoreRgb);",
252 " coastEdge(u_edgeW, puv.x, puv.y, u_tileY * 0.6 + 2.9, u_shoreEdgeW,",
253 " vec2(1.0 - puv.x, puv.y), foam, shoreMix, shoreRgb);",
254 " coastEdge(u_edgeE, 1.0 - puv.x, puv.y, u_tileY * 0.8 + 4.1, u_shoreEdgeE,",
255 " vec2(1.0 - puv.x, puv.y), foam, shoreMix, shoreRgb);",
256 " rgb = mix(rgb, foamCol, clamp(foam, 0.0, 0.9));",
257 " rgb = mix(rgb, shoreRgb, clamp(shoreMix, 0.0, 1.0));",
207 " if (u_edgeN > 0.5) {",
208 " edgeProx = max(edgeProx, smoothstep(0.42, 0.0, puv.y));",
209 " sampleShore(1.0, u_shoreEdgeN, vec2(puv.x, 1.0 - puv.y), shoreRgb, shoreW);",
210 " }",
211 " if (u_edgeS > 0.5) {",
212 " edgeProx = max(edgeProx, smoothstep(0.42, 0.0, 1.0 - puv.y));",
213 " sampleShore(1.0, u_shoreEdgeS, vec2(puv.x, 1.0 - puv.y), shoreRgb, shoreW);",
214 " }",
215 " if (u_edgeW > 0.5) {",
216 " edgeProx = max(edgeProx, smoothstep(0.42, 0.0, puv.x));",
217 " sampleShore(1.0, u_shoreEdgeW, vec2(1.0 - puv.x, puv.y), shoreRgb, shoreW);",
218 " }",
219 " if (u_edgeE > 0.5) {",
220 " edgeProx = max(edgeProx, smoothstep(0.42, 0.0, 1.0 - puv.x));",
221 " sampleShore(1.0, u_shoreEdgeE, vec2(1.0 - puv.x, puv.y), shoreRgb, shoreW);",
222 " }",
223 " float lightAmt = smoothstep(0.42, 0.78, caustic);",
224 " float darkAmt = 1.0 - smoothstep(0.28, 0.58, caustic);",
225 " vec3 edgeBlue = mix(vec3(0.62, 0.88, 0.98), vec3(0.42, 0.70, 0.90), deepMix);",
226 " rgb = mix(rgb, edgeBlue, edgeProx * lightAmt * 0.85);",
227 " if (shoreW > 0.5) {",
228 " rgb = mix(rgb, shoreRgb, edgeProx * darkAmt * 0.92);",
229 " }",
258 230 " float litTop = mix(u_litNW, u_litNE, v_texCoord.x);",
259 231 " float litBottom = mix(u_litSW, u_litSE, v_texCoord.x);",
260 232 " float lit = mix(litTop, litBottom, v_texCoord.y);",
@@ -134,82 +134,44 @@ const WATER_GROUND_FRAG = [
134 "uniform float u_shoreEdgeS;",
134 "uniform float u_shoreEdgeS;",
135 "uniform float u_shoreEdgeW;",
135 "uniform float u_shoreEdgeW;",
136 "uniform vec4 u_shoreUvRect;",
136 "uniform vec4 u_shoreUvRect;",
137 "float hash21(vec2 p) {",
137 "vec2 rot2(vec2 p, float a) {",
138 " return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453123);",
138 " float c = cos(a);",
139 " float s = sin(a);",
140 " return vec2(c * p.x - s * p.y, s * p.x + c * p.y);",
139 "}",
141 "}",
140 "float vnoise(vec2 p) {",
142 // Wrap UV into (0,1) so CLAMP_TO_EDGE textures still tile when scrolled.
141 " vec2 i = floor(p);",
143 "vec2 wrapUv(vec2 uv) {",
142 " vec2 f = fract(p);",
144 " return fract(uv) * 0.998 + 0.001;",
143 " f = f * f * (3.0 - 2.0 * f);",
144 " float a = hash21(i);",
145 " float b = hash21(i + vec2(1.0, 0.0));",
146 " float c = hash21(i + vec2(0.0, 1.0));",
147 " float d = hash21(i + vec2(1.0, 1.0));",
148 " return mix(mix(a, b, f.x), mix(c, d, f.x), f.y);",
149 "}",
145 "}",
150 // dir: wave travel direction; steep Q in (0..1); amp; len wavelength in tiles; speed.
146 "float sampleShore(",
151 "void gerstner(vec2 world, vec2 dir, float steep, float amp, float len, float speed, float t,",
147 " float enabled, float useShore, vec2 shoreNu,",
152 " inout float h, inout float dx, inout float dy) {",
148 " inout vec3 shoreRgb, inout float shoreW",
153 " float invLen = 1.0 / max(len, 0.001);",
154 " float d2 = max(dot(dir, dir), 1e-5);",
155 " vec2 d = dir * inversesqrt(d2);",
156 " float k = 6.2831853 * invLen;",
157 " float phase = k * dot(d, world) - speed * t;",
158 " float s = sin(phase);",
159 " float c = cos(phase);",
160 " h += amp * c;",
161 " float qak = steep * amp * k;",
162 " dx -= d.x * qak * s;",
163 " dy -= d.y * qak * s;",
164 "}",
165 // Shore-normal traveling crest (foam line parallel to coast), not along-shore dots.
166 "void coastEdge(",
167 " float enabled, float dist, float along, float phaseOff, float useShore,",
168 " vec2 shoreNu, inout float foam, inout float shoreMix, inout vec3 shoreRgb",
169 ") {",
149 ") {",
170 " if (enabled < 0.5) { return; }",
150 " if (enabled < 0.5 || useShore < 0.5 || u_hasShore < 0.5) { return 0.0; }",
171 " float n = vnoise(vec2(along * 1.1 + phaseOff, floor(u_time * 0.2) + phaseOff));",
151 " vec2 shoreUv = u_shoreUvRect.xy + shoreNu * (u_shoreUvRect.zw - u_shoreUvRect.xy);",
172 " float frontWobble = 0.035 * sin(along * 6.28318 * 0.85 + phaseOff + u_time * 0.35);",
152 " vec4 shore = texture2D(u_secondary, shoreUv);",
173 " float travel = fract(u_time * 0.32 + along * 0.12 + phaseOff * 0.07 + n * 0.25);",
153 " if (shore.a <= 0.1) { return 0.0; }",
174 " float crestDist = mix(0.30, 0.015, smoothstep(0.0, 0.72, travel)) + frontWobble;",
154 " shoreRgb = shore.rgb;",
175 " float wash = smoothstep(0.72, 1.0, travel);",
155 " shoreW = 1.0;",
176 " float line = exp(-pow((dist - crestDist) * 26.0, 2.0));",
156 " return 1.0;",
177 " float sheet = (1.0 - smoothstep(0.0, crestDist + 0.05, dist)) * (1.0 - wash) * 0.22;",
178 " foam = max(foam, line * (0.75 + 0.25 * (1.0 - wash)) + sheet);",
179 " float retreat = wash * smoothstep(0.11, 0.0, dist);",
180 " if (retreat > 0.04 && useShore > 0.5 && u_hasShore > 0.5) {",
181 " vec2 shoreUv = u_shoreUvRect.xy + shoreNu * (u_shoreUvRect.zw - u_shoreUvRect.xy);",
182 " vec4 shore = texture2D(u_secondary, shoreUv);",
183 " if (shore.a > 0.1) {",
184 " shoreRgb = shore.rgb;",
185 " shoreMix = max(shoreMix, retreat);",
186 " }",
187 " }",
188 "}",
157 "}",
189 "void main() {",
158 "void main() {",
190 " vec2 px = floor(v_texCoord * 16.0) + 0.5;",
159 " vec2 px = floor(v_texCoord * 16.0) + 0.5;",
191 " vec2 puv = px / 16.0;",
160 " vec2 puv = px / 16.0;",
192 " vec2 world = vec2(u_tileX, u_tileY) + puv;",
161 " vec2 world = vec2(u_tileX, u_tileY) + puv;",
193 " float t = u_time;",
162 " float t = u_time;",
194 " float nSlow = vnoise(world * 0.55 + vec2(t * 0.04, -t * 0.03));",
163 // Three baked Perlin channels, scrolled at different angles / speeds, additive caustics.
195 " float nFine = vnoise(world * 2.4 + vec2(-t * 0.11, t * 0.09));",
164 " vec2 uvR = wrapUv(rot2(world * 0.42, 0.35) + vec2(t * 0.028, t * 0.019));",
196 " float texN = texture2D(u_texture, fract(world * 0.37)).r;",
165 " vec2 uvG = wrapUv(rot2(world * 0.51, -1.05) + vec2(-t * 0.022, t * 0.031));",
197 " float h = 0.0;",
166 " vec2 uvB = wrapUv(rot2(world * 0.63, 1.55) + vec2(t * 0.035, -t * 0.017));",
198 " float dx = 0.0;",
167 " float nR = texture2D(u_texture, uvR).r;",
199 " float dy = 0.0;",
168 " float nG = texture2D(u_texture, uvG).g;",
200 // Cardinal + diagonal crossings so no single oval stretch dominates.
169 " float nB = texture2D(u_texture, uvB).b;",
201 " gerstner(world, vec2(1.0, 0.0), 0.42, 0.20, 2.8, 1.05, t, h, dx, dy);",
170 " float caustic = nR * 0.45 + nG * 0.40 + nB * 0.35;",
202 " gerstner(world, vec2(0.0, 1.0), 0.42, 0.18, 2.6, 1.12, t, h, dx, dy);",
171 " caustic += nR * nG * 0.55 + nG * nB * 0.35;",
203 " gerstner(world, vec2(1.0, 0.35), 0.50, 0.16, 3.1, 1.18, t, h, dx, dy);",
172 " caustic = clamp(caustic * 0.72, 0.0, 1.0);",
204 " gerstner(world, vec2(-0.55, 1.0), 0.52, 0.15, 1.9, 1.48, t, h, dx, dy);",
173 " caustic = pow(caustic, 1.35);",
205 " gerstner(world, vec2(0.75, -0.85), 0.40, 0.11, 1.1, 2.05, t, h, dx, dy);",
174 // Soft shallow↔deep blend toward water neighbors (half-tile ramp).
206 " gerstner(world, vec2(-0.95, -0.35), 0.45, 0.12, 1.55, 1.72, t, h, dx, dy);",
207 " gerstner(world, vec2(0.4, -1.0), 0.38, 0.09, 0.95, 2.25, t, h, dx, dy);",
208 " h += (nSlow - 0.5) * 0.10 + (nFine - 0.5) * 0.05 + (texN - 0.5) * 0.04;",
209 " float slopeLit = clamp(0.55 + dx * 0.55 + dy * 0.28, 0.0, 1.0);",
210 " float crest = smoothstep(0.12, 0.72, h);",
211 " float trough = smoothstep(-0.05, -0.65, h);",
212 // Soft shallow↔deep blend toward water neighbors (full half-tile ramp).
213 " float deepMix = u_deep;",
175 " float deepMix = u_deep;",
214 " float blendW = 0.0;",
176 " float blendW = 0.0;",
215 " float blendAcc = 0.0;",
177 " float blendAcc = 0.0;",
@@ -231,30 +193,40 @@ const WATER_GROUND_FRAG = [
231 " }",
193 " }",
232 " if (blendW > 0.001) {",
194 " if (blendW > 0.001) {",
233 " float nbrDeep = clamp(blendAcc / blendW, 0.0, 1.0);",
195 " float nbrDeep = clamp(blendAcc / blendW, 0.0, 1.0);",
234 " float w = clamp(blendW, 0.0, 1.0);",
196 " deepMix = mix(u_deep, nbrDeep, clamp(blendW, 0.0, 1.0));",
235 " deepMix = mix(u_deep, nbrDeep, w);",
236 " }",
197 " }",
237 " vec3 shallowDeep = mix(vec3(0.16, 0.42, 0.62), vec3(0.09, 0.24, 0.40), deepMix);",
198 " vec3 deepCol = mix(vec3(0.12, 0.36, 0.56), vec3(0.06, 0.18, 0.34), deepMix);",
238 " vec3 shallowLit = mix(vec3(0.34, 0.66, 0.82), vec3(0.20, 0.45, 0.66), deepMix);",
199 " vec3 midCol = mix(vec3(0.22, 0.52, 0.70), vec3(0.12, 0.34, 0.54), deepMix);",
239 " vec3 troughCol = mix(vec3(0.10, 0.28, 0.44), vec3(0.05, 0.14, 0.28), deepMix);",
200 " vec3 litCol = mix(vec3(0.55, 0.82, 0.94), vec3(0.34, 0.60, 0.82), deepMix);",
240 " vec3 crestCol = mix(vec3(0.72, 0.88, 0.96), vec3(0.48, 0.70, 0.88), deepMix);",
201 " vec3 rgb = mix(deepCol, midCol, smoothstep(0.15, 0.55, caustic));",
241 " vec3 rgb = mix(shallowDeep, shallowLit, slopeLit);",
202 " rgb = mix(rgb, litCol, smoothstep(0.45, 0.92, caustic));",
242 " rgb = mix(rgb, troughCol, trough * 0.85);",
203 // Coast: bright caustic → push light-blue; dark → retreat into shore neighbor.
243 " rgb = mix(rgb, crestCol, crest * (0.35 + 0.55 * slopeLit));",
204 " float edgeProx = 0.0;",
244 " vec3 foamCol = vec3(0.82, 0.94, 1.0);",
205 " float shoreW = 0.0;",
245 " float foam = 0.0;",
246 " float shoreMix = 0.0;",
247 " vec3 shoreRgb = rgb;",
206 " vec3 shoreRgb = rgb;",
248 " coastEdge(u_edgeN, puv.y, puv.x, u_tileX * 0.7 + 0.1, u_shoreEdgeN,",
207 " if (u_edgeN > 0.5) {",
249 " vec2(puv.x, 1.0 - puv.y), foam, shoreMix, shoreRgb);",
208 " edgeProx = max(edgeProx, smoothstep(0.42, 0.0, puv.y));",
250 " coastEdge(u_edgeS, 1.0 - puv.y, puv.x, u_tileX * 0.5 + 1.7, u_shoreEdgeS,",
209 " sampleShore(1.0, u_shoreEdgeN, vec2(puv.x, 1.0 - puv.y), shoreRgb, shoreW);",
251 " vec2(puv.x, 1.0 - puv.y), foam, shoreMix, shoreRgb);",
210 " }",
252 " coastEdge(u_edgeW, puv.x, puv.y, u_tileY * 0.6 + 2.9, u_shoreEdgeW,",
211 " if (u_edgeS > 0.5) {",
253 " vec2(1.0 - puv.x, puv.y), foam, shoreMix, shoreRgb);",
212 " edgeProx = max(edgeProx, smoothstep(0.42, 0.0, 1.0 - puv.y));",
254 " coastEdge(u_edgeE, 1.0 - puv.x, puv.y, u_tileY * 0.8 + 4.1, u_shoreEdgeE,",
213 " sampleShore(1.0, u_shoreEdgeS, vec2(puv.x, 1.0 - puv.y), shoreRgb, shoreW);",
255 " vec2(1.0 - puv.x, puv.y), foam, shoreMix, shoreRgb);",
214 " }",
256 " rgb = mix(rgb, foamCol, clamp(foam, 0.0, 0.9));",
215 " if (u_edgeW > 0.5) {",
257 " rgb = mix(rgb, shoreRgb, clamp(shoreMix, 0.0, 1.0));",
216 " edgeProx = max(edgeProx, smoothstep(0.42, 0.0, puv.x));",
217 " sampleShore(1.0, u_shoreEdgeW, vec2(1.0 - puv.x, puv.y), shoreRgb, shoreW);",
218 " }",
219 " if (u_edgeE > 0.5) {",
220 " edgeProx = max(edgeProx, smoothstep(0.42, 0.0, 1.0 - puv.x));",
221 " sampleShore(1.0, u_shoreEdgeE, vec2(1.0 - puv.x, puv.y), shoreRgb, shoreW);",
222 " }",
223 " float lightAmt = smoothstep(0.42, 0.78, caustic);",
224 " float darkAmt = 1.0 - smoothstep(0.28, 0.58, caustic);",
225 " vec3 edgeBlue = mix(vec3(0.62, 0.88, 0.98), vec3(0.42, 0.70, 0.90), deepMix);",
226 " rgb = mix(rgb, edgeBlue, edgeProx * lightAmt * 0.85);",
227 " if (shoreW > 0.5) {",
228 " rgb = mix(rgb, shoreRgb, edgeProx * darkAmt * 0.92);",
229 " }",
258 " float litTop = mix(u_litNW, u_litNE, v_texCoord.x);",
230 " float litTop = mix(u_litNW, u_litNE, v_texCoord.x);",
259 " float litBottom = mix(u_litSW, u_litSE, v_texCoord.x);",
231 " float litBottom = mix(u_litSW, u_litSE, v_texCoord.x);",
260 " float lit = mix(litTop, litBottom, v_texCoord.y);",
232 " float lit = mix(litTop, litBottom, v_texCoord.y);",
shared/definitions/tiles.js +1 −1
@@ -136,7 +136,7 @@ export const GROUND_DEFINITIONS = Object.freeze({
136 136 frames: 1,
137 137 framesPerRow: 1,
138 138 animationSpeed: 0,
139 /** Per-tile procedural wave shader; never atlas-baked. */
139 /** Per-tile procedural caustic shader; never atlas-baked. */
140 140 proceduralWaves: true,
141 141 outdoor: true
142 142 }),
@@ -136,7 +136,7 @@ export const GROUND_DEFINITIONS = Object.freeze({
136 frames: 1,
136 frames: 1,
137 framesPerRow: 1,
137 framesPerRow: 1,
138 animationSpeed: 0,
138 animationSpeed: 0,
139 /** Per-tile procedural wave shader; never atlas-baked. */
139 /** Per-tile procedural caustic shader; never atlas-baked. */
140 proceduralWaves: true,
140 proceduralWaves: true,
141 outdoor: true
141 outdoor: true
142 }),
142 }),