|
@@ -4,6 +4,8 @@ var BABYLON = BABYLON || {};
|
|
|
|
|
|
(function () {
|
|
|
BABYLON.Camera = function (name, position, scene) {
|
|
|
+ BABYLON.Node.call(this);
|
|
|
+
|
|
|
this.name = name;
|
|
|
this.id = name;
|
|
|
this.position = position;
|
|
@@ -19,6 +21,7 @@ var BABYLON = BABYLON || {};
|
|
|
}
|
|
|
|
|
|
this._computedViewMatrix = BABYLON.Matrix.Identity();
|
|
|
+ this._projectionMatrix = new BABYLON.Matrix();
|
|
|
|
|
|
// Animations
|
|
|
this.animations = [];
|
|
@@ -28,6 +31,9 @@ var BABYLON = BABYLON || {};
|
|
|
|
|
|
// Viewport
|
|
|
this.viewport = new BABYLON.Viewport(0, 0, 1.0, 1.0);
|
|
|
+
|
|
|
+ //Cache
|
|
|
+ BABYLON.Camera.prototype._initCache.call(this);
|
|
|
};
|
|
|
|
|
|
BABYLON.Camera.prototype = Object.create(BABYLON.Node.prototype);
|
|
@@ -51,6 +57,84 @@ var BABYLON = BABYLON || {};
|
|
|
BABYLON.Camera.prototype.getScene = function () {
|
|
|
return this._scene;
|
|
|
};
|
|
|
+
|
|
|
+ //Cache
|
|
|
+ BABYLON.Camera.prototype._initCache = function () {
|
|
|
+ this._cache.position = new BABYLON.Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
|
|
|
+ this._cache.upVector = new BABYLON.Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
|
|
|
+
|
|
|
+ this._cache.mode = undefined;
|
|
|
+ this._cache.minZ = undefined;
|
|
|
+ this._cache.maxZ = undefined;
|
|
|
+
|
|
|
+ this._cache.fov = undefined;
|
|
|
+ this._cache.aspectRatio = undefined;
|
|
|
+
|
|
|
+ this._cache.orthoLeft = undefined;
|
|
|
+ this._cache.orthoRight = undefined;
|
|
|
+ this._cache.orthoBottom = undefined;
|
|
|
+ this._cache.orthoTop = undefined;
|
|
|
+ this._cache.renderWidth = undefined;
|
|
|
+ this._cache.renderHeight = undefined;
|
|
|
+ };
|
|
|
+
|
|
|
+ BABYLON.Camera.prototype._updateCache = function (ignoreParentClass) {
|
|
|
+ if(!ignoreParentClass)
|
|
|
+ BABYLON.Node.prototype._updateCache.call(this);
|
|
|
+
|
|
|
+ this._cache.position.copyFrom(this.position);
|
|
|
+ this._cache.upVector.copyFrom(this.upVector);
|
|
|
+
|
|
|
+ this._cache.mode = this.mode;
|
|
|
+ this._cache.minZ = this.minZ;
|
|
|
+ this._cache.maxZ = this.maxZ;
|
|
|
+
|
|
|
+ this._cache.fov = this.fov;
|
|
|
+ this._cache.aspectRatio = engine.getAspectRatio();
|
|
|
+
|
|
|
+ this._cache.orthoLeft = this.orthoLeft;
|
|
|
+ this._cache.orthoRight = this.orthoRight;
|
|
|
+ this._cache.orthoBottom = this.orthoBottom;
|
|
|
+ this._cache.orthoTop = this.orthoTop;
|
|
|
+ this._cache.renderWidth = engine.getRenderWidth()
|
|
|
+ this._cache.renderHeight = engine.getRenderHeight();
|
|
|
+ };
|
|
|
+
|
|
|
+ // Synchronized
|
|
|
+ BABYLON.Camera.prototype._isSynchronized = function () {
|
|
|
+ return this._isSynchronizedViewMatrix() && this._isSynchronizedProjectionMatrix();
|
|
|
+ };
|
|
|
+
|
|
|
+ BABYLON.Camera.prototype._isSynchronizedViewMatrix = function () {
|
|
|
+ if (!BABYLON.Node.prototype._isSynchronized.call(this))
|
|
|
+ return false;
|
|
|
+
|
|
|
+ return this._cache.position.equals(this.position)
|
|
|
+ && this._cache.upVector.equals(this.upVector);
|
|
|
+ };
|
|
|
+
|
|
|
+ BABYLON.Camera.prototype._isSynchronizedProjectionMatrix = function () {
|
|
|
+ var r = this._cache.mode === this.mode
|
|
|
+ && this._cache.minZ === this.minZ
|
|
|
+ && this._cache.maxZ === this.maxZ;
|
|
|
+
|
|
|
+ if (!r)
|
|
|
+ return false;
|
|
|
+
|
|
|
+ if (this.mode === BABYLON.Camera.PERSPECTIVE_CAMERA) {
|
|
|
+ r = this._cache.fov === this.fov
|
|
|
+ && this._cache.aspectRatio === engine.getAspectRatio();
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ r = this._cache.orthoLeft === this.orthoLeft
|
|
|
+ && this._cache.orthoRight === this.orthoRight
|
|
|
+ && this._cache.orthoBottom === this.orthoBottom
|
|
|
+ && this._cache.orthoTop === this.orthoTop
|
|
|
+ && this._cache.renderWidth === engine.getRenderWidth()
|
|
|
+ && this._cache.renderHeight === engine.getRenderHeight();
|
|
|
+ }
|
|
|
+ return r;
|
|
|
+ };
|
|
|
|
|
|
// Methods
|
|
|
BABYLON.Camera.prototype.attachControl = function (canvas) {
|
|
@@ -61,6 +145,11 @@ var BABYLON = BABYLON || {};
|
|
|
|
|
|
BABYLON.Camera.prototype._update = function () {
|
|
|
};
|
|
|
+
|
|
|
+ BABYLON.Camera.prototype._updateFromScene = function () {
|
|
|
+ this.updateCache();
|
|
|
+ this._update();
|
|
|
+ };
|
|
|
|
|
|
BABYLON.Camera.prototype.getWorldMatrix = function () {
|
|
|
var viewMatrix = this.getViewMatrix();
|
|
@@ -79,28 +168,35 @@ var BABYLON = BABYLON || {};
|
|
|
};
|
|
|
|
|
|
BABYLON.Camera.prototype.getViewMatrix = function () {
|
|
|
- this._computedViewMatrix = this._getViewMatrix();
|
|
|
+ this._computedViewMatrix = this._computeViewMatrix();
|
|
|
|
|
|
- if (this.parent && this.parent.getWorldMatrix) {
|
|
|
- if (!this._worldMatrix) {
|
|
|
- this._worldMatrix = BABYLON.Matrix.Identity();
|
|
|
- }
|
|
|
-
|
|
|
- this._computedViewMatrix.invertToRef(this._worldMatrix);
|
|
|
+ if(!this.parent
|
|
|
+ || !this.parent.getWorldMatrix
|
|
|
+ || (!this.hasNewParent() && this.parent.isSynchronized())) {
|
|
|
+ return this._computedViewMatrix;
|
|
|
+ }
|
|
|
+
|
|
|
+ this._computedViewMatrix.invertToRef(this._worldMatrix);
|
|
|
|
|
|
- this._worldMatrix.multiplyToRef(this.parent.getWorldMatrix(), this._computedViewMatrix);
|
|
|
+ this._worldMatrix.multiplyToRef(this.parent.getWorldMatrix(), this._computedViewMatrix);
|
|
|
|
|
|
- this._computedViewMatrix.invert();
|
|
|
+ this._computedViewMatrix.invert();
|
|
|
|
|
|
+ return this._computedViewMatrix;
|
|
|
+ };
|
|
|
+
|
|
|
+ BABYLON.Camera.prototype._computeViewMatrix = function (force) {
|
|
|
+ if (!force && this._isSynchronizedViewMatrix()) {
|
|
|
return this._computedViewMatrix;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ this._computedViewMatrix = this._getViewMatrix();
|
|
|
return this._computedViewMatrix;
|
|
|
};
|
|
|
|
|
|
- BABYLON.Camera.prototype.getProjectionMatrix = function () {
|
|
|
- if (!this._projectionMatrix) {
|
|
|
- this._projectionMatrix = new BABYLON.Matrix();
|
|
|
+ BABYLON.Camera.prototype.getProjectionMatrix = function (force) {
|
|
|
+ if(!force && this._isSynchronizedProjectionMatrix()) {
|
|
|
+ return this._projectionMatrix;
|
|
|
}
|
|
|
|
|
|
var engine = this._scene.getEngine();
|