Skip to content

The native runtime for three.js: three.js, down to the metal. 45× smaller than Electron, on a Sony PS3, and path-traced at 60 fps on ray-tracing GPUs.

2.0 KB
Raw Blame
// What a grid program has simulated survives a rebuild of the live scene (gpu.rs `carry_grids`): volume/grid-program's
// grid, 1.5 a second into every cell, in the real-time renderer for 24 frames; after 12 the frame script adds a group,
// which a build places (the scene is built again; hiding a mesh, as this did, is an edit in place now). The grid holds
// 0.6 as if nothing had happened, and the wall behind the absorber reads 2 e^-1.2 = 0.6024. (Started again at the
// rebuild it held 0.3: 1.098.)
import { BoxGeometry, DensityGrid, GridProgram, Group, Mesh, MeshStandardMaterial, VolumeMesh } from "@threert/scene";
import { defineGame } from "@threert/gameplay";
import { CENTER, camera, result, sceneWithSky, type TestMeta } from "./lib.ts";
import { GRID_MEDIUM } from "./volume-grid-uniform.ts";

export const test: TestMeta = {
  id: "volume/grid-rebuild-live", features: ["volume", "density-grid", "edits"], reference: "analytic", temporal: true,
  metric: { kind: "radiance", region: CENTER, expected: 2 * Math.exp(-1.2), tolerance: 0.03 },
  live: 24,
  note: "as volume/grid-program; a mesh behind the camera hidden at frame 12 rebuilds the scene",
};

const FILL = new GridProgram("test_fill_rebuild", {
  params: { Rate: 1 },
  code: `
    s.a.x += $Rate * i.dt;`,
});

let frames = 0;
export default defineGame<Record<string, never>>({
  state: {},
  setup: () => {
    frames = 0;
    const scene = sceneWithSky(0);
    const wall = new Mesh(new BoxGeometry(8, 8, 0.1), new MeshStandardMaterial({ name: "wall", color: 0x000000, emissive: 0xffffff, emissiveIntensity: 2 }));
    wall.position.set(0, 0, -2);
    const box = new VolumeMesh({ volume: GRID_MEDIUM.with({ Density: 2 }), majorant: 2, grid: new DensityGrid({ size: [16, 16, 8], program: FILL.with({ Rate: 1.5 }) }) });
    box.scale.set(2, 2, 1);
    return result(scene, camera(scene, [0, 0, 5], [0, 0, 0]), [wall, box]);
  },
  frame(f) {
    if (++frames === 12) f.scene.add(new Group());
  },
  handlers: {},
});