|
| 1 | +// The reflective floor: a `reflector()` TSL node renders the scene from a mirrored |
| 2 | +// camera into its own render target, sampled through a normal-map-perturbed UV and |
| 3 | +// blended additively into `MeshPhongNodeMaterial.emissiveNode` (not `colorNode` — the |
| 4 | +// reflection ADDS onto the checkerboard diffuse, it doesn't replace it). |
| 5 | +import { useMemo } from 'react' |
| 6 | +import { useTexture } from '@react-three/drei/webgpu' |
| 7 | +import { reflector, texture, uv } from 'three/tsl' |
| 8 | +import { BoxGeometry, Mesh, MeshPhongNodeMaterial, RepeatWrapping, SRGBColorSpace } from 'three/webgpu' |
| 9 | +import type { Node } from 'three/webgpu' |
| 10 | + |
| 11 | +const FLOOR_COLOR_URL = |
| 12 | + 'https://cdn.jsdelivr.net/gh/mrdoob/three.js@r185/examples/textures/floors/FloorsCheckerboard_S_Diffuse.jpg' |
| 13 | +const FLOOR_NORMAL_URL = |
| 14 | + 'https://cdn.jsdelivr.net/gh/mrdoob/three.js@r185/examples/textures/floors/FloorsCheckerboard_S_Normal.jpg' |
| 15 | + |
| 16 | +// Reflector resolution scale is fixed at the original's hardcoded value: it sizes an |
| 17 | +// internal render target at construction time (not a reactive uniform), so it isn't |
| 18 | +// exposed as a leva control here — see reflection.tsx's header DIVERGENCE note. |
| 19 | +const REFLECTION_RESOLUTION_SCALE = 0.2 |
| 20 | + |
| 21 | +export function ReflectiveFloor() { |
| 22 | + const [floorColor, floorNormal] = useTexture([FLOOR_COLOR_URL, FLOOR_NORMAL_URL]) |
| 23 | + |
| 24 | + const { mesh, reflectionTarget } = useMemo(() => { |
| 25 | + floorColor.wrapS = RepeatWrapping |
| 26 | + floorColor.wrapT = RepeatWrapping |
| 27 | + floorColor.colorSpace = SRGBColorSpace |
| 28 | + floorColor.repeat.set(15, 15) |
| 29 | + |
| 30 | + floorNormal.wrapS = RepeatWrapping |
| 31 | + floorNormal.wrapT = RepeatWrapping |
| 32 | + floorNormal.repeat.set(15, 15) |
| 33 | + |
| 34 | + const floorUV = uv().mul(15) |
| 35 | + const floorNormalOffset = texture(floorNormal, floorUV).xy.mul(2).sub(1).mul(0.02) |
| 36 | + |
| 37 | + const reflection = reflector({ resolutionScale: REFLECTION_RESOLUTION_SCALE }) |
| 38 | + reflection.target.rotateX(-Math.PI / 2) |
| 39 | + // Non-null: `reflector()` always constructs with a default `uvNode` (`screenUV.flipX()`) |
| 40 | + // — @types/three declares the field nullable for the general `TextureNode` case, but |
| 41 | + // this instance is never null at runtime. |
| 42 | + reflection.uvNode = reflection.uvNode!.add(floorNormalOffset) |
| 43 | + |
| 44 | + const material = new MeshPhongNodeMaterial() |
| 45 | + material.colorNode = texture(floorColor, floorUV) |
| 46 | + // Cast: `emissiveNode` is read generically by `NodeMaterial`'s base |
| 47 | + // `setupOutgoingLight()` for every subclass (confirmed against |
| 48 | + // `three/src/materials/nodes/NodeMaterial.js`), but @types/three only declares it on |
| 49 | + // `MeshStandardNodeMaterial` — same class of duck-typed-property gap as |
| 50 | + // `scene.fogNode`/`scene.backgroundNode` (see reflection.tsx header DIVERGENCE). |
| 51 | + ;(material as unknown as { emissiveNode: Node | null }).emissiveNode = reflection.mul(0.25) |
| 52 | + material.normalMap = floorNormal |
| 53 | + material.normalScale.set(0.2, -0.2) |
| 54 | + |
| 55 | + const mesh = new Mesh(new BoxGeometry(50, 0.001, 50), material) |
| 56 | + mesh.receiveShadow = true |
| 57 | + |
| 58 | + return { mesh, reflectionTarget: reflection.target } |
| 59 | + }, [floorColor, floorNormal]) |
| 60 | + |
| 61 | + return ( |
| 62 | + <> |
| 63 | + <primitive object={mesh} /> |
| 64 | + <primitive object={reflectionTarget} /> |
| 65 | + </> |
| 66 | + ) |
| 67 | +} |
0 commit comments