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(assets): a metal arrives as a metal

esenginec28dbc503bf0
diff --git a/desktop/public/scenes/mat-model.esscene b/desktop/public/scenes/mat-model.esscene
new file mode 100644
index 000000000..63430f39f
--- /dev/null
+++ b/desktop/public/scenes/mat-model.esscene
@@ -0,0 +1,170 @@
+{
+ "version": 1,
+ "name": "Main",
+ "entities": [
+ {
+ "id": 0,
+ "name": "Camera",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 0,
+ "y": 0,
+ "z": 10
+ }
+ }
+ },
+ {
+ "type": "Camera",
+ "data": {
+ "projectionType": 1,
+ "orthoSize": 300,
+ "isActive": true,
+ "priority": 0
+ }
+ }
+ ]
+ },
+ {
+ "id": 1,
+ "name": "Metal",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": -150,
+ "y": 0,
+ "z": 0
+ }
+ }
+ },
+ {
+ "type": "Sprite",
+ "data": {
+ "size": {
+ "x": 160,
+ "y": 160
+ },
+ "color": {
+ "r": 0,
+ "g": 0.6,
+ "b": 0,
+ "a": 1
+ },
+ "material": "/scenes/mat-model/metal.esmaterial"
+ }
+ }
+ ]
+ },
+ {
+ "id": 2,
+ "name": "Dielectric",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 150,
+ "y": 0,
+ "z": 0
+ }
+ }
+ },
+ {
+ "type": "Sprite",
+ "data": {
+ "size": {
+ "x": 160,
+ "y": 160
+ },
+ "color": {
+ "r": 0,
+ "g": 0.6,
+ "b": 0,
+ "a": 1
+ },
+ "material": "/scenes/mat-model/dielectric.esmaterial"
+ }
+ }
+ ]
+ },
+ {
+ "id": 3,
+ "name": "MetalLight",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": -150,
+ "y": 0,
+ "z": 0
+ }
+ }
+ },
+ {
+ "type": "Light2D",
+ "data": {
+ "type": 0,
+ "color": {
+ "r": 1,
+ "g": 1,
+ "b": 1,
+ "a": 1
+ },
+ "intensity": 1,
+ "radius": 150
+ }
+ }
+ ]
+ },
+ {
+ "id": 4,
+ "name": "DielectricLight",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 150,
+ "y": 0,
+ "z": 0
+ }
+ }
+ },
+ {
+ "type": "Light2D",
+ "data": {
+ "type": 0,
+ "color": {
+ "r": 1,
+ "g": 1,
+ "b": 1,
+ "a": 1
+ },
+ "intensity": 1,
+ "radius": 150
+ }
+ }
+ ]
+ }
+ ]
+}
diff --git a/desktop/public/scenes/mat-model/dielectric.esmaterial b/desktop/public/scenes/mat-model/dielectric.esmaterial
new file mode 100644
index 000000000..7645cbef7
--- /dev/null
+++ b/desktop/public/scenes/mat-model/dielectric.esmaterial
@@ -0,0 +1,11 @@
+{
+ "version": "1.0",
+ "type": "material",
+ "shader": "builtin:model",
+ "blendMode": 0,
+ "depthTest": false,
+ "properties": {
+ "u_metallic": 0,
+ "u_roughness": 0.4
+ }
+}
diff --git a/desktop/public/scenes/mat-model/metal.esmaterial b/desktop/public/scenes/mat-model/metal.esmaterial
new file mode 100644
index 000000000..f91c6ebc5
--- /dev/null
+++ b/desktop/public/scenes/mat-model/metal.esmaterial
@@ -0,0 +1,11 @@
+{
+ "version": "1.0",
+ "type": "material",
+ "shader": "builtin:model",
+ "blendMode": 0,
+ "depthTest": false,
+ "properties": {
+ "u_metallic": 1,
+ "u_roughness": 0.4
+ }
+}
diff --git a/desktop/public/scenes/model-material_m0.esmaterial b/desktop/public/scenes/model-material_m0.esmaterial
index 7db117af0..a8e7257ab 100644
--- a/desktop/public/scenes/model-material_m0.esmaterial
+++ b/desktop/public/scenes/model-material_m0.esmaterial
@@ -13,6 +13,8 @@
"g": 1,
"b": 0,
"a": 1
- }
+ },
+ "u_metallic": 0,
+ "u_roughness": 1
}
}
diff --git a/desktop/public/scenes/model-material_m1.esmaterial b/desktop/public/scenes/model-material_m1.esmaterial
index ff1c5bb2c..c32586716 100644
--- a/desktop/public/scenes/model-material_m1.esmaterial
+++ b/desktop/public/scenes/model-material_m1.esmaterial
@@ -8,6 +8,8 @@
"cull": 1,
"properties": {
"u_occlusionMap": "model-material_0.png",
- "u_occlusionStrength": 1
+ "u_occlusionStrength": 1,
+ "u_metallic": 0,
+ "u_roughness": 1
}
}
diff --git a/desktop/public/scenes/model-material_m2.esmaterial b/desktop/public/scenes/model-material_m2.esmaterial
index 2aa6da3a8..8612b1430 100644
--- a/desktop/public/scenes/model-material_m2.esmaterial
+++ b/desktop/public/scenes/model-material_m2.esmaterial
@@ -7,6 +7,8 @@
"depthWrite": true,
"cull": 1,
"properties": {
- "u_alphaCutoff": 0.5
+ "u_alphaCutoff": 0.5,
+ "u_metallic": 0,
+ "u_roughness": 1
}
}
diff --git a/desktop/tests/gltf-import.test.ts b/desktop/tests/gltf-import.test.ts
index 4b0d06983..89465f57f 100644
--- a/desktop/tests/gltf-import.test.ts
+++ b/desktop/tests/gltf-import.test.ts
@@ -159,15 +159,17 @@ describe('glTF material import', () => {
alphaMode: 'MASK', alphaCutoff: 0.4, doubleSided: false,
extensions: { KHR_materials_clearcoat: {} },
}];
- const { warnings } = await importGltfMeshes(gltf(doc), 'model');
+ const { meshes, warnings } = await importGltfMeshes(gltf(doc), 'model');
const line = warnings.join('\n');
- expect(line).toContain('metallic-roughness');
expect(line).toContain('normal-map scale 0.4');
expect(line).toContain('KHR_materials_clearcoat');
- // Shading a metal needs a view direction, which 2D lights have not got; the
- // channels that DO reach a shader are products, not warnings.
+ // 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');
expect(line).not.toContain('emissive');
expect(line).not.toContain('alpha cutoff');
+ expect(materialProducts(meshes, 'model')[0]!.data.properties)
+ .toMatchObject({ u_metallic: 1, u_roughness: 0.3 });
});
it('is opaque and single-sided unless the source says otherwise', async () => {
@@ -340,12 +342,16 @@ describe('glTF material products', () => {
expect(products).toHaveLength(1);
expect(products[0]!.name).toBe('model_m0');
expect(products[0]!.data.shader).toBe('builtin:model');
+ // A material with no pbrMetallicRoughness block is glTF's default: fully
+ // metallic, fully rough. Written out, since the engine's own default is not.
expect(products[0]!.data.properties).toEqual({
u_emissive: { r: 1, g: 0.5, b: 0, a: 1 },
u_emissiveMap: 'model_0.png',
u_occlusionMap: 'model_0.png',
u_occlusionStrength: 0.25,
u_alphaCutoff: 0.4,
+ u_metallic: 1,
+ u_roughness: 1,
});
});
diff --git a/desktop/tests/import-model.test.ts b/desktop/tests/import-model.test.ts
index 1d1ca237d..20af2e582 100644
--- a/desktop/tests/import-model.test.ts
+++ b/desktop/tests/import-model.test.ts
@@ -252,12 +252,17 @@ describe('importing a model', () => {
expect(meta(path.join(root, 'assets/models/robot.esmesh')).uuid).toBe(first);
});
- it('reports what the source says and this engine cannot draw', async () => {
+ it('a metal arrives as one, through the editor door', async () => {
const src = path.join(outside, 'metal.gltf');
const doc = JSON.parse(gltf()) as { materials: { pbrMetallicRoughness: { metallicFactor: number } }[] };
doc.materials[0]!.pbrMetallicRoughness.metallicFactor = 1;
writeFileSync(src, JSON.stringify(doc));
const res = await importAssets(root, 'assets/models', [src]);
- expect(res.warnings?.join('\n')).toContain('metallic-roughness');
+ expect(res.warnings?.join('\n') ?? '').not.toContain('metallic-roughness');
+
+ const material = JSON.parse(
+ readFileSync(path.join(root, 'assets/models/metal_m0.esmaterial'), 'utf8'),
+ ) as { properties: Record<string, unknown> };
+ expect(material.properties.u_metallic).toBe(1);
});
});
diff --git a/docs/astro/src/content/docs/assets/models.mdx b/docs/astro/src/content/docs/assets/models.mdx
index 807bfcfbd..c09bda49c 100644
--- a/docs/astro/src/content/docs/assets/models.mdx
+++ b/docs/astro/src/content/docs/assets/models.mdx
@@ -93,6 +93,11 @@ component has no room for. Written only when the source uses one of them, on the
| `emissiveFactor` / `emissiveTexture` | `u_emissive` / `u_emissiveMap` |
| `occlusionTexture` (+ `strength`) | `u_occlusionMap` / `u_occlusionStrength` |
| `alphaMode: MASK` + `alphaCutoff` | `u_alphaCutoff` |
+| `metallicFactor` / `roughnessFactor` / `metallicRoughnessTexture` | `u_metallic` / `u_roughness` / `u_metallicRoughnessMap` |
+
+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
+product that left them out would describe a surface the source did not.
The material also restates the draw's blending, depth and culling, because a material
replaces them — a model told to occlude itself keeps doing so through its material.
@@ -122,7 +127,6 @@ joints.
| Limit | Why |
|---|---|
| 64 joints per mesh | The pose is a uniform block, which is 4KB at that size — inside what a WebGL2 block is guaranteed. A mesh wanting more draws undeformed. |
-| A material is not applied to a skinned mesh | Its program is compiled for a vertex source that reads a model matrix, and a skinned one does not have one. |
| Joint count must match the bind pose | A mesh whose `MeshSkin` names a different number of joints draws undeformed rather than with vertices resolved against the wrong matrices. |
## Compressed geometry
@@ -138,7 +142,6 @@ when it runs.
| Not imported | Why |
|---|---|
-| `metallicFactor`, `roughnessFactor`, `metallicRoughnessTexture` | Shading metal needs a view direction, and these lights are 2D. A factor nothing reads would be a knob that lies. |
| Morph targets, `weights` animation channels | Blending between shapes needs the targets themselves, which are not carried. |
| A second UV set (`TEXCOORD_1`), `KHR_texture_transform` | UVs are used as authored. |
| Non-triangle primitives (`mode` ≠ 4) | The renderer draws triangle lists. |
diff --git a/docs/astro/src/content/docs/graphics/materials.mdx b/docs/astro/src/content/docs/graphics/materials.mdx
index 31b276da4..ed0417240 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`. 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`). 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
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 66e404f08..46b51777f 100644
--- a/docs/astro/src/content/docs/zh-cn/assets/models.mdx
+++ b/docs/astro/src/content/docs/zh-cn/assets/models.mdx
@@ -87,6 +87,10 @@ glTF 以**米**为单位。一个世界单位是一个**设计像素**。所以
| `emissiveFactor` / `emissiveTexture` | `u_emissive` / `u_emissiveMap` |
| `occlusionTexture`(含 `strength`) | `u_occlusionMap` / `u_occlusionStrength` |
| `alphaMode: MASK` + `alphaCutoff` | `u_alphaCutoff` |
+| `metallicFactor` / `roughnessFactor` / `metallicRoughnessTexture` | `u_metallic` / `u_roughness` / `u_metallicRoughnessMap` |
+
+金属度与粗糙度即使等于引擎自己的默认值也会写出来:glTF 把两个系数都默认成 1(完全粗糙的
+金属),而 shader 把金属度默认成 0 —— 省略它们的产物会描述出一个源文件没有说过的表面。
材质还会把这次 draw 的混合、深度和剔除重述一遍 —— 因为材质会**替换**它们,
这样一个被要求自我遮挡的模型,在有了材质之后仍然自我遮挡。
@@ -115,7 +119,6 @@ mesh 就会变形。
| 限制 | 为什么 |
|---|---|
| 每个 mesh 最多 64 根骨骼 | 姿态走 uniform 块,64 根是 4KB —— 在 WebGL2 对一个块的保证范围内。超过的 mesh 按不变形绘制。 |
-| 材质不作用于蒙皮 mesh | 材质的 program 是为「读 model 矩阵」的顶点源编译的,而蒙皮的没有 model 矩阵。 |
| 骨骼数必须与绑定姿态一致 | `MeshSkin` 列出的骨骼数对不上时按不变形绘制,而不是让顶点去解析错误的矩阵。 |
## 压缩几何
@@ -129,7 +132,6 @@ mesh 就会变形。
| 不导入 | 为什么 |
|---|---|
-| `metallicFactor`、`roughnessFactor`、`metallicRoughnessTexture` | 算金属高光要 view 方向,而这里的灯光是 2D 的。写进产物却没人读,那就是个会撒谎的旋钮。 |
| morph target、`weights` 动画通道 | 在多个形状之间混合需要那些 target 本身,而它们没有被带过来。 |
| 第二套 UV(`TEXCOORD_1`)、`KHR_texture_transform` | UV 按作者写的原样使用。 |
| 非三角形 primitive(`mode` ≠ 4) | 渲染器画的是三角形列表。 |
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 c045aa1ef..d60cdf4d2 100644
--- a/docs/astro/src/content/docs/zh-cn/graphics/materials.mdx
+++ b/docs/astro/src/content/docs/zh-cn/graphics/materials.mdx
@@ -28,7 +28,7 @@ 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`。模型导入会写出它。 |
+| **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 步骤、编辑器三处以同样方式解析
diff --git a/examples/model-import/assets/models/robot_m1.esmaterial b/examples/model-import/assets/models/robot_m1.esmaterial
index 27f557105..342bbb363 100644
--- a/examples/model-import/assets/models/robot_m1.esmaterial
+++ b/examples/model-import/assets/models/robot_m1.esmaterial
@@ -12,6 +12,8 @@
"g": 0.25,
"b": 0.1,
"a": 1
- }
+ },
+ "u_metallic": 0,
+ "u_roughness": 1
}
}
diff --git a/pipeline/src/assets/gltfImport.ts b/pipeline/src/assets/gltfImport.ts
index e6d78decf..0f427e2f2 100644
--- a/pipeline/src/assets/gltfImport.ts
+++ b/pipeline/src/assets/gltfImport.ts
@@ -65,6 +65,11 @@ export interface ImportedMaterial {
/** Ambient occlusion in the map's red channel, scaled by @ref occlusionStrength. */
occlusionTexture?: ImportedImageRef;
occlusionStrength?: number;
+ /** metallicFactor / roughnessFactor — glTF's defaults are 1 for both. */
+ metallic?: number;
+ roughness?: number;
+ /** glTF's packing: roughness in green, metal in blue, multiplying the factors. */
+ metallicRoughnessTexture?: ImportedImageRef;
/** alphaMode MASK: a fragment below this alpha is discarded (glTF default 0.5). */
alphaCutoff?: number;
/** Not BLEND: drawn without blending, and taking part in depth. */
@@ -594,9 +599,7 @@ function readTexture(ctx: MaterialContext, cache: Map<number, ImportedImageRef |
/**
* One glTF material, read into the two things that can hold it (@ref
- * materialProducts). Metal and roughness stay reported-not-imported: shading
- * them needs a view direction and this engine's lights are 2D, so a metallic
- * factor in a product would be a knob nothing reads.
+ * materialProducts).
*/
function readMaterial(ctx: MaterialContext, textureCache: Map<number, ImportedImageRef | null>,
index: number): ImportedMaterial {
@@ -606,9 +609,6 @@ function readMaterial(ctx: MaterialContext, textureCache: Map<number, ImportedIm
const factor = pbr.baseColorFactor ?? [1, 1, 1, 1];
const unused: string[] = [];
- if (pbr.metallicRoughnessTexture || (pbr.metallicFactor ?? 1) !== 0 || (pbr.roughnessFactor ?? 1) !== 1) {
- unused.push('metallic-roughness');
- }
if (src.normalTexture?.scale !== undefined && src.normalTexture.scale !== 1) {
unused.push(`normal-map scale ${src.normalTexture.scale}`);
}
@@ -623,7 +623,13 @@ function readMaterial(ctx: MaterialContext, textureCache: Map<number, ImportedIm
? readTexture(ctx, textureCache, src.emissiveTexture, label) : null;
const occlusionMap = src.occlusionTexture
? readTexture(ctx, textureCache, src.occlusionTexture, label) : null;
+ const metalRoughMap = pbr.metallicRoughnessTexture
+ ? readTexture(ctx, textureCache, pbr.metallicRoughnessTexture, label) : null;
const emissive = src.emissiveFactor ?? (emissiveMap ? [1, 1, 1] : [0, 0, 0]);
+ // glTF defaults both factors to 1 — a material that says nothing is a fully
+ // rough metal, and writing that out is what makes a product say so.
+ const metallic = pbr.metallicFactor ?? 1;
+ const roughness = pbr.roughnessFactor ?? 1;
return {
index,
name: src.name ?? `material_${index}`,
@@ -643,6 +649,9 @@ function readMaterial(ctx: MaterialContext, textureCache: Map<number, ImportedIm
occlusionTexture: occlusionMap,
occlusionStrength: src.occlusionTexture?.strength ?? 1,
} : {}),
+ metallic,
+ roughness,
+ ...(metalRoughMap ? { metallicRoughnessTexture: metalRoughMap } : {}),
...(src.alphaMode === 'MASK' ? { alphaCutoff: src.alphaCutoff ?? 0.5 } : {}),
};
}
@@ -1264,9 +1273,12 @@ function materialImageRef(image: ImportedImageRef, refs: ProductRefs): string {
* and samplers, and the component has neither: its per-object attributes end at
* location 15, the ceiling both backends guarantee.
*/
+// A metal-roughness pair the engine's own defaults already are (dielectric, fully
+// rough) needs nothing written; glTF's defaults are 1 and 1, so most materials do.
function needsMaterial(material: ImportedMaterial | undefined): material is ImportedMaterial {
return !!material && !!(material.normalTexture || material.emissive || material.emissiveTexture
- || material.occlusionTexture || material.alphaCutoff);
+ || material.occlusionTexture || material.alphaCutoff || material.metallicRoughnessTexture
+ || (material.metallic ?? 0) !== 0 || (material.roughness ?? 1) !== 1);
}
/** `<stem>_m<source material index>` — the product a material becomes. */
@@ -1299,6 +1311,11 @@ export function materialProducts(meshes: ImportedMesh[], stem: string,
bind('u_normalMap', material.normalTexture);
bind('u_emissiveMap', material.emissiveTexture);
bind('u_occlusionMap', material.occlusionTexture);
+ bind('u_metallicRoughnessMap', material.metallicRoughnessTexture);
+ // Written even at the engine's own defaults: glTF's are 1 and 1, so a
+ // product that left them out would read as a surface it is not.
+ if (material.metallic !== undefined) properties.u_metallic = material.metallic;
+ if (material.roughness !== undefined) properties.u_roughness = material.roughness;
if (material.emissive) {
const [r, g, b] = material.emissive;
properties.u_emissive = { r, g, b, a: 1 };
diff --git a/sdk/src/render/builtinShaders.ts b/sdk/src/render/builtinShaders.ts
index b3538ea6d..ecab38f69 100644
--- a/sdk/src/render/builtinShaders.ts
+++ b/sdk/src/render/builtinShaders.ts
@@ -337,6 +337,9 @@ const MODEL = `#pragma shader "Model"
#pragma param u_occlusionMap texture default(white)
#pragma param u_occlusionStrength float default(1) range(0,1) ui(slider)
#pragma param u_alphaCutoff float default(0) range(0,1) ui(slider)
+#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)
#pragma fragment
precision mediump float;
@@ -356,6 +359,7 @@ out vec4 fragColor;
// Base colour rides the draw itself (slot 0 x vertex colour) — glTF's baseColor
// is what a Mesh2D already carries. Emission is added AFTER lighting (it is light
// this surface makes) and occlusion scales only the ambient term.
+// Metal-roughness packing is glTF's: roughness in green, metal in blue.
void main() {
vec4 base = texture(u_textures[0], v_texCoord) * v_color * u_tint;
if (base.a < u_alphaCutoff) discard;
@@ -366,7 +370,11 @@ void main() {
vec3 N = sampleNormal(u_normalMap, v_texCoord);
#endif
float ao = mix(1.0, texture(u_occlusionMap, v_texCoord).r, u_occlusionStrength);
- vec3 lit = applyLighting2DAO(base.rgb, N, v_worldPos, ao);
+ vec3 mr = texture(u_metallicRoughnessMap, v_texCoord).rgb;
+ highp vec3 P = vec3(v_worldPos, 0.0);
+ // specular 1: a glTF material reflects unless KHR_materials_specular says less.
+ vec3 lit = applyLightingPBR(base.rgb, N, P, viewDirection(P),
+ u_metallic * mr.b, u_roughness * mr.g, 1.0, ao);
fragColor = vec4(lit + u_emissive.rgb * texture(u_emissiveMap, v_texCoord).rgb, base.a);
}
#pragma end
@@ -383,7 +391,11 @@ void main() {
#endif
let occl = textureSampleLevel(u_occlusionMap, u_occlusionMap_s, v.v_texCoord, 0.0).r;
let ao = mix(1.0, occl, mc.u_occlusionStrength);
- let lit = applyLighting2DAO(base.rgb, N, v.v_worldPos, ao);
+ let mr = textureSampleLevel(u_metallicRoughnessMap, u_metallicRoughnessMap_s,
+ v.v_texCoord, 0.0).rgb;
+ let P = vec3f(v.v_worldPos, 0.0);
+ let lit = applyLightingPBR(base.rgb, N, P, viewDirection(P),
+ mc.u_metallic * mr.b, mc.u_roughness * mr.g, 1.0, ao);
let emit = mc.u_emissive.rgb
* textureSampleLevel(u_emissiveMap, u_emissiveMap_s, v.v_texCoord, 0.0).rgb;
return vec4f(lit + emit, base.a);
diff --git a/tools/renderScenes.mjs b/tools/renderScenes.mjs
index c65db75a6..2e654dd66 100644
--- a/tools/renderScenes.mjs
+++ b/tools/renderScenes.mjs
@@ -130,6 +130,10 @@ export const SCENES = [
// light over each. Specular 1 saturates white, specular 0 stays diffuse green,
// and the view vector under a PERSPECTIVE camera no untouched block can fake.
{ id: "mat-pbr", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_SCENE: "/scenes/mat-pbr.esscene", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "2", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.5,\"y\":0.5,\"rgb\":[255,255,255],\"tol\":20},{\"x\":0.2,\"y\":0.5,\"rgb\":[0,76,0],\"tol\":20},{\"x\":0.8,\"y\":0.5,\"rgb\":[86,128,248],\"tol\":20},{\"x\":0.5,\"y\":0.08,\"rgb\":[0,0,0],\"tol\":20}]" } },
+ // What an imported model's material now says: same albedo, same light, same
+ // roughness — metal 1 has no diffuse and reflects its own colour, metal 0
+ // keeps the diffuse and adds a white lobe (ignore roughness: white drops 97).
+ { id: "mat-model-pbr", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_SCENE: "/scenes/mat-model.esscene", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "2", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.25,\"y\":0.5,\"rgb\":[0,255,0],\"tol\":20},{\"x\":0.75,\"y\":0.5,\"rgb\":[100,253,100],\"tol\":20},{\"x\":0.5,\"y\":0.5,\"rgb\":[0,0,0],\"tol\":20}]" } },
{ id: "material-builtin-lit", tier: "nightly", webgpu: true, env: { ESTELLA_VERIFY_SCENE: "/scenes/mat-builtin-lit.esscene", ESTELLA_VERIFY_MANIFEST: "/scenes/mat-builtin-lit.textures.json", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "2", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.5,\"y\":0.5,\"rgb\":[0,255,0],\"tol\":30}]" } },
{ id: "material-dissolve", tier: "nightly", webgpu: true, env: { ESTELLA_VERIFY_SCENE: "/scenes/mat-dissolve.esscene", ESTELLA_VERIFY_MANIFEST: "/scenes/mat-dissolve.textures.json", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "2", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.5,\"y\":0.5,\"rgb\":[255,0,0],\"tol\":30}]" } },
{ id: "material-graph", tier: "nightly", webgpu: true, env: { ESTELLA_VERIFY_SCENE: "/scenes/mat-graph.esscene", ESTELLA_VERIFY_MANIFEST: "/scenes/mat-graph.textures.json", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "2", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.5,\"y\":0.5,\"rgb\":[255,0,0],\"tol\":40}]" } },