• Stars
    star
    264
  • Rank 155,103 (Top 4 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 2 years ago
  • Updated 10 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

๐Ÿšง Constructive solid geometry for React

Demo Interactive Physics Instances Slicing

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).

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

function Cross() {
  return (
    <mesh>
      <meshStandardMaterial />
      <Geometry>

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.

        <Base scale={[2, 0.5, 0.5]}>
          <boxGeometry />
        </Base>

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

  • Subtraction subtracts the geometry from the previous
  • Addition adds the geometry to the previous
  • Intersection is the overlap between the geometry and the previous
  • Difference is the negative overlap between the geometry and the previous
        <Addition scale={[0.5, 2, 0.5]}>
          <boxGeometry />
        </Addition>
      <Geometry>
    </mesh>
  )
}

A more complex example

import * as CSG from '@react-three/csg'

function Shape() {
  return (
    <mesh>
      <meshNormalMaterial />
      {/** This will yield a regular THREE.BufferGeometry which needs to be paired with a mesh. */}
      <Geometry>
        {/** The chain begins with a base geometry, where all operations are carried out on. */}
        <Base geometry={bunnyGeometry} scale={1.5} position={[0, 0.5, 0]} />
        {/** Chain your boolean operations: Addition, Subtraction, Difference and Intersection. */}
        <Subtraction position={[-1, 1, 1]}>
          {/** Geometry can be set by prop or by child, just like any regular <mesh>. */}
          <sphereGeometry />
        </Subtraction>
        {/** Geometry is re-usable, form hierachies with previously created CSG geometries. */}
        <Addition position={[0, 0, -0.75]}>
          {/** Combining two boxes into a cross */}
          <Geometry>
            <Base geometry={boxGeometry} scale={[2, 0.5, 0.5]} />
            <Addition geometry={boxGeometry} scale={[0.5, 2, 0.5]} />
          </Geometry>
        </Addition>
        {/** You can deeply nest operations. */}
        <group position={[0.5, 1, 0.9]}>
          <Subtraction>
            <sphereGeometry args={[0.65, 32, 32]} />
          </Subtraction>
        </group>
      </Geometry>
    </mesh>
  )
}

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.

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

