UNPKG

@polygonjs/polygonjs

Version:

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

73 lines (72 loc) 2.65 kB
"use strict"; import { BaseSopOperation } from "./_Base"; import { BufferGeometry, CatmullRomCurve3, Float32BufferAttribute, Vector3 } from "three"; import { InputCloneMode } from "../../../engine/poly/InputCloneMode"; import { ObjectType } from "../../../core/geometry/Constant"; import { SPLINE_CURVE_TYPES } from "../../../core/geometry/Curve"; import { pointsFromObject } from "../../../core/geometry/entities/point/CorePointUtils"; const EPSILON = 1e-3; const _points = []; export class CurveGetPointSopOperation extends BaseSopOperation { constructor() { super(...arguments); this._current = new Vector3(); this._next = new Vector3(); } static type() { return "curveGetPoint"; } cook(inputCoreGroups, params) { const inputCoreGroup = inputCoreGroups[0]; const objects = inputCoreGroup.allObjects(); const newObjects = []; for (let object of objects) { const newObject = this._createSplineFromCoreObject(object, params); if (newObject) { newObjects.push(newObject); } } return this.createCoreGroupFromObjects(newObjects); } _createSplineFromCoreObject(object, params) { const { t, closed, curveType, tension, tTangent } = params; pointsFromObject(object, _points); if (_points.length < 2) { return; } const pointPositions = _points.map((p) => p.position(new Vector3())); const curveTypeName = SPLINE_CURVE_TYPES[curveType]; const curve = new CatmullRomCurve3(pointPositions, closed, curveTypeName, tension); const positions = new Array(3); curve.getPoint(t, this._current); this._current.toArray(positions, 0); const geometry = new BufferGeometry(); geometry.setAttribute("position", new Float32BufferAttribute(positions, 3)); if (tTangent) { const tangents = new Array(3); const tangentName = params.tangentName; const inFirstHalf = t < 0.5; const t2 = inFirstHalf ? t + EPSILON : t - EPSILON; curve.getPoint(t2, this._next); if (inFirstHalf) { this._next.sub(this._current).normalize(); } else { this._current.sub(this._next).normalize(); this._next.copy(this._current); } this._next.toArray(tangents, 0); geometry.setAttribute(tangentName, new Float32BufferAttribute(tangents, 3)); } const newObject = BaseSopOperation.createObject(geometry, ObjectType.POINTS); return newObject; } } CurveGetPointSopOperation.DEFAULT_PARAMS = { t: 0, closed: false, curveType: 0, tension: 0.5, tTangent: false, tangentName: "tangent" }; CurveGetPointSopOperation.INPUT_CLONED_STATE = InputCloneMode.NEVER;