@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
52 lines (51 loc) • 1.68 kB
JavaScript
;
import { CSGSopNode } from "./_BaseCSG";
import { NodeParamsConfig, ParamConfig } from "../utils/params/ParamsConfig";
import { SopType } from "../../poly/registers/nodes/types/Sop";
import { primitives } from "@jscad/modeling";
import { vector2ToCsgVec2 } from "../../../core/geometry/modules/csg/CsgVecToVector";
import { Vector2 } from "three";
const { line } = primitives;
const _lastPt = new Vector2();
const _pos = new Vector2();
class CSGLineSopParamsConfig extends NodeParamsConfig {
constructor() {
super(...arguments);
/** @param length */
this.length = ParamConfig.FLOAT(1, { range: [0, 10] });
/** @param points count */
this.pointsCount = ParamConfig.INTEGER(2, {
range: [2, 128],
rangeLocked: [true, false]
});
/** @param origin */
this.origin = ParamConfig.VECTOR2([0, 0]);
/** @param direction */
this.direction = ParamConfig.VECTOR2([1, 0]);
}
}
const ParamsConfig = new CSGLineSopParamsConfig();
export class CSGLineSopNode extends CSGSopNode {
constructor() {
super(...arguments);
this.paramsConfig = ParamsConfig;
}
static type() {
return SopType.CSG_LINE;
}
cook(inputCoreGroups) {
const pointsCount = this.pv.pointsCount;
const vec2s = new Array(pointsCount);
_lastPt.copy(this.pv.direction).normalize().multiplyScalar(this.pv.length);
for (let i = 0; i < pointsCount; i++) {
const i_n = i / (pointsCount - 1);
_pos.copy(_lastPt).multiplyScalar(i_n);
_pos.add(this.pv.origin);
const vec2 = [0, 0];
vec2s[i] = vec2;
vector2ToCsgVec2(_pos, vec2);
}
const geo = line(vec2s);
this.setCSGGeometry(geo);
}
}