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.

feat(render): screen-space subsurface scattering blurs the lit colour of surfaces a material marks

yhhde080f84d3db
diff --git a/CHANGELOG.md b/CHANGELOG.md
index c188db0c9..51ba23781 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -17,6 +17,7 @@ published separately; it ships inside the editor.
### Added
- **Contact shadows and screen-space bounce light.** Contact Shadows and Screen Space GI effects add small sharp shadows at contacts and one bounce of colour between surfaces, and the weather softens the sun's shadows. `contactShadows` marches a short ray toward the scene's sun in the depth buffer; `ssgi` gathers eight rays a pixel over the G-buffer's normals at half size into a float target and averages the neighbours on the same surface before laying the light, times the receiver's colour, over the picture. The Weather component raises the sun's `sourceAngle` and lowers its `shadowStrength` as the sky clouds over.
+- **Subsurface scattering.** A Subsurface Scattering effect blurs the lit colour of the surfaces a material marks as scattering, red furthest, without bleeding across a depth edge. The Model material's `u_subsurface` marks its pixels (alpha 0.996, which nothing shows and the effect restores to 1); the effect is an x and a y pass whose width is a world length, each colour reaching a different share of it. Only marked neighbours on the same surface are gathered. It blurs the whole lit colour, specular included, and the width is the effect's, shared by every marked material; the low quality level leaves it out.
- **Refraction.** Glass and water refract: a material with transmission bends and tints the opaque scene behind it, softened by its roughness. The frame draws the scene in two halves with a copy of the picture taken between them: the materials that read it (a shader that declares `u_sceneColor`, the Model shader while its transmission is above zero) are drawn second, ray-bent through their thickness. The surface's own highlights are no longer dimmed by blending, as they were when transmission was only coverage. Only the opaque scene is refracted, and it needs a post-process chain to copy from; without one transmission falls back to the earlier coverage.
- **Ground-truth ambient occlusion.** Ground-truth ambient occlusion (GTAO) is a post-process effect beside SSAO: a crease darkens by how closed it really is and a flat floor stays white, with no bias to tune. It marches the depth buffer along a few slice planes through each pixel, finds the horizon on both sides and integrates the open arc against the surface's own normal; the blur and composite are SSAO's, the low quality level leaves it out, and the ambient-occlusion switch governs both.
- **A sharper upscale.** A scene drawn at a render scale below 1 is stretched back to the screen with a resampler that keeps edges steeper than a bilinear copy. The resampler is Catmull-Rom clamped to the texels around each point, so it draws no halo; a whole-number stretch is still an exact copy. Measured on a sprite edge at the low level's 0.75 scale: the edge's 10-90% width is 1.04 px against 1.15 px bilinear (0.69 px at full size).
diff --git a/desktop b/desktop
index 5d7ff28de..28e31754e 160000
--- a/desktop
+++ b/desktop
@@ -1 +1 @@
-Subproject commit 5d7ff28de563101751c3a228614714497525caa7
+Subproject commit 28e31754e355af383e80a2a24c38a25a9b4e9baa
diff --git a/docs/astro/src/content/docs/graphics/materials.mdx b/docs/astro/src/content/docs/graphics/materials.mdx
index d5130dd50..319af885a 100644
--- a/docs/astro/src/content/docs/graphics/materials.mdx
+++ b/docs/astro/src/content/docs/graphics/materials.mdx
@@ -51,6 +51,7 @@ The **Model** shader is a metal/rough surface with layers on top. Each is off at
| Sheen | `u_sheenColor`, `u_sheenRoughness` | The soft light that cloth throws back at grazing angles (a Charlie lobe), taken out of the base so that it does not add energy. Velvet, satin, felt. |
| Anisotropy | `u_anisotropy` (-1..1), `u_anisotropyRotation` | A highlight stretched across the grooves instead of round: brushed metal, hair-like fibres, CDs. The grooves run along the UV frame turned by the rotation; the environment reflection is bent the same way. |
| Translucency | `u_translucency`, `u_translucencyColor`, `u_thickness` | Light that scatters into a thin surface: the lit side wraps past the terminator, and a lamp behind shows through, tinted, less the thicker it is. Leaves, paper, wax, a lampshade, skin at the ears. |
+| Subsurface | `u_subsurface` | Marks the surface as one light spreads through: with the [Subsurface Scattering](/docs/graphics/post-processing/#subsurface-scattering) effect in the volume, its lit colour is blurred along the surface, red furthest. Skin, wax, marble. |
| Thin film | `u_iridescence`, `u_iridescenceIor`, `u_iridescenceThickness` (nm) | The reflection changes hue with the angle: soap, oil, beetle shell, coated glass. |
| Index of refraction | `u_ior`, `u_specular` | How strongly a non-metal reflects head-on (1.5 is glass and most plastics; a gemstone is 2.4), and a master scale on the reflection. |
| Transmission | `u_transmission`, `u_attenuationColor`, `u_thickness`, `u_refractionStrength` | Glass and water: the surface shows what is behind it, bent by the glass (thicker bends more), tinted, and softened by its roughness; it reflects more at grazing angles. |
@@ -60,7 +61,7 @@ Everything here is written by model import from the glTF extensions of the same
**Limits.** These are cheap, real-time models, and say where they stop:
- **Transmission refracts the opaque scene only.** A material that holds `u_transmission` above zero is drawn after the rest of the scene, from a copy of the picture taken just before: the ray is bent through the surface's thickness and the copy is read where it lands. So glass does not refract other glass, and a thin shell bends the picture by a fixed amount rather than as a lens that flips it. It needs a scene the engine can copy, which means a post-process chain is running (any effect, an output transform, or a render scale below 1); without one the surface falls back to alpha-blended coverage and shows the background undistorted.
-- **Translucency does not blur.** There is no screen-space subsurface pass; the light that enters at one point leaves at that point.
+- **Translucency is a shading term, not a blur.** It wraps light past the terminator and lets a lamp behind show through, but the light that enters at a point leaves at that point. The spreading itself is the Subsurface Scattering effect's, and it blurs the whole lit colour of the marked surface, so a sharp highlight on it is softened too.
- **A layer's textures are not read.** Each layer is a constant for the whole material (the Model shader has no texture unit left for more); a glTF extension's maps are reported at import and its factors are kept.
- **Sheen and thin film are fits.** Sheen's energy loss and the film's interference are analytic approximations in place of the lookup tables a production renderer bakes.
- **Not provided:** hair (a Marschner-style strand model), eyes, a separate normal map for the coat, layered materials beyond one coat.
diff --git a/docs/astro/src/content/docs/graphics/post-processing.mdx b/docs/astro/src/content/docs/graphics/post-processing.mdx
index 3ddddcc74..e4ffa3d11 100644
--- a/docs/astro/src/content/docs/graphics/post-processing.mdx
+++ b/docs/astro/src/content/docs/graphics/post-processing.mdx
@@ -30,6 +30,7 @@ several can stack in one frame at no extra target cost beyond the passes.
| `lightShafts` | Sun rays through whatever stands in front of it | `intensity`, `sunAzimuth`, `sunElevation`, `decay`, `reach` |
| `ssao` | Ambient occlusion from a ring of depth samples around each point | `radius`, `intensity`, `bias`, `power` |
| `gtao` | Ground-truth ambient occlusion: the horizon each point can see, integrated against its own normal | `radius`, `intensity`, `power`, `thickness` |
+| `subsurface` | Light spreading through a marked surface: its lit colour blurred along it, red furthest | `width`, `strength`, `color` |
| `contactShadows` | The small sharp shadows at a contact a shadow map is too coarse to hold | `intensity`, `length`, `thickness` |
| `ssgi` | One bounce of diffuse light between the surfaces the camera sees | `intensity`, `radius`, `thickness`, `maxBounce` |
| `autoExposure` | Eye adaptation: exposure follows how bright the picture is | `bias`, `speedUp`, `speedDown`, `minEV`, `maxEV` |
@@ -98,6 +99,20 @@ The clock is the game's. A capture that has to be the same on every run sets `ti
wants. What this is not: the ground's shape is read from what the camera sees (a hollow behind a wall is not known),
and the rain is a screen-space layer, not particles that collide with the world.
+### Subsurface scattering
+
+Skin, wax, marble and leaves show the lit picture softened along the surface, each colour by a different amount: red
+travels far, blue barely. `subsurface` is that blur, as an x pass and a y pass over the finished picture. Which
+surfaces take part is the material's choice — a Model material with `u_subsurface` above zero marks its pixels (it
+draws them with alpha 0.996, which nothing on screen shows and which the effect gives back as 1) — and the effect
+decides how far: `width` is a world length for the widest colour, `color` the proportions between the channels (the
+brightest travels the full width), `strength` how much of the blur is mixed in. A marked pixel gathers only from marked
+neighbours on the same surface, so an ear does not bleed into the wall behind it.
+
+It blurs the lit colour as it stands, specular included, so a sharp highlight on a scattering surface is softened with
+it, and the width belongs to the effect, so every scattering material in view shares it. The effect needs scene depth
+like ambient occlusion does, and the low quality level leaves it out.
+
### Ambient occlusion
Two estimators share one blur and one composite. `ssao` counts how many of a ring of nearby points stand above the
diff --git a/docs/astro/src/content/docs/zh-cn/graphics/materials.mdx b/docs/astro/src/content/docs/zh-cn/graphics/materials.mdx
index b32fb6b41..9d1789ee3 100644
--- a/docs/astro/src/content/docs/zh-cn/graphics/materials.mdx
+++ b/docs/astro/src/content/docs/zh-cn/graphics/materials.mdx
@@ -45,6 +45,7 @@ std140 uniform 块,于是 CPU 能按名字设置、并安全地送到 GPU。一
| 绒面(Sheen) | `u_sheenColor`、`u_sheenRoughness` | 布料在掠射角回弹的柔光(Charlie 瓣),并从底层里扣掉对应能量,不凭空增加亮度。天鹅绒、缎面、毛毡。 |
| 各向异性 | `u_anisotropy`(-1..1)、`u_anisotropyRotation` | 高光沿垂直于纹路的方向被拉长而不是圆的:拉丝金属、纤维、光盘。纹路沿 UV 坐标系按旋转角转过后的方向;环境反射也按同一方向弯折。 |
| 半透(Translucency) | `u_translucency`、`u_translucencyColor`、`u_thickness` | 光散射进薄的表面:受光面越过明暗交界线向背光侧延伸,背后的灯会透出来并带上颜色,越厚越弱。树叶、纸、蜡、灯罩、耳朵边缘的皮肤。 |
+| 次表面 | `u_subsurface` | 把表面标记为光会在其中扩散的表面:体积里有[次表面散射](/docs/zh-cn/graphics/post-processing/#次表面散射)效果时,它的受光颜色会沿表面被模糊,红色最远。皮肤、蜡、大理石。 |
| 薄膜干涉 | `u_iridescence`、`u_iridescenceIor`、`u_iridescenceThickness`(nm) | 反射的色相随角度变化:肥皂泡、油膜、甲虫壳、镀膜玻璃。 |
| 折射率 | `u_ior`、`u_specular` | 非金属正对时反射有多强(1.5 是玻璃和大多数塑料,宝石是 2.4),以及反射的总缩放。 |
| 透射(Transmission) | `u_transmission`、`u_attenuationColor`、`u_thickness`、`u_refractionStrength` | 玻璃和水:表面显示它背后的东西,被玻璃折射(越厚折得越多)、带上颜色、并按粗糙度变柔;掠射角处反射更强。 |
@@ -54,7 +55,7 @@ std140 uniform 块,于是 CPU 能按名字设置、并安全地送到 GPU。一
**限制。** 这些是便宜的实时模型,下面说清楚它们止步在哪里:
- **透射只折射不透明场景。** `u_transmission` 大于 0 的材质在场景其余部分之后绘制,读取的是之前刚拍下的画面副本:光线穿过表面厚度后被折弯,在落点处读取副本。所以玻璃不会折射另一块玻璃,薄壳对画面的偏折是固定的量,而不是像透镜那样把画面翻转。它需要引擎能拷贝的场景,也就是有后处理链在运行(任意效果、输出变换,或小于 1 的渲染比例);没有时表面退回 alpha 混合覆盖,背景不会变形。
-- **半透不做模糊。** 没有屏幕空间次表面散射 pass;从某一点进入的光就在这一点出去。
+- **半透是着色项,不是模糊。** 它让光越过明暗交界线向背光侧延伸,并让背后的灯透出来,但从某一点进入的光仍在这一点出去。真正的扩散是次表面散射效果的事,它会模糊被标记表面的整个受光颜色,所以表面上锐利的高光也会被一并变软。
- **不读取层的贴图。** 每一层对整个材质是一个常量(Model shader 已经没有多余的纹理单元);glTF 扩展的贴图在导入时会报告,其系数会保留。
- **绒面和薄膜是拟合。** 绒面的能量损失和薄膜的干涉是解析近似,代替了生产级渲染器烘焙的查找表。
- **不提供:** 头发(Marschner 式的发丝模型)、眼睛、清漆单独的法线贴图、超过一层清漆的分层材质。
diff --git a/docs/astro/src/content/docs/zh-cn/graphics/post-processing.mdx b/docs/astro/src/content/docs/zh-cn/graphics/post-processing.mdx
index f4bcc4654..528ff2555 100644
--- a/docs/astro/src/content/docs/zh-cn/graphics/post-processing.mdx
+++ b/docs/astro/src/content/docs/zh-cn/graphics/post-processing.mdx
@@ -32,6 +32,7 @@ Estella 通过 ping-pong 帧缓冲运行全屏后处理。加一个 `PostProcess
| `lightShafts` | 太阳光穿过前方遮挡物形成的光束 | `intensity`、`sunAzimuth`、`sunElevation`、`decay`、`reach` |
| `ssao` | 由每个点周围一圈深度采样得出的环境光遮蔽 | `radius`、`intensity`、`bias`、`power` |
| `gtao` | 真值环境光遮蔽:每个点能看见的地平线,按它自身法线积分 | `radius`、`intensity`、`power`、`thickness` |
+| `subsurface` | 光在被标记的表面里扩散:受光颜色沿表面被模糊,红色最远 | `width`、`strength`、`color` |
| `contactShadows` | 阴影贴图太粗放不下的接触处的小而锐利的阴影 | `intensity`、`length`、`thickness` |
| `ssgi` | 相机看得见的表面之间的一次漫反射光弹射 | `intensity`、`radius`、`thickness`、`maxBounce` |
| `autoExposure` | 眼睛适应:曝光跟随画面亮度 | `bias`、`speedUp`、`speedDown`、`minEV`、`maxEV` |
@@ -77,6 +78,12 @@ Estella 通过 ping-pong 帧缓冲运行全屏后处理。加一个 `PostProcess
时钟是游戏的。要在每次运行都相同的截图,把 `timeScale` 设为 0,`phase` 设为想要的时间。它不是什么:地面的形状是从相机看到的东西里读出来的(墙后面的凹处它不知道),
雨是屏幕空间的一层,不是会与世界碰撞的粒子。
+### 次表面散射
+
+皮肤、蜡、大理石和树叶会让受光的画面沿表面变柔,每种颜色柔的程度不同:红色传得远,蓝色几乎不传。`subsurface` 就是这个模糊,作为作用在完成画面上的 x 和 y 两个 pass。哪些表面参与由材质决定——`u_subsurface` 大于 0 的 Model 材质会标记自己的像素(以 alpha 0.996 绘制,屏幕上看不出来,效果会把它还原成 1)——传多远由效果决定:`width` 是最宽那个颜色的世界长度,`color` 是各通道之间的比例(最亮的传满整个宽度),`strength` 是混入多少模糊。被标记的像素只从同一表面上被标记的邻居那里采样,所以耳朵不会渗到身后的墙上。
+
+它模糊的是原样的受光颜色,包括高光,所以散射表面上锐利的高光会被一并变软;宽度属于效果,视野里所有散射材质共用同一个宽度。和环境光遮蔽一样,它需要场景深度,低质量档位不包含它。
+
### 环境光遮蔽
两种估计器共用一套模糊和合成。`ssao` 数的是周围一圈点里有多少高于表面;它更便宜,需要 `bias` 来避免平坦的地面给自己上阴影。`gtao` 沿每个像素的几个切片平面在深度缓冲里步进,找出两侧地平线升到多高,再对剩下的开阔弧段按表面自身法线积分——所以没有要调的 bias,平坦的地面保持白色,夹角处的暗度就是它实际有多封闭。两者的 `radius` 都是世界长度。`thickness`(GTAO)让栏杆这类很薄的遮挡物可以被看穿。两者都读场景深度、以半分辨率运行,并且模糊时不会跨过深度边缘。质量档位用同一个环境光遮蔽开关管理两者,低档位不包含 GTAO。
diff --git a/sdk/src/postprocess/effects.ts b/sdk/src/postprocess/effects.ts
index efc966fdc..13c05ff57 100644
--- a/sdk/src/postprocess/effects.ts
+++ b/sdk/src/postprocess/effects.ts
@@ -4,6 +4,7 @@ import type { ShaderHandle } from '../render/material';
import { postProcessEffects } from './postProcessEffects';
import { lensEffects } from './lensEffects';
import { gtaoPasses } from './gtao';
+import { subsurfacePasses } from './subsurface';
import { SHARPEN_WGSL } from './computeEffects';
export interface EffectUniformDef {
@@ -546,6 +547,27 @@ registerEffect({
],
});
+registerEffect({
+ type: 'subsurface',
+ label: 'Subsurface Scattering',
+ factory: () => subsurfacePasses.createSubsurface(0),
+ // Light that enters a marked surface and leaves it nearby: the lit picture blurred along the surface, each colour by its
+ // own amount. Which surfaces: the ones whose material holds a Subsurface above zero (the Model material's `u_subsurface`).
+ uniforms: [
+ // A world length: how far the widest colour travels. Skin is a few millimetres; wax and marble are centimetres.
+ { name: 'u_width', label: 'Width', min: 0.1, max: 80, step: 0.1, defaultValue: 8 },
+ { name: 'u_strength', label: 'Strength', min: 0, max: 1, step: 0.01, defaultValue: 1, neutralValue: 0 },
+ ],
+ // The colour sets the proportions between the channels: the brightest travels the full width.
+ colors: [
+ { name: 'u_color', label: 'Scatter Colour', defaultValue: { r: 1, g: 0.45, b: 0.25, a: 1 } },
+ ],
+ multiPass: [
+ { name: 'sss_x', factory: () => subsurfacePasses.createSubsurface(0) },
+ { name: 'sss_y', factory: () => subsurfacePasses.createSubsurface(1) },
+ ],
+});
+
registerEffect({
type: 'gtao',
label: 'Ground-Truth Ambient Occlusion',
diff --git a/sdk/src/postprocess/subsurface.ts b/sdk/src/postprocess/subsurface.ts
new file mode 100644
index 000000000..a4c8da13c
--- /dev/null
+++ b/sdk/src/postprocess/subsurface.ts
@@ -0,0 +1,127 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * @file subsurface.ts
+ * @brief Screen-space subsurface scattering: light that enters a surface and leaves it somewhere nearby.
+ * @details A surface that scatters light (skin, wax, marble, a leaf) shows the lit picture blurred along the surface, and each
+ * colour blurred by a different amount: red travels far, blue barely. This is that blur, separable (an x pass and a y
+ * pass), run on the finished picture over the pixels a material marked as scattering.
+ *
+ * The mark is the scene colour's alpha: a material with `u_subsurface` above zero draws opaque pixels at 0.996 instead of 1,
+ * which is invisible on screen and survives every pass between the scene and this one (and a multisample edge average).
+ * The second pass writes alpha 1 again, so nothing downstream sees the mark. A pixel that is not marked is passed
+ * through, and a marked pixel gathers only from marked neighbours on the same surface: a depth step stops the blur, which is
+ * what keeps an ear from bleeding into the wall behind it.
+ *
+ * Limits worth knowing: it blurs the lit colour, speculars included, so a sharp highlight on a scattering surface is
+ * softened with it; and the width belongs to the effect, not the material, so every scattering material in view shares it.
+ */
+import type { ShaderHandle } from '../render/material';
+import { Material } from '../render/material';
+import { SSAO_GLSL, SSAO_WGSL } from './postProcessEffects';
+
+export const subsurfacePasses = {
+ createSubsurface(axis: 0 | 1): ShaderHandle {
+ const dir = axis === 0 ? 'vec2(1.0, 0.0)' : 'vec2(0.0, 1.0)';
+ const dirW = axis === 0 ? 'vec2f(1.0, 0.0)' : 'vec2f(0.0, 1.0)';
+ // The last pass gives the alpha back; the first keeps the mark for the second to read.
+ const outAlpha = axis === 0 ? 'c.a' : '1.0';
+ const source = `#pragma shader "PP Subsurface ${axis.toFixed(0)}"
+#pragma version 300 es
+#pragma domain PostProcess
+#pragma param u_width float default(8) range(0.1,80)
+#pragma param u_strength float default(1) range(0,1)
+#pragma param u_color color default(1,0.45,0.25,1)
+
+#pragma fragment
+precision highp float;
+
+in vec2 v_texCoord;
+uniform sampler2D u_texture;
+uniform highp sampler2D u_sceneDepth;
+out vec4 fragColor;
+
+${SSAO_GLSL}
+
+bool scatters(float a) { return a < 0.9985 && a > 0.9; }
+
+void main() {
+ vec4 c = texture(u_texture, v_texCoord);
+ if (!scatters(c.a)) { fragColor = vec4(c.rgb, ${outAlpha}); return; }
+
+ ivec2 dsize = textureSize(u_sceneDepth, 0);
+ ivec2 t = screenTexel(v_texCoord, dsize);
+ float d = depthAt(t, dsize);
+ vec2 cuv = uvOfTexel(t, dsize);
+ vec3 P = worldFromDepth(cuv, d);
+ float worldPerTexel = max(length(worldFromDepth(cuv + vec2(1.0 / float(dsize.x), 0.0), d) - P), 1e-6);
+ // How far the widest colour reaches, in texels: a world length, so the blur keeps its size as the camera moves away.
+ float reach = min(u_width / worldPerTexel, 48.0);
+ if (reach < 0.75) { fragColor = vec4(c.rgb, ${outAlpha}); return; }
+
+ // Each colour's share of the widest: the brightest of the tint reaches all the way, the others less.
+ vec3 k = max(u_color.rgb / max(max(u_color.r, u_color.g), u_color.b), vec3(0.12));
+ vec2 texel = 1.0 / vec2(textureSize(u_texture, 0));
+ const int N = 6;
+ vec3 sum = c.rgb;
+ vec3 weight = vec3(1.0);
+ for (int i = 1; i <= N; ++i) {
+ float x = float(i) / float(N);
+ vec3 w = exp(-0.5 * (x * x) / (0.25 * k * k));
+ for (int side = -1; side <= 1; side += 2) {
+ vec2 uv = v_texCoord + ${dir} * (float(side) * x * reach) * texel;
+ vec4 s = texture(u_texture, uv);
+ if (!scatters(s.a)) continue;
+ float ds = depthAt(screenTexel(uv, dsize), dsize);
+ // Another surface, nearer or further than can be reached across this one, does not scatter into it.
+ if (length(worldFromDepth(uv, ds) - P) > u_width * 1.6) continue;
+ sum += s.rgb * w;
+ weight += w;
+ }
+ }
+ fragColor = vec4(mix(c.rgb, sum / weight, u_strength), ${outAlpha});
+}
+#pragma end
+
+#pragma fragment wgsl
+${SSAO_WGSL}
+
+fn scatters(a : f32) -> bool { return a < 0.9985 && a > 0.9; }
+
+@fragment fn fs_main(v : VSOut) -> @location(0) vec4f {
+ let c = textureSampleLevel(t0, s0, v.v_texCoord, 0.0);
+ if (!scatters(c.a)) { return vec4f(c.rgb, ${outAlpha}); }
+
+ let dsize = vec2i(textureDimensions(t7, 0));
+ let t = screenTexel(v.v_texCoord, dsize);
+ let d = depthAt(t, dsize);
+ let cuv = uvOfTexel(t, dsize);
+ let P = worldFromDepth(cuv, d);
+ let worldPerTexel = max(length(worldFromDepth(cuv + vec2f(1.0 / f32(dsize.x), 0.0), d) - P), 1e-6);
+ let reach = min(mc.u_width / worldPerTexel, 48.0);
+ if (reach < 0.75) { return vec4f(c.rgb, ${outAlpha}); }
+
+ let k = max(mc.u_color.rgb / max(max(mc.u_color.r, mc.u_color.g), mc.u_color.b), vec3f(0.12));
+ let texel = 1.0 / vec2f(textureDimensions(t0, 0));
+ var sum = c.rgb;
+ var weight = vec3f(1.0);
+ for (var i : i32 = 1; i <= 6; i = i + 1) {
+ let x = f32(i) / 6.0;
+ let w = exp(-0.5 * (x * x) / (0.25 * k * k));
+ for (var side : i32 = -1; side <= 1; side = side + 2) {
+ let uv = v.v_texCoord + ${dirW} * (f32(side) * x * reach) * texel;
+ let s = textureSampleLevel(t0, s0, uv, 0.0);
+ if (!scatters(s.a)) { continue; }
+ let ds = depthAt(screenTexel(uv, dsize), dsize);
+ if (length(worldFromDepth(uv, ds) - P) > mc.u_width * 1.6) { continue; }
+ sum = sum + s.rgb * w;
+ weight = weight + w;
+ }
+ }
+ return vec4f(mix(c.rgb, sum / weight, vec3f(mc.u_strength)), ${outAlpha});
+}
+#pragma end
+`;
+ return Material.compileShader(source);
+ },
+};
diff --git a/sdk/src/render/builtinShaders.ts b/sdk/src/render/builtinShaders.ts
index 959e0a7d3..080e9b3d1 100644
--- a/sdk/src/render/builtinShaders.ts
+++ b/sdk/src/render/builtinShaders.ts
@@ -377,6 +377,9 @@ const MODEL = `#pragma shader "Model"
#pragma param u_thickness float default(0) range(0,1) ui(slider)
#pragma param u_transmission float default(0) range(0,1) ui(slider)
#pragma param u_attenuationColor color default(1,1,1,1)
+// Marks this surface as one light scatters through (skin, wax, a leaf): its pixels are drawn with alpha 0.996, which the
+// Subsurface Scattering effect reads and blurs along the surface. Does nothing without that effect; the effect gives alpha back.
+#pragma param u_subsurface float default(0) range(0,1) ui(slider)
// The picture of the opaque scene as it stood behind this surface, which the frame takes a copy of before it draws a material
// that holds a transmission above zero (and only then: a draw that reads it is made after the copy). The white it falls back
// to has alpha 1, and a real copy has 0.5 — which is how this tells whether it has one.
@@ -533,6 +536,7 @@ void main() {
}
vec3 lit = applyLightingSurface(base.rgb * (1.0 - u_transmission * refracted), N, P, viewDir,
u_metallic * mr.b, roughness, u_specular, ao, ext);
+ if (u_subsurface > 0.0 && u_alphaCutoff <= 0.0) alpha = min(alpha, 0.996);
fragColor = vec4(lit + seen + u_emissive.rgb * texture(u_emissiveMap, v_texCoord).rgb, alpha);
#ifdef ES_GBUFFER
// Alpha says something was drawn here (0 is "nothing wrote"), and carries the metalness above 0.75: a pixel that
@@ -670,6 +674,7 @@ fn packNormal(n : vec3f) -> vec2f {
}
let lit = applyLightingSurface(base.rgb * (1.0 - mc.u_transmission * refracted), N, P, viewDir,
mc.u_metallic * mr.b, roughness, mc.u_specular, ao, ext);
+ if (mc.u_subsurface > 0.0 && mc.u_alphaCutoff <= 0.0) { alpha = min(alpha, 0.996); }
let emit = mc.u_emissive.rgb
* textureSampleGrad(u_emissiveMap, u_emissiveMap_s, v.v_texCoord, duvx, duvy).rgb;
#ifdef ES_GBUFFER
diff --git a/sdk/src/render/quality.ts b/sdk/src/render/quality.ts
index 52d8ecf9e..1f9382a78 100644
--- a/sdk/src/render/quality.ts
+++ b/sdk/src/render/quality.ts
@@ -60,7 +60,7 @@ const PROFILES: Record<QualityLevel, QualityProfile> = {
shadowAtlasSize: 1024, shadowCellSize: 256, shadowCascades: 2,
planarReflectionScale: 0.25, planarReflectionMaxSize: 512, planarReflectionUpdatesPerSecond: 0,
shadowDistance: 0, particleLimit: 1000, postProcess: true, ssao: false,
- skipEffects: ['ssr', 'taa', 'depthOfField', 'lightShafts', 'motionBlur', 'lensFlare', 'gtao'],
+ skipEffects: ['ssr', 'taa', 'depthOfField', 'lightShafts', 'motionBlur', 'lensFlare', 'gtao', 'subsurface'],
},
medium: {
renderScale: 1, minRenderScale: 0.65, msaaSamples: 2,
diff --git a/sdk/tests/model-shader-layers.test.ts b/sdk/tests/model-shader-layers.test.ts
index bcdfef812..0f37981d3 100644
--- a/sdk/tests/model-shader-layers.test.ts
+++ b/sdk/tests/model-shader-layers.test.ts
@@ -17,7 +17,7 @@ const [glsl, wgsl] = [source.slice(0, split), source.slice(split)];
const LAYERS = [
'u_ior', 'u_specular', 'u_clearcoat', 'u_clearcoatRoughness', 'u_sheenColor', 'u_sheenRoughness', 'u_anisotropy',
'u_anisotropyRotation', 'u_translucency', 'u_translucencyColor', 'u_thickness', 'u_transmission', 'u_attenuationColor',
- 'u_iridescence', 'u_iridescenceIor', 'u_iridescenceThickness',
+ 'u_iridescence', 'u_iridescenceIor', 'u_iridescenceThickness', 'u_subsurface', 'u_refractionStrength',
];
describe('the Model shader\'s shading layers', () => {
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index 19b69e794..b17b9ae0a 100644
--- a/tools/releaseNotes.mjs
+++ b/tools/releaseNotes.mjs
@@ -2493,6 +2493,8 @@ export const NOTED = {
{"note":"Skinned meshes are followed by their bones, from the palette they were last drawn with."},
"feat(render): fog follows the scene's sun and can walk the air against its shadow map":
{"note":"Height Fog and Light Shafts follow the scene's own sun, and Height Fog can carry the sun's shadows as volumetric light."},
+ "feat(render): screen-space subsurface scattering blurs the lit colour of surfaces a material marks":
+ {"note":"A Subsurface Scattering effect blurs the lit colour of the surfaces a material marks as scattering, red furthest, without bleeding across a depth edge."},
"feat(render): a transmissive material refracts the opaque scene behind it":
{"note":"Glass and water refract: a material with transmission bends and tints the opaque scene behind it, softened by its roughness."},
"feat(render): ground-truth ambient occlusion joins the post-process effects":