R3F v10 Alpha 6
在巴黎会议的反馈之后,我们决定让 v10 对 R3F 的某些核心部分进行彻底重构,以便更好地与 Web 及 three.js 生态系统保持一致。Alpha 6 也是迄今为止规模最大的 Alpha 版本,自 Alpha 5 以来已提交了超过 200 次代码变更。
新的渲染器(WebGPURenderer)已成为默认选项
在 Alpha 5 中,新渲染器需通过属性标签手动启用:``,因此不会破坏现有功能。社区反馈认为,鉴于 WebGPU 对 Web 未来的重要性,且 three.js 本身已将重心转向 WebGPU 与 TSL,大版本更新正是做出这一决断的时机。节点材质、计算着色器以及渲染管线仅在新渲染器中生效。我们希望 v10 的默认配置能与 three.js 及整个 Web 技术栈的发展方向保持一致。
重要提示 新渲染器(WebGPURenderer)以 WebGPU 为标准,但也支持配置或自动降级至 WebGL2。许多使用 TSL 编写的功能仍可正常运行。这并非单纯的 WebGPU 与 WebGL 之争,而只是旧版“仅支持 WebGL”的渲染器与新版“具备更多现代特性”的渲染器之间的区别。
如何切换: 如果您使用了自定义 GLSL,或依赖基于 GLSL 着色器构建的旧版库与工具,只需修改一处导入语句:
diff - import { Canvas } from '@react-three/fiber' + import { Canvas } from '@react-three/fiber/legacy'
仅此一次更改即可。Hooks 和库仍从 @react-three/fiber 导入,因为所有入口共享同一套核心代码。如果配置错误,R3F 会明确提示您。在 WebGPU 画布上使用 GLSL 材质时,控制台会输出指向 /legacy 的警告信息。在 /legacy 画布上使用节点材质时,系统会抛出明确的错误提示,而非在第一帧直接崩溃。在默认导入中使用 gl={...} 同样会触发报错,并指引您前往 /legacy。
随着 WebGPU 成为默认选项,@react-three/fiber/webgpu 已不再必要。它目前仍可运行,但已被标记为弃用,将在首个 Beta 版本中移除。
## 按需加载渲染器
R3F 的核心代码现已完全解耦,不再直接引入 three.js。每个渲染器都独立存在于自己的小型模块中,默认导入仅在首次挂载 `` 时才会加载 WebGPU。
设计初衷: 若将两个渲染器打包在一起,极易导致两者同时被引入您的项目包中。现在,使用 /legacy 的应用仅包含 WebGLRenderer,而采用默认导入的应用仅包含 WebGPURenderer(后者同样支持降级至 WebGL)。无论您选择哪个入口,只要库从 @react-three/fiber 导入 Hooks,就不会向您的应用额外添加渲染器或第二份 R3F 副本。我们在每次构建时都会进行验证:CI 流程会使用 Vite 和 esbuild 打包示例应用,并确认各应用实际包含的渲染器类型。
类型定义跟随导入路径 从 @react-three/fiber 导入的 useThree、useFrame 和 onCreated 均针对 WebGPURenderer 进行了类型定义,因此 state.renderer.compute(...) 可直接调用,无需强制类型转换。而从 /legacy 导入时,它们则针对 WebGLRenderer 进行类型定义。在此之前,这些 Hooks 的类型定义兼容两种渲染器,导致使用仅支持 WebGPU 的成员变量时,必须手动进行类型转换或额外配置。
支持双渲染器的库可从新的 @react-three/fiber/extension 入口导入。
TSL Hooks 独立为单独包:@react-three/tsl
useUniforms、useUniform、useNodes、useLocalNodes、useBuffers、useGPUStorage 与 useRenderPipeline 已从 @react-three/fiber/webgpu 迁移至 @react-three/tsl。修改导入路径后,除 useRenderPipeline(详见下文)外,其余用法均保持不变。
设计初衷:
这些 Hooks 专用于 WebGPU,且迭代频率高于 R3F 的其他模块。独立分包允许它们按自身节奏发布,同时避免对 WebGL 应用造成干扰。此外,这也是首个基于 @react-three/fiber/extension 构建的包。该扩展接口作为公开的 API 表面,可供任意库调用,足以证明扩展机制已切实可行。
该包引入了多项改进:
告别类型断言
通过名称读取的 Uniforms 与 Nodes 现已可直接在 TSL 中运行。此前这两行代码均需强制类型转换:
const { uTime, uColor } = useUniforms() material.colorNode = uColor.mul(sin(uTime))精确的类型定义(按需启用)
如需更严格的类型检查,可一次性完成 Uniforms 注册。此后各 Hook 及 state.uniforms 均能识别其名称与确切类型,拼写失误将直接触发类型错误。Nodes、Buffers 与 GPU Storage 亦遵循相同注册逻辑。此步骤为可选项:即便不注册,前述功能亦可正常运行。
declare module '@react-three/tsl' { interface Register { uniforms: typeof globalUniforms } }
<h3 id="sec-9">// anywhere in your app const { uTime } = useUniforms() uTime.value // number ```</h3>
### `useLocalNodes` 提供类似着色器 `useMemo` 的缓存机制
<pre>传入依赖数组后,若传入空数组 `[]`,则图结构仅会初始化一次。该 Hook 还会自动追踪实际读取的共享 Uniforms 与 Nodes,因此仅在相关依赖发生替换时才会触发重建。在此之前,只要在任意位置注册 Uniform,都会导致所有调用该 Hook 的图结构强制重建。</pre>
<pre><code>const { colorNode } = useLocalNodes(({ uniforms }) => ({ colorNode: uniforms.uColor.mul(sin(time)), }), [])</code></pre>
For more precision, register your uniforms once. Every hook and <code>state.uniforms</code> then knows their names and exact types, and a typo becomes a type error. Nodes, buffers and GPU storage register the same way. It's optional: everything above works without it.
<pre><code>// uniforms.ts
export const globalUniforms = { uTime: 0, uColor: new Color('hotpink') }
declare module '@react-three/tsl' {
interface Register {
uniforms: typeof globalUniforms
}
}
// anywhere in your app
const { uTime } = useUniforms()
uTime.value // number
</code></pre>
<h3 id="sec-11"><strong><code>useLocalNodes</code> works like <code>useMemo</code> for shaders.</strong></h3>
Give it a dependency array, and <code>[]</code> builds the graph once. It also tracks which shared uniforms and nodes it actually read, so it rebuilds only when one of those is replaced. Before, registering any uniform anywhere rebuilt every graph that used the hook.
<pre><code>const { colorNode } = useLocalNodes(({ uniforms }) => ({
colorNode: uniforms.uColor.mul(sin(time)),
}), [])
</code></pre>
It also handles nodes that go onto a three object, like fog. Return a function: the graph is built during render, then installed after commit, and the cleanup runs when the component unmounts. This used to take a placeholder return value plus a separate <code>useThree</code> and <code>useEffect</code>.
<pre><code>useLocalNodes(({ scene, uniforms }) => {
const fogNode = fog(uniforms.fogColor, rangeFogFactor(uniforms.near, uniforms.far))
return () => {
scene.fogNode = fogNode
return () => {
scene.fogNode = null
}
}
}, [])
</code></pre>
<h3 id="sec-12"><strong>Post-processing belongs to the component that sets it up.</strong></h3>
Mount a component with <code>useRenderPipeline</code> and the effect is on. Unmount it and the effect is gone. Before, the pipeline kept rendering until you called <code>reset()</code>, and StrictMode or Fast Refresh built a second scene pass.
<pre><code>function Bloom() {
useRenderPipeline(({ renderPipeline, passes }) => {
renderPipeline.outputNode = bloom(passes.scenePass.getTextureNode())
})
return null
}
// {bloomOn && <Bloom />} toggles the effect
</code></pre>
To keep an effect running after its component unmounts, pass <code>{ persist: true }</code> as the third argument.
<h2 id="sec-13">Multi-canvas is one prop</h2>
原文较长(约 12,047 字),完整内容请<a href="https://github.com/pmndrs/react-three-fiber/releases/tag/v10.0.0-alpha.6" rel="nofollow noopener noreferrer">浏览更多</a>。
