@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
72 lines (71 loc) • 2.47 kB
JavaScript
"use strict";
import { CSGSopNode } from "./_BaseCSG";
import { NodeParamsConfig, ParamConfig } from "../utils/params/ParamsConfig";
import { SopType } from "../../poly/registers/nodes/types/Sop";
import { primitives, maths } from "@jscad/modeling";
import { vector3ToCsgVec3 } from "../../../core/geometry/modules/csg/CsgVecToVector";
import { CoreMath } from "../../../core/math/_Module";
const { cylinder, roundedCylinder } = primitives;
class CSGTubeSopParamsConfig extends NodeParamsConfig {
constructor() {
super(...arguments);
/** @param height */
this.height = ParamConfig.FLOAT(1, { range: [0, 10] });
/** @param radius */
this.radius = ParamConfig.FLOAT(1, { range: [0, 10] });
/** @param segments */
this.segments = ParamConfig.INTEGER(32, {
range: [4, 128],
rangeLocked: [true, false]
});
/** @param center */
this.center = ParamConfig.VECTOR3([0, 0, 0]);
/** @param rounded */
this.rounded = ParamConfig.BOOLEAN(0);
/** @param rounded radius */
this.roundedRadius = ParamConfig.FLOAT(0.1, {
range: [0, 1],
rangeLocked: [true, false],
visibleIf: { rounded: 1 }
});
}
}
const ParamsConfig = new CSGTubeSopParamsConfig();
export class CSGTubeSopNode extends CSGSopNode {
constructor() {
super(...arguments);
this.paramsConfig = ParamsConfig;
this._center = [0, 0, 0];
}
static type() {
return SopType.CSG_TUBE;
}
cook(inputCoreGroups) {
try {
vector3ToCsgVec3(this.pv.center, this._center);
const { height, radius, segments } = this.pv;
const cylinderOptions = {
center: this._center,
height,
radius,
segments
};
const createRoundedCylinder = () => {
const maxSize = Math.min(height * 0.5 - 2 * maths.constants.EPS, radius - 2 * maths.constants.EPS);
const minSize = 2 * maths.constants.EPS;
const roundRadius = CoreMath.clamp(this.pv.roundedRadius, minSize, maxSize);
const roundedCylinderOptions = {
...cylinderOptions,
roundRadius
};
return roundedCylinder(roundedCylinderOptions);
};
const geo = this.pv.rounded ? createRoundedCylinder() : cylinder(cylinderOptions);
this.setCSGGeometry(geo);
} catch (err) {
const message = err instanceof Error ? err.message : "failed to create geometry";
this.states.error.set(message);
this.setCSGObjects([]);
}
}
}