3D engine that AI agents drive
Read the box-mapping varyings only when a material uses them (#388)
Nguyen Xuan An3018013204e7
diff --git a/changes/388-box-map-varyings.md b/changes/388-box-map-varyings.mdnew file mode 100644index 00000000..7837ec53--- /dev/null+++ b/changes/388-box-map-varyings.md@@ -0,0 +1 @@+- Draw the plain lit frame faster (issue #388): the fragment shader now reads the three box-mapping varyings (`boxPosition`, `boxNormal`, `boxNormalMatrix`) only when a material sets `boxMapScale`, so a scene without box mapping no longer makes every vertex compute and pass 15 floats it never uses. On the GPU the main pass of the plain Lakeside, Old church and Warehouse worlds at 1920x1080 drops by 0.2 to 0.5 ms per frame, and the 16 bit target, the MSAA resolve and the post pass turn out to cost 0.3 ms of the 3.3 ms. Output is byte identical on the reference scenes. `entry:core` and both goal scenes get 18 to 20 bytes smaller.diff --git a/src/renderer.js b/src/renderer.jsindex fc234915..699645d7 100644--- a/src/renderer.js+++ b/src/renderer.js@@ -53,6 +53,10 @@ function postUniformLocations(context, program) {}const MAP_SHADER_FEATURES = ['baseMap', 'normalMap', 'metallicRoughnessMap', 'occlusionMap', 'emissiveMap'];+// Part or material fields that name their shader feature and switch it on when set.+const PRESENT_SHADER_FEATURES = ['skin', 'crowd', 'morph', 'lightMap', 'waterReflection'];+// Material fields that name their shader feature and switch it on above zero.+const POSITIVE_SHADER_FEATURES = ['moss', 'anisotropy', 'iridescence', 'toon', 'boxMapScale'];// Tags whose look the main fragment shader draws with code that other tags never reach.const TAG_SHADER_FEATURES = new Set(['floor', 'water', 'foliage', 'terrain', 'grass', 'fire', 'smoke', 'unlit']);@@ -67,24 +71,22 @@ function shaderFeaturesOfLighting(lighting) {function shaderFeaturesOfParts(parts) {const features = new Set();- for (const { material, skin, morph, crowd } of parts) {- if (skin) features.add('skin');- if (crowd) features.add('crowd');- if (morph) features.add('morph');+ for (const part of parts) {+ const { material } = part;+ for (const name of PRESENT_SHADER_FEATURES) {+ if (part[name] || material[name]) features.add(name);+ }MATERIAL_MAPS.forEach((field, index) => {if (material[field]) features.add(MAP_SHADER_FEATURES[index]);});- if (material.lightMap) features.add('lightMap');if (material.environmentProbe) features.add('probe');- if (material.waterReflection) features.add('waterReflection');if (material.normalMap?.uvTransform) features.add('normalMapTransform');Object.keys(material.layerMaps ?? {}).forEach(name => features.add(name));if (material.layers) features.add('splat');- if (material.moss > 0) features.add('moss');- if (material.anisotropy > 0) features.add('anisotropy');+ for (const name of POSITIVE_SHADER_FEATURES) {+ if (material[name] > 0) features.add(name);+ }if (material.lightMask !== undefined) features.add('lightLinking');- if (material.iridescence > 0) features.add('iridescence');- if (material.toon > 0) features.add('toon');if ((material.alphaMode ?? 'opaque') !== 'opaque') features.add('alphaMode');if (material.subsurface?.weight > 0) features.add('subsurface');if (materialLayerValue(material, 'clearcoat') > 0) features.add('clearcoat');diff --git a/src/shaders.js b/src/shaders.jsindex 47ed8550..b7bf4d74 100644--- a/src/shaders.js+++ b/src/shaders.js@@ -533,19 +533,19 @@ void main() {bool canopyShaded = CANOPY_TAG;if (doubleSided > 0.5 && !gl_FrontFacing && !canopyShaded) normal = -normal;vec3 geometricNormal = normal;- if (USES_NORMAL_MAP) normal = boxMapScale > 0.0 ? boxMappedNormal(normal) : mappedNormal(normal);+ if (USES_NORMAL_MAP) normal = BOX_MAPPED ? boxMappedNormal(normal) : mappedNormal(normal);vec3 viewDirection = normalize(eye - worldPosition);vec3 color = mix(baseColor, armorColor, step(0.5, surface.w) * (1.0 - step(1.5, surface.w)));if (!USES_SPLAT) color *= vertexTint;float textureAlpha = 1.0;if (USES_BASE_MAP) {- vec4 sampledBase = boxMapScale > 0.0 ? sampleBox(baseMap) : texture(baseMap, textureUV);+ vec4 sampledBase = BOX_MAPPED ? sampleBox(baseMap) : texture(baseMap, textureUV);color *= sampledBase.rgb;textureAlpha = sampledBase.a;}float alpha = surfaceAlpha(textureAlpha);vec3 emission = emissionColor * surface.z;- if (USES_EMISSIVE_MAP) emission *= (boxMapScale > 0.0 ? sampleBox(emissiveMap) : texture(emissiveMap, mapUV(hasEmissiveMap))).rgb;+ if (USES_EMISSIVE_MAP) emission *= (BOX_MAPPED ? sampleBox(emissiveMap) : texture(emissiveMap, mapUV(hasEmissiveMap))).rgb;if (USES_RENDER_PASS) {outputColor = vec4(renderPassValue(color, normal, emission), 1.0);return;@@ -562,7 +562,7 @@ void main() {roughness = armorSurface.y;}if (USES_METALLIC_ROUGHNESS_MAP) {- vec4 sampled = boxMapScale > 0.0 ? sampleBox(metallicRoughnessMap) : texture(metallicRoughnessMap, mapUV(hasMetallicRoughnessMap));+ vec4 sampled = BOX_MAPPED ? sampleBox(metallicRoughnessMap) : texture(metallicRoughnessMap, mapUV(hasMetallicRoughnessMap));metallic *= sampled.b;roughness *= sampled.g;}@@ -699,6 +699,7 @@ const SHADER_FEATURE_CONDITIONS = {occlusionMap: ['USES_OCCLUSION_MAP', 'hasOcclusionMap > 0.5'],emissiveMap: ['USES_EMISSIVE_MAP', 'hasEmissiveMap > 0.5'],lightMap: ['USES_LIGHT_MAP', 'hasLightMap > 0.5'],+ boxMapScale: ['BOX_MAPPED', 'boxMapScale > 0.0'],splat: ['USES_SPLAT', 'splatEnabled > 0.5'],moss: ['USES_MOSS', 'materialMoss.x > 0.0'],alphaMode: ['USES_ALPHA_MODE', 'alphaMode > 0.5'],