@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
80 lines (79 loc) • 2.79 kB
JavaScript
"use strict";
import { CSGSopNode } from "./_BaseCSG";
import { NodeParamsConfig, ParamConfig } from "../utils/params/ParamsConfig";
import { vector3ToCsgVec3 } from "../../../core/geometry/modules/csg/CsgVecToVector";
import { csgVec3MultScalar } from "../../../core/geometry/modules/csg/math/CsgMathVec3";
import { SopType } from "../../poly/registers/nodes/types/Sop";
import { step } from "../../../core/geometry/modules/csg/CsgConstant";
import { CoreMath } from "../../../core/math/_Module";
import { primitives, maths } from "@jscad/modeling";
const { cuboid, roundedCuboid } = primitives;
class CSGBoxSopParamsConfig extends NodeParamsConfig {
constructor() {
super(...arguments);
/** @param size */
this.size = ParamConfig.FLOAT(1, {
range: [0, 10],
rangeLocked: [true, false],
step
});
/** @param sizes */
this.sizes = ParamConfig.VECTOR3([1, 1, 1]);
/** @param center */
this.center = ParamConfig.VECTOR3([0, 0, 0]);
/** @param bevel */
this.rounded = ParamConfig.BOOLEAN(0);
/** @param bevel radius */
this.roundedRadius = ParamConfig.FLOAT(0.1, {
range: [0, 1],
rangeLocked: [true, false],
visibleIf: { rounded: 1 }
});
/** @param bevel segments */
this.roundedSegments = ParamConfig.INTEGER(4, {
range: [1, 8],
rangeLocked: [true, false],
visibleIf: { rounded: 1 }
});
}
}
const ParamsConfig = new CSGBoxSopParamsConfig();
export class CSGBoxSopNode extends CSGSopNode {
constructor() {
super(...arguments);
this.paramsConfig = ParamsConfig;
this._center = [0, 0, 0];
this._sizes = [0, 0, 0];
}
static type() {
return SopType.CSG_BOX;
}
cook(inputCoreGroups) {
vector3ToCsgVec3(this.pv.center, this._center);
vector3ToCsgVec3(this.pv.sizes, this._sizes);
csgVec3MultScalar(this._sizes, this.pv.size);
try {
const cuboidOptions = {
center: this._center,
size: this._sizes
};
const createRoundedCuboid = () => {
const maxSize = Math.min(this._sizes[0], this._sizes[1], this._sizes[2]) * 0.5 - 2 * maths.constants.EPS;
const minSize = 2 * maths.constants.EPS;
const roundRadius = CoreMath.clamp(this.pv.roundedRadius, minSize, maxSize);
const roundedCuboidOptions = {
...cuboidOptions,
roundRadius,
segments: this.pv.roundedSegments * 4
};
return roundedCuboid(roundedCuboidOptions);
};
const geo = this.pv.rounded ? createRoundedCuboid() : cuboid(cuboidOptions);
this.setCSGGeometry(geo);
} catch (err) {
const message = err instanceof Error ? err.message : "failed to create geometry";
this.states.error.set(message);
this.setCSGObjects([]);
}
}
}