Преглед на файлове

Camera don't compute their projection matrix at each frame anymore.

Extension of the cache system in BABYLON.Camera to take appart the ViewMatrix and the ProjectionMatrix.

In derived camera, override _isSynchronizedViewMatrix() instead of _isSynchronized(). Nothing else change.
Julien преди 11 години
родител
ревизия
a0d7192806
променени са 3 файла, в които са добавени 68 реда и са изтрити 14 реда
  1. 5 5
      Babylon/Cameras/babylon.arcRotateCamera.js
  2. 61 7
      Babylon/Cameras/babylon.camera.js
  3. 2 2
      Babylon/Cameras/babylon.freeCamera.js

+ 5 - 5
Babylon/Cameras/babylon.arcRotateCamera.js

@@ -21,9 +21,9 @@ var BABYLON = BABYLON || {};
 
         this._viewMatrix = new BABYLON.Matrix();
 
-        this.getViewMatrix();
-
         BABYLON.ArcRotateCamera.prototype._initCache.call(this);
+        
+        this.getViewMatrix();
     };
     
     BABYLON.ArcRotateCamera.prototype = Object.create(BABYLON.Camera.prototype);
@@ -40,7 +40,7 @@ var BABYLON = BABYLON || {};
     BABYLON.ArcRotateCamera.prototype.upperRadiusLimit = null;
     BABYLON.ArcRotateCamera.prototype.angularSensibility = 1000.0;
 
-    BABYLON.ArcRotateCamera.prototype._getTargetPosition = function () {       
+    BABYLON.ArcRotateCamera.prototype._getTargetPosition = function () {
         return this.target.position || this.target;
     };
     
@@ -63,8 +63,8 @@ var BABYLON = BABYLON || {};
     };
 
     // Synchronized
-    BABYLON.ArcRotateCamera.prototype._isSynchronized = function () {
-        if (!BABYLON.Camera.prototype._isSynchronized.call(this))
+    BABYLON.ArcRotateCamera.prototype._isSynchronizedViewMatrix = function () {
+        if (!BABYLON.Camera.prototype._isSynchronizedViewMatrix.call(this))
             return false;
         
         return this._cache.target.equals(this._getTargetPosition())

+ 61 - 7
Babylon/Cameras/babylon.camera.js

@@ -21,6 +21,7 @@ var BABYLON = BABYLON || {};
         }
 
         this._computedViewMatrix = BABYLON.Matrix.Identity();
+        this._projectionMatrix = new BABYLON.Matrix();
 
         // Animations
         this.animations = [];
@@ -61,25 +62,79 @@ var BABYLON = BABYLON || {};
     BABYLON.Camera.prototype._initCache = function () {
         this._cache.position = this.position.clone();
         this._cache.upVector = this.upVector.clone();
+
+        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();
     };
 
     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) {
+            if (this.mode === BABYLON.Camera.PERSPECTIVE_CAMERA) {
+                r = r & this._cache.fov === this.fov
+                     && this._cache.aspectRatio === engine.getAspectRatio();
+            }
+            else {
+                r = 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) {
@@ -131,8 +186,7 @@ var BABYLON = BABYLON || {};
     };
     
     BABYLON.Camera.prototype._computeViewMatrix = function (force) {
-
-        if (!force && this.isSynchronized()) {
+        if (!force && this._isSynchronizedViewMatrix()) {
             return this._computedViewMatrix;
         }
         
@@ -140,9 +194,9 @@ var BABYLON = BABYLON || {};
         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();

+ 2 - 2
Babylon/Cameras/babylon.freeCamera.js

@@ -82,8 +82,8 @@ var BABYLON = BABYLON || {};
     };
 
     // Synchronized
-    BABYLON.FreeCamera.prototype._isSynchronized = function () {
-        if (!BABYLON.Camera.prototype._isSynchronized.call(this))
+    BABYLON.FreeCamera.prototype._isSynchronizedViewMatrix = function () {
+        if (!BABYLON.Camera.prototype._isSynchronizedViewMatrix.call(this))
             return false;
 
         var lockedTargetPosition = this._getLockedTargetPosition();