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.

perf(terrain): the Terrain shader reads only the layers the ground has, and the turned second reading only near the eye

yhh7c8294f8bbf7
diff --git a/sdk/src/terrain/terrainShader.ts b/sdk/src/terrain/terrainShader.ts
index e4855ccb3..8f1683b92 100644
--- a/sdk/src/terrain/terrainShader.ts
+++ b/sdk/src/terrain/terrainShader.ts
@@ -82,6 +82,17 @@ float tnNoise(vec2 p) {
// The second reading of a layer: turned, on a tile 1.37 times as wide, offset.
vec2 tnTurned(vec2 p, float tile) { return mat2(0.8, -0.6, 0.6, 0.8) * p / (tile * 1.37) + vec2(0.37, 0.71); }
float tnLuma(vec3 c) { return dot(c, vec3(0.299, 0.587, 0.114)); }
+// A layer at p on its tile, with the second turned reading mixed in by t. The gradients are the ones taken where every lane runs,
+// so a layer can be read only where it is used.
+vec3 tnRead(sampler2D layer, vec2 p, float tile, float t, vec2 gx, vec2 gy) {
+ vec3 c = textureGrad(layer, p / tile, gx / tile, gy / tile).rgb;
+ if (t > 0.01) {
+ mat2 turn = mat2(0.8, -0.6, 0.6, 0.8);
+ float wide = tile * 1.37;
+ c = mix(c, textureGrad(layer, tnTurned(p, tile), turn * gx / wide, turn * gy / wide).rgb, t);
+ }
+ return c;
+}
void main() {
#ifdef MESH_NORMALS
@@ -91,25 +102,30 @@ void main() {
highp vec3 P = vec3(v_worldPos.x, 0.0, v_worldPos.y);
highp vec3 N = vec3(0.0, 1.0, 0.0);
#endif
- // Every layer read twice and blended by slow noise of its own tile's scale, so neither repeat is seen.
- float t0 = smoothstep(0.3, 0.7, tnNoise(P.xz / (u_tile0 * 9.0))) * u_antiTile;
- float t1 = smoothstep(0.3, 0.7, tnNoise(P.xz / (u_tile1 * 9.0) + 17.0)) * u_antiTile;
- float t2 = smoothstep(0.3, 0.7, tnNoise(P.xz / (u_tile2 * 9.0) + 31.0)) * u_antiTile;
- float t3 = smoothstep(0.3, 0.7, tnNoise(P.xz / (u_tile3 * 9.0) + 47.0)) * u_antiTile;
- vec3 s0 = mix(texture(u_layer0, P.xz / u_tile0).rgb, texture(u_layer0, tnTurned(P.xz, u_tile0)).rgb, t0);
- vec3 s1 = mix(texture(u_layer1, P.xz / u_tile1).rgb, texture(u_layer1, tnTurned(P.xz, u_tile1)).rgb, t1);
- vec3 s2 = mix(texture(u_layer2, P.xz / u_tile2).rgb, texture(u_layer2, tnTurned(P.xz, u_tile2)).rgb, t2);
- // Rock is seen from three sides, in proportion to how the ground faces them, so a sheer face is not stretched.
- vec3 axis = pow(abs(N), vec3(4.0));
- axis /= dot(axis, vec3(1.0));
- vec3 rockTop = mix(texture(u_layer3, P.xz / u_tile3).rgb, texture(u_layer3, tnTurned(P.xz, u_tile3)).rgb, t3);
- vec3 s3 = texture(u_layer3, P.zy / u_tile3).rgb * axis.x + rockTop * axis.y + texture(u_layer3, P.xy / u_tile3).rgb * axis.z;
-
vec4 w = max(v_color, vec4(0.0));
w /= max(dot(w, vec4(1.0)), 1e-4);
// The strongest layer wins its seam more cleanly the higher the sharpness.
w = pow(w, vec4(u_sharpness));
w /= max(dot(w, vec4(1.0)), 1e-4);
+
+ // Only the layers the ground has are read, from gradients taken here, where every lane runs. Near the eye each is read a second time
+ // and blended by slow noise of its own tile's scale, so no repeat is seen; far off, where a repeat is too small to see, it is not.
+ vec3 dpx = dFdx(P), dpy = dFdy(P);
+ float eye = u_camera.w > 0.5 ? length(P - u_camera.xyz) : 0.0;
+ float near = (1.0 - smoothstep(u_tile0 * 12.0, u_tile0 * 30.0, eye)) * u_antiTile;
+ vec3 s0 = vec3(0.0), s1 = vec3(0.0), s2 = vec3(0.0), s3 = vec3(0.0);
+ if (w.x > 1e-3) s0 = tnRead(u_layer0, P.xz, u_tile0, smoothstep(0.3, 0.7, tnNoise(P.xz / (u_tile0 * 9.0))) * near, dpx.xz, dpy.xz);
+ if (w.y > 1e-3) s1 = tnRead(u_layer1, P.xz, u_tile1, smoothstep(0.3, 0.7, tnNoise(P.xz / (u_tile1 * 9.0) + 17.0)) * near, dpx.xz, dpy.xz);
+ if (w.z > 1e-3) s2 = tnRead(u_layer2, P.xz, u_tile2, smoothstep(0.3, 0.7, tnNoise(P.xz / (u_tile2 * 9.0) + 31.0)) * near, dpx.xz, dpy.xz);
+ if (w.w > 1e-3) {
+ // Rock is seen from three sides, in proportion to how the ground faces them, so a sheer face is not stretched; a side that
+ // carries almost none of it is not read.
+ vec3 axis = pow(abs(N), vec3(4.0));
+ axis /= dot(axis, vec3(1.0));
+ s3 = tnRead(u_layer3, P.xz, u_tile3, smoothstep(0.3, 0.7, tnNoise(P.xz / (u_tile3 * 9.0) + 47.0)) * near, dpx.xz, dpy.xz) * axis.y;
+ if (axis.x > 0.02) s3 += textureGrad(u_layer3, P.zy / u_tile3, dpx.zy / u_tile3, dpy.zy / u_tile3).rgb * axis.x;
+ if (axis.z > 0.02) s3 += textureGrad(u_layer3, P.xy / u_tile3, dpx.xy / u_tile3, dpy.xy / u_tile3).rgb * axis.z;
+ }
// Then by relief: each layer stands as high as it is bright, and where two meet the higher one shows.
vec4 heights = vec4(tnLuma(s0), tnLuma(s1), tnLuma(s2), tnLuma(s3));
vec4 stand = heights + w * 1.5;
@@ -124,7 +140,6 @@ void main() {
// The relief, as a bump from the screen-space change of its height, faded out before it is finer than a pixel.
float relief = dot(heights, w) * u_relief;
- vec3 dpx = dFdx(P), dpy = dFdy(P);
float dhx = dFdx(relief), dhy = dFdy(relief);
vec3 r1 = cross(dpy, N), r2 = cross(N, dpx);
float det = dot(dpx, r1);
@@ -162,6 +177,15 @@ fn tnNoise(p : vec2f) -> f32 {
}
fn tnTurned(p : vec2f, tile : f32) -> vec2f { return mat2x2f(0.8, -0.6, 0.6, 0.8) * p / (tile * 1.37) + vec2f(0.37, 0.71); }
fn tnLuma(c : vec3f) -> f32 { return dot(c, vec3f(0.299, 0.587, 0.114)); }
+fn tnRead(layer : texture_2d<f32>, smp : sampler, p : vec2f, tile : f32, t : f32, gx : vec2f, gy : vec2f) -> vec3f {
+ var c = textureSampleGrad(layer, smp, p / tile, gx / tile, gy / tile).rgb;
+ if (t > 0.01) {
+ let turn = mat2x2f(0.8, -0.6, 0.6, 0.8);
+ let wide = tile * 1.37;
+ c = mix(c, textureSampleGrad(layer, smp, tnTurned(p, tile), turn * gx / wide, turn * gy / wide).rgb, vec3f(t));
+ }
+ return c;
+}
#ifdef ES_GBUFFER
struct FragOut {
@@ -192,25 +216,31 @@ fn packNormal(n : vec3f) -> vec2f {
let P = vec3f(v.v_worldPos.x, 0.0, v.v_worldPos.y);
let N = vec3f(0.0, 1.0, 0.0);
#endif
- // Every sample and every derivative is taken here, in uniform control flow.
- let t0 = smoothstep(0.3, 0.7, tnNoise(P.xz / (mc.u_tile0 * 9.0))) * mc.u_antiTile;
- let t1 = smoothstep(0.3, 0.7, tnNoise(P.xz / (mc.u_tile1 * 9.0) + 17.0)) * mc.u_antiTile;
- let t2 = smoothstep(0.3, 0.7, tnNoise(P.xz / (mc.u_tile2 * 9.0) + 31.0)) * mc.u_antiTile;
- let t3 = smoothstep(0.3, 0.7, tnNoise(P.xz / (mc.u_tile3 * 9.0) + 47.0)) * mc.u_antiTile;
- let s0 = mix(textureSample(u_layer0, u_layer0_s, P.xz / mc.u_tile0).rgb, textureSample(u_layer0, u_layer0_s, tnTurned(P.xz, mc.u_tile0)).rgb, vec3f(t0));
- let s1 = mix(textureSample(u_layer1, u_layer1_s, P.xz / mc.u_tile1).rgb, textureSample(u_layer1, u_layer1_s, tnTurned(P.xz, mc.u_tile1)).rgb, vec3f(t1));
- let s2 = mix(textureSample(u_layer2, u_layer2_s, P.xz / mc.u_tile2).rgb, textureSample(u_layer2, u_layer2_s, tnTurned(P.xz, mc.u_tile2)).rgb, vec3f(t2));
- let rockX = textureSample(u_layer3, u_layer3_s, P.zy / mc.u_tile3).rgb;
- let rockTop = mix(textureSample(u_layer3, u_layer3_s, P.xz / mc.u_tile3).rgb, textureSample(u_layer3, u_layer3_s, tnTurned(P.xz, mc.u_tile3)).rgb, vec3f(t3));
- let rockZ = textureSample(u_layer3, u_layer3_s, P.xy / mc.u_tile3).rgb;
- var axis = pow(abs(N), vec3f(4.0));
- axis = axis / dot(axis, vec3f(1.0));
- let s3 = rockX * axis.x + rockTop * axis.y + rockZ * axis.z;
-
var w = max(v.v_color, vec4f(0.0));
w = w / max(dot(w, vec4f(1.0)), 1e-4);
w = pow(w, vec4f(mc.u_sharpness));
w = w / max(dot(w, vec4f(1.0)), 1e-4);
+
+ // Derivatives taken here, in uniform control flow; the layers are read only where the ground has them.
+ let dpx = dpdx(P);
+ let dpy = dpdy(P);
+ var eye = 0.0;
+ if (frame.camera.w > 0.5) { eye = length(P - frame.camera.xyz); }
+ let near = (1.0 - smoothstep(mc.u_tile0 * 12.0, mc.u_tile0 * 30.0, eye)) * mc.u_antiTile;
+ var s0 = vec3f(0.0);
+ var s1 = vec3f(0.0);
+ var s2 = vec3f(0.0);
+ var s3 = vec3f(0.0);
+ if (w.x > 1e-3) { s0 = tnRead(u_layer0, u_layer0_s, P.xz, mc.u_tile0, smoothstep(0.3, 0.7, tnNoise(P.xz / (mc.u_tile0 * 9.0))) * near, dpx.xz, dpy.xz); }
+ if (w.y > 1e-3) { s1 = tnRead(u_layer1, u_layer1_s, P.xz, mc.u_tile1, smoothstep(0.3, 0.7, tnNoise(P.xz / (mc.u_tile1 * 9.0) + 17.0)) * near, dpx.xz, dpy.xz); }
+ if (w.z > 1e-3) { s2 = tnRead(u_layer2, u_layer2_s, P.xz, mc.u_tile2, smoothstep(0.3, 0.7, tnNoise(P.xz / (mc.u_tile2 * 9.0) + 31.0)) * near, dpx.xz, dpy.xz); }
+ if (w.w > 1e-3) {
+ var axis = pow(abs(N), vec3f(4.0));
+ axis = axis / dot(axis, vec3f(1.0));
+ s3 = tnRead(u_layer3, u_layer3_s, P.xz, mc.u_tile3, smoothstep(0.3, 0.7, tnNoise(P.xz / (mc.u_tile3 * 9.0) + 47.0)) * near, dpx.xz, dpy.xz) * axis.y;
+ if (axis.x > 0.02) { s3 = s3 + textureSampleGrad(u_layer3, u_layer3_s, P.zy / mc.u_tile3, dpx.zy / mc.u_tile3, dpy.zy / mc.u_tile3).rgb * axis.x; }
+ if (axis.z > 0.02) { s3 = s3 + textureSampleGrad(u_layer3, u_layer3_s, P.xy / mc.u_tile3, dpx.xy / mc.u_tile3, dpy.xy / mc.u_tile3).rgb * axis.z; }
+ }
let heights = vec4f(tnLuma(s0), tnLuma(s1), tnLuma(s2), tnLuma(s3));
let stand = heights + w * 1.5;
let top = max(max(stand.x, stand.y), max(stand.z, stand.w)) - 0.35;
@@ -223,8 +253,6 @@ fn packNormal(n : vec3f) -> vec2f {
albedo = albedo * mix(1.0, 0.7 + 0.6 * broad, mc.u_macroVariation);
let relief = dot(heights, w) * mc.u_relief;
- let dpx = dpdx(P);
- let dpy = dpdy(P);
let dhx = dpdx(relief);
let dhy = dpdy(relief);
let r1 = cross(dpy, N);
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index 0ce1f6d64..679b4958e 100644
--- a/tools/releaseNotes.mjs
+++ b/tools/releaseNotes.mjs
@@ -2738,4 +2738,8 @@ export const NOTED = {
{"internal":"Console noise in the editor (a 404 and a duplicate-key warning); nothing a game does changes."},
"fix(render): buffers growing to fit a frame log at debug, and a pass seats an input only where it reads one":
{"internal":"Log levels and a guarded uniform write: the console no longer warns about normal buffer growth or a sky pass with no input texture; nothing drawn changes."},
+ "perf(terrain): the Terrain shader reads only the layers the ground has, and the turned second reading only near the eye":
+ {"note":"A richer Terrain shader."},
+ "perf(examples): Terrain & Water grows its plants in fewer, larger cells and draws its ground at a lower detail":
+ {"internal":"An example's own settings."},
};