@awayjs/view
Version:
View for AwayJS
101 lines (100 loc) • 4.05 kB
JavaScript
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._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._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 targetCoordinateSpace = this._asset;
var picker = this._pool.picker;
// a null invTargetMatrix means local coords to the node
var invTargetMatrix = (targetCoordinateSpace != picker.node)
? targetCoordinateSpace.getInverseMatrix3D()
: null;
this._box = picker._getBoxBoundsInternal(invTargetMatrix, 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 };