@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
64 lines (63 loc) • 2.21 kB
JavaScript
;
import { CSGSopNode } from "./_BaseCSG";
import { NodeParamsConfig, ParamConfig } from "../utils/params/ParamsConfig";
import { SopType } from "../../poly/registers/nodes/types/Sop";
import { isBooleanTrue } from "../../../core/Type";
import { csgIsGeom3 } from "../../../core/geometry/modules/csg/CsgCoreType";
import { vector3ToCsgVec3 } from "../../../core/geometry/modules/csg/CsgVecToVector";
import { transforms, geometries } from "@jscad/modeling";
import { csgApplyTransform } from "../../../core/geometry/modules/csg/math/CsgMat4";
const { mirror } = transforms;
class CSGMirrorSopParamsConfig extends NodeParamsConfig {
constructor() {
super(...arguments);
/** @param origin */
this.origin = ParamConfig.VECTOR3([0, 0, 0]);
/** @param normal */
this.normal = ParamConfig.VECTOR3([0, 1, 0]);
/** @param invert */
this.invert = ParamConfig.BOOLEAN(1);
}
}
const ParamsConfig = new CSGMirrorSopParamsConfig();
export class CSGMirrorSopNode extends CSGSopNode {
constructor() {
super(...arguments);
this.paramsConfig = ParamsConfig;
this._origin = [0, 0, 0];
this._normal = [0, 0, 0];
}
static type() {
return SopType.CSG_MIRROR;
}
initializeNode() {
this.io.inputs.setCount(1);
}
cook(inputCoreGroups) {
const inputObjects = inputCoreGroups[0].csgObjects();
if (inputObjects && inputObjects.length != 0) {
vector3ToCsgVec3(this.pv.origin, this._origin);
vector3ToCsgVec3(this.pv.normal, this._normal);
const options = {
origin: this._origin,
normal: this._normal
};
const newGeometries = [];
for (const inputObject of inputObjects) {
const inputGeometry = inputObject.csgGeometry();
let newGeometry = mirror(options, inputGeometry);
csgApplyTransform(newGeometry);
if (csgIsGeom3(newGeometry)) {
if (isBooleanTrue(this.pv.invert)) {
const invertedGeometry = geometries.geom3.invert(newGeometry);
newGeometry = invertedGeometry;
}
}
newGeometries.push(newGeometry);
}
this.setCSGGeometries(newGeometries);
} else {
this.setCSGObjects([]);
}
}
}