GithubHelp home page GithubHelp logo

edoardoo / three-csg-ts Goto Github PK

View Code? Open in Web Editor NEW

This project forked from samalexander/three-csg-ts

0.0 1.0 0.0 1.04 MB

CSG library for use with THREE.js

Home Page: https://www.npmjs.com/package/three-csg-ts

License: MIT License

TypeScript 100.00%

three-csg-ts's Introduction

three-csg-ts

CSG (Constructive Solid Geometry) library for three.js with Typescript support.

This is a typescript rewrite of THREE-CSGMesh.

Example

Concept

CSG is the name of a technique for generating a new geometry as a function of two input geometries.

CSG is sometimes referred to as "Boolean" operators in 3d modelling packages.

Internally it uses a structure called a BSP (binary space partitioning) tree to carry out these operations.

The supported operations are .subtract, .union, and .intersect.

By using different combinations of these 3 operations, and changing the order of the input models, you can construct any combination of the input models.

Installation

  • Install with npm npm i -save three-csg-ts
  • Install with yarn yarn add three-csg-ts

Example usage

import * as THREE from 'three';
import { CSG } from 'three-csg-ts';

// Make 2 box meshes..
const meshA = new THREE.Mesh(new THREE.BoxGeometry(1,1,1), new THREE.MeshNormalMaterial());
const meshB = new THREE.Mesh(new THREE.BoxGeometry(1,1,1));

// Offset one of the boxes by half its width..
meshB.position.add(new THREE.Vector3(0.5, 0.5, 0.5));

// Make sure the .matrix of each mesh is current
meshA.updateMatrix();
meshB.updateMatrix();

// Create a bsp tree from each of the meshes
const bspA = CSG.fromMesh(meshA);
const bspB = CSG.fromMesh(meshB);

// Subtract one bsp from the other via .subtract... other supported modes are .union and .intersect
const bspResult = bspA.subtract(bspB);

// Get the resulting mesh from the result bsp
const meshResult = CSG.toMesh(bspResult, meshA.matrix);

// Set the results material to the material of the first cube.
meshResult.material = meshA.material;

three-csg-ts's People

Contributors

samalexander avatar xawill avatar

Watchers

James Cloos 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.