create-threenative 0.2.2 → 0.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +10 -2
- package/agent-docs/asset-mcp-loop.md +33 -0
- package/agent-docs/ctx-surface.md +69 -0
- package/agent-docs/engine-capabilities.md +4 -0
- package/agent-docs/framework-blocks-you.md +60 -0
- package/agent-docs/look-at-it-and-budget-the-look.md +27 -0
- package/agent-docs/performance-default.md +42 -0
- package/agent-docs/pixel-budget.md +20 -0
- package/agent-docs/playtest-fail-closed.md +23 -0
- package/agent-docs/references/assertion-reference.md +652 -0
- package/agent-docs/references/capability-reference.md +3279 -0
- package/agent-docs/references/capture-the-frame.md +53 -0
- package/agent-docs/references/ctx-cookbook.md +51 -0
- package/agent-docs/references/debug-surface.md +63 -0
- package/agent-docs/references/finding-assets.md +147 -0
- package/agent-docs/references/gameplay-recipes.md +54 -0
- package/agent-docs/references/menu-screens.md +69 -0
- package/agent-docs/references/mobile-memory-budget.md +87 -0
- package/agent-docs/references/sculpt-from-a-reference.md +39 -0
- package/agent-docs/references/visual-baseline.md +133 -0
- package/agent-docs/references/webview-ui.md +113 -0
- package/agent-docs/sculpt-loop.md +16 -0
- package/agent-files/.agents/skills/threenative-builder/SKILL.md +7 -0
- package/agent-files/.agents/skills/threenative-verifier/SKILL.md +7 -0
- package/agent-files/.claude/agents/threenative-builder.md +7 -0
- package/agent-files/.claude/agents/threenative-verifier.md +7 -0
- package/agent-files/.threenative/agents/builder.md +32 -0
- package/agent-files/.threenative/agents/verifier.md +31 -0
- package/agent-files/AGENT-ROLES.md +26 -0
- package/capabilities.json +3542 -0
- package/dist/index.d.ts +191 -3
- package/dist/index.js +1707 -73
- package/dist/threenative.d.ts +9 -2
- package/dist/threenative.js +1825 -148
- package/package.json +30 -6
- package/template-assets/loading.ts +315 -0
- package/template-assets/patches/three@0.185.1.patch +287 -0
- package/templates/action-rpg/.codex/config.toml +15 -0
- package/templates/action-rpg/.mcp.json +6 -2
- package/templates/action-rpg/AGENTS.md +370 -6
- package/templates/action-rpg/CLAUDE.md +370 -6
- package/templates/action-rpg/index.html +0 -1
- package/templates/action-rpg/package.json +15 -11
- package/templates/action-rpg/playtests/combat.playtest.json +23 -9
- package/templates/action-rpg/playtests/fail.playtest.json +3 -3
- package/templates/action-rpg/playtests/inventory.playtest.json +16 -10
- package/templates/action-rpg/playtests/performance.playtest.json +22 -0
- package/templates/action-rpg/playtests/progress.playtest.json +18 -13
- package/templates/action-rpg/playtests/win.playtest.json +10 -4
- package/templates/action-rpg/public/icon.png +0 -0
- package/templates/action-rpg/src/entities/Enemy.ts +14 -4
- package/templates/action-rpg/src/game.ts +21 -0
- package/templates/action-rpg/src/render/lighting.ts +6 -1
- package/templates/action-rpg/src/render/loading.ts +267 -42
- package/templates/action-rpg/src/render/postprocessing.ts +68 -15
- package/templates/action-rpg/src/render/worldEnvironment.ts +583 -0
- package/templates/action-rpg/src/scenes/Play.ts +49 -19
- package/templates/action-rpg/src/state.ts +4 -0
- package/templates/action-rpg/src/style.css +45 -1
- package/templates/action-rpg/src/ui/AbilityBar.tsx +9 -7
- package/templates/action-rpg/src/ui/App.tsx +9 -8
- package/templates/action-rpg/src/ui/CharacterPanel.tsx +10 -8
- package/templates/action-rpg/src/ui/GameUi.tsx +24 -0
- package/templates/action-rpg/src/ui/Hud.tsx +10 -8
- package/templates/action-rpg/src/ui/InventoryPanel.tsx +9 -7
- package/templates/action-rpg/src/ui/main.tsx +14 -0
- package/templates/action-rpg/threenative.config.ts +21 -8
- package/templates/action-rpg/vite.config.ts +32 -2
- package/templates/defense/.codex/config.toml +15 -0
- package/templates/defense/.mcp.json +6 -2
- package/templates/defense/AGENTS.md +371 -8
- package/templates/defense/CLAUDE.md +371 -8
- package/templates/defense/README.md +2 -1
- package/templates/defense/index.html +0 -1
- package/templates/defense/package.json +16 -12
- package/templates/defense/playtests/leak.playtest.json +9 -4
- package/templates/defense/playtests/performance.playtest.json +22 -0
- package/templates/defense/playtests/placement.playtest.json +5 -5
- package/templates/defense/playtests/pointer-placement.playtest.json +89 -0
- package/templates/defense/playtests/scan.playtest.json +3 -3
- package/templates/defense/playtests/survive.playtest.json +11 -6
- package/templates/defense/public/icon.png +0 -0
- package/templates/defense/src/attackers/Attacker.ts +28 -12
- package/templates/defense/src/game.ts +24 -2
- package/templates/defense/src/render/lighting.ts +6 -1
- package/templates/defense/src/render/loading.ts +263 -37
- package/templates/defense/src/render/postprocessing.ts +70 -13
- package/templates/defense/src/render/shapes.ts +38 -0
- package/templates/defense/src/render/worldEnvironment.ts +583 -0
- package/templates/defense/src/scenes/Defense.ts +109 -50
- package/templates/defense/src/state.ts +6 -0
- package/templates/defense/src/style.css +45 -1
- package/templates/defense/src/ui/App.tsx +9 -4
- package/templates/defense/src/ui/GameUi.tsx +20 -0
- package/templates/defense/src/ui/Hud.tsx +6 -5
- package/templates/defense/src/ui/main.tsx +14 -0
- package/templates/defense/threenative.config.ts +21 -8
- package/templates/defense/vite.config.ts +34 -1
- package/templates/minimal/.codex/config.toml +15 -0
- package/templates/minimal/.mcp.json +6 -2
- package/templates/minimal/AGENTS.md +360 -255
- package/templates/minimal/CLAUDE.md +360 -255
- package/templates/minimal/index.html +0 -1
- package/templates/minimal/package.json +14 -11
- package/templates/minimal/playtests/atmosphere.playtest.json +32 -0
- package/templates/minimal/playtests/play.playtest.json +17 -5
- package/templates/minimal/public/icon.png +0 -0
- package/templates/minimal/src/game.ts +1 -0
- package/templates/minimal/src/render/camera.ts +1 -1
- package/templates/minimal/src/render/hud.ts +24 -8
- package/templates/minimal/src/render/lighting.ts +23 -2
- package/templates/minimal/src/render/loading.ts +23 -103
- package/templates/minimal/src/render/postprocessing.ts +92 -14
- package/templates/minimal/src/render/sky.ts +37 -32
- package/templates/minimal/src/render/worldEnvironment.ts +583 -0
- package/templates/minimal/src/scenes/Play.ts +91 -13
- package/templates/minimal/src/state.ts +8 -1
- package/templates/minimal/threenative.config.ts +15 -10
- package/templates/minimal/vite.config.ts +28 -1
- package/templates/platformer/.codex/config.toml +15 -0
- package/templates/platformer/.mcp.json +6 -2
- package/templates/platformer/AGENTS.md +385 -130
- package/templates/platformer/CLAUDE.md +385 -130
- package/templates/platformer/index.html +0 -1
- package/templates/platformer/package.json +17 -14
- package/templates/platformer/playtests/avoidance.playtest.json +6 -1
- package/templates/platformer/playtests/branding.playtest.json +29 -0
- package/templates/platformer/playtests/buffer.playtest.json +1 -1
- package/templates/platformer/playtests/collect.playtest.json +1 -1
- package/templates/platformer/playtests/collision-layers.playtest.json +2 -2
- package/templates/platformer/playtests/coyote.playtest.json +42 -8
- package/templates/platformer/playtests/damage.playtest.json +4 -1
- package/templates/platformer/playtests/dash.playtest.json +2 -2
- package/templates/platformer/playtests/jump.playtest.json +2 -3
- package/templates/platformer/playtests/native/touch-controls.playtest.json +33 -0
- package/templates/platformer/playtests/oneway.playtest.json +1 -1
- package/templates/platformer/playtests/performance.playtest.json +6 -5
- package/templates/platformer/playtests/respawn.playtest.json +2 -2
- package/templates/platformer/playtests/stomp-rise.playtest.json +17 -3
- package/templates/platformer/playtests/stomp.playtest.json +11 -3
- package/templates/platformer/playtests/terminal-loop-fail.playtest.json +13 -4
- package/templates/platformer/playtests/terminal-loop-win.playtest.json +3 -3
- package/templates/platformer/playtests/touch-controls-web.playtest.json +46 -0
- package/templates/platformer/public/icon.png +0 -0
- package/templates/platformer/src/entities/Character.ts +16 -10
- package/templates/platformer/src/entities/Patrol.ts +5 -0
- package/templates/platformer/src/game.ts +24 -1
- package/templates/platformer/src/render/lighting.ts +6 -1
- package/templates/platformer/src/render/loading.ts +270 -61
- package/templates/platformer/src/render/postprocessing.ts +68 -15
- package/templates/platformer/src/render/touch-controls.ts +54 -36
- package/templates/platformer/src/render/worldEnvironment.ts +583 -0
- package/templates/platformer/src/scenes/Level.ts +47 -22
- package/templates/platformer/src/state.ts +4 -0
- package/templates/platformer/src/style.css +45 -1
- package/templates/platformer/src/ui/App.tsx +9 -4
- package/templates/platformer/src/ui/GameUi.tsx +20 -0
- package/templates/platformer/src/ui/Hud.tsx +6 -5
- package/templates/platformer/src/ui/main.tsx +14 -0
- package/templates/platformer/threenative.config.ts +20 -10
- package/templates/platformer/vite.config.ts +34 -1
- package/templates/racing/.codex/config.toml +15 -0
- package/templates/racing/.mcp.json +6 -2
- package/templates/racing/AGENTS.md +370 -6
- package/templates/racing/CLAUDE.md +370 -6
- package/templates/racing/index.html +0 -1
- package/templates/racing/package.json +16 -12
- package/templates/racing/playtests/boost.playtest.json +5 -5
- package/templates/racing/playtests/outcome.playtest.json +14 -4
- package/templates/racing/playtests/performance.playtest.json +22 -0
- package/templates/racing/playtests/ranking.playtest.json +2 -2
- package/templates/racing/playtests/rescue.playtest.json +12 -6
- package/templates/racing/playtests/reverse.playtest.json +3 -3
- package/templates/racing/playtests/shortcut.playtest.json +3 -3
- package/templates/racing/public/icon.png +0 -0
- package/templates/racing/src/game.ts +24 -1
- package/templates/racing/src/render/camera.ts +10 -6
- package/templates/racing/src/render/lighting.ts +6 -1
- package/templates/racing/src/render/loading.ts +291 -34
- package/templates/racing/src/render/postprocessing.ts +70 -13
- package/templates/racing/src/render/worldEnvironment.ts +583 -0
- package/templates/racing/src/scenes/Race.ts +68 -36
- package/templates/racing/src/state.ts +4 -0
- package/templates/racing/src/style.css +45 -1
- package/templates/racing/src/track/Lap.ts +20 -9
- package/templates/racing/src/track/Ranking.ts +53 -14
- package/templates/racing/src/track/Track.ts +29 -16
- package/templates/racing/src/track/TrackSector.ts +14 -4
- package/templates/racing/src/ui/App.tsx +9 -4
- package/templates/racing/src/ui/GameUi.tsx +20 -0
- package/templates/racing/src/ui/Hud.tsx +6 -5
- package/templates/racing/src/ui/main.tsx +14 -0
- package/templates/racing/threenative.config.ts +21 -8
- package/templates/racing/vite.config.ts +34 -1
- package/templates/sailing/.codex/config.toml +15 -0
- package/templates/sailing/.mcp.json +20 -0
- package/templates/sailing/AGENTS.md +388 -0
- package/templates/sailing/CLAUDE.md +390 -0
- package/templates/sailing/README.md +25 -0
- package/templates/sailing/index.html +12 -0
- package/templates/sailing/kit.json +7 -0
- package/templates/sailing/native-playtests/survives.playtest.json +21 -0
- package/templates/sailing/package.json +52 -0
- package/templates/sailing/playtests/fail.playtest.json +24 -0
- package/templates/sailing/playtests/performance.playtest.json +21 -0
- package/templates/sailing/playtests/survives.playtest.json +38 -0
- package/templates/sailing/playtests/win.playtest.json +31 -0
- package/templates/sailing/public/favicon.svg +5 -0
- package/templates/sailing/public/icon.png +0 -0
- package/templates/sailing/src/entities/Ship.ts +96 -0
- package/templates/sailing/src/game.ts +40 -0
- package/templates/sailing/src/main.ts +15 -0
- package/templates/sailing/src/render/camera.ts +21 -0
- package/templates/sailing/src/render/lighting.ts +36 -0
- package/templates/sailing/src/render/loading.ts +315 -0
- package/templates/sailing/src/render/materials.ts +28 -0
- package/templates/sailing/src/render/palette.ts +24 -0
- package/templates/sailing/src/render/postprocessing.ts +33 -0
- package/templates/sailing/src/render/props.ts +52 -0
- package/templates/sailing/src/render/sky.ts +32 -0
- package/templates/sailing/src/render/water-material.ts +14 -0
- package/templates/sailing/src/render/water-shaders.ts +12 -0
- package/templates/sailing/src/render/wave-nodes.ts +10 -0
- package/templates/sailing/src/render/worldEnvironment.ts +590 -0
- package/templates/sailing/src/scenes/Boot.ts +12 -0
- package/templates/sailing/src/scenes/Sailing.ts +106 -0
- package/templates/sailing/src/state.ts +10 -0
- package/templates/sailing/src/style.css +61 -0
- package/templates/sailing/src/ui/App.tsx +15 -0
- package/templates/sailing/src/ui/GameUi.tsx +12 -0
- package/templates/sailing/src/ui/Hud.tsx +55 -0
- package/templates/sailing/src/ui/Menu.tsx +30 -0
- package/templates/sailing/src/ui/main.tsx +7 -0
- package/templates/sailing/tailwind.config.ts +2 -0
- package/templates/sailing/threenative.config.ts +34 -0
- package/templates/sailing/tsconfig.json +17 -0
- package/templates/sailing/vite.config.ts +34 -0
- package/templates/shooter/.codex/config.toml +15 -0
- package/templates/shooter/.mcp.json +6 -2
- package/templates/shooter/AGENTS.md +373 -7
- package/templates/shooter/CLAUDE.md +373 -7
- package/templates/shooter/index.html +0 -1
- package/templates/shooter/package.json +16 -11
- package/templates/shooter/playtests/fail.playtest.json +5 -4
- package/templates/shooter/playtests/input-control.playtest.json +131 -0
- package/templates/shooter/playtests/per-item.playtest.json +59 -0
- package/templates/shooter/playtests/performance.playtest.json +22 -0
- package/templates/shooter/playtests/shooter.playtest.json +22 -18
- package/templates/shooter/playtests/survives.playtest.json +1 -1
- package/templates/shooter/public/favicon.svg +1 -1
- package/templates/shooter/public/icon.png +0 -0
- package/templates/shooter/src/game.ts +24 -1
- package/templates/shooter/src/main.ts +3 -13
- package/templates/shooter/src/render/camera.ts +62 -12
- package/templates/shooter/src/render/lighting.ts +6 -1
- package/templates/shooter/src/render/loading.ts +262 -33
- package/templates/shooter/src/render/palette.ts +1 -1
- package/templates/shooter/src/render/postprocessing.ts +68 -16
- package/templates/shooter/src/render/shapes.ts +178 -0
- package/templates/shooter/src/render/ui.css +34 -1
- package/templates/shooter/src/render/worldEnvironment.ts +583 -0
- package/templates/shooter/src/scenes/Play.ts +246 -73
- package/templates/shooter/src/state.ts +13 -0
- package/templates/shooter/src/style.css +26 -0
- package/templates/shooter/src/ui/App.tsx +10 -12
- package/templates/shooter/src/ui/GameUi.tsx +20 -0
- package/templates/shooter/src/ui/Hud.tsx +8 -10
- package/templates/shooter/src/ui/Menu.tsx +24 -9
- package/templates/shooter/src/ui/main.tsx +15 -0
- package/templates/shooter/src/weapons/Projectile.ts +21 -10
- package/templates/shooter/threenative.config.ts +21 -8
- package/templates/shooter/vite.config.ts +32 -2
- package/templates/starter/.codex/config.toml +15 -0
- package/templates/starter/.mcp.json +6 -2
- package/templates/starter/AGENTS.md +429 -326
- package/templates/starter/CLAUDE.md +429 -326
- package/templates/starter/assets/pickup.wav +0 -0
- package/templates/starter/gitignore +4 -0
- package/templates/starter/index.html +0 -1
- package/templates/starter/native-playtests/react-hud.playtest.json +20 -0
- package/templates/starter/native-playtests/render-chain.playtest.json +12 -0
- package/templates/starter/package.json +16 -12
- package/templates/starter/playtests/assets.playtest.json +65 -0
- package/templates/starter/playtests/buffer.playtest.json +59 -9
- package/templates/starter/playtests/cloth.playtest.json +113 -0
- package/templates/starter/playtests/coyote.playtest.json +60 -8
- package/templates/starter/playtests/forward.playtest.json +29 -4
- package/templates/starter/playtests/gameover.playtest.json +66 -0
- package/templates/starter/playtests/goal.playtest.json +67 -0
- package/templates/starter/playtests/hot-reload.playtest.json +48 -8
- package/templates/starter/playtests/look.playtest.json +34 -6
- package/templates/starter/playtests/models-baseline.png +0 -0
- package/templates/starter/playtests/models.playtest.json +74 -0
- package/templates/starter/playtests/monitoring.playtest.json +38 -7
- package/templates/starter/playtests/odometer.playtest.json +54 -0
- package/templates/starter/playtests/pause.playtest.json +27 -4
- package/templates/starter/playtests/play.playtest.json +43 -7
- package/templates/starter/playtests/respawn.playtest.json +46 -6
- package/templates/starter/playtests/restart.playtest.json +65 -10
- package/templates/starter/playtests/seed.playtest.json +44 -8
- package/templates/starter/playtests/survives.playtest.json +27 -4
- package/templates/starter/playtests/textures-baseline.png +0 -0
- package/templates/starter/playtests/textures.playtest.json +74 -0
- package/templates/starter/playtests/zoom-pinch.playtest.json +86 -0
- package/templates/starter/playtests/zoom-wheel.playtest.json +59 -0
- package/templates/starter/public/icon.png +0 -0
- package/templates/starter/src/entities/Goal.ts +122 -0
- package/templates/starter/src/entities/Player.ts +40 -2
- package/templates/starter/src/game.ts +25 -0
- package/templates/starter/src/render/camera.ts +31 -5
- package/templates/starter/src/render/easing.ts +5 -0
- package/templates/starter/src/render/effects/gradualBackground.ts +48 -0
- package/templates/starter/src/render/effects/lensDistortion.ts +43 -0
- package/templates/starter/src/render/effects/sparkle.ts +49 -0
- package/templates/starter/src/render/lighting.ts +6 -1
- package/templates/starter/src/render/loading.ts +315 -0
- package/templates/starter/src/render/materials.ts +17 -1
- package/templates/starter/src/render/pennant.ts +54 -0
- package/templates/starter/src/render/postprocessing.ts +46 -15
- package/templates/starter/src/render/scenery.ts +83 -0
- package/templates/starter/src/render/shapes.ts +9 -21
- package/templates/starter/src/render/worldEnvironment.ts +583 -0
- package/templates/starter/src/scenes/Play.ts +146 -36
- package/templates/starter/src/state.ts +12 -0
- package/templates/starter/src/style.css +45 -1
- package/templates/starter/src/ui/App.tsx +9 -6
- package/templates/starter/src/ui/GameUi.tsx +20 -0
- package/templates/starter/src/ui/Hud.tsx +52 -29
- package/templates/starter/src/ui/Menu.tsx +25 -23
- package/templates/starter/src/ui/main.tsx +14 -0
- package/templates/starter/threenative.config.ts +27 -9
- package/templates/starter/tools/look.mjs +109 -0
- package/templates/starter/vite.config.ts +32 -2
- package/templates/minimal/assets/.gitkeep +0 -0
- package/templates/minimal/public/.gitkeep +0 -0
- package/templates/platformer/assets/.gitkeep +0 -0
- package/templates/platformer/public/.gitkeep +0 -0
- package/templates/starter/assets/.gitkeep +0 -0
- package/templates/starter/public/.gitkeep +0 -0
- package/templates/starter/public/pickup.ogg +0 -0
- /package/templates/starter/{public → assets}/native-proof.glb +0 -0
- /package/templates/starter/{public → assets}/native-proof.png +0 -0
|
@@ -3,11 +3,13 @@ import {
|
|
|
3
3
|
CylinderGeometry,
|
|
4
4
|
Group,
|
|
5
5
|
Mesh,
|
|
6
|
+
MeshStandardMaterial,
|
|
6
7
|
type Object3D,
|
|
7
8
|
SphereGeometry,
|
|
8
9
|
TorusGeometry,
|
|
9
10
|
} from "three";
|
|
10
11
|
import { createMaterials } from "./materials.js";
|
|
12
|
+
import { palette } from "./palette.js";
|
|
11
13
|
|
|
12
14
|
export function board(width = 28, depth = 20): Mesh {
|
|
13
15
|
const mesh = new Mesh(new BoxGeometry(width, 0.2, depth), createMaterials().ground);
|
|
@@ -17,6 +19,42 @@ export function board(width = 28, depth = 20): Mesh {
|
|
|
17
19
|
return mesh;
|
|
18
20
|
}
|
|
19
21
|
|
|
22
|
+
export function buildTiles(width = 28, depth = 20, size = 2): Group {
|
|
23
|
+
const group = new Group();
|
|
24
|
+
group.name = "build-tiles";
|
|
25
|
+
const geometry = new BoxGeometry(size, 0.02, size);
|
|
26
|
+
const columns = Math.floor(width / size);
|
|
27
|
+
const rows = Math.floor(depth / size);
|
|
28
|
+
for (let column = 0; column < columns; column += 1) {
|
|
29
|
+
for (let row = 0; row < rows; row += 1) {
|
|
30
|
+
const tile = new Mesh(
|
|
31
|
+
geometry,
|
|
32
|
+
new MeshStandardMaterial({
|
|
33
|
+
color: palette.accent,
|
|
34
|
+
depthWrite: false,
|
|
35
|
+
emissive: palette.accent,
|
|
36
|
+
emissiveIntensity: 0,
|
|
37
|
+
opacity: 0,
|
|
38
|
+
roughness: 0.7,
|
|
39
|
+
transparent: true,
|
|
40
|
+
}),
|
|
41
|
+
);
|
|
42
|
+
tile.name = `build-tile-${column}-${row}`;
|
|
43
|
+
tile.position.set((column + 0.5) * size - width / 2, 0.01, (row + 0.5) * size - depth / 2);
|
|
44
|
+
tile.visible = false;
|
|
45
|
+
group.add(tile);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return group;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function setTileHighlighted(tile: Object3D, highlighted: boolean): void {
|
|
52
|
+
if (!(tile instanceof Mesh) || !(tile.material instanceof MeshStandardMaterial)) return;
|
|
53
|
+
tile.visible = highlighted;
|
|
54
|
+
tile.material.emissiveIntensity = highlighted ? 0.8 : 0;
|
|
55
|
+
tile.material.opacity = highlighted ? 0.42 : 0;
|
|
56
|
+
}
|
|
57
|
+
|
|
20
58
|
export function routeSegment(length: number, width: number): Object3D {
|
|
21
59
|
const materials = createMaterials();
|
|
22
60
|
const group = new Group();
|
|
@@ -0,0 +1,583 @@
|
|
|
1
|
+
// Generated for you: ordinary Three.js; ThreeNative does not read this file. Delete or
|
|
2
|
+
// rewrite it freely — every default in here is a starting point, not a framework look.
|
|
3
|
+
//
|
|
4
|
+
// `WorldEnvironment` is Godot's name for the node that says which lighting effects a scene
|
|
5
|
+
// runs and how strong they are, and the property names below are Godot's `Environment`
|
|
6
|
+
// properties where Godot has one (`ssr_enabled`, `glow_enabled`, `tonemap_mode`) and
|
|
7
|
+
// Three.js's node names where it does not (`ssgi`, `gtao`). Nothing here is a new word.
|
|
8
|
+
//
|
|
9
|
+
// What it owns: building the stage factories and handing them to
|
|
10
|
+
// `renderer.createRenderChain()`, which decides the order stages run in, whether each can
|
|
11
|
+
// run on this target, and prints `TN_RENDER_CHAIN` naming every stage as applied or
|
|
12
|
+
// refused **with a reason** — so a stage that silently no-op'd is never mistaken for one
|
|
13
|
+
// you turned off. What it does not own: any colour, and any strength that is not a quality
|
|
14
|
+
// tier. Those are arguments, supplied by `postprocessing.ts`, yours to change.
|
|
15
|
+
|
|
16
|
+
import {
|
|
17
|
+
ACESFilmicToneMapping,
|
|
18
|
+
AgXToneMapping,
|
|
19
|
+
type Camera,
|
|
20
|
+
type DirectionalLight,
|
|
21
|
+
NeutralToneMapping,
|
|
22
|
+
type PerspectiveCamera,
|
|
23
|
+
type Scene,
|
|
24
|
+
} from "three";
|
|
25
|
+
import { bloom } from "three/addons/tsl/display/BloomNode.js";
|
|
26
|
+
import { denoise } from "three/addons/tsl/display/DenoiseNode.js";
|
|
27
|
+
import { ao } from "three/addons/tsl/display/GTAONode.js";
|
|
28
|
+
import { godrays } from "three/addons/tsl/display/GodraysNode.js";
|
|
29
|
+
import { ssgi } from "three/addons/tsl/display/SSGINode.js";
|
|
30
|
+
import { ssr } from "three/addons/tsl/display/SSRNode.js";
|
|
31
|
+
import { sharpen } from "three/addons/tsl/display/SharpenNode.js";
|
|
32
|
+
import {
|
|
33
|
+
color,
|
|
34
|
+
convertToTexture,
|
|
35
|
+
float,
|
|
36
|
+
metalness,
|
|
37
|
+
mrt,
|
|
38
|
+
normalView,
|
|
39
|
+
output,
|
|
40
|
+
pass,
|
|
41
|
+
roughness,
|
|
42
|
+
screenUV,
|
|
43
|
+
smoothstep,
|
|
44
|
+
vec2,
|
|
45
|
+
} from "three/tsl";
|
|
46
|
+
import type { Node } from "three/webgpu";
|
|
47
|
+
|
|
48
|
+
/** Godot's `Environment.tonemap_mode`, with the modes Three.js actually ships. */
|
|
49
|
+
export type TonemapMode = "aces" | "agx" | "neutral";
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* SSGI sample budget. The three tiers are the presets SSGINode's own documentation
|
|
53
|
+
* recommends for the temporally-filtered case, not numbers invented here. An unknown
|
|
54
|
+
* tier throws rather than quietly becoming the default.
|
|
55
|
+
*/
|
|
56
|
+
export type SsgiQuality = "low" | "medium" | "high";
|
|
57
|
+
|
|
58
|
+
const SSGI_QUALITY: Record<SsgiQuality, { sliceCount: number; stepCount: number }> = {
|
|
59
|
+
low: { sliceCount: 1, stepCount: 12 },
|
|
60
|
+
medium: { sliceCount: 2, stepCount: 8 },
|
|
61
|
+
high: { sliceCount: 3, stepCount: 16 },
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const TONEMAP: Record<TonemapMode, number> = {
|
|
65
|
+
aces: ACESFilmicToneMapping,
|
|
66
|
+
agx: AgXToneMapping,
|
|
67
|
+
neutral: NeutralToneMapping,
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
export interface IWorldEnvironmentOptions {
|
|
71
|
+
/** Screen-space indirect diffuse light. Godot calls this `ssil_enabled`. */
|
|
72
|
+
readonly ssgiEnabled?: boolean;
|
|
73
|
+
readonly ssgiQuality?: SsgiQuality;
|
|
74
|
+
/** How much of the gathered indirect light reaches the frame. Godot's `ssil_intensity`. */
|
|
75
|
+
readonly ssgiIntensity?: number;
|
|
76
|
+
/** How far, in world units, indirect light is gathered from. Room scale, not contact scale. */
|
|
77
|
+
readonly ssgiRadius?: number;
|
|
78
|
+
/** Screen-space reflections. Godot's `ssr_enabled`. */
|
|
79
|
+
readonly ssrEnabled?: boolean;
|
|
80
|
+
/**
|
|
81
|
+
* How far, in world units, a reflection ray is traced.
|
|
82
|
+
*
|
|
83
|
+
* `SSRNode` defaults this to **1**, which is one metre. On any scene larger than a
|
|
84
|
+
* tabletop that reads as "screen-space reflections are on and do nothing" — the ray
|
|
85
|
+
* terminates before it reaches the thing that should be standing in the floor.
|
|
86
|
+
*/
|
|
87
|
+
readonly ssrMaxDistance?: number;
|
|
88
|
+
/**
|
|
89
|
+
* Fraction of the display resolution the reflection is traced at.
|
|
90
|
+
*
|
|
91
|
+
* A reflection in a rough surface carries almost no high-frequency detail, so tracing it
|
|
92
|
+
* at half resolution costs a quarter of the rays and is very hard to see in the result.
|
|
93
|
+
*/
|
|
94
|
+
readonly ssrResolutionScale?: number;
|
|
95
|
+
/** Spatial denoise over the SSGI result. Off is noisier and cheaper. */
|
|
96
|
+
readonly denoiseEnabled?: boolean;
|
|
97
|
+
/** Raymarched shafts. Needs `godraysLight`, and that light must cast shadows. */
|
|
98
|
+
readonly godraysEnabled?: boolean;
|
|
99
|
+
/** How dense the participating medium is. Higher is foggier — and the whole frame brighter. */
|
|
100
|
+
readonly godraysDensity?: number;
|
|
101
|
+
/**
|
|
102
|
+
* Haze below this value is discarded instead of added to the frame.
|
|
103
|
+
*
|
|
104
|
+
* Without it the pass is a uniform brightener rather than a shaft renderer: it returns a
|
|
105
|
+
* non-zero value for nearly every pixel, and that "something" is tiny in linear space and
|
|
106
|
+
* enormous after the tone curve, which lifts shadows hardest. Subtracting a floor keeps
|
|
107
|
+
* the air inside a real beam and discards the ambient lift.
|
|
108
|
+
*/
|
|
109
|
+
readonly godraysFloor?: number;
|
|
110
|
+
/** Multiplier applied after the floor, so beams can be bright without the haze returning. */
|
|
111
|
+
readonly godraysIntensity?: number;
|
|
112
|
+
/**
|
|
113
|
+
* Raymarch steps per pixel. `GodraysNode` defaults to 60.
|
|
114
|
+
*
|
|
115
|
+
* Every step is a shadow-map sample, so this multiplies directly into the cost of the
|
|
116
|
+
* whole pass. The pass jitters its sample positions, so a lower count trades a hard shaft
|
|
117
|
+
* edge for a slightly noisier one rather than for banding — which the denoiser then
|
|
118
|
+
* largely absorbs.
|
|
119
|
+
*/
|
|
120
|
+
readonly godraysSteps?: number;
|
|
121
|
+
/**
|
|
122
|
+
* Ceiling on shaft brightness.
|
|
123
|
+
*
|
|
124
|
+
* This is also the single strongest control over how bright the *whole* scene looks: the
|
|
125
|
+
* pass adds its accumulated illumination to every pixel, not only to the pixels inside a
|
|
126
|
+
* visible beam, so raising it raises the shadow floor of the entire frame.
|
|
127
|
+
*/
|
|
128
|
+
readonly godraysMaxDensity?: number;
|
|
129
|
+
/**
|
|
130
|
+
* Ground-truth ambient occlusion, at contact scale. Godot's `ssao_enabled`.
|
|
131
|
+
*
|
|
132
|
+
* Deliberately a *second* occlusion term next to the one SSGI already produces. SSGI's AO
|
|
133
|
+
* is gathered over `ssgiRadius` — metres — the right scale for "this hall is enclosed"
|
|
134
|
+
* and the wrong one for "this foot is standing on this floor". This pass runs at a radius
|
|
135
|
+
* of centimetres and multiplies on top.
|
|
136
|
+
*/
|
|
137
|
+
readonly gtaoEnabled?: boolean;
|
|
138
|
+
/** Occlusion gather radius in world units. Contact scale, not room scale. */
|
|
139
|
+
readonly gtaoRadius?: number;
|
|
140
|
+
/** Exponent on the occlusion term: higher is a darker, tighter contact. */
|
|
141
|
+
readonly gtaoScale?: number;
|
|
142
|
+
/** Directions sampled per pixel. Cost is linear in this. */
|
|
143
|
+
readonly gtaoSamples?: number;
|
|
144
|
+
/** Fraction of display resolution the occlusion is traced at. */
|
|
145
|
+
readonly gtaoResolutionScale?: number;
|
|
146
|
+
/**
|
|
147
|
+
* Contrast-adaptive sharpening (RCAS) over the finished frame.
|
|
148
|
+
*
|
|
149
|
+
* Everything upstream of it blurs: the GI gather is denoised, the reflection is traced at
|
|
150
|
+
* reduced resolution and roughness-blurred. Each is worth its softness on its own and the
|
|
151
|
+
* sum is a frame with no micro-detail left. RCAS is contrast-adaptive, so it puts the
|
|
152
|
+
* edges back without ringing the soft areas.
|
|
153
|
+
*/
|
|
154
|
+
readonly sharpenEnabled?: boolean;
|
|
155
|
+
/** RCAS sharpness. **0 is maximum sharpening and 2 is none** — it is a radius, not a gain. */
|
|
156
|
+
readonly sharpenStrength?: number;
|
|
157
|
+
/** Corner darkening, as a fraction removed at the extreme corner. Zero disables it. */
|
|
158
|
+
readonly vignetteAmount?: number;
|
|
159
|
+
/** Godot's `glow_enabled`. */
|
|
160
|
+
readonly bloomEnabled?: boolean;
|
|
161
|
+
readonly bloomStrength?: number;
|
|
162
|
+
/** How far a bright pixel bleeds, as a fraction of the screen. */
|
|
163
|
+
readonly bloomRadius?: number;
|
|
164
|
+
/** Luminance a pixel must exceed before it blooms at all. */
|
|
165
|
+
readonly bloomThreshold?: number;
|
|
166
|
+
readonly tonemapMode?: TonemapMode;
|
|
167
|
+
/**
|
|
168
|
+
* Scene-referred exposure: multiplied into the scene pass **before** the tone curve, so
|
|
169
|
+
* the GI gather, reflections and bloom threshold all see the exposed image and the bloom
|
|
170
|
+
* threshold means the same thing at any exposure.
|
|
171
|
+
*
|
|
172
|
+
* `renderer.toneMappingExposure` is live too (measured on three@0.185.1 — see
|
|
173
|
+
* `docs/verification/exposure-ab-2026-08-30.md`), and applies at the same point in the
|
|
174
|
+
* graph, but only when no output node is installed. When every stage here is off, this
|
|
175
|
+
* class falls back to that scalar; when any stage runs, this multiply is the shutter.
|
|
176
|
+
*/
|
|
177
|
+
readonly exposure?: number;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export interface IWorldEnvironmentTarget {
|
|
181
|
+
/** The directional light godrays are raymarched against. Required only by `godraysEnabled`. */
|
|
182
|
+
readonly godraysLight?: DirectionalLight;
|
|
183
|
+
/**
|
|
184
|
+
* Composes the colour the chain starts from, given the scene pass it renders.
|
|
185
|
+
*
|
|
186
|
+
* The chain builds `pass(scene, camera)` itself, so a game that composes something onto the
|
|
187
|
+
* beauty pass before any stage runs — aerial perspective, read against that pass's own view-Z,
|
|
188
|
+
* is the one in these templates — has nowhere else to put it. The node returned here is the
|
|
189
|
+
* game's; the chain only decides where it is spliced in, which is first, ahead of exposure and
|
|
190
|
+
* every stage.
|
|
191
|
+
*/
|
|
192
|
+
readonly baseColour?: (scenePass: ReturnType<typeof pass>) => Node<"vec4">;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* What travels down the chain: the composed colour, as TSL's proxied vec4 node — the thing
|
|
197
|
+
* that carries `.mul`, `.add`, `.rgb` and `.r`. Every stage takes one and returns one, so the
|
|
198
|
+
* compiler checks the composition instead of taking a cast's word for it.
|
|
199
|
+
*/
|
|
200
|
+
type ChainNode = Node<"vec4">;
|
|
201
|
+
type ChainContext = {
|
|
202
|
+
readonly tier: "high" | "medium" | "low" | "off";
|
|
203
|
+
};
|
|
204
|
+
type ChainStage = {
|
|
205
|
+
readonly name: "ambientOcclusion" | "ssgi" | "godRays" | "ssr" | "sharpen" | "bloom" | "vignette";
|
|
206
|
+
readonly available?: (context: ChainContext) => boolean | string;
|
|
207
|
+
/** The chain plumbing is stage-agnostic, so it names the node it carries `unknown`. */
|
|
208
|
+
readonly build: (input: unknown, context: ChainContext) => unknown;
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* The one seam between the chain's `unknown` and this file's node algebra.
|
|
213
|
+
*
|
|
214
|
+
* What the chain hands a stage is the colour composed so far — the previous stage's return, or
|
|
215
|
+
* the exposed scene pass for the first one — and that is always a `ChainNode`. Saying so once,
|
|
216
|
+
* here, is what lets every stage below be written and *checked* as vec4-in, vec4-out instead of
|
|
217
|
+
* asserting its way through each expression. The wrapper adds no node to the graph.
|
|
218
|
+
*/
|
|
219
|
+
function stage(definition: {
|
|
220
|
+
readonly name: ChainStage["name"];
|
|
221
|
+
readonly available?: (context: ChainContext) => boolean | string;
|
|
222
|
+
readonly build: (input: ChainNode, context: ChainContext) => ChainNode;
|
|
223
|
+
}): ChainStage {
|
|
224
|
+
return {
|
|
225
|
+
...definition,
|
|
226
|
+
build: (input, context) => definition.build(input as ChainNode, context),
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* `three/addons` declares each TSL stage as its raw node class, and `DenoiseNode` is the only
|
|
232
|
+
* one in this chain whose declaration omits the element API (`.mul`, `.rgb`, `.r`) that the
|
|
233
|
+
* proxied node object it actually hands back at runtime carries — every other stage here is a
|
|
234
|
+
* `ChainNode` on its own, checked by the compiler. This is an assertion and nothing else: no
|
|
235
|
+
* conversion runs, no node is added to the graph, so it cannot move a pixel.
|
|
236
|
+
*/
|
|
237
|
+
function denoised(node: ReturnType<typeof denoise>): ChainNode {
|
|
238
|
+
return node as unknown as ChainNode;
|
|
239
|
+
}
|
|
240
|
+
export type OutputRenderer = {
|
|
241
|
+
kind: string;
|
|
242
|
+
raw: unknown;
|
|
243
|
+
/**
|
|
244
|
+
* Installs a node graph directly. Only reached when a `baseColour` is composed and every
|
|
245
|
+
* stage is off: the chain installs nothing for an empty stage list, and dropping the game's
|
|
246
|
+
* own composition on the floor would be the silent no-op this class exists to prevent.
|
|
247
|
+
*/
|
|
248
|
+
setOutputNode?: (node: unknown) => void;
|
|
249
|
+
createRenderChain?: (options: {
|
|
250
|
+
input?: unknown;
|
|
251
|
+
request?: { stages?: readonly string[]; tier?: ChainContext["tier"] };
|
|
252
|
+
stages?: readonly ChainStage[];
|
|
253
|
+
}) => {
|
|
254
|
+
applied: { dropped: readonly { name: string; reason: string }[]; stages: readonly string[] };
|
|
255
|
+
};
|
|
256
|
+
};
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Builds a Godot-named lighting chain and installs it through the renderer's render chain.
|
|
260
|
+
*
|
|
261
|
+
* Every stage a game turns on reports itself: the chain prints `TN_RENDER_CHAIN` naming
|
|
262
|
+
* each requested stage as applied or refused with a reason that is never blank. A game with
|
|
263
|
+
* GI switched off on a WebGL fallback looks exactly like a game that chose not to use it —
|
|
264
|
+
* unless it reads that line.
|
|
265
|
+
*/
|
|
266
|
+
export class WorldEnvironment {
|
|
267
|
+
readonly #options: Required<IWorldEnvironmentOptions>;
|
|
268
|
+
|
|
269
|
+
constructor(options: IWorldEnvironmentOptions = {}) {
|
|
270
|
+
const quality = options.ssgiQuality ?? "medium";
|
|
271
|
+
if (SSGI_QUALITY[quality] === undefined) {
|
|
272
|
+
// Fail closed. A typo'd tier that silently became "medium" is a quality setting
|
|
273
|
+
// nobody can trust afterwards.
|
|
274
|
+
throw new Error(
|
|
275
|
+
`Unknown ssgiQuality '${String(quality)}'. Use one of: ${Object.keys(SSGI_QUALITY).join(", ")}.`,
|
|
276
|
+
);
|
|
277
|
+
}
|
|
278
|
+
const tonemapMode = options.tonemapMode ?? "aces";
|
|
279
|
+
if (TONEMAP[tonemapMode] === undefined) {
|
|
280
|
+
throw new Error(
|
|
281
|
+
`Unknown tonemapMode '${String(tonemapMode)}'. Use one of: ${Object.keys(TONEMAP).join(", ")}.`,
|
|
282
|
+
);
|
|
283
|
+
}
|
|
284
|
+
this.#options = {
|
|
285
|
+
ssgiEnabled: options.ssgiEnabled ?? false,
|
|
286
|
+
ssgiQuality: quality,
|
|
287
|
+
ssgiIntensity: options.ssgiIntensity ?? 1,
|
|
288
|
+
ssgiRadius: options.ssgiRadius ?? 12,
|
|
289
|
+
ssrEnabled: options.ssrEnabled ?? false,
|
|
290
|
+
ssrMaxDistance: options.ssrMaxDistance ?? 30,
|
|
291
|
+
ssrResolutionScale: options.ssrResolutionScale ?? 0.5,
|
|
292
|
+
denoiseEnabled: options.denoiseEnabled ?? true,
|
|
293
|
+
godraysEnabled: options.godraysEnabled ?? false,
|
|
294
|
+
godraysDensity: options.godraysDensity ?? 0.7,
|
|
295
|
+
godraysFloor: options.godraysFloor ?? 0,
|
|
296
|
+
godraysIntensity: options.godraysIntensity ?? 1,
|
|
297
|
+
godraysSteps: options.godraysSteps ?? 60,
|
|
298
|
+
godraysMaxDensity: options.godraysMaxDensity ?? 0.5,
|
|
299
|
+
gtaoEnabled: options.gtaoEnabled ?? false,
|
|
300
|
+
gtaoRadius: options.gtaoRadius ?? 0.25,
|
|
301
|
+
gtaoScale: options.gtaoScale ?? 1,
|
|
302
|
+
gtaoSamples: options.gtaoSamples ?? 16,
|
|
303
|
+
gtaoResolutionScale: options.gtaoResolutionScale ?? 1,
|
|
304
|
+
sharpenEnabled: options.sharpenEnabled ?? false,
|
|
305
|
+
sharpenStrength: options.sharpenStrength ?? 0.2,
|
|
306
|
+
vignetteAmount: options.vignetteAmount ?? 0,
|
|
307
|
+
bloomEnabled: options.bloomEnabled ?? true,
|
|
308
|
+
bloomStrength: options.bloomStrength ?? 0.7,
|
|
309
|
+
bloomRadius: options.bloomRadius ?? 0.5,
|
|
310
|
+
bloomThreshold: options.bloomThreshold ?? 0.2,
|
|
311
|
+
tonemapMode,
|
|
312
|
+
exposure: options.exposure ?? 1,
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* Installs the chain. Returns what actually ran — the same applied report the chain
|
|
318
|
+
* printed — so a scene can assert on it instead of scraping the console.
|
|
319
|
+
*/
|
|
320
|
+
apply(
|
|
321
|
+
renderer: OutputRenderer,
|
|
322
|
+
scene: Scene,
|
|
323
|
+
camera: Camera,
|
|
324
|
+
target: IWorldEnvironmentTarget = {},
|
|
325
|
+
): {
|
|
326
|
+
dropped: readonly { name: string; reason: string }[];
|
|
327
|
+
stages: readonly string[];
|
|
328
|
+
} {
|
|
329
|
+
const options = this.#options;
|
|
330
|
+
const raw = renderer.raw as { toneMapping?: number; toneMappingExposure?: number };
|
|
331
|
+
raw.toneMapping = TONEMAP[options.tonemapMode];
|
|
332
|
+
|
|
333
|
+
const requested = (
|
|
334
|
+
["ssgi", "ambientOcclusion", "godRays", "ssr", "sharpen", "bloom", "vignette"] as const
|
|
335
|
+
).filter(
|
|
336
|
+
(name) =>
|
|
337
|
+
(name === "ssgi" && options.ssgiEnabled) ||
|
|
338
|
+
(name === "ambientOcclusion" && options.gtaoEnabled) ||
|
|
339
|
+
(name === "godRays" && options.godraysEnabled) ||
|
|
340
|
+
(name === "ssr" && options.ssrEnabled) ||
|
|
341
|
+
(name === "sharpen" && options.sharpenEnabled) ||
|
|
342
|
+
(name === "bloom" && options.bloomEnabled) ||
|
|
343
|
+
(name === "vignette" && options.vignetteAmount > 0),
|
|
344
|
+
);
|
|
345
|
+
|
|
346
|
+
// With no stage running there is no node graph to install — the renderer's own
|
|
347
|
+
// tone-mapping path renders the frame, and the exposure scalar is live there (measured:
|
|
348
|
+
// `docs/verification/exposure-ab-2026-08-30.md`). With any stage running, exposure is
|
|
349
|
+
// applied as a multiply on the scene pass instead: scene-referred, so every downstream
|
|
350
|
+
// stage sees the exposed image and the bloom threshold means the same thing at any
|
|
351
|
+
// exposure. Both land at the same point in the graph — before the tone curve.
|
|
352
|
+
if (requested.length === 0 && target.baseColour === undefined) {
|
|
353
|
+
raw.toneMappingExposure = options.exposure;
|
|
354
|
+
this.#reportApplied([], []);
|
|
355
|
+
return { dropped: [], stages: [] };
|
|
356
|
+
}
|
|
357
|
+
raw.toneMappingExposure = 1;
|
|
358
|
+
|
|
359
|
+
const scenePass = pass(scene, camera);
|
|
360
|
+
// Normals come from the multi-render-target alongside the colour. SSGI can derive them
|
|
361
|
+
// from depth alone, but the derived normal is flat across a triangle, which shows up as
|
|
362
|
+
// faceted bounce on exactly the smooth surfaces GI is bought for. Metalness and
|
|
363
|
+
// roughness ride along so SSR knows which pixels are supposed to reflect.
|
|
364
|
+
if (options.ssgiEnabled || options.ssrEnabled || options.gtaoEnabled) {
|
|
365
|
+
scenePass.setMRT(mrt({ output, normal: normalView, metalness, roughness }));
|
|
366
|
+
}
|
|
367
|
+
const depth = scenePass.getTextureNode("depth");
|
|
368
|
+
const normal = scenePass.getTextureNode("normal");
|
|
369
|
+
const metal = scenePass.getTextureNode("metalness");
|
|
370
|
+
const rough = scenePass.getTextureNode("roughness");
|
|
371
|
+
// Every stage here is perspective-camera maths — view-space reconstruction from depth,
|
|
372
|
+
// and a projection matrix inverse. The scene's camera is the one the pass rendered with.
|
|
373
|
+
const view = camera as PerspectiveCamera;
|
|
374
|
+
|
|
375
|
+
const base = target.baseColour?.(scenePass) ?? scenePass.getTextureNode("output");
|
|
376
|
+
const exposed = convertToTexture(base).mul(options.exposure);
|
|
377
|
+
const giDenoise = (node: ChainNode): ChainNode =>
|
|
378
|
+
options.denoiseEnabled ? denoised(denoise(node, depth, normal, view)) : node;
|
|
379
|
+
|
|
380
|
+
const stages: ChainStage[] = [
|
|
381
|
+
stage({
|
|
382
|
+
name: "ssgi",
|
|
383
|
+
build: (input) => {
|
|
384
|
+
const gi = ssgi(input, depth, normal, view);
|
|
385
|
+
const tier = SSGI_QUALITY[options.ssgiQuality];
|
|
386
|
+
gi.sliceCount.value = tier.sliceCount;
|
|
387
|
+
gi.stepCount.value = tier.stepCount;
|
|
388
|
+
gi.radius.value = options.ssgiRadius;
|
|
389
|
+
|
|
390
|
+
// SSGI writes two targets: AO in a single-channel `RedFormat` texture, and the GI
|
|
391
|
+
// term in an RGB one. Multiplying the beauty pass by the whole AO vec4 therefore
|
|
392
|
+
// zeroes green and blue and the entire scene renders monochrome red — take `.r`
|
|
393
|
+
// for the scalar and `.rgb` for the colour.
|
|
394
|
+
//
|
|
395
|
+
// No albedo buffer here, so the beauty pass stands in for it: occlusion darkens
|
|
396
|
+
// what is already lit, and the gathered indirect light is tinted by the surface it
|
|
397
|
+
// lands on. That is the standard approximation, and it is why a red wall tints a
|
|
398
|
+
// white floor at all. `ssgiIntensity` is the game's dial on that second term —
|
|
399
|
+
// gathered light is unbounded, and a small white room at 1.0 blows out.
|
|
400
|
+
const occlusion = giDenoise(gi.getAONode());
|
|
401
|
+
const indirect = giDenoise(gi.getGINode());
|
|
402
|
+
return input.mul(occlusion.r).add(input.mul(indirect.rgb).mul(options.ssgiIntensity));
|
|
403
|
+
},
|
|
404
|
+
}),
|
|
405
|
+
stage({
|
|
406
|
+
name: "ambientOcclusion",
|
|
407
|
+
build: (input) => {
|
|
408
|
+
const contact = ao(depth, normal, view);
|
|
409
|
+
contact.radius.value = options.gtaoRadius;
|
|
410
|
+
contact.scale.value = options.gtaoScale;
|
|
411
|
+
contact.samples.value = options.gtaoSamples;
|
|
412
|
+
contact.resolutionScale = options.gtaoResolutionScale;
|
|
413
|
+
// `RedFormat`, like SSGI's AO target: `.r` is the whole occlusion term, and
|
|
414
|
+
// multiplying by the vec4 would zero green and blue and render the scene red.
|
|
415
|
+
// Applied before SSGI by the chain's canonical order; composing it after the GI
|
|
416
|
+
// combine instead darkens crevices the gather re-lit — that is a look choice, and
|
|
417
|
+
// this file is where you make it.
|
|
418
|
+
return input.mul(contact.r);
|
|
419
|
+
},
|
|
420
|
+
}),
|
|
421
|
+
stage({
|
|
422
|
+
name: "godRays",
|
|
423
|
+
available: () => {
|
|
424
|
+
// Godrays are raymarched against the light's shadow map: the shaft is the volume
|
|
425
|
+
// the shadow map reports as lit. A light that casts no shadow yields a black pass,
|
|
426
|
+
// which would read as "godrays are on and do nothing" — so refuse it by name.
|
|
427
|
+
const light = target.godraysLight;
|
|
428
|
+
if (light === undefined) return "godraysEnabled is true but no godraysLight was passed";
|
|
429
|
+
if (light.castShadow !== true)
|
|
430
|
+
return `light '${light.name || "sun"}' does not cast shadows`;
|
|
431
|
+
return true;
|
|
432
|
+
},
|
|
433
|
+
build: (input) => {
|
|
434
|
+
const light = target.godraysLight as DirectionalLight;
|
|
435
|
+
const shafts = godrays(depth, view, light);
|
|
436
|
+
shafts.density.value = options.godraysDensity;
|
|
437
|
+
shafts.maxDensity.value = options.godraysMaxDensity;
|
|
438
|
+
shafts.raymarchSteps.value = options.godraysSteps;
|
|
439
|
+
// The pass jitters its samples with interleaved noise; at low step counts the
|
|
440
|
+
// noise renders as a regular lattice over every hazy pixel. The bilateral
|
|
441
|
+
// denoiser built for the GI gather smooths exactly that — high-frequency,
|
|
442
|
+
// depth-coherent — and the shaft edges survive because they sit on geometry
|
|
443
|
+
// silhouettes the depth term owns. Denoise before the floor: the floor's
|
|
444
|
+
// subtraction amplifies relative noise on near-floor values.
|
|
445
|
+
let shaft: ChainNode = convertToTexture(shafts);
|
|
446
|
+
if (options.denoiseEnabled) shaft = denoised(denoise(shaft, depth, normal, view));
|
|
447
|
+
// Floor, then scale, then tint. The floor is what turns this from a whole-frame
|
|
448
|
+
// brightener into a shaft renderer; the tint by the light's own colour is what
|
|
449
|
+
// stops a warm sun throwing a white beam.
|
|
450
|
+
const cleaned = shaft.rgb.sub(options.godraysFloor).max(0).mul(options.godraysIntensity);
|
|
451
|
+
return input.add(cleaned.mul(color(light.color)));
|
|
452
|
+
},
|
|
453
|
+
}),
|
|
454
|
+
stage({
|
|
455
|
+
name: "ssr",
|
|
456
|
+
build: (input) => {
|
|
457
|
+
// Three defaults that together make SSR silently do nothing on a real scene, all
|
|
458
|
+
// fixed here and each worth stating:
|
|
459
|
+
// maxDistance defaults to 1 world unit — a ray that dies after a metre
|
|
460
|
+
// reflectNonMetals defaults to false, so a polished *stone* floor never reflects
|
|
461
|
+
// roughnessNode null means every surface is treated as a perfect mirror
|
|
462
|
+
// `normal`, not `normal.xyz`. SSR calls `.sample()` on this node, so it needs the
|
|
463
|
+
// whole TextureNode; a swizzle produces a plain vec3 and the pass dies at shader
|
|
464
|
+
// build. `@types/three` declares the parameter as `Node<"vec3">`, which is what
|
|
465
|
+
// makes the swizzle look correct — the types are wrong here and the runtime is
|
|
466
|
+
// right. Both `colorNode` and `normalNode` are `.sample()`d inside the pass, so
|
|
467
|
+
// the composed input must be materialised ONCE and that texture reused on both
|
|
468
|
+
// sides of the add: passing the unmaterialised node to the pass while adding the
|
|
469
|
+
// reflections onto a second copy builds two parallel graphs, and the frame comes
|
|
470
|
+
// out as the bare background colour.
|
|
471
|
+
const base = convertToTexture(input);
|
|
472
|
+
const reflections = ssr(base, depth, normal as unknown as Parameters<typeof ssr>[2], {
|
|
473
|
+
camera: view,
|
|
474
|
+
metalnessNode: metal.r,
|
|
475
|
+
roughnessNode: rough.g,
|
|
476
|
+
reflectNonMetals: true,
|
|
477
|
+
});
|
|
478
|
+
reflections.maxDistance.value = options.ssrMaxDistance;
|
|
479
|
+
reflections.resolutionScale = options.ssrResolutionScale;
|
|
480
|
+
return base.add(reflections);
|
|
481
|
+
},
|
|
482
|
+
}),
|
|
483
|
+
stage({
|
|
484
|
+
name: "sharpen",
|
|
485
|
+
build: (input) => {
|
|
486
|
+
// Last of the image stages but one, because RCAS is defined on the finished
|
|
487
|
+
// picture: it looks at the contrast around a pixel and sharpens in proportion to
|
|
488
|
+
// it, so running it before bloom would sharpen edges that bloom then spreads
|
|
489
|
+
// back out. The third argument keeps the node from materialising its own render
|
|
490
|
+
// target; the chain's graph is fused instead.
|
|
491
|
+
return sharpen(convertToTexture(input), options.sharpenStrength, false);
|
|
492
|
+
},
|
|
493
|
+
}),
|
|
494
|
+
stage({
|
|
495
|
+
name: "bloom",
|
|
496
|
+
build: (input) =>
|
|
497
|
+
input.add(
|
|
498
|
+
bloom(
|
|
499
|
+
convertToTexture(input),
|
|
500
|
+
options.bloomStrength,
|
|
501
|
+
options.bloomRadius,
|
|
502
|
+
options.bloomThreshold,
|
|
503
|
+
),
|
|
504
|
+
),
|
|
505
|
+
}),
|
|
506
|
+
stage({
|
|
507
|
+
name: "vignette",
|
|
508
|
+
build: (input) => {
|
|
509
|
+
// Radius measured from the centre in screen-diagonal units, so the corner is 1
|
|
510
|
+
// and the falloff does not change shape when the window does. It starts at 0.55 —
|
|
511
|
+
// well outside the subject in a 16:9 frame — so the vignette is a corner, not a
|
|
512
|
+
// spotlight.
|
|
513
|
+
const radius = screenUV.sub(vec2(0.5, 0.5)).mul(vec2(1.78, 1)).length().mul(1.04);
|
|
514
|
+
const fall = smoothstep(float(0.55), float(1.02), radius).mul(options.vignetteAmount);
|
|
515
|
+
return input.mul(fall.oneMinus());
|
|
516
|
+
},
|
|
517
|
+
}),
|
|
518
|
+
];
|
|
519
|
+
|
|
520
|
+
// A composed base colour with every stage off still has to reach the frame. The chain
|
|
521
|
+
// installs nothing for an empty stage list, so this is the one path that goes direct.
|
|
522
|
+
if (requested.length === 0) {
|
|
523
|
+
if (renderer.kind !== "webgpu") {
|
|
524
|
+
this.#reportApplied([], [{ name: "baseColour", reason: `renderer:${renderer.kind}` }]);
|
|
525
|
+
return { dropped: [], stages: [] };
|
|
526
|
+
}
|
|
527
|
+
if (renderer.setOutputNode === undefined)
|
|
528
|
+
throw new Error("setOutputNode is unavailable, so the composed base colour cannot run.");
|
|
529
|
+
renderer.setOutputNode(exposed);
|
|
530
|
+
this.#reportApplied([], []);
|
|
531
|
+
return { dropped: [], stages: [] };
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
if (renderer.createRenderChain === undefined) throw new Error("RenderChain is unavailable.");
|
|
535
|
+
const chain = renderer.createRenderChain({
|
|
536
|
+
input: exposed,
|
|
537
|
+
request: { stages: requested, tier: "high" },
|
|
538
|
+
stages,
|
|
539
|
+
});
|
|
540
|
+
this.#reportApplied(chain.applied.stages, chain.applied.dropped);
|
|
541
|
+
return { dropped: chain.applied.dropped, stages: chain.applied.stages };
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
/**
|
|
545
|
+
* Prints `TN_WORLD_ENVIRONMENT`: every stage this class can run, named as applied or refused
|
|
546
|
+
* **with a reason that is never blank**, plus the tone curve and exposure that framed them.
|
|
547
|
+
*
|
|
548
|
+
* The chain prints its own line for the stages it installed, and it prints nothing at all when
|
|
549
|
+
* nothing was requested — which is exactly the case where a reader most needs to know that GI
|
|
550
|
+
* is off because this file says so rather than because a node silently no-op'd. This line is
|
|
551
|
+
* emitted every run, including the run where every stage is off.
|
|
552
|
+
*/
|
|
553
|
+
#reportApplied(
|
|
554
|
+
applied: readonly string[],
|
|
555
|
+
dropped: readonly { name: string; reason: string }[],
|
|
556
|
+
): void {
|
|
557
|
+
const options = this.#options;
|
|
558
|
+
const off: Record<ChainStage["name"], string> = {
|
|
559
|
+
ambientOcclusion: "gtaoEnabled is false",
|
|
560
|
+
bloom: "bloomEnabled is false",
|
|
561
|
+
godRays: "godraysEnabled is false",
|
|
562
|
+
sharpen: "sharpenEnabled is false",
|
|
563
|
+
ssgi: "ssgiEnabled is false",
|
|
564
|
+
ssr: "ssrEnabled is false",
|
|
565
|
+
vignette: "vignetteAmount is 0",
|
|
566
|
+
};
|
|
567
|
+
const stages = (Object.keys(off) as ChainStage["name"][]).sort().map((name) => {
|
|
568
|
+
if (applied.includes(name)) return { applied: true, name };
|
|
569
|
+
const refused = dropped.find((entry) => entry.name === name);
|
|
570
|
+
return { applied: false, name, reason: refused?.reason ?? off[name] };
|
|
571
|
+
});
|
|
572
|
+
console.info(
|
|
573
|
+
`TN_WORLD_ENVIRONMENT:${JSON.stringify({
|
|
574
|
+
denoise: options.denoiseEnabled,
|
|
575
|
+
exposure: options.exposure,
|
|
576
|
+
marker: "TN_WORLD_ENVIRONMENT",
|
|
577
|
+
ssgiQuality: options.ssgiQuality,
|
|
578
|
+
stages,
|
|
579
|
+
tonemapMode: options.tonemapMode,
|
|
580
|
+
})}`,
|
|
581
|
+
);
|
|
582
|
+
}
|
|
583
|
+
}
|