UNPKG

@polygonjs/polygonjs

Version:

node-based WebGL 3D engine https://polygonjs.com

82 lines (78 loc) 2.7 kB
/** * Creates a cube. * * */ 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 {CoreGroup} from '../../../core/geometry/Group'; 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 { /** @param size */ size = ParamConfig.FLOAT(1, { range: [0, 10], rangeLocked: [true, false], step, }); /** @param sizes */ sizes = ParamConfig.VECTOR3([1, 1, 1]); /** @param center */ center = ParamConfig.VECTOR3([0, 0, 0]); /** @param bevel */ rounded = ParamConfig.BOOLEAN(0); /** @param bevel radius */ roundedRadius = ParamConfig.FLOAT(0.1, { range: [0, 1], rangeLocked: [true, false], visibleIf: {rounded: 1}, }); /** @param bevel segments */ roundedSegments = ParamConfig.INTEGER(4, { range: [1, 8], rangeLocked: [true, false], visibleIf: {rounded: 1}, }); } const ParamsConfig = new CSGBoxSopParamsConfig(); export class CSGBoxSopNode extends CSGSopNode<CSGBoxSopParamsConfig> { override paramsConfig = ParamsConfig; static override type() { return SopType.CSG_BOX; } private _center: maths.vec3.Vec3 = [0, 0, 0]; private _sizes: maths.vec3.Vec3 = [0, 0, 0]; override cook(inputCoreGroups: CoreGroup[]) { vector3ToCsgVec3(this.pv.center, this._center); vector3ToCsgVec3(this.pv.sizes, this._sizes); csgVec3MultScalar(this._sizes, this.pv.size); try { const cuboidOptions: primitives.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: primitives.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([]); } } }