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.

Live: a shadow swept over a still floor leaves no ripples (history held where the light changed) (#176)

Evan (Yifeng) Wang0778f4df373e
diff --git a/docs/RUNTIME.md b/docs/RUNTIME.md
index 92b847a..f0dd3e1 100644
--- a/docs/RUNTIME.md
+++ b/docs/RUNTIME.md
@@ -1599,9 +1599,9 @@ none of them it traces its light there. Two limits are known and held by tests s
## Moving objects in the live view
The live view's histories (the irradiance and specular ones on the traced grid, the upscaler's colour on the output
-grid) follow a moving object by its motion vectors (`motion_in`), and three rules keep it from leaving copies of
+grid) follow a moving object by its motion vectors (`motion_in`), and four rules keep it from leaving copies of
itself where it was (`live.wgsl`; measured by `node tools/bench-object-ghosting.ts`: a dragged device, a light thrown
-over a polished floor, the camera still):
+over a polished floor, a character running over a sunlit floor, the camera still):
- **What it uncovers starts again.** The g-buffer says whether a surface is a moving object's that crossed a traced
sample this frame (`GB.moves`; one that moved less, a leaf in the wind or a far walker, is left to a still edge's
@@ -1617,6 +1617,14 @@ over a polished floor, the camera still):
- **Reflections follow what moves past them.** While the lighting is live (a moving object sets it), a still
surface's specular history is clipped to this frame's neighbourhood (`SPEC_CLIP` deviations plus `SPEC_CLIP_REL`
of its luminance): a light carried past a polished floor left its reflection along its path.
+- **Where the light changed, the history waits for it.** While the lighting is live, a traced sample whose light
+ jumped since the previous frame (by `LIGHT_CHANGE` of itself and `LIGHT_FLICKER` times its own flicker: a shadow's
+ edge passed, a lamp's pool moved) starts its flicker count again (`track`), and the output pixels around it hold
+ their history to this frame's samples until it has settled (`LIGHT_SETTLE` frames, `upscale`). The floor under a
+ running character stands still, so nothing else clamped it: its sun shadow left a line of its edge a frame, rippling
+ behind it.
+
+![A character running over a sunlit floor, before (left) and after (right): the ripples its shadow's edge left behind](images/shadow-ghosting.jpg)
![Before and after: a dragged device's edge lines and text copies, a thrown light's trail and its streak in the floor's reflection](images/object-ghosting.jpg)
diff --git a/docs/images/shadow-ghosting.jpg b/docs/images/shadow-ghosting.jpg
new file mode 100644
index 0000000..7c6f924
Binary files /dev/null and b/docs/images/shadow-ghosting.jpg differ
diff --git a/renderer/shaders/live.wgsl b/renderer/shaders/live.wgsl
index fef20db..8b2f334 100644
--- a/renderer/shaders/live.wgsl
+++ b/renderer/shaders/live.wgsl
@@ -1052,6 +1052,19 @@ fn track(@builtin(global_invocation_id) gid: vec3<u32>) {
}
}
let still = 1.0 - clamp(length(vel) / UP_SPEED, 0.0, 1.0);
+ // The light here changed (a shadow's edge passed, a lamp's pool moved, a light switched): the composed sample
+ // jumped by more than LIGHT_CHANGE of itself since the previous frame at the same surface point, and by far more
+ // than it flickers under the jitter (`Flicker`). Its history is of the light before: `upscale` holds it to this
+ // frame's samples. Without it, a shadow swept over a still floor (a character running in the sun) left a
+ // line of its edge a frame, fading over UP_SAMPLES frames: the floor stands still, so nothing clamped it, and a
+ // line a pixel wide widens its own range. While the lighting is live (`mode.z`: something moved, as anti-lag).
+ // The flicker's frame count starts again there (`n`): until it is LIGHT_SETTLE again, `upscale` holds the history
+ // around to this frame's samples (the line of an edge outlives the frame that edge passed).
+ var changed = false;
+ if (L.mode.z != 0u && f.n >= 2.0) {
+ let jump = abs(now - f.last.x) / max(max(now, f.last.x), 1.0e-4 / exposure_scale());
+ changed = jump > LIGHT_CHANGE && jump > LIGHT_FLICKER * f.sd;
+ }
var sd = select(1.0, f.sd, f.n >= 1.0);
if (f.n >= 2.0) {
let bend = min(abs(now - 2.0 * f.last.x + f.last.y) / max(max(now, max(f.last.x, f.last.y)), 1e-9), UP_ONSET * max(f.sd, 0.01));
@@ -1091,7 +1104,7 @@ fn track(@builtin(global_invocation_id) gid: vec3<u32>) {
at = L.jit.xy;
}
life *= 1.0 - reactive;
- up_state[this_half(q)] = vec4<u32>(pack2x16float(vec2<f32>(now, select(now, f.last.x, f.n >= 1.0))), pack2x16float(vec2<f32>(sd * still, min(max(f.n + still, 1.0), 9.0))), pack2x16float(vec2<f32>(life, min(lz, 6.0e4))), pack2x16float(at));
+ up_state[this_half(q)] = vec4<u32>(pack2x16float(vec2<f32>(now, select(now, f.last.x, f.n >= 1.0))), pack2x16float(vec2<f32>(sd * still, select(min(max(f.n + still, 1.0), 9.0), 1.0, changed))), pack2x16float(vec2<f32>(life, min(lz, 6.0e4))), pack2x16float(at));
}
// The upscaler works on linear radiance and its means are plain ones. (Weighted by 1 / (1 + luminance x exposure), so
@@ -1111,6 +1124,12 @@ const UP_REJECT: f32 = 0.25;
const UP_REACTIVE: f32 = 4.0;
// The share of a pixel a medium that changes with time takes (`medium_share`) from which the pixel is as reactive.
const UP_MEDIUM: f32 = 0.25;
+// How much a composed sample's luminance changes from the previous frame, as a share of it, and in deviations of its
+// own flicker, before the light there counts as changed (`track`), and the flicker frame count from which it counts
+// as settled again (`upscale`).
+const LIGHT_CHANGE: f32 = 0.35;
+const LIGHT_FLICKER: f32 = 2.5;
+const LIGHT_SETTLE: f32 = 6.0;
// How far outside the range of the history's pixels around it a point sample may lie (`judge`): this many deviations
// of the traced sample's own flicker (`Flicker`). Of 1, 2, 3 (measured while the deviation was still an amount of
// light): Bistro held still changes 0.49, 0.42, 0.38 from frame to frame, and Tokyo's skyline at dusk is furthest from
@@ -1522,6 +1541,7 @@ fn upscale(@builtin(global_invocation_id) gid: vec3<u32>) {
// (A lock spares the output pixels at the thing it is for: where in its traced pixel that is, `Lock`; full within
// LOCK_REACH output pixels, none half a pixel further.)
var lock = 0.0;
+ var shaded = 0.0;
let centre = vec2<f32>(f32(gid.x), f32(gid.y)) + 0.5;
for (var k = 0; k < 9; k++) {
let qx = bc.x + k % 3 - 1;
@@ -1537,6 +1557,11 @@ fn upscale(@builtin(global_invocation_id) gid: vec3<u32>) {
let j = u32(qy * iw + qx);
let at = (vec2<f32>(f32(qx), f32(qy)) + unpack2x16float(up_state[this_half(j)].w)) * up;
lock = max(lock, locked(j) * clamp(LOCK_REACH + 0.5 - length(at - centre), 0.0, 1.0));
+ // (A sample whose light changed lately, `track`, near enough to count: fully the frame it changed, less as its
+ // flicker's count grows back to LIGHT_SETTLE.)
+ if (L.mode.z != 0u && wt > 0.02) {
+ shaded = max(shaded, clamp((LIGHT_SETTLE - unpack2x16float(up_state[this_half(j)].y).y) / (LIGHT_SETTLE - 1.0), 0.0, 1.0));
+ }
}
let cur = select(fb[u32(bc.y * iw + bc.x)].rgb, sum / wsum, wsum > 1e-9);
let conf = min(wsum, 1.0);
@@ -1582,7 +1607,8 @@ fn upscale(@builtin(global_invocation_id) gid: vec3<u32>) {
let kept = hist.w * (1.0 - verdict.w);
if (kept > 0.0) {
var hc = max(hist.rgb + verdict.xyz, vec3<f32>(0.0));
- hc = mix(hc, clamp(hc, xlo, xhi), moving * (1.0 - lock));
+ // (And where the light changed among the samples around: the history is of the light before.)
+ hc = mix(hc, clamp(hc, xlo, xhi), max(moving, shaded) * (1.0 - lock));
let most = min(mix(UP_SAMPLES, UP_MOVING, moving), mix(UP_SAMPLES, UP_REACTIVE, clamp(sample_vel[u32(bc.y * iw + bc.x)].w, 0.0, 1.0)));
count = min(kept + conf, most);
let a = clamp(conf / max(count, 1e-4), 0.0, 1.0);
diff --git a/tools/bench-object-ghosting.ts b/tools/bench-object-ghosting.ts
index d6b2a3a..03c5659 100644
--- a/tools/bench-object-ghosting.ts
+++ b/tools/bench-object-ghosting.ts
@@ -2,7 +2,9 @@
// object moves, the way an app's drag or a physics body moves it: a pose a frame, by host message. Two scenes:
// device: a device dragged over a workbench (object-ghosting/device.game.ts, Pocket Bench's Tab5 made of boxes);
// orb: a glowing orb thrown across a dark room with a polished floor (object-ghosting/orb.game.ts, the Atrium of
-// threert/native-app-template at night): a moving light, seen directly and in the floor's reflection. What the moving object leaves behind (copies
+// threert/native-app-template at night): a moving light, seen directly and in the floor's reflection;
+// walker: a round character running over a sunlit plank floor (object-ghosting/walker.game.ts, Mochi Party's
+// mochi seen as the game sees it): its hard sun shadow sweeping over a still floor. What the moving object leaves behind (copies
// of its edges where it was, its screen smeared over the wall) is measured against a converged frame of the same pose
// (history reset, then accumulated), as bench-ghosting does.
//
@@ -11,7 +13,8 @@
// Moves (one pose a frame at 60 fps):
// device slide: x -0.18 -> +0.18 m in 0.5 s (about 13 px a frame at 1280 x 720), then held;
// device lift: y 0.2 -> 0.34 m and a quarter turn of yaw in 0.4 s, then held;
-// orb throw: x -0.6 -> +0.6 m, 0.12 m over the floor, in 0.5 s (2.4 m/s), then held.
+// orb throw: x -0.6 -> +0.6 m, 0.12 m over the floor, in 0.5 s (2.4 m/s), then held;
+// walker run: x -2 -> +2 m in 0.9 s (4.4 m/s, a mochi running), then held.
// Shots: the move's midpoint, and 0 / 0.1 / 0.3 s after it ends. Per shot, over the band the device sweeps: mean dE,
// p95, and trail_px (p95 over rows of the longest horizontal run of pixels off by dE > 10).
import { spawn, execFileSync } from "node:child_process";
@@ -45,6 +48,9 @@ const SCENES: Record<string, Record<string, Move>> = {
orb: {
throw: { from: { x: -0.6, y: 0.12, z: -0.1, ry: 0 }, to: { x: 0.6, y: 0.12, z: -0.1, ry: 0 }, frames: 30, band: "0,0.3,1,1" },
},
+ walker: {
+ run: { from: { x: -2, y: 0, z: 0.6, ry: 0 }, to: { x: 2, y: 0, z: 0.6, ry: 0 }, frames: 54, band: "0,0.35,1,0.95" },
+ },
};
const sceneArg = flag("--scene", "all");
const sceneNames = sceneArg === "all" ? Object.keys(SCENES) : [sceneArg];
diff --git a/tools/object-ghosting/walker.game.ts b/tools/object-ghosting/walker.game.ts
new file mode 100644
index 0000000..4e57238
--- /dev/null
+++ b/tools/object-ghosting/walker.game.ts
@@ -0,0 +1,65 @@
+// A round character running over a sunlit plank floor, seen from above and behind as a third-person game sees it
+// (Mochi Party, made on threert/native-app-template): its hard sun shadow sweeps over the floor, its body over the
+// planks. The driver (bench-object-ghosting.ts) moves it with `{t: "pose", x, y, z}` host messages, one per frame.
+import { BoxGeometry, DataTexture, DirectionalLight, Group, Mesh, MeshPhysicalMaterial, MeshStandardMaterial, PerspectiveCamera, Scene, SkyAtmosphere, SphereGeometry } from "@threert/scene";
+import { defineGame, host } from "@threert/gameplay";
+
+let walker: Group;
+
+host.on("pose", (m) => {
+ walker.position.set(Number(m.x ?? 0), Number(m.y ?? 0), Number(m.z ?? 0));
+});
+
+function planks(): DataTexture {
+ const w = 256, h = 256, px = new Uint8Array(w * h * 4);
+ for (let y = 0; y < h; y++) {
+ const row = Math.floor(y / 32), seam = y % 32 < 2, shift = (row * 97) % w;
+ for (let x = 0; x < w; x++) {
+ const xx = (x + shift) % w, joint = xx % 128 < 2;
+ const k = seam || joint ? 0.72 : 0.92 + 0.08 * Math.sin(row * 3.1 + xx * 0.05);
+ px.set([214 * k, 160 * k, 108 * k, 255], (y * w + x) * 4);
+ }
+ }
+ const t = new DataTexture(w, h, px);
+ t.repeat = [4, 4];
+ return t;
+}
+
+export default defineGame({
+ state: {},
+ handlers: {},
+ setup: () => {
+ const scene = new Scene();
+ const sun = new DirectionalLight(0xfff1dc, 80000);
+ sun.setSunAngles(38, 205);
+ scene.add(sun);
+ scene.environment = new SkyAtmosphere({ sun });
+ const floor = new Mesh(new BoxGeometry(10, 0.1, 8), new MeshStandardMaterial({ map: planks(), roughness: 0.55 }));
+ floor.position.y = -0.05;
+ scene.add(floor);
+ const wall = new Mesh(new BoxGeometry(10, 2.6, 0.2), new MeshStandardMaterial({ color: 0xf2dcc4, roughness: 0.9 }));
+ wall.position.set(0, 1.3, -4);
+ scene.add(wall);
+ const rug = new Mesh(new BoxGeometry(2.6, 0.012, 2.6), new MeshStandardMaterial({ color: 0x9be8c4, roughness: 0.95 }));
+ rug.position.set(0.6, 0.006, 0.4);
+ scene.add(rug);
+ walker = new Group();
+ const skin = new MeshPhysicalMaterial({ color: 0xff8fb1, roughness: 0.35, clearcoat: 0.8, clearcoatRoughness: 0.15 });
+ const body = new Mesh(new SphereGeometry(0.35, 32, 20), skin);
+ body.position.y = 0.34;
+ walker.add(body);
+ for (const s of [-1, 1]) {
+ const foot = new Mesh(new SphereGeometry(0.095, 16, 10), skin);
+ foot.position.set(s * 0.15, 0.04, 0.05);
+ walker.add(foot);
+ }
+ walker.position.set(-2, 0, 0.6);
+ scene.add(walker);
+ const camera = new PerspectiveCamera(48, 16 / 9, 0.05, 100);
+ camera.position.set(0, 4.0, 5.9);
+ camera.lookAt(0, 0.3, 0.3);
+ camera.setManualExposure(13.4);
+ scene.add(camera);
+ return { scene, camera, options: { name: "object-ghosting-walker", width: 1280, height: 720 } };
+ },
+});