@awayjs/core
Version:
AwayJS core classes
110 lines (109 loc) • 4.16 kB
JavaScript
import { __extends } from "tslib";
import { Vector3D } from '../geom/Vector3D';
import { CoordinateSystem } from './CoordinateSystem';
import { ProjectionBase } from './ProjectionBase';
var PerspectiveProjection = /** @class */ (function (_super) {
__extends(PerspectiveProjection, _super);
function PerspectiveProjection(fieldOfView, coordinateSystem) {
if (fieldOfView === void 0) { fieldOfView = 60; }
if (coordinateSystem === void 0) { coordinateSystem = CoordinateSystem.LEFT_HANDED; }
var _this = _super.call(this, coordinateSystem) || this;
_this.fieldOfView = fieldOfView;
return _this;
}
Object.defineProperty(PerspectiveProjection.prototype, "fieldOfView", {
/**
*
*/
get: function () {
return Math.atan(0.5 / this.scale) * 360 / Math.PI;
},
set: function (value) {
this.scale = 0.5 / Math.tan(value * Math.PI / 360);
},
enumerable: false,
configurable: true
});
/**
*
* @param position
* @param target
*/
PerspectiveProjection.prototype.project = function (position, target) {
if (target === void 0) { target = null; }
var v = this.viewMatrix3D.transformVector(position, target);
v.x = v.x / v.w;
v.y = -v.y / v.w;
//z is remapped to w
v.z = v.w;
v.w = 1;
return v;
};
/**
*
* @param nX
* @param nY
* @param sZ
* @param target
*/
PerspectiveProjection.prototype.unproject = function (nX, nY, sZ, target) {
if (target === void 0) { target = null; }
if (target == null)
target = new Vector3D();
target.x = nX * sZ;
target.y = -nY * sZ;
target.z = (this._far + this._near) / (this._far - this._near) * sZ
- 2 * this._far * this._near / (this._far - this._near);
target.w = sZ;
this.inverseViewMatrix3D.transformVector(target, target);
target.w = 1;
return target;
};
/**
*
* @returns {PerspectiveProjection}
*/
PerspectiveProjection.prototype.clone = function () {
var clone = new PerspectiveProjection(this.fieldOfView, this._coordinateSystem);
clone._near = this._near;
clone._far = this._far;
clone._coordinateSystem = this._coordinateSystem;
return clone;
};
/**
*
* @private
*/
PerspectiveProjection.prototype._updateFrustumMatrix3D = function () {
_super.prototype._updateFrustumMatrix3D.call(this);
var raw = this._frustumMatrix3D._rawData;
var scaleV = this._scale;
var scaleH = this._scale / this._ratio;
this._frustumRect.left = 0.5 * (this._originX - 1) / scaleH;
this._frustumRect.top = 0.5 * (this._originY - 1) / scaleV;
this._frustumRect.right = this._frustumRect.left + 1 / scaleH;
this._frustumRect.bottom = this._frustumRect.top + 1 / scaleV;
raw[0] = 2 * scaleH; //2/(right - left);
raw[5] = 2 * scaleV; //2/(bottom - top);
raw[8] = this._originX; //(right + left)/(right - left)
raw[9] = this._originY; //(bottom + top)/(bottom - top);
raw[10] = (this._far + this._near) / (this._far - this._near);
raw[11] = 1;
raw[1] = raw[2] = raw[3] = raw[4] = raw[6] = raw[7] = raw[12] = raw[13] = raw[15] = 0;
raw[14] = -2 * this._far * this._near / (this._far - this._near);
if (this._coordinateSystem == CoordinateSystem.RIGHT_HANDED)
raw[5] = -raw[5];
};
PerspectiveProjection.prototype._updateProperties = function () {
_super.prototype._updateProperties.call(this);
var rawData = this._frustumMatrix3D._rawData;
this._near = rawData[14] / (-1 - rawData[10]);
this._far = rawData[14] / (1 - rawData[10]);
this._scale = rawData[5] / 2;
this._ratio = 0.5 * this._scale / rawData[0];
this._originX = rawData[8];
this._originY = rawData[9];
};
return PerspectiveProjection;
}(ProjectionBase));
export { PerspectiveProjection };