• Stars
    star
    1,744
  • Rank 25,562 (Top 0.6 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 12 years ago
  • Updated over 4 years ago

Reviews

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

Repository Details

Constructive solid geometry on meshes using BSP trees in JavaScript

csg.js

Constructive Solid Geometry (CSG) is a modeling technique that uses Boolean operations like union and intersection to combine 3D solids. This library implements CSG operations on meshes elegantly and concisely using BSP trees, and is meant to serve as an easily understandable implementation of the algorithm. All edge cases involving overlapping coplanar polygons in both solids are correctly handled.

Example usage:

var cube = CSG.cube();
var sphere = CSG.sphere({ radius: 1.3 });
var polygons = cube.subtract(sphere).toPolygons();

Documentation

Detailed documentation can be automatically generated using Docco.

Demos

Implementation Details

All CSG operations are implemented in terms of two functions, clipTo() and invert(), which remove parts of a BSP tree inside another BSP tree and swap solid and empty space, respectively. To find the union of a and b, we want to remove everything in a inside b and everything in b inside a, then combine polygons from a and b into one solid:

a.clipTo(b);
b.clipTo(a);
a.build(b.allPolygons());

The only tricky part is handling overlapping coplanar polygons in both trees. The code above keeps both copies, but we need to keep them in one tree and remove them in the other tree. To remove them from b we can clip the inverse of b against a. The code for union now looks like this:

a.clipTo(b);
b.clipTo(a);
b.invert();
b.clipTo(a);
b.invert();
a.build(b.allPolygons());

Subtraction and intersection naturally follow from set operations. If union is A | B, subtraction is A - B = ~(~A | B) and intersection is A & B = ~(~A | ~B) where ~ is the complement operator.

License

Copyright (c) 2011 Evan Wallace (http://madebyevan.com/), under the MIT license.

More Repositories

1

esbuild

An extremely fast bundler for the web
Go
36,786
star
2

glfx.js

An image effects library for JavaScript using WebGL
JavaScript
3,189
star
3

thumbhash

A very compact representation of an image placeholder
Swift
3,122
star
4

node-source-map-support

Adds source map support to node.js (for stack traces)
JavaScript
2,141
star
5

lightgl.js

A lightweight WebGL library
JavaScript
1,501
star
6

thinscript

A low-level programming language inspired by TypeScript
C
1,353
star
7

webgl-filter

An image editor in WebGL
JavaScript
1,024
star
8

webgl-water

WebGL Water Demo
JavaScript
958
star
9

theta

JavaScript
714
star
10

polywasm

JavaScript
621
star
11

kiwi

A schema-based binary format for efficiently encoding trees of data
C++
558
star
12

source-map-visualization

A simple visualization of source map data
JavaScript
459
star
13

glslx

A GLSL type checker, code formatter, and minifier for WebGL
HTML
391
star
14

skew

A web-first, cross-platform programming language with an optimizing compiler
JavaScript
371
star
15

webgl-path-tracing

Path tracing in GLSL using WebGL
JavaScript
331
star
16

fsm

Finite State Machine Designer
JavaScript
283
star
17

rapt

Robots Are People Too, a platformer in HTML5
C++
208
star
18

float-toy

Use this to build intuition for the IEEE floating-point format
JavaScript
148
star
19

sky

A text editor written in the Skew programming language
Objective-C++
136
star
20

indirectbuffer

A small library for out-of-heap memory in emscripten
C++
125
star
21

font-texture-generator

HTML
114
star
22

pbjs

A minimal implementation of Google Protocol Buffers for JavaScript
JavaScript
91
star
23

buddy-malloc

An implementation of buddy memory allocation
C
89
star
24

packer

Simple port of /packer/ by Dean Edwards to node.js
JavaScript
85
star
25

webgl-recorder

Record all WebGL calls from any app for playback later
JavaScript
79
star
26

socket.io-python

A socket.io bridge for Python
Python
68
star
27

glslx-vscode

GLSLX support in Visual Studio Code
TypeScript
64
star
28

tla-fuzzer

A fuzzer for various top-level await bundling strategies
JavaScript
49
star
29

node-flatbuffers

An implementation of FlatBuffers in pure JavaScript
JavaScript
47
star
30

unwind

Universal disassembler for Python bytecode (supports Python 2 and 3)
Python
46
star
31

esbuild-plugin-glslx

A plugin for esbuild that enables importing *.glslx files.
JavaScript
37
star
32

polyfrag

Geometry library that splits polyhedra using 3D planes
C++
37
star
33

emscripten-library-generator

A library generator for the emscripten compiler
JavaScript
36
star
34

gl4

A small library for working with OpenGL 4
C++
34
star
35

cs224final

Final project for CS 224: Interactive Computer Graphics
C++
34
star
36

figma-fill-rule-editor

A Figma plugin for editing fill rules
HTML
34
star
37

bitscript

An experimental language that compiles to JavaScript and C++
JavaScript
32
star
38

silvermath.js

A high-quality equation editor for JavaScript
JavaScript
31
star
39

banner

A python scraper for Banner, Brown University's course catalog
Python
26
star
40

package-json-browser-tests

JavaScript
25
star
41

bundler-esm-cjs-tests

JavaScript
23
star
42

gl

A small C++ library for working with OpenGL
C++
23
star
43

esoptimize

A JavaScript AST optimizer
JavaScript
18
star
44

wasm-lang

A hobby language that compiles to WebAssembly
TypeScript
18
star
45

OES_texture_float_linear-polyfill

A polyfill for OES_texture_float_linear (a WebGL extension)
JavaScript
17
star
46

node-terminal-notifier

A node API for notifications on Mac OS X Lion
JavaScript
17
star
47

node-spacemouse

An OS X driver for the 3dconnexion SpaceMouse in JavaScript
JavaScript
17
star
48

soda

A course browser for Brown University
JavaScript
17
star
49

mineverse

A little experiment, please ignore
JavaScript
17
star
50

decorator-tests

TypeScript
17
star
51

cppcodegen

Generates C++ code from a JSON AST
TypeScript
15
star
52

ride

A web-based IDE for ROS
JavaScript
15
star
53

simple_svg_parser

A small library for getting geometry out of an svg file
Python
14
star
54

jest-esbuild-demo

JavaScript
13
star
55

fast-closure-compiler

Make the Google Closure Compiler start faster
JavaScript
13
star
56

mobile-touchpads

Use one or more iPhones as remote touchpads in an HTML page
JavaScript
13
star
57

easyxml

Concise XML generation in Python
Python
13
star
58

webgl-vr-flight

Needs an iPhone 6 and Google Cardboard
HTML
10
star
59

uint8array-json-parser

JSON.parse() for Uint8Array to get around the maximum string size in V8
JavaScript
9
star
60

webgl-vr-editor

Makefile
9
star
61

oculus-rift-webgl

Oculus Rift experiments with WebGL
8
star
62

bend

A statically-typed programming language for the web
JavaScript
8
star
63

skew-nanojpeg

A port of the NanoJPEG library
HTML
7
star
64

rand.js

Super simple pseudo-random number generator in JavaScript
JavaScript
7
star
65

minisharp

An optimizing compiler from a subset of C# to JavaScript
C#
6
star
66

mdi-material-ui-inline

Makefile
5
star
67

glvr

GLUT-style bindings for the Oculus Rift
C
5
star
68

gccjs

A simple build tool that uses Google Closure Compiler
Java
5
star
69

skew-lang.org

HTML
5
star
70

skew-vscode

Skew support in Visual Studio Code
TypeScript
5
star
71

skew-imports

4
star
72

css-import-tests

CSS
4
star
73

js-destructuring-fuzzer

JavaScript
4
star
74

section_testing

A Rust library for section-style testing
Rust
4
star
75

dlmax

Python
3
star
76

head-tracker-window

HTML
3
star
77

browser-source-map-bugs

JavaScript
3
star
78

skew-atom

Syntax highlighting for Skew in the Atom editor
CoffeeScript
2
star
79

dome-calibration

Ignore this. I'm only using a public repository because I need a quick HTTPS website.
HTML
2
star
80

SimpleLibOVR

Simple C API for Oculus Rift orientation info
C++
2
star
81

entry-point-resolve-test

Testing what happens when you bundle a package name
Shell
1
star
82

skew-rapt

Makefile
1
star
83

clang-initializer-list-plugin

A plugin for clang that checks if a constructor is missing a POD field in its initializer list
C++
1
star
84

thin-ir

TypeScript
1
star