UNPKG

@polygonjs/polygonjs

Version:

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

53 lines (48 loc) 1.5 kB
/** * Creates CSG polygons. * * */ import {CSGSopNode} from './_BaseCSG'; import {NodeParamsConfig, ParamConfig} from '../utils/params/ParamsConfig'; import {CoreGroup} from '../../../core/geometry/Group'; import {SopType} from '../../poly/registers/nodes/types/Sop'; import type {maths} from '@jscad/modeling'; import {primitives} from '@jscad/modeling'; import {csgVec2sToJSON} from '../../../core/geometry/modules/csg/math/CsgMathVec2'; const {polygon} = primitives; const DEFAULT_POINTS: maths.vec2.Vec2[] = [ [-1, -1], [-1, 1], [1, 1], [1, -1], ]; const DEFAULT_PATHS: Array<Array<number>> = [[0, 1, 2, 3]]; class CSGPolygonSopParamsConfig extends NodeParamsConfig { /** @param points */ points = ParamConfig.STRING(JSON.stringify(csgVec2sToJSON(DEFAULT_POINTS))); /** @param paths */ paths = ParamConfig.STRING(JSON.stringify(DEFAULT_PATHS)); } const ParamsConfig = new CSGPolygonSopParamsConfig(); export class CSGPolygonSopNode extends CSGSopNode<CSGPolygonSopParamsConfig> { override paramsConfig = ParamsConfig; static override type() { return SopType.CSG_POLYGON; } override cook(inputCoreGroups: CoreGroup[]) { 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([]); } } }