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.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
feat(terrain): the Terrain shader meets its layers by relief, hides their tiling and wets the waterline
yhh7e4d6730a74f
diff --git a/sdk/src/terrain/terrainShader.ts b/sdk/src/terrain/terrainShader.tsindex dcc2a4f34..e4855ccb3 100644--- a/sdk/src/terrain/terrainShader.ts+++ b/sdk/src/terrain/terrainShader.ts@@ -30,8 +30,18 @@ const TERRAIN = `#pragma shader "Terrain"#pragma param u_tile3 float default(40) range(1,800) ui(slider)// How much a layer with more weight takes over from the others at the seam: 1 is a plain blend, higher a harder edge.#pragma param u_sharpness float default(1.6) range(1,8) ui(slider)-// The wide, slow variation laid over the first layer so a big meadow does not read as one repeated tile.+// How much the seams follow the layers' own relief (their brightness): rock shows through the gaps in the grass before the grass gives way,+// and sand fills the low places between stones. 0 is a plain blend of the weights.+#pragma param u_heightBlend float default(0.7) range(0,1) ui(slider)+// How much of each layer is read a second time, turned and on a wider tile, and blended in by slow noise, so no repeat of it is seen across a field.+#pragma param u_antiTile float default(0.6) range(0,1) ui(slider)+// The wide, slow variation in brightness laid over every layer so a big meadow is not one colour.#pragma param u_macroVariation float default(0.35) range(0,1) ui(slider)+// How deep, in world units, the layers' relief is drawn into the light (their brightness taken as height); 0 leaves the ground smooth.+#pragma param u_relief float default(0.05) range(0,100000) ui(slider)+// Ground at the water: below u_wetLevel + u_wetBand (world heights) it darkens and shines, most at the waterline. A band of 0 draws none.+#pragma param u_wetLevel float default(0) range(-100000,100000) ui(slider)+#pragma param u_wetBand float default(0) range(0,100000) ui(slider)#pragma param u_roughness float default(0.92) range(0,1) ui(slider)#pragma fragment@@ -59,6 +69,20 @@ vec2 packNormal(vec3 n) {out vec4 fragColor;#endif+float tnHash(vec2 p) {+ p = fract(p * vec2(0.1031, 0.1030));+ p += dot(p, p.yx + 33.33);+ return fract((p.x + p.y) * p.x);+}+float tnNoise(vec2 p) {+ vec2 i = floor(p), f = fract(p);+ f = f * f * (3.0 - 2.0 * f);+ return mix(mix(tnHash(i), tnHash(i + vec2(1.0, 0.0)), f.x), mix(tnHash(i + vec2(0.0, 1.0)), tnHash(i + vec2(1.0, 1.0)), f.x), f.y);+}+// 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)); }+void main() {#ifdef MESH_NORMALShighp vec3 P = v_worldXYZ;@@ -67,35 +91,78 @@ 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);+ // 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;+ float top = max(max(stand.x, stand.y), max(stand.z, stand.w)) - 0.35;+ vec4 hw = max(stand - top, vec4(0.0)) * step(vec4(1e-3), w);+ hw /= max(dot(hw, vec4(1.0)), 1e-4);+ w = mix(w, hw, u_heightBlend);- vec3 c0 = texture(u_layer0, P.xz / u_tile0).rgb * u_tint0.rgb;- c0 *= mix(1.0, texture(u_layer0, P.xz / (u_tile0 * 7.3)).r * 1.8, u_macroVariation);- vec3 c1 = texture(u_layer1, P.xz / u_tile1).rgb * u_tint1.rgb;- vec3 c2 = texture(u_layer2, P.xz / u_tile2).rgb * u_tint2.rgb;- // 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 c3 = (texture(u_layer3, P.zy / u_tile3).rgb * axis.x- + texture(u_layer3, P.xz / u_tile3).rgb * axis.y- + texture(u_layer3, P.xy / u_tile3).rgb * axis.z) * u_tint3.rgb;- vec3 albedo = c0 * w.x + c1 * w.y + c2 * w.z + c3 * w.w;+ vec3 albedo = s0 * u_tint0.rgb * w.x + s1 * u_tint1.rgb * w.y + s2 * u_tint2.rgb * w.z + s3 * u_tint3.rgb * w.w;+ float broad = tnNoise(P.xz / (u_tile0 * 7.3)) * 0.6 + tnNoise(P.xz / (u_tile0 * 29.0) + 5.0) * 0.4;+ albedo *= mix(1.0, 0.7 + 0.6 * broad, u_macroVariation);++ // 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);+ vec3 surfGrad = sign(det) * (dhx * r1 + dhy * r2);+ float fine = 1.0 - smoothstep(0.02, 0.2, length(dpx) / max(u_tile0, 1e-4));+ vec3 Nb = normalize(abs(det) * N - surfGrad * fine);+ if (abs(det) > 1e-12 && u_relief > 0.0) N = Nb;++ // Wet at the water: darker, and smooth enough to shine.+ float wet = u_wetBand > 0.0 ? 1.0 - smoothstep(u_wetLevel, u_wetLevel + u_wetBand, P.y) : 0.0;+ albedo *= mix(1.0, 0.55, wet);+ float roughness = mix(u_roughness, 0.3, wet);SurfaceExt ext = surfaceExtNone(N);vec3 viewDir = viewDirection(P);- vec3 lit = applyLightingSurface(albedo, N, P, viewDir, 0.0, u_roughness, 0.5, 1.0, ext);+ vec3 lit = applyLightingSurface(albedo, N, P, viewDir, 0.0, roughness, 0.5, 1.0, ext);fragColor = vec4(lit, 1.0);#ifdef ES_GBUFFER- fragSurface = vec4(packNormal(N), u_roughness, 0.75);+ fragSurface = vec4(packNormal(N), roughness, 0.75);#endif}#pragma end#pragma fragment wgsl+fn tnHash(p0 : vec2f) -> f32 {+ var p = fract(p0 * vec2f(0.1031, 0.1030));+ p = p + dot(p, p.yx + 33.33);+ return fract((p.x + p.y) * p.x);+}+fn tnNoise(p : vec2f) -> f32 {+ let i = floor(p);+ var f = fract(p);+ f = f * f * (3.0 - 2.0 * f);+ return mix(mix(tnHash(i), tnHash(i + vec2f(1.0, 0.0)), f.x), mix(tnHash(i + vec2f(0.0, 1.0)), tnHash(i + vec2f(1.0, 1.0)), f.x), f.y);+}+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)); }+#ifdef ES_GBUFFERstruct FragOut {@location(0) color : vec4f,@@ -125,35 +192,62 @@ 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 is taken here, in uniform control flow, where a derivative is allowed.- let layer0 = textureSample(u_layer0, u_layer0_s, P.xz / mc.u_tile0).rgb;- let macro0 = textureSample(u_layer0, u_layer0_s, P.xz / (mc.u_tile0 * 7.3)).r;- let layer1 = textureSample(u_layer1, u_layer1_s, P.xz / mc.u_tile1).rgb;- let layer2 = textureSample(u_layer2, u_layer2_s, P.xz / mc.u_tile2).rgb;+ // 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 rockY = textureSample(u_layer3, u_layer3_s, P.xz / 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);+ 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;+ var hw = max(stand - vec4f(top), vec4f(0.0)) * step(vec4f(1e-3), w);+ hw = hw / max(dot(hw, vec4f(1.0)), 1e-4);+ w = mix(w, hw, vec4f(mc.u_heightBlend));- let c0 = layer0 * mc.u_tint0.rgb * mix(1.0, macro0 * 1.8, mc.u_macroVariation);- let c1 = layer1 * mc.u_tint1.rgb;- let c2 = layer2 * mc.u_tint2.rgb;- var axis = pow(abs(N), vec3f(4.0));- axis = axis / dot(axis, vec3f(1.0));- let c3 = (rockX * axis.x + rockY * axis.y + rockZ * axis.z) * mc.u_tint3.rgb;- let albedo = c0 * w.x + c1 * w.y + c2 * w.z + c3 * w.w;+ var albedo = s0 * mc.u_tint0.rgb * w.x + s1 * mc.u_tint1.rgb * w.y + s2 * mc.u_tint2.rgb * w.z + s3 * mc.u_tint3.rgb * w.w;+ let broad = tnNoise(P.xz / (mc.u_tile0 * 7.3)) * 0.6 + tnNoise(P.xz / (mc.u_tile0 * 29.0) + 5.0) * 0.4;+ 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);+ let r2 = cross(N, dpx);+ let det = dot(dpx, r1);+ let surfGrad = sign(det) * (dhx * r1 + dhy * r2);+ let fine = 1.0 - smoothstep(0.02, 0.2, length(dpx) / max(mc.u_tile0, 1e-4));+ let Nb = normalize(abs(det) * N - surfGrad * fine);+ var Ns = N;+ if (abs(det) > 1e-12 && mc.u_relief > 0.0) { Ns = Nb; }++ var wet = 0.0;+ if (mc.u_wetBand > 0.0) { wet = 1.0 - smoothstep(mc.u_wetLevel, mc.u_wetLevel + mc.u_wetBand, P.y); }+ albedo = albedo * mix(1.0, 0.55, wet);+ let roughness = mix(mc.u_roughness, 0.3, wet);- let ext = surfaceExtNone(N);+ let ext = surfaceExtNone(Ns);let viewDir = viewDirection(P);- let lit = applyLightingSurface(albedo, N, P, viewDir, 0.0, mc.u_roughness, 0.5, 1.0, ext);+ let lit = applyLightingSurface(albedo, Ns, P, viewDir, 0.0, roughness, 0.5, 1.0, ext);#ifdef ES_GBUFFERvar frag : FragOut;frag.color = vec4f(lit, 1.0);- frag.surface = vec4f(packNormal(N), mc.u_roughness, 0.75);+ frag.surface = vec4f(packNormal(Ns), roughness, 0.75);return frag;#elsereturn vec4f(lit, 1.0);