用途:在用户要求构建 Three.js 场景、搭建 React Three Fiber、用 useFrame 做网格动画或加载 GLTF/GLB 时,提供基于 R3F v9 与 pmndrs 生态的 3D 网页开发指导。
版本约束:R3F v9、drei v9+ 与 rapier v2 对应 React 19;three 需 r171+。React 18 项目应使用 fiber v8 搭配 rapier v1,不要混用版本线。
安装:新建 Vite 项目可执行 bun create vite my-scene -- --template react-ts,然后 cd my-scene、bun add three @react-three/fiber @react-three/drei、bun add -d @types/three、bun dev。现有 React 19 项目只需 bun add three @react-three/fiber @react-three/drei 和 @types/three。可选:bun add @react-three/rapier(物理)、bun add zustand(状态)、bun add leva(调试 GUI)。
场景设置要点:Canvas 默认 antialias 为 true、outputColorSpace 为 SRGBColorSpace、toneMapping 为 ACESFilmicToneMapping、ColorManagement 开启;应设置 dpr={[1, 2]} 限制设备像素比;阴影用 shadows="soft" 并给网格加 castShadow/receiveShadow;静态场景可用 frameloop="demand"。
主要能力:涵盖 Drei 助手(OrbitControls、ScrollControls、PresentationControls、KeyboardControls、Environment、Stage、ContactShadows、Float、Text、Html、useGLTF、useTexture、Instances/Instance、Detailed、PerformanceMonitor、MeshTransmissionMaterial);资源管线包括 useGLTF 加载与缓存、Suspense 包裹、useGLTF.preload,以及 gltfjsx 命令行 npx gltfjsx model.glb --transform --types --shadows 生成组件并压缩模型。
性能规则:移动端绘制调用少于 100,桌面端少于 300;useFrame 中不得 setState,应直接改 ref;动画用 delta 而非固定增量;onPointerMove 中改 ref;不在 useFrame 中读取响应式 store,而用 store.getState();不在渲染循环中条件挂载卸载,改用 visible;不在 useFrame 内新建 THREE.Vector3(),应在外部分配后用 .set() 复用;相同网格不要重复创建几何体与材质,用 useMemo 或 Instances 共享;动态创建的对象须在 useEffect 清理中调用 geometry.dispose() 与 material.dispose()。
限制:以上为资料中明确内容;未提供的功能、收费、价格与平台信息暂未提供。