UNPKG

@awayjs/scene

Version:
222 lines (221 loc) 8.61 kB
import { __extends } from "tslib"; import { Box, Sphere } from '@awayjs/core'; import { _Pick_PickableBase, PickEntity } from '@awayjs/view'; import { RenderableEvent } from '@awayjs/renderer'; import { DisplayObject } from './DisplayObject'; /** * A Line Segment primitive. */ var LineSegment = /** @class */ (function (_super) { __extends(LineSegment, _super); /** * Create a line segment * * @param startPosition Start position of the line segment * @param endPosition Ending position of the line segment * @param thickness Thickness of the line */ function LineSegment(material, startPosition, endPosition, thickness) { if (thickness === void 0) { thickness = 1; } var _this = _super.call(this) || this; _this.material = material; _this._startPosition = startPosition; _this._endPosition = endPosition; _this._halfThickness = thickness * 0.5; return _this; } Object.defineProperty(LineSegment.prototype, "assetType", { /** * */ get: function () { return LineSegment.assetType; }, enumerable: false, configurable: true }); Object.defineProperty(LineSegment.prototype, "startPosition", { /** * */ get: function () { return this._startPosition; }, set: function (value) { if (this._startPosition == value) return; this._startPosition = value; this.invalidateElements(); }, enumerable: false, configurable: true }); Object.defineProperty(LineSegment.prototype, "endPosition", { /** * */ get: function () { return this._endPosition; }, set: function (value) { if (this._endPosition == value) return; this._endPosition = value; this.invalidateElements(); }, enumerable: false, configurable: true }); Object.defineProperty(LineSegment.prototype, "thickness", { /** * */ get: function () { return this._halfThickness * 2; }, set: function (value) { if (this._halfThickness == value) return; this._halfThickness = value * 0.5; this.invalidateElements(); }, enumerable: false, configurable: true }); LineSegment.prototype.getEntity = function () { return this; }; LineSegment.prototype._acceptTraverser = function (traverser) { traverser.applyTraversable(this); }; LineSegment.assetType = '[asset LineSegment]'; return LineSegment; }(DisplayObject)); export { LineSegment }; import { LineElements } from '@awayjs/renderer'; import { _Render_RenderableBase, RenderEntity, MaterialUtils } from '@awayjs/renderer'; /** * @class away.pool._Render_LineSegment */ var _Render_LineSegment = /** @class */ (function (_super) { __extends(_Render_LineSegment, _super); function _Render_LineSegment() { return _super !== null && _super.apply(this, arguments) || this; } /** * //TODO * * @returns {base.LineElements} * @protected */ _Render_LineSegment.prototype._getStageElements = function () { var elements = _Render_LineSegment._lineGraphics[this._asset.id] || (_Render_LineSegment._lineGraphics[this._asset.id] = new LineElements()); var start = this._asset.startPosition; var end = this._asset.endPosition; var positions = new Float32Array(6); var thickness = new Float32Array(1); positions[0] = start.x; positions[1] = start.y; positions[2] = start.z; positions[3] = end.x; positions[4] = end.y; positions[5] = end.z; thickness[0] = this._asset.thickness; elements.setPositions(positions); elements.setThickness(thickness); return elements.getAbstraction(this._stage); }; _Render_LineSegment.prototype._getRenderMaterial = function () { var material = this._asset.material || MaterialUtils.getDefaultColorMaterial(); return material.getAbstraction(this.entity.renderer.getRenderElements(this.stageElements.elements)); }; _Render_LineSegment.prototype._getStyle = function () { return this._asset.style; }; _Render_LineSegment._lineGraphics = {}; return _Render_LineSegment; }(_Render_RenderableBase)); export { _Render_LineSegment }; /** * @class away.pool._Render_Shape */ var _Pick_LineSegment = /** @class */ (function (_super) { __extends(_Pick_LineSegment, _super); function _Pick_LineSegment() { var _this = _super.call(this) || this; _this._lineSegmentBoxDirty = true; _this._lineSegmentSphereDirty = true; _this._onInvalidateElementsDelegate = function (event) { return _this._onInvalidateElements(event); }; return _this; } /** * //TODO * * @param renderEntity * @param shape * @param level * @param indexOffset */ _Pick_LineSegment.prototype.init = function (lineSegment, pickEntity) { _super.prototype.init.call(this, lineSegment, pickEntity); this._asset.addEventListener(RenderableEvent.INVALIDATE_ELEMENTS, this._onInvalidateElementsDelegate); }; _Pick_LineSegment.prototype._onInvalidateElements = function (event) { this._lineSegmentBoxDirty = true; this._lineSegmentSphereDirty = true; }; _Pick_LineSegment.prototype.onClear = function (event) { this._asset.removeEventListener(RenderableEvent.INVALIDATE_ELEMENTS, this._onInvalidateElementsDelegate); _super.prototype.onClear.call(this, event); this._lineSegmentBox = null; this._lineSegmentBoxDirty = true; this._lineSegmentSphere = null; this._lineSegmentSphereDirty = true; }; _Pick_LineSegment.prototype.hitTestPoint = function (x, y, z) { return true; }; _Pick_LineSegment.prototype.getBoxBounds = function (matrix3D, strokeFlag, cache, target) { if (matrix3D === void 0) { matrix3D = null; } if (strokeFlag === void 0) { strokeFlag = true; } if (cache === void 0) { cache = null; } if (target === void 0) { target = null; } if (this._lineSegmentBoxDirty) { this._lineSegmentBoxDirty = false; var startPosition = this._asset.startPosition; var endPosition = this._asset.endPosition; this._lineSegmentBox = new Box(Math.min(startPosition.x, endPosition.x), Math.min(startPosition.y, endPosition.y), Math.min(startPosition.z, endPosition.z), Math.abs(startPosition.x - endPosition.x), Math.abs(startPosition.y - endPosition.y), Math.abs(startPosition.z - endPosition.z)); } return (matrix3D ? matrix3D.transformBox(this._lineSegmentBox) : this._lineSegmentBox).union(target, target || cache); }; _Pick_LineSegment.prototype.getSphereBounds = function (center, matrix3D, strokeFlag, cache, target) { if (matrix3D === void 0) { matrix3D = null; } if (strokeFlag === void 0) { strokeFlag = true; } if (cache === void 0) { cache = null; } if (target === void 0) { target = null; } if (this._lineSegmentSphereDirty) { this._lineSegmentSphereDirty = false; var startPosition = this._asset.startPosition; var endPosition = this._asset.endPosition; var halfWidth = (endPosition.x - startPosition.x) / 2; var halfHeight = (endPosition.y - startPosition.y) / 2; var halfDepth = (endPosition.z - startPosition.z) / 2; this._lineSegmentSphere = new Sphere(startPosition.x + halfWidth, startPosition.y + halfHeight, startPosition.z + halfDepth, Math.sqrt(halfWidth * halfWidth + halfHeight * halfHeight + halfDepth * halfDepth)); } return (matrix3D ? matrix3D.transformSphere(this._lineSegmentSphere) : this._lineSegmentSphere).union(target, target || cache); }; _Pick_LineSegment.prototype.testCollision = function (collision, closestFlag) { collision.traversable = null; //TODO return false; }; return _Pick_LineSegment; }(_Pick_PickableBase)); export { _Pick_LineSegment }; RenderEntity.registerRenderable(_Render_LineSegment, LineSegment); PickEntity.registerPickable(_Pick_LineSegment, LineSegment);