Giter Site home page Giter Site logo

sam-apostel / react-three-csg Goto Github PK

View Code? Open in Web Editor NEW

This project forked from pmndrs/react-three-csg

0.0 0.0 0.0 603 KB

๐Ÿšง Constructive solid geometry for React

Home Page: https://csg.pmnd.rs

License: MIT License

JavaScript 8.99% TypeScript 86.18% CSS 4.83%

react-three-csg's Introduction

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
}

react-three-csg's People

Contributors

drcmda avatar github-actions[bot] avatar isaac-mason avatar sam-apostel avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.