Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

fix(water): a reflection's step and the thickness of what it meets are shares of the distance, not a number of units, and the sky in the copy of distances is as far as the copy can say

yhh4e3f6af864fd
diff --git a/sdk/src/water/waterShader.ts b/sdk/src/water/waterShader.ts
index af55b929b..7f5e04d48 100644
--- a/sdk/src/water/waterShader.ts
+++ b/sdk/src/water/waterShader.ts
@@ -574,7 +574,8 @@ float waterSunVisibility(vec3 worldPos, int first, int count) {
/// What a ray from p along r meets in the picture of the scene the frame copied, as a colour with alpha 1, or alpha 0 where it meets nothing on screen.
vec4 waterReflect(vec3 p, vec3 r, float eyeDist) {
- float stride = max(0.5, eyeDist * 0.03);
+ // Every length here is a share of how far off the water is: a step, and how thick what the ray meets is taken to be. None is a number of units.
+ float stride = max(eyeDist * 0.03, 1e-4);
float travelled = stride;
for (int i = 0; i < 20; ++i) {
vec3 q = p + r * travelled;
@@ -589,7 +590,7 @@ vec4 waterReflect(vec3 p, vec3 r, float eyeDist) {
// The ray has gone behind something the picture holds: that is what it met, if it did not skip clean through a thin one.
// Fading toward the picture's edge: a reflection that runs off screen thins away instead of stopping on a line.
vec2 edge = min(uv, 1.0 - uv);
- if (scene.g < 0.5 && sceneDist < qDist && qDist - sceneDist < stride * 2.0 + 1.0) return vec4(textureLod(u_sceneColor, uv, 0.0).rgb, smoothstep(0.0, 0.08, min(edge.x, edge.y)));
+ if (scene.g < 0.5 && sceneDist < qDist && qDist - sceneDist < stride * 2.0 + qDist * 0.002) return vec4(textureLod(u_sceneColor, uv, 0.0).rgb, smoothstep(0.0, 0.08, min(edge.x, edge.y)));
stride *= 1.25;
travelled += stride;
}
@@ -1098,7 +1099,7 @@ fn waterSunVisibility(worldPos : vec3f, first : i32, count : i32) -> f32 {
/// What a ray from p along r meets in the picture of the scene the frame copied: the colour (xyz) and 1 (w), or w 0 where it meets nothing on screen.
fn waterReflect(p : vec3f, r : vec3f, eyeDist : f32) -> vec4f {
- var stride = max(0.5, eyeDist * 0.03);
+ var stride = max(eyeDist * 0.03, 1e-4);
var travelled = stride;
for (var i = 0; i < 20; i++) {
let q = p + r * travelled;
@@ -1111,7 +1112,7 @@ fn waterReflect(p : vec3f, r : vec3f, eyeDist : f32) -> vec4f {
let scene = textureSampleLevel(u_sceneDepthMap, u_sceneDepthMap_s, uv, 0.0).rg;
let sceneDist = scene.r;
let edge = min(uv, vec2f(1.0) - uv);
- if (scene.g < 0.5 && sceneDist < qDist && qDist - sceneDist < stride * 2.0 + 1.0) { return vec4f(textureSampleLevel(u_sceneColor, u_sceneColor_s, uv, 0.0).rgb, smoothstep(0.0, 0.08, min(edge.x, edge.y))); }
+ if (scene.g < 0.5 && sceneDist < qDist && qDist - sceneDist < stride * 2.0 + qDist * 0.002) { return vec4f(textureSampleLevel(u_sceneColor, u_sceneColor_s, uv, 0.0).rgb, smoothstep(0.0, 0.08, min(edge.x, edge.y))); }
stride = stride * 1.25;
travelled = travelled + stride;
}
diff --git a/src/esengine/data/shaders/blit.esshader b/src/esengine/data/shaders/blit.esshader
index c5cc4cee0..82c7bfca7 100644
--- a/src/esengine/data/shaders/blit.esshader
+++ b/src/esengine/data/shaders/blit.esshader
@@ -68,13 +68,13 @@ vec3 displayLookup(vec3 c) {
void main() {
#ifdef ES_COPY_DEPTH
// How far each pixel of the opaque scene is from the eye, in world units, for a surface that measures the column of water or
- // glass in front of it. A sky pixel is far, and says so in g: its colour in the copy is not the sky's (that is painted later), so a
+ // glass in front of it. A sky pixel is as far as the copy can say (the largest number its 16-bit floats hold), and says so in g: its colour in the copy is not the sky's (that is painted later), so a
// reflection must not take it for something met. Alpha 0.5 is the same "a real copy" mark as the colour copy's.
ivec2 depthSize = textureSize(u_sceneDepth, 0);
float rawDepth = texelFetch(u_sceneDepth, clamp(ivec2(v_texCoord * vec2(depthSize)), ivec2(0), depthSize - ivec2(1)), 0).r;
vec3 scenePoint = worldFromDepth(v_texCoord, rawDepth);
float eyeDistance = u_camera.w > 0.5 ? length(scenePoint - u_camera.xyz) : -dot(scenePoint, u_camera.xyz);
- fragColor = vec4(rawDepth >= 1.0 ? 60000.0 : eyeDistance, rawDepth >= 1.0 ? 1.0 : 0.0, 0.0, 0.5);
+ fragColor = vec4(rawDepth >= 1.0 ? 65504.0 : eyeDistance, rawDepth >= 1.0 ? 1.0 : 0.0, 0.0, 0.5);
return;
#endif
#ifdef ES_UPSCALE
@@ -185,7 +185,7 @@ fn displayLookup(c : vec3f) -> vec3f {
let scenePoint = worldFromDepth(v.v_texCoord, rawDepth);
var eyeDistance = -dot(scenePoint, frame.camera.xyz);
if (frame.camera.w > 0.5) { eyeDistance = length(scenePoint - frame.camera.xyz); }
- return vec4f(select(eyeDistance, 60000.0, rawDepth >= 1.0), select(0.0, 1.0, rawDepth >= 1.0), 0.0, 0.5);
+ return vec4f(select(eyeDistance, 65504.0, rawDepth >= 1.0), select(0.0, 1.0, rawDepth >= 1.0), 0.0, 0.5);
#endif
#ifdef ES_UPSCALE
var c = upscale(v.v_texCoord);
diff --git a/src/esengine/renderer/rhi/ShaderEmbeds.generated.hpp b/src/esengine/renderer/rhi/ShaderEmbeds.generated.hpp
index 1c1d89f98..a5914663e 100644
--- a/src/esengine/renderer/rhi/ShaderEmbeds.generated.hpp
+++ b/src/esengine/renderer/rhi/ShaderEmbeds.generated.hpp
@@ -343,7 +343,7 @@ void main() {
float rawDepth = texelFetch(u_sceneDepth, clamp(ivec2(v_texCoord * vec2(depthSize)), ivec2(0), depthSize - ivec2(1)), 0).r;
vec3 scenePoint = worldFromDepth(v_texCoord, rawDepth);
float eyeDistance = u_camera.w > 0.5 ? length(scenePoint - u_camera.xyz) : -dot(scenePoint, u_camera.xyz);
- fragColor = vec4(rawDepth >= 1.0 ? 60000.0 : eyeDistance, rawDepth >= 1.0 ? 1.0 : 0.0, 0.0, 0.5);
+ fragColor = vec4(rawDepth >= 1.0 ? 65504.0 : eyeDistance, rawDepth >= 1.0 ? 1.0 : 0.0, 0.0, 0.5);
return;
#endif
#ifdef ES_UPSCALE
@@ -454,7 +454,7 @@ fn displayLookup(c : vec3f) -> vec3f {
let scenePoint = worldFromDepth(v.v_texCoord, rawDepth);
var eyeDistance = -dot(scenePoint, frame.camera.xyz);
if (frame.camera.w > 0.5) { eyeDistance = length(scenePoint - frame.camera.xyz); }
- return vec4f(select(eyeDistance, 60000.0, rawDepth >= 1.0), select(0.0, 1.0, rawDepth >= 1.0), 0.0, 0.5);
+ return vec4f(select(eyeDistance, 65504.0, rawDepth >= 1.0), select(0.0, 1.0, rawDepth >= 1.0), 0.0, 0.5);
#endif
#ifdef ES_UPSCALE
var c = upscale(v.v_texCoord);