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(water): water is a body and a surface, and its ripples do not repeat - the body's colour is lit as light from under the surface instead of darkening on the far side of every ripple, the surface is a coat of water's reflectance over it, the ripples are three layers of the map at sizes that share no multiple with gusts and slicks across them, and every number of it is a material param
yhhcf04c39ace3d
diff --git a/docs/astro/src/content/docs/graphics/water.mdx b/docs/astro/src/content/docs/graphics/water.mdxindex 74a2efbfb..d4064534c 100644--- a/docs/astro/src/content/docs/graphics/water.mdx+++ b/docs/astro/src/content/docs/graphics/water.mdx@@ -300,7 +300,14 @@ is its x and z, `u_origin` is where the mesh was moved to as it follows the came- **Caustics.** `u_causticStrength` (0 turns them off), `u_causticScale` (the size of one cell, in world units) and `u_causticSpeed` shape the web of light on what is under the water, seen fromabove and, through the effect, from below.- **Waves.** `u_waveLength`, `u_waveHeight`, `u_waveSpeed`, `u_waveDirection`; the ripples the wind raises (`u_rippleStrength`, `u_rippleLength`) ride- on them: six short waves around the wind's heading, from `u_rippleLength` down to a fifth of it, each fading out with distance before it would shimmer.+ on them: three layers of a tiling map of ripples the engine makes (`default(ripples)`, or the material's own `u_detailMap`), at sizes that share no+ multiple so no repeat shows, each carried downwind, their mips flattening what a pixel cannot hold. `u_rippleTile` is the map's repeat in ripple lengths,+ `u_rippleFineSize` / `u_rippleWideSize` the other two layers' sizes as shares of it, `u_rippleWeight` / `u_rippleFineWeight` / `u_rippleWideWeight` how much+ each adds, `u_rippleTurn` how far off the wind two of them run. Gusts ruffle the water in slow patches and leave slicks between: `u_rippleGusts` (0 for an+ even sea), `u_gustCalm` and `u_gustRuffle` (what a slick and a ruffled patch multiply the ripples by), `u_gustSize`.+- **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 how+ much 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 the+ surface mirrors seen straight on (water's 0.0204).- **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 53c37fd8f..d31a38bfa 100644--- a/docs/astro/src/content/docs/zh-cn/graphics/water.mdx+++ b/docs/astro/src/content/docs/zh-cn/graphics/water.mdx@@ -199,7 +199,8 @@ Details 只显示水体这种形态用得到的字段:湖有尺寸和边界,河`builtin:water` 也可以单独用在任何网格上,这是组件出现之前做一块平水面的方式。`u_waveSpace` 取默认值(0)时,波浪按世界的 xz 平面度量,什么也不抬起,表面被当作是平的。区域把它设为 1:网格的 uv 是它的 x 和 z,`u_origin` 是网格跟随相机移到的位置,顶点被抬到区域的水面和它的波浪上。- **焦散。** `u_causticStrength`(0 关掉它)、`u_causticScale`(一格的大小,世界单位)和 `u_causticSpeed` 决定水下东西上那张光网的样子,从上面看是这样,通过效果从下面看也是这样。-- **波浪。** `u_waveLength`、`u_waveHeight`、`u_waveSpeed`、`u_waveDirection`;风吹起的涟漪(`u_rippleStrength`、`u_rippleLength`)骑在它们上面:围绕风向的六道短波,从 `u_rippleLength` 到它的五分之一,每道在会闪烁之前随距离淡出。+- **波浪。** `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)。- **雨。** `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 84c5b9928..33f8c3fa1 100644--- a/sdk/src/water/waterShader.ts+++ b/sdk/src/water/waterShader.ts@@ -50,6 +50,23 @@ const WATER = `#pragma shader "Water"// The small ripples the long waves carry: how steep and how long each is.#pragma param u_rippleStrength float default(0.3) range(0,1) ui(slider)#pragma param u_rippleLength float default(250) range(10,4000) ui(slider) unit(length)+// The ripples' three layers: the map's repeat in ripple lengths, the fine and the wide layer's size as shares of it, each layer's weight, and how far+// off the wind (degrees) the fine and the wide one run. Then the gusts: how much of them, what a slick and a ruffled patch multiply the ripples by, a patch's size.+#pragma param u_rippleTile float default(9) range(1,64) ui(slider)+#pragma param u_rippleFineSize float default(0.317) range(0.05,1) ui(slider)+#pragma param u_rippleWideSize float default(2.65) range(1,16) ui(slider)+#pragma param u_rippleWeight float default(1.26) range(0,4) ui(slider)+#pragma param u_rippleFineWeight float default(0.84) range(0,4) ui(slider)+#pragma param u_rippleWideWeight float default(0.98) range(0,4) ui(slider)+#pragma param u_rippleTurn float default(25) range(0,90) ui(slider)+#pragma param u_rippleGusts float default(0.6) range(0,1) ui(slider)+#pragma param u_gustCalm float default(0.35) range(0,1) ui(slider)+#pragma param u_gustRuffle float default(1.45) range(1,3) ui(slider)+#pragma param u_gustSize float default(2200) range(100,100000) ui(slider) unit(length)+// How much the ripples shade the water's own colour (1: wholly, as a painted surface; 0: not at all, as light from under one), and what the surface+// mirrors seen straight on (water's 0.0204).+#pragma param u_bodyShading float default(0.25) range(0,1) ui(slider)+#pragma param u_reflectance float default(0.0204) range(0,0.2) ui(slider)// How hard it rains on the water (the weather writes it): drops ring out on the surface, a few to every u_rippleLength.#pragma param u_rain float default(0) range(0,4) ui(slider)#pragma param u_roughness float default(0.04) range(0,1) ui(slider)@@ -400,19 +417,27 @@ float waterNoise(vec2 p) {return mix(mix(waterHash(i), waterHash(i + vec2(1.0, 0.0)), f.x),mix(waterHash(i + vec2(0.0, 1.0)), waterHash(i + vec2(1.0, 1.0)), f.x), f.y);}-/// The slope of the ripples the wind raises at p: two fields of the ripple map (u_detailMap: the engine's own, default(ripples), unless a material-/// gives another), the longest wave of the first u_rippleLength long, the second finer and running off the wind, each carried downwind at the speed-/// its length gives deep water. px is how far p moves a pixel right and a pixel up: the map is read by it, so its mips flatten what a pixel cannot hold.+/// The slope of the ripples the wind raises at p: three reads of the ripple map (u_detailMap: the engine's own, default(ripples), unless a material+/// gives another) at sizes that share no multiple, each carried its own way downwind, so no one size's repeat shows; and gusts, slow patches where+/// the wind ruffles the water and slicks where it does not (u_rippleGusts). px is how far p moves a pixel right and up: the map's mips flatten what a pixel cannot hold.vec2 rippleSlope(vec2 p, float time, vec4 px) {float base = radians(u_waveDirection);+ float turn = radians(u_rippleTurn);vec2 along = vec2(sin(base), cos(base));- vec2 aside = vec2(along.x * 0.906308 + along.y * 0.422618, along.y * 0.906308 - along.x * 0.422618);- float tile = u_rippleLength * 6.0;- float fine = tile * 0.37;- float run = sqrt(9.8 * u_rippleLength / 6.2831853) * u_waveSpeed * time;- vec2 a = textureGrad(u_detailMap, (p - along * run) / tile, px.xy / tile, px.zw / tile).rg * 2.0 - 1.0;- vec2 b = textureGrad(u_detailMap, (p - aside * (run * 0.608276)) / fine, px.xy / fine, px.zw / fine).ba * 2.0 - 1.0;- return (a / sqrt(max(1.0 - dot(a, a), 0.0625)) + b / sqrt(max(1.0 - dot(b, b), 0.0625))) * (u_rippleStrength * 1.3);+ vec2 aside = vec2(sin(base + turn), cos(base + turn));+ float tile = u_rippleLength * u_rippleTile;+ float fine = tile * u_rippleFineSize;+ float wide = tile * u_rippleWideSize;+ // Each layer is carried at the speed deep water gives the waves it mostly holds, a quarter of its repeat long.+ vec3 run = sqrt(9.8 * vec3(tile, fine, wide) / 25.1327412) * (u_waveSpeed * time);+ vec2 a = textureGrad(u_detailMap, (p - along * run.x) / tile, px.xy / tile, px.zw / tile).rg * 2.0 - 1.0;+ vec2 b = textureGrad(u_detailMap, (p.yx - aside.yx * run.y) / fine + 0.4, px.yx / fine, px.wz / fine).ba * 2.0 - 1.0;+ vec2 c = textureGrad(u_detailMap, (p + aside * run.z) / wide + 0.7, px.xy / wide, px.zw / wide).ba * 2.0 - 1.0;+ vec2 slope = a / sqrt(max(1.0 - dot(a, a), 0.0625)) * u_rippleWeight + (b / sqrt(max(1.0 - dot(b, b), 0.0625))).yx * u_rippleFineWeight+ + c / sqrt(max(1.0 - dot(c, c), 0.0625)) * u_rippleWideWeight;+ float paws = waterNoise(p / u_gustSize + along * (time * 0.11)) * 0.6 + waterNoise(p / (u_gustSize * 3.4) - aside * (time * 0.03)) * 0.4;+ float gust = mix(1.0, mix(u_gustCalm, u_gustRuffle, smoothstep(0.3, 0.7, paws)), u_rippleGusts);+ return slope * (u_rippleStrength * gust);}/// The slope rain adds at p: in every cell of two offset grids a third of u_rippleLength across, a drop lands at a place and a moment of@@ -827,8 +852,11 @@ void main() {// Foam floats on top: from under the water it is a thin shadow on the light, not a white sheet.if (below) foam *= 0.2;+ // Two layers, as water is: the body, lit as what is under the surface (its light does not turn with every ripple, so the ripples do not+ // shade it in dark spots), and the surface over it, a coat of water's own reflectance that mirrors by the ripples' normal.+ vec3 Nbody = normalize(mix(Ngeo, N, u_bodyShading));SurfaceExt ext = surfaceExtNone(N);- ext.misc.x = 0.0204;+ ext.misc.x = 0.0;vec3 albedo = mix(scatterColor * murk, u_foamColor.rgb, foam);// Rain stipples the surface finer than any ring: it reflects a little less sharply.// Waves finer than a pixel cannot be drawn as glints: they would flash pixel by pixel. How far the normal turns across a pixel is@@ -839,14 +867,15 @@ void main() {float roughness = sqrt(sqrt(clamp(plain * plain * plain * plain + spread, 0.0, 1.0)));// The sea is seldom in shadow and its edge is lost in the waves: the sun's shadow is read as one hard edge here, not as a penumbra.g_shadowHard = 1.0;- vec3 lit = applyLightingSurface(albedo, N, P, viewDir, 0.0, roughness, 1.0, 1.0, ext);+ ext.coat = vec4(1.0, roughness, max(u_reflectance, 1e-4), 0.0);+ vec3 lit = applyLightingSurface(albedo, Nbody, P, viewDir, 0.0, 1.0, 0.0, 1.0, ext);float cosV = clamp(dot(N, viewDir), 0.0, 1.0);- float fresnel = 0.0204 + (1.0 - 0.0204) * pow(1.0 - cosV, 5.0);+ float fresnel = u_reflectance + (1.0 - u_reflectance) * pow(1.0 - cosV, 5.0);if (below) {// Out of the water the glancing rays are reflected whole: Snell's window, a cone of sky overhead in a mirror of the water below it.float sinT = 1.333 * sqrt(max(1.0 - cosV * cosV, 0.0));float cosT = sqrt(max(1.0 - sinT * sinT, 0.0));- fresnel = sinT >= 1.0 ? 1.0 : 0.0204 + (1.0 - 0.0204) * pow(1.0 - cosT, 5.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);// 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.@@ -919,14 +948,21 @@ fn waterNoise(p : vec2f) -> f32 {fn rippleSlope(p : vec2f, time : f32, px : vec4f) -> vec2f {let base = radians(mc.u_waveDirection);+ let turn = radians(mc.u_rippleTurn);let along = vec2f(sin(base), cos(base));- let aside = vec2f(along.x * 0.906308 + along.y * 0.422618, along.y * 0.906308 - along.x * 0.422618);- let tile = mc.u_rippleLength * 6.0;- let fine = tile * 0.37;- let run = sqrt(9.8 * mc.u_rippleLength / 6.2831853) * mc.u_waveSpeed * time;- let a = textureSampleGrad(u_detailMap, u_detailMap_s, (p - along * run) / tile, px.xy / tile, px.zw / tile).rg * 2.0 - vec2f(1.0);- let b = textureSampleGrad(u_detailMap, u_detailMap_s, (p - aside * (run * 0.608276)) / fine, px.xy / fine, px.zw / fine).ba * 2.0 - vec2f(1.0);- return (a / sqrt(max(1.0 - dot(a, a), 0.0625)) + b / sqrt(max(1.0 - dot(b, b), 0.0625))) * (mc.u_rippleStrength * 1.3);+ let aside = vec2f(sin(base + turn), cos(base + turn));+ let tile = mc.u_rippleLength * mc.u_rippleTile;+ let fine = tile * mc.u_rippleFineSize;+ let wide = tile * mc.u_rippleWideSize;+ let run = sqrt(9.8 * vec3f(tile, fine, wide) / 25.1327412) * (mc.u_waveSpeed * time);+ let a = textureSampleGrad(u_detailMap, u_detailMap_s, (p - along * run.x) / tile, px.xy / tile, px.zw / tile).rg * 2.0 - vec2f(1.0);+ let b = textureSampleGrad(u_detailMap, u_detailMap_s, (p.yx - aside.yx * run.y) / fine + vec2f(0.4), px.yx / fine, px.wz / fine).ba * 2.0 - vec2f(1.0);+ let c = textureSampleGrad(u_detailMap, u_detailMap_s, (p + aside * run.z) / wide + vec2f(0.7), px.xy / wide, px.zw / wide).ba * 2.0 - vec2f(1.0);+ let slope = a / sqrt(max(1.0 - dot(a, a), 0.0625)) * mc.u_rippleWeight + (b / sqrt(max(1.0 - dot(b, b), 0.0625))).yx * mc.u_rippleFineWeight+ + c / sqrt(max(1.0 - dot(c, c), 0.0625)) * mc.u_rippleWideWeight;+ let paws = waterNoise(p / mc.u_gustSize + along * (time * 0.11)) * 0.6 + waterNoise(p / (mc.u_gustSize * 3.4) - aside * (time * 0.03)) * 0.4;+ let gust = mix(1.0, mix(mc.u_gustCalm, mc.u_gustRuffle, smoothstep(0.3, 0.7, paws)), mc.u_rippleGusts);+ return slope * (mc.u_rippleStrength * gust);}fn rainRings(p : vec2f, time : f32, rain : f32) -> vec2f {@@ -1332,8 +1368,9 @@ fn waterSurface(p : vec2f, time : f32, dist : f32, px : vec4f, amp : f32, calm :}if (below) { foam = foam * 0.2; }+ let Nbody = normalize(mix(Ngeo, N, mc.u_bodyShading));var ext = surfaceExtNone(N);- ext.misc.x = 0.0204;+ ext.misc.x = 0.0;let albedo = mix(scatterColor * murk, mc.u_foamColor.rgb, vec3f(foam));let turnX = dpdx(N);let turnY = dpdy(N);@@ -1341,13 +1378,14 @@ fn waterSurface(p : vec2f, time : f32, dist : f32, px : vec4f, amp : f32, calm :let plain = mix(mc.u_roughness + 0.05 * clamp(mc.u_rain, 0.0, 1.0), 0.7, foam);let roughness = sqrt(sqrt(clamp(plain * plain * plain * plain + spread, 0.0, 1.0)));g_shadowHard = 1.0;- let lit = applyLightingSurface(albedo, N, P, viewDir, 0.0, roughness, 1.0, 1.0, ext);+ ext.coat = vec4f(1.0, roughness, max(mc.u_reflectance, 1e-4), 0.0);+ let lit = applyLightingSurface(albedo, Nbody, P, viewDir, 0.0, 1.0, 0.0, 1.0, ext);let cosV = clamp(dot(N, viewDir), 0.0, 1.0);- var fresnel = 0.0204 + (1.0 - 0.0204) * pow(1.0 - cosV, 5.0);+ var fresnel = mc.u_reflectance + (1.0 - mc.u_reflectance) * pow(1.0 - cosV, 5.0);if (below) {let sinT = 1.333 * sqrt(max(1.0 - cosV * cosV, 0.0));let cosT = sqrt(max(1.0 - sinT * sinT, 0.0));- if (sinT >= 1.0) { fresnel = 1.0; } else { fresnel = 0.0204 + (1.0 - 0.0204) * pow(1.0 - cosT, 5.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 mirror = vec3f(0.0);diff --git a/src/esengine/renderer/frame/RippleNormals.cpp b/src/esengine/renderer/frame/RippleNormals.cppindex c91999e60..7d27635e4 100644--- a/src/esengine/renderer/frame/RippleNormals.cpp+++ b/src/esengine/renderer/frame/RippleNormals.cpp@@ -26,17 +26,17 @@ u32 next(u32& state) {f32 unit(u32& state) { return static_cast<f32>(next(state) >> 8) / 16777216.0f; }/// Whole numbers of cycles across the texture, so the field tiles; many of them at every heading, so no two draw a lattice. A wave's-/// slope falls with its frequency as a wind sea's does (height by k^-2, slope by k^-1).+/// slope falls slowly with its frequency (k^-0.55): the long ones carry the look, the short ones the glitter.std::vector<Wave> spectrum(u32 seed) {std::vector<Wave> waves;waves.reserve(WAVES);u32 state = seed;while (waves.size() < WAVES) {- const i32 kx = static_cast<i32>(next(state) % 81) - 40;- const i32 kz = static_cast<i32>(next(state) % 81) - 40;+ const i32 kx = static_cast<i32>(next(state) % 41) - 20;+ const i32 kz = static_cast<i32>(next(state) % 41) - 20;const f32 k = std::sqrt(static_cast<f32>(kx * kx + kz * kz));- if (k < static_cast<f32>(RIPPLE_NORMALS_LONGEST) || k > 40.0f) continue;- waves.push_back({static_cast<f32>(kx), static_cast<f32>(kz), 1.0f / k, unit(state) * TAU});+ if (k < static_cast<f32>(RIPPLE_NORMALS_LONGEST) || k > 20.0f) continue;+ waves.push_back({static_cast<f32>(kx), static_cast<f32>(kz), std::pow(k, -0.55f), unit(state) * TAU});}return waves;}diff --git a/src/esengine/renderer/frame/RippleNormals.hpp b/src/esengine/renderer/frame/RippleNormals.hppindex 294178058..1286e5b84 100644--- a/src/esengine/renderer/frame/RippleNormals.hpp+++ b/src/esengine/renderer/frame/RippleNormals.hpp@@ -12,8 +12,8 @@namespace esengine {inline constexpr u32 RIPPLE_NORMALS_SIZE = 256;-/// The longest wave in a field runs this many times across the texture: a reader that wants ripples of length L tiles it every L times this.-inline constexpr u32 RIPPLE_NORMALS_LONGEST = 6;+/// The longest wave in a field runs this many times across the texture, the shortest twenty: a field read at one size holds a decade of lengths.+inline constexpr u32 RIPPLE_NORMALS_LONGEST = 2;/// RGBA8, RIPPLE_NORMALS_SIZE square. A texel's rg (and ba) is the xy of a unit normal whose z is up: the slope there is xy / z.std::vector<u8> buildRippleNormals();diff --git a/src/esengine/resource/ShaderParser.cpp b/src/esengine/resource/ShaderParser.cppindex 6f184a47f..2516d7008 100644--- a/src/esengine/resource/ShaderParser.cpp+++ b/src/esengine/resource/ShaderParser.cpp@@ -2288,7 +2288,9 @@ fn applyLightingSurfaceBRDF(diffuse : vec3f, F0 : vec3f, N : vec3f, worldPos : vif (ext.coat.x > 0.0) {let coatNdotV = max(dot(ext.coatN.xyz, V), 1e-4);let cab = envBRDFApprox(coatNdotV, ext.coat.y);- let Fc = (0.04 * cab.x + cab.y) * ext.coat.x;+ // coat.z is the coat's reflectance seen straight on; 0 is a varnish's 0.04.+ let coatF0 = select(0.04, ext.coat.z, ext.coat.z > 0.0);+ let Fc = (coatF0 * cab.x + cab.y) * ext.coat.x;baseScale *= 1.0 - Fc;extra += envRadiance(reflect(-V, ext.coatN.xyz), ext.coat.y, reflectionColumn(), worldPos) * (ao * Fc);}@@ -2422,7 +2424,8 @@ fn shadeLight(pd : vec4f, col : vec4f, sp : vec4f, sh : vec4f, sm : vec4f, fo :if (ext.coat.x > 0.0) {let coatNdotL = max(dot(ext.coatN.xyz, L), 0.0);let coatA = max(ext.coat.y * ext.coat.y, 1e-3);- let Fc = (0.04 + 0.96 * pow(1.0 - VdotH, 5.0)) * ext.coat.x;+ let coatF0 = select(0.04, ext.coat.z, ext.coat.z > 0.0);+ let Fc = (coatF0 + (1.0 - coatF0) * pow(1.0 - VdotH, 5.0)) * ext.coat.x;extra += irr * coatNdotL* (distributionGGX(max(dot(ext.coatN.xyz, Hx), 0.0), coatA) * (0.25 / max(VdotH * VdotH, 1e-4))* Fc * 3.14159265);@@ -3844,7 +3847,8 @@ ShaderParser::AssembledStage ShaderParser::assembleStageEx(const ParsedShader& p" if (ext.coat.x > 0.0) {\n"" highp float coatNdotV = max(dot(ext.coatN.xyz, V), 1e-4);\n"" highp vec2 cab = envBRDFApprox(coatNdotV, ext.coat.y);\n"- " highp float Fc = (0.04 * cab.x + cab.y) * ext.coat.x;\n"+ " highp float coatF0 = ext.coat.z > 0.0 ? ext.coat.z : 0.04;\n"+ " highp float Fc = (coatF0 * cab.x + cab.y) * ext.coat.x;\n"" baseScale *= 1.0 - Fc;\n"" extra += envRadiance(reflect(-V, ext.coatN.xyz), ext.coat.y, reflectionColumn(), worldPos) * (ao * Fc);\n"" }\n"@@ -3975,7 +3979,8 @@ ShaderParser::AssembledStage ShaderParser::assembleStageEx(const ParsedShader& p" if (ext.coat.x > 0.0) {\n"" highp float coatNdotL = max(dot(ext.coatN.xyz, L), 0.0);\n"" highp float coatA = max(ext.coat.y * ext.coat.y, 1e-3);\n"- " highp float Fc = (0.04 + 0.96 * pow(1.0 - VdotH, 5.0)) * ext.coat.x;\n"+ " highp float coatF0 = ext.coat.z > 0.0 ? ext.coat.z : 0.04;\n"+ " highp float Fc = (coatF0 + (1.0 - coatF0) * pow(1.0 - VdotH, 5.0)) * ext.coat.x;\n"" extra += irr * coatNdotL\n"" * (distributionGGX(max(dot(ext.coatN.xyz, Hx), 0.0), coatA) * (0.25 / max(VdotH * VdotH, 1e-4))\n"" * Fc * 3.14159265);\n"