@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
52 lines (51 loc) • 1.61 kB
JavaScript
;
import { CSGSopNode } from "./_BaseCSG";
import { NodeParamsConfig, ParamConfig } from "../utils/params/ParamsConfig";
import { SopType } from "../../poly/registers/nodes/types/Sop";
import { primitives } from "@jscad/modeling";
import { csgVec3sToJSON } from "../../../core/geometry/modules/csg/math/CsgMathVec3";
const { polyhedron } = primitives;
const DEFAULT_POINTS = [
[-1, -1, -1],
[-1, -1, 1],
[1, 1, 1],
[1, 1, -1]
];
const DEFAULT_FACES = [[0, 1, 2, 3]];
class CSGPolyhedronSopParamsConfig extends NodeParamsConfig {
constructor() {
super(...arguments);
/** @param points */
this.points = ParamConfig.STRING(JSON.stringify(csgVec3sToJSON(DEFAULT_POINTS)));
/** @param paths */
this.faces = ParamConfig.STRING(JSON.stringify(DEFAULT_FACES));
/** @param outward */
this.outward = ParamConfig.BOOLEAN(1);
}
}
const ParamsConfig = new CSGPolyhedronSopParamsConfig();
export class CSGPolyhedronSopNode extends CSGSopNode {
constructor() {
super(...arguments);
this.paramsConfig = ParamsConfig;
}
static type() {
return SopType.CSG_POLYHEDRON;
}
cook(inputCoreGroups) {
try {
const points = JSON.parse(this.pv.points);
const faces = JSON.parse(this.pv.faces);
const geo = polyhedron({
points,
faces,
orientation: this.pv.outward ? "outward" : "inward"
});
this.setCSGGeometry(geo);
} catch (err) {
const message = err instanceof Error ? err.message : "failed to create geometry";
this.states.error.set(message);
this.setCSGObjects([]);
}
}
}