3D engine that AI agents drive
Write the box varyings only for box mapped scenes and pack emission with the instance ordinal (part of #388)
Nguyen Xuan Aneb54a73aa438
diff --git a/changes/388-varyings.md b/changes/388-varyings.mdnew file mode 100644index 00000000..078d7c99--- /dev/null+++ b/changes/388-varyings.md@@ -0,0 +1 @@+- Draw the plain lit frame faster (issue #388): the vertex shader now writes the three box-mapping varyings (`boxPosition`, `boxNormal`, `boxNormalMatrix`) only when a material sets `boxMapScale`, and the per-instance emission colour and object ordinal travel in one flat `vec4` instead of two outputs. A scene without box mapping no longer computes the box position (a transform of every vertex by three matrix column lengths) or passes 15 floats it never reads, and each vertex carries one register fewer. Output is byte identical on the reference scenes. `entry:core` and both goal scenes get 0 to 3 bytes smaller.diff --git a/src/liquid-shader.js b/src/liquid-shader.jsindex ea996608..0fbc6383 100644--- a/src/liquid-shader.js+++ b/src/liquid-shader.js@@ -17,7 +17,6 @@ flat in vec4 surface;in vec4 shadowPosition;in vec2 textureUV;in vec3 vertexTint;-flat in vec3 emissionColor;uniform vec3 eye;uniform mat4 viewProjection;uniform sampler2D sceneColor;diff --git a/src/shaders.js b/src/shaders.jsindex b7bf4d74..96698b30 100644--- a/src/shaders.js+++ b/src/shaders.js@@ -79,10 +79,8 @@ out vec2 textureUV;out vec2 lightmapUV;out vec3 vertexTint;out float vertexSkyVisibility;-flat out vec3 emissionColor;-flat out float instanceOrdinal;-out vec3 boxPosition;-out vec3 boxNormal;+flat out vec4 instanceEmission;+out vec3 boxPosition, boxNormal;flat out mat3 boxNormalMatrix;${skinningShader(features)}${morphingShader(features)}@@ -104,17 +102,18 @@ void main() {vec3 localNormal = USES_MORPH ? morphedNormal(normal) : normal;vec3 deformedNormal = (USES_SKIN || USES_CROWD) ? transpose(inverse(mat3(deformation))) * localNormal : localNormal;worldNormal = normalize(instanceNormal * deformedNormal);- // Metres along the object's own axes, so a pattern follows a moving node and is not stretched by its scale.+ // Metres along the object's own axes: a pattern follows its node and ignores scale.+ if (${new Set(features).has('boxMapScale')}) {boxPosition = (USES_MORPH ? morphedPosition(position) : position) * vec3(length(instanceMatrix[0].xyz), length(instanceMatrix[1].xyz), length(instanceMatrix[2].xyz));boxNormal = deformedNormal;boxNormalMatrix = instanceNormal;+ }baseColor = instanceColor;textureUV = uv;lightmapUV = uvSets.zw;vertexTint = USES_CROWD ? vec3(1.0) : vertexColor.rgb;vertexSkyVisibility = USES_CROWD ? 1.0 : vertexColor.a;- emissionColor = instanceEmissive;- instanceOrdinal = float(gl_InstanceID);+ instanceEmission = vec4(instanceEmissive, gl_InstanceID);surface = instanceSurface;shadowPosition = lightProjection * world;gl_Position = viewProjection * world;@@ -205,8 +204,7 @@ in vec2 textureUV;in vec2 lightmapUV;in vec3 vertexTint;in float vertexSkyVisibility;-flat in vec3 emissionColor;-flat in float instanceOrdinal;+flat in vec4 instanceEmission;uniform sampler2D lightMap;uniform float lightMapScale;uniform float hasLightMap;@@ -262,8 +260,7 @@ uniform float hasOcclusionMap;uniform float hasEmissiveMap;uniform vec2 normalScale;uniform float occlusionStrength;-in vec3 boxPosition;-in vec3 boxNormal;+in vec3 boxPosition, boxNormal;flat in mat3 boxNormalMatrix;uniform float boxMapScale;uniform int boxMapAxis;@@ -520,7 +517,7 @@ vec3 renderPassValue(vec3 color, vec3 normal, vec3 emission) {float high = floor(steps / 256.0);return vec3(high, steps - high * 256.0, 0.0) / 255.0;}- float identifier = renderPassObjectBase + instanceOrdinal + 1.0;+ float identifier = renderPassObjectBase + instanceEmission.w + 1.0;float high = floor(identifier / 65536.0);float middle = floor((identifier - high * 65536.0) / 256.0);return vec3(identifier - high * 65536.0 - middle * 256.0, middle, high) / 255.0;@@ -544,7 +541,7 @@ void main() {textureAlpha = sampledBase.a;}float alpha = surfaceAlpha(textureAlpha);- vec3 emission = emissionColor * surface.z;+ vec3 emission = instanceEmission.rgb * surface.z;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);