3D engine that AI agents drive
Merge the grid fog trace with the grid worker (issue #543)
Nguyen Xuan An0c36f2d333c7
diff --cc src/irradiance-grid.jsindex 5a16ff4e,f13c9d3b..80ba6e2f--- a/src/irradiance-grid.js+++ b/src/irradiance-grid.js@@@ -291,20 -326,25 +326,36 @@@ export function traceIrradianceGrid(parconst length = Math.hypot(dx, dy, dz);return tracer.trace(point[0], point[1], point[2], dx / length, dy / length, dz / length, length, true).nearestTriangle < 0;};+ if (scatteredData) {+ fillInvalidNodes(scatteredData, Uint8Array.from(valid), layout.size);+ smoothNodes(scatteredData, valid, layout.size, isConnected);+ }fillInvalidNodes(data, valid, layout.size);smoothNodes(data, valid, layout.size, isConnected);- return withMeanRadiance({ ...layout, data, nodes: nodeCount, solidNodes, seconds: (performance.now() - started) / 1000 });+ const grid = { ...layout, data, nodes: nodeCount, solidNodes, seconds: (performance.now() - started) / 1000 };- const scatteredGrid = scatteredData ? { ...layout, data: scatteredData } : null;- // The fog's scattering reads the grid without importing this lazy module: the mean light arriving at a point from every- // direction is the constant term of the fit. Not enumerable, so the report that copies the grid's fields stays plain data.- // It leaves out the light the fog scattered, which the fog on screen adds again with its own sun term.- Object.defineProperty(grid, 'meanRadiance', {- value: position => {- const total = evaluateIrradianceGrid(grid, position, NO_DIRECTION);- if (!scatteredGrid) return total;- const scattered = evaluateIrradianceGrid(scatteredGrid, position, NO_DIRECTION);- return total.map((value, channel) => Math.max(0, value - scattered[channel]));- },- });++ if (scatteredData) {++ grid.fogScattered = scatteredData;++ }++ return withMeanRadiance(grid);+}++/**+ * The fog's scattering reads the grid without importing this lazy module: the mean light arriving at a point from every+ * direction is the constant term of the fit. Not enumerable, so the report that copies the grid's fields stays plain data,- * and a grid that came back from a worker (which clones plain data only) gets it here.++ * and a grid that came back from a worker (which clones plain data only) gets it here. It leaves out the light the fog++ * itself scattered (`fogScattered`, same layout as `data`), which the fog on screen adds again with its own sun term.+ */+export function withMeanRadiance(grid) {+ if (!Object.hasOwn(grid, 'meanRadiance')) {- Object.defineProperty(grid, 'meanRadiance', { value: position => evaluateIrradianceGrid(grid, position, NO_DIRECTION) });++ Object.defineProperty(grid, 'meanRadiance', {++ value: position => {++ const total = evaluateIrradianceGrid(grid, position, NO_DIRECTION);++ if (!grid.fogScattered) return total;++ const scattered = evaluateIrradianceGrid({ ...grid, data: grid.fogScattered }, position, NO_DIRECTION);++ return total.map((value, channel) => Math.max(0, value - scattered[channel]));++ },++ });+ }return grid;}