|
| 1 | +/** |
| 2 | + * shadowmap |
| 3 | + * R3F port of three.js `webgpu_shadowmap`, running on WebGPU. |
| 4 | + * Original: https://threejs.org/examples/#webgpu_shadowmap (~175 lines of JS) |
| 5 | + * |
| 6 | + * DEMONSTRATES |
| 7 | + * - `NodeMaterial.maskNode` (Shapes.tsx): a fractal-noise discard mask (MaterialX's |
| 8 | + * `mx_fractal_noise_float`) punched through the torus knot's fragment shader — since |
| 9 | + * the shadow pass shares the same material graph, discarded fragments cast no shadow |
| 10 | + * either, unlike legacy alpha-test setups that need a separate depth material to keep |
| 11 | + * a caster's silhouette and its shadow in sync |
| 12 | + * - `NodeMaterial.receivedShadowPositionNode` (Ground.tsx): perturbing the position the |
| 13 | + * ground material samples shadow maps at (`mx_fractal_noise_vec3`), producing a |
| 14 | + * wavy/distorted shadow edge with no extra vertex displacement |
| 15 | + * - Two independent shadow-casting lights (`SpotLight` + `DirectionalLight`, Lights.tsx) |
| 16 | + * configured via fiber's dash-path props (`shadow-camera-*`, `shadow-mapSize-*`, |
| 17 | + * `shadow-radius`) |
| 18 | + * - `useUniforms` feeding a leva-controlled threshold straight into the discard mask's |
| 19 | + * TSL graph (same "feed a value in, keep the graph identity stable" pattern as |
| 20 | + * tsl-halftone), tying a live control directly to the maskNode technique above |
| 21 | + * - Legacy `<fog attach="fog">` (a plain `THREE.Fog`, not a TSL `fogNode`) rendering |
| 22 | + * correctly under the WebGPU node-material pipeline — three's `NodeManager.updateFog()` |
| 23 | + * auto-wraps `scene.fog` into a fog node every frame, so the declarative fiber `<fog>` |
| 24 | + * idiom (used elsewhere in this corpus for legacy WebGL-style fog) needs no cast or |
| 25 | + * TSL rewrite here; see header DIVERGENCE for why this refines an existing AGENTS.md note |
| 26 | + * |
| 27 | + * DIVERGENCE from original |
| 28 | + * - Folder pattern: split by scene role into `Lights.tsx` (spot + directional, orbit + |
| 29 | + * bob animation), `Shapes.tsx` (torus knot + pillars), and `Ground.tsx` (the noise- |
| 30 | + * shaded/shadow-perturbed plane) — the flat file exceeded the ~200-line threshold |
| 31 | + * - OrbitControls (`target`, `minDistance`, `maxDistance`, no pan lock) becomes |
| 32 | + * DemoHelpers' camera-controls v3 wrapper; grid disabled (`grid={false}`) — the |
| 33 | + * original's own 600x600 noise-shaded ground plane IS the subject of this example |
| 34 | + * (`receivedShadowPositionNode`), and an infinite world grid at the same height would |
| 35 | + * visually compete with it |
| 36 | + * - `THREE.Timer` dropped; `useFrame`'s `state.delta`/`state.time` drive the per-frame |
| 37 | + * rotation/orbit/bob directly, same translation as every other port in this corpus |
| 38 | + * - The four pillars are four independent JSX `meshPhongNodeMaterial` instances instead |
| 39 | + * of the original's one shared material + three `.clone()`s — declarative simplicity, |
| 40 | + * the four extra material instances are negligible next to the torus knot / ground |
| 41 | + * node graphs |
| 42 | + * - The ground's `colorNode` drops the original's dead position-perturbation calc (a |
| 43 | + * `pos.xz` noise offset computed via `toVar()`/`addAssign()` but never read before the |
| 44 | + * node returns — apparent copy/paste from `receivedShadowPositionNode` just above it |
| 45 | + * in the original source); the perturbation is kept where it's actually used |
| 46 | + * - `renderer.inspector`'s dat.gui-less imperative setup gains a small leva panel |
| 47 | + * (`maskThreshold`, `shadowRadius`, `spinSpeed`, `exposure`) — the original has no UI |
| 48 | + * at all; added for interactivity/pedagogy, same rationale as this corpus's other |
| 49 | + * zero-GUI ports (sky, sprites) |
| 50 | + * - `spinSpeed` uniformly scales the torus knot's three rotation axes, the directional |
| 51 | + * light group's orbit, and the light's z-bob frequency together (one dial, not the |
| 52 | + * original's four independent hard-coded rates) |
| 53 | + * - `renderer.toneMappingExposure` driven from leva (same escape hatch as |
| 54 | + * `sky`/`postprocessing-bloom-emissive`: a WebGPURenderer property, not a TSL uniform) |
| 55 | + */ |
| 56 | +import { useEffect } from 'react' |
| 57 | +import { Canvas, useThree } from '@react-three/fiber/webgpu' |
| 58 | +import { useControls, folder } from 'leva' |
| 59 | +import { ACESFilmicToneMapping } from 'three/webgpu' |
| 60 | +import { DemoHelpers } from '../../utils/DemoHelpers' |
| 61 | +import { Ground } from './Ground' |
| 62 | +import { Lights } from './Lights' |
| 63 | +import { Pillars, TorusKnot } from './Shapes' |
| 64 | + |
| 65 | +// renderer.toneMappingExposure is a WebGPURenderer property, not a TSL uniform — set |
| 66 | +// imperatively (same pattern as sky.tsx / postprocessing-bloom-emissive.tsx). |
| 67 | +function ToneMappingExposure({ exposure }: { exposure: number }) { |
| 68 | + const renderer = useThree((s) => s.renderer) |
| 69 | + |
| 70 | + useEffect(() => { |
| 71 | + renderer.toneMappingExposure = exposure |
| 72 | + }, [renderer, exposure]) |
| 73 | + |
| 74 | + return null |
| 75 | +} |
| 76 | + |
| 77 | +export default function Shadowmap() { |
| 78 | + const { maskThreshold, shadowRadius, spinSpeed, exposure } = useControls('shadowmap', { |
| 79 | + maskThreshold: { value: 0, min: -1, max: 1, step: 0.01 }, |
| 80 | + shadow: folder({ |
| 81 | + shadowRadius: { value: 4, min: 0, max: 10, step: 0.5 }, |
| 82 | + }), |
| 83 | + spinSpeed: { value: 1, min: 0, max: 3, step: 0.05 }, |
| 84 | + exposure: { value: 1, min: 0, max: 2, step: 0.01 }, |
| 85 | + }) |
| 86 | + |
| 87 | + return ( |
| 88 | + <Canvas |
| 89 | + renderer={{ toneMapping: ACESFilmicToneMapping }} |
| 90 | + shadows |
| 91 | + background="#222244" |
| 92 | + camera={{ position: [0, 10, 20], fov: 45, near: 1, far: 1000 }} |
| 93 | + > |
| 94 | + <fog attach="fog" args={['#222244', 50, 100]} /> |
| 95 | + <Lights shadowRadius={shadowRadius} spinSpeed={spinSpeed} /> |
| 96 | + <TorusKnot maskThreshold={maskThreshold} spinSpeed={spinSpeed} /> |
| 97 | + <Pillars /> |
| 98 | + <Ground /> |
| 99 | + <ToneMappingExposure exposure={exposure} /> |
| 100 | + <DemoHelpers grid={false} target={[0, 2, 0]} minDistance={7} maxDistance={40} /> |
| 101 | + </Canvas> |
| 102 | + ) |
| 103 | +} |
0 commit comments