Pārlūkot izejas kodu

Adding to AnaglyphFreeCamera
Cameras now can have subCameras
Optimizing AnaglyphArcRotateCamera

David Catuhe 11 gadi atpakaļ
vecāks
revīzija
0e223ffb8e

+ 63 - 38
Babylon/Cameras/babylon.anaglyphCamera.js

@@ -3,35 +3,38 @@
 var BABYLON = BABYLON || {};
 
 (function () {
-    var eventPrefix = BABYLON.Tools.GetPointerPrefix();
+    // Common
+    var buildCamera = function (that, name) {
+        that._leftCamera.isIntermediate = true;
 
-    BABYLON.AnaglyphCamera = function (name, alpha, beta, radius, target, eyeSpace, scene) {
+        that.subCameras.push(that._leftCamera);
+        that.subCameras.push(that._rightCamera);
+
+        that._leftTexture = new BABYLON.PassPostProcess(name + "_leftTexture", 1.0, that._leftCamera);
+        that._anaglyphPostProcess = new BABYLON.AnaglyphPostProcess(name + "_anaglyph", 1.0, that._rightCamera);
+
+        that._anaglyphPostProcess.onApply = function(effect) {
+            effect.setTextureFromPostProcess("leftSampler", that._leftTexture);
+        };
+
+        that._update();
+    };
+
+    // ArcRotate
+    BABYLON.AnaglyphArcRotateCamera = function (name, alpha, beta, radius, target, eyeSpace, scene) {
         BABYLON.ArcRotateCamera.call(this, name, alpha, beta, radius, target, scene);
 
         this._eyeSpace = BABYLON.Tools.ToRadians(eyeSpace);
 
         this._leftCamera = new BABYLON.ArcRotateCamera(name + "_left", alpha - this._eyeSpace, beta, radius, target, scene);
         this._rightCamera = new BABYLON.ArcRotateCamera(name + "_right", alpha + this._eyeSpace, beta, radius, target, scene);
-        
-        this._leftTexture = new BABYLON.PassPostProcess(name + "_leftTexture", 1.0, this._leftCamera);
-        this._rightTexture = new BABYLON.PassPostProcess(name + "_rightTexture", 1.0, this._rightCamera);
-
-        this._anaglyphPostProcess = new BABYLON.AnaglyphPostProcess(name + "_anaglyph", 1.0, this);
 
-        var that = this;
-        this._anaglyphPostProcess.onApply = function (effect) {
-            effect.setTextureFromPostProcess("leftSampler", that._leftTexture);
-            effect.setTextureFromPostProcess("rightSampler", that._rightTexture);
-        };
-
-        scene.activeCameras.push(this._leftCamera);
-        scene.activeCameras.push(this._rightCamera);
-        scene.activeCameras.push(this);
+        buildCamera(this, name);
     };
 
-    BABYLON.AnaglyphCamera.prototype = Object.create(BABYLON.ArcRotateCamera.prototype);
+    BABYLON.AnaglyphArcRotateCamera.prototype = Object.create(BABYLON.ArcRotateCamera.prototype);
 
-    BABYLON.AnaglyphCamera.prototype._update = function () {
+    BABYLON.AnaglyphArcRotateCamera.prototype._update = function () {
         this._updateCamera(this._leftCamera);
         this._updateCamera(this._rightCamera);
 
@@ -41,36 +44,58 @@ var BABYLON = BABYLON || {};
         BABYLON.ArcRotateCamera.prototype._update.call(this);
     };
 
-    BABYLON.AnaglyphCamera.prototype._updateCamera = function (camera) {
-        camera.inertialAlphaOffset = this.inertialAlphaOffset;
-        camera.inertialBetaOffset = this.inertialBetaOffset;
-        camera.inertialRadiusOffset = this.inertialRadiusOffset;
-        camera.lowerAlphaLimit = this.lowerAlphaLimit;
-        camera.upperAlphaLimit = this.upperAlphaLimit;
-        camera.lowerBetaLimit = this.lowerBetaLimit;
-        camera.upperBetaLimit = this.upperBetaLimit;
-        camera.lowerRadiusLimit = this.lowerRadiusLimit;
-        camera.upperRadiusLimit = this.upperRadiusLimit;
-        camera.angularSensibility = this.angularSensibility;
-        camera.wheelPrecision = this.wheelPrecision;
+    BABYLON.AnaglyphArcRotateCamera.prototype._updateCamera = function (camera) {
+        camera.beta = this.beta;
+        camera.radius = this.radius;
 
         camera.minZ = this.minZ;
         camera.maxZ = this.maxZ;
 
+        camera.fov = this.fov;
+
         camera.target = this.target;
     };
 
-    BABYLON.AnaglyphCamera.prototype.attachControl = function (canvas, noPreventDefault) {
-        BABYLON.ArcRotateCamera.prototype.attachControl.call(this, canvas);
+    // FreeCamera
+    BABYLON.AnaglyphFreeCamera = function (name, position, eyeSpace, scene) {
+        BABYLON.FreeCamera.call(this, name, position, scene);
+
+        this._eyeSpace = BABYLON.Tools.ToRadians(eyeSpace);
+        this._transformMatrix = new BABYLON.Matrix();
+
+        this._leftCamera = new BABYLON.FreeCamera(name + "_left", position, scene);
+        this._rightCamera = new BABYLON.FreeCamera(name + "_right", position, scene);
 
-        this._leftCamera.attachControl(canvas, noPreventDefault);
-        this._rightCamera.attachControl(canvas, noPreventDefault);
+        buildCamera(this, name, eyeSpace);
     };
 
-    BABYLON.AnaglyphCamera.prototype.detachControl = function (canvas) {
-        BABYLON.ArcRotateCamera.prototype.detachControl.call(this, canvas);
+    BABYLON.AnaglyphFreeCamera.prototype = Object.create(BABYLON.FreeCamera.prototype);
+
+    BABYLON.AnaglyphFreeCamera.prototype._getSubCameraPosition = function(eyeSpace, result) {
+        var target = this.getTarget();
+        BABYLON.Matrix.Translation(-target.x, -target.y, -target.z).multiplyToRef(BABYLON.Matrix.RotationY(eyeSpace), this._transformMatrix);
+
+        this._transformMatrix = this._transformMatrix.multiply(BABYLON.Matrix.Translation(target.x, target.y, target.z));
+
+        BABYLON.Vector3.TransformCoordinatesToRef(this.position, this._transformMatrix, result);
+    };
+
+    BABYLON.AnaglyphFreeCamera.prototype._update = function () {
+        this._getSubCameraPosition(-this._eyeSpace, this._leftCamera.position);
+        this._getSubCameraPosition(this._eyeSpace, this._rightCamera.position);
+
+        this._updateCamera(this._leftCamera);
+        this._updateCamera(this._rightCamera);
+
+        BABYLON.FreeCamera.prototype._update.call(this);
+    };
+
+    BABYLON.AnaglyphFreeCamera.prototype._updateCamera = function (camera) {
+        camera.minZ = this.minZ;
+        camera.maxZ = this.maxZ;
+
+        camera.fov = this.fov;
 
-        this._leftCamera.detachControl(canvas);
-        this._rightCamera.detachControl(canvas);
+        camera.setTarget(this.getTarget());
     };
 })();

+ 5 - 1
Babylon/Cameras/babylon.camera.js

@@ -8,7 +8,7 @@ var BABYLON = BABYLON || {};
         
         this.name = name;
         this.id = name;
-        this.position = position;
+        this.position = position.clone();
         this.upVector = BABYLON.Vector3.Up();
 
         scene.cameras.push(this);
@@ -20,6 +20,9 @@ var BABYLON = BABYLON || {};
         this._computedViewMatrix = BABYLON.Matrix.Identity();
         this._projectionMatrix = new BABYLON.Matrix();
 
+        // Sub-cameras
+        this.subCameras = [];
+
         // Animations
         this.animations = [];
 
@@ -50,6 +53,7 @@ var BABYLON = BABYLON || {};
     BABYLON.Camera.prototype.maxZ = 1000.0;
     BABYLON.Camera.prototype.inertia = 0.9;
     BABYLON.Camera.prototype.mode = BABYLON.Camera.PERSPECTIVE_CAMERA;
+    BABYLON.Camera.prototype.isIntermediate = false;
 
     // Properties
     BABYLON.Camera.prototype.getScene = function () {

+ 4 - 0
Babylon/Cameras/babylon.freeCamera.js

@@ -132,6 +132,10 @@ var BABYLON = BABYLON || {};
         }
     };
 
+    BABYLON.FreeCamera.prototype.getTarget = function () {
+        return this._currentTarget;
+    };
+
     // Controls
     BABYLON.FreeCamera.prototype.attachControl = function (canvas, noPreventDefault) {
         var previousPosition;

+ 1 - 1
Babylon/PostProcess/babylon.anaglyphPostProcess.js

@@ -4,7 +4,7 @@ var BABYLON = BABYLON || {};
 
 (function () {
     BABYLON.AnaglyphPostProcess = function (name, ratio, camera, samplingMode, engine, reusable) {
-        BABYLON.PostProcess.call(this, name, "anaglyph", null, ["leftSampler", "rightSampler"], ratio, camera, samplingMode, engine, reusable);
+        BABYLON.PostProcess.call(this, name, "anaglyph", null, ["leftSampler"], ratio, camera, samplingMode, engine, reusable);
     };
 
     BABYLON.AnaglyphPostProcess.prototype = Object.create(BABYLON.PostProcess.prototype);

+ 5 - 1
Babylon/PostProcess/babylon.postProcessManager.js

@@ -41,7 +41,7 @@ var BABYLON = BABYLON || {};
         postProcesses[this._scene.activeCamera._postProcessesTakenIndices[0]].activate(this._scene.activeCamera);
     };
     
-    BABYLON.PostProcessManager.prototype._finalizeFrame = function () {
+    BABYLON.PostProcessManager.prototype._finalizeFrame = function (doNotPresent) {
         var postProcesses = this._scene.activeCamera._postProcesses;
         var postProcessesTakenIndices = this._scene.activeCamera._postProcessesTakenIndices;
         if (postProcessesTakenIndices.length === 0 || !this._scene.postProcessesEnabled) {
@@ -57,6 +57,10 @@ var BABYLON = BABYLON || {};
                 engine.restoreDefaultFramebuffer();
             }
 
+            if (doNotPresent) {
+                break;
+            }
+
             var effect = postProcesses[postProcessesTakenIndices[index]].apply();
 
             if (effect) {

+ 1 - 2
Babylon/Shaders/anaglyph.fragment.fx

@@ -5,7 +5,6 @@ precision mediump float;
 // Samplers
 varying vec2 vUV;
 uniform sampler2D textureSampler;
-uniform sampler2D rightSampler;
 uniform sampler2D leftSampler;
 
 void main(void)
@@ -13,7 +12,7 @@ void main(void)
     vec4 leftFrag = texture2D(leftSampler, vUV);
     leftFrag = vec4(1.0, leftFrag.g, leftFrag.b, 1.0);
 
-    vec4 rightFrag = texture2D(rightSampler, vUV);
+	vec4 rightFrag = texture2D(textureSampler, vUV);
     rightFrag = vec4(rightFrag.r, 1.0, 1.0, 1.0);
 
     gl_FragColor = vec4(rightFrag.rgb * leftFrag.rgb, 1.0);

+ 22 - 9
Babylon/babylon.scene.js

@@ -608,7 +608,7 @@ var BABYLON = BABYLON || {};
         this._particlesDuration += new Date() - beforeParticlesDate;
     };
 
-    BABYLON.Scene.prototype._renderForCamera = function (camera, mustClearDepth) {
+    BABYLON.Scene.prototype._renderForCamera = function (camera) {
         var engine = this._engine;
 
         this.activeCamera = camera;
@@ -619,11 +619,6 @@ var BABYLON = BABYLON || {};
         // Viewport
         engine.setViewport(this.activeCamera.viewport);
 
-        // Clear
-        if (mustClearDepth) {
-            this._engine.clear(this.clearColor, false, true);
-        }
-
         // Camera
         this._renderId++;
         this.setTransformMatrix(this.activeCamera.getViewMatrix(), this.activeCamera.getProjectionMatrix());
@@ -701,7 +696,7 @@ var BABYLON = BABYLON || {};
         this._renderDuration += new Date() - beforeRenderDate;
 
         // Finalize frame
-        this.postProcessManager._finalizeFrame();
+        this.postProcessManager._finalizeFrame(camera.isIntermediate);
 
         // Update camera
         this.activeCamera._updateFromScene();
@@ -710,6 +705,24 @@ var BABYLON = BABYLON || {};
         this._renderTargets.reset();
     };
 
+    BABYLON.Scene.prototype._processSubCameras = function (camera) {
+        if (camera.subCameras.length == 0) {
+            this._renderForCamera(camera);
+            return;
+        }
+
+        // Sub-cameras
+        for (var index = 0; index < camera.subCameras.length; index++) {
+            this._renderForCamera(camera.subCameras[index]);
+        }
+
+        this.activeCamera = camera;
+        this.setTransformMatrix(this.activeCamera.getViewMatrix(), this.activeCamera.getProjectionMatrix());
+
+        // Update camera
+        this.activeCamera._updateFromScene();
+    };
+
     BABYLON.Scene.prototype.render = function () {
         var startDate = new Date();
         this._particlesDuration = 0;
@@ -757,10 +770,10 @@ var BABYLON = BABYLON || {};
             var currentRenderId = this._renderId;
             for (var cameraIndex = 0; cameraIndex < this.activeCameras.length; cameraIndex++) {
                 this._renderId = currentRenderId;
-                this._renderForCamera(this.activeCameras[cameraIndex], cameraIndex != 0);
+                this._processSubCameras(this.activeCameras[cameraIndex]);
             }
         } else {
-            this._renderForCamera(this.activeCamera);
+            this._processSubCameras(this.activeCamera);
         }
 
         // After render