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): share native BRDF for specular glossiness materials

yhh649b85a09d04
diff --git a/fixtures/scenes/mat-specular-glossiness.esscene b/fixtures/scenes/mat-specular-glossiness.esscene
new file mode 100644
index 000000000..4012a58c2
--- /dev/null
+++ b/fixtures/scenes/mat-specular-glossiness.esscene
@@ -0,0 +1,170 @@
+{
+ "version": 4,
+ "name": "SpecularGlossiness",
+ "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.3,
+ "g": 0.6,
+ "b": 0.2,
+ "a": 1
+ },
+ "material": "/scenes/specular-glossiness/factors.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.3,
+ "g": 0.6,
+ "b": 0.2,
+ "a": 1
+ },
+ "material": "/scenes/specular-glossiness/texture.esmaterial"
+ }
+ }
+ ]
+ },
+ {
+ "id": 3,
+ "name": "MetalLight",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": -150,
+ "y": 0,
+ "z": 0
+ }
+ }
+ },
+ {
+ "type": "Light",
+ "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": "Light",
+ "data": {
+ "type": 0,
+ "color": {
+ "r": 1,
+ "g": 1,
+ "b": 1,
+ "a": 1
+ },
+ "intensity": 1,
+ "radius": 150
+ }
+ }
+ ]
+ }
+ ]
+}
diff --git a/fixtures/scenes/normalmap-scale-negative.esmaterial b/fixtures/scenes/normalmap-scale-negative.esmaterial
new file mode 100644
index 000000000..25a83aecc
--- /dev/null
+++ b/fixtures/scenes/normalmap-scale-negative.esmaterial
@@ -0,0 +1,15 @@
+{
+ "version": "1.0",
+ "type": "material",
+ "shader": "builtin:model",
+ "blendMode": 9,
+ "depthTest": true,
+ "depthWrite": true,
+ "cull": 1,
+ "properties": {
+ "u_normalMap": "normalmap-quad_0.png",
+ "u_metallic": 0,
+ "u_roughness": 1,
+ "u_normalScale": -1
+ }
+}
diff --git a/fixtures/scenes/normalmap-scale-negative.esprefab b/fixtures/scenes/normalmap-scale-negative.esprefab
new file mode 100644
index 000000000..921abef9c
--- /dev/null
+++ b/fixtures/scenes/normalmap-scale-negative.esprefab
@@ -0,0 +1,37 @@
+{
+ "version": "2",
+ "name": "normalmap-scale-negative",
+ "rootEntityId": "n0",
+ "entities": [
+ {
+ "prefabEntityId": "n0",
+ "name": "Quad",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {}
+ },
+ {
+ "type": "MeshRenderer",
+ "data": {
+ "mesh": "scenes/normalmap-quad.esmesh",
+ "lit": true,
+ "material": "scenes/normalmap-scale-negative.esmaterial",
+ "color": {
+ "r": 1,
+ "g": 1,
+ "b": 1,
+ "a": 1
+ },
+ "opaque": true,
+ "cullBackfaces": true,
+ "enabled": true
+ }
+ }
+ ]
+ }
+ ]
+}
diff --git a/fixtures/scenes/normalmap-scale-zero.esmaterial b/fixtures/scenes/normalmap-scale-zero.esmaterial
new file mode 100644
index 000000000..98c23ba9a
--- /dev/null
+++ b/fixtures/scenes/normalmap-scale-zero.esmaterial
@@ -0,0 +1,15 @@
+{
+ "version": "1.0",
+ "type": "material",
+ "shader": "builtin:model",
+ "blendMode": 9,
+ "depthTest": true,
+ "depthWrite": true,
+ "cull": 1,
+ "properties": {
+ "u_normalMap": "normalmap-quad_0.png",
+ "u_metallic": 0,
+ "u_roughness": 1,
+ "u_normalScale": 0
+ }
+}
diff --git a/fixtures/scenes/normalmap-scale-zero.esprefab b/fixtures/scenes/normalmap-scale-zero.esprefab
new file mode 100644
index 000000000..9a86be5d6
--- /dev/null
+++ b/fixtures/scenes/normalmap-scale-zero.esprefab
@@ -0,0 +1,37 @@
+{
+ "version": "2",
+ "name": "normalmap-scale-zero",
+ "rootEntityId": "n0",
+ "entities": [
+ {
+ "prefabEntityId": "n0",
+ "name": "Quad",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {}
+ },
+ {
+ "type": "MeshRenderer",
+ "data": {
+ "mesh": "scenes/normalmap-quad.esmesh",
+ "lit": true,
+ "material": "scenes/normalmap-scale-zero.esmaterial",
+ "color": {
+ "r": 1,
+ "g": 1,
+ "b": 1,
+ "a": 1
+ },
+ "opaque": true,
+ "cullBackfaces": true,
+ "enabled": true
+ }
+ }
+ ]
+ }
+ ]
+}
diff --git a/fixtures/scenes/specular-glossiness-depth.esprefab b/fixtures/scenes/specular-glossiness-depth.esprefab
new file mode 100644
index 000000000..84365c56f
--- /dev/null
+++ b/fixtures/scenes/specular-glossiness-depth.esprefab
@@ -0,0 +1,94 @@
+{
+ "version": "2",
+ "name": "SpecularGlossinessDepth",
+ "rootEntityId": "root",
+ "entities": [
+ {
+ "prefabEntityId": "root",
+ "name": "DepthPair",
+ "parent": null,
+ "children": [
+ "front",
+ "rear"
+ ],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {}
+ }
+ ]
+ },
+ {
+ "prefabEntityId": "front",
+ "name": "front",
+ "parent": "root",
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 0,
+ "y": 0,
+ "z": 1
+ }
+ }
+ },
+ {
+ "type": "MeshRenderer",
+ "data": {
+ "mesh": "scenes/normalmap-quad.esmesh",
+ "lit": false,
+ "color": {
+ "r": 1,
+ "g": 0,
+ "b": 0,
+ "a": 1
+ },
+ "opaque": true,
+ "cullBackfaces": true,
+ "enabled": true
+ }
+ }
+ ]
+ },
+ {
+ "prefabEntityId": "rear",
+ "name": "rear",
+ "parent": "root",
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 0,
+ "y": 0,
+ "z": 0
+ }
+ }
+ },
+ {
+ "type": "MeshRenderer",
+ "data": {
+ "mesh": "scenes/normalmap-quad.esmesh",
+ "lit": true,
+ "material": "scenes/specular-glossiness/blended.esmaterial",
+ "color": {
+ "r": 0,
+ "g": 1,
+ "b": 0,
+ "a": 0.5
+ },
+ "opaque": false,
+ "cullBackfaces": true,
+ "enabled": true
+ }
+ }
+ ]
+ }
+ ]
+}
diff --git a/fixtures/scenes/specular-glossiness/blended.esmaterial b/fixtures/scenes/specular-glossiness/blended.esmaterial
new file mode 100644
index 000000000..a51c8d621
--- /dev/null
+++ b/fixtures/scenes/specular-glossiness/blended.esmaterial
@@ -0,0 +1,20 @@
+{
+ "version": "1.0",
+ "type": "material",
+ "shader": "builtin:model",
+ "blendMode": 0,
+ "depthTest": true,
+ "switches": {
+ "ES_SPECULAR_GLOSSINESS": true
+ },
+ "depthWrite": false,
+ "properties": {
+ "u_specularColor": {
+ "r": 0,
+ "g": 0,
+ "b": 0,
+ "a": 1
+ },
+ "u_glossiness": 0
+ }
+}
diff --git a/fixtures/scenes/specular-glossiness/factors.esmaterial b/fixtures/scenes/specular-glossiness/factors.esmaterial
new file mode 100644
index 000000000..3f56dbc7d
--- /dev/null
+++ b/fixtures/scenes/specular-glossiness/factors.esmaterial
@@ -0,0 +1,19 @@
+{
+ "version": "1.0",
+ "type": "material",
+ "shader": "builtin:model",
+ "blendMode": 0,
+ "depthTest": false,
+ "switches": {
+ "ES_SPECULAR_GLOSSINESS": true
+ },
+ "properties": {
+ "u_specularColor": {
+ "r": 0.10793025005694963,
+ "g": 0.030761675024425943,
+ "b": 0.4043063083007554,
+ "a": 1
+ },
+ "u_glossiness": 0.40156862745098043
+ }
+}
diff --git a/fixtures/scenes/specular-glossiness/packed.png b/fixtures/scenes/specular-glossiness/packed.png
new file mode 100644
index 000000000..feb87ad86
Binary files /dev/null and b/fixtures/scenes/specular-glossiness/packed.png differ
diff --git a/fixtures/scenes/specular-glossiness/packed.png.meta b/fixtures/scenes/specular-glossiness/packed.png.meta
new file mode 100644
index 000000000..f560c6e04
--- /dev/null
+++ b/fixtures/scenes/specular-glossiness/packed.png.meta
@@ -0,0 +1,12 @@
+{
+ "uuid": "017b8d4a-8d0d-4446-9b0d-462d25c38b89",
+ "version": "2.0",
+ "type": "texture",
+ "importer": {
+ "sRGB": false,
+ "premultiplyAlpha": false,
+ "filterMode": "nearest",
+ "wrapMode": "clamp",
+ "compress": false
+ }
+}
diff --git a/fixtures/scenes/specular-glossiness/texture.esmaterial b/fixtures/scenes/specular-glossiness/texture.esmaterial
new file mode 100644
index 000000000..40aa9d175
--- /dev/null
+++ b/fixtures/scenes/specular-glossiness/texture.esmaterial
@@ -0,0 +1,20 @@
+{
+ "version": "1.0",
+ "type": "material",
+ "shader": "builtin:model",
+ "blendMode": 0,
+ "depthTest": false,
+ "switches": {
+ "ES_SPECULAR_GLOSSINESS": true
+ },
+ "properties": {
+ "u_specularColor": {
+ "r": 0.5,
+ "g": 0.6,
+ "b": 0.7,
+ "a": 1
+ },
+ "u_glossiness": 0.8,
+ "u_metallicRoughnessMap": "packed.png"
+ }
+}
diff --git a/sdk/src/character/characterShaders.ts b/sdk/src/character/characterShaders.ts
index c6248e280..b6a877224 100644
--- a/sdk/src/character/characterShaders.ts
+++ b/sdk/src/character/characterShaders.ts
@@ -16,6 +16,7 @@ const HAIR = `#pragma shader "Hair"
#pragma domain Lit
#pragma param u_tint color default(1,1,1,1)
#pragma param u_normalMap texture default(flatnormal) texel(data)
+#pragma param u_normalScale float default(1) range(-4,4) ui(slider)
#pragma param u_ormMap texture default(white) texel(data)
#pragma param u_occlusionStrength float default(1) range(0,1) ui(slider)
// Which way each strand runs: red and green are a direction across the texture, the way an authored flow map writes it.
@@ -96,7 +97,7 @@ void main() {
#ifdef MESH_NORMALS
highp vec3 Ng = normalize(v_worldNormal);
highp vec3 P = v_worldXYZ;
- highp vec3 N = perturbNormal(Ng, P, v_texCoord, sampleNormal(u_normalMap, v_texCoord));
+ highp vec3 N = perturbNormal(Ng, P, v_texCoord, scaleNormal(sampleNormal(u_normalMap, v_texCoord), u_normalScale));
if (!gl_FrontFacing) { N = -N; Ng = -Ng; }
// The strand: the flow map's direction across the texture, put onto the surface and made to lie across the normal.
vec2 flow = texture(u_flowMap, v_texCoord).rg * 2.0 - 1.0;
@@ -192,7 +193,7 @@ fn packNormal(n : vec3f) -> vec2f {
#ifdef MESH_NORMALS
var Ng = normalize(v.v_worldNormal);
let P = v.v_worldXYZ;
- var N = perturbNormal(Ng, P, v.v_texCoord, sampleNormalGrad(u_normalMap, u_normalMap_s, v.v_texCoord, duvx, duvy));
+ var N = perturbNormal(Ng, P, v.v_texCoord, scaleNormal(sampleNormalGrad(u_normalMap, u_normalMap_s, v.v_texCoord, duvx, duvy), mc.u_normalScale));
if (!front) { N = -N; Ng = -Ng; }
var T = perturbNormal(Ng, P, v.v_texCoord, vec3f(flowTex, 0.0));
T = T - N * dot(N, T);
@@ -261,6 +262,7 @@ const SKIN = `#pragma shader "Skin"
#pragma domain Lit
#pragma param u_tint color default(1,1,1,1)
#pragma param u_normalMap texture default(flatnormal) texel(data)
+#pragma param u_normalScale float default(1) range(-4,4) ui(slider)
#pragma param u_ormMap texture default(white) texel(data)
#pragma param u_occlusionStrength float default(1) range(0,1) ui(slider)
// A second, fine normal map tiled over the surface (pores, creases) laid on the sharp normal the shine reads: how strongly, and how many times it repeats.
@@ -320,7 +322,7 @@ out vec4 fragColor;
#endif
vec3 skinNormalAt(float bias) {
- return normalize(texture(u_normalMap, v_texCoord, bias).xyz * 2.0 - 1.0);
+ return scaleNormal(texture(u_normalMap, v_texCoord, bias).xyz * 2.0 - 1.0, u_normalScale);
}
// The baked normal with the detail's slope added to it, in the tangent frame both are read in.
@@ -414,10 +416,10 @@ fn packNormal(n : vec3f) -> vec2f {
let base = textureSample(t0, s0, v.v_texCoord) * v.v_color * mc.u_tint;
let orm = textureSample(u_ormMap, u_ormMap_s, v.v_texCoord);
let detail = textureSample(u_detailMap, u_detailMap_s, v.v_texCoord * mc.u_detailTile).xy * 2.0 - vec2f(1.0);
- let bakedSharp = normalize(textureSampleBias(u_normalMap, u_normalMap_s, v.v_texCoord, 0.0).xyz * 2.0 - vec3f(1.0));
+ let bakedSharp = scaleNormal(textureSampleBias(u_normalMap, u_normalMap_s, v.v_texCoord, 0.0).xyz * 2.0 - vec3f(1.0), mc.u_normalScale);
let tangentSharp = normalize(vec3f(bakedSharp.xy + detail * mc.u_detailStrength, bakedSharp.z));
- let tangentMid = normalize(textureSampleBias(u_normalMap, u_normalMap_s, v.v_texCoord, mc.u_blurGreen).xyz * 2.0 - vec3f(1.0));
- let tangentSoft = normalize(textureSampleBias(u_normalMap, u_normalMap_s, v.v_texCoord, mc.u_blurRed).xyz * 2.0 - vec3f(1.0));
+ let tangentMid = scaleNormal(textureSampleBias(u_normalMap, u_normalMap_s, v.v_texCoord, mc.u_blurGreen).xyz * 2.0 - vec3f(1.0), mc.u_normalScale);
+ let tangentSoft = scaleNormal(textureSampleBias(u_normalMap, u_normalMap_s, v.v_texCoord, mc.u_blurRed).xyz * 2.0 - vec3f(1.0), mc.u_normalScale);
#ifdef MESH_NORMALS
var Ng = normalize(v.v_worldNormal);
let P = v.v_worldXYZ;
diff --git a/sdk/src/render/builtinShaders.ts b/sdk/src/render/builtinShaders.ts
index 67b13db58..7583b3d18 100644
--- a/sdk/src/render/builtinShaders.ts
+++ b/sdk/src/render/builtinShaders.ts
@@ -461,8 +461,10 @@ void main() {
const MODEL = `#pragma shader "Model"
#pragma version 300 es
#pragma domain Lit
+#pragma switch ES_SPECULAR_GLOSSINESS
#pragma param u_tint color default(1,1,1,1)
#pragma param u_normalMap texture default(flatnormal) texel(data)
+#pragma param u_normalScale float default(1) range(-4,4) ui(slider)
#pragma param u_emissive color default(0,0,0,1)
#pragma param u_emissiveMap texture default(white)
#pragma param u_occlusionMap texture default(white) texel(data)
@@ -472,6 +474,10 @@ 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)
+// The same packed-map unit serves either workflow; the specular/glossiness permutation
+// reads sRGB specular from RGB and linear gloss from A, without adding a sampler.
+#pragma param u_specularColor color default(1,1,1,1)
+#pragma param u_glossiness float default(1) range(0,1) ui(slider)
// 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)
@@ -570,14 +576,14 @@ void main() {
#ifdef MESH_NORMALS
highp vec3 Ng = normalize(v_worldNormal);
highp vec3 N = perturbNormal(Ng, v_worldXYZ, v_texCoord,
- sampleNormal(u_normalMap, v_texCoord));
+ scaleNormal(sampleNormal(u_normalMap, v_texCoord), u_normalScale));
// A double-sided surface is lit from the side it is seen from (glTF's rule).
if (!gl_FrontFacing) { N = -N; Ng = -Ng; }
#else
- vec3 N = sampleNormal(u_normalMap, v_texCoord);
+ vec3 N = scaleNormal(sampleNormal(u_normalMap, v_texCoord), u_normalScale);
#endif
float ao = mix(1.0, texture(u_occlusionMap, v_texCoord).r, u_occlusionStrength);
- vec3 mr = texture(u_metallicRoughnessMap, v_texCoord).rgb;
+ vec4 mr = texture(u_metallicRoughnessMap, v_texCoord);
#ifdef MESH_NORMALS
// The surface's OWN place, which a point light, a probe volume and a
// reflection's box projection all measure against; z = 0 is a plane's answer.
@@ -586,7 +592,14 @@ void main() {
highp vec3 P = vec3(v_worldPos, 0.0);
#endif
// specular 1: a glTF material reflects unless KHR_materials_specular says less.
+ #ifdef ES_SPECULAR_GLOSSINESS
+ vec3 specularColor = clamp(u_specularColor.rgb * srgbToLinear(mr.rgb), 0.0, 1.0);
+ float metallic = 0.0;
+ float roughness = 1.0 - u_glossiness * mr.a;
+#else
+ float metallic = u_metallic * mr.b;
float roughness = u_roughness * mr.g;
+#endif
#ifdef MESH_NORMALS
roughness = specularAA(dndx, dndy, roughness);
#endif
@@ -645,15 +658,21 @@ void main() {
base.rgb = mix(base.rgb, base.rgb * u_attenuationColor.rgb, u_transmission);
}
}
+#ifdef ES_SPECULAR_GLOSSINESS
+ vec3 diffuse = base.rgb * (1.0 - max(max(specularColor.r, specularColor.g), specularColor.b));
+ vec3 lit = applyLightingSurfaceBRDF(diffuse, specularColor, N, P, viewDir, roughness, 1.0, ao, ext);
+#else
+ vec3 diffuse = base.rgb * (1.0 - clamp(metallic, 0.0, 1.0));
vec3 lit = applyLightingSurface(base.rgb * (1.0 - u_transmission * refracted), N, P, viewDir,
- u_metallic * mr.b, roughness, u_specular, ao, ext);
+ metallic, roughness, u_specular, ao, ext);
+#endif
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
// multisampling averaged with an unwritten one falls below it, and an effect can tell such an edge from a surface.
- fragSurface = vec4(packNormal(normalize(N)), roughness, 0.75 + 0.25 * clamp(u_metallic * mr.b, 0.0, 1.0));
- fragAlbedo = vec4(base.rgb * (1.0 - clamp(u_metallic * mr.b, 0.0, 1.0)), 1.0);
+ fragSurface = vec4(packNormal(normalize(N)), roughness, 0.75 + 0.25 * clamp(metallic, 0.0, 1.0));
+ fragAlbedo = vec4(diffuse, 1.0);
#endif
}
#pragma end
@@ -716,20 +735,27 @@ fn packNormal(n : vec3f) -> vec2f {
#ifdef MESH_NORMALS
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));
+ scaleNormal(sampleNormalGrad(u_normalMap, u_normalMap_s, v.v_texCoord, duvx, duvy), mc.u_normalScale));
if (!front) { N = -N; Ng = -Ng; }
#else
- let N = sampleNormalGrad(u_normalMap, u_normalMap_s, v.v_texCoord, duvx, duvy);
+ let N = scaleNormal(sampleNormalGrad(u_normalMap, u_normalMap_s, v.v_texCoord, duvx, duvy), mc.u_normalScale);
#endif
let occl = textureSampleGrad(u_occlusionMap, u_occlusionMap_s, v.v_texCoord, duvx, duvy).r;
let ao = mix(1.0, occl, mc.u_occlusionStrength);
- let mr = textureSampleGrad(u_metallicRoughnessMap, u_metallicRoughnessMap_s, v.v_texCoord, duvx, duvy).rgb;
+ let mr = textureSampleGrad(u_metallicRoughnessMap, u_metallicRoughnessMap_s, v.v_texCoord, duvx, duvy);
#ifdef MESH_NORMALS
let P = v.v_worldXYZ;
#else
let P = vec3f(v.v_worldPos, 0.0);
#endif
+ #ifdef ES_SPECULAR_GLOSSINESS
+ let specularColor = clamp(mc.u_specularColor.rgb * srgbToLinear(mr.rgb), vec3f(0.0), vec3f(1.0));
+ let metallic = 0.0;
+ var roughness = 1.0 - mc.u_glossiness * mr.a;
+#else
+ let metallic = mc.u_metallic * mr.b;
var roughness = mc.u_roughness * mr.g;
+#endif
#ifdef MESH_NORMALS
roughness = specularAA(dndx, dndy, roughness);
#endif
@@ -788,16 +814,22 @@ fn packNormal(n : vec3f) -> vec2f {
base = vec4f(mix(base.rgb, base.rgb * mc.u_attenuationColor.rgb, vec3f(mc.u_transmission)), base.a);
}
}
+#ifdef ES_SPECULAR_GLOSSINESS
+ let diffuse = base.rgb * (1.0 - max(max(specularColor.r, specularColor.g), specularColor.b));
+ let lit = applyLightingSurfaceBRDF(diffuse, specularColor, N, P, viewDir, roughness, 1.0, ao, ext);
+#else
+ let diffuse = base.rgb * (1.0 - clamp(metallic, 0.0, 1.0));
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);
+ metallic, roughness, mc.u_specular, ao, ext);
+#endif
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
var frag : FragOut;
frag.color = vec4f(lit + seen + emit, alpha);
- frag.surface = vec4f(packNormal(normalize(N)), roughness, 0.75 + 0.25 * clamp(mc.u_metallic * mr.b, 0.0, 1.0));
- frag.albedo = vec4f(base.rgb * (1.0 - clamp(mc.u_metallic * mr.b, 0.0, 1.0)), 1.0);
+ frag.surface = vec4f(packNormal(normalize(N)), roughness, 0.75 + 0.25 * clamp(metallic, 0.0, 1.0));
+ frag.albedo = vec4f(diffuse, 1.0);
return frag;
#else
return vec4f(lit + seen + emit, alpha);
diff --git a/sdk/tests/model-shader-specular-aa.test.ts b/sdk/tests/model-shader-specular-aa.test.ts
index 14a2cbe6e..f33fdaa28 100644
--- a/sdk/tests/model-shader-specular-aa.test.ts
+++ b/sdk/tests/model-shader-specular-aa.test.ts
@@ -32,8 +32,12 @@ 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(/applyLightingSurface\([^;]*u_metallic \* mr\.b, roughness,/);
+ expect(glsl).toMatch(/float metallic = u_metallic \* mr\.b/);
+ expect(glsl).toMatch(/applyLightingSurface\([^;]*metallic, roughness,/);
+ expect(glsl).toMatch(/applyLightingSurfaceBRDF\([^;]*roughness,/);
expect(wgsl).toMatch(/roughness = specularAA\(dndx, dndy, roughness\)/);
- expect(wgsl).toMatch(/applyLightingSurface\([^;]*mc\.u_metallic \* mr\.b, roughness,/);
+ expect(wgsl).toMatch(/let metallic = mc\.u_metallic \* mr\.b/);
+ expect(wgsl).toMatch(/applyLightingSurface\([^;]*metallic, roughness,/);
+ expect(wgsl).toMatch(/applyLightingSurfaceBRDF\([^;]*roughness,/);
});
});
diff --git a/src/esengine/resource/ShaderParser.cpp b/src/esengine/resource/ShaderParser.cpp
index 0d5457d68..6732a7684 100644
--- a/src/esengine/resource/ShaderParser.cpp
+++ b/src/esengine/resource/ShaderParser.cpp
@@ -1785,6 +1785,11 @@ fn sampleNormalGrad(map : texture_2d<f32>, samp : sampler, uv : vec2f,
let xy = textureSampleGrad(map, samp, uv, ddx, ddy).xy * 2.0 - 1.0;
return vec3f(xy, sqrt(max(0.0, 1.0 - dot(xy, xy))));
}
+fn scaleNormal(normal : vec3f, scale : f32) -> vec3f {
+ let n = normal * vec3f(scale, scale, 1.0);
+ if (dot(n, n) < 1e-12) { return vec3f(0.0, 0.0, 1.0); }
+ return normalize(n);
+}
fn perturbNormal(N : vec3f, worldPos : vec3f, uv : vec2f, tangentNormal : vec3f) -> vec3f {
// The derivatives first: they are only valid in uniform control flow, and the choice below depends on a variable.
let dp1 = dpdx(worldPos);
@@ -2120,9 +2125,8 @@ fn filmFresnel(F : vec3f, cosTheta : f32, film : vec4f) -> vec3f {
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));
+fn applyLightingSurfaceBRDF(diffuse : vec3f, F0 : vec3f, N : vec3f, worldPos : vec3f, V : vec3f,
+ roughness : f32, specular : f32, ao : f32, ext : SurfaceExt) -> vec3f {
let a = max(roughness * roughness, 1e-3);
let NdotV = max(dot(N, V), 1e-4);
var lit = indirectIrradiance(N) * ao;
@@ -2194,7 +2198,7 @@ fn applyLightingSurface(albedo : vec3f, N : vec3f, worldPos : vec3f, V : vec3f,
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 * kd + gloss;
+ let base = diffuse * lit * kd + gloss;
var baseScale = 1.0;
if (ext.coat.x > 0.0) {
let coatNdotV = max(dot(ext.coatN.xyz, V), 1e-4);
@@ -2210,6 +2214,11 @@ fn applyLightingSurface(albedo : vec3f, N : vec3f, worldPos : vec3f, V : vec3f,
}
return base * baseScale + extra;
}
+fn applyLightingSurface(albedo : vec3f, N : vec3f, worldPos : vec3f, V : vec3f, metallic : f32,
+ roughness : f32, specular : f32, ao : f32, ext : SurfaceExt) -> vec3f {
+ return applyLightingSurfaceBRDF(albedo * (1.0 - metallic), mix(vec3f(ext.misc.x), albedo, vec3f(metallic)),
+ N, worldPos, V, roughness, specular, ao, ext);
+}
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));
@@ -3167,6 +3176,10 @@ ShaderParser::AssembledStage ShaderParser::assembleStageEx(const ParsedShader& p
" highp vec2 xy = textureGrad(map, uv, ddx, ddy).xy * 2.0 - 1.0;\n"
" return vec3(xy, sqrt(max(0.0, 1.0 - dot(xy, xy))));\n"
"}\n"
+ "highp vec3 scaleNormal(in highp vec3 normal, in highp float scale) {\n"
+ " highp vec3 n = normal * vec3(scale, scale, 1.0);\n"
+ " return dot(n, n) < 1e-12 ? vec3(0.0, 0.0, 1.0) : normalize(n);\n"
+ "}\n"
// Tangent frame from screen-space derivatives, so a tangent-space normal
// lands in world space without the geometry carrying a tangent channel —
// which most exported models do not. Degenerate UVs return N unchanged.
@@ -3561,13 +3574,12 @@ ShaderParser::AssembledStage ShaderParser::assembleStageEx(const ParsedShader& p
// 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 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"
+ "highp vec3 applyLightingSurfaceBRDF(in highp vec3 diffuse, in highp vec3 F0, in highp vec3 N,\n"
+ " in highp vec3 worldPos, in highp vec3 V,\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(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
@@ -3636,7 +3648,7 @@ ShaderParser::AssembledStage ShaderParser::assembleStageEx(const ParsedShader& p
" gloss += envRadiance(envR, roughness, reflectionColumn(), worldPos)\n"
" * (ao * specular)\n"
" * envF;\n"
- " highp vec3 base = albedo * (1.0 - metallic) * lit * kd + gloss;\n"
+ " highp vec3 base = diffuse * lit * kd + 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"
@@ -3652,6 +3664,12 @@ ShaderParser::AssembledStage ShaderParser::assembleStageEx(const ParsedShader& p
" }\n"
" return base * baseScale + extra;\n"
"}\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, in highp float roughness,\n"
+ " in highp float specular, in highp float ao, in SurfaceExt ext) {\n"
+ " return applyLightingSurfaceBRDF(albedo * (1.0 - metallic), mix(vec3(ext.misc.x), albedo, metallic),\n"
+ " N, worldPos, V, roughness, specular, ao, ext);\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"
diff --git a/tools/renderScenes.mjs b/tools/renderScenes.mjs
index 7297ba3bb..fd9ca7b93 100644
--- a/tools/renderScenes.mjs
+++ b/tools/renderScenes.mjs
@@ -665,6 +665,22 @@ export const SCENES = [
// 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}]" } },
+ // Equal BRDF inputs expressed once as constants and once through the legacy
+ // specular/glossiness map. RGB is sRGB; alpha stays linear. Using metallic/
+ // roughness, decoding alpha, or ignoring either factor breaks this pair.
+ { id: 'mat-specular-glossiness', tier: 'pr', webgpu: true, env: {
+ ESTELLA_VERIFY_SCENE: '/scenes/mat-specular-glossiness.esscene',
+ ESTELLA_VERIFY_W: '256', ESTELLA_VERIFY_H: '256', ESTELLA_VERIFY_STEPS: '4',
+ ESTELLA_VERIFY_TWINS: JSON.stringify({ pairs: [[0.25, 0.5, 0.75, 0.5]], tol: 2 }),
+ ESTELLA_VERIFY_EXPECT: JSON.stringify([{ x: 0.25, y: 0.5, rgb: [95, 103, 235], tol: 8 }]),
+ } },
+ // Exercise the shipped texture uploader and colour-space handling too.
+ { id: 'mat-specular-glossiness-runtime', tier: 'pr', webgpu: true, env: {
+ ESTELLA_VERIFY_SCENE: '/scenes/mat-specular-glossiness.esscene',
+ ESTELLA_VERIFY_W: '256', ESTELLA_VERIFY_H: '256', ESTELLA_VERIFY_STEPS: '4',
+ ESTELLA_VERIFY_COLORSPACE: 'linear', ESTELLA_VERIFY_TEXTURES: 'runtime',
+ ESTELLA_VERIFY_TWINS: JSON.stringify({ pairs: [[0.25, 0.5, 0.75, 0.5]], tol: 2 }),
+ } },
// The same two materials with NO light on them, only the ambient environment.
// A metal has no diffuse, so reflecting that environment is all it has: remove
// the reflection and the left probe is black while the right one barely moves.
@@ -803,6 +819,27 @@ export const SCENES = [
// both halves take the same light. Its texels point -X and +X, so the lit half
// and the unlit one are the tangent frame, derived per pixel from derivatives.
{ id: "mesh-normalmap", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_PREFAB: "/scenes/normalmap-quad.esprefab", ESTELLA_VERIFY_SCENE: "/scenes/mesh-normalmap.esscene", ESTELLA_VERIFY_MANIFEST: "/scenes/mesh-normalmap.textures.json", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "4", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.375,\"y\":0.5,\"rgb\":[180,180,180],\"tol\":45},{\"x\":0.625,\"y\":0.5,\"rgb\":[0,0,0],\"tol\":30}]" } },
+ // Zero strength makes both oppositely tilted texels flat, even when their Z
+ // is zero. Negative strength reverses which half faces the light.
+ ...[
+ ['zero', [[180, 180, 180], [180, 180, 180]]],
+ ['negative', [[0, 0, 0], [180, 180, 180]]],
+ ].map(([name, colors]) => ({ id: `mesh-normalmap-scale-${name}`, tier: 'pr', webgpu: true, env: {
+ ESTELLA_VERIFY_PREFAB: `/scenes/normalmap-scale-${name}.esprefab`,
+ ESTELLA_VERIFY_SCENE: '/scenes/mesh-normalmap.esscene',
+ ESTELLA_VERIFY_MANIFEST: '/scenes/mesh-normalmap.textures.json',
+ ESTELLA_VERIFY_W: '256', ESTELLA_VERIFY_H: '256', ESTELLA_VERIFY_STEPS: '4',
+ ESTELLA_VERIFY_EXPECT: JSON.stringify(colors.map((rgb, i) => ({ x: i ? 0.625 : 0.375, y: 0.5, rgb, tol: name === 'zero' ? 12 : 45 }))),
+ } })),
+ // A blended green mesh behind an opaque red mesh must not paint over it.
+ // This also compiles the specular/glossiness variant on the mesh path.
+ { id: 'mesh-specular-glossiness-depth', tier: 'pr', webgpu: true, env: {
+ ESTELLA_VERIFY_PREFAB: '/scenes/specular-glossiness-depth.esprefab',
+ ESTELLA_VERIFY_SCENE: '/scenes/mesh-normalmap.esscene',
+ ESTELLA_VERIFY_MANIFEST: '/scenes/mesh-normalmap.textures.json',
+ ESTELLA_VERIFY_W: '256', ESTELLA_VERIFY_H: '256', ESTELLA_VERIFY_STEPS: '4',
+ ESTELLA_VERIFY_EXPECT: JSON.stringify([{ x: 0.5, y: 0.5, rgb: [255, 0, 0], tol: 8 }]),
+ } },
// The quad of mesh-normalmap with a tangent a quarter turn from the one its UVs imply: read against the frame the mesh
// carries, both halves come out alike (33), where the derived frame lights them 180 and 0.
{ id: "mesh-normalmap-tangent", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_PREFAB: "/scenes/normalmap-quad-tangent.esprefab", ESTELLA_VERIFY_SCENE: "/scenes/mesh-normalmap-tangent.esscene", ESTELLA_VERIFY_MANIFEST: "/scenes/mesh-normalmap-tangent.textures.json", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "4", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.375,\"y\":0.5,\"rgb\":[33,33,33],\"tol\":12},{\"x\":0.625,\"y\":0.5,\"rgb\":[33,33,33],\"tol\":12}]" } },