Skip to content

Commit

Permalink
CSG2: Using Manifold for boolean operations (#15713)
Browse files Browse the repository at this point in the history
* Introducing the import from Module

* .

* Better

* Initial integration

* Integration done!

* oupsie

* Tests

* NGE

* Fix ES6

* Update packages/dev/core/src/Meshes/csg2.ts

Co-authored-by: Popov72 <[email protected]>

* Update packages/dev/core/src/Meshes/csg2.ts

Co-authored-by: Popov72 <[email protected]>

* Update packages/dev/core/src/Meshes/csg2.ts

Co-authored-by: Popov72 <[email protected]>

* Update packages/dev/core/src/Meshes/csg2.ts

Co-authored-by: Popov72 <[email protected]>

* Update packages/dev/core/src/Meshes/csg2.ts

Co-authored-by: Popov72 <[email protected]>

* Addressing feedback

* Factorize code

---------

Co-authored-by: Popov72 <[email protected]>
  • Loading branch information
deltakosh and Popov72 authored Oct 21, 2024
1 parent 9e61762 commit bb77918
Show file tree
Hide file tree
Showing 12 changed files with 671 additions and 14 deletions.
27 changes: 22 additions & 5 deletions packages/dev/core/src/Meshes/Node/Blocks/booleanGeometryBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { NodeGeometryBlockConnectionPointTypes } from "../Enums/nodeGeometryConn
import type { VertexData } from "../../mesh.vertexData";
import type { NodeGeometryBuildState } from "../nodeGeometryBuildState";
import { PropertyTypeForEdition, editableInPropertyPage } from "../../../Decorators/nodeDecorator";
import { CSG } from "core/Meshes/csg";
import { CSG2, InitializeCSG2Async, IsCSG2Ready } from "core/Meshes/csg2";
import type { Nullable } from "core/types";

/**
* Operations supported by the boolean block
Expand Down Expand Up @@ -43,6 +44,22 @@ export class BooleanGeometryBlock extends NodeGeometryBlock {
})
public operation = BooleanGeometryOperations.Intersect;

private _csg2LoadingPromise: Promise<void>;
/**
* @internal
*/
public override get _isReadyState(): Nullable<Promise<void>> {
if (IsCSG2Ready()) {
return null;
}

if (!this._csg2LoadingPromise) {
this._csg2LoadingPromise = InitializeCSG2Async();
}

return this._csg2LoadingPromise;
}

/**
* Create a new BooleanGeometryBlock
* @param name defines the block name
Expand Down Expand Up @@ -115,10 +132,10 @@ export class BooleanGeometryBlock extends NodeGeometryBlock {
vertexData1.colors = new Array<number>(vertexCount * 4);
}

const CSG0 = CSG.FromVertexData(vertexData0);
const CSG1 = CSG.FromVertexData(vertexData1);
const CSG0 = CSG2.FromVertexData(vertexData0);
const CSG1 = CSG2.FromVertexData(vertexData1);

let boolCSG: CSG;
let boolCSG: CSG2;

switch (this.operation) {
case BooleanGeometryOperations.Intersect:
Expand All @@ -128,7 +145,7 @@ export class BooleanGeometryBlock extends NodeGeometryBlock {
boolCSG = CSG0.subtract(CSG1);
break;
case BooleanGeometryOperations.Union:
boolCSG = CSG0.union(CSG1);
boolCSG = CSG0.add(CSG1);
break;
}

Expand Down
17 changes: 16 additions & 1 deletion packages/dev/core/src/Meshes/Node/nodeGeometry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ export class NodeGeometry {
}

/**
* Build the final geometry
* Build the final geometry. Please note that the geometry MAY not be ready until the onBuildObservable is raised.
* @param verbose defines if the build should log activity
* @param updateBuildId defines if the internal build Id should be updated (default is true)
* @param autoConfigure defines if the autoConfigure method should be called when initializing blocks (default is false)
Expand All @@ -245,6 +245,21 @@ export class NodeGeometry {
// Initialize blocks
this._initializeBlock(this.outputBlock, autoConfigure);

// Check async states
const promises: Promise<void>[] = [];
for (const block of this.attachedBlocks) {
if (block._isReadyState) {
promises.push(block._isReadyState);
}
}

if (promises.length) {
Promise.all(promises).then(() => {
this.build(verbose, updateBuildId, autoConfigure);
});
return;
}

// Build
const state = new NodeGeometryBuildState();

Expand Down
7 changes: 7 additions & 0 deletions packages/dev/core/src/Meshes/Node/nodeGeometryBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,13 @@ export class NodeGeometryBlock {
return null;
}

/**
* @internal
*/
public get _isReadyState(): Nullable<Promise<void>> {
return null;
}

/**
* Creates a new NodeGeometryBlock
* @param name defines the block name
Expand Down
1 change: 1 addition & 0 deletions packages/dev/core/src/Meshes/csg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ class Node {

/**
* Class for building Constructive Solid Geometry
* @deprecated Please use CSG2 instead
*/
export class CSG {
private _polygons = new Array<CSGPolygon>();
Expand Down
Loading

0 comments on commit bb77918

Please sign in to comment.