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): materials carry a clear coat, sheen, anisotropy, translucency, thin film, index of refraction and transmission

yhhed56e1e32b0a
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 60e4cee97..2ecea6179 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.
+- **Shading layers.** The Model material has shading layers — clear coat, sheen, anisotropy, translucency, thin film, index of refraction and transmission — and a glTF with the matching extensions arrives using them. Clear coat puts a second, sharper highlight over the base and darkens what is under it; sheen is the soft grazing-angle light of cloth; anisotropy stretches a highlight across brushed grooves; translucency lets light wrap past the terminator and show through from behind; thin film shifts the reflection's hue with the angle; the index of refraction sets how strongly a non-metal reflects; transmission lets a surface show what is behind it, drawn blended with no refraction. Custom Lit shaders reach the same lighting through `applyLightingSurface` and a `SurfaceExt`. A layer's textures are not read — its factors are — and the import says so; a material that sets none of them draws as before.
- **Many lights.** A frame now carries up to 112 lights: sixteen are read for every pixel and the rest are tiled, so a street of lamps lights what each one reaches. The sun, the lights that cast a shadow and then the brightest go in the lighting loop as before; every other point and spot light without a shadow map is recorded per screen tile (16 x 12) and shaded only by the pixels it reaches, so the cost follows the lights near a pixel and not the lights in the scene. Tiled lights do not cast shadows, and the whole lighting block still fits the 16 KB a uniform block is guaranteed, so it runs on WebGL2 and WebGPU alike.
- **Play on WebGPU.** Play runs the game on the graphics backend the project asks for, so a WebGPU project is played on WebGPU and its compute effects and Voxel GI are seen in the editor. Until now Play always ran on WebGL2 whatever the project asked for; it now follows `features.rendering.backend` the way a shipped build does, and falls back to WebGL2 on a machine with no WebGPU.
- **Voxel GI.** Voxel GI adds diffuse bounce light from a world-space voxel cache the camera remembers, on WebGPU. The cache is a voxel grid around the camera that each frame takes in the lit picture (the radiance and facing of the surfaces it sees) and fades what it held; light is gathered by marching cosine-weighted rays through it, so a wall that has left the screen still tints the floor beside it and a moving light lights its surroundings, which Screen Space GI cannot do. It holds only what the camera has seen, at the voxel's size, with one diffuse bounce: it is not a trace of the scene's geometry, and not Lumen. A device with no compute stage goes without it.
diff --git a/desktop b/desktop
index 87edafec7..73b1187ca 160000
--- a/desktop
+++ b/desktop
@@ -1 +1 @@
-Subproject commit 87edafec7c5c2c46012c358f11a99f1829bd584c
+Subproject commit 73b1187cadf62599e6a27e3336a64bae3b163abb
diff --git a/docs/astro/src/content/docs/assets/models.mdx b/docs/astro/src/content/docs/assets/models.mdx
index dbb707c24..b316bb974 100644
--- a/docs/astro/src/content/docs/assets/models.mdx
+++ b/docs/astro/src/content/docs/assets/models.mdx
@@ -188,6 +188,13 @@ component has no room for. Written only when the source uses one of them, on the
| `occlusionTexture` (+ `strength`) | `u_occlusionMap` / `u_occlusionStrength` |
| `alphaMode: MASK` + `alphaCutoff` | `u_alphaCutoff` |
| `metallicFactor` / `roughnessFactor` / `metallicRoughnessTexture` | `u_metallic` / `u_roughness` / `u_metallicRoughnessMap` |
+| `KHR_materials_clearcoat` | `u_clearcoat` / `u_clearcoatRoughness` |
+| `KHR_materials_sheen` | `u_sheenColor` / `u_sheenRoughness` |
+| `KHR_materials_anisotropy` | `u_anisotropy` / `u_anisotropyRotation` |
+| `KHR_materials_ior`, `KHR_materials_specular` (`specularFactor`) | `u_ior`, `u_specular` |
+| `KHR_materials_iridescence` | `u_iridescence` / `u_iridescenceIor` / `u_iridescenceThickness` (the middle of the min–max range) |
+| `KHR_materials_diffuse_transmission` | `u_translucency` / `u_translucencyColor` |
+| `KHR_materials_transmission`, `KHR_materials_volume` | `u_transmission`, `u_thickness`, `u_attenuationColor` — and the material draws blended |
Metal and roughness are written even when they match the engine's own defaults: glTF
defaults both factors to 1 (a fully rough metal) and the shader defaults metal to 0, so a
@@ -289,6 +296,7 @@ when it runs.
| A second UV set (`TEXCOORD_1`), `KHR_texture_transform`, an FBX uv transform | UVs are used as authored. |
| Non-triangle primitives (`mode` ≠ 4) | The renderer draws triangle lists. |
| `normalTexture.scale` | The map is applied at full strength. |
+| The textures of a shading layer (`clearcoatTexture`, `sheenColorTexture`, `anisotropyTexture`, …), `specularColorFactor`, `attenuationDistance` | Each layer is a constant for the whole material; its factors are kept and the textures are reported. See [Shading layers](/docs/graphics/materials/#shading-layers). |
| Cameras and lights in an FBX | A scene's camera and lighting are the scene's, not the model's. |
| Separate FBX metalness/roughness maps, an FBX opacity map | See [FBX](#fbx). |
diff --git a/docs/astro/src/content/docs/graphics/materials.mdx b/docs/astro/src/content/docs/graphics/materials.mdx
index ca25ccaa5..d2c75c15a 100644
--- a/docs/astro/src/content/docs/graphics/materials.mdx
+++ b/docs/astro/src/content/docs/graphics/materials.mdx
@@ -32,7 +32,7 @@ creates a material from a built-in template — the same list ships in code as
| **Dissolve** | `sprite-dissolve` | Noise burn-away with a glowing edge (`u_progress` 0→1). |
| **Pixelate** | `sprite-pixelate` | Quantizes UVs to a `u_pixels` grid. |
| **UV Scroll** | `sprite-uv-scroll` | Scrolls the texture by `u_scrollSpeed` × time. |
-| **Model** | `model` | What a glTF material says beyond base color: `u_normalMap`, `u_emissive`(`Map`), `u_occlusionMap` + `u_occlusionStrength`, `u_alphaCutoff`, `u_metallic` / `u_roughness` (+ `u_metallicRoughnessMap`). Written by model import. |
+| **Model** | `model` | What a glTF material says beyond base color: `u_normalMap`, `u_emissive`(`Map`), `u_occlusionMap` + `u_occlusionStrength`, `u_alphaCutoff`, `u_metallic` / `u_roughness` (+ `u_metallicRoughnessMap`), and the [shading layers](#shading-layers) — clear coat, sheen, anisotropy, translucency, thin film, index of refraction, transmission. Written by model import. |
A new material **references** its template by id — it stores `shader: "builtin:sprite-unlit"`
and writes **no shader file** to disk. The engine compiles the built-in from in-code source
@@ -41,6 +41,43 @@ so a fresh material draws immediately with nothing extra to manage. A real `.ess
lands on disk when you **convert** the material to a unique shader (below) or **create a
shader** yourself.
+## Shading layers
+
+The **Model** shader is a metal/rough surface with layers on top. Each is off at its default, a layer at zero costs the surface nothing, and a material that says none of them is the plain surface it always was.
+
+| Layer | Parameters | What it does |
+|---|---|---|
+| Clear coat | `u_clearcoat`, `u_clearcoatRoughness` | A smooth lacquer over the surface: a second, sharper highlight, the base underneath darkened by what the coat reflects, and an environment reflection of its own. The coat is lit by the surface's own normal, so a bump map roughens what is under it and not the lacquer. Car paint, varnished wood, wet stone. |
+| 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. |
+| 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` | Glass and water: the surface lets that much of what is behind show, tinted, and reflects more at grazing angles. See the limits. |
+
+Everything here is written by model import from the glTF extensions of the same names (`KHR_materials_clearcoat`, `_sheen`, `_anisotropy`, `_ior`, `_specular`, `_iridescence`, `_transmission`, `_volume`, `_diffuse_transmission`) — see [Models](/docs/assets/models/).
+
+**Limits.** These are cheap, real-time models, and say where they stop:
+
+- **Transmission shows no refraction.** There is no picture of the opaque scene to bend, so a transmissive surface is drawn alpha-blended: the background behind it is undistorted, and the highlights on it are dimmed by the same blend. Pull the transmission down for a surface that must keep its highlights.
+- **Translucency does not blur.** There is no screen-space subsurface pass; the light that enters at one point leaves at that point.
+- **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.
+
+### In your own shader
+
+A `Lit` shader reaches the same lighting. `applyLightingSurface(albedo, N, worldPos, V, metallic, roughness, specular, ao, ext)` is `applyLightingPBR` with the layers; start `ext` from `surfaceExtNone(N)` and set what you use:
+
+| Field | Holds |
+|---|---|
+| `ext.coat` | `x` strength, `y` perceptual roughness; `ext.coatN.xyz` the coat's normal |
+| `ext.sheen` | `rgb` colour (black is off), `a` perceptual roughness |
+| `ext.aniso` | `xyz` tangent (unit, across `N`), `w` strength -1..1 (0 is off) |
+| `ext.scatter` | `rgb` colour the scattered light takes, `a` wrap 0..1; `ext.trans.x` how much passes through, `ext.trans.y` thickness 0..1 |
+| `ext.film` | `x` strength, `y` film index, `z` thickness in nanometres |
+| `ext.misc.x` | reflectance at normal incidence of a non-metal (0.04 for an index of 1.5) |
+
## Choosing & sharing shaders in the editor
Select a material in the Content Browser and the Details panel opens its inspector with a
diff --git a/docs/astro/src/content/docs/zh-cn/assets/models.mdx b/docs/astro/src/content/docs/zh-cn/assets/models.mdx
index 67dc2a8cc..7692ea8b6 100644
--- a/docs/astro/src/content/docs/zh-cn/assets/models.mdx
+++ b/docs/astro/src/content/docs/zh-cn/assets/models.mdx
@@ -162,6 +162,13 @@ chart,每片投影到自己的平面,再按**同一个**世界-纹素比打包
| `occlusionTexture`(含 `strength`) | `u_occlusionMap` / `u_occlusionStrength` |
| `alphaMode: MASK` + `alphaCutoff` | `u_alphaCutoff` |
| `metallicFactor` / `roughnessFactor` / `metallicRoughnessTexture` | `u_metallic` / `u_roughness` / `u_metallicRoughnessMap` |
+| `KHR_materials_clearcoat` | `u_clearcoat` / `u_clearcoatRoughness` |
+| `KHR_materials_sheen` | `u_sheenColor` / `u_sheenRoughness` |
+| `KHR_materials_anisotropy` | `u_anisotropy` / `u_anisotropyRotation` |
+| `KHR_materials_ior`、`KHR_materials_specular`(`specularFactor`) | `u_ior`、`u_specular` |
+| `KHR_materials_iridescence` | `u_iridescence` / `u_iridescenceIor` / `u_iridescenceThickness`(取最小–最大范围的中间值) |
+| `KHR_materials_diffuse_transmission` | `u_translucency` / `u_translucencyColor` |
+| `KHR_materials_transmission`、`KHR_materials_volume` | `u_transmission`、`u_thickness`、`u_attenuationColor`——材质按混合方式绘制 |
金属度与粗糙度即使等于引擎自己的默认值也会写出来:glTF 把两个系数都默认成 1(完全粗糙的
金属),而 shader 把金属度默认成 0 —— 省略它们的产物会描述出一个源文件没有说过的表面。
@@ -253,6 +260,7 @@ Physical、Blender 的 Principled 最终都落到[材质](#材质)那一节列
| 第二套 UV(`TEXCOORD_1`)、`KHR_texture_transform`、FBX 的 uv transform | UV 按作者写的原样使用。 |
| 非三角形 primitive(`mode` ≠ 4) | 渲染器画的是三角形列表。 |
| `normalTexture.scale` | 法线贴图按全强度应用。 |
+| 着色层的贴图(`clearcoatTexture`、`sheenColorTexture`、`anisotropyTexture` 等)、`specularColorFactor`、`attenuationDistance` | 每一层对整个材质是一个常量;系数保留,贴图只做报告。见[着色层](/docs/zh-cn/graphics/materials/#着色层)。 |
| FBX 里的相机与灯光 | 场景的相机与灯光属于场景,不属于模型。 |
| FBX 分离的金属度/粗糙度贴图、FBX 的不透明度贴图 | 见 [FBX](#fbx)。 |
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 d05a40316..5f905331b 100644
--- a/docs/astro/src/content/docs/zh-cn/graphics/materials.mdx
+++ b/docs/astro/src/content/docs/zh-cn/graphics/materials.mdx
@@ -28,13 +28,50 @@ std140 uniform 块,于是 CPU 能按名字设置、并安全地送到 GPU。一
| **Dissolve** | `sprite-dissolve` | 噪声烧蚀 + 发光边缘(`u_progress` 0→1)。 |
| **Pixelate** | `sprite-pixelate` | UV 量化到 `u_pixels` 网格。 |
| **UV Scroll** | `sprite-uv-scroll` | 按 `u_scrollSpeed` × 时间滚动纹理。 |
-| **Model** | `model` | glTF 材质在 base color 之外说的东西:`u_normalMap`、`u_emissive`(`Map`)、`u_occlusionMap` + `u_occlusionStrength`、`u_alphaCutoff`、`u_metallic` / `u_roughness`(以及 `u_metallicRoughnessMap`)。模型导入会写出它。 |
+| **Model** | `model` | glTF 材质在 base color 之外说的东西:`u_normalMap`、`u_emissive`(`Map`)、`u_occlusionMap` + `u_occlusionStrength`、`u_alphaCutoff`、`u_metallic` / `u_roughness`(以及 `u_metallicRoughnessMap`),还有[着色层](#着色层)——清漆、绒面、各向异性、半透、薄膜干涉、折射率、透射。模型导入会写出它。 |
新建材质是**按 id 引用**其模板的——它存的是 `shader: "builtin:sprite-unlit"`,并**不会**在磁盘上
写出 shader 文件。引擎从内置源码编译该模板(运行时加载器、cook 步骤、编辑器三处以同样方式解析
`builtin:<id>`),所以新材质立即就能绘制,无需额外管理。只有当你把材质**转为独立着色器**(见下文)
或自己**新建着色器**时,才会有真正的 `.esshader` 落到磁盘上。
+## 着色层
+
+**Model** shader 是一个金属/粗糙度表面,上面可以叠层。每一层默认关闭,强度为零的层不消耗任何开销,一个什么都不写的材质就是它一直以来的那个普通表面。
+
+| 层 | 参数 | 作用 |
+|---|---|---|
+| 清漆(Clear coat) | `u_clearcoat`、`u_clearcoatRoughness` | 表面上一层光滑的漆:第二个更锐利的高光,底层被清漆反射走的那部分变暗,并带有自己的环境反射。清漆用表面自身的法线着色,所以法线贴图只会让漆下面变粗糙,不会让漆变粗糙。车漆、清漆木头、湿石头。 |
+| 绒面(Sheen) | `u_sheenColor`、`u_sheenRoughness` | 布料在掠射角回弹的柔光(Charlie 瓣),并从底层里扣掉对应能量,不凭空增加亮度。天鹅绒、缎面、毛毡。 |
+| 各向异性 | `u_anisotropy`(-1..1)、`u_anisotropyRotation` | 高光沿垂直于纹路的方向被拉长而不是圆的:拉丝金属、纤维、光盘。纹路沿 UV 坐标系按旋转角转过后的方向;环境反射也按同一方向弯折。 |
+| 半透(Translucency) | `u_translucency`、`u_translucencyColor`、`u_thickness` | 光散射进薄的表面:受光面越过明暗交界线向背光侧延伸,背后的灯会透出来并带上颜色,越厚越弱。树叶、纸、蜡、灯罩、耳朵边缘的皮肤。 |
+| 薄膜干涉 | `u_iridescence`、`u_iridescenceIor`、`u_iridescenceThickness`(nm) | 反射的色相随角度变化:肥皂泡、油膜、甲虫壳、镀膜玻璃。 |
+| 折射率 | `u_ior`、`u_specular` | 非金属正对时反射有多强(1.5 是玻璃和大多数塑料,宝石是 2.4),以及反射的总缩放。 |
+| 透射(Transmission) | `u_transmission`、`u_attenuationColor` | 玻璃和水:表面让背后的东西按该比例透出来并带上颜色,掠射角处反射更强。见下方限制。 |
+
+这些都由模型导入从同名的 glTF 扩展写出(`KHR_materials_clearcoat`、`_sheen`、`_anisotropy`、`_ior`、`_specular`、`_iridescence`、`_transmission`、`_volume`、`_diffuse_transmission`)——见[模型](/docs/zh-cn/assets/models/)。
+
+**限制。** 这些是便宜的实时模型,下面说清楚它们止步在哪里:
+
+- **透射没有折射。** 没有不透明场景的画面可以拿来扭曲,所以透射表面按 alpha 混合绘制:它后面的背景不会变形,表面上的高光也会被同一次混合压暗。需要保住高光的表面请把透射调低。
+- **半透不做模糊。** 没有屏幕空间次表面散射 pass;从某一点进入的光就在这一点出去。
+- **不读取层的贴图。** 每一层对整个材质是一个常量(Model shader 已经没有多余的纹理单元);glTF 扩展的贴图在导入时会报告,其系数会保留。
+- **绒面和薄膜是拟合。** 绒面的能量损失和薄膜的干涉是解析近似,代替了生产级渲染器烘焙的查找表。
+- **不提供:** 头发(Marschner 式的发丝模型)、眼睛、清漆单独的法线贴图、超过一层清漆的分层材质。
+
+### 在你自己的 shader 里
+
+`Lit` shader 可以用同一套光照。`applyLightingSurface(albedo, N, worldPos, V, metallic, roughness, specular, ao, ext)` 就是带层的 `applyLightingPBR`;用 `surfaceExtNone(N)` 起一个 `ext`,再设置你用到的字段:
+
+| 字段 | 含义 |
+|---|---|
+| `ext.coat` | `x` 强度,`y` 感知粗糙度;`ext.coatN.xyz` 清漆的法线 |
+| `ext.sheen` | `rgb` 颜色(黑色为关闭),`a` 感知粗糙度 |
+| `ext.aniso` | `xyz` 切线(单位向量,垂直于 `N`),`w` 强度 -1..1(0 为关闭) |
+| `ext.scatter` | `rgb` 散射光的颜色,`a` 包裹 0..1;`ext.trans.x` 透过多少,`ext.trans.y` 厚度 0..1 |
+| `ext.film` | `x` 强度,`y` 膜的折射率,`z` 厚度(纳米) |
+| `ext.misc.x` | 非金属正对时的反射率(折射率 1.5 对应 0.04) |
+
## 在编辑器里选取与共享着色器
在内容浏览器里选中一个材质,Details 面板会打开它的检视器,顶部(参数上方)是一个 **Shader** 区。
diff --git a/pipeline/src/assets/gltfImport.ts b/pipeline/src/assets/gltfImport.ts
index 1b2f0c109..c90a5fe12 100644
--- a/pipeline/src/assets/gltfImport.ts
+++ b/pipeline/src/assets/gltfImport.ts
@@ -576,10 +576,19 @@ function readMaterial(ctx: MaterialContext, textureCache: Map<number, ImportedIm
if (src.normalTexture?.scale !== undefined && src.normalTexture.scale !== 1) {
unused.push(`normal-map scale ${src.normalTexture.scale}`);
}
- for (const name of Object.keys(src.extensions ?? {})) {
- if (name !== 'KHR_materials_emissive_strength') unused.push(name);
+ const partial: string[] = [];
+ for (const [name, value] of Object.entries(src.extensions ?? {})) {
+ if (!SHADING_EXTENSIONS.has(name)) unused.push(name);
+ // A layer's maps are not carried (the Model shader has no unit left for them): its factors are, and what a
+ // map would have varied across the surface is said to be flat.
+ else if (value && typeof value === 'object'
+ && Object.keys(value).some(key => key.endsWith('Texture'))) partial.push(name);
}
if (unused.length > 0) ctx.warnings.push(`${label}: ${unused.join(', ')} not imported`);
+ if (partial.length > 0) {
+ ctx.warnings.push(`${label}: the textures of ${partial.join(', ')} are not imported (their factors are)`);
+ }
+ const layers = readShadingLayers(src.extensions ?? {});
const texture = pbr.baseColorTexture
? readTexture(ctx, textureCache, pbr.baseColorTexture, label) : null;
@@ -611,8 +620,9 @@ function readMaterial(ctx: MaterialContext, textureCache: Map<number, ImportedIm
baseColor: [factor[0] ?? 1, factor[1] ?? 1, factor[2] ?? 1, factor[3] ?? 1],
// A glTF is OPAQUE and single-sided unless it says otherwise, so a model
// arrives occluding itself the way it was authored to. MASK is opaque
- // too — what it adds is the cutoff below.
- opaque: (src.alphaMode ?? 'OPAQUE') !== 'BLEND',
+ // too — what it adds is the cutoff below. Transmission lets what is behind
+ // show, which a surface that writes depth and blends nothing cannot.
+ opaque: (src.alphaMode ?? 'OPAQUE') !== 'BLEND' && !((layers.transmission ?? 0) > 0),
cullBackfaces: src.doubleSided !== true,
...(texture ? { baseColorTexture: texture } : {}),
...(normal ? { normalTexture: normal } : {}),
@@ -628,9 +638,86 @@ function readMaterial(ctx: MaterialContext, textureCache: Map<number, ImportedIm
roughness,
...(metalRoughMap ? { metallicRoughnessTexture: metalRoughMap } : {}),
...(src.alphaMode === 'MASK' ? { alphaCutoff: src.alphaCutoff ?? 0.5 } : {}),
+ ...layers,
};
}
+/** The extensions that say something about SHADING and are read into the Model shader's layers. */
+const SHADING_EXTENSIONS = new Set([
+ 'KHR_materials_emissive_strength', 'KHR_materials_clearcoat', 'KHR_materials_sheen', 'KHR_materials_anisotropy',
+ 'KHR_materials_ior', 'KHR_materials_specular', 'KHR_materials_transmission', 'KHR_materials_volume',
+ 'KHR_materials_diffuse_transmission', 'KHR_materials_iridescence',
+]);
+
+type Layers = Pick<ImportedMaterial, 'clearcoat' | 'clearcoatRoughness' | 'sheenColor' | 'sheenRoughness' | 'anisotropy'
+ | 'anisotropyRotation' | 'ior' | 'specular' | 'translucency' | 'translucencyColor' | 'transmission' | 'thickness'
+ | 'attenuationColor' | 'iridescence' | 'iridescenceIor' | 'iridescenceThickness'>;
+
+/**
+ * The shading layers a material's extensions state, as the engine's own parameters. A number that is not one, or a
+ * colour that is not three of them, is read as the extension's default — an exporter's slip is not worth refusing a
+ * model for. Only what the source SAYS is written, so a plain material still needs no product at all.
+ */
+function readShadingLayers(extensions: Record<string, unknown>): Layers {
+ const ext = (name: string): Record<string, unknown> | undefined => {
+ const value = extensions[name];
+ return value && typeof value === 'object' ? value as Record<string, unknown> : undefined;
+ };
+ const num = (value: unknown, fallback: number): number =>
+ typeof value === 'number' && Number.isFinite(value) ? value : fallback;
+ const rgb = (value: unknown, fallback: [number, number, number]): [number, number, number] =>
+ Array.isArray(value) && value.length >= 3 && value.slice(0, 3).every(v => typeof v === 'number' && Number.isFinite(v))
+ ? [value[0], value[1], value[2]] : fallback;
+ const out: Layers = {};
+
+ const coat = ext('KHR_materials_clearcoat');
+ if (coat && num(coat.clearcoatFactor, 0) > 0) {
+ out.clearcoat = num(coat.clearcoatFactor, 0);
+ out.clearcoatRoughness = num(coat.clearcoatRoughnessFactor, 0);
+ }
+ const sheen = ext('KHR_materials_sheen');
+ if (sheen) {
+ const color = rgb(sheen.sheenColorFactor, [0, 0, 0]);
+ if (color.some(v => v > 0)) {
+ out.sheenColor = color;
+ out.sheenRoughness = num(sheen.sheenRoughnessFactor, 0);
+ }
+ }
+ const anisotropy = ext('KHR_materials_anisotropy');
+ if (anisotropy && num(anisotropy.anisotropyStrength, 0) !== 0) {
+ out.anisotropy = num(anisotropy.anisotropyStrength, 0);
+ out.anisotropyRotation = num(anisotropy.anisotropyRotation, 0);
+ }
+ const ior = ext('KHR_materials_ior');
+ if (ior && num(ior.ior, 1.5) !== 1.5) out.ior = Math.max(num(ior.ior, 1.5), 1);
+ const specular = ext('KHR_materials_specular');
+ if (specular && num(specular.specularFactor, 1) !== 1) out.specular = Math.min(Math.max(num(specular.specularFactor, 1), 0), 1);
+ const diffuse = ext('KHR_materials_diffuse_transmission');
+ if (diffuse && num(diffuse.diffuseTransmissionFactor, 0) > 0) {
+ out.translucency = Math.min(num(diffuse.diffuseTransmissionFactor, 0), 1);
+ out.translucencyColor = rgb(diffuse.diffuseTransmissionColorFactor, [1, 1, 1]);
+ }
+ const transmission = ext('KHR_materials_transmission');
+ if (transmission && num(transmission.transmissionFactor, 0) > 0) {
+ out.transmission = Math.min(num(transmission.transmissionFactor, 0), 1);
+ const volume = ext('KHR_materials_volume');
+ if (volume) {
+ const tint = rgb(volume.attenuationColor, [1, 1, 1]);
+ if (tint.some(v => v !== 1)) out.attenuationColor = tint;
+ }
+ }
+ const volume = ext('KHR_materials_volume');
+ if (volume && num(volume.thicknessFactor, 0) > 0) out.thickness = Math.min(num(volume.thicknessFactor, 0), 1);
+ const film = ext('KHR_materials_iridescence');
+ if (film && num(film.iridescenceFactor, 0) > 0) {
+ out.iridescence = Math.min(num(film.iridescenceFactor, 0), 1);
+ out.iridescenceIor = Math.max(num(film.iridescenceIor, 1.3), 1);
+ // The film's thickness is a range a map walks across; with the map unread, the middle of it.
+ out.iridescenceThickness = (num(film.iridescenceThicknessMinimum, 100) + num(film.iridescenceThicknessMaximum, 400)) / 2;
+ }
+ return out;
+}
+
/**
* A node's local transform. A glTF gives either TRS or a column-major matrix,
* and the engine's Transform holds TRS — so a matrix is decomposed here rather
diff --git a/pipeline/src/assets/modelImport.ts b/pipeline/src/assets/modelImport.ts
index 21f6fecfb..38feb93aa 100644
--- a/pipeline/src/assets/modelImport.ts
+++ b/pipeline/src/assets/modelImport.ts
@@ -84,6 +84,33 @@ export interface ImportedMaterial {
roughness?: number;
/** glTF's packing: roughness in green, metal in blue, multiplying the factors. */
metallicRoughnessTexture?: ImportedImageRef;
+ /**
+ * What the surface has beyond a metal/rough pair, one field per glTF extension that says it — the values are
+ * the extension's own factors, absent where the source says nothing (the engine's defaults are glTF's).
+ */
+ /** KHR_materials_clearcoat: a smooth lacquer over the surface. */
+ clearcoat?: number;
+ clearcoatRoughness?: number;
+ /** KHR_materials_sheen: the soft rim light of cloth. */
+ sheenColor?: [number, number, number];
+ sheenRoughness?: number;
+ /** KHR_materials_anisotropy: a highlight stretched along grooves, turned by `anisotropyRotation` radians. */
+ anisotropy?: number;
+ anisotropyRotation?: number;
+ /** KHR_materials_ior and KHR_materials_specular's factor. */
+ ior?: number;
+ specular?: number;
+ /** KHR_materials_diffuse_transmission: light that scatters through a thin surface, tinted. */
+ translucency?: number;
+ translucencyColor?: [number, number, number];
+ /** KHR_materials_transmission, and KHR_materials_volume's thickness and tint. */
+ transmission?: number;
+ thickness?: number;
+ attenuationColor?: [number, number, number];
+ /** KHR_materials_iridescence: a thin film over the surface. */
+ iridescence?: number;
+ iridescenceIor?: number;
+ iridescenceThickness?: number;
/** A fragment below this alpha is discarded. */
alphaCutoff?: number;
/** Drawn without blending, and taking part in depth. */
@@ -526,6 +553,21 @@ function materialImageRef(image: ImportedImageRef, refs: ProductRefs): string {
return ref.startsWith('assets/') || ref.startsWith('/') ? ref : `/${ref}`;
}
+/**
+ * The Model shader's parameter each layer field becomes. A colour is a `{r, g, b, a}` object and a number is itself,
+ * so one table carries them all and a field the source did not state is simply not written.
+ */
+const MATERIAL_LAYER_PROPERTIES: readonly [keyof ImportedMaterial, string][] = [
+ ['clearcoat', 'u_clearcoat'], ['clearcoatRoughness', 'u_clearcoatRoughness'],
+ ['sheenColor', 'u_sheenColor'], ['sheenRoughness', 'u_sheenRoughness'],
+ ['anisotropy', 'u_anisotropy'], ['anisotropyRotation', 'u_anisotropyRotation'],
+ ['ior', 'u_ior'], ['specular', 'u_specular'],
+ ['translucency', 'u_translucency'], ['translucencyColor', 'u_translucencyColor'],
+ ['transmission', 'u_transmission'], ['thickness', 'u_thickness'], ['attenuationColor', 'u_attenuationColor'],
+ ['iridescence', 'u_iridescence'], ['iridescenceIor', 'u_iridescenceIor'],
+ ['iridescenceThickness', 'u_iridescenceThickness'],
+];
+
/**
* Whether this material says anything a MeshRenderer's own fields cannot: shading is
* per-material constants and samplers, and the component has neither. A metal-
@@ -535,7 +577,8 @@ function materialImageRef(image: ImportedImageRef, refs: ProductRefs): string {
function needsMaterial(material: ImportedMaterial | undefined): material is ImportedMaterial {
return !!material && !!(material.normalTexture || material.emissive || material.emissiveTexture
|| material.occlusionTexture || material.alphaCutoff || material.metallicRoughnessTexture
- || (material.metallic ?? 0) !== 0 || (material.roughness ?? 1) !== 1);
+ || (material.metallic ?? 0) !== 0 || (material.roughness ?? 1) !== 1
+ || MATERIAL_LAYER_PROPERTIES.some(([field]) => material[field] !== undefined));
}
/** `<stem>_m<source material index>` — the product a material becomes. */
@@ -602,6 +645,12 @@ export function materialProducts(meshes: ImportedMesh[], stem: string,
properties.u_occlusionStrength = material.occlusionStrength;
}
if (material.alphaCutoff !== undefined) properties.u_alphaCutoff = material.alphaCutoff;
+ for (const [field, parameter] of MATERIAL_LAYER_PROPERTIES) {
+ const value = material[field];
+ if (value === undefined) continue;
+ properties[parameter] = Array.isArray(value)
+ ? { r: value[0], g: value[1], b: value[2], a: 1 } : value;
+ }
out.push({
name: materialName(stem, material),
diff --git a/pipeline/tests/gltf-import.test.ts b/pipeline/tests/gltf-import.test.ts
index 66273c6f3..8292110d0 100644
--- a/pipeline/tests/gltf-import.test.ts
+++ b/pipeline/tests/gltf-import.test.ts
@@ -210,12 +210,12 @@ describe('glTF material import', () => {
pbrMetallicRoughness: { metallicFactor: 1, roughnessFactor: 0.3 },
normalTexture: { index: 0, scale: 0.4 }, emissiveFactor: [1, 0, 0],
alphaMode: 'MASK', alphaCutoff: 0.4, doubleSided: false,
- extensions: { KHR_materials_clearcoat: {} },
+ extensions: { KHR_materials_pbrSpecularGlossiness: {} },
}];
const { meshes, warnings } = await importGltfMeshes(gltf(doc), 'model');
const line = warnings.join('\n');
expect(line).toContain('normal-map scale 0.4');
- expect(line).toContain('KHR_materials_clearcoat');
+ expect(line).toContain('KHR_materials_pbrSpecularGlossiness');
// The channels that DO reach a shader are products, not warnings — metal and
// roughness among them now that the lighting model has a view direction.
expect(line).not.toContain('metallic-roughness');
@@ -465,11 +465,62 @@ describe('glTF material products', () => {
it('defaults an omitted strength to one and still reports unsupported extensions', async () => {
const doc = shaded({
emissiveFactor: [1, 0.5, 0],
- extensions: { KHR_materials_emissive_strength: {}, KHR_materials_clearcoat: {} },
+ extensions: { KHR_materials_emissive_strength: {}, KHR_materials_pbrSpecularGlossiness: {} },
});
const { meshes, warnings } = await importGltfMeshes(gltf(doc), 'model');
expect(meshes[0]!.material?.emissive).toEqual([1, 0.5, 0]);
- expect(warnings).toEqual(['material "Shaded": KHR_materials_clearcoat not imported']);
+ expect(warnings).toEqual(['material "Shaded": KHR_materials_pbrSpecularGlossiness not imported']);
+ });
+
+ it('carries the shading layers its extensions state, as the Model shader parameters', async () => {
+ const { meshes, warnings } = await importGltfMeshes(gltf(shaded({
+ extensions: {
+ KHR_materials_clearcoat: { clearcoatFactor: 0.8, clearcoatRoughnessFactor: 0.1 },
+ KHR_materials_sheen: { sheenColorFactor: [0.9, 0.8, 0.7], sheenRoughnessFactor: 0.4 },
+ KHR_materials_anisotropy: { anisotropyStrength: 0.6, anisotropyRotation: 1.5 },
+ KHR_materials_ior: { ior: 1.8 },
+ KHR_materials_specular: { specularFactor: 0.5 },
+ KHR_materials_iridescence: { iridescenceFactor: 1, iridescenceIor: 1.4, iridescenceThicknessMinimum: 200, iridescenceThicknessMaximum: 400 },
+ KHR_materials_diffuse_transmission: { diffuseTransmissionFactor: 0.7, diffuseTransmissionColorFactor: [1, 0.5, 0.25] },
+ },
+ })), 'model');
+ const products = materialProducts(meshes, 'model');
+ expect(warnings).toEqual([]);
+ expect(meshes[0]!.material?.opaque).toBe(true);
+ expect(products[0]!.data.properties).toMatchObject({
+ u_clearcoat: 0.8, u_clearcoatRoughness: 0.1,
+ u_sheenColor: { r: 0.9, g: 0.8, b: 0.7, a: 1 }, u_sheenRoughness: 0.4,
+ u_anisotropy: 0.6, u_anisotropyRotation: 1.5, u_ior: 1.8, u_specular: 0.5,
+ u_iridescence: 1, u_iridescenceIor: 1.4, u_iridescenceThickness: 300,
+ u_translucency: 0.7, u_translucencyColor: { r: 1, g: 0.5, b: 0.25, a: 1 },
+ });
+ });
+
+ it('draws a transmissive surface blended, and tints it by the volume attenuation', async () => {
+ const { products } = await productsOf(shaded({
+ extensions: {
+ KHR_materials_transmission: { transmissionFactor: 0.9 },
+ KHR_materials_volume: { thicknessFactor: 0.3, attenuationColor: [0.5, 0.8, 1] },
+ },
+ }));
+ expect(products[0]!.data).toMatchObject({ blendMode: BlendMode.Normal, depthWrite: false });
+ expect(products[0]!.data.properties).toMatchObject({
+ u_transmission: 0.9, u_thickness: 0.3, u_attenuationColor: { r: 0.5, g: 0.8, b: 1, a: 1 },
+ });
+ });
+
+ it('writes nothing for a layer that is at its default, and says its maps are not imported', async () => {
+ const { meshes, warnings } = await importGltfMeshes(gltf(shaded({
+ extensions: {
+ KHR_materials_clearcoat: { clearcoatFactor: 0 },
+ KHR_materials_ior: { ior: 1.5 },
+ KHR_materials_sheen: { sheenColorFactor: [0, 0, 0], sheenColorTexture: { index: 0 } },
+ },
+ })), 'model');
+ expect(meshes[0]!.material?.clearcoat).toBeUndefined();
+ expect(meshes[0]!.material?.ior).toBeUndefined();
+ expect(meshes[0]!.material?.sheenColor).toBeUndefined();
+ expect(warnings).toEqual(['material "Shaded": the textures of KHR_materials_sheen are not imported (their factors are)']);
});
it.each([-1, '24', null])('reports an invalid emission strength %s', async strength => {
diff --git a/sdk/src/render/builtinShaders.ts b/sdk/src/render/builtinShaders.ts
index 7ebbc3fbd..4082c584c 100644
--- a/sdk/src/render/builtinShaders.ts
+++ b/sdk/src/render/builtinShaders.ts
@@ -362,6 +362,24 @@ const MODEL = `#pragma shader "Model"
#pragma param u_metallic float default(0) range(0,1) ui(slider)
#pragma param u_roughness float default(1) range(0,1) ui(slider)
#pragma param u_metallicRoughnessMap texture default(white) texel(data)
+// What a surface has beyond a metal/rough pair. Every one is zero (or glTF's own default) for a surface that has none of it,
+// and the lighting skips a layer whose strength is zero, so a plain material pays nothing for these.
+#pragma param u_ior float default(1.5) range(1,3) ui(slider)
+#pragma param u_specular float default(1) range(0,1) ui(slider)
+#pragma param u_clearcoat float default(0) range(0,1) ui(slider)
+#pragma param u_clearcoatRoughness float default(0.03) range(0,1) ui(slider)
+#pragma param u_sheenColor color default(0,0,0,1)
+#pragma param u_sheenRoughness float default(0.5) range(0,1) ui(slider)
+#pragma param u_anisotropy float default(0) range(-1,1) ui(slider)
+#pragma param u_anisotropyRotation float default(0) range(-3.1416,3.1416) ui(slider)
+#pragma param u_translucency float default(0) range(0,1) ui(slider)
+#pragma param u_translucencyColor color default(1,1,1,1)
+#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)
+#pragma param u_iridescence float default(0) range(0,1) ui(slider)
+#pragma param u_iridescenceIor float default(1.3) range(1,2.5) ui(slider)
+#pragma param u_iridescenceThickness float default(400) range(0,1200) ui(slider)
#pragma fragment
precision mediump float;
@@ -431,10 +449,11 @@ void main() {
alpha = min(alpha, 1.0);
}
#ifdef MESH_NORMALS
- highp vec3 N = perturbNormal(normalize(v_worldNormal), v_worldXYZ, v_texCoord,
+ highp vec3 Ng = normalize(v_worldNormal);
+ highp vec3 N = perturbNormal(Ng, v_worldXYZ, v_texCoord,
sampleNormal(u_normalMap, v_texCoord));
// A double-sided surface is lit from the side it is seen from (glTF's rule).
- if (!gl_FrontFacing) N = -N;
+ if (!gl_FrontFacing) { N = -N; Ng = -Ng; }
#else
vec3 N = sampleNormal(u_normalMap, v_texCoord);
#endif
@@ -452,8 +471,41 @@ void main() {
#ifdef MESH_NORMALS
roughness = specularAA(dndx, dndy, roughness);
#endif
- vec3 lit = applyLightingPBR(base.rgb, N, P, viewDirection(P),
- u_metallic * mr.b, roughness, 1.0, ao);
+ SurfaceExt ext = surfaceExtNone(N);
+ if (u_ior != 1.5) {
+ float r = (u_ior - 1.0) / (u_ior + 1.0);
+ ext.misc.x = r * r;
+ }
+#ifdef MESH_NORMALS
+ // The coat's normal is the surface's own, not the bump map's: a coat is smooth over whatever it covers.
+ if (u_clearcoat > 0.0) {
+ ext.coat = vec4(u_clearcoat, max(u_clearcoatRoughness, 0.045), 0.0, 0.0);
+ ext.coatN = vec4(Ng, 0.0);
+ }
+ if (u_anisotropy != 0.0) {
+ // The direction the grooves run, from the UV frame turned by the rotation, then made to lie across N.
+ highp vec3 T = perturbNormal(Ng, P, v_texCoord, vec3(cos(u_anisotropyRotation), sin(u_anisotropyRotation), 0.0));
+ T -= N * dot(N, T);
+ if (dot(T, T) < 1e-6) T = cross(N, abs(N.y) < 0.99 ? vec3(0.0, 1.0, 0.0) : vec3(1.0, 0.0, 0.0));
+ ext.aniso = vec4(normalize(T), clamp(u_anisotropy, -0.99, 0.99));
+ }
+#endif
+ if (dot(u_sheenColor.rgb, vec3(1.0)) > 0.0) ext.sheen = vec4(u_sheenColor.rgb, max(u_sheenRoughness, 0.07));
+ if (u_translucency > 0.0) {
+ ext.scatter = vec4(u_translucencyColor.rgb * base.rgb, u_translucency * 0.5);
+ ext.trans = vec4(u_translucency, u_thickness, 0.0, 0.0);
+ }
+ if (u_iridescence > 0.0) ext.film = vec4(u_iridescence, max(u_iridescenceIor, 1.0), u_iridescenceThickness, 0.0);
+ vec3 viewDir = viewDirection(P);
+ // Light that passes through. With no picture of what is behind to refract, the surface lets that much of it show: its
+ // coverage falls with the transmission, and returns at grazing angles where glass reflects instead of passing light.
+ if (u_transmission > 0.0) {
+ float cosV = clamp(dot(N, viewDir), 0.0, 1.0);
+ alpha *= 1.0 - u_transmission * (1.0 - (0.04 + 0.96 * pow(1.0 - cosV, 5.0)));
+ base.rgb = mix(base.rgb, base.rgb * u_attenuationColor.rgb, u_transmission);
+ }
+ vec3 lit = applyLightingSurface(base.rgb, N, P, viewDir,
+ u_metallic * mr.b, roughness, u_specular, ao, ext);
fragColor = vec4(lit + 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
@@ -505,7 +557,7 @@ fn packNormal(n : vec3f) -> vec2f {
#ifdef MESH_LIGHTMAP
g_lightmap = v.v_lightmap;
#endif
- let base = textureSampleGrad(t0, s0, v.v_texCoord, duvx, duvy) * v.v_color * mc.u_tint;
+ var base = textureSampleGrad(t0, s0, v.v_texCoord, duvx, duvy) * v.v_color * mc.u_tint;
// Before any discard: a derivative needs every fragment of its quad.
let edge = max(fwidth(base.a), 1e-4);
var alpha = base.a;
@@ -515,9 +567,10 @@ fn packNormal(n : vec3f) -> vec2f {
alpha = min(alpha, 1.0);
}
#ifdef MESH_NORMALS
- var N = perturbNormal(normalize(v.v_worldNormal), v.v_worldXYZ, v.v_texCoord,
+ var Ng = normalize(v.v_worldNormal);
+ var N = perturbNormal(Ng, v.v_worldXYZ, v.v_texCoord,
sampleNormalGrad(u_normalMap, u_normalMap_s, v.v_texCoord, duvx, duvy));
- if (!front) { N = -N; }
+ if (!front) { N = -N; Ng = -Ng; }
#else
let N = sampleNormalGrad(u_normalMap, u_normalMap_s, v.v_texCoord, duvx, duvy);
#endif
@@ -533,8 +586,42 @@ fn packNormal(n : vec3f) -> vec2f {
#ifdef MESH_NORMALS
roughness = specularAA(dndx, dndy, roughness);
#endif
- let lit = applyLightingPBR(base.rgb, N, P, viewDirection(P),
- mc.u_metallic * mr.b, roughness, 1.0, ao);
+ var ext = surfaceExtNone(N);
+ if (mc.u_ior != 1.5) {
+ let r = (mc.u_ior - 1.0) / (mc.u_ior + 1.0);
+ ext.misc.x = r * r;
+ }
+#ifdef MESH_NORMALS
+ // See the GLSL twin: the coat's normal is the surface's own, not the bump map's.
+ if (mc.u_clearcoat > 0.0) {
+ ext.coat = vec4f(mc.u_clearcoat, max(mc.u_clearcoatRoughness, 0.045), 0.0, 0.0);
+ ext.coatN = vec4f(Ng, 0.0);
+ }
+ if (mc.u_anisotropy != 0.0) {
+ var T = perturbNormal(Ng, P, v.v_texCoord, vec3f(cos(mc.u_anisotropyRotation), sin(mc.u_anisotropyRotation), 0.0));
+ T -= N * dot(N, T);
+ if (dot(T, T) < 1e-6) {
+ var seed = vec3f(1.0, 0.0, 0.0);
+ if (abs(N.y) < 0.99) { seed = vec3f(0.0, 1.0, 0.0); }
+ T = cross(N, seed);
+ }
+ ext.aniso = vec4f(normalize(T), clamp(mc.u_anisotropy, -0.99, 0.99));
+ }
+#endif
+ if (dot(mc.u_sheenColor.rgb, vec3f(1.0)) > 0.0) { ext.sheen = vec4f(mc.u_sheenColor.rgb, max(mc.u_sheenRoughness, 0.07)); }
+ if (mc.u_translucency > 0.0) {
+ ext.scatter = vec4f(mc.u_translucencyColor.rgb * base.rgb, mc.u_translucency * 0.5);
+ ext.trans = vec4f(mc.u_translucency, mc.u_thickness, 0.0, 0.0);
+ }
+ if (mc.u_iridescence > 0.0) { ext.film = vec4f(mc.u_iridescence, max(mc.u_iridescenceIor, 1.0), mc.u_iridescenceThickness, 0.0); }
+ let viewDir = viewDirection(P);
+ if (mc.u_transmission > 0.0) {
+ let cosV = clamp(dot(N, viewDir), 0.0, 1.0);
+ alpha *= 1.0 - mc.u_transmission * (1.0 - (0.04 + 0.96 * pow(1.0 - cosV, 5.0)));
+ base = vec4f(mix(base.rgb, base.rgb * mc.u_attenuationColor.rgb, vec3f(mc.u_transmission)), base.a);
+ }
+ let lit = applyLightingSurface(base.rgb, N, P, viewDir,
+ mc.u_metallic * mr.b, roughness, mc.u_specular, ao, ext);
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/tests/model-shader-layers.test.ts b/sdk/tests/model-shader-layers.test.ts
new file mode 100644
index 000000000..bcdfef812
--- /dev/null
+++ b/sdk/tests/model-shader-layers.test.ts
@@ -0,0 +1,54 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * @file The Model shader's shading layers — clear coat, sheen, anisotropy, translucency, thin film, index of refraction,
+ * transmission — are declared once and read by both languages.
+ *
+ * The two stages are written apart, so a layer one of them forgot reads as a parameter that does nothing on one backend.
+ * This holds each layer's parameter to the block and to both stages, and the surface they fill to the same lighting call.
+ */
+import { describe, it, expect } from 'vitest';
+import { builtinShaderTemplate } from '../src/render/builtinShaders';
+
+const source = builtinShaderTemplate('model')!.source;
+const split = source.indexOf('#pragma fragment wgsl');
+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',
+];
+
+describe('the Model shader\'s shading layers', () => {
+ it.each(LAYERS)('declares %s as a material parameter and reads it in both stages', (name) => {
+ expect(source).toContain(`#pragma param ${name} `);
+ expect(glsl).toContain(name);
+ expect(wgsl).toContain(`mc.${name}`);
+ });
+
+ it('defaults every layer to off, so a material that says nothing is the plain surface', () => {
+ for (const name of ['u_clearcoat', 'u_anisotropy', 'u_translucency', 'u_transmission', 'u_iridescence']) {
+ expect(source).toMatch(new RegExp(`#pragma param ${name} float default\\(0\\)`));
+ }
+ expect(source).toMatch(/#pragma param u_sheenColor color default\(0,0,0,1\)/);
+ // glTF's own defaults, which are not zero.
+ expect(source).toMatch(/#pragma param u_ior float default\(1\.5\)/);
+ expect(source).toMatch(/#pragma param u_specular float default\(1\)/);
+ });
+
+ it('hands the filled surface to the lighting, in each language', () => {
+ expect(glsl).toMatch(/applyLightingSurface\([^;]*, ext\)/);
+ expect(wgsl).toMatch(/applyLightingSurface\([^;]*, ext\)/);
+ });
+
+ it('lights the coat by the surface\'s own normal, not the bump map\'s', () => {
+ expect(glsl).toMatch(/ext\.coatN = vec4\(Ng, 0\.0\)/);
+ expect(wgsl).toMatch(/ext\.coatN = vec4f\(Ng, 0\.0\)/);
+ });
+
+ it('lets transmission through as coverage, with no refraction of what is behind', () => {
+ expect(glsl).toMatch(/alpha \*= 1\.0 - u_transmission/);
+ expect(wgsl).toMatch(/alpha \*= 1\.0 - mc\.u_transmission/);
+ });
+});
diff --git a/sdk/tests/model-shader-specular-aa.test.ts b/sdk/tests/model-shader-specular-aa.test.ts
index 845e3ff92..14a2cbe6e 100644
--- a/sdk/tests/model-shader-specular-aa.test.ts
+++ b/sdk/tests/model-shader-specular-aa.test.ts
@@ -32,8 +32,8 @@ describe('the Model shader filters specular by the normal derivative', () => {
it('lights with the filtered roughness, not the authored one', () => {
expect(glsl).toMatch(/roughness = specularAA\(dndx, dndy, roughness\)/);
- expect(glsl).toMatch(/applyLightingPBR\([^;]*u_metallic \* mr\.b, roughness,/);
+ expect(glsl).toMatch(/applyLightingSurface\([^;]*u_metallic \* mr\.b, roughness,/);
expect(wgsl).toMatch(/roughness = specularAA\(dndx, dndy, roughness\)/);
- expect(wgsl).toMatch(/applyLightingPBR\([^;]*mc\.u_metallic \* mr\.b, roughness,/);
+ expect(wgsl).toMatch(/applyLightingSurface\([^;]*mc\.u_metallic \* mr\.b, roughness,/);
});
});
diff --git a/src/esengine/resource/ShaderParser.cpp b/src/esengine/resource/ShaderParser.cpp
index 7ebc4360d..4aeb843f5 100644
--- a/src/esengine/resource/ShaderParser.cpp
+++ b/src/esengine/resource/ShaderParser.cpp
@@ -1627,18 +1627,53 @@ fn distanceFalloff(dist : f32, reach : f32, fo : vec4f) -> f32 {
let ramp = clamp((reach - dist) / max(reach - fo.x, 1e-4), 0.0, 1.0);
return pow(ramp, max(fo.y, 1e-3));
}
-fn applyLightingPBR(albedo : vec3f, N : vec3f, worldPos : vec3f, V : vec3f, metallic : f32,
- roughness : f32, specular : f32, ao : f32) -> vec3f {
- let F0 = mix(vec3f(0.04), albedo, vec3f(metallic));
+)"
+// Split again for the same reason: the surface layers and the lighting that reads them.
+R"(struct SurfaceExt { coat : vec4f, coatN : vec4f, sheen : vec4f, aniso : vec4f, scatter : vec4f, trans : vec4f, film : vec4f, misc : vec4f };
+fn surfaceExtNone(N : vec3f) -> SurfaceExt {
+ return SurfaceExt(vec4f(0.0), vec4f(N, 0.0), vec4f(0.0), vec4f(0.0), vec4f(0.0), vec4f(0.0), vec4f(0.0),
+ vec4f(0.04, 0.0, 0.0, 0.0));
+}
+fn distributionCharlie(NdotH : f32, a : f32) -> f32 {
+ let invA = 1.0 / max(a, 1e-3);
+ let sin2 = max(1.0 - NdotH * NdotH, 0.0078125);
+ return (2.0 + invA) * pow(sin2, invA * 0.5) / 6.28318531;
+}
+fn visibilityNeubelt(NdotV : f32, NdotL : f32) -> f32 {
+ return 1.0 / max(4.0 * (NdotL + NdotV - NdotL * NdotV), 1e-4);
+}
+fn distributionGGXAniso(at : f32, ab : f32, ToH : f32, BoH : f32, NoH : f32) -> f32 {
+ let a2 = at * ab;
+ let d = vec3f(ab * ToH, at * BoH, a2 * NoH);
+ let b2 = a2 / max(dot(d, d), 1e-7);
+ return a2 * b2 * b2 / 3.14159265;
+}
+fn visibilityGGXAniso(at : f32, ab : f32, ToV : f32, BoV : f32, ToL : f32, BoL : f32, NoV : f32, NoL : f32) -> f32 {
+ let lambdaV = NoL * length(vec3f(at * ToV, ab * BoV, NoV));
+ let lambdaL = NoV * length(vec3f(at * ToL, ab * BoL, NoL));
+ return 0.5 / max(lambdaV + lambdaL, 1e-5);
+}
+fn filmFresnel(F : vec3f, cosTheta : f32, film : vec4f) -> vec3f {
+ let sinT2 = (1.0 - cosTheta * cosTheta) / (film.y * film.y);
+ let cosT = sqrt(max(1.0 - sinT2, 0.0));
+ let opd = 2.0 * film.y * film.z * cosT;
+ let tint = vec3f(0.5) + vec3f(0.5) * cos(vec3f(6.28318531 * opd) / vec3f(650.0, 510.0, 475.0));
+ return clamp(F * mix(vec3f(1.0), tint * 2.0, vec3f(film.x)), vec3f(0.0), vec3f(1.0));
+}
+fn applyLightingSurface(albedo : vec3f, N : vec3f, worldPos : vec3f, V : vec3f, metallic : f32,
+ roughness : f32, specular : f32, ao : f32, ext : SurfaceExt) -> vec3f {
+ let F0 = mix(vec3f(ext.misc.x), albedo, vec3f(metallic));
let a = max(roughness * roughness, 1e-3);
let NdotV = max(dot(N, V), 1e-4);
var lit = indirectIrradiance(N) * ao;
var gloss = vec3f(0.0);
+ var extra = vec3f(0.0);
for (var i = 0; i < 16; i++) {
let term = shadeLight(lc.u_lights[i].posDir, lc.u_lights[i].color, lc.u_lights[i].spot, lc.u_lights[i].shadow,
- lc.u_lights[i].shadowMap, lc.u_lights[i].falloff, N, V, worldPos, a, F0, NdotV, specular, roughness);
+ lc.u_lights[i].shadowMap, lc.u_lights[i].falloff, N, V, worldPos, a, F0, NdotV, specular, roughness, ext);
lit += term.lit;
gloss += term.gloss;
+ extra += term.extra;
}
// The lights past the sixteen: this pixel's tile is found from where the fragment lands through the camera it was drawn by
// (normalized device coordinates, the same on every backend), and only the lights whose bit is set in its mask are shaded.
@@ -1659,22 +1694,53 @@ fn applyLightingPBR(albedo : vec3f, N : vec3f, worldPos : vec3f, V : vec3f, meta
if (k >= tiledCount) { break; }
let term = shadeLight(lc.tiledLights[k].posDir, lc.tiledLights[k].color, lc.tiledLights[k].spot,
lc.tiledLights[k].shadow, lc.tiledLights[k].shadowMap, lc.tiledLights[k].falloff,
- N, V, worldPos, a, F0, NdotV, specular, roughness);
+ N, V, worldPos, a, F0, NdotV, specular, roughness, ext);
lit += term.lit;
gloss += term.gloss;
+ extra += term.extra;
}
}
}
let ab = envBRDFApprox(NdotV, roughness);
- gloss += envRadiance(reflect(-V, N), roughness, reflectionColumn(), worldPos) * (ao * specular)
- * (F0 * ab.x + vec3f(ab.y));
- return albedo * (1.0 - metallic) * lit + gloss;
+ var envR = reflect(-V, N);
+ if (ext.aniso.w != 0.0) {
+ let T = ext.aniso.xyz;
+ var grain = T;
+ if (ext.aniso.w >= 0.0) { grain = cross(N, T); }
+ let bent = normalize(cross(cross(grain, V), grain));
+ envR = reflect(-V, normalize(mix(N, bent, vec3f(abs(ext.aniso.w)))));
+ }
+ var envF = F0 * ab.x + vec3f(ab.y);
+ if (ext.film.x > 0.0) { envF = filmFresnel(envF, NdotV, ext.film); }
+ gloss += envRadiance(envR, roughness, reflectionColumn(), worldPos) * (ao * specular)
+ * envF;
+ let base = albedo * (1.0 - metallic) * lit + gloss;
+ var baseScale = 1.0;
+ if (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;
+ baseScale *= 1.0 - Fc;
+ extra += envRadiance(reflect(-V, ext.coatN.xyz), ext.coat.y, reflectionColumn(), worldPos) * (ao * Fc);
+ }
+ if (dot(ext.sheen.rgb, vec3f(1.0)) > 0.0) {
+ let sheenAlbedo = 0.2 + 0.3 * ext.sheen.a;
+ baseScale *= 1.0 - max(max(ext.sheen.r, ext.sheen.g), ext.sheen.b) * sheenAlbedo;
+ extra += ext.sheen.rgb * (indirectIrradiance(N) * ao * sheenAlbedo);
+ }
+ return base * baseScale + extra;
}
-struct LightTerm { lit : vec3f, gloss : vec3f };
+fn applyLightingPBR(albedo : vec3f, N : vec3f, worldPos : vec3f, V : vec3f, metallic : f32,
+ roughness : f32, specular : f32, ao : f32) -> vec3f {
+ return applyLightingSurface(albedo, N, worldPos, V, metallic, roughness, specular, ao, surfaceExtNone(N));
+}
+struct LightTerm { lit : vec3f, gloss : vec3f, extra : vec3f };
fn shadeLight(pd : vec4f, col : vec4f, sp : vec4f, sh : vec4f, sm : vec4f, fo : vec4f, N : vec3f, V : vec3f,
- worldPos : vec3f, a : f32, F0 : vec3f, NdotV : f32, specular : f32, roughness : f32) -> LightTerm {
+ worldPos : vec3f, a : f32, F0 : vec3f, NdotV : f32, specular : f32, roughness : f32,
+ ext : SurfaceExt) -> LightTerm {
var lit = vec3f(0.0);
var gloss = vec3f(0.0);
+ var extra = vec3f(0.0);
{
var L : vec3f;
var atten : f32;
@@ -1715,14 +1781,52 @@ fn shadeLight(pd : vec4f, col : vec4f, sp : vec4f, sh : vec4f, sm : vec4f, fo :
lit += radiance;
if (specular > 0.0) {
let H = normalize(L + V);
- let brdf = distributionGGX(max(dot(N, H), 0.0), a)
+ var brdf : f32;
+ if (ext.aniso.w != 0.0) {
+ let T = ext.aniso.xyz;
+ let B = cross(N, T);
+ let at = max(a * (1.0 + ext.aniso.w), 0.002);
+ let ab = max(a * (1.0 - ext.aniso.w), 0.002);
+ brdf = distributionGGXAniso(at, ab, dot(T, H), dot(B, H), max(dot(N, H), 0.0))
+ * visibilityGGXAniso(at, ab, dot(T, V), dot(B, V), dot(T, L), dot(B, L), NdotV, ndotl);
+ } else {
+ brdf = distributionGGX(max(dot(N, H), 0.0), a)
* geometrySmith(NdotV, ndotl, roughness)
/ max(4.0 * NdotV * ndotl, 1e-4);
- gloss += radiance * fresnelSchlick(max(dot(V, H), 0.0), F0)
+ }
+ var F = fresnelSchlick(max(dot(V, H), 0.0), F0);
+ if (ext.film.x > 0.0) { F = filmFresnel(F, max(dot(V, H), 0.0), ext.film); }
+ gloss += radiance * F
* (brdf * 3.14159265 * specular);
}
+ if (col.a > 0.0 && atten > 0.0
+ && (ext.coat.x > 0.0 || ext.scatter.a > 0.0 || dot(ext.sheen.rgb, vec3f(1.0)) > 0.0)) {
+ let irr = col.rgb * (col.a * atten);
+ let Hx = normalize(L + V);
+ let VdotH = max(dot(V, Hx), 0.0);
+ 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;
+ extra += irr * coatNdotL
+ * (distributionGGX(max(dot(ext.coatN.xyz, Hx), 0.0), coatA) * (0.25 / max(VdotH * VdotH, 1e-4))
+ * Fc * 3.14159265);
+ }
+ if (dot(ext.sheen.rgb, vec3f(1.0)) > 0.0) {
+ let sheenA = max(ext.sheen.a * ext.sheen.a, 1e-3);
+ extra += irr * ndotl * ext.sheen.rgb
+ * (distributionCharlie(max(dot(N, Hx), 0.0), sheenA) * visibilityNeubelt(NdotV, ndotl) * 3.14159265);
+ }
+ if (ext.scatter.a > 0.0) {
+ let wrapped = clamp((dot(N, L) + ext.scatter.a) / (1.0 + ext.scatter.a), 0.0, 1.0);
+ extra += irr * ext.scatter.rgb * max(wrapped - ndotl, 0.0);
+ let Hb = normalize(L + N * 0.3);
+ extra += irr * ext.scatter.rgb
+ * (pow(clamp(dot(V, -Hb), 0.0, 1.0), 4.0) * ext.trans.x * (1.0 - ext.trans.y));
+ }
+ }
}
- return LightTerm(lit, gloss);
+ return LightTerm(lit, gloss, extra);
}
fn applyLighting2DAO(albedo : vec3f, N : vec3f, worldPos : vec2f, ao : f32) -> vec3f {
let P = vec3f(worldPos, 0.0);
@@ -2579,33 +2683,85 @@ ShaderParser::AssembledStage ShaderParser::assembleStageEx(const ParsedShader& p
" highp float ramp = clamp((reach - dist) / max(reach - fo.x, 1e-4), 0.0, 1.0);\n"
" return pow(ramp, max(fo.y, 1e-3));\n"
"}\n"
+ // What a surface has beyond a base colour and a metal/rough pair: a clear coat over it, a sheen on it, grooves that stretch
+ // its highlight, light that scatters into it or passes through it, a thin film that tints its reflection. All zero is the
+ // plain surface (surfaceExtNone), and every term below sits behind a branch on it, so the plain surface pays nothing.
+ // coat x strength, y perceptual roughness coatN the coat's own normal (the geometric one, not the bump map's)
+ // sheen rgb colour, a perceptual roughness aniso xyz tangent (unit, across N), w strength -1..1
+ // scatter rgb colour scattered light takes, a wrap 0..1
+ // trans x light passing through, y thickness 0..1 film x strength, y film index, z thickness in nm
+ // misc x dielectric reflectance at normal incidence (0.04 for an index of 1.5)
+ "struct SurfaceExt {\n"
+ " highp vec4 coat;\n"
+ " highp vec4 coatN;\n"
+ " highp vec4 sheen;\n"
+ " highp vec4 aniso;\n"
+ " highp vec4 scatter;\n"
+ " highp vec4 trans;\n"
+ " highp vec4 film;\n"
+ " highp vec4 misc;\n"
+ "};\n"
+ "SurfaceExt surfaceExtNone(in highp vec3 N) {\n"
+ " return SurfaceExt(vec4(0.0), vec4(N, 0.0), vec4(0.0), vec4(0.0), vec4(0.0), vec4(0.0), vec4(0.0),\n"
+ " vec4(0.04, 0.0, 0.0, 0.0));\n"
+ "}\n"
+ "highp float distributionCharlie(in highp float NdotH, in highp float a) {\n"
+ " highp float invA = 1.0 / max(a, 1e-3);\n"
+ " highp float sin2 = max(1.0 - NdotH * NdotH, 0.0078125);\n"
+ " return (2.0 + invA) * pow(sin2, invA * 0.5) / 6.28318531;\n"
+ "}\n"
+ "highp float visibilityNeubelt(in highp float NdotV, in highp float NdotL) {\n"
+ " return 1.0 / max(4.0 * (NdotL + NdotV - NdotL * NdotV), 1e-4);\n"
+ "}\n"
+ "highp float distributionGGXAniso(in highp float at, in highp float ab, in highp float ToH,\n"
+ " in highp float BoH, in highp float NoH) {\n"
+ " highp float a2 = at * ab;\n"
+ " highp vec3 d = vec3(ab * ToH, at * BoH, a2 * NoH);\n"
+ " highp float b2 = a2 / max(dot(d, d), 1e-7);\n"
+ " return a2 * b2 * b2 / 3.14159265;\n"
+ "}\n"
+ "highp float visibilityGGXAniso(in highp float at, in highp float ab, in highp float ToV, in highp float BoV,\n"
+ " in highp float ToL, in highp float BoL, in highp float NoV, in highp float NoL) {\n"
+ " highp float lambdaV = NoL * length(vec3(at * ToV, ab * BoV, NoV));\n"
+ " highp float lambdaL = NoV * length(vec3(at * ToL, ab * BoL, NoL));\n"
+ " return 0.5 / max(lambdaV + lambdaL, 1e-5);\n"
+ "}\n"
+ "highp vec3 filmFresnel(in highp vec3 F, in highp float cosTheta, in highp vec4 film) {\n"
+ " highp float sinT2 = (1.0 - cosTheta * cosTheta) / (film.y * film.y);\n"
+ " highp float cosT = sqrt(max(1.0 - sinT2, 0.0));\n"
+ " highp float opd = 2.0 * film.y * film.z * cosT;\n"
+ " highp vec3 tint = vec3(0.5) + 0.5 * cos(6.28318531 * opd / vec3(650.0, 510.0, 475.0));\n"
+ " return clamp(F * mix(vec3(1.0), tint * 2.0, film.x), vec3(0.0), vec3(1.0));\n"
+ "}\n"
// The body of the per-light loop, a function so that the lights the tiles carry are shaded by the same code as the sixteen.
"void shadeLight(in highp vec4 pd, in highp vec4 col, in highp vec4 sp, in highp vec4 sh, in highp vec4 sm,\n"
" in highp vec4 fo, in highp vec3 N, in highp vec3 V, in highp vec3 worldPos,\n"
" in highp float a, in highp vec3 F0, in highp float NdotV, in highp float specular,\n"
- " in highp float roughness, inout highp vec3 lit, inout highp vec3 gloss);\n"
+ " in highp float roughness, in SurfaceExt ext, inout highp vec3 lit, inout highp vec3 gloss,\n"
+ " inout highp vec3 extra);\n"
// The lighting model in its general form; a lit 2D surface is its zero
// (metallic 0, roughness 1, specular 0 leaves albedo * NdotL, pixel for pixel
// what this engine has always drawn), so there is one model and not two.
- "highp vec3 applyLightingPBR(in highp vec3 albedo, in highp vec3 N, in highp vec3 worldPos,\n"
- " in highp vec3 V, in highp float metallic,\n"
- " in highp float roughness, in highp float specular,\n"
- " in highp float ao) {\n"
+ "highp vec3 applyLightingSurface(in highp vec3 albedo, in highp vec3 N, in highp vec3 worldPos,\n"
+ " in highp vec3 V, in highp float metallic,\n"
+ " in highp float roughness, in highp float specular,\n"
+ " in highp float ao, in SurfaceExt ext) {\n"
// glTF's specularFactor scales the whole lobe, so 0 removes it rather than
// leaving Schlick's grazing-angle rim behind.
- " highp vec3 F0 = mix(vec3(0.04), albedo, metallic);\n"
+ " highp vec3 F0 = mix(vec3(ext.misc.x), albedo, metallic);\n"
" highp float a = max(roughness * roughness, 1e-3);\n"
" highp float NdotV = max(dot(N, V), 1e-4);\n"
// Occlusion darkens the light that arrives from everywhere, which is the
// environment; a surface's own lights are unobstructed by it.
" highp vec3 lit = indirectIrradiance(N) * ao;\n"
" highp vec3 gloss = vec3(0.0);\n"
+ " highp vec3 extra = vec3(0.0);\n"
// A Light has no third coordinate, so distance stays in the plane and a
// point light's height is its radius. Normal and view are the 3D part.
" for (int i = 0; i < 16; ++i) {\n"
" shadeLight(u_lights[i].posDir, u_lights[i].color, u_lights[i].spot, u_lights[i].shadow,\n"
" u_lights[i].shadowMap, u_lights[i].falloff, N, V, worldPos, a, F0, NdotV,\n"
- " specular, roughness, lit, gloss);\n"
+ " specular, roughness, ext, lit, gloss, extra);\n"
" }\n"
// The lights past the sixteen: this pixel's tile is found from where the fragment lands through the camera it was drawn by
// (normalized device coordinates, the same on every backend), and only the lights whose bit is set in its mask are shaded.
@@ -2625,7 +2781,7 @@ ShaderParser::AssembledStage ShaderParser::assembleStageEx(const ParsedShader& p
" if (k >= tiledCount) break;\n"
" shadeLight(u_tiledLights[k].posDir, u_tiledLights[k].color, u_tiledLights[k].spot,\n"
" u_tiledLights[k].shadow, u_tiledLights[k].shadowMap, u_tiledLights[k].falloff,\n"
- " N, V, worldPos, a, F0, NdotV, specular, roughness, lit, gloss);\n"
+ " N, V, worldPos, a, F0, NdotV, specular, roughness, ext, lit, gloss, extra);\n"
" }\n"
" }\n"
" }\n"
@@ -2633,15 +2789,45 @@ ShaderParser::AssembledStage ShaderParser::assembleStageEx(const ParsedShader& p
// radiance from every direction. A metal has no diffuse, so without a
// reflection of it a metal is black wherever no light happens to point.
" highp vec2 ab = envBRDFApprox(NdotV, roughness);\n"
- " gloss += envRadiance(reflect(-V, N), roughness, reflectionColumn(), worldPos)\n"
+ " highp vec3 envR = reflect(-V, N);\n"
+ " if (ext.aniso.w != 0.0) {\n"
+ " highp vec3 T = ext.aniso.xyz;\n"
+ " highp vec3 grain = ext.aniso.w >= 0.0 ? cross(N, T) : T;\n"
+ " highp vec3 bent = normalize(cross(cross(grain, V), grain));\n"
+ " envR = reflect(-V, normalize(mix(N, bent, abs(ext.aniso.w))));\n"
+ " }\n"
+ " highp vec3 envF = F0 * ab.x + ab.y;\n"
+ " if (ext.film.x > 0.0) envF = filmFresnel(envF, NdotV, ext.film);\n"
+ " gloss += envRadiance(envR, roughness, reflectionColumn(), worldPos)\n"
" * (ao * specular)\n"
- " * (F0 * ab.x + ab.y);\n"
- " return albedo * (1.0 - metallic) * lit + gloss;\n"
+ " * envF;\n"
+ " highp vec3 base = albedo * (1.0 - metallic) * lit + gloss;\n"
+ " highp float baseScale = 1.0;\n"
+ " 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"
+ " baseScale *= 1.0 - Fc;\n"
+ " extra += envRadiance(reflect(-V, ext.coatN.xyz), ext.coat.y, reflectionColumn(), worldPos) * (ao * Fc);\n"
+ " }\n"
+ " if (dot(ext.sheen.rgb, vec3(1.0)) > 0.0) {\n"
+ " highp float sheenAlbedo = 0.2 + 0.3 * ext.sheen.a;\n"
+ " baseScale *= 1.0 - max(max(ext.sheen.r, ext.sheen.g), ext.sheen.b) * sheenAlbedo;\n"
+ " extra += ext.sheen.rgb * (indirectIrradiance(N) * ao * sheenAlbedo);\n"
+ " }\n"
+ " return base * baseScale + extra;\n"
+ "}\n"
+ "highp vec3 applyLightingPBR(in highp vec3 albedo, in highp vec3 N, in highp vec3 worldPos,\n"
+ " in highp vec3 V, in highp float metallic,\n"
+ " in highp float roughness, in highp float specular,\n"
+ " in highp float ao) {\n"
+ " return applyLightingSurface(albedo, N, worldPos, V, metallic, roughness, specular, ao, surfaceExtNone(N));\n"
"}\n"
"void shadeLight(in highp vec4 pd, in highp vec4 col, in highp vec4 sp, in highp vec4 sh, in highp vec4 sm,\n"
" in highp vec4 fo, in highp vec3 N, in highp vec3 V, in highp vec3 worldPos,\n"
" in highp float a, in highp vec3 F0, in highp float NdotV, in highp float specular,\n"
- " in highp float roughness, inout highp vec3 lit, inout highp vec3 gloss) {\n"
+ " in highp float roughness, in SurfaceExt ext, inout highp vec3 lit, inout highp vec3 gloss,\n"
+ " inout highp vec3 extra) {\n"
" highp vec3 L;\n"
" highp float atten;\n"
" highp vec2 target = pd.xy;\n" // shadow-ray aim point (light position by default)
@@ -2690,12 +2876,50 @@ ShaderParser::AssembledStage ShaderParser::assembleStageEx(const ParsedShader& p
// which the diffuse side cancels against albedo/pi and never has to spell out.
" if (specular > 0.0) {\n"
" highp vec3 H = normalize(L + V);\n"
- " highp float brdf = distributionGGX(max(dot(N, H), 0.0), a)\n"
- " * geometrySmith(NdotV, ndotl, roughness)\n"
- " / max(4.0 * NdotV * ndotl, 1e-4);\n"
- " gloss += radiance * fresnelSchlick(max(dot(V, H), 0.0), F0)\n"
+ " highp float brdf;\n"
+ " if (ext.aniso.w != 0.0) {\n"
+ " highp vec3 T = ext.aniso.xyz;\n"
+ " highp vec3 B = cross(N, T);\n"
+ " highp float at = max(a * (1.0 + ext.aniso.w), 0.002);\n"
+ " highp float ab = max(a * (1.0 - ext.aniso.w), 0.002);\n"
+ " brdf = distributionGGXAniso(at, ab, dot(T, H), dot(B, H), max(dot(N, H), 0.0))\n"
+ " * visibilityGGXAniso(at, ab, dot(T, V), dot(B, V), dot(T, L), dot(B, L), NdotV, ndotl);\n"
+ " } else {\n"
+ " brdf = distributionGGX(max(dot(N, H), 0.0), a)\n"
+ " * geometrySmith(NdotV, ndotl, roughness)\n"
+ " / max(4.0 * NdotV * ndotl, 1e-4);\n"
+ " }\n"
+ " highp vec3 F = fresnelSchlick(max(dot(V, H), 0.0), F0);\n"
+ " if (ext.film.x > 0.0) F = filmFresnel(F, max(dot(V, H), 0.0), ext.film);\n"
+ " gloss += radiance * F\n"
" * (brdf * 3.14159265 * specular);\n"
" }\n"
+ " if (col.a > 0.0 && atten > 0.0\n"
+ " && (ext.coat.x > 0.0 || ext.scatter.a > 0.0 || dot(ext.sheen.rgb, vec3(1.0)) > 0.0)) {\n"
+ " highp vec3 irr = col.rgb * (col.a * atten);\n"
+ " highp vec3 Hx = normalize(L + V);\n"
+ " highp float VdotH = max(dot(V, Hx), 0.0);\n"
+ " 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"
+ " 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"
+ " }\n"
+ " if (dot(ext.sheen.rgb, vec3(1.0)) > 0.0) {\n"
+ " highp float sheenA = max(ext.sheen.a * ext.sheen.a, 1e-3);\n"
+ " extra += irr * ndotl * ext.sheen.rgb\n"
+ " * (distributionCharlie(max(dot(N, Hx), 0.0), sheenA) * visibilityNeubelt(NdotV, ndotl) * 3.14159265);\n"
+ " }\n"
+ " if (ext.scatter.a > 0.0) {\n"
+ " highp float wrapped = clamp((dot(N, L) + ext.scatter.a) / (1.0 + ext.scatter.a), 0.0, 1.0);\n"
+ " extra += irr * ext.scatter.rgb * max(wrapped - ndotl, 0.0);\n"
+ " highp vec3 Hb = normalize(L + N * 0.3);\n"
+ " extra += irr * ext.scatter.rgb\n"
+ " * (pow(clamp(dot(V, -Hb), 0.0, 1.0), 4.0) * ext.trans.x * (1.0 - ext.trans.y));\n"
+ " }\n"
+ " }\n"
"}\n"
"highp vec3 applyLighting2DAO(highp vec3 albedo, highp vec3 N, highp vec2 worldPos,\n"
" highp float ao) {\n"
diff --git a/tools/check-shader-blocks.mjs b/tools/check-shader-blocks.mjs
index 4869c57c6..e95b9b8e4 100644
--- a/tools/check-shader-blocks.mjs
+++ b/tools/check-shader-blocks.mjs
@@ -100,7 +100,7 @@ const ALIASES = {
const SIGNATURE_GAPS = {
sampleNormal: 'WGSL takes the texture and its sampler apart; GLSL has one combined type',
sampleNormalGrad: 'WGSL takes the texture and its sampler apart; GLSL has one combined type',
- shadeLight: 'WGSL returns the light and gloss it adds (a struct); GLSL accumulates them in two inout arguments',
+ shadeLight: 'WGSL returns the light, gloss and extra light it adds (a struct); GLSL accumulates them in three inout arguments',
};
/** A field name as the comparison sees it: the `u_` a shader adds is not part of it. */
@@ -220,7 +220,7 @@ function helpers(pattern) {
return out;
}
-const glslFns = helpers(/^\s*(?:highp\s+)?(?:void|vec[234]|float|mat[234])\s+(\w+)\s*\(([^)]*)\)\s*\{/gm);
+const glslFns = helpers(/^\s*(?:highp\s+)?(?:void|vec[234]|float|mat[234]|SurfaceExt)\s+(\w+)\s*\(([^)]*)\)\s*\{/gm);
const wgslFns = helpers(/\bfn\s+(\w+)\s*\(([^)]*)\)\s*->/g);
const shape = (counts) => counts.length === 1 ? `${counts[0]} argument(s)`
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index 4b0e6d8e0..f1a054075 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): materials carry a clear coat, sheen, anisotropy, translucency, thin film, index of refraction and transmission":
+ {"note":"The Model material has shading layers — clear coat, sheen, anisotropy, translucency, thin film, index of refraction and transmission — and a glTF with the matching extensions arrives using them."},
"feat(render): a frame carries 112 lights, the lights past the sixteenth lighting their pixels through screen tiles":
{"note":"A frame now carries up to 112 lights: sixteen are read for every pixel and the rest are tiled, so a street of lamps lights what each one reaches."},
"feat(editor): Play runs on the graphics backend the project asks for":