UNPKG

@awayjs/core

Version:
414 lines (413 loc) 14.7 kB
import { __extends } from "tslib"; import { Transform } from '../base/Transform'; import { Matrix3D } from '../geom/Matrix3D'; import { EventDispatcher } from '../events/EventDispatcher'; import { ProjectionEvent } from '../events/ProjectionEvent'; import { TransformEvent } from '../events/TransformEvent'; import { AbstractMethodError } from '../errors/AbstractMethodError'; import { CoordinateSystem } from './CoordinateSystem'; import { Rectangle } from '../geom/Rectangle'; import { Plane3D } from '../geom/Plane3D'; var ProjectionBase = /** @class */ (function (_super) { __extends(ProjectionBase, _super); function ProjectionBase(coordinateSystem) { if (coordinateSystem === void 0) { coordinateSystem = CoordinateSystem.LEFT_HANDED; } var _this = _super.call(this) || this; _this._viewMatrix3D = new Matrix3D(); _this._inverseViewMatrix3D = new Matrix3D(); _this._frustumMatrix3D = new Matrix3D(); _this._near = 20; _this._far = 3000; _this._scale = 1; _this._ratio = 1; _this._originX = 0; _this._originY = 0; _this._frustumRect = new Rectangle(); _this._viewFrustumCorners = []; _this._viewMatrix3DDirty = true; _this._inverseViewMatrix3DDirty = true; _this._frustumMatrix3DDirty = true; _this._viewFrustumCornersDirty = true; _this._viewFrustumPlanesDirty = true; _this._coordinateSystem = coordinateSystem; _this._onInvalidateConcatenatedMatrix3DDelegate = function (event) { return _this._onInvalidateConcatenatedMatrix3D(event); }; return _this; } Object.defineProperty(ProjectionBase.prototype, "transform", { get: function () { if (!this._transform) this.transform = new Transform(); return this._transform; }, set: function (value) { if (this._transform == value) return; if (this._transform) this._transform.removeEventListener(TransformEvent.INVALIDATE_MATRIX3D, this._onInvalidateConcatenatedMatrix3DDelegate); this._transform = value; if (this._transform) this._transform.addEventListener(TransformEvent.INVALIDATE_MATRIX3D, this._onInvalidateConcatenatedMatrix3DDelegate); this._invalidateViewMatrix3D(); }, enumerable: false, configurable: true }); Object.defineProperty(ProjectionBase.prototype, "coordinateSystem", { /** * The handedness of the coordinate system projection. The default is LEFT_HANDED. */ get: function () { return this._coordinateSystem; }, set: function (value) { if (this._coordinateSystem == value) return; this._coordinateSystem = value; this._invalidateFrustumMatrix3D(); }, enumerable: false, configurable: true }); Object.defineProperty(ProjectionBase.prototype, "scale", { /** * */ get: function () { if (this._propertiesDirty) this._updateProperties(); return this._scale; }, set: function (value) { if (this._scale == value) return; this._scale = value; this._invalidateFrustumMatrix3D(); }, enumerable: false, configurable: true }); Object.defineProperty(ProjectionBase.prototype, "ratio", { /** * */ get: function () { if (this._propertiesDirty) this._updateProperties(); return this._ratio; }, set: function (value) { if (this._ratio == value) return; this._ratio = value; this._invalidateFrustumMatrix3D(); }, enumerable: false, configurable: true }); Object.defineProperty(ProjectionBase.prototype, "frustumMatrix3D", { /** * * @returns {Matrix3D} */ get: function () { if (this._frustumMatrix3DDirty) this._updateFrustumMatrix3D(); return this._frustumMatrix3D; }, set: function (value) { this._frustumMatrix3D = value; this._invalidateViewMatrix3D(); this._invalidateProperties(); }, enumerable: false, configurable: true }); Object.defineProperty(ProjectionBase.prototype, "viewFrustumCorners", { /** * * @returns {number[]} */ get: function () { if (this._viewFrustumCornersDirty) { this._viewFrustumCornersDirty = false; if (this._frustumMatrix3DDirty) this._updateFrustumMatrix3D(); var left = this._frustumRect.left; var right = this._frustumRect.right; var top_1 = this._frustumRect.top; var bottom = this._frustumRect.bottom; if (this._propertiesDirty) this._updateProperties(); var near = this._near; var far = this._far; var vfc = this._viewFrustumCorners; vfc[0] = vfc[9] = near * left; vfc[3] = vfc[6] = near * right; vfc[1] = vfc[4] = near * top_1; vfc[7] = vfc[10] = near * bottom; vfc[12] = vfc[21] = far * left; vfc[15] = vfc[18] = far * right; vfc[13] = vfc[16] = far * top_1; vfc[19] = vfc[22] = far * bottom; vfc[2] = vfc[5] = vfc[8] = vfc[11] = near; vfc[14] = vfc[17] = vfc[20] = vfc[23] = far; if (this._transform) this._transform.matrix3D.transformVectors(vfc, vfc); } return this._viewFrustumCorners; }, enumerable: false, configurable: true }); Object.defineProperty(ProjectionBase.prototype, "viewFrustumPlanes", { /** * */ get: function () { if (this._viewFrustumPlanesDirty) { this._viewFrustumPlanesDirty = false; if (!this._viewFrustumPlanes) { this._viewFrustumPlanes = []; for (var i = 0; i < 6; ++i) this._viewFrustumPlanes[i] = new Plane3D(); } var raw = this.viewMatrix3D._rawData; var invLen = void 0; var c11 = raw[0], c12 = raw[4], c13 = raw[8], c14 = raw[12]; var c21 = raw[1], c22 = raw[5], c23 = raw[9], c24 = raw[13]; var c31 = raw[2], c32 = raw[6], c33 = raw[10], c34 = raw[14]; var c41 = raw[3], c42 = raw[7], c43 = raw[11], c44 = raw[15]; var a = void 0, b = void 0, c = void 0, p = void 0; // left plane p = this._viewFrustumPlanes[0]; a = c41 + c11; b = c42 + c12; c = c43 + c13; invLen = 1 / Math.sqrt(a * a + b * b + c * c); p.a = a * invLen; p.b = b * invLen; p.c = c * invLen; p.d = -(c44 + c14) * invLen; // right plane p = this._viewFrustumPlanes[1]; a = c41 - c11; b = c42 - c12; c = c43 - c13; invLen = 1 / Math.sqrt(a * a + b * b + c * c); p.a = a * invLen; p.b = b * invLen; p.c = c * invLen; p.d = (c14 - c44) * invLen; // bottom p = this._viewFrustumPlanes[2]; a = c41 + c21; b = c42 + c22; c = c43 + c23; invLen = 1 / Math.sqrt(a * a + b * b + c * c); p.a = a * invLen; p.b = b * invLen; p.c = c * invLen; p.d = -(c44 + c24) * invLen; // top p = this._viewFrustumPlanes[3]; a = c41 - c21; b = c42 - c22; c = c43 - c23; invLen = 1 / Math.sqrt(a * a + b * b + c * c); p.a = a * invLen; p.b = b * invLen; p.c = c * invLen; p.d = (c24 - c44) * invLen; // near p = this._viewFrustumPlanes[4]; a = c31; b = c32; c = c33; invLen = 1 / Math.sqrt(a * a + b * b + c * c); p.a = a * invLen; p.b = b * invLen; p.c = c * invLen; p.d = -c34 * invLen; // far p = this._viewFrustumPlanes[5]; a = c41 - c31; b = c42 - c32; c = c43 - c33; invLen = 1 / Math.sqrt(a * a + b * b + c * c); p.a = a * invLen; p.b = b * invLen; p.c = c * invLen; p.d = (c34 - c44) * invLen; } return this._viewFrustumPlanes; }, enumerable: false, configurable: true }); Object.defineProperty(ProjectionBase.prototype, "near", { /** * * @returns {number} */ get: function () { if (this._propertiesDirty) this._updateProperties(); return this._near; }, set: function (value) { if (value == this._near) return; this._near = value; this._invalidateFrustumMatrix3D(); }, enumerable: false, configurable: true }); Object.defineProperty(ProjectionBase.prototype, "originX", { /** * * @returns {number} */ get: function () { if (this._propertiesDirty) this._updateProperties(); return this._originX; }, set: function (value) { if (this._originX == value) return; this._originX = value; this._invalidateFrustumMatrix3D(); }, enumerable: false, configurable: true }); Object.defineProperty(ProjectionBase.prototype, "originY", { /** * * @returns {number} */ get: function () { if (this._propertiesDirty) this._updateProperties(); return this._originY; }, set: function (value) { if (this._originY == value) return; this._originY = value; this._invalidateFrustumMatrix3D(); }, enumerable: false, configurable: true }); Object.defineProperty(ProjectionBase.prototype, "far", { /** * * @returns {number} */ get: function () { if (this._propertiesDirty) this._updateProperties(); return this._far; }, set: function (value) { if (value == this._far) return; this._far = value; this._invalidateFrustumMatrix3D(); }, enumerable: false, configurable: true }); Object.defineProperty(ProjectionBase.prototype, "frustumRect", { get: function () { if (this._frustumMatrix3DDirty) this._updateFrustumMatrix3D(); return this._frustumRect; }, enumerable: false, configurable: true }); /** * * @param position * @param target */ ProjectionBase.prototype.project = function (position, target) { if (target === void 0) { target = null; } throw new AbstractMethodError(); }; /** * * @param nX * @param nY * @param sZ * @param target */ ProjectionBase.prototype.unproject = function (nX, nY, sZ, target) { if (target === void 0) { target = null; } throw new AbstractMethodError(); }; Object.defineProperty(ProjectionBase.prototype, "viewMatrix3D", { get: function () { if (this._viewMatrix3DDirty) { this._viewMatrix3DDirty = false; if (this._transform) { this._viewMatrix3D.copyFrom(this._transform.inverseMatrix3D); this._viewMatrix3D.append(this.frustumMatrix3D); } else { this._viewMatrix3D.copyFrom(this.frustumMatrix3D); } } return this._viewMatrix3D; }, enumerable: false, configurable: true }); Object.defineProperty(ProjectionBase.prototype, "inverseViewMatrix3D", { get: function () { if (this._inverseViewMatrix3DDirty) { this._inverseViewMatrix3DDirty = false; this._inverseViewMatrix3D.copyFrom(this.viewMatrix3D); this._inverseViewMatrix3D.invert(); } return this._inverseViewMatrix3D; }, enumerable: false, configurable: true }); ProjectionBase.prototype.clone = function () { throw new AbstractMethodError(); }; ProjectionBase.prototype._invalidateProperties = function () { this._propertiesDirty = true; }; ProjectionBase.prototype._invalidateViewMatrix3D = function () { this._viewMatrix3DDirty = true; this._inverseViewMatrix3DDirty = true; this._viewFrustumCornersDirty = true; this._viewFrustumPlanesDirty = true; this.dispatchEvent(new ProjectionEvent(ProjectionEvent.INVALIDATE_VIEW_MATRIX3D, this)); }; ProjectionBase.prototype._invalidateFrustumMatrix3D = function () { if (this._propertiesDirty) this._updateProperties(); this._frustumMatrix3DDirty = true; this.dispatchEvent(new ProjectionEvent(ProjectionEvent.INVALIDATE_FRUSTUM_MATRIX3D, this)); this._invalidateViewMatrix3D(); }; ProjectionBase.prototype._updateFrustumMatrix3D = function () { this._frustumMatrix3DDirty = false; }; ProjectionBase.prototype._updateProperties = function () { this._propertiesDirty = false; }; ProjectionBase.prototype._onInvalidateConcatenatedMatrix3D = function (event) { this._invalidateViewMatrix3D(); }; return ProjectionBase; }(EventDispatcher)); export { ProjectionBase };