function Shape() {
  const csg = useRef()
  return (
    <mesh>
      <Geometry ref={csg}>
        <Base geometry={bunnyGeometry} />
        <PivotControls depthTest={false} anchor={[0, 0, 0]} onDrag={() => csg.current.update()}>
          <Subtraction geometry={sphereGeometry} />
        </PivotControls>

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.

const Shape = () => (
  <mesh>
    <Geometry>
      <Base geometry={bunnyGeometry} />
      <Cutter />

function Cutter() {
  const { update } = useCSG()
  return (
    <PivotControls onDrag={update}>
      <Subtraction>
        <boxGeometry />
      </Subtraction>
    </PivotControls>

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.

function Shape() {
  return (
    <mesh>
      <Geometry useGroups>
        <Base geometry={bunnyGeometry}>
          {/** The base material. Again it can be defined by prop or by child. */}
          <meshStandardMaterial />
        </Base>
        <Subtraction position={[-1, 1, 1]} material={metal}>
          {/** This cut-out will be blue. */ }
          <meshStandardMaterial color="blue" />
        </Subtraction>
        {/** etc. */}
        <Addition position={[1, -1, -1]} geometry={sphereGeometry} material={stone}>

Showing the operations

The following will make all operations visible.

function Shape() {
  return (
    <mesh>
      <Geometry showOperations>

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

function Shape() {
  return (
    <mesh>
      <Geometry>
        <Base geometry={bunnyGeometry} />
        <Addition geometry={carrotGeometry} showOperation />

API Types

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
}

More Repositories

1

zustand

๐Ÿป Bear necessities for state management in React
TypeScript
45,348
star
2

react-spring

โœŒ๏ธ A spring physics based React animation library
TypeScript
27,857
star
3

react-three-fiber

๐Ÿ‡จ๐Ÿ‡ญ A React renderer for Three.js
TypeScript
26,202
star
4

jotai

๐Ÿ‘ป Primitive and flexible state management for React
TypeScript
18,007
star
5

use-gesture

๐Ÿ‘‡Bread n butter utility for component-tied mouse/touch gestures in React and Vanilla Javascript.
TypeScript
8,861
star
6

valtio

๐Ÿ’Š Valtio makes proxy-state simple for React and Vanilla
TypeScript
8,738
star
7

drei

๐Ÿฅ‰ useful helpers for react-three-fiber
JavaScript
8,042
star
8

leva

๐ŸŒ‹ React-first components GUI
TypeScript
4,825
star
9

gltfjsx

๐ŸŽฎ Turns GLTFs into JSX components
JavaScript
4,251
star
10

use-cannon

๐Ÿ‘‹๐Ÿ’ฃ physics based hooks for @react-three/fiber
TypeScript
2,700
star
11

react-three-next

React Three Fiber, Threejs, Nextjs starter
JavaScript
2,370
star
12

postprocessing

A post processing library for three.js.
JavaScript
2,263
star
13

racing-game

๐ŸŽ Open source racing game developed by everyone willing
TypeScript
2,120
star
14

xr

๐Ÿคณ VR/AR for react-three-fiber
TypeScript
2,051
star
15

uikit

๐ŸŽจ user interfaces for react-three-fiber
TypeScript
2,048
star
16

react-three-flex

๐Ÿ’ช๐Ÿ“ฆ Flexbox for react-three-fiber
TypeScript
1,640
star
17

suspend-react

๐Ÿšฅ Async/await for React components
TypeScript
1,358
star
18

react-postprocessing

๐Ÿ“ฌ postprocessing for react-three-fiber
JavaScript
1,074
star
19

detect-gpu

Classifies GPUs based on their 3D rendering benchmark score allowing the developer to provide sensible default settings for graphically intensive applications.
TypeScript
1,017
star
20

react-three-rapier

๐Ÿคบ Rapier physics in React
TypeScript
1,005
star
21

lamina

๐Ÿฐ An extensible, layer based shader material for ThreeJS
TypeScript
1,005
star
22

its-fine

๐Ÿถ๐Ÿ”ฅ A collection of escape hatches for React.
TypeScript
978
star
23

react-use-measure

๐Ÿ™Œ Utility to measure view bounds
TypeScript
832
star
24

react-nil

โƒ A react null renderer
TypeScript
785
star
25

maath

๐Ÿชถ Math helpers for the rest of us
TypeScript
783
star
26

threejs-journey

โš›๏ธ Bruno Simons journey demos in React
TypeScript
718
star
27

three-stdlib

๐Ÿ“š Stand-alone library of threejs examples designed to run without transpilation in node & browser
JavaScript
651
star
28

react-three-editor

๐Ÿ”Œ A one of a kind scene editor that writes changes back into your code
TypeScript
615
star
29

react-three-a11y

โ™ฟ๏ธ Accessibility tools for React Three Fiber
TypeScript
534
star
30

ecctrl

๐Ÿ•น๏ธ A floating rigibody character controller
TypeScript
498
star
31

react-three-offscreen

๐Ÿ“บ Offscreen worker canvas for react-three-fiber
TypeScript
443
star
32

react-zdog

โšก๏ธ๐Ÿถ React bindings for zdog
JavaScript
441
star
33

drei-vanilla

๐Ÿฆ drei-inspired helpers for threejs
TypeScript
436
star
34

use-asset

๐Ÿ“ฆ A promise caching strategy for React Suspense
TypeScript
413
star
35

tunnel-rat

๐Ÿ€ Non gratum anus rodentum
TypeScript
329
star
36

react-three-lgl

๐Ÿ”† A React abstraction for the LGL Raycaster
TypeScript
262
star
37

gltf-react-three

Convert GLTF files to React Three Fiber Components
JavaScript
258
star
38

market

๐Ÿ“ฆ Download CC0 assets ready to use in your next 3D Project
JavaScript
250
star
39

component-material

๐Ÿงฉ Compose modular materials in React
TypeScript
160
star
40

env

๐Ÿ’„ An app to create, edit, and preview HDR environment maps in the browser
TypeScript
151
star
41

react-ogl

๐Ÿฆด A barebones react renderer for ogl.
TypeScript
150
star
42

use-p2

๐Ÿ‘‹๐Ÿ’ฃ 2d physics hooks for @react-three/fiber
TypeScript
144
star
43

react-spring-examples

JavaScript
139
star
44

react-three-gpu-pathtracer

โšก๏ธ A React abstraction for the popular three-gpu-pathtracer
TypeScript
132
star
45

react-three-lightmap

In-browser lightmap/AO baker for react-three-fiber and ThreeJS
TypeScript
127
star
46

cannon-es-debugger

Wireframe debugger for use with cannon-es https://github.com/react-spring/cannon-es
HTML
102
star
47

rafz

๐Ÿ’ One loop to frame them all.
TypeScript
96
star
48

docs

๐Ÿ–จ๏ธ mdx documentation generator for `pmndrs/*/docs` folders
TypeScript
91
star
49

assets

๐Ÿ“ฆ Importable base64 encoded CC0 assets
Makefile
91
star
50

swc-jotai

Rust
88
star
51

react-three-jolt

โšก Jolt physics in React
TypeScript
84
star
52

react-three-scissor

โœ‚ Multiple scenes, one canvas! WebGL Scissoring implementation for React Three Fiber.
TypeScript
79
star
53

eslint-plugin-valtio

An eslint plugin for better valtio experience
JavaScript
74
star
54

react-three-babel

๐Ÿ› A Babel plugin that automatically builds the extend catalogue of known native Three.js elements
TypeScript
60
star
55

react-spring.io

โœŒ๏ธ A spring physics based React animation library
TypeScript
56
star
56

r3f-website

Website for React Three Fiber
JavaScript
27
star
57

directed

A flexible, minimal scheduler written in TypeScript
TypeScript
24
star
58

market-assets

JavaScript
19
star
59

react-three-8thwall

JavaScript
17
star
60

drei-assets

JavaScript
16
star
61

discord

๐Ÿค– Poimandres Discord Bot
TypeScript
10
star
62

branding

TypeScript
7
star
63

market-assets-do

JavaScript
5
star
64

envinfo

Easily collect useful information for bug reports
JavaScript
4
star
65

leva-wg

1
star