@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
71 lines (70 loc) • 2.15 kB
JavaScript
"use strict";
import { CSGSopNode } from "./_BaseCSG";
import { NodeParamsConfig, ParamConfig } from "../utils/params/ParamsConfig";
import { SopType } from "../../poly/registers/nodes/types/Sop";
import { vector2ToCsgVec2 } from "../../../core/geometry/modules/csg/CsgVecToVector";
import { step } from "../../../core/geometry/modules/csg/CsgConstant";
import { primitives } from "@jscad/modeling";
const { circle } = primitives;
class CSGCircleSopParamsConfig extends NodeParamsConfig {
constructor() {
super(...arguments);
/** @param radius */
this.radius = ParamConfig.FLOAT(1, {
range: [0, 10],
rangeLocked: [true, false]
});
/** @param segments */
this.segments = ParamConfig.INTEGER(32, {
range: [4, 128],
rangeLocked: [true, false]
});
/** @param center */
this.center = ParamConfig.VECTOR2([0, 0]);
/** @param open */
this.open = ParamConfig.BOOLEAN(0);
/** @param start angle */
this.startAngle = ParamConfig.FLOAT(0, {
range: [0, 2 * Math.PI],
rangeLocked: [true, true],
step,
visibleIf: { open: 1 }
});
/** @param end angle */
this.endAngle = ParamConfig.FLOAT("$PI", {
range: [0, 2 * Math.PI],
rangeLocked: [true, true],
step,
visibleIf: { open: 1 }
});
}
}
const ParamsConfig = new CSGCircleSopParamsConfig();
export class CSGCircleSopNode extends CSGSopNode {
constructor() {
super(...arguments);
this.paramsConfig = ParamsConfig;
this._center = [0, 0];
}
static type() {
return SopType.CSG_CIRCLE;
}
cook(inputCoreGroups) {
vector2ToCsgVec2(this.pv.center, this._center);
try {
const { radius, startAngle, endAngle, segments, open } = this.pv;
const geo = circle({
center: this._center,
radius,
segments,
startAngle: open ? startAngle : 0,
endAngle: open ? endAngle : 0
});
this.setCSGGeometry(geo);
} catch (err) {
const message = err instanceof Error ? err.message : "failed to create geometry";
this.states.error.set(message);
this.setCSGObjects([]);
}
}
}