Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
WaterSunShader.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
3#pragma once
4
5namespace esengine {
6
7// Forward lighting reads the refracted flux at the opaque receiver. Compare each bilinear
8// tap at its own depth so flux on a fish cannot leak through its silhouette to the bed.
9// Neutral flux is one. Interpolating the signed change keeps an empty beam dark.
10inline constexpr char WATER_SUN_GLSL[] = R"(
11#ifdef ES_SURFACE_3D
12precision highp float;
13precision highp int;
14uniform highp sampler2D u_waterSunField;
15void waterIncidentSun(inout Light lamp, vec3 position) {
16 float depth = u_waterDepth.x - position.y;
17 float reach = u_waterDepth.z - u_waterDepth.y;
18 vec2 uv = (position.xz + u_waterSun.xy * depth - u_waterRect.xy) / u_waterRect.z;
19 vec2 edge = min(uv, 1.0 - uv);
20 float fade = smoothstep(0.025, 0.09, min(edge.x, edge.y))
21 * (1.0 - smoothstep(reach - u_waterDepth.w, reach, depth));
22 if (fade <= 0.0 || depth < -u_waterDepth.y) return;
23 int n = textureSize(u_waterSunField, 0).y;
24 vec2 grid = uv * float(n) - 0.5;
25 ivec2 cell = ivec2(floor(grid));
26 vec2 fraction = fract(grid);
27 vec3 sun = -normalize(lamp.posDir.xyw);
28 vec3 bend = vec3(0.0);
29 float change = 0.0;
30 for (int i = 0; i < 4; ++i) {
31 ivec2 corner = ivec2(i & 1, i >> 1);
32 ivec2 at = clamp(cell + corner, ivec2(0), ivec2(n - 1));
33 vec4 hit = texelFetch(u_waterSunField, at + ivec2(n, 0), 0);
34 float gain = max((255.0 * hit.a - 128.0) * (4.0/127.0), 0.0);
35 if (gain <= 0.0) continue;
36 float z = 2.0 * dot(hit.rgb, vec3(1.0, 1.0/255.0, 1.0/65025.0)) - 1.0;
37 float behind = depth - (z * u_waterDepth.z - u_waterDepth.y);
38 float tolerance = 2.5 * u_waterRect.w;
39 if (behind < -tolerance) continue;
40 vec4 m = vec4(0.0);
41 if (behind <= tolerance) m = texelFetch(u_waterSunField, at, 0) * (255.0/32.0);
42 vec2 w = mix(1.0 - fraction, fraction, vec2(corner));
43 float weight = w.x * w.y;
44 // Unit gain reproduces measured flux. Higher artistic contrast preserves
45 // positive light between folds and cannot turn a lit patch into a clipped black plateau.
46 float contrast = (m.r - 1.0) / (m.r + 1.0);
47 float flux = mix(1.0, m.r, min(gain, 1.0)) * (1.0 + (1.0 - 1.0/max(gain, 1.0)) * contrast);
48 change += weight * (flux - 1.0);
49 if (m.r > 1e-4) {
50 vec2 h = clamp(2.0 * m.gb / m.r - 1.0, -1.0, 1.0);
51 vec3 ray = normalize(vec3(h.x, sqrt(max(1.0 - dot(h,h), 1e-5)), h.y));
52 bend += weight * min(gain, 1.0) * (ray - sun);
53 }
54 }
55 vec3 direction = normalize(sun + fade * bend);
56 lamp.posDir.xyw = -direction;
57 // The atlas measures flux per horizontal area. Convert to incident radiance
58 // before the receiver's own BRDF applies its actual normal and material response.
59 lamp.color.rgb *= max(0.0, 1.0 + fade * change) * sun.y / max(direction.y, 1e-5);
60}
61#endif
62)";
63
64inline constexpr char WATER_SUN_WGSL[] = R"(
65#ifdef ES_SURFACE_3D
66@group(1) @binding(32) var u_waterSunField : texture_2d<f32>;
67fn waterIncidentSun(input : Light, position : vec3f) -> Light {
68 var lamp = input;
69 let depth = lc.u_waterDepth.x - position.y;
70 let reach = lc.u_waterDepth.z - lc.u_waterDepth.y;
71 let uv = (position.xz + lc.u_waterSun.xy * depth - lc.u_waterRect.xy) / lc.u_waterRect.z;
72 let edge = min(uv, 1.0 - uv);
73 let fade = smoothstep(0.025, 0.09, min(edge.x, edge.y))
74 * (1.0 - smoothstep(reach - lc.u_waterDepth.w, reach, depth));
75 if (fade <= 0.0 || depth < -lc.u_waterDepth.y) { return lamp; }
76 let n = i32(textureDimensions(u_waterSunField, 0).y);
77 let grid = uv * f32(n) - 0.5;
78 let cell = vec2i(floor(grid));
79 let fraction = fract(grid);
80 let sun = -normalize(lamp.posDir.xyw);
81 var bend = vec3f(0.0);
82 var change = 0.0;
83 for (var i = 0; i < 4; i++) {
84 let corner = vec2i(i & 1, i >> 1u);
85 let at = clamp(cell + corner, vec2i(0), vec2i(n - 1));
86 let hit = textureLoad(u_waterSunField, at + vec2i(n, 0), 0);
87 let gain = max((255.0 * hit.a - 128.0) * (4.0/127.0), 0.0);
88 if (gain <= 0.0) { continue; }
89 let z = 2.0 * dot(hit.rgb, vec3f(1.0, 1.0/255.0, 1.0/65025.0)) - 1.0;
90 let behind = depth - (z * lc.u_waterDepth.z - lc.u_waterDepth.y);
91 let tolerance = 2.5 * lc.u_waterRect.w;
92 if (behind < -tolerance) { continue; }
93 var m = vec4f(0.0);
94 if (behind <= tolerance) { m = textureLoad(u_waterSunField, at, 0) * (255.0/32.0); }
95 let w = mix(1.0 - fraction, fraction, vec2f(corner));
96 let weight = w.x * w.y;
97 let contrast = (m.r - 1.0) / (m.r + 1.0);
98 let flux = mix(1.0, m.r, min(gain, 1.0)) * (1.0 + (1.0 - 1.0/max(gain, 1.0)) * contrast);
99 change += weight * (flux - 1.0);
100 if (m.r > 1e-4) {
101 let h = clamp(2.0 * m.gb / m.r - 1.0, vec2f(-1), vec2f(1));
102 let ray = normalize(vec3f(h.x, sqrt(max(1.0 - dot(h,h), 1e-5)), h.y));
103 bend += weight * min(gain, 1.0) * (ray - sun);
104 }
105 }
106 let direction = normalize(sun + fade * bend);
107 lamp.posDir = vec4f(-direction.xy, lamp.posDir.z, -direction.z);
108 lamp.color = vec4f(lamp.color.rgb * (max(0.0, 1.0 + fade * change) * sun.y / max(direction.y, 1e-5)), lamp.color.a);
109 return lamp;
110}
111#endif
112)";
113} // namespace esengine
constexpr char WATER_SUN_GLSL[]
constexpr char WATER_SUN_WGSL[]