百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
R

react-three-csg

> 编程语言
开源

React 的构造实体几何

369 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

React 的构造实体几何

bash
yarn add @react-three/csg

Constructive solid geometry for React, a small abstraction around gkjohnson/three-bvh-csg.

Begin with a Geometry which is a regular THREE.BufferGeometry that you can pair with a mesh, or anything else that relies on geometry (physics rigid bodies etc).

javascript
import { Geometry, Base, Addition, Subtraction, ReverseSubtraction, Intersection, Difference } from '@react-three/csg'

function Cross() {
  return (
    
      
      

You must first give it a Base which is the geometry foundation for all ensuing operations. All operations within Geometry, including Base, behave like regular meshes. They all receive geometry (and optionally a material, see using-multi-material-groups), you can also nest, group and transform them.

javascript
        
          
        

Now you chain your operations, as many as you like, but keep in mind that order matters. The following operations are available:

  • Addition adds the geometry to the previous
  • Subtraction subtracts the geometry from the previous
  • ReverseSubtraction subtracts the previous geometry from the geometry
  • Intersection is the overlap between the geometry and the previous
  • Difference is the negative overlap between the geometry and the previous
javascript
        
          
        
      
    
  )
}

A more complex example

…

Updating the operations and runtime usage

Call update() on the main geometry to re-create it. Keep in mind that although the base CSG implementation is fast, this is something you may want to avoid doing often or runtime, depending on the complexity of your geometry.

The following would allow the user to move a cutter around with the mouse.

javascript
import { PivotControls } from '@react-three/drei'

function Shape() {
  const csg = useRef()
  return (
    
      
        
         csg.current.update()}>
          
        

Using the context API

The useCSG hook exposes the update function, which can be used to update the geometry. This is useful when you want to update the geometry from a child component.

javascript
const Shape = () => (
  
    
      
      

function Cutter() {
  const { update } = useCSG()
  return (
    
      
        
      
    

Using multi-material groups

With the useGroups prop you can instruct CSG to generate material groups. Thereby instead of ending up with a single clump of uniformly textured geometry you can, for instance, make cuts with different materials. Each operation now takes its own material! The resulting material group will be inserted into the mesh that carries the output operation.

javascript
function Shape() {
  return (
    
      
        
          {/** The base material. Again it can be defined by prop or by child. */}
          
        
        
          {/** This cut-out will be blue. */ }
          
        
        {/** etc. */}
        

Showing the operations

The following will make all operations visible.

javascript
function Shape() {
  return (
    
      

Whereas if you want to show only a single operation, you can do so by setting the showOperation prop on the root.

javascript
function Shape() {
  return (
    
      
        
        

API Types

typescript
export type CSGGeometryProps = {
  children?: React.ReactNode
  /** Use material groups, each operation can have its own material, default: false */
  useGroups?: boolean
  /** Show operation meshes, default: false */
  showOperations?: boolean
  /** Re-compute vertx normals, default: false */
  computeVertexNormals?: boolean
}

export type CSGGeometryApi = {
  computeVertexNormals: boolean
  showOperations: boolean
  useGroups: boolean
  update: () => void
}

export type CSGGeometryRef = CSGGeometryApi & {
  geometry: THREE.BufferGeometry
  operations: THREE.Group
}

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

TypeScript

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言