{"id":13773910,"url":"https://github.com/pmndrs/react-three-csg","last_synced_at":"2025-05-15T03:08:24.529Z","repository":{"id":46755361,"uuid":"515630529","full_name":"pmndrs/react-three-csg","owner":"pmndrs","description":"🚧 Constructive solid geometry for React","archived":false,"fork":false,"pushed_at":"2025-03-02T05:21:17.000Z","size":3081,"stargazers_count":331,"open_issues_count":11,"forks_count":14,"subscribers_count":7,"default_branch":"main","last_synced_at":"2025-04-30T00:23:35.170Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://csg.pmnd.rs","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pmndrs.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2022-07-19T14:59:06.000Z","updated_at":"2025-04-27T04:17:27.000Z","dependencies_parsed_at":"2024-01-13T11:57:52.276Z","dependency_job_id":"9decba7f-5013-4ff3-ae91-84046f451c85","html_url":"https://github.com/pmndrs/react-three-csg","commit_stats":{"total_commits":76,"total_committers":3,"mean_commits":"25.333333333333332","dds":"0.19736842105263153","last_synced_commit":"552f61aef0163e4d584ba3bc1e0408c66a74c126"},"previous_names":[],"tags_count":28,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pmndrs%2Freact-three-csg","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pmndrs%2Freact-three-csg/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pmndrs%2Freact-three-csg/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pmndrs%2Freact-three-csg/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pmndrs","download_url":"https://codeload.github.com/pmndrs/react-three-csg/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253134850,"owners_count":21859512,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-03T17:01:21.620Z","updated_at":"2025-05-15T03:08:24.451Z","avatar_url":"https://github.com/pmndrs.png","language":"TypeScript","funding_links":[],"categories":["Graphics"],"sub_categories":["Visual Animation Editor"],"readme":"\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://codesandbox.io/s/mlgzsc\"\u003e\u003cimg width=\"19%\" src=\"https://codesandbox.io/api/v1/sandboxes/mlgzsc/screenshot.png\" alt=\"Demo\"/\u003e\u003c/a\u003e\n  \u003ca href=\"https://codesandbox.io/s/y52tmt\"\u003e\u003cimg width=\"19%\" src=\"https://codesandbox.io/api/v1/sandboxes/y52tmt/screenshot.png\" alt=\"Interactive\"/\u003e\u003c/a\u003e\n  \u003ca href=\"https://codesandbox.io/s/mw0dtc\"\u003e\u003cimg width=\"19%\" src=\"https://codesandbox.io/api/v1/sandboxes/mw0dtc/screenshot.png\" alt=\"Physics\"/\u003e\u003c/a\u003e\n  \u003ca href=\"https://codesandbox.io/s/k3ly88\"\u003e\u003cimg width=\"19%\" src=\"https://codesandbox.io/api/v1/sandboxes/k3ly88/screenshot.png\" alt=\"Instances\"/\u003e\u003c/a\u003e\n  \u003ca href=\"https://codesandbox.io/s/k4qfiz\"\u003e\u003cimg width=\"19%\" src=\"https://codesandbox.io/api/v1/sandboxes/k4qfiz/screenshot.png\" alt=\"Slicing\"/\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n```shell\nyarn add @react-three/csg\n```\n\nConstructive solid geometry for React, a small abstraction around [gkjohnson/three-bvh-csg](https://github.com/gkjohnson/three-bvh-csg).\n\nBegin 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).\n\n```jsx\nimport { Geometry, Base, Addition, Subtraction, ReverseSubtraction, Intersection, Difference } from '@react-three/csg'\n\nfunction Cross() {\n  return (\n    \u003cmesh\u003e\n      \u003cmeshStandardMaterial /\u003e\n      \u003cGeometry\u003e\n```\n\nYou 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](#using-multi-material-groups)), you can also nest, group and transform them.\n\n```jsx\n        \u003cBase scale={[2, 0.5, 0.5]}\u003e\n          \u003cboxGeometry /\u003e\n        \u003c/Base\u003e\n```\n\nNow you chain your operations, as many as you like, but keep in mind that order matters. The following operations are available:\n\n- `Addition` adds the geometry to the previous\n- `Subtraction` subtracts the geometry from the previous\n- `ReverseSubtraction` subtracts the previous geometry from the geometry\n- `Intersection` is the overlap between the geometry and the previous\n- `Difference` is the negative overlap between the geometry and the previous\n\n```jsx\n        \u003cAddition scale={[0.5, 2, 0.5]}\u003e\n          \u003cboxGeometry /\u003e\n        \u003c/Addition\u003e\n      \u003cGeometry\u003e\n    \u003c/mesh\u003e\n  )\n}\n```\n\n#### A more complex example\n\n```jsx\nfunction Shape() {\n  return (\n    \u003cmesh\u003e\n      \u003cmeshNormalMaterial /\u003e\n      {/** This will yield a regular THREE.BufferGeometry which needs to be paired with a mesh. */}\n      \u003cGeometry\u003e\n        {/** The chain begins with a base geometry, where all operations are carried out on. */}\n        \u003cBase geometry={bunnyGeometry} scale={1.5} position={[0, 0.5, 0]} /\u003e\n        {/** Chain your boolean operations: Addition, Subtraction, Difference and Intersection. */}\n        \u003cSubtraction position={[-1, 1, 1]}\u003e\n          {/** Geometry can be set by prop or by child, just like any regular \u003cmesh\u003e. */}\n          \u003csphereGeometry /\u003e\n        \u003c/Subtraction\u003e\n        {/** Geometry is re-usable, form hierachies with previously created CSG geometries. */}\n        \u003cAddition position={[0, 0, -0.75]}\u003e\n          {/** Combining two boxes into a cross */}\n          \u003cGeometry\u003e\n            \u003cBase geometry={boxGeometry} scale={[2, 0.5, 0.5]} /\u003e\n            \u003cAddition geometry={boxGeometry} scale={[0.5, 2, 0.5]} /\u003e\n          \u003c/Geometry\u003e\n        \u003c/Addition\u003e\n        {/** You can deeply nest operations. */}\n        \u003cgroup position={[0.5, 1, 0.9]}\u003e\n          \u003cSubtraction\u003e\n            \u003csphereGeometry args={[0.65, 32, 32]} /\u003e\n          \u003c/Subtraction\u003e\n        \u003c/group\u003e\n      \u003c/Geometry\u003e\n    \u003c/mesh\u003e\n  )\n}\n```\n\n#### Updating the operations and runtime usage\n\nCall `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.\n\nThe following would allow the user to move a cutter around with the mouse.\n\n```jsx\nimport { PivotControls } from '@react-three/drei'\n\nfunction Shape() {\n  const csg = useRef()\n  return (\n    \u003cmesh\u003e\n      \u003cGeometry ref={csg}\u003e\n        \u003cBase geometry={bunnyGeometry} /\u003e\n        \u003cPivotControls depthTest={false} anchor={[0, 0, 0]} onDrag={() =\u003e csg.current.update()}\u003e\n          \u003cSubtraction geometry={sphereGeometry} /\u003e\n        \u003c/PivotControls\u003e\n```\n\n#### Using the context API\n\nThe `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.\n\n```jsx\nconst Shape = () =\u003e (\n  \u003cmesh\u003e\n    \u003cGeometry\u003e\n      \u003cBase geometry={bunnyGeometry} /\u003e\n      \u003cCutter /\u003e\n\nfunction Cutter() {\n  const { update } = useCSG()\n  return (\n    \u003cPivotControls onDrag={update}\u003e\n      \u003cSubtraction\u003e\n        \u003cboxGeometry /\u003e\n      \u003c/Subtraction\u003e\n    \u003c/PivotControls\u003e\n```\n\n#### Using multi-material groups\n\nWith 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.\n\n```jsx\nfunction Shape() {\n  return (\n    \u003cmesh\u003e\n      \u003cGeometry useGroups\u003e\n        \u003cBase geometry={bunnyGeometry}\u003e\n          {/** The base material. Again it can be defined by prop or by child. */}\n          \u003cmeshStandardMaterial /\u003e\n        \u003c/Base\u003e\n        \u003cSubtraction position={[-1, 1, 1]} material={metal}\u003e\n          {/** This cut-out will be blue. */ }\n          \u003cmeshStandardMaterial color=\"blue\" /\u003e\n        \u003c/Subtraction\u003e\n        {/** etc. */}\n        \u003cAddition position={[1, -1, -1]} geometry={sphereGeometry} material={stone}\u003e\n```\n\n#### Showing the operations\n\nThe following will make all operations visible.\n\n```jsx\nfunction Shape() {\n  return (\n    \u003cmesh\u003e\n      \u003cGeometry showOperations\u003e\n```\n\nWhereas if you want to show only a single operation, you can do so by setting the `showOperation` prop on the root.\n\n```jsx\nfunction Shape() {\n  return (\n    \u003cmesh\u003e\n      \u003cGeometry\u003e\n        \u003cBase geometry={bunnyGeometry} /\u003e\n        \u003cAddition geometry={carrotGeometry} showOperation /\u003e\n```\n\n### API Types\n\n```tsx\nexport type CSGGeometryProps = {\n  children?: React.ReactNode\n  /** Use material groups, each operation can have its own material, default: false */\n  useGroups?: boolean\n  /** Show operation meshes, default: false */\n  showOperations?: boolean\n  /** Re-compute vertx normals, default: false */\n  computeVertexNormals?: boolean\n}\n\nexport type CSGGeometryApi = {\n  computeVertexNormals: boolean\n  showOperations: boolean\n  useGroups: boolean\n  update: () =\u003e void\n}\n\nexport type CSGGeometryRef = CSGGeometryApi \u0026 {\n  geometry: THREE.BufferGeometry\n  operations: THREE.Group\n}\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpmndrs%2Freact-three-csg","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpmndrs%2Freact-three-csg","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpmndrs%2Freact-three-csg/lists"}