3D engine that AI agents drive
JavaScript 3d3d-engineagentic-aiai-agentsblenderclaude-codegame-enginegaussian-splattinggltfjavascriptmcpmcp-servermodel-context-protocolpath-tracerpath-tracingpbrrenderingthreejs-alternativewebgl2webgpu
Make bloom strength 0 mean bloom off through one check (#2025)
Nguyen Xuan An171bfc27d86c
diff --git a/changes/2025.md b/changes/2025.mdnew file mode 100644index 00000000..fe4f6bf2--- /dev/null+++ b/changes/2025.md@@ -0,0 +1 @@+- effects.bloom.strength 0 now means bloom off through one check, `isBloomActive`, that the renderer asks everywhere (targets, pass, composite, shader step); a strength 0 frame is byte for byte the frame without bloom (#2025)diff --git a/changes/2025.size.md b/changes/2025.size.mdnew file mode 100644index 00000000..726b0a8f--- /dev/null+++ b/changes/2025.size.md@@ -0,0 +1,2 @@+- goal:gltf-box: 16 bytes for the single isBloomActive check that replaces six scattered strength tests (#2025)+- goal:agent-scene: 17 bytes, the same single isBloomActive check (#2025)diff --git a/src/effects.js b/src/effects.jsindex 8fb47498..5080eee9 100644--- a/src/effects.js+++ b/src/effects.js@@ -13,6 +13,11 @@ function numberInRange(value, fallback, minimum, maximum, name) {return result;}+/** True when effects ask for a bloom pass. Strength 0 means bloom off: no extraction, blur or composite pass runs for it (#2025). The renderer asks only this. */+export function isBloomActive(effects) {+ return effects?.bloom?.strength > 0;+}+export function resolveEffects(effects = {}) {if (!effects || typeof effects !== 'object' || Array.isArray(effects)) throw new Error('Effects must be an object.');const bloom = effects.bloom ?? {};diff --git a/src/renderer.js b/src/renderer.jsindex 2a3cdec3..08ec2972 100644--- a/src/renderer.js+++ b/src/renderer.js@@ -10,7 +10,7 @@ import { planRenderSize, estimateTextureBytes, estimateFrameBytes, memoryBudgetEimport { applyProjection } from './camera.js';import { fittedNearPlane, sceneBoundsOfParts } from './near-plane-fit.js';import { installReversedDepth, prepareCameraRelativeProgram } from './reversed-depth.js';-import { resolveEffects } from './effects.js';+import { isBloomActive, resolveEffects } from './effects.js';import { frameDiagnostics } from './frame-diagnostics.js';import { depthLayerOf, flushOrderOf, RANKS_PER_LAYER } from './depth-layer.js';import { createLightingState, updateLightingState } from './lighting.js';@@ -989,7 +989,7 @@ export class CrucibleRenderer {await this.programsReady();if (this.disposed) return false;// The bloom targets take the driver tens of milliseconds under load: a frame that blooms allocates them in a task of its own, not inside the draw.- if (!this.benchmark && !this.bloomTargets.length && resolveEffects(frame.effects).bloom.strength > 0) {+ if (!this.benchmark && !this.bloomTargets.length && isBloomActive(resolveEffects(frame.effects))) {await new Promise(resolve => setTimeout(resolve, 0));if (this.disposed) return false;this.ensureBloomTargets();@@ -2308,7 +2308,7 @@ export class CrucibleRenderer {const steps = [];if (this.transparent) steps.push('transparent');if (occluded) steps.push('occlusion');- if (effects.bloom.strength > 0) steps.push('bloom');+ if (isBloomActive(effects)) steps.push('bloom');if (volume.density > 0) steps.push('volume');if (volume.density > 0 && volume.field) steps.push('field');if (volume.density > 0 && requireFeatureCode('volumeFog').support.usesDetail(volume)) steps.push('detail');@@ -2368,9 +2368,9 @@ export class CrucibleRenderer {// A grid with velocity blurs over the shutter of the scene's motion blur, or over environment.volumeShutter when the caller sets one (#1759).const localVolumes = this.prepareLocalVolumes(environment.localVolumes, frame, environment.volumeTime ?? frame.time ?? 0, environment.volumeShutter ?? effects.motionBlur.shutter);// A glowing volume is drawn before bloom extraction so it blooms; the final pass then skips its local step.- const compositedLocalVolumes = localVolumes && effects.bloom.strength > 0 && this.localVolumeBuffers.emissive;+ const compositedLocalVolumes = localVolumes && isBloomActive(effects) && this.localVolumeBuffers.emissive;if (compositedLocalVolumes) sourceTexture = requireFeatureCode('volumeFog').support.compositeLocalVolumes(this, frame, environment, sourceTexture);- if (effects.bloom.strength > 0) this.drawBloom(effects.bloom, sourceTexture);+ if (isBloomActive(effects)) this.drawBloom(effects.bloom, sourceTexture);const occluded = effects.ambientOcclusion > 0 && Boolean(this.depthTexture);if (occluded) this.drawAmbientOcclusion(frame, effects);// A frame that names an outputFramebuffer (src/float-output.js) draws its final pass there, not on the canvas.@@ -2404,9 +2404,9 @@ export class CrucibleRenderer {fogSupport?.bindUniforms(this, post, volume, environment, frame);if (localVolumes && !compositedLocalVolumes) requireFeatureCode('volumeFog').support.bindLocalVolumes(this, post, frame, environment);context.activeTexture(context.TEXTURE2);- context.bindTexture(context.TEXTURE_2D, effects.bloom.strength > 0 ? this.bloomTargets[1].texture : this.whiteTexture);+ context.bindTexture(context.TEXTURE_2D, isBloomActive(effects) ? this.bloomTargets[1].texture : this.whiteTexture);context.uniform1i(post.uniforms.bloomTexture, 2);- context.uniform1f(post.uniforms.bloomStrength, effects.bloom.strength);+ context.uniform1f(post.uniforms.bloomStrength, isBloomActive(effects) ? effects.bloom.strength : 0);context.activeTexture(context.TEXTURE4);context.bindTexture(context.TEXTURE_2D, occluded ? this.occlusionTarget.texture : this.whiteTexture);context.uniform1i(post.uniforms.occlusionTexture, 4);diff --git a/tests/bloom-zero.test.js b/tests/bloom-zero.test.jsnew file mode 100644index 00000000..6290cc89--- /dev/null+++ b/tests/bloom-zero.test.js@@ -0,0 +1,94 @@+import test, { after } from 'node:test';+import assert from 'node:assert/strict';+import { mkdtemp, readFile, rm } from 'node:fs/promises';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import { setTimeout as delay } from 'node:timers/promises';+import { Client } from '@modelcontextprotocol/client';+import { InMemoryTransport } from '@modelcontextprotocol/server';+import { createAgentRuntime } from '../scripts/agent-runtime.mjs';+import { createAgentMcpServer } from '../scripts/mcp-server.mjs';+import { isBloomActive, resolveEffects } from '../src/effects.js';+import { CrucibleRenderer } from '../src/renderer.js';+import { decodePngPixels } from './support/png-pixels.mjs';++// Bloom strength 0 means bloom off (#2025): no pass, no targets, no shader step, the same pixels as a scene without bloom.++test('isBloomActive is true only for a strength above 0', () => {+ assert.equal(isBloomActive(resolveEffects({ bloom: { strength: 0 } })), false);+ assert.equal(isBloomActive(resolveEffects({})), false);+ assert.equal(isBloomActive({ bloom: { strength: -0 } }), false);+ assert.equal(isBloomActive({ bloom: { strength: Number.NaN } }), false);+ assert.equal(isBloomActive({}), false);+ assert.equal(isBloomActive(resolveEffects({ bloom: { strength: 0.01 } })), true);+});++test('the post steps of a strength 0 frame hold no bloom step, those of a positive strength do', () => {+ const stepsOf = strength => CrucibleRenderer.prototype.postStepsOf.call({ transparent: false }, {}, resolveEffects({ bloom: { strength } }), { density: 0 }, false, null, false);+ assert.ok(!stepsOf(0).includes('bloom'));+ assert.ok(stepsOf(0.3).includes('bloom'));+});++test('prepareFrame allocates no bloom targets for strength 0 even with other effects set', async () => {+ const calls = [];+ const renderer = { benchmark: false, disposed: false, bloomTargets: [], context: {}, programsReady: async () => {}, ensureBloomTargets: () => calls.push('ensureBloomTargets'), render: () => {} };+ const frame = { effects: { exposure: 1.2, vignette: 0.3, bloom: { strength: 0, threshold: 0.4, radius: 2 } } };+ await CrucibleRenderer.prototype.prepareFrame.call(renderer, frame);+ assert.deepEqual(calls, []);+});++const SIDE = 64;+const directories = [];+const sessions = new Map();+async function server() {+ if (sessions.has('server')) return sessions.get('server');+ const directory = await mkdtemp(join(tmpdir(), 'crucible-bloom-zero-'));+ directories.push(directory);+ const runtime = createAgentRuntime({ backend: 'cpu', memory: 192, jobs: join(directory, 'jobs') });+ const mcp = createAgentMcpServer(runtime);+ const client = new Client({ name: 'bloom-zero-test', version: '1' });+ const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();+ await mcp.connect(serverTransport);+ await client.connect(clientTransport);+ const entry = { client, close: async () => { await client.close(); await mcp.close(); await runtime.close(); } };+ sessions.set('server', entry);+ return entry;+}+after(async () => {+ if (sessions.has('server')) await sessions.get('server').close();+ for (const directory of directories) await rm(directory, { recursive: true, force: true });+});++async function call(client, name, argumentsValue) {+ const result = await client.callTool({ name, arguments: argumentsValue }, { timeout: 120000 });+ assert.equal(result.isError, undefined, result.content?.find(item => item.type === 'text')?.text);+ return result.structuredContent;+}++async function renderBytes(effects) {+ const { client } = await server();+ const recipe = {+ format: 'crucible.scene', schemaVersion: 1, geometries: [], materials: [], nodes: [],+ camera: { eye: [0, 1, 6], target: [0, 1, 0], fov: 40 },+ environment: { localVolumes: [{ shape: 'sphere', center: [0, 1, 0], radius: 1, density: 4, emission: [1, 0.6, 0.2], emissionStrength: 30 }] },+ effects,+ };+ const created = await call(client, 'scene_create', { recipe });+ const submitted = await call(client, 'scene_render_submit', { sessionId: created.sessionId, expectedRevision: created.revision ?? 0, settings: { kind: 'image', width: SIDE, height: SIDE } });+ for (let attempt = 0; attempt < 900; attempt++) {+ const record = await call(client, 'scene_render_status', { jobId: submitted.jobId });+ if (record.state === 'completed') break;+ assert.ok(!['cancelled', 'failed'].includes(record.state), JSON.stringify(record));+ await delay(100);+ }+ const result = await call(client, 'scene_render_result', { jobId: submitted.jobId });+ return decodePngPixels(await readFile(result.artifacts[0].path));+}++test('with bloom strength 0 the threshold and radius change no pixel, and a positive strength does', { timeout: 240000 }, async () => {+ const others = { exposure: 1.2, vignette: 0.3 };+ const plain = await renderBytes({ ...others, bloom: { strength: 0 } });+ assert.deepEqual(await renderBytes({ ...others, bloom: { strength: 0, threshold: 0.1, radius: 4 } }), plain);+ const bloomed = await renderBytes({ ...others, bloom: { strength: 0.5, threshold: 0.1, radius: 4 } });+ assert.notDeepEqual(bloomed, plain, 'the test scene must glow when bloom is on, else the check above proves nothing');+});