Skip to content

3D engine that AI agents drive

Emit grid fire by Blender's flame remap so the smoke edge glows orange (#1735)

Nguyen Xuan Ana5efa7a92c24
diff --git a/changes/1735.md b/changes/1735.md
new file mode 100644
index 00000000..80232b17
--- /dev/null
+++ b/changes/1735.md
@@ -0,0 +1 @@
+- Grid volumes take `flameTemperature: { ignition, maximum }`, Blender's flame remap (`flame > 0.01 ? ignition + flame * (maximum - ignition) : 0`), so the blackbody comes from the flame channel at 1500 to 3000 K whatever the smoke density, and the add-explosion fireball uses it: the smoke edge glows orange as it does in Cycles. Volumes without the field render as before. The Glaze, CPU, WebGPU and native paths and the fire light share the remap.
diff --git a/changes/1735.size.md b/changes/1735.size.md
new file mode 100644
index 00000000..ad45c1c6
--- /dev/null
+++ b/changes/1735.size.md
@@ -0,0 +1 @@
+- scene:agent-scene+lazy: the flameTemperature field (resolve, validation and the shared remap) is what lets a grid fire glow at its smoke edge like Cycles; 490 bytes gzip.
diff --git a/docs/agent-scene.md b/docs/agent-scene.md
index 9be32ecc..61d50ef9 100644
--- a/docs/agent-scene.md
+++ b/docs/agent-scene.md
@@ -434,7 +434,7 @@ Final resource references and hierarchy must be valid before publication.
What it writes (read it back with `scene_export_recipe`, then tweak any part):
- `effects.flash` and an `effects.distortion` ring at the centre, which fade by their own decay and falloff on the scene clock.
-- One gas-sim grid volume in `environment.localVolumes` for the fireball and the smoke: a baked simulation of a ground blast (`src/gas-sim.js`), lit by the physical blackbody of each voxel's temperature (`blackbodyIntensity`), with `castShadow` on so the smoke shades the floor and `lightsScene` on so the fire lights the set. It is the look of the gas-sim preview (#1735). `size` scales it: the box is 3.2 times `size` on a side, the density and emission shrink with the box so the blast keeps its optical depth, and it plays at 24 frames a second divided by the square root of `size / 2.5`, so a bigger fire rises slower. The grid is 32 cubed and 72 frames (3 s of simulation at the reference size), held on its last frame, baked once per fuel and seed in about 4 s. `oily` bakes more and darker smoke and thickens the volume.
+- One gas-sim grid volume in `environment.localVolumes` for the fireball and the smoke: a baked simulation of a ground blast (`src/gas-sim.js`), lit by the physical blackbody of each voxel's flame, remapped to 1500 to 3000 K as Blender does (`flameTemperature`, `blackbodyIntensity`), so the smoke edge glows orange, with `castShadow` on so the smoke shades the floor and `lightsScene` on so the fire lights the set. It is the look of the gas-sim preview (#1735). `size` scales it: the box is 3.2 times `size` on a side, the density and emission shrink with the box so the blast keeps its optical depth, and it plays at 24 frames a second divided by the square root of `size / 2.5`, so a bigger fire rises slower. The grid is 32 cubed and 72 frames (3 s of simulation at the reference size), held on its last frame, baked once per fuel and seed in about 4 s. `oily` bakes more and darker smoke and thickens the volume.
- The grid files are named `explosion-<fuel>-<seed>-velocity_0000.67vg` to `_0071`, plus `explosion-empty-velocity.67vg` for the wait. With `motionBlur: false` the files carry no velocity and the names drop `-velocity` (`explosion-<fuel>-<seed>_0000.67vg`, `explosion-empty.67vg`); a velocity bake is twice the bytes (about 28 MB for the 72 frames) and bakes in about the same time. The empty file is for the wait before `start` (one empty frame per frame of playback, so `start` is at most about 18 s at size 2.5 and the error says the limit for the size you pass). No such files exist on disk: the MCP server bakes them from the name (about 4 s the first time for a fuel and seed, cached after) and sends them to the preview page with `scene_apply`, so the page spends no time on the bake, and so `scene_export_recipe`, `scene_create` and the render jobs work with the recipe as it is.
- `effects.bloom.strength` raised to at least 0.5 so the glow spills past the flames. The volume's own light replaces the old point light and its track.
- `effects.flash` and an `effects.distortion` ring at the centre, which fade by their own decay and falloff on the scene clock.
diff --git a/docs/volumetric.md b/docs/volumetric.md
index 4a8a2f5a..94973535 100644
--- a/docs/volumetric.md
+++ b/docs/volumetric.md
@@ -223,6 +223,7 @@ A simulated explosion, fire or smoke plume is a grid of numbers, not a noise box
| `absorption` | `[1, 1, 1]` | The colour the smoke absorbs (Blender's Absorption Color, the same field as other volumes) |
| `emission`, `emissionStrength` | `[0, 0, 0]`, `1` | Light given off per unit length times the `flame` channel (the density when the grid has no flame channel) |
| `temperatureScale` | `1` | Kelvin per unit of the `temperature` channel |
+| `flameTemperature` | none | `{ ignition, maximum }` in temperature units (Blender's fire: `{ ignition: 1.5, maximum: 3 }`, 1500 K to 3000 K at `temperatureScale` 1000). The blackbody then comes from the `flame` channel, not the `temperature` channel: `flame > 0.01 ? ignition + flame * (maximum - ignition) : 0`, the remap Blender applies to a fire simulation (`rna_fluid.cc`). The light is added per step length and does not depend on the smoke density, so thin smoke with a little flame glows orange at its edge, as in Cycles. A grid without a `flame` channel gives no blackbody under it. The native path tracer gets the remapped flame as its temperature channel |
| `blackbodyIntensity` | `0` | `0` to `1`. Adds the blackbody light of the temperature, times `emissionStrength`. `0` ignores the temperature channel (Blender's Blackbody Intensity). The light is physical, as in Blender: Stefan-Boltzmann `5.670373e-8 * 1e-6 / pi * kelvin^4` (0.09 at 1500 K, 1.5 at 3000 K) in a colour of luminance 1 whose hue is the Planck curve (the same table as `emissionRamp`). Before 0.21.0 it was a Tanner Helland fit times `(kelvin / 1500)^4`, 3 to 4 times too bright against Cycles, so a scene tuned to that needs a smaller `emissionStrength` or `blackbodyIntensity` |
| `anisotropy` | `0` | `-0.9` to `0.9`. The Henyey-Greenstein asymmetry of the key light's scatter when `scatter` is above 0 |
| `steps` | `64` | `4` to `128` samples through the box |
diff --git a/scripts/explosion-cycles-reference.mjs b/scripts/explosion-cycles-reference.mjs
index 5e799da9..cc4896f3 100644
--- a/scripts/explosion-cycles-reference.mjs
+++ b/scripts/explosion-cycles-reference.mjs
@@ -6,6 +6,7 @@ import { mkdir, writeFile } from 'node:fs/promises';
import { setPriority, constants } from 'node:os';
import { join, resolve } from 'node:path';
import { explosionGridBytes } from '../src/explosion-grids.js';
+import { decodeVolumeGrid, encodeVolumeGrid, floatToHalf, gridTemperature } from '../src/volume-grid.js';
const DEFAULT_BLENDER = 'C:/Program Files/Blender Foundation/Blender 5.3/blender.exe';
const GRID_FIELDS = ['center', 'size', 'density', 'absorption', 'emission', 'emissionStrength', 'temperatureScale', 'blackbodyIntensity'];
@@ -22,12 +23,21 @@ function runBlender(blender, script, scriptArguments) {
});
}
+// With a flame remap (#1735) Cycles gets Blender's own temperature grid: the temperature channel replaced by the remapped flame, which its Principled Volume
+// then reads with Temperature = temperatureScale.
+function gridBytesForCycles(bytes, flameTemperature) {
+ if (!flameTemperature) return bytes;
+ const grid = decodeVolumeGrid(bytes);
+ const remapped = gridTemperature(grid, flameTemperature);
+ return encodeVolumeGrid({ resolution: grid.resolution, size: grid.size, channels: { ...grid.channels, temperature: Uint16Array.from(remapped, floatToHalf) } });
+}
+
/** Renders `frameIndex` of the grid volume with Cycles and returns the path of the PNG. */
export async function renderCyclesReference({ volume, frameIndex, camera, floor, floorColor, folder, width, height, samples, blender = DEFAULT_BLENDER }) {
await mkdir(folder, { recursive: true });
const name = volume.frames[frameIndex];
const gridPath = join(folder, name);
- await writeFile(gridPath, explosionGridBytes(name));
+ await writeFile(gridPath, gridBytesForCycles(explosionGridBytes(name), volume.flameTemperature));
await runBlender(blender, resolve('scripts/grid-to-vdb.py'), [gridPath, folder]);
const volumeFields = Object.fromEntries(GRID_FIELDS.map(field => [field, volume[field]]));
const spec = {
diff --git a/src/agent-explosion.js b/src/agent-explosion.js
index 3291df5c..8401355a 100644
--- a/src/agent-explosion.js
+++ b/src/agent-explosion.js
@@ -180,7 +180,10 @@ function addAirSteps(candidate, plan) {
// background; at 9.5 (on top of the exact step integral, #1894) the edge falloff is 0.79 of the Cycles one and the RMSE drops from 0.030 to 0.020.
// steps 32 is one step per voxel of the 32 cubed grid: against 96 steps the picture differs by an RMSE of 0.003 (linear, 0 to 1), and a CPU preview
// look costs a third (SwiftShader, 768 x 432: first look 11.7 s to 4.9 s, later looks 4.2 s to 1.7 s), which keeps scene_look well under its 30 s limit.
+// flameTemperature is Blender's fire remap (#1735): every voxel with flame emits at 1500 K up to 3000 K by its flame value, whatever its smoke density, which
+// is the orange rim Cycles draws along the smoke edge (the temperature channel alone cools to nothing there).
const BLAST_VOLUME = Object.freeze({
+ flameTemperature: Object.freeze({ ignition: 1.5, maximum: 3 }),
density: 3, emission: [1, 0.45, 0.12], emissionStrength: 9.5, blackbodyIntensity: 0.3, absorption: [1, 1, 1], steps: 32,
});
const OILY_DENSITY_GAIN = 1.5;
@@ -236,6 +239,7 @@ function addBlastVolume(candidate, plan) {
emission: [...BLAST_VOLUME.emission],
emissionStrength: round(BLAST_VOLUME.emissionStrength / scale),
temperatureScale: GAS_SIM_TEMPERATURE_UNIT,
+ flameTemperature: { ...BLAST_VOLUME.flameTemperature },
blackbodyIntensity: BLAST_VOLUME.blackbodyIntensity,
steps: BLAST_VOLUME.steps,
castShadow: true,
diff --git a/src/fog-shader.js b/src/fog-shader.js
index 0caefa2c..34a614e6 100644
--- a/src/fog-shader.js
+++ b/src/fog-shader.js
@@ -242,8 +242,8 @@ function localGridTexture(renderer, grid) {
function localGridEmissionTexture(renderer, volume) {
const grid = volume?.gridData ?? null;
- const baked = grid ? bakedGridEmission(grid, volume.temperatureScale) : null;
- return uploadGridTexture(renderer, 'localGridEmissionCache', grid, volume?.temperatureScale ?? 0, (texels, voxel, texel) => {
+ const baked = grid ? bakedGridEmission(grid, volume.temperatureScale, volume.flameTemperature) : null;
+ return uploadGridTexture(renderer, 'localGridEmissionCache', grid, `${volume?.temperatureScale ?? 0}|${volume?.flameTemperature?.ignition}|${volume?.flameTemperature?.maximum}`, (texels, voxel, texel) => {
texels[texel] = floatToHalf(Math.min(baked.red[voxel], HALF_FLOAT_MAXIMUM));
texels[texel + 1] = floatToHalf(Math.min(baked.green[voxel], HALF_FLOAT_MAXIMUM));
texels[texel + 2] = floatToHalf(Math.min(baked.blue[voxel], HALF_FLOAT_MAXIMUM));
diff --git a/src/local-volume-fire-light.js b/src/local-volume-fire-light.js
index 1da5565f..910f988d 100644
--- a/src/local-volume-fire-light.js
+++ b/src/local-volume-fire-light.js
@@ -1,5 +1,5 @@
import { MAX_POINT_LIGHTS } from './light-limits.js';
-import { volumeGridFloats, blackbodyRadiance } from './volume-grid.js';
+import { volumeGridFloats, gridTemperature, blackbodyRadiance } from './volume-grid.js';
// Fire is light (#1046). A local volume with `lightsScene: true` adds one point light at the centre of its emission, so the ground, the
// props and the smoke around a fireball are lit by it. The light's intensity is the emission integrated over the volume: a medium that
@@ -25,13 +25,13 @@ const totalsByGrid = new WeakMap();
// The summed emission of one grid frame in grid cell units and its luminance-weighted centroid in the unit cube. Decoded once per frame of the sequence.
function gridEmissionSums(gridData, volume) {
- const key = `${volume.emission}|${volume.emissionStrength}|${volume.temperatureScale}|${volume.blackbodyIntensity}`;
+ const key = `${volume.emission}|${volume.emissionStrength}|${volume.temperatureScale}|${volume.blackbodyIntensity}|${volume.flameTemperature?.ignition}|${volume.flameTemperature?.maximum}`;
const cached = totalsByGrid.get(gridData);
if (cached?.key === key) return cached;
const floats = volumeGridFloats(gridData);
const [width, height, depth] = gridData.resolution;
const flame = floats.flame ?? floats.density;
- const temperature = floats.temperature ?? null;
+ const temperature = gridTemperature(gridData, volume.flameTemperature) ?? null;
const total = [0, 0, 0];
const centroidWeight = [0, 0, 0];
const blackbody = [0, 0, 0];
diff --git a/src/local-volume-grid.js b/src/local-volume-grid.js
index aacc32ce..d61668e4 100644
--- a/src/local-volume-grid.js
+++ b/src/local-volume-grid.js
@@ -15,6 +15,21 @@ function resolveFrameNames(frames, label) {
});
}
+// Blender's flame remap (#1735): `flameTemperature: { ignition, maximum }` (temperature units, so 1.5 and 3 are 1500 K and 3000 K at temperatureScale 1000)
+// makes the volume emit from the flame channel as `flame > 0.01 ? ignition + flame * (maximum - ignition) : 0` instead of from the temperature channel.
+function resolveFlameTemperature(input, label) {
+ if (input === undefined) return {};
+ const fieldLabel = `${label}.flameTemperature`;
+ if (input === null || typeof input !== 'object' || Array.isArray(input)) throw new TypeError(`${fieldLabel} must be { ignition, maximum }, such as { ignition: 1.5, maximum: 3 } (Blender's fire defaults); leave it out to emit from the temperature channel`);
+ for (const key of Object.keys(input)) {
+ if (key !== 'ignition' && key !== 'maximum') throw new TypeError(`${fieldLabel}.${key} is not a field; it takes ignition and maximum`);
+ }
+ const ignition = finiteRange(input.ignition, 0, 1e3, `${fieldLabel}.ignition`);
+ const maximum = finiteRange(input.maximum, 0, 1e3, `${fieldLabel}.maximum`);
+ if (maximum < ignition) throw new RangeError(`${fieldLabel}.maximum (${maximum}) must be at least ignition (${ignition}): the flame grows from the ignition temperature up to the maximum`);
+ return { flameTemperature: { ignition, maximum } };
+}
+
// A grid volume is a box (center, size) whose density, temperature and flame come from a registered grid (volume-grid.js). Its size defaults to the
// grid file's own size once the grid is bound. The faded edge and the noise belong to the other shapes: a grid has its own detail.
export function resolveGridVolume(input, resolved, label) {
@@ -45,6 +60,7 @@ export function resolveGridVolume(input, resolved, label) {
...(animated ? { frames: input.frames.slice(), fps: finiteRange(input.fps ?? 24, 0.01, 240, `${label}.fps`), loop: input.loop ?? true } : { grid: input.grid }),
...(input.size === undefined ? {} : { size: vectorField(input.size, 0.001, 1e4, `${label}.size`) }),
temperatureScale: finiteRange(input.temperatureScale ?? 1, 0, 1e5, `${label}.temperatureScale`),
+ ...resolveFlameTemperature(input.flameTemperature, label),
blackbodyIntensity: finiteRange(input.blackbodyIntensity ?? 0, 0, 1, `${label}.blackbodyIntensity`),
anisotropy: finiteRange(input.anisotropy ?? 0, -0.9, 0.9, `${label}.anisotropy`),
};
diff --git a/src/local-volume-resolve.js b/src/local-volume-resolve.js
index 9aaf1745..b3b564a0 100644
--- a/src/local-volume-resolve.js
+++ b/src/local-volume-resolve.js
@@ -8,9 +8,9 @@ import { finiteRange, vectorField } from './volumetric.js';
export const MAX_LOCAL_VOLUMES = 8;
export const LOCAL_VOLUME_SHAPES = Object.freeze(['box', 'sphere', 'mesh', 'grid']);
export const LOCAL_VOLUME_FIT = Object.freeze(['bounds', 'mesh']);
-export const LOCAL_VOLUME_FIELDS = Object.freeze(['shape', 'part', 'fit', 'center', 'size', 'radius', 'density', 'falloff', 'absorption', 'emission', 'emissionStrength', 'scatter', 'blur', 'noiseAmount', 'noiseScale', 'noiseOctaves', 'steps', 'grid', 'frames', 'fps', 'loop', 'temperatureScale', 'blackbodyIntensity', 'anisotropy', 'lightsScene', 'shadowDetail', 'castShadow', 'scatterOctaves', 'emissionRamp']);
+export const LOCAL_VOLUME_FIELDS = Object.freeze(['shape', 'part', 'fit', 'center', 'size', 'radius', 'density', 'falloff', 'absorption', 'emission', 'emissionStrength', 'scatter', 'blur', 'noiseAmount', 'noiseScale', 'noiseOctaves', 'steps', 'grid', 'frames', 'fps', 'loop', 'temperatureScale', 'flameTemperature', 'blackbodyIntensity', 'anisotropy', 'lightsScene', 'shadowDetail', 'castShadow', 'scatterOctaves', 'emissionRamp']);
// The fields only a grid volume takes (#1045), and the steps its denser march may take (the shader's grid loop is bounded by the same number).
-export const LOCAL_VOLUME_GRID_FIELDS = Object.freeze(['grid', 'frames', 'fps', 'loop', 'temperatureScale', 'blackbodyIntensity', 'anisotropy']);
+export const LOCAL_VOLUME_GRID_FIELDS = Object.freeze(['grid', 'frames', 'fps', 'loop', 'temperatureScale', 'flameTemperature', 'blackbodyIntensity', 'anisotropy']);
export const LOCAL_VOLUME_MAXIMUM_STEPS = 32;
export const LOCAL_VOLUME_MAXIMUM_SCATTER_OCTAVES = 4;
export const LOCAL_VOLUME_MAXIMUM_NOISE_OCTAVES = 5;
diff --git a/src/local-volumes.js b/src/local-volumes.js
index 22131b76..2fe22405 100644
--- a/src/local-volumes.js
+++ b/src/local-volumes.js
@@ -224,7 +224,7 @@ export function localVolumeGridSample(volume, x, y, z, out, withBlackbody = fals
out.fill(0);
return out;
}
- const baked = withBlackbody ? bakedGridEmission(volume.gridData, volume.temperatureScale) : null;
+ const baked = withBlackbody ? bakedGridEmission(volume.gridData, volume.temperatureScale, volume.flameTemperature) : null;
return sampleVolumeGrid(volumeGridFloats(volume.gridData), volume.gridData.resolution, unitX, unitY, unitZ, out, baked);
}
diff --git a/src/path-tracer-native-volumes.js b/src/path-tracer-native-volumes.js
index 4712a5c1..4bfd7dd3 100644
--- a/src/path-tracer-native-volumes.js
+++ b/src/path-tracer-native-volumes.js
@@ -2,11 +2,17 @@
// step that appends the packed grids to a FORMAT-v2 input. Cook reads the box, the grid and the Principled Volume terms; a Crucible grid
// volume is the same box (centre and size, no rotation), so the terms carry over one for one.
+import { floatToHalf, gridTemperature } from './volume-grid.js';
+
/** The most bytes a FORMAT-v2 texel pool holds (Cook's TEXTURE_POOL_MAX_BYTES): the textures and the grids share it. */
export const NATIVE_TEXEL_POOL_MAX_BYTES = 1 << 30;
const COOK_GRID_CHANNELS = ['density', 'temperature', 'flame'];
+function halfFloatsOf(values) {
+ return values ? Uint16Array.from(values, floatToHalf) : new Uint16Array(0);
+}
+
function requireNativeGridVolume(volume, index) {
const label = `environment.localVolumes[${index}]`;
if (volume.shape !== 'grid' || volume.part || !volume.gridData) {
@@ -28,6 +34,12 @@ export function cookGridVolume(volume) {
for (const name of COOK_GRID_CHANNELS) {
if (channels[name]) cookChannels[name] = channels[name];
}
+ // Cook takes the blackbody of the temperature channel, so a flame remap (#1735) travels as that channel: the flame grid as Blender makes the temperature grid.
+ if (volume.flameTemperature) {
+ const remapped = gridTemperature(volume.gridData, volume.flameTemperature);
+ if (remapped) cookChannels.temperature = halfFloatsOf(remapped);
+ else delete cookChannels.temperature;
+ }
return {
resolution: Array.from(resolution),
channels: cookChannels,
diff --git a/src/path-tracer-webgpu-local-volumes.js b/src/path-tracer-webgpu-local-volumes.js
index 3f87f3c3..8e4b83bc 100644
--- a/src/path-tracer-webgpu-local-volumes.js
+++ b/src/path-tracer-webgpu-local-volumes.js
@@ -137,7 +137,7 @@ export function packWebGpuGrid(volume) {
const floats = volumeGridFloats(gridData);
const flame = floats.flame ?? floats.density;
const temperature = volume.emissionRamp ? floats.temperature : null;
- const baked = withBlackbody ? bakedGridEmission(gridData, volume.temperatureScale) : null;
+ const baked = withBlackbody ? bakedGridEmission(gridData, volume.temperatureScale, volume.flameTemperature) : null;
const velocity = withVelocity ? gridUnitVelocity(gridData, volume.size) : null;
const voxelCount = width * height * depth;
for (let voxel = 0; voxel < voxelCount; voxel++) {
diff --git a/src/volume-grid.js b/src/volume-grid.js
index 50114730..59df7eea 100644
--- a/src/volume-grid.js
+++ b/src/volume-grid.js
@@ -333,18 +333,45 @@ function mixCorners(data) {
}
const bakedEmissionByGrid = new WeakMap();
+const temperatureByGrid = new WeakMap();
+
+/** Blender's flame cut-off: a voxel with a flame value at or below it is cold (rna_fluid.cc, where the flame grid becomes the temperature grid). */
+export const FLAME_TEMPERATURE_THRESHOLD = 0.01;
+
+/** The temperature of one voxel from its flame as Blender remaps it: `flame > 0.01 ? ignition + flame * (maximum - ignition) : 0`, in temperature units. */
+export function flameToTemperature(flame, flameTemperature) {
+ return flame > FLAME_TEMPERATURE_THRESHOLD ? flameTemperature.ignition + flame * (flameTemperature.maximum - flameTemperature.ignition) : 0;
+}
+
+/**
+ * The temperature channel a grid volume emits from: the grid's own, or with `flameTemperature` ({ ignition, maximum }) the flame channel remapped by
+ * flameToTemperature, so every voxel with flame sits between the ignition and the maximum however thin its smoke. Float32Array, decoded once per grid and
+ * remap; undefined when the grid lacks the source channel.
+ */
+export function gridTemperature(grid, flameTemperature = null) {
+ if (!flameTemperature) return volumeGridFloats(grid).temperature;
+ const key = `${flameTemperature.ignition}|${flameTemperature.maximum}`;
+ let byRemap = temperatureByGrid.get(grid);
+ if (!byRemap) temperatureByGrid.set(grid, byRemap = new Map());
+ if (!byRemap.has(key)) {
+ const flame = volumeGridFloats(grid).flame;
+ byRemap.set(key, flame ? flame.map(value => flameToTemperature(value, flameTemperature)) : undefined);
+ }
+ return byRemap.get(key);
+}
/**
* The blackbody radiance of every voxel's temperature times `temperatureScale`, as three Float32Arrays (`red`, `green`, `blue`) with the grid's voxel order,
- * decoded once per grid and scale. Values past the half float limit (HALF_FLOAT_MAXIMUM, above about 35,000 K) are
- * clamped by the rasterizer's upload. The blackbody intensity and emission strength stay uniforms. A grid without a temperature channel bakes to all zeros.
+ * decoded once per grid, scale and flame remap (gridTemperature). Values past the half float limit (HALF_FLOAT_MAXIMUM, above about 35,000 K) are
+ * clamped by the rasterizer's upload. The blackbody intensity and emission strength stay uniforms. A grid without the source channel bakes to all zeros.
*/
-export function bakedGridEmission(grid, temperatureScale) {
+export function bakedGridEmission(grid, temperatureScale, flameTemperature = null) {
+ const remapKey = flameTemperature ? `${flameTemperature.ignition}|${flameTemperature.maximum}` : '';
const cached = bakedEmissionByGrid.get(grid);
- if (cached?.temperatureScale === temperatureScale) return cached;
- const temperature = volumeGridFloats(grid).temperature;
+ if (cached?.temperatureScale === temperatureScale && cached.remapKey === remapKey) return cached;
+ const temperature = gridTemperature(grid, flameTemperature);
const voxels = grid.resolution[0] * grid.resolution[1] * grid.resolution[2];
- const baked = { temperatureScale, red: new Float32Array(voxels), green: new Float32Array(voxels), blue: new Float32Array(voxels) };
+ const baked = { temperatureScale, remapKey, red: new Float32Array(voxels), green: new Float32Array(voxels), blue: new Float32Array(voxels) };
const radiance = [0, 0, 0];
if (temperature) {
for (let voxel = 0; voxel < voxels; voxel++) {
diff --git a/tests/agent-explosion.test.js b/tests/agent-explosion.test.js
index c3371a66..f3d8ec95 100644
--- a/tests/agent-explosion.test.js
+++ b/tests/agent-explosion.test.js
@@ -204,10 +204,10 @@ test('the MCP server finds the grids an add-explosion operation will name, so th
assert.equal(readVolumeGridFiles([blast({ seed: -1 })], '.').size, 0);
});
-// Both options are off: the recipe is the one the first release wrote, byte for byte (hashes taken from main before #1775, refreshed for #1736's emission change).
+// Both options are off: the recipe is the one the first release wrote, byte for byte (hashes taken from main before #1775, refreshed for #1736's emission change and #1735's flame remap).
const FIRST_RELEASE_HASHES = new Map([
- ['{}', 'f4243c2be216986f1db1dbd8a20f32a0e76bebb01d361c1973f00bc939bf8a79'],
- ['{"start":0.5,"fuel":"oily","seed":7}', 'eea21da58c694709f99fab840c6b73e58ef9b7cf3b8720db00ed8a3d7fb67f61'],
+ ['{}', 'd391923bb69c2e00dbe3cb82811dc3a8cb0f76f5f3a3d9c0ec5cd8ec39b7ac24'],
+ ['{"start":0.5,"fuel":"oily","seed":7}', '48ede2e133f804d0a35b675edbd031289702993cdbcbc60ec0872e132ec1dfc0'],
]);
test('sparks specks and motionBlur false write exactly the recipe of the first release, and false is an alias of specks', () => {
diff --git a/tests/volume-grid-flame-temperature.test.js b/tests/volume-grid-flame-temperature.test.js
new file mode 100644
index 00000000..d1178cea
--- /dev/null
+++ b/tests/volume-grid-flame-temperature.test.js
@@ -0,0 +1,81 @@
+// Blender's flame remap for grid volumes (#1735): `flame > 0.01 ? ignition + flame * (maximum - ignition) : 0` makes every voxel with flame emit by its
+// flame value, whatever its smoke density, which is the orange rim along the smoke edge of a Cycles fire.
+
+import test from 'node:test';
+import assert from 'node:assert/strict';
+import { bakedGridEmission, decodeVolumeGrid, encodeVolumeGrid, flameToTemperature, floatToHalf, gridTemperature, blackbodyRadiance } from '../src/volume-grid.js';
+import { resolveLocalVolumes } from '../src/local-volumes.js';
+import { cookGridVolume } from '../src/path-tracer-native-volumes.js';
+
+const BLENDER_FIRE = { ignition: 1.5, maximum: 3 };
+
+// Four voxels in a row: thick smoke with flame, thin smoke with flame, smoke with no flame, flame below the cut-off. The temperature channel is cool everywhere.
+function fourVoxelGrid() {
+ return decodeVolumeGrid(encodeVolumeGrid({
+ resolution: [4, 1, 1],
+ channels: {
+ density: Float32Array.of(1, 0.01, 1, 1),
+ temperature: Float32Array.of(0.1, 0.1, 0.1, 0.1),
+ flame: Float32Array.of(0.5, 0.5, 0, 0.005),
+ },
+ }));
+}
+
+test('the flame remap is 0 up to the 0.01 cut-off, then ignition plus flame times the span', () => {
+ assert.equal(flameToTemperature(0, BLENDER_FIRE), 0);
+ assert.equal(flameToTemperature(0.01, BLENDER_FIRE), 0);
+ assert.equal(flameToTemperature(0.5, BLENDER_FIRE), 2.25);
+ assert.equal(flameToTemperature(1, BLENDER_FIRE), 3);
+ assert.ok(Math.abs(flameToTemperature(0.0101, BLENDER_FIRE) - 1.5) < 0.02, 'a faint flame already sits at the ignition temperature, not at 0');
+});
+
+test('with the remap the emission comes from the flame grid and not from the smoke density', () => {
+ const grid = fourVoxelGrid();
+ const baked = bakedGridEmission(grid, 1000, BLENDER_FIRE);
+ const expected = blackbodyRadiance(2250, [0, 0, 0]);
+ for (const voxel of [0, 1]) {
+ assert.ok(Math.abs(baked.red[voxel] - expected[0]) <= expected[0] * 1e-6, `voxel ${voxel} red`);
+ assert.ok(Math.abs(baked.green[voxel] - expected[1]) <= expected[1] * 1e-6, `voxel ${voxel} green`);
+ }
+ assert.equal(baked.red[0], baked.red[1], 'density 1 and density 0.01 emit the same');
+ assert.equal(baked.red[2], 0, 'no flame, no emission');
+ assert.equal(baked.red[3], 0, 'a flame under the cut-off is cold');
+});
+
+test('without the remap the grid bakes its temperature channel as before, and the two bakes do not share a cache', () => {
+ const grid = fourVoxelGrid();
+ const plain = bakedGridEmission(grid, 1000);
+ const remapped = bakedGridEmission(grid, 1000, BLENDER_FIRE);
+ const cool = blackbodyRadiance(gridTemperature(grid)[2] * 1000, [0, 0, 0]);
+ assert.ok(Math.abs(plain.red[2] - cool[0]) <= cool[0] * 1e-6);
+ assert.notEqual(plain, remapped);
+ assert.equal(bakedGridEmission(grid, 1000), bakedGridEmission(grid, 1000), 'the plain bake is still cached');
+ assert.equal(gridTemperature(grid), gridTemperature(grid, null));
+});
+
+test('a grid without a flame channel bakes to no emission under the remap', () => {
+ const grid = decodeVolumeGrid(encodeVolumeGrid({ resolution: [2, 1, 1], channels: { density: Float32Array.of(1, 1), temperature: Float32Array.of(2, 2) } }));
+ assert.equal(bakedGridEmission(grid, 1000, BLENDER_FIRE).red.every(value => value === 0), true);
+});
+
+test('flameTemperature resolves on a grid volume, is refused elsewhere and with a wrong shape, and leaves a volume without it unchanged', () => {
+ const base = { shape: 'grid', grid: 'blast.67vg', center: [0, 0, 0], size: [1, 1, 1] };
+ const [plain] = resolveLocalVolumes([base]);
+ assert.equal('flameTemperature' in plain, false);
+ const [withRemap] = resolveLocalVolumes([{ ...base, flameTemperature: BLENDER_FIRE }]);
+ assert.deepEqual(withRemap.flameTemperature, BLENDER_FIRE);
+ assert.throws(() => resolveLocalVolumes([{ ...base, flameTemperature: { ignition: 3, maximum: 1.5 } }]), /maximum \(1.5\) must be at least ignition \(3\)/);
+ assert.throws(() => resolveLocalVolumes([{ ...base, flameTemperature: { ignition: 1.5 } }]), /ignition|maximum/);
+ assert.throws(() => resolveLocalVolumes([{ ...base, flameTemperature: { ignition: 1.5, maximum: 3, boost: 2 } }]), /boost is not a field/);
+ assert.throws(() => resolveLocalVolumes([{ ...base, flameTemperature: 1500 }]), /must be \{ ignition, maximum \}/);
+ assert.throws(() => resolveLocalVolumes([{ shape: 'box', center: [0, 0, 0], size: [1, 1, 1], flameTemperature: BLENDER_FIRE }]), /belongs to shape "grid"/);
+});
+
+test('Cook gets the remapped flame as the temperature channel, and the plain channel when there is no remap', () => {
+ const gridData = fourVoxelGrid();
+ const volume = { gridData, center: [0, 0, 0], size: [1, 1, 1], density: 1, absorption: [1, 1, 1], scatter: 0, anisotropy: 0, emission: [1, 1, 1], emissionStrength: 1, temperatureScale: 1000, blackbodyIntensity: 1 };
+ assert.equal(cookGridVolume(volume).channels.temperature, gridData.channels.temperature);
+ const remapped = cookGridVolume({ ...volume, flameTemperature: BLENDER_FIRE }).channels.temperature;
+ assert.deepEqual(Array.from(remapped), [2.25, 2.25, 0, 0].map(floatToHalf));
+ assert.notDeepEqual(Array.from(remapped), Array.from(gridData.channels.temperature));
+});