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.

three.js adapter: a total budget for shadow maps; Showcase: the cases' heading clears the nav

Yifeng "Evan" Wang780320b63aea
diff --git a/site/showcase/index.html b/site/showcase/index.html
index e6b5698..0a55112 100644
--- a/site/showcase/index.html
+++ b/site/showcase/index.html
@@ -32,6 +32,7 @@
.pk .acts { grid-column: 1 / -1; display: flex; flex-wrap: wrap; gap: 10px; align-items: center; }
.pk .addr { grid-column: 1 / -1; font: 400 11.5px/1.5 var(--font-mono); color: var(--mute); overflow-wrap: anywhere; }
/* ---- Cases: what the engine does outside games, by industry ---- */
+ #cases { scroll-margin-top: 96px; }
.filters { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 26px; }
.filters button { appearance: none; font: 500 13.5px/1 var(--font-sans); color: var(--bone-2); background: transparent; border: 1px solid var(--line-2); border-radius: 999px; padding: 9px 14px; cursor: pointer; transition: color 0.2s, border-color 0.2s, background 0.2s; }
.filters button:hover { color: var(--bone); border-color: var(--bone-2); }
diff --git a/tools/three/engine.ts b/tools/three/engine.ts
index 8ce0a83..e31234d 100644
--- a/tools/three/engine.ts
+++ b/tools/three/engine.ts
@@ -55,7 +55,9 @@ interface Tex {
/** Feature -> count of scene elements that use something three.js cannot express (reported per scene). */
export type Dropped = Record<string, number>;
-const SHADOW = { directional: 4096, spot: 2048, point: 1024, maxPoint: 4, maxSpot: 8, radius: 2 };
+// maxTotal: a shadow map is a texture unit in every lit material's shader, next to the material's own maps and the
+// environment, and WebGL promises 16 units: with 8 spot and 4 point shadows a textured material stopped compiling.
+const SHADOW = { directional: 4096, spot: 2048, point: 1024, maxPoint: 4, maxSpot: 8, maxTotal: 8, radius: 2 };
/**
* Emissive surfaces light nothing in three.js, so the adapter does what a three.js developer would do by hand: point
@@ -862,6 +864,13 @@ export class ThreeEngine implements Engine {
};
shadowed("PointLight", SHADOW.maxPoint);
shadowed("SpotLight", SHADOW.maxSpot);
+ // And together: the directional lights keep theirs, the rest go to the spot and point lights nearest the camera.
+ const casting = lights.filter((l) => l.castShadow && (l.type === "DirectionalLight" || l.type === "SpotLight" || l.type === "PointLight"));
+ const dist = (l: THREE.Light) => (l.type === "DirectionalLight" ? -1 : l.getWorldPosition(new THREE.Vector3()).distanceTo(camPos));
+ casting.sort((a, b) => dist(a) - dist(b)).slice(SHADOW.maxTotal).forEach((l) => {
+ l.castShadow = false;
+ this.drop(`shadows over the total budget (${SHADOW.maxTotal})`, l.name);
+ });
// Directional shadow: an orthographic frustum around the shadow-casting geometry near the camera.
const box = new THREE.Box3();