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
fix(water): water is as deep as it is, not as far as the eye's line through it - the light a pixel shows crossed the depth under it at the slope light takes in water, where the straight line to the bed made any sea seen low across it deep and blue; the bed is dimmed by the water its light came down through too, and how soon the water's colour deepens is a param
yhhc37f6fdd2b33
diff --git a/docs/astro/src/content/docs/graphics/water.mdx b/docs/astro/src/content/docs/graphics/water.mdxindex d4064534c..22678639e 100644--- a/docs/astro/src/content/docs/graphics/water.mdx+++ b/docs/astro/src/content/docs/graphics/water.mdx@@ -308,6 +308,10 @@ is its x and z, `u_origin` is where the mesh was moved to as it follows the came- **Body and surface.** The water's own colour is lit as light from under the surface, and the surface over it mirrors by the ripples: `u_bodyShading` is howmuch the ripples shade the colour as well (0.25; at 1 every ripple darkens the water on its far side, as a painted surface would), `u_reflectance` what thesurface mirrors seen straight on (water's 0.0204).+- **Depth.** The water a pixel's light crossed is the depth under it over the slope light takes in water (never more than 49 degrees off straight+ down), not the eye's own line to the bed: a lagoon seen low across the sea is as clear as from above it. `u_density` and `u_absorption` take the light+ out, `u_deepening` is how soon the water's own colour turns from shallow to deep (a share of the density; lower keeps a lagoon turquoise over deeper+ water), and `u_bedShade` how much the bed is also dimmed by the water its light came down through (1, as water does; 0 lights it as if dry).- **Rain.** When a `Weather` drives the rain, drops ring out on the surface (`u_rain`, written by the zone): rings that spread and die, a few to every`u_rippleLength`, more of them the harder it rains, and the surface a little less sharp. They are drawn, not simulated: they put nothing in the zone's ripplesand make no foam, and they fade out with distance. On a zone's surface the zone writes the four every frame (its own length, speed and heading, anddiff --git a/docs/astro/src/content/docs/zh-cn/graphics/water.mdx b/docs/astro/src/content/docs/zh-cn/graphics/water.mdxindex d31a38bfa..9e014759b 100644--- a/docs/astro/src/content/docs/zh-cn/graphics/water.mdx+++ b/docs/astro/src/content/docs/zh-cn/graphics/water.mdx@@ -201,6 +201,7 @@ Details 只显示水体这种形态用得到的字段:湖有尺寸和边界,河- **焦散。** `u_causticStrength`(0 关掉它)、`u_causticScale`(一格的大小,世界单位)和 `u_causticSpeed` 决定水下东西上那张光网的样子,从上面看是这样,通过效果从下面看也是这样。- **波浪。** `u_waveLength`、`u_waveHeight`、`u_waveSpeed`、`u_waveDirection`;风吹起的涟漪(`u_rippleStrength`、`u_rippleLength`)骑在它们上面:引擎生成的一张可平铺涟漪图(`default(ripples)`,或材质自己的 `u_detailMap`)取三层,尺寸互不成倍数所以看不出重复,各自顺风漂移,像素盖不住的由多级渐远抹平。`u_rippleTile` 是这张图每多少个涟漪长度重复一次,`u_rippleFineSize` / `u_rippleWideSize` 是另外两层相对它的尺寸,`u_rippleWeight` / `u_rippleFineWeight` / `u_rippleWideWeight` 是各层的份量,`u_rippleTurn` 是其中两层偏离风向多少度。阵风让水面一块皱一块平:`u_rippleGusts`(0 为均匀的海面)、`u_gustCalm` 和 `u_gustRuffle`(平滑处和起皱处给涟漪乘的倍数)、`u_gustSize`。- **水体与表面。** 水体自身的颜色按从水下透出的光来照亮,表面那层按涟漪法线反射:`u_bodyShading` 是涟漪同时给水体颜色打多少明暗(0.25;为 1 时每道涟漪背光一侧都变暗,像一块涂了颜色的表面),`u_reflectance` 是正对着看时表面的反射率(水是 0.0204)。+- **深度。** 一个像素的光在水里走过的路程,是它下方的水深除以光在水中的倾斜(光进水后偏离竖直不超过 49 度),不是视线到水底的那条直线:贴着海面看过去的泻湖和从上方看一样清。`u_density` 和 `u_absorption` 把光吸掉,`u_deepening` 是水自身的颜色多快从浅色变成深色(相对密度的比例;调低能让泻湖在更深的水上仍是青绿),`u_bedShade` 是水底额外被光照下来时穿过的水压暗多少(1,水本来如此;0 则像干地一样照亮)。- **雨。** `Weather` 驱动雨时,雨滴在水面上激起环纹(`u_rain`,由区域写入):向外扩散然后消失的圆环,每个 `u_rippleLength` 里有几个,雨越大越多,水面也稍微变得不那么锐利。它们是画出来的而不是模拟的:不会往区域的涟漪里放任何东西,也不产生泡沫,并随距离淡出。在区域的水面上,这四个由区域每帧写入(它自己的波长、速度和方向,高度则是天气的份额,再乘以场里每一处的高度),所以材质里这四个值在那里不起作用。- **碎闪。** 阳光在细小坡面上的亮点来自两张可平铺、在表面上滚动的法线图,它们打包在材质唯一容得下的那一张纹理 `u_detailMap` 里:红绿是第一张的坡度(x 和 z,0.5 为平),蓝和透明是第二张的,按数据读取。每张有自己的 `u_detailTile`(重复一次的世界单位)、`u_detailStrength`(比图上画的陡多少倍;0 表示不用,没设置的材质因此保持原样)和 `u_detailFlow`(每秒漂移的世界单位)。亮点是这些坡面受光后的镜面反应,不是画上去的白色贴图,所以只出现在太阳、眼睛和坡面对齐的地方,光变弱它们也跟着淡出。它们只在片元里计算:几何体和 `Waters.sample` 不带它们。- **折射与水深。** 水下看到的是不透明场景的画面,被波浪按水量多少偏移。水深是**量出来的**:绘制水之前,帧会拷贝一份场景每个像素离眼睛多远,水在某个像素处的深度就是那个距离与水面自己的距离之差。光沿着这段水柱被吸收——红色最先(`u_absorption`、`u_density`)——水再还给你它自己的颜色,随水变深从 `u_shallowColor` 过渡到 `u_deepColor`。diff --git a/sdk/src/water/waterShader.ts b/sdk/src/water/waterShader.tsindex 33f8c3fa1..f0f1c99a0 100644--- a/sdk/src/water/waterShader.ts+++ b/sdk/src/water/waterShader.ts@@ -42,6 +42,11 @@ const WATER = `#pragma shader "Water"// How fast each colour is absorbed along the way through the water (red goes first), and how thick the water is, per world unit.#pragma param u_absorption color default(0.45,0.09,0.05,1)#pragma param u_density float default(0.0012) range(0,0.04) ui(slider) unit(perLength)+// How soon the water's own colour turns from the shallow one to the deep one, as a share of the density: lower keeps a lagoon its shallow colour over deeper water.+#pragma param u_deepening float default(0.5) range(0,4) ui(slider)+// How much the bed is dimmed by the water its light came down through, as well as by the water it is seen through: 1 is what water does+// (a bed ten metres down is lit by what ten metres let through), 0 lights it as if it were dry.+#pragma param u_bedShade float default(1) range(0,1) ui(slider)// The waves: height, length of the longest, how sharp their crests are drawn, how fast they travel and which way (degrees).#pragma param u_waveHeight float default(30) range(0,800) ui(slider) unit(length)#pragma param u_waveLength float default(1400) range(50,30000) ui(slider) unit(length)@@ -807,11 +812,15 @@ void main() {}// The water in between takes the light out of it, red first, and gives back its own colour: more of it the deeper it goes.- vec3 transmit = exp(-u_absorption.rgb * (u_density * column));+ // Light bends down as it enters water and is never more than 49 degrees off straight down in it: what it crosses is the depth over that+ // slope, not the eye's own line to the bed, which seen low across the water is many times as long and would turn any sea deep.+ float across = sqrt(max(1.0 - viewDir.y * viewDir.y, 0.0)) / 1.333;+ float water = below ? column : column * abs(viewDir.y) / sqrt(1.0 - across * across);+ vec3 transmit = exp(-u_absorption.rgb * (u_density * water));// From below the water between the eye and the surface is the camera's own medium (the underwater effect fogs it), not a column behind the surface.if (below) transmit = vec3(1.0);float murk = 1.0 - dot(transmit, vec3(1.0 / 3.0));- vec3 scatterColor = mix(u_shallowColor.rgb, u_deepColor.rgb, 1.0 - exp(-column * u_density * 0.5));+ vec3 scatterColor = mix(u_shallowColor.rgb, u_deepColor.rgb, 1.0 - exp(-water * u_density * u_deepening));// Foam: along the shore, where the water is shallow, broken by noise that moves; and on the crests of the waves.float vertical = column * max(abs(viewDir.y), 0.15);@@ -877,7 +886,8 @@ void main() {float cosT = sqrt(max(1.0 - sinT * sinT, 0.0));fresnel = sinT >= 1.0 ? 1.0 : u_reflectance + (1.0 - u_reflectance) * pow(1.0 - cosT, 5.0);}- vec3 seen = behind * transmit * (1.0 - fresnel) * (1.0 - foam);+ vec3 down = below ? vec3(1.0) : exp(-u_absorption.rgb * (u_density * column * abs(viewDir.y) * u_bedShade));+ vec3 seen = behind * transmit * down * (1.0 - fresnel) * (1.0 - foam);// From below the water is a mirror past the critical angle, and what it mirrors is the world under it: the picture, followed along the reflected ray.vec3 mirror = vec3(0.0);if (below && fresnel > 0.02 && haveColor && haveDepth && u_mirror > 0.5) {@@ -1322,10 +1332,13 @@ fn waterSurface(p : vec2f, time : f32, dist : f32, px : vec4f, amp : f32, calm :}}- var transmit = exp(-mc.u_absorption.rgb * (mc.u_density * column));+ let across = sqrt(max(1.0 - viewDir.y * viewDir.y, 0.0)) / 1.333;+ var water = column;+ if (!below) { water = column * abs(viewDir.y) / sqrt(1.0 - across * across); }+ var transmit = exp(-mc.u_absorption.rgb * (mc.u_density * water));if (below) { transmit = vec3f(1.0); }let murk = 1.0 - dot(transmit, vec3f(1.0 / 3.0));- let scatterColor = mix(mc.u_shallowColor.rgb, mc.u_deepColor.rgb, vec3f(1.0 - exp(-column * mc.u_density * 0.5)));+ let scatterColor = mix(mc.u_shallowColor.rgb, mc.u_deepColor.rgb, vec3f(1.0 - exp(-water * mc.u_density * mc.u_deepening)));let vertical = column * max(abs(viewDir.y), 0.15);var baked = min(shoreDist, shoreDepth);@@ -1387,7 +1400,9 @@ fn waterSurface(p : vec2f, time : f32, dist : f32, px : vec4f, amp : f32, calm :let cosT = sqrt(max(1.0 - sinT * sinT, 0.0));if (sinT >= 1.0) { fresnel = 1.0; } else { fresnel = mc.u_reflectance + (1.0 - mc.u_reflectance) * pow(1.0 - cosT, 5.0); }}- var seen = behind * transmit * (1.0 - fresnel) * (1.0 - foam);+ var down = vec3f(1.0);+ if (!below) { down = exp(-mc.u_absorption.rgb * (mc.u_density * column * abs(viewDir.y) * mc.u_bedShade)); }+ var seen = behind * transmit * down * (1.0 - fresnel) * (1.0 - foam);var mirror = vec3f(0.0);if (below && fresnel > 0.02 && haveColor && haveDepth && mc.u_mirror > 0.5) {let met = waterReflect(P, reflect(-viewDir, N), waterDist);