R3f Geometry

enzed/r3f-skills/skills/r3f-geometry

作者 enzed4a11805f0973无许可证128 个星标收录于 2026年10月8日更新于 2026年10月8日仓库5周前更新

Build React Three Fiber geometry, custom buffers, instanced meshes, points, and lines. Use for shape construction and geometry draw-call optimization, rather than material appearance.

AI 生成的概览

指导构建 React Three Fiber 几何体、自定义缓冲区、实例化网格、点与线。

功能
提供在 React Three Fiber 中构建形状的指导:原生 JSX 几何体、自定义 BufferGeometry 与缓冲区属性、实例化网格、点、线、文本和边缘。内容涵盖通过实例化优化绘制调用、缓冲区更新与释放规则、法线、UV 通道以及边界重算。还包含一份关于原生实例化的参考文件,适用于规模大或频繁更新的集合。
适用场景
适用于在 React Three Fiber 场景中构建或优化 3D 形状,尤其是在原生几何体、自定义缓冲区和实例化之间做选择时。也适合排查几何体更新、剔除、射线检测或资源释放问题。
运行要求
需要一个已安装 Three.js 和 Drei 的 React Three Fiber 项目;代理应先检查已安装的版本。不附带脚本,包含一份参考文档。

React Three Fiber geometry

Inspect installed Three.js, Fiber, and Drei versions before copying constructor arguments or helper props. Examples target Fiber 9 / React 19 and Three.js r185.

Choose the representation

  • Use native JSX geometry for ordinary shapes. args match the Three.js constructor; changing them reconstructs geometry, so animate transforms rather than constructor inputs.
  • Use BufferGeometry for custom topology and buffer-based points for large particle sets. Keep generated arrays stable and use deterministic generation when results must be reproducible.
  • Use Drei Instances for convenient declarative instances with events. For large or frequently updated sets, read native instancing [blocked] to avoid per-instance React overhead.
  • Instancing shares geometry/material and reduces draw calls. Merely sharing a geometry among separate meshes does not batch their draws.
  • Drei Merged creates instancing abstractions from meshes, not BufferGeometry objects; it does not concatenate arbitrary static geometry. For actual merging, inspect mergeGeometries from three/addons/utils/BufferGeometryUtils.js and ensure compatible attributes/indexing.

Declarative instances

Mount below Canvas with lighting. Each Instance belongs to its nearest Instances provider.

tsx
import { Instance, Instances } from '@react-three/drei'
export default function Example() {  return (    <Instances limit={3} range={3}>      <boxGeometry args={[0.7, 0.7, 0.7]} />      <meshStandardMaterial />      <Instance position={[-1.2, 0, 0]} color="coral" />      <Instance position={[0, 0, 0]} color="skyblue" />      <Instance position={[1.2, 0, 0]} color="gold" />    </Instances>  )}

Custom buffers and updates

  • position and normal attributes usually have item size 3; UVs have item size 2. Construct JSX buffer attributes with args={[typedArray, itemSize]} and the correct attach, not just array/count props with no constructor arguments.
  • Indices refer to vertices; winding determines the front face. Duplicate vertices at hard normals or UV seams. Indexed vertices share all attributes, not just positions.
  • Lit geometry needs normals. Use computeVertexNormals() when appropriate; do not recompute every frame if a shader or analytic normals can express the deformation more cheaply.
  • After CPU buffer writes, set attribute.needsUpdate = true. Choose dynamic usage before the first GPU upload when buffers will change often.
  • Recompute bounding boxes/spheres after geometry changes that affect them. GPU vertex displacement does not update CPU bounds or raycasting automatically.
  • UV selection is explicit on modern Three.js: texture.channel selects uv, uv1, uv2, or uv3. Do not unconditionally copy uv into uv2 for AO.
  • Avoid rebuilding buffers for pointer movement. If topology is fixed, change attribute contents or uniforms instead.

Helpers and tradeoffs

  • Drei Line supports useful line widths; native WebGL line width is limited by the platform. Check worldUnits when choosing screen-space versus world-space thickness.
  • Use Drei Text for flat text and Text3D for extruded geometry. Current TextGeometry uses depth, not the historical height; verify the helper's installed types and font format.
  • Use Edges for sharp-edge outlines, not as a replacement for screen-space selection effects.
  • Bounds and Center change framing/transforms; decide whether they should update after asset loading, resizing, or interaction.
  • Segment count should follow silhouette, deformation, and viewing distance. A “high quality” fixed count is not universally better.
  • Declaratively created geometry can be owned by R3F. Shared or cached geometry needs a shared lifetime; do not dispose it from one instance while others remain mounted.

Verify

Check bounds/culling after updates, raycast hit positions, resource cleanup, and draw calls. Test at the intended object count; three instances do not establish performance at ten thousand.

Sources

来源与署名

来源:enzed/r3f-skills位于skills/r3f-geometry提交4a11805

许可证: 无许可证

内容归原作者所有。SourceWeft 从公开仓库中收录这些内容。

举报或申请下架