3D engine that AI agents drive
Move the wind heading out of the core renderer into the on-demand world material module
Nguyen Xuan An066f441abfea
diff --git a/scripts/agent-preview-browser.js b/scripts/agent-preview-browser.jsindex 7a747bc8..4eeb8aa4 100644--- a/scripts/agent-preview-browser.js+++ b/scripts/agent-preview-browser.js@@ -9,7 +9,7 @@ import { installCoplanarDiagnostics } from '../src/coplanar-overlap.js';import { applyProjection } from '../src/camera.js';import { mat4 } from 'gl-matrix';import { resolveAgentShadow, agentEnvironmentMap } from '../src/agent-look.js';-import { agentSceneModule } from '../src/agent-scene-modules.js';+import { agentSceneModule, agentSceneModuleLoaded } from '../src/agent-scene-modules.js';import { agentGroundPlacement, createAgentGround, placeAgentGround, FLOOR_COLOR } from '../src/agent-ground.js';import { groundBounceEnvironment } from '../src/ground-bounce.js';import { createAgentAssetScene } from '../src/agent-asset.js';@@ -173,6 +173,7 @@ function draw(record, time = 0, camera = record.scene.camera, frameOptions = {})record.shadowKey = shadowKey;}record.renderer.world = environment.world;+ applyRecipeWindHeading(record.renderer, environment);record.renderer.render({ ...camera, environment: litEnvironment(record.scene.parts, environment, Boolean(record.renderer.groundPart)), effects, exposure: effects.exposure, time, lines: environment.lines, ...windFrame(environment), ...frameOptions });const error = record.renderer.context.getError();if (error !== 0) {@@ -233,10 +234,16 @@ function cascadeCountFor(scene) {return agentSceneModule('cascades').effectiveShadowCascades(scene.environment, scene.shadow);}-// Wind strength and heading (degrees, 0 toward +x, turning toward +z) of the recipe; absent keeps the renderer default.+// Wind strength of the recipe; absent keeps the renderer default. The heading is applied by applyRecipeWindHeading.function windFrame(environment) {if (!environment.wind) return {};- return { wind: environment.wind.strength, windHeading: environment.wind.direction * Math.PI / 180 };+ return { wind: environment.wind.strength };+}++// The heading lives in the on-demand world material code, so the core renderer carries nothing for it.+function applyRecipeWindHeading(renderer, environment) {+ if (!agentSceneModuleLoaded('worldMaterials')) return;+ agentSceneModule('worldMaterials').applyWindHeading(renderer, (environment.wind?.direction ?? 0) * Math.PI / 180);}function cascadesFor(scene) {diff --git a/src/agent-scene-modules.js b/src/agent-scene-modules.jsindex 10e9d276..233a6176 100644--- a/src/agent-scene-modules.js+++ b/src/agent-scene-modules.js@@ -52,7 +52,7 @@ const MODULE_MARKERS = {timeline: ['"timeline":{'],layerShaders: ['"anisotropy"', '"iridescence"'],liquidShader: ['"transmission"', '"liquid"', '"foam"'],- worldMaterials: ['"tag":"water"', '"tag":"foliage"', '"tag":"grass"', '"tag":"terrain"', '"tag":"fire"', '"tag":"smoke"', '"moss"'],+ worldMaterials: ['"tag":"water"', '"vegetation":{', '"tag":"foliage"', '"tag":"grass"', '"tag":"terrain"', '"tag":"fire"', '"tag":"smoke"', '"moss"'],terrain: ['"type":"terrain"'],// No recipe text asks for it, only the path option of scene_inspect does, so a host loads it with loadAllAgentSceneModules.navigation: [],diff --git a/src/renderer.js b/src/renderer.jsindex 4c03c4c5..1e6f304b 100644--- a/src/renderer.js+++ b/src/renderer.js@@ -527,7 +527,7 @@ export class Renderer67 {this.splatResources = new Map();this.textureBytes = 4;this.shadowsDirty = true;- this.windState = { time: 0, strength: 0.3, heading: 0 };+ this.windState = { time: 0, strength: 0.3 };// The shadow map the constructor draws uses no assembly, so a first frame with none does not draw it again.this.assemblyAmount = 0;const groups = groupParts(this.groundPart ? [...parts, this.groundPart] : parts, batching);@@ -632,7 +632,7 @@ export class Renderer67 {this.depthAlphaUniforms = Object.fromEntries(DEPTH_ALPHA_UNIFORMS.map(name => [name, this.context.getUniformLocation(this.depthProgram, name)]));this.depthUniform = this.context.getUniformLocation(this.depthProgram, 'lightProjection');this.depthAssemblyUniform = this.context.getUniformLocation(this.depthProgram, 'assemblyAmount');- this.depthWindUniforms = { time: this.context.getUniformLocation(this.depthProgram, 'time'), wind: this.context.getUniformLocation(this.depthProgram, 'wind'), heading: this.context.getUniformLocation(this.depthProgram, 'windHeading') };+ this.depthWindUniforms = { time: this.context.getUniformLocation(this.depthProgram, 'time'), wind: this.context.getUniformLocation(this.depthProgram, 'wind') };this.partCount = parts.length;this.triangles = parts.reduce((total, part) => total + part.geometry.indices.length / 3, 0);this.whiteTexture = this.createTexture(null);@@ -707,7 +707,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', 'windHeading', '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', ...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);}@@ -1349,7 +1349,6 @@ export class Renderer67 {context.uniform1f(this.depthAssemblyUniform, this.assemblyAmount ?? 0);context.uniform1f(this.depthWindUniforms.time, this.windState.time);context.uniform1f(this.depthWindUniforms.wind, this.windState.strength);- context.uniform1f(this.depthWindUniforms.heading, this.windState.heading);// The base map sampler must not alias the shadow texture on unit 0 while that texture is the render target.context.uniform1i(this.depthAlphaUniforms.baseMap, 1);context.enable(context.POLYGON_OFFSET_FILL);@@ -1421,10 +1420,10 @@ export class Renderer67 {/** Stores the wind time and strength the shadow pass must use; true when swaying batches need a new shadow map. */updateWindState(frame) {- const next = { time: frame.time ?? 0, strength: frame.wind ?? 0.3, heading: frame.windHeading ?? 0 };+ const next = { time: frame.time ?? 0, strength: frame.wind ?? 0.3 };const previous = this.windState;this.windState = next;- if (previous.time === next.time && previous.strength === next.strength && previous.heading === next.heading) return false;+ if (previous.time === next.time && previous.strength === next.strength) return false;return this.batches.some(batch => batch.castShadow && SWAYING_TAGS.has(batch.parts[0].material.tag));}@@ -2143,7 +2142,6 @@ export class Renderer67 {context.uniform1f(this.uniforms.ambientIntensity, this.lighting.ambientIntensity);context.uniform1f(this.uniforms.time, frame.time ?? 0);context.uniform1f(this.uniforms.wind, frame.wind ?? 0.3);- context.uniform1f(this.uniforms.windHeading, frame.windHeading ?? 0);context.uniform1f(this.uniforms.worldMode, this.world ? 1 : 0);context.uniform1f(this.uniforms.daylight, environment.daylight);context.uniform3fv(this.uniforms.lightDirection, environment.lightDirection);diff --git a/src/world-material-shader.js b/src/world-material-shader.jsindex 557179ae..dfe48c79 100644--- a/src/world-material-shader.js+++ b/src/world-material-shader.js@@ -161,3 +161,20 @@ void mossMasks(vec3 normal, float amount, out float sharpMask, out float softMas};registerShaderFeatureCode('worldMaterials', worldMaterialSlots);++/**+ * Turns the foliage and grass wind to `radians` about the vertical axis (0 is the default heading toward +x).+ * The renderer never sets windHeading, so the value stays on the main and shadow programs between frames; call this+ * before each render, because a rebuilt program starts at 0. A turned heading marks the shadow map stale.+ */+export function applyWindHeading(renderer, radians) {+ const context = renderer.context;+ for (const program of [renderer.program, renderer.depthProgram]) {+ const location = context.getUniformLocation(program, 'windHeading');+ if (!location) continue;+ context.useProgram(program);+ context.uniform1f(location, radians);+ }+ if (renderer.windHeading !== radians) renderer.shadowsDirty = true;+ renderer.windHeading = radians;+}diff --git a/tests/support/preview-harness.mjs b/tests/support/preview-harness.mjsindex 8f3abba3..a6108746 100644--- a/tests/support/preview-harness.mjs+++ b/tests/support/preview-harness.mjs@@ -4,7 +4,7 @@ import { mat4 } from 'gl-matrix';import { createAgentScene, needsMeshBooleans, loadMeshBooleans, meshBooleansLoaded, needsPhysics, loadPhysicsEngine, physicsEngineLoaded, loadAllAgentSceneModules, allAgentSceneModulesLoaded } from '../../src/agent-scene.js';import { applyProjection } from '../../src/camera.js';import { resolveAgentShadow, agentEnvironmentMap } from '../../src/agent-look.js';-import { agentSceneModule } from '../../src/agent-scene-modules.js';+import { agentSceneModule, agentSceneModuleLoaded } from '../../src/agent-scene-modules.js';import { createAgentAssetScene } from '../../src/agent-asset.js';import { parseGltf } from '../../src/gltf.js';import { assetCopyKey, recipeAssetNeeds } from '../../src/agent-scene-assets.js';@@ -77,7 +77,7 @@ export function previewHarness() {parseGltf, Uint8Array, atob, sessionProblem, assetCopyKey, recipeAssetNeeds,applyProjection, mat4, resolveAgentShadow, agentEnvironmentMap, BRANCH_LIMITS,// Line art needs a real renderer with render passes, so the fault-injected one gets a no-op install.- agentSceneModule: name => (name === 'lineArt' ? { installLineArt: renderer => renderer } : agentSceneModule(name)), createBranchStore, printableLabel,+ agentSceneModule: name => (name === 'lineArt' ? { installLineArt: renderer => renderer } : agentSceneModule(name)), agentSceneModuleLoaded, createBranchStore, printableLabel,Renderer67: FaultInjectedRenderer, skyOpenness: () => 1, fogSkyVisibility: () => null,// The fault-injected renderer has no floor part; the automatic floor is covered by agent-ground.test.js and the live smokes.agentGroundPlacement: () => null, createAgentGround: () => null, placeAgentGround: () => {}, FLOOR_COLOR: [0.22, 0.22, 0.21], groundBounceEnvironment: (parts, environment) => environment,