awayjs-display
Version:
AwayJS displaylist classes
1,185 lines (1,175 loc) • 847 kB
JavaScript
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
else if(typeof define === 'function' && define.amd)
define("awayjs-display", [], factory);
else if(typeof exports === 'object')
exports["awayjs-display"] = factory();
else
root["awayjs-display"] = factory();
})(this, function() {
return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;
/******/
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ exports: {},
/******/ id: moduleId,
/******/ loaded: false
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.loaded = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ function(module, exports, __webpack_require__) {
module.exports = __webpack_require__(1);
/***/ },
/* 1 */
/***/ function(module, exports, __webpack_require__) {
"use strict";
var Camera_1 = __webpack_require__(2);
var DirectionalLight_1 = __webpack_require__(39);
var Sprite_1 = __webpack_require__(51);
var Billboard_1 = __webpack_require__(63);
var LineSegment_1 = __webpack_require__(86);
var TextField_1 = __webpack_require__(87);
var PointLight_1 = __webpack_require__(91);
var LightProbe_1 = __webpack_require__(93);
var Skybox_1 = __webpack_require__(80);
var Shape_1 = __webpack_require__(95);
var MovieClip_1 = __webpack_require__(96);
var CameraNode_1 = __webpack_require__(100);
var DirectionalLightNode_1 = __webpack_require__(113);
var EntityNode_1 = __webpack_require__(101);
var LightProbeNode_1 = __webpack_require__(114);
var PartitionBase_1 = __webpack_require__(115);
var PointLightNode_1 = __webpack_require__(116);
var SkyboxNode_1 = __webpack_require__(117);
PartitionBase_1.default.registerAbstraction(CameraNode_1.default, Camera_1.default);
PartitionBase_1.default.registerAbstraction(DirectionalLightNode_1.default, DirectionalLight_1.default);
PartitionBase_1.default.registerAbstraction(EntityNode_1.default, Sprite_1.default);
PartitionBase_1.default.registerAbstraction(EntityNode_1.default, Shape_1.default);
PartitionBase_1.default.registerAbstraction(EntityNode_1.default, MovieClip_1.default);
PartitionBase_1.default.registerAbstraction(EntityNode_1.default, Billboard_1.default);
PartitionBase_1.default.registerAbstraction(EntityNode_1.default, LineSegment_1.default);
PartitionBase_1.default.registerAbstraction(EntityNode_1.default, TextField_1.default);
PartitionBase_1.default.registerAbstraction(LightProbeNode_1.default, LightProbe_1.default);
PartitionBase_1.default.registerAbstraction(PointLightNode_1.default, PointLight_1.default);
PartitionBase_1.default.registerAbstraction(SkyboxNode_1.default, Skybox_1.default);
/***/ },
/* 2 */
/***/ function(module, exports, __webpack_require__) {
"use strict";
var Matrix3D_1 = __webpack_require__(3);
var Plane3D_1 = __webpack_require__(10);
var ProjectionEvent_1 = __webpack_require__(12);
var PerspectiveProjection_1 = __webpack_require__(14);
var HierarchicalProperties_1 = __webpack_require__(22);
var BoundsType_1 = __webpack_require__(23);
var DisplayObjectContainer_1 = __webpack_require__(24);
var CameraEvent_1 = __webpack_require__(38);
var Camera = (function (_super) {
__extends(Camera, _super);
function Camera(projection) {
var _this = this;
if (projection === void 0) { projection = null; }
_super.call(this);
this._viewProjection = new Matrix3D_1.default();
this._viewProjectionDirty = true;
this._frustumPlanesDirty = true;
this._pIsEntity = true;
this._onProjectionMatrixChangedDelegate = function (event) { return _this.onProjectionMatrixChanged(event); };
this._projection = projection || new PerspectiveProjection_1.default();
this._projection.addEventListener(ProjectionEvent_1.default.MATRIX_CHANGED, this._onProjectionMatrixChangedDelegate);
this._frustumPlanes = [];
for (var i = 0; i < 6; ++i)
this._frustumPlanes[i] = new Plane3D_1.default();
this.z = -1000;
//default bounds type
this._boundsType = BoundsType_1.default.NULL;
}
Object.defineProperty(Camera.prototype, "assetType", {
//@override
get: function () {
return Camera.assetType;
},
enumerable: true,
configurable: true
});
Camera.prototype.onProjectionMatrixChanged = function (event) {
this._viewProjectionDirty = true;
this._frustumPlanesDirty = true;
this.dispatchEvent(event);
};
Object.defineProperty(Camera.prototype, "frustumPlanes", {
get: function () {
if (this._frustumPlanesDirty)
this.updateFrustum();
return this._frustumPlanes;
},
enumerable: true,
configurable: true
});
Camera.prototype.updateFrustum = function () {
var a, b, c;
//var d : Number;
var c11, c12, c13, c14;
var c21, c22, c23, c24;
var c31, c32, c33, c34;
var c41, c42, c43, c44;
var p;
var raw = this.viewProjection.rawData;
var invLen;
c11 = raw[0];
c12 = raw[4];
c13 = raw[8];
c14 = raw[12];
c21 = raw[1];
c22 = raw[5];
c23 = raw[9];
c24 = raw[13];
c31 = raw[2];
c32 = raw[6];
c33 = raw[10];
c34 = raw[14];
c41 = raw[3];
c42 = raw[7];
c43 = raw[11];
c44 = raw[15];
// left plane
p = this._frustumPlanes[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._frustumPlanes[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._frustumPlanes[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._frustumPlanes[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._frustumPlanes[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._frustumPlanes[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;
this._frustumPlanesDirty = false;
};
Camera.prototype.pInvalidateHierarchicalProperties = function (bitFlag) {
if (_super.prototype.pInvalidateHierarchicalProperties.call(this, bitFlag))
return true;
if (this._hierarchicalPropsDirty & HierarchicalProperties_1.default.SCENE_TRANSFORM) {
this._viewProjectionDirty = true;
this._frustumPlanesDirty = true;
}
return false;
};
Object.defineProperty(Camera.prototype, "projection", {
/**
*
*/
get: function () {
return this._projection;
},
set: function (value) {
if (this._projection == value)
return;
if (!value)
throw new Error("Projection cannot be null!");
this._projection.removeEventListener(ProjectionEvent_1.default.MATRIX_CHANGED, this._onProjectionMatrixChangedDelegate);
this._projection = value;
this._projection.addEventListener(ProjectionEvent_1.default.MATRIX_CHANGED, this._onProjectionMatrixChangedDelegate);
this.dispatchEvent(new CameraEvent_1.default(CameraEvent_1.default.PROJECTION_CHANGED, this));
},
enumerable: true,
configurable: true
});
Object.defineProperty(Camera.prototype, "viewProjection", {
/**
*
*/
get: function () {
if (this._viewProjectionDirty) {
this._viewProjection.copyFrom(this.inverseSceneTransform);
this._viewProjection.append(this._projection.matrix);
this._viewProjectionDirty = false;
}
return this._viewProjection;
},
enumerable: true,
configurable: true
});
/**
* Calculates the ray in scene space from the camera to the given normalized coordinates in screen space.
*
* @param nX The normalised x coordinate in screen space, -1 corresponds to the left edge of the viewport, 1 to the right.
* @param nY The normalised y coordinate in screen space, -1 corresponds to the top edge of the viewport, 1 to the bottom.
* @param sZ The z coordinate in screen space, representing the distance into the screen.
* @return The ray from the camera to the scene space position of the given screen coordinates.
*/
Camera.prototype.getRay = function (nX, nY, sZ) {
return this.sceneTransform.deltaTransformVector(this._projection.unproject(nX, nY, sZ));
};
/**
* Calculates the normalised position in screen space of the given scene position.
*
* @param point3d the position vector of the scene coordinates to be projected.
* @return The normalised screen position of the given scene coordinates.
*/
Camera.prototype.project = function (point3d) {
return this._projection.project(this.inverseSceneTransform.transformVector(point3d));
};
/**
* Calculates the scene position of the given normalized coordinates in screen space.
*
* @param nX The normalised x coordinate in screen space, minus the originX offset of the projection property.
* @param nY The normalised y coordinate in screen space, minus the originY offset of the projection property.
* @param sZ The z coordinate in screen space, representing the distance into the screen.
* @return The scene position of the given screen coordinates.
*/
Camera.prototype.unproject = function (nX, nY, sZ) {
return this.sceneTransform.transformVector(this._projection.unproject(nX, nY, sZ));
};
Camera.prototype._applyRenderer = function (renderer) {
// Since this getter is invoked every iteration of the render loop, and
// the prefab construct could affect the sub-sprites, the prefab is
// validated here to give it a chance to rebuild.
if (this._iSourcePrefab)
this._iSourcePrefab._iValidate();
//nothing to do here
};
Camera.assetType = "[asset Camera]";
return Camera;
}(DisplayObjectContainer_1.default));
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = Camera;
/***/ },
/* 3 */
/***/ function(module, exports, __webpack_require__) {
"use strict";
var Box_1 = __webpack_require__(4);
var MathConsts_1 = __webpack_require__(6);
var Orientation3D_1 = __webpack_require__(7);
var Vector3D_1 = __webpack_require__(5);
var ArgumentError_1 = __webpack_require__(8);
var Matrix3D = (function () {
/**
* Creates a Matrix3D object.
*/
function Matrix3D(v) {
if (v === void 0) { v = null; }
/**
* A Vector of 16 Numbers, where every four elements is a column of a 4x4 matrix.
*
* <p>An exception is thrown if the rawData property is set to a matrix that is not invertible. The Matrix3D
* object must be invertible. If a non-invertible matrix is needed, create a subclass of the Matrix3D object.</p>
*/
this.rawData = new Float32Array(16);
this._position = new Vector3D_1.default();
this._positionDirty = true;
if (v != null && v.length == 16) {
this.copyRawDataFrom(v);
}
else {
this.identity();
}
}
/**
* Appends the matrix by multiplying another Matrix3D object by the current Matrix3D object.
*/
Matrix3D.prototype.append = function (lhs) {
var lrd = lhs.rawData;
var m111 = this.rawData[0];
var m112 = this.rawData[1];
var m113 = this.rawData[2];
var m114 = this.rawData[3];
var m121 = this.rawData[4];
var m122 = this.rawData[5];
var m123 = this.rawData[6];
var m124 = this.rawData[7];
var m131 = this.rawData[8];
var m132 = this.rawData[9];
var m133 = this.rawData[10];
var m134 = this.rawData[11];
var m141 = this.rawData[12];
var m142 = this.rawData[13];
var m143 = this.rawData[14];
var m144 = this.rawData[15];
var m211 = lrd[0];
var m212 = lrd[1];
var m213 = lrd[2];
var m214 = lrd[3];
var m221 = lrd[4];
var m222 = lrd[5];
var m223 = lrd[6];
var m224 = lrd[7];
var m231 = lrd[8];
var m232 = lrd[9];
var m233 = lrd[10];
var m234 = lrd[11];
var m241 = lrd[12];
var m242 = lrd[13];
var m243 = lrd[14];
var m244 = lrd[15];
this.rawData[0] = m111 * m211 + m112 * m221 + m113 * m231 + m114 * m241;
this.rawData[1] = m111 * m212 + m112 * m222 + m113 * m232 + m114 * m242;
this.rawData[2] = m111 * m213 + m112 * m223 + m113 * m233 + m114 * m243;
this.rawData[3] = m111 * m214 + m112 * m224 + m113 * m234 + m114 * m244;
this.rawData[4] = m121 * m211 + m122 * m221 + m123 * m231 + m124 * m241;
this.rawData[5] = m121 * m212 + m122 * m222 + m123 * m232 + m124 * m242;
this.rawData[6] = m121 * m213 + m122 * m223 + m123 * m233 + m124 * m243;
this.rawData[7] = m121 * m214 + m122 * m224 + m123 * m234 + m124 * m244;
this.rawData[8] = m131 * m211 + m132 * m221 + m133 * m231 + m134 * m241;
this.rawData[9] = m131 * m212 + m132 * m222 + m133 * m232 + m134 * m242;
this.rawData[10] = m131 * m213 + m132 * m223 + m133 * m233 + m134 * m243;
this.rawData[11] = m131 * m214 + m132 * m224 + m133 * m234 + m134 * m244;
this.rawData[12] = m141 * m211 + m142 * m221 + m143 * m231 + m144 * m241;
this.rawData[13] = m141 * m212 + m142 * m222 + m143 * m232 + m144 * m242;
this.rawData[14] = m141 * m213 + m142 * m223 + m143 * m233 + m144 * m243;
this.rawData[15] = m141 * m214 + m142 * m224 + m143 * m234 + m144 * m244;
this._positionDirty = true;
};
/**
* Appends an incremental rotation to a Matrix3D object.
*/
Matrix3D.prototype.appendRotation = function (degrees, axis) {
this.append(Matrix3D.getAxisRotation(axis.x, axis.y, axis.z, degrees));
};
/**
* Appends an incremental skew change along the x, y, and z axes to a Matrix3D object.
*/
Matrix3D.prototype.appendSkew = function (xSkew, ySkew, zSkew) {
if (xSkew == 0 && ySkew == 0 && zSkew == 0)
return;
var raw = Matrix3D.tempRawData;
raw[0] = 1;
raw[1] = 0;
raw[2] = 0;
raw[3] = 0;
raw[4] = xSkew;
raw[5] = 1;
raw[6] = 0;
raw[7] = 0;
raw[8] = ySkew;
raw[9] = zSkew;
raw[10] = 1;
raw[11] = 0;
raw[12] = 0;
raw[13] = 0;
raw[14] = 0;
raw[15] = 1;
this.append(Matrix3D.tempMatrix);
};
/**
* Appends an incremental scale change along the x, y, and z axes to a Matrix3D object.
*/
Matrix3D.prototype.appendScale = function (xScale, yScale, zScale) {
if (xScale == 1 && yScale == 1 && zScale == 1)
return;
var raw = Matrix3D.tempRawData;
raw[0] = xScale;
raw[1] = 0;
raw[2] = 0;
raw[3] = 0;
raw[4] = 0;
raw[5] = yScale;
raw[6] = 0;
raw[7] = 0;
raw[8] = 0;
raw[9] = 0;
raw[10] = zScale;
raw[11] = 0;
raw[12] = 0;
raw[13] = 0;
raw[14] = 0;
raw[15] = 1;
this.append(Matrix3D.tempMatrix);
};
/**
* Appends an incremental translation, a repositioning along the x, y, and z axes, to a Matrix3D object.
*/
Matrix3D.prototype.appendTranslation = function (x, y, z) {
this.rawData[12] += x;
this.rawData[13] += y;
this.rawData[14] += z;
this._positionDirty = true;
};
/**
* Returns a new Matrix3D object that is an exact copy of the current Matrix3D object.
*/
Matrix3D.prototype.clone = function () {
return new Matrix3D(this.rawData);
};
/**
* Copies a Vector3D object into specific column of the calling Matrix3D object.
*/
Matrix3D.prototype.copyColumnFrom = function (column, vector3D) {
switch (column) {
case 0:
this.rawData[0] = vector3D.x;
this.rawData[1] = vector3D.y;
this.rawData[2] = vector3D.z;
this.rawData[3] = vector3D.w;
break;
case 1:
this.rawData[4] = vector3D.x;
this.rawData[5] = vector3D.y;
this.rawData[6] = vector3D.z;
this.rawData[7] = vector3D.w;
break;
case 2:
this.rawData[8] = vector3D.x;
this.rawData[9] = vector3D.y;
this.rawData[10] = vector3D.z;
this.rawData[11] = vector3D.w;
break;
case 3:
this.rawData[12] = vector3D.x;
this.rawData[13] = vector3D.y;
this.rawData[14] = vector3D.z;
this.rawData[15] = vector3D.w;
break;
default:
throw new ArgumentError_1.default("ArgumentError, Column " + column + " out of bounds [0, ..., 3]");
}
};
/**
* Copies specific column of the calling Matrix3D object into the Vector3D object.
*/
Matrix3D.prototype.copyColumnTo = function (column, vector3D) {
switch (column) {
case 0:
vector3D.x = this.rawData[0];
vector3D.y = this.rawData[1];
vector3D.z = this.rawData[2];
vector3D.w = this.rawData[3];
break;
case 1:
vector3D.x = this.rawData[4];
vector3D.y = this.rawData[5];
vector3D.z = this.rawData[6];
vector3D.w = this.rawData[7];
break;
case 2:
vector3D.x = this.rawData[8];
vector3D.y = this.rawData[9];
vector3D.z = this.rawData[10];
vector3D.w = this.rawData[11];
break;
case 3:
vector3D.x = this.rawData[12];
vector3D.y = this.rawData[13];
vector3D.z = this.rawData[14];
vector3D.w = this.rawData[15];
break;
default:
throw new ArgumentError_1.default("ArgumentError, Column " + column + " out of bounds [0, ..., 3]");
}
};
/**
* Copies all of the matrix data from the source Matrix3D object into the calling Matrix3D object.
*/
Matrix3D.prototype.copyFrom = function (source) {
var sourceRaw = source.rawData;
var len = sourceRaw.length;
for (var c = 0; c < len; c++)
this.rawData[c] = sourceRaw[c];
};
/**
* Copies this Matrix3D object into a destination Matrix3D object.
*/
Matrix3D.prototype.copyTo = function (target) {
var targetRaw = target.rawData;
var len = this.rawData.length;
for (var c = 0; c < len; c++)
targetRaw[c] = this.rawData[c];
};
Matrix3D.prototype.copyRawDataFrom = function (vector, index, transpose) {
if (index === void 0) { index = 0; }
if (transpose === void 0) { transpose = false; }
if (transpose)
this.transpose();
var len = vector.length - index;
for (var c = 0; c < len; c++)
this.rawData[c] = vector[c + index];
if (transpose)
this.transpose();
};
Matrix3D.prototype.copyRawDataTo = function (vector, index, transpose) {
if (index === void 0) { index = 0; }
if (transpose === void 0) { transpose = false; }
if (transpose)
this.transpose();
var len = this.rawData.length;
for (var c = 0; c < len; c++)
vector[c + index] = this.rawData[c];
if (transpose)
this.transpose();
};
/**
* Copies a Vector3D object into specific row of the calling Matrix3D object.
*/
Matrix3D.prototype.copyRowFrom = function (row, vector3D) {
switch (row) {
case 0:
this.rawData[0] = vector3D.x;
this.rawData[4] = vector3D.y;
this.rawData[8] = vector3D.z;
this.rawData[12] = vector3D.w;
break;
case 1:
this.rawData[1] = vector3D.x;
this.rawData[5] = vector3D.y;
this.rawData[9] = vector3D.z;
this.rawData[13] = vector3D.w;
break;
case 2:
this.rawData[2] = vector3D.x;
this.rawData[6] = vector3D.y;
this.rawData[10] = vector3D.z;
this.rawData[14] = vector3D.w;
break;
case 3:
this.rawData[3] = vector3D.x;
this.rawData[7] = vector3D.y;
this.rawData[11] = vector3D.z;
this.rawData[15] = vector3D.w;
break;
default:
throw new ArgumentError_1.default("ArgumentError, Row " + row + " out of bounds [0, ..., 3]");
}
};
/**
* Copies specific row of the calling Matrix3D object into the Vector3D object.
*/
Matrix3D.prototype.copyRowTo = function (row, vector3D) {
switch (row) {
case 0:
vector3D.x = this.rawData[0];
vector3D.y = this.rawData[4];
vector3D.z = this.rawData[8];
vector3D.w = this.rawData[12];
break;
case 1:
vector3D.x = this.rawData[1];
vector3D.y = this.rawData[5];
vector3D.z = this.rawData[9];
vector3D.w = this.rawData[13];
break;
case 2:
vector3D.x = this.rawData[2];
vector3D.y = this.rawData[6];
vector3D.z = this.rawData[10];
vector3D.w = this.rawData[14];
break;
case 3:
vector3D.x = this.rawData[3];
vector3D.y = this.rawData[7];
vector3D.z = this.rawData[11];
vector3D.w = this.rawData[15];
break;
default:
throw new ArgumentError_1.default("ArgumentError, Row " + row + " out of bounds [0, ..., 3]");
}
};
/**
* Returns the transformation matrix's translation, rotation, and scale settings as a Vector of three Vector3D objects.
*/
Matrix3D.prototype.decompose = function (orientationStyle) {
if (orientationStyle === void 0) { orientationStyle = "eulerAngles"; }
var q;
if (this._components == null)
this._components = [null, new Vector3D_1.default(), new Vector3D_1.default(), new Vector3D_1.default()];
var colX = new Vector3D_1.default(this.rawData[0], this.rawData[1], this.rawData[2]);
var colY = new Vector3D_1.default(this.rawData[4], this.rawData[5], this.rawData[6]);
var colZ = new Vector3D_1.default(this.rawData[8], this.rawData[9], this.rawData[10]);
var scale = this._components[3];
var skew = this._components[2];
//compute X scale factor and normalise colX
scale.x = colX.length;
colX.scaleBy(1 / scale.x);
//compute XY shear factor and make colY orthogonal to colX
skew.x = colX.dotProduct(colY);
colY = Vector3D_1.default.combine(colY, colX, 1, -skew.x);
//compute Y scale factor and normalise colY
scale.y = colY.length;
colY.scaleBy(1 / scale.y);
skew.x /= scale.y;
//compute XZ and YZ shears and make colZ orthogonal to colX and colY
skew.y = colX.dotProduct(colZ);
colZ = Vector3D_1.default.combine(colZ, colX, 1, -skew.y);
skew.z = colY.dotProduct(colZ);
colZ = Vector3D_1.default.combine(colZ, colY, 1, -skew.z);
//compute Z scale and normalise colZ
scale.z = colZ.length;
colZ.scaleBy(1 / scale.z);
skew.y /= scale.z;
skew.z /= scale.z;
//at this point, the matrix (in cols) is orthonormal
//check for a coordinate system flip. If the determinant is -1, negate the z scaling factor
if (colX.dotProduct(colY.crossProduct(colZ)) < 0) {
scale.z = -scale.z;
colZ.x = -colZ.x;
colZ.y = -colZ.y;
colZ.z = -colZ.z;
}
var rot = this._components[1];
switch (orientationStyle) {
case Orientation3D_1.default.AXIS_ANGLE:
rot.w = Math.acos((colX.x + colY.y + colZ.z - 1) / 2);
var len = Math.sqrt((colY.z - colZ.y) * (colY.z - colZ.y) + (colZ.x - colX.z) * (colZ.x - colX.z) + (colX.y - colY.x) * (colX.y - colY.x));
rot.x = (colY.z - colZ.y) / len;
rot.y = (colZ.x - colX.z) / len;
rot.z = (colX.y - colY.x) / len;
break;
case Orientation3D_1.default.QUATERNION:
var tr = colX.x + colY.y + colZ.z;
if (tr > 0) {
rot.w = Math.sqrt(1 + tr) / 2;
rot.x = (colY.z - colZ.y) / (4 * rot.w);
rot.y = (colZ.x - colX.z) / (4 * rot.w);
rot.z = (colX.y - colY.x) / (4 * rot.w);
}
else if ((colX.x > colY.y) && (colX.x > colZ.z)) {
rot.x = Math.sqrt(1 + colX.x - colY.y - colZ.z) / 2;
rot.w = (colY.z - colZ.y) / (4 * rot.x);
rot.y = (colX.y + colY.x) / (4 * rot.x);
rot.z = (colZ.x + colX.z) / (4 * rot.x);
}
else if (colY.y > colZ.z) {
rot.y = Math.sqrt(1 + colY.y - colX.x - colZ.z) / 2;
rot.x = (colX.y + colY.x) / (4 * rot.y);
rot.w = (colZ.x - colX.z) / (4 * rot.y);
rot.z = (colY.z + colZ.y) / (4 * rot.y);
}
else {
rot.z = Math.sqrt(1 + colZ.z - colX.x - colY.y) / 2;
rot.x = (colZ.x + colX.z) / (4 * rot.z);
rot.y = (colY.z + colZ.y) / (4 * rot.z);
rot.w = (colX.y - colY.x) / (4 * rot.z);
}
break;
case Orientation3D_1.default.EULER_ANGLES:
rot.y = Math.asin(-colX.z);
//var cos:number = Math.cos(rot.y);
if (colX.z != 1 && colX.z != -1) {
rot.x = Math.atan2(colY.z, colZ.z);
rot.z = Math.atan2(colX.y, colX.x);
}
else {
rot.z = 0;
rot.x = Math.atan2(colY.x, colY.y);
}
break;
}
this._components[0] = this.position;
return this._components;
};
/**
* Uses the transformation matrix without its translation elements to transform a Vector3D object from one space
* coordinate to another.
*/
Matrix3D.prototype.deltaTransformVector = function (v, t) {
if (t === void 0) { t = null; }
var x = v.x;
var y = v.y;
var z = v.z;
if (!t)
t = new Vector3D_1.default();
t.x = x * this.rawData[0] + y * this.rawData[4] + z * this.rawData[8];
t.y = x * this.rawData[1] + y * this.rawData[5] + z * this.rawData[9];
t.z = x * this.rawData[2] + y * this.rawData[6] + z * this.rawData[10];
t.w = x * this.rawData[3] + y * this.rawData[7] + z * this.rawData[11];
return t;
};
/**
* Converts the current matrix to an identity or unit matrix.
*/
Matrix3D.prototype.identity = function () {
this.rawData[0] = 1;
this.rawData[1] = 0;
this.rawData[2] = 0;
this.rawData[3] = 0;
this.rawData[4] = 0;
this.rawData[5] = 1;
this.rawData[6] = 0;
this.rawData[7] = 0;
this.rawData[8] = 0;
this.rawData[9] = 0;
this.rawData[10] = 1;
this.rawData[11] = 0;
this.rawData[12] = 0;
this.rawData[13] = 0;
this.rawData[14] = 0;
this.rawData[15] = 1;
this._positionDirty = true;
};
/**
* [static] Interpolates the translation, rotation, and scale transformation of one matrix toward those of the target matrix.
*/
Matrix3D.interpolate = function (thisMat, toMat, percent) {
var m = new Matrix3D();
for (var i = 0; i < 16; ++i)
m.rawData[i] = thisMat.rawData[i] + (toMat.rawData[i] - thisMat.rawData[i]) * percent;
return m;
};
/**
* Interpolates this matrix towards the translation, rotation, and scale transformations of the target matrix.
*/
Matrix3D.prototype.interpolateTo = function (toMat, percent) {
for (var i = 0; i < 16; ++i)
this.rawData[i] = this.rawData[i] + (toMat.rawData[i] - this.rawData[i]) * percent;
};
/**
* Inverts the current matrix.
*/
Matrix3D.prototype.invert = function () {
var d = this.determinant;
var invertable = Math.abs(d) > 0.00000000001;
if (invertable) {
d = 1 / d;
var m11 = this.rawData[0];
var m12 = this.rawData[1];
var m13 = this.rawData[2];
var m14 = this.rawData[3];
var m21 = this.rawData[4];
var m22 = this.rawData[5];
var m23 = this.rawData[6];
var m24 = this.rawData[7];
var m31 = this.rawData[8];
var m32 = this.rawData[9];
var m33 = this.rawData[10];
var m34 = this.rawData[11];
var m41 = this.rawData[12];
var m42 = this.rawData[13];
var m43 = this.rawData[14];
var m44 = this.rawData[15];
this.rawData[0] = d * (m22 * (m33 * m44 - m43 * m34) - m32 * (m23 * m44 - m43 * m24) + m42 * (m23 * m34 - m33 * m24));
this.rawData[1] = -d * (m12 * (m33 * m44 - m43 * m34) - m32 * (m13 * m44 - m43 * m14) + m42 * (m13 * m34 - m33 * m14));
this.rawData[2] = d * (m12 * (m23 * m44 - m43 * m24) - m22 * (m13 * m44 - m43 * m14) + m42 * (m13 * m24 - m23 * m14));
this.rawData[3] = -d * (m12 * (m23 * m34 - m33 * m24) - m22 * (m13 * m34 - m33 * m14) + m32 * (m13 * m24 - m23 * m14));
this.rawData[4] = -d * (m21 * (m33 * m44 - m43 * m34) - m31 * (m23 * m44 - m43 * m24) + m41 * (m23 * m34 - m33 * m24));
this.rawData[5] = d * (m11 * (m33 * m44 - m43 * m34) - m31 * (m13 * m44 - m43 * m14) + m41 * (m13 * m34 - m33 * m14));
this.rawData[6] = -d * (m11 * (m23 * m44 - m43 * m24) - m21 * (m13 * m44 - m43 * m14) + m41 * (m13 * m24 - m23 * m14));
this.rawData[7] = d * (m11 * (m23 * m34 - m33 * m24) - m21 * (m13 * m34 - m33 * m14) + m31 * (m13 * m24 - m23 * m14));
this.rawData[8] = d * (m21 * (m32 * m44 - m42 * m34) - m31 * (m22 * m44 - m42 * m24) + m41 * (m22 * m34 - m32 * m24));
this.rawData[9] = -d * (m11 * (m32 * m44 - m42 * m34) - m31 * (m12 * m44 - m42 * m14) + m41 * (m12 * m34 - m32 * m14));
this.rawData[10] = d * (m11 * (m22 * m44 - m42 * m24) - m21 * (m12 * m44 - m42 * m14) + m41 * (m12 * m24 - m22 * m14));
this.rawData[11] = -d * (m11 * (m22 * m34 - m32 * m24) - m21 * (m12 * m34 - m32 * m14) + m31 * (m12 * m24 - m22 * m14));
this.rawData[12] = -d * (m21 * (m32 * m43 - m42 * m33) - m31 * (m22 * m43 - m42 * m23) + m41 * (m22 * m33 - m32 * m23));
this.rawData[13] = d * (m11 * (m32 * m43 - m42 * m33) - m31 * (m12 * m43 - m42 * m13) + m41 * (m12 * m33 - m32 * m13));
this.rawData[14] = -d * (m11 * (m22 * m43 - m42 * m23) - m21 * (m12 * m43 - m42 * m13) + m41 * (m12 * m23 - m22 * m13));
this.rawData[15] = d * (m11 * (m22 * m33 - m32 * m23) - m21 * (m12 * m33 - m32 * m13) + m31 * (m12 * m23 - m22 * m13));
}
this._positionDirty = true;
return invertable;
};
/* TODO implement pointAt
public pointAt( pos:Vector3D, at:Vector3D = null, up:Vector3D = null )
{
}
*/
/**
* Prepends a matrix by multiplying the current Matrix3D object by another Matrix3D object.
*/
Matrix3D.prototype.prepend = function (rhs) {
var m111 = rhs.rawData[0];
var m112 = rhs.rawData[1];
var m113 = rhs.rawData[2];
var m114 = rhs.rawData[3];
var m121 = rhs.rawData[4];
var m122 = rhs.rawData[5];
var m123 = rhs.rawData[6];
var m124 = rhs.rawData[7];
var m131 = rhs.rawData[8];
var m132 = rhs.rawData[9];
var m133 = rhs.rawData[10];
var m134 = rhs.rawData[11];
var m141 = rhs.rawData[12];
var m142 = rhs.rawData[13];
var m143 = rhs.rawData[14];
var m144 = rhs.rawData[15];
var m211 = this.rawData[0];
var m212 = this.rawData[1];
var m213 = this.rawData[2];
var m214 = this.rawData[3];
var m221 = this.rawData[4];
var m222 = this.rawData[5];
var m223 = this.rawData[6];
var m224 = this.rawData[7];
var m231 = this.rawData[8];
var m232 = this.rawData[9];
var m233 = this.rawData[10];
var m234 = this.rawData[11];
var m241 = this.rawData[12];
var m242 = this.rawData[13];
var m243 = this.rawData[14];
var m244 = this.rawData[15];
this.rawData[0] = m111 * m211 + m112 * m221 + m113 * m231 + m114 * m241;
this.rawData[1] = m111 * m212 + m112 * m222 + m113 * m232 + m114 * m242;
this.rawData[2] = m111 * m213 + m112 * m223 + m113 * m233 + m114 * m243;
this.rawData[3] = m111 * m214 + m112 * m224 + m113 * m234 + m114 * m244;
this.rawData[4] = m121 * m211 + m122 * m221 + m123 * m231 + m124 * m241;
this.rawData[5] = m121 * m212 + m122 * m222 + m123 * m232 + m124 * m242;
this.rawData[6] = m121 * m213 + m122 * m223 + m123 * m233 + m124 * m243;
this.rawData[7] = m121 * m214 + m122 * m224 + m123 * m234 + m124 * m244;
this.rawData[8] = m131 * m211 + m132 * m221 + m133 * m231 + m134 * m241;
this.rawData[9] = m131 * m212 + m132 * m222 + m133 * m232 + m134 * m242;
this.rawData[10] = m131 * m213 + m132 * m223 + m133 * m233 + m134 * m243;
this.rawData[11] = m131 * m214 + m132 * m224 + m133 * m234 + m134 * m244;
this.rawData[12] = m141 * m211 + m142 * m221 + m143 * m231 + m144 * m241;
this.rawData[13] = m141 * m212 + m142 * m222 + m143 * m232 + m144 * m242;
this.rawData[14] = m141 * m213 + m142 * m223 + m143 * m233 + m144 * m243;
this.rawData[15] = m141 * m214 + m142 * m224 + m143 * m234 + m144 * m244;
this._positionDirty = true;
};
/**
* Prepends an incremental rotation to a Matrix3D object.
*/
Matrix3D.prototype.prependRotation = function (degrees, axis) {
var m = Matrix3D.getAxisRotation(axis.x, axis.y, axis.z, degrees);
/*
if ( pivot != null )
{
var p:Vector3D = pivot;
m.appendTranslation( p.x, p.y, p.z );
}
*/
this.prepend(m);
};
/**
* Prepends an incremental scale change along the x, y, and z axes to a Matrix3D object.
*/
Matrix3D.prototype.prependScale = function (xScale, yScale, zScale) {
if (xScale == 1 && yScale == 1 && zScale == 1)
return;
var raw = Matrix3D.tempRawData;
raw[0] = xScale;
raw[1] = 0;
raw[2] = 0;
raw[3] = 0;
raw[4] = 0;
raw[5] = yScale;
raw[6] = 0;
raw[7] = 0;
raw[8] = 0;
raw[9] = 0;
raw[10] = zScale;
raw[11] = 0;
raw[12] = 0;
raw[13] = 0;
raw[14] = 0;
raw[15] = 1;
this.prepend(Matrix3D.tempMatrix);
};
/**
* Prepends an incremental translation, a repositioning along the x, y, and z axes, to a Matrix3D object.
*/
Matrix3D.prototype.prependTranslation = function (x, y, z) {
var raw = Matrix3D.tempRawData;
raw[0] = 1;
raw[1] = 0;
raw[2] = 0;
raw[3] = 0;
raw[4] = 0;
raw[5] = 1;
raw[6] = 0;
raw[7] = 0;
raw[8] = 0;
raw[9] = 0;
raw[10] = 1;
raw[11] = 0;
raw[12] = x;
raw[13] = y;
raw[14] = z;
raw[15] = 1;
this.prepend(Matrix3D.tempMatrix);
};
// TODO orientationStyle
/**
* Sets the transformation matrix's translation, rotation, and scale settings.
*/
Matrix3D.prototype.recompose = function (components) {
var pos = components[0] || this.position;
this.identity();
var scale = components[3];
if (scale && (scale.x != 1 || scale.y != 1 || scale.z != 1))
this.appendScale(scale.x, scale.y, scale.z);
var skew = components[2];
if (skew && (skew.x != 0 || skew.y != 0 || skew.z != 0))
this.appendSkew(skew.x, skew.y, skew.z);
var sin;
var cos;
var raw = Matrix3D.tempRawData;
raw[12] = 0;
raw[13] = 0;
raw[14] = 0;
raw[15] = 0;
var rotation = components[1];
if (rotation) {
var angle = -rotation.x;
if (angle != 0) {
sin = Math.sin(angle);
cos = Math.cos(angle);
raw[0] = 1;
raw[1] = 0;
raw[2] = 0;
raw[3] = 0;
raw[4] = 0;
raw[5] = cos;
raw[6] = -sin;
raw[7] = 0;
raw[8] = 0;
raw[9] = sin;
raw[10] = cos;
raw[11] = 0;
this.append(Matrix3D.tempMatrix);
}
angle = -rotation.y;
if (angle != 0) {
sin = Math.sin(angle);
cos = Math.cos(angle);
raw[0] = cos;
raw[1] = 0;
raw[2] = sin;
raw[3] = 0;
raw[4] = 0;
raw[5] = 1;
raw[6] = 0;
raw[7] = 0;
raw[8] = -sin;
raw[9] = 0;
raw[10] = cos;
raw[11] = 0;
this.append(Matrix3D.tempMatrix);
}
angle = -rotation.z;
if (angle != 0) {
sin = Math.sin(angle);
cos = Math.cos(angle);
raw[0] = cos;
raw[1] = -sin;
raw[2] = 0;
raw[3] = 0;
raw[4] = sin;
raw[5] = cos;
raw[6] = 0;
raw[7] = 0;
raw[8] = 0;
raw[9] = 0;
raw[10] = 1;
raw[11] = 0;
this.append(Matrix3D.tempMatrix);
}
}
this.rawData[12] = pos.x;
this.rawData[13] = pos.y;
this.rawData[14] = pos.z;
if (components[0])
this._positionDirty = true;
this.rawData[15] = 1;
return true;
};
Matrix3D.prototype.transformVector = function (v, t) {
if (t === void 0) { t = null; }
if (v == null)
return t || new Vector3D_1.default();
var x = v.x;
var y = v.y;
var z = v.z;
if (!t)
t = new Vector3D_1.default();
t.x = x * this.rawData[0] + y * this.rawData[4] + z * this.rawData[8] + this.rawData[12];
t.y = x * this.rawData[1] + y * this.rawData[5] + z * this.rawData[9] + this.rawData[13];
t.z = x * this.rawData[2] + y * this.rawData[6] + z * this.rawData[10] + this.rawData[14];
t.w = x * this.rawData[3] + y * this.rawData[7] + z * this.rawData[11] + this.rawData[15];
return t;
};
Matrix3D.prototype.transformBox = function (b, t) {
if (t === void 0) { t = null; }
if (b == null)
return t || new Box_1.default();
var minX, minY, minZ;
var maxX, maxY, maxZ;
maxX = b.width + (minX = b.x);
maxY = b.height + (minY = b.y);
maxZ = b.depth + (minZ = b.z);
if (!t)
t = new Box_1.default();
//TODO: take account of shear
t.width = maxX * this.rawData[0] + maxY * this.rawData[4] + maxZ * this.rawData[8] + this.rawData[12] - (t.x = minX * this.rawData[0] + minY * this.rawData[4] + minZ * this.rawData[8] + this.rawData[12]);
t.height = maxX * this.rawData[1] + maxY * this.rawData[5] + maxZ * this.rawData[9] + this.rawData[13] - (t.y = minX * this.rawData[1] + minY * this.rawData[5] + minZ * this.rawData[9] + this.rawData[13]);
t.depth = maxX * this.rawData[2] + maxY * this.rawData[6] + maxZ * this.rawData[10] + this.rawData[14] - (t.z = minX * this.rawData[2] + minY * this.rawData[6] + minZ * this.rawData[10] + this.rawData[14]);
return t;
};
/**
* Uses the transformation matrix to transform a Vector of Numbers from one coordinate space to another.
*/
Matrix3D.prototype.transformVectors = function (vin, vout) {
var i = 0;
var x = 0, y = 0, z = 0;
while (i + 3 <= vin.length) {
x = vin[i];
y = vin[i + 1];
z = vin[i + 2];
vout[i] = x * this.rawData[0] + y * this.rawData[4] + z * this.rawData[8] + this.rawData[12];
vout[i + 1] = x * this.rawData[1] + y * this.rawData[5] + z * this.rawData[9] + this.rawData[13];
vout[i + 2] = x * this.rawData[2] + y * this.rawData[6] + z * this.rawData[10] + this.rawData[14];
i += 3;
}
};
/**
* Converts the current Matrix3D object to a matrix where the rows and columns are swapped.
*/
Matrix3D.prototype.transpose = function () {
var raw = Matrix3D.tempRawData;
this.copyRawDataTo(raw);
this.rawData[1] = raw[4];
this.rawData[2] = raw[8];
this.rawData[3] = raw[12];
this.rawData[4] = raw[1];
this.rawData[6] = raw[9];
this.rawData[7] = raw[13];
this.rawData[8] = raw[2];
this.rawData[9] = raw[6];
this.rawData[11] = raw[14];
this.rawData[12] = raw[3];
this.rawData[13] = raw[7];
this.rawData[14] = raw[11];
this._positionDirty = true;
};
Matrix3D.getAxisRotation = function (x, y, z, degrees) {
// internal class use by rotations which have been tested
var m = new Matrix3D();
var rad = degrees * MathConsts_1.default.DEGREES_TO_RADIANS;
var c = Math.cos(rad);
var s = Math.sin(rad);
var t = 1 - c;
var tmp1, tmp2;
m.rawData[0] = c + x * x * t;
m.rawData[5] = c + y * y * t;
m.rawData[10] = c + z * z * t;
tmp1 = x * y * t;
tmp2 = z * s;
m.rawData[1] = tmp1 + tmp2;
m.rawData[4] = tmp1 - tmp2;
tmp1 = x * z * t;
tmp2 = y * s;
m.rawData[8] = tmp1 + tmp2;
m.rawData[2] = tmp1 - tmp2;
tmp1 = y * z * t;
tmp2 = x * s;
m.rawData[9] = tmp1 - tmp2;
m.rawData[6] = tmp1 + tmp2;
return m;
};
Object.defineProperty(Matrix3D.prototype, "determinant", {
/**
* [read-only] A Number that determines whether a matrix is invertible.
*/
get: function () {
return ((this.rawData[0] * this.rawData[5] - this.rawData[4] * this.rawData[1]) * (this.rawData[10] * this.rawData[15] - this.rawData[14] * this.rawData[11]) - (this.rawData[0] * this.rawData[9] - this.rawData[8] * this.rawData[1]) * (this.rawData[6] * this.rawData[15] - this.rawData[14] * this.rawData[7]) + (this.rawData[0] * this.rawData[13] - this.rawData[12] * this.rawData[1]) * (this.rawData[6] * this.rawData[11] - this.rawData[10] * this.rawData[7]) + (this.rawData[4] * this.rawData[9] - this.rawData[8] * this.rawData[5]) * (this.rawData[2] * this.rawData[15] - this.rawData[14] * this.rawData[3]) - (this.rawData[4] * this.rawData[13] - this.rawData[12] * this.rawData[5]) * (this.rawData[2] * this.rawData[11] - this.rawData[10] * this.rawData[3]) + (this.rawData[8] * this.rawData[13] - this.rawData[12] * this.rawData[9]) * (this.rawData[2] * this.rawData[7] - this.rawData[6] * this.rawData[3]));
},
enumerable: true,
configurable: true
});
Object.defineProperty(Matrix3D.prototype, "position", {
/**
* A Vector3D object that holds the position, the 3D coordinate (x,y,z) of a display object within the
* transformation's frame of reference.
*/
get: function () {
if (this._positionDirty) {
this._positionDirty = false;
this._position.x = this.rawData[12];
this._position.y = this.rawData[13];
this._position.z = this.rawData[14];
}
return this._position;
},
enumerable: true,
configurable: true
});
Matrix3D.prototype.invalidatePosition = function () {
this._positionDirty = true;
};
Matrix3D.prototype.toFixed = function (decimalPlace) {
var magnitude = Math.pow(10, decimalPlace);
return "matrix3d(" + Math.round(this.rawData[0] * magnitude) / magnitude + "," + Math.round(this.rawData[1] * magnitude) / magnitude + "," + Math.round(this.rawData[2] * magnitude) / magnitude + "," + Math.round(this.rawData[3] * magnitude) / magnitude + "," + Math.round(this.rawData[4] * magnitude) / magnitude + "," + Math.round(this.rawData[5] * magnitude) / magnitude + "," + Math.round(this.rawData[6] * magnitude) / magnitude + "," + Math.round(this.rawData[7] * magnitude) / magnitude + "," + Math.round(this.rawData[8] * magnitude) / magnitude + "," + Math.round(this.rawData[9] * magnitude) / magnitude + "," + Math.round(this.rawData[10] * magnitude) / magnitude + "," + Math.round(this.rawData[11] * magnitude) / magnitude + "," + Math.round(this.rawData[12] * magnitude) / magnitude + "," + Math.round(this.rawData[13] * magnitude) / magnitude + "," + Math.round(this.rawData[14] * magnitude) / magnitude + "," + Math.round(this.rawData[15] * magnitude) / magnitude + ")";
};
Matrix3D.prototype.toString = function () {
return "matrix3d(" + Math.round(this.rawData[0] * 1000) / 1000 +