UNPKG

@polygonjs/polygonjs

Version:

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

74 lines (73 loc) 2.38 kB
"use strict"; import { CSGSopNode } from "./_BaseCSG"; import { NodeParamsConfig, ParamConfig } from "../utils/params/ParamsConfig"; import { Matrix4 } from "three"; import { vector3ToCsgVec3 } from "../../../core/geometry/modules/csg/CsgVecToVector"; import { csgApplyMatrix4 } from "../../../core/geometry/modules/csg/math/CsgMat4"; import { SopType } from "../../poly/registers/nodes/types/Sop"; import { primitives } from "@jscad/modeling"; const { sphere, geodesicSphere } = primitives; class CSGSphereSopParamsConfig extends NodeParamsConfig { constructor() { super(...arguments); /** @param radius */ this.radius = ParamConfig.FLOAT(1, { range: [0, 10] }); /** @param center */ this.center = ParamConfig.VECTOR3([0, 0, 0]); /** @param geodesic */ this.geodesic = ParamConfig.BOOLEAN(0); /** @param segments */ this.segments = ParamConfig.INTEGER(16, { range: [4, 128], rangeLocked: [true, false], visibleIf: { geodesic: 0 } }); /** @param frequency */ this.frequency = ParamConfig.INTEGER(4, { range: [1, 32], rangeLocked: [true, false], visibleIf: { geodesic: 1 } }); } /** @param axes */ // axes = ParamConfig.VECTOR3([0, 1, 0]); } const ParamsConfig = new CSGSphereSopParamsConfig(); export class CSGSphereSopNode extends CSGSopNode { constructor() { super(...arguments); this.paramsConfig = ParamsConfig; this._center = [0, 0, 0]; this._matrix4 = new Matrix4(); } static type() { return SopType.CSG_SPHERE; } // private _axes: maths.vec3.Vec3 = [0, 1, 0]; cook(inputCoreGroups) { vector3ToCsgVec3(this.pv.center, this._center); const geo = this.pv.geodesic ? this._createGeodesicSphere() : this._createSphere(); this.setCSGGeometry(geo); } _createSphere() { return sphere({ center: this._center, radius: this.pv.radius, segments: this.pv.segments // axes: this._axes, }); } _createGeodesicSphere() { const geo = geodesicSphere({ radius: this.pv.radius, frequency: this.pv.frequency * 6 // mult by 6 here to make it more intuitive }); if (this.pv.center.length() > 0) { this._matrix4.identity(); this._matrix4.makeTranslation(this.pv.center.x, this.pv.center.y, this.pv.center.z); csgApplyMatrix4(geo, this._matrix4); } return geo; } }