@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
49 lines (48 loc) • 1.45 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 { csgVec2sToJSON } from "../../../core/geometry/modules/csg/math/CsgMathVec2";
const { polygon } = primitives;
const DEFAULT_POINTS = [
[-1, -1],
[-1, 1],
[1, 1],
[1, -1]
];
const DEFAULT_PATHS = [[0, 1, 2, 3]];
class CSGPolygonSopParamsConfig extends NodeParamsConfig {
constructor() {
super(...arguments);
/** @param points */
this.points = ParamConfig.STRING(JSON.stringify(csgVec2sToJSON(DEFAULT_POINTS)));
/** @param paths */
this.paths = ParamConfig.STRING(JSON.stringify(DEFAULT_PATHS));
}
}
const ParamsConfig = new CSGPolygonSopParamsConfig();
export class CSGPolygonSopNode extends CSGSopNode {
constructor() {
super(...arguments);
this.paramsConfig = ParamsConfig;
}
static type() {
return SopType.CSG_POLYGON;
}
cook(inputCoreGroups) {
try {
const points = JSON.parse(this.pv.points);
const paths = JSON.parse(this.pv.paths);
const geo = polygon({
points,
paths
});
this.setCSGGeometry(geo);
} catch (err) {
const message = err instanceof Error ? err.message : "failed to create geometry";
this.states.error.set(message);
this.setCSGObjects([]);
}
}
}