Home

Awesome

NOTICE

This project is abandoned! We've since switched over to Manifold.

Old README:

OctreeCSG-ea

A stripped down engine-agnostic version of the OctreeCSG library, ported to Typescript. This is very work-in-progress.

Note that this project is not affiliated with the OctreeCSG project.

Because this is a stripped down version of the library, some features are missing, such as directly operating with meshes and assigning materials.

Table of Contents

Usage

OctreeCSG comes as a Javascript Module and can be imported with the following command:

import { OctreeCSG } from 'octreecsg-ea';

This assumes that the library was installed as a node module. If not, replace octreecsg-ea with a full path to the bundle.

Note that if asynchronous operations are used, then the job dispatcher should be created with:

OctreeCSGJobDispatcher.create('/OctreeCSG-ea.worker.min.js', 2, 1000);

This only has to be done once, and further calls will be ignored. The first argument is the path to the web worker script, the second argument is the size of the worker pool (how many web workers should be created), and the third argument is the timeout in milliseconds for the creation of each worker (if this time is exceeded, then the worker fails to be created). The worker script must be copied to the root of the domain.

If the job dispatcher is not created, or fails to be created, then the library will still work, but asynchronous operations will simply act as synchronous operations wrapped in a promise (operations will not be done in a separate thread), which may introduce stuttering to a game that is using this library.

Operations

OctreeCSG.union:

Merges two Octrees (octreeA and octreeB) to one Octree

ParameterDescription
octreeAFirst octree object
octreeBSecond octree object

OctreeCSG.subtract:

Subtracts octreeB from octreeA and returns the result Octree

ParameterDescription
octreeAFirst octree object
octreeBSecond octree object

OctreeCSG.intersect:

Returns the intersection of octreeA and octreeB

ParameterDescription
octreeAFirst octree object
octreeBSecond octree object

OctreeCSG.operation

CSG Hierarchy of Operations (syntax may change), provides a simple method to combine several CSG operations into one

ParameterDescription
objInput object with the CSG hierarchy
returnOctrees(Optional) Specifies whether to return the Octrees as part of the result or not (true / false). Default: false

Input object structure:

KeyExpected Value
opType of operation to perform as string, options: union, subtract and intersect
material(Optional) Used only in the root level of the object, if a material is provided the returned object will be a three.js mesh instead of an Octree. Value can be a single material or an array of materials
objAFirst object, can be a three.js mesh, Octree or a sub-structure of the CSG operation
objBSecond object, can be a three.js mesh, Octree or a sub-structure of the CSG operation

Array Operations

OctreeCSG provides 3 methods to perform CSG operations on an array of meshes / octrees

ParameterDescription
objArrAn array of meshes or octrees to perform the CSG operation on

List of Methods:

Asynchronous Operations

OctreeCSG provides asynchronous CSG methods for all the advanced CSG operations.

List of Methods:

OctreeCSG Flags

The following flags and variables control how OctreeCSG operates.

Flag / VariableDefault ValueDescription
OctreeCSG.useWindingNumberfalseDetermines if to use the ray-triangle intersection algorithm or the Winding number algorithm. The Winding number alogirthm can be more accurate than the ray-triangle algorithm on some occasions at the cost of performance. Options: true, false
OctreeCSG.maxLevel16Maximum number of sub-Octree levels in the tree
OctreeCSG.polygonsPerTree100Minimum number of polygons (triangles) in a sub-Octree before a split is needed

Limitations

The current worker script size is huge due to browser and esbuild limitations. Ideally, the worker script would share code with the rest of the library, however, esbuild code splitting is only available for ES6 modules, but module workers are not yet supported on Firefox.

As a workaround, the worker is bundled with the library and gl-matrix as an IIFE, which increases the size of the worker script dramatically.

Resources