[v10][tsl] Uniforms defined as importable objects, typed across files
Summary
Let uniforms be defined once as an exported object and then read by that object instead of by a string. The types then flow through the import, so uniforms are typed in every file with no declare module and no explicit generics.
// sky.uniforms.ts
export const sky = uniforms(() => ({ uColor: color('red'), uTime: 0 }))
// A.tsx: creates the uniforms if they don't exist yet, per root
const { uColor } = useUniforms(sky)
// B.tsx: fully typed
const { uTime } = useUniforms(sky)
useFrame((state, delta) => { sky.from(state).uTime.value += delta })Why
Uniforms are looked up by string today ('player', 'uTime'), and a string can't carry a type. So reading a uniform in a file other than the one that created it needs a cast or an explicit schema, e.g. useUniforms<{ blur: number }>('fx'). Looking uniforms up by an imported object fixes that the way jotai atoms and TanStack Query's queryOptions do.
- No collisions. The object is the key, so two files can each define a
uColor. - Per-root nodes. Each root gets its own nodes, keyed by the root's store (
WeakMap) and set up through the planned root extension registry. - Devtools. The object can carry a debug name for the Inspector.
- Nothing breaks. String scopes and names keep working as the loose, dynamic path.
Context
This follows the planned extraction of the TSL hooks into @react-three/tsl, which adds typed global access through a Register interface. This issue is the import-based alternative for people who don't want to write a declare module. Related: #3769, #3887.
Source: pmndrs/react-three-fiber