3D engine that AI agents drive
Compile skinning, morphing, wind, waves and explosion out of the vertex shader when no part uses them; skip the colour clear under an environment background (Part of #74)
Nguyen Xuan An75552a9ec14b
diff --git a/src/renderer.js b/src/renderer.jsindex ddce0e89..089d3e6f 100644--- a/src/renderer.js+++ b/src/renderer.js@@ -1,5 +1,5 @@import { mat4, mat3 } from 'gl-matrix';-import { layerTextureSupport, vertexShader, createFragmentShader, liquidFragmentShader, depthVertexShader, depthFragmentShader, benchmarkVertexShader, benchmarkFragmentShader } from './shaders.js';+import { layerTextureSupport, vertexShader, createVertexShader, createFragmentShader, liquidFragmentShader, depthVertexShader, depthFragmentShader, benchmarkVertexShader, benchmarkFragmentShader } from './shaders.js';import { postFragment, createPostFragment, POST_STEPS, skyFragment, bloomFragment } from './post.js';import { fullscreenVertex, startProgram, finishProgram, compileProgram } from './gl-program.js';import { ambientOcclusionFragment } from './ambient-occlusion.js';@@ -56,7 +56,9 @@ function shaderFeaturesOfLighting(lighting) {function shaderFeaturesOfParts(parts) {const features = new Set();- for (const { material } of parts) {+ for (const { material, skin, morph } of parts) {+ if (skin) features.add('skin');+ if (morph) features.add('morph');MATERIAL_MAPS.forEach((field, index) => {if (material[field]) features.add(MAP_SHADER_FEATURES[index]);});@@ -557,7 +559,7 @@ export class Renderer67 {startMainProgram() {const benchmark = this.benchmark;const fragmentSource = benchmark ? benchmarkFragmentShader : createFragmentShader(this.shaderFeatures);- return startProgram(this.context, benchmark ? benchmarkVertexShader : vertexShader, fragmentSource);+ return startProgram(this.context, benchmark ? benchmarkVertexShader : createVertexShader(this.shaderFeatures), fragmentSource);}buildMainProgram() {@@ -1922,7 +1924,9 @@ export class Renderer67 {} else {context.clearColor(0.145, 0.162, 0.177, 1);}- context.clear(context.COLOR_BUFFER_BIT | context.DEPTH_BUFFER_BIT);+ // The environment background covers every pixel, so the colour clear before it would be overwritten.+ const backgroundCoversFrame = !passMode && Boolean(environmentMap?.showBackground) && !this.transparent;+ context.clear(backgroundCoversFrame ? context.DEPTH_BUFFER_BIT : context.COLOR_BUFFER_BIT | context.DEPTH_BUFFER_BIT);const environmentBackgroundDrawn = !passMode && this.drawEnvironmentBackground(environmentMap, frame, exposure);if (this.world && !this.benchmark && !passMode && !environmentBackgroundDrawn) this.drawSky(frame);context.enable(context.DEPTH_TEST);diff --git a/src/shaders.js b/src/shaders.jsindex 33827906..604050a0 100644--- a/src/shaders.js+++ b/src/shaders.js@@ -89,7 +89,9 @@ vec3 windOffset(vec3 localPosition, vec3 worldPoint, vec2 leafUv, float surfaceT}`;-export const vertexShader = `#version 300 es+function vertexShaderSource(definitions) {+ return `#version 300 es+${definitions}precision highp float;layout(location=0) in vec3 position;layout(location=1) in vec3 normal;@@ -127,22 +129,22 @@ ${morphingShader}${windShader}void main() {vec2 uv = uvSets.xy;- mat4 deformation = skinTransform();- vec4 world = instanceMatrix * deformation * vec4(morphedPosition(position), 1.0);+ mat4 deformation = USES_SKIN ? skinTransform() : mat4(1.0);+ vec4 world = instanceMatrix * deformation * vec4(USES_MORPH ? morphedPosition(position) : position, 1.0);world.xyz += assemblyOffset * assemblyAmount + assemblyDetailOffset * smoothstep(0.3, 1.0, assemblyAmount);- world.xyz += windOffset(position, world.xyz, uv, instanceSurface.w, time, wind);- if (instanceSurface.w == 3.0) {+ if (USES_WIND) world.xyz += windOffset(position, world.xyz, uv, instanceSurface.w, time, wind);+ if (USES_WAVES && instanceSurface.w == 3.0) {world.y += sin(world.x * 1.4 + time * 0.8) * 0.025 + sin(world.z * 1.8 - time) * 0.02;}- if (instanceSurface.w == 8.0 || instanceSurface.w == 9.0) {+ if (USES_EXPLOSION && (instanceSurface.w == 8.0 || instanceSurface.w == 9.0)) {world.xyz = mix(explosionCenter, world.xyz, 0.03 + explosionProgress * 0.97);}worldPosition = world.xyz;- vec3 localNormal = morphedNormal(normal);- vec3 deformedNormal = skinEnabled > 0.5 ? transpose(inverse(mat3(deformation))) * localNormal : localNormal;+ vec3 localNormal = USES_MORPH ? morphedNormal(normal) : normal;+ vec3 deformedNormal = USES_SKIN ? 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.- boxPosition = morphedPosition(position) * vec3(length(instanceMatrix[0].xyz), length(instanceMatrix[1].xyz), length(instanceMatrix[2].xyz));+ 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;@@ -155,6 +157,7 @@ void main() {shadowPosition = lightProjection * world;gl_Position = viewProjection * world;}`;+}export const depthVertexShader = `#version 300 esprecision highp float;@@ -964,6 +967,9 @@ const SHADER_FEATURE_CONDITIONS = {// compiles the other out: ENVIRONMENT_ENABLED is true, false, or the uniform when both are listed.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']);+function environmentCondition(enabled) {const hasMap = enabled.has('environmentMap');const hasStudio = enabled.has('studioLighting');@@ -996,11 +1002,31 @@ function requireFeatureCode(feature) {}function featureCodeSplicer(features) {- const codes = features.filter(feature => !(feature in SHADER_FEATURE_CONDITIONS) && !ENVIRONMENT_FEATURES.includes(feature)).map(requireFeatureCode);+ const codes = features.filter(feature => !(feature in SHADER_FEATURE_CONDITIONS) && !ENVIRONMENT_FEATURES.includes(feature) && !VERTEX_ONLY_FEATURES.includes(feature)).map(requireFeatureCode);return slot => codes.map(code => code[slot] ?? '').join('');}-export const SHADER_FEATURES = Object.freeze([...Object.keys(SHADER_FEATURE_CONDITIONS), ...ENVIRONMENT_FEATURES]);+export const SHADER_FEATURES = Object.freeze([...Object.keys(SHADER_FEATURE_CONDITIONS), ...ENVIRONMENT_FEATURES, ...VERTEX_ONLY_FEATURES]);++// 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.+function vertexDefinitions(enabled) {+ const condition = (active, expression) => (active ? `(${expression})` : 'false');+ return [+ `#define USES_SKIN ${condition(enabled.has('skin'), 'skinEnabled > 0.5')}`,+ `#define USES_MORPH ${condition(enabled.has('morph'), 'morphEnabled > 0.5')}`,+ `#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')}`,+ ].join('\n');+}++/** The vertex shader for a set of features, listed as for createFragmentShader. */+export function createVertexShader(features = SHADER_FEATURES) {+ return vertexShaderSource(vertexDefinitions(new Set(features)));+}++export const vertexShader = createVertexShader();/** The fragment shader for a set of features. A feature with separate code throws until its module is imported. */export function createFragmentShader(features = SHADER_FEATURES) {