3D engine that AI agents drive
Carry vertex colours and per-instance values as varyings only when a draw needs them (part of #388)
Nguyen Xuan Anda4b36088d74
diff --git a/src/renderer.js b/src/renderer.jsindex 4cdeebb3..3c9670ee 100644--- a/src/renderer.js+++ b/src/renderer.js@@ -93,6 +93,8 @@ function shaderFeaturesOfParts(parts) {if (materialLayerValue(material, 'clearcoat') > 0) features.add('clearcoat');if (materialLayerValue(material, 'sheenColor').some(channel => channel !== 0)) features.add('sheen');if (TAG_SHADER_FEATURES.has(material.tag)) features.add(material.tag);+ // Without vertex colours the tint and sky visibility are the constants of the unbound attribute, so no varying carries them.+ if ([part.geometry, ...(part.lods ?? []).map(level => level.geometry)].some(geometry => geometry.colors || geometry.skyVisibility)) features.add('vertexColors');}return features;}@@ -622,6 +624,7 @@ export class Renderer67 {this.shadowBiasPerSlope = 0;this.inverseViewProjection = mat4.create();this.shaderFeatures = shaderFeaturesOfParts(groups.flat());+ if (groups.some(group => group.length > 1)) this.shaderFeatures.add('instanced');this.shaderFeatures.add(environment.map ? 'environmentMap' : 'studioLighting');shaderFeaturesOfLighting(this.lighting).forEach(feature => this.shaderFeatures.add(feature));if (shading === 'light') this.shaderFeatures.add(LIGHT_SHADING_FEATURE);@@ -728,7 +731,7 @@ export class Renderer67 {finishMainProgram(pendingProgram) {this.program = finishProgram(this.context, pendingProgram);- this.uniforms = Object.fromEntries(['materialDielectric', 'materialCoat', 'materialSheen', 'materialAnisotropy', 'materialIridescence', 'lightLinkMask', 'boxMapScale', 'boxMapAxis', 'lightMap', 'lightMapScale', 'hasLightMap', 'viewProjection', 'lightProjection', 'eye', 'armorColor', 'armorSurface', 'exposure', 'inspection', 'shadowMap', 'shadowBiasPerSlope', ...MAP_UNIFORMS, ...MAP_FLAGS, ...SKIN_UNIFORMS, ...MORPH_UNIFORMS, 'normalScale', 'occlusionStrength', 'materialMoss', ...SPLAT_UNIFORMS, 'hdrEnabled', 'time', 'wind', 'worldMode', 'daylight', 'fogColor', 'lightDirection', 'fogDensity', 'waterRipple', 'rippleTime', 'explosionProgress', 'explosionCenter', 'assemblyAmount', 'renderPassMode', 'renderPassDepthRange', 'renderPassObjectBase', ...ALPHA_UNIFORMS].map((name) => [name, this.context.getUniformLocation(this.program, name)]));+ this.uniforms = Object.fromEntries(['materialDielectric', 'materialCoat', 'materialSheen', 'materialAnisotropy', 'materialIridescence', 'lightLinkMask', 'boxMapScale', 'boxMapAxis', 'lightMap', 'lightMapScale', 'hasLightMap', 'viewProjection', 'lightProjection', 'eye', 'armorColor', 'armorSurface', 'exposure', 'inspection', 'shadowMap', 'shadowBiasPerSlope', ...MAP_UNIFORMS, ...MAP_FLAGS, ...SKIN_UNIFORMS, ...MORPH_UNIFORMS, 'normalScale', 'occlusionStrength', 'materialMoss', ...SPLAT_UNIFORMS, 'hdrEnabled', 'time', 'wind', 'worldMode', 'daylight', 'fogColor', 'lightDirection', 'fogDensity', 'waterRipple', 'rippleTime', 'explosionProgress', 'explosionCenter', 'assemblyAmount', 'renderPassMode', 'renderPassDepthRange', 'renderPassObjectBase', 'baseColor', 'surface', 'instanceEmission', 'boxNormalMatrix', ...ALPHA_UNIFORMS].map((name) => [name, this.context.getUniformLocation(this.program, name)]));for (const name of LIGHT_UNIFORMS) {this.uniforms[name] = this.context.getUniformLocation(this.program, name);}@@ -819,7 +822,7 @@ export class Renderer67 {// Parts can arrive after construction (replaceParts) or change their tag (updateParts). When one needs a shader feature// that the program left out, the program is compiled again with that feature added.requireShaderFeatures(parts) {- this.requireFeatureNames([...shaderFeaturesOfParts(parts)]);+ this.requireFeatureNames([...shaderFeaturesOfParts(parts), ...(parts.length > 1 ? ['instanced'] : [])]);}requireFeatureNames(features) {@@ -2004,6 +2007,15 @@ export class Renderer67 {this.drawBatch(batch, textured, selection);}+ // When every batch holds one instance, its flat values (colour, surface, emission, box normal matrix) are uniforms, not varyings.+ setSingleInstanceUniforms(data) {+ const context = this.context;+ context.uniform3f(this.uniforms.baseColor, data[25], data[26], data[27]);+ context.uniform4f(this.uniforms.surface, data[28], data[29], data[30], data[31]);+ context.uniform4f(this.uniforms.instanceEmission, data[38], data[39], data[40], 0);+ if (this.shaderFeatures.has('boxMapScale')) context.uniformMatrix3fv(this.uniforms.boxNormalMatrix, false, data.subarray(16, 25));+ }+drawBatch(batch, textured = false, selection = null, levelSet = batch) {if (this.hasMorphs) {const uniforms = textured ? this.uniforms : this.depthMorphUniforms;@@ -2063,6 +2075,7 @@ export class Renderer67 {this.context.uniform1f(this.uniforms.lightMapScale, batch.mapState[15]);}}+ if (textured && !this.benchmark && !this.shaderFeatures.has('instanced')) this.setSingleInstanceUniforms(batch.instanceData);if (selection && !selection.isEverything) {this.drawSelectedLevels(batch, selection, levelSet);return;diff --git a/src/shaders.js b/src/shaders.jsindex a2b5ca31..f4c92a82 100644--- a/src/shaders.js+++ b/src/shaders.js@@ -71,15 +71,19 @@ uniform float explosionProgress;uniform vec3 explosionCenter;uniform float assemblyAmount;out vec3 worldPosition, worldNormal;+#ifdef INSTANCEDflat out vec3 baseColor;flat out vec4 surface;-out vec4 shadowPosition;-out vec2 textureUV, lightmapUV;+flat out vec4 instanceEmission;+flat out mat3 boxNormalMatrix;+#endif+#ifdef VERTEX_COLORSout vec3 vertexTint;out float vertexSkyVisibility;-flat out vec4 instanceEmission;+#endif+out vec4 shadowPosition;+out vec2 textureUV, lightmapUV;out vec3 boxPosition, boxNormal;-flat out mat3 boxNormalMatrix;${skinningShader(features)}${morphingShader(features)}${windShader(features)}@@ -104,15 +108,21 @@ void main() {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;+#ifdef INSTANCEDboxNormalMatrix = instanceNormal;+#endif}- baseColor = instanceColor;textureUV = uv;lightmapUV = uvSets.zw;- vertexTint = USES_CROWD ? vec3(1.0) : vertexColor.rgb;- vertexSkyVisibility = USES_CROWD ? 1.0 : vertexColor.a;+#ifdef INSTANCED+ baseColor = instanceColor;instanceEmission = vec4(instanceEmissive, gl_InstanceID);surface = instanceSurface;+#endif+#ifdef VERTEX_COLORS+ vertexTint = USES_CROWD ? vec3(1.0) : vertexColor.rgb;+ vertexSkyVisibility = USES_CROWD ? 1.0 : vertexColor.a;+#endifshadowPosition = lightProjection * world;gl_Position = viewProjection * world;}`;@@ -199,13 +209,26 @@ function buildFragmentShader(featureDefines, featureCode) {${featureDefines}precision highp float;in vec3 worldPosition, worldNormal;+#ifdef INSTANCEDflat in vec3 baseColor;flat in vec4 surface;-in vec4 shadowPosition;-in vec2 textureUV, lightmapUV;+flat in vec4 instanceEmission;+flat in mat3 boxNormalMatrix;+#else+uniform vec3 baseColor;+uniform vec4 surface;+uniform vec4 instanceEmission;+uniform mat3 boxNormalMatrix;+#endif+#ifdef VERTEX_COLORSin vec3 vertexTint;in float vertexSkyVisibility;-flat in vec4 instanceEmission;+#else+const vec3 vertexTint = vec3(1.0);+const float vertexSkyVisibility = 1.0;+#endif+in vec4 shadowPosition;+in vec2 textureUV, lightmapUV;uniform sampler2D lightMap;uniform float lightMapScale;uniform float hasLightMap;@@ -262,7 +285,6 @@ uniform float hasEmissiveMap;uniform vec2 normalScale;uniform float occlusionStrength;in vec3 boxPosition, boxNormal;-flat in mat3 boxNormalMatrix;uniform float boxMapScale;uniform int boxMapAxis;// Dielectric reflectance at normal incidence, clear coat (strength, roughness) and sheen (colour, roughness)@@ -729,7 +751,7 @@ export const LIGHT_SHADING_MAXIMUM_SAMPLES = 2;export const ENVIRONMENT_FEATURES =Object.freeze(['environmentMap', 'studioLighting']);// Features that only the vertex shader reads: a part with a skeleton or morph targets.-const VERTEX_ONLY_FEATURES = Object.freeze(['skin', 'morph', 'crowd']);+const VERTEX_ONLY_FEATURES = Object.freeze(['skin', 'morph', 'crowd', 'vertexColors', 'instanced']);function environmentCondition(enabled) {const hasMap = enabled.has('environmentMap');@@ -795,6 +817,11 @@ export const SHADER_FEATURES = Object.freeze([...Object.keys(SHADER_FEATURE_COND// The vertex shader pays on every vertex for skinning, morphing, wind, waves and the explosion, and SwiftShader runs a// choice between them whether or not the uniform picks it, so a renderer without those parts compiles them out.+// The varyings that both shaders carry: per-vertex colours, and the per-instance values that a draw of one instance sets as uniforms.+function interpolantDefines(enabled) {+ return [['vertexColors', 'VERTEX_COLORS'], ['instanced', 'INSTANCED']].filter(([feature]) => enabled.has(feature)).map(([, macro]) => `#define ${macro}`);+}+function vertexDefinitions(enabled) {const condition = (active, expression) => (active ? `(${expression})` : 'false');return [@@ -804,6 +831,7 @@ function vertexDefinitions(enabled) {`#define USES_WIND ${condition(enabled.has('foliage') || enabled.has('grass'), 'true')}`,`#define USES_WAVES ${condition(enabled.has('water'), 'true')}`,`#define USES_EXPLOSION ${condition(enabled.has('fire') || enabled.has('smoke'), 'true')}`,+ ...interpolantDefines(enabled),].join('\n');}@@ -819,7 +847,7 @@ export const vertexShader = createVertexShader(SHADER_FEATURES.filter(feature =>export function createFragmentShader(features = SHADER_FEATURES) {const enabled = new Set(features);const lines = Object.entries(SHADER_FEATURE_CONDITIONS).map(([feature, [macro, condition]]) => `#define ${macro} ${enabled.has(feature) ? `(${condition})` : 'false'}`);- lines.push('#define CANOPY_TAG (TAG_FOLIAGE || TAG_GRASS)');+ lines.push('#define CANOPY_TAG (TAG_FOLIAGE || TAG_GRASS)', ...interpolantDefines(enabled));lines.push(`#define ENVIRONMENT_ENABLED ${environmentCondition(enabled)}`);// Not in SHADER_FEATURES: the preset is chosen by the renderer, so a shader built from every feature keeps the full look.lines.push(`#define LIGHT_SHADING ${enabled.has(LIGHT_SHADING_FEATURE) ? 'true' : 'false'}`);