@awayjs/view
Version:
View for AwayJS
106 lines (105 loc) • 4.4 kB
JavaScript
import { __extends } from "tslib";
import { PlaneClassification } from '@awayjs/core';
import { BoundingVolumeBase } from './BoundingVolumeBase';
var BoundingSphere = /** @class */ (function (_super) {
__extends(BoundingSphere, _super);
function BoundingSphere() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this._radius = 0;
_this._centerX = 0;
_this._centerY = 0;
_this._centerZ = 0;
return _this;
// public _pCreateBoundsPrimitive():Sprite
// {
// this._prefab = new PrimitiveSpherePrefab(null, ElementsType.LINE);
// return <Sprite> this._prefab.getNewObject();
// }
}
//private _prefab:PrimitiveSpherePrefab;
BoundingSphere.prototype.nullify = function () {
this._centerX = this._centerY = this._centerZ = 0;
this._radius = 0;
};
BoundingSphere.prototype.isInFrustum = function (planes, numPlanes) {
if (this._invalid)
this._update();
for (var i = 0; i < numPlanes; ++i) {
var plane = planes[i];
var flippedExtentX = plane.a < 0 ? -this._radius : this._radius;
var flippedExtentY = plane.b < 0 ? -this._radius : this._radius;
var flippedExtentZ = plane.c < 0 ? -this._radius : this._radius;
var projDist = plane.a * (this._centerX + flippedExtentX) + plane.b * (this._centerY + flippedExtentY) + plane.c * (this._centerZ + flippedExtentZ) - plane.d;
if (projDist < 0) {
return false;
}
}
return true;
};
BoundingSphere.prototype.rayIntersection = function (position, direction, targetNormal) {
if (this._invalid)
this._update();
return this._sphere.rayIntersection(position, direction, targetNormal);
};
BoundingSphere.prototype.getSphere = function () {
if (this._invalid)
this._update();
return this._sphere;
};
//@override
BoundingSphere.prototype.classifyToPlane = function (plane) {
var a = plane.a;
var b = plane.b;
var c = plane.c;
var dd = 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 rr = (a + b + c) * this._radius;
return dd > rr ? PlaneClassification.FRONT : dd < -rr ? PlaneClassification.BACK : PlaneClassification.INTERSECT;
};
BoundingSphere.prototype._update = function () {
_super.prototype._update.call(this);
var picker = this.pool.picker;
var matrix3D;
if (this._targetCoordinateSpace) {
if (this._targetCoordinateSpace == picker.node) {
matrix3D = picker.node.container.transform.matrix3D;
}
else {
matrix3D = picker.node.getMatrix3D().clone();
if (this._targetCoordinateSpace.parent)
matrix3D.append(this._targetCoordinateSpace.parent.getInverseMatrix3D());
}
}
this._sphere = picker._getSphereBoundsInternal(null, matrix3D, this._strokeFlag, this._fastFlag, this._sphere);
var matrix = picker.node.getMatrix3D();
var cx = this._sphere.x;
var cy = this._sphere.y;
var cz = this._sphere.z;
var r = this._sphere.radius;
var raw = matrix._rawData;
var m11 = raw[0], m12 = raw[4], m13 = raw[8], m14 = raw[12];
var m21 = raw[1], m22 = raw[5], m23 = raw[9], m24 = raw[13];
var m31 = raw[2], m32 = raw[6], m33 = raw[10], m34 = raw[14];
this._centerX = cx * m11 + cy * m12 + cz * m13 + m14;
this._centerY = cx * m21 + cy * m22 + cz * m23 + m24;
this._centerZ = cx * m31 + cy * m32 + cz * m33 + m34;
var rx = m11 + m12 + m13;
var ry = m21 + m22 + m23;
var rz = m31 + m32 + m33;
this._radius = r * Math.sqrt((rx * rx + ry * ry + rz * rz) / 3);
// if (this._prefab) {
// this._prefab.radius = r;
// this._boundsPrimitive.x = cx;
// this._boundsPrimitive.y = cy;
// this._boundsPrimitive.z = cz;
// this._boundsPrimitive.transform.matrix3D = matrix;
// }
};
return BoundingSphere;
}(BoundingVolumeBase));
export { BoundingSphere };