UNPKG

@awayjs/view

Version:
114 lines (113 loc) 4.44 kB
import { __extends } from "tslib"; import { PlaneClassification } from '@awayjs/core'; import { BoundingVolumeBase } from './BoundingVolumeBase'; /** * BoundingBox represents a bounding box volume that has its planes aligned to the local coordinate axes of the bounded object. * This is useful for most sprites. */ var BoundingBox = /** @class */ (function (_super) { __extends(BoundingBox, _super); function BoundingBox() { var _this = _super !== null && _super.apply(this, arguments) || this; _this._x = 0; _this._y = 0; _this._z = 0; _this._width = 0; _this._height = 0; _this._depth = 0; _this._centerX = 0; _this._centerY = 0; _this._centerZ = 0; _this._halfExtentsX = 0; _this._halfExtentsY = 0; _this._halfExtentsZ = 0; return _this; // public _createBoundsPrimitive():Sprite // { // this._prefab = new PrimitiveCubePrefab(null, ElementsType.LINE); // return <Sprite> this._prefab.getNewObject(); // } } /** * @inheritDoc */ BoundingBox.prototype.nullify = function () { this._x = this._y = this._z = 0; this._width = this._height = this._depth = 0; this._centerX = this._centerY = this._centerZ = 0; this._halfExtentsX = this._halfExtentsY = this._halfExtentsZ = 0; }; /** * @inheritDoc */ BoundingBox.prototype.isInFrustum = function (planes, numPlanes) { if (this._invalid) this._update(); if (this._box == null) return; for (var i = 0; i < numPlanes; ++i) { var plane = planes[i]; var a = plane.a; var b = plane.b; var c = plane.c; var flippedExtentX = a < 0 ? -this._halfExtentsX : this._halfExtentsX; var flippedExtentY = b < 0 ? -this._halfExtentsY : this._halfExtentsY; var flippedExtentZ = c < 0 ? -this._halfExtentsZ : this._halfExtentsZ; var projDist = a * (this._centerX + flippedExtentX) + b * (this._centerY + flippedExtentY) + c * (this._centerZ + flippedExtentZ) - plane.d; if (projDist < 0) return false; } return true; }; BoundingBox.prototype.rayIntersection = function (position, direction, targetNormal) { if (this._invalid) this._update(); if (this._box == null) return -1; return this._box.rayIntersection(position, direction, targetNormal); }; BoundingBox.prototype.getBox = function () { if (this._invalid) this._update(); return this._box; }; BoundingBox.prototype.classifyToPlane = function (plane) { var a = plane.a; var b = plane.b; var c = plane.c; var centerDistance = a * this._centerX + b * this._centerY + c * this._centerZ - plane.d; if (a < 0) a = -a; if (b < 0) b = -b; if (c < 0) c = -c; var boundOffset = a * this._halfExtentsX + b * this._halfExtentsY + c * this._halfExtentsZ; return centerDistance > boundOffset ? PlaneClassification.FRONT : centerDistance < -boundOffset ? PlaneClassification.BACK : PlaneClassification.INTERSECT; }; BoundingBox.prototype._update = function () { _super.prototype._update.call(this); var picker = this.pool.picker; var matrix3D; if (this._targetCoordinateSpace != picker.node) { if (this._targetCoordinateSpace == picker.node.parent) { matrix3D = picker.node.container.transform.matrix3D; } else { matrix3D = picker.node.getMatrix3D().clone(); matrix3D.append(this._targetCoordinateSpace.getInverseMatrix3D()); } } this._box = picker._getBoxBoundsInternal(matrix3D, this._strokeFlag, this._fastFlag, this._box); if (this._box == null) return; this._halfExtentsX = this._box.width / 2; this._halfExtentsY = this._box.height / 2; this._halfExtentsZ = this._box.depth / 2; this._centerX = this._box.x + this._halfExtentsX; this._centerY = this._box.y + this._halfExtentsY; this._centerZ = this._box.z + this._halfExtentsZ; }; return BoundingBox; }(BoundingVolumeBase)); export { BoundingBox };