UNPKG

@polygonjs/polygonjs

Version:

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

69 lines (68 loc) 2.2 kB
"use strict"; import { ObjectType } from "./../../../core/geometry/Constant"; import { BaseSopOperation } from "./_Base"; import { Vector3, BoxGeometry, Box3 } from "three"; import { CoreTransform } from "../../../core/Transform"; import { InputCloneMode } from "../../../engine/poly/InputCloneMode"; const tmpBox = new Box3(); const tmpSize = new Vector3(); const tmpCenter = new Vector3(); function _roundDivision(division) { return Math.max(1, Math.floor(division)); } export class BoxSopOperation extends BaseSopOperation { constructor() { super(...arguments); this._coreTransform = new CoreTransform(); } static type() { return "box"; } cook(inputCoreGroups, params) { const inputCoreGroup = inputCoreGroups[0]; const geometry = inputCoreGroup ? this._cookWithInput(inputCoreGroup, params) : this._cookWithoutInput(params); const object = BaseSopOperation.createObject(geometry, ObjectType.MESH); if (this._node) { object.name = this._node.name(); } return this.createCoreGroupFromObjects([object]); } _cookWithoutInput(params) { const { divisions, size, sizes } = params; const geometry = new BoxGeometry( size * sizes.x, size * sizes.y, size * sizes.z, _roundDivision(divisions.x), _roundDivision(divisions.y), _roundDivision(divisions.z) ); geometry.translate(params.center.x, params.center.y, params.center.z); geometry.computeVertexNormals(); return geometry; } _cookWithInput(coreGroup, params) { coreGroup.boundingBox(tmpBox); tmpBox.getSize(tmpSize); tmpBox.getCenter(tmpCenter); const divisions = params.divisions; const geometry = new BoxGeometry( tmpSize.x, tmpSize.y, tmpSize.z, _roundDivision(divisions.x), _roundDivision(divisions.y), _roundDivision(divisions.z) ); const matrix = this._coreTransform.translationMatrix(tmpCenter); geometry.applyMatrix4(matrix); return geometry; } } BoxSopOperation.DEFAULT_PARAMS = { sizes: new Vector3(1, 1, 1), size: 1, divisions: new Vector3(1, 1, 1), center: new Vector3(0, 0, 0) }; BoxSopOperation.INPUT_CLONED_STATE = InputCloneMode.NEVER;