Browse Source

Add Holographic Camera

sevan 8 năm trước cách đây
mục cha
commit
6d4a3a9ee2

+ 1 - 0
Tools/Gulp/config.json

@@ -206,6 +206,7 @@
       "../../src/PostProcess/babylon.lensRenderingPipeline.js",
       "../../src/PostProcess/babylon.colorCorrectionPostProcess.js",
       "../../src/Cameras/babylon.stereoscopicCameras.js",
+      "../../src/Cameras/Holographic/babylon.holographicCamera.js",
       "../../src/PostProcess/babylon.hdrRenderingPipeline.js",
       "../../src/Rendering/babylon.edgesRenderer.js",
       "../../src/PostProcess/babylon.tonemapPostProcess.js",

+ 110 - 0
src/Cameras/Holographic/babylon.holographicCamera.ts

@@ -0,0 +1,110 @@
+module BABYLON {
+    export class HolographicCamera extends Camera {
+        
+        private _identityProjection: Matrix;
+
+        private _scriptProjection: Matrix;
+        private _scriptViewProjection: Matrix;
+
+        private _onBeforeRenderObserver: Observer<Scene>;
+
+        private _holographicViewMatrix: Matrix;        
+        private _holographicViewMatrixChanged = false;
+
+        constructor(name: string, position: Vector3, scene: Scene) {            
+            super(name, position, scene);
+
+            this._holographicViewMatrix = new Matrix();
+
+            scene.clearColor = new BABYLON.Color4(0, 0, 0, 0);
+            
+            var self = this;
+            this._onBeforeRenderObserver = scene.onBeforeRenderObservable.add(function (scene) {
+                self._holographicViewMatrix.m = (<any>window).getViewMatrix();
+                self.setViewMatrix(self._holographicViewMatrix);
+            })
+
+            this._identityProjection = BABYLON.Matrix.Identity();
+
+            this._scriptProjection = BABYLON.Matrix.Transpose(BABYLON.Matrix.PerspectiveFovLH(30, window.innerWidth / window.innerHeight, 1, 20));
+            this._scriptViewProjection = BABYLON.Matrix.Identity();
+
+            this.fov = 30;
+            this.minZ = 1.0;
+            this.maxZ = 20;
+            this.mode = BABYLON.Camera.PERSPECTIVE_CAMERA;
+            this.isIntermediate = false;
+            this.viewport = new BABYLON.Viewport(0, 0, 1.0, 1.0);
+            this.layerMask = 0x0FFFFFFF;
+            this.fovMode = BABYLON.Camera.FOVMODE_VERTICAL_FIXED;
+            this.cameraRigMode = BABYLON.Camera.RIG_MODE_NONE;
+
+            scene.addCamera(this);
+            if (!scene.activeCamera) {
+                scene.activeCamera = this;
+            }
+        }
+
+        public getTypeName(): string {
+            return "HolographicCamera";
+        };
+        
+        public _initCache(): void {
+        };
+
+        public _updateCache(): void {
+            // Check why ???
+            this._holographicViewMatrixChanged = false;
+        };
+
+        public _updateFromScene(): void {
+        };
+
+        // Synchronized
+        public _isSynchronizedViewMatrix() : boolean {
+            return !this._holographicViewMatrixChanged;
+        };
+        public _isSynchronizedProjectionMatrix() : boolean {
+            return true;
+        };
+
+        public setViewMatrix(view: Matrix) : void {
+            this._holographicViewMatrix = view;
+            this.position.x = view.m[12];
+            this.position.y = view.m[13];
+            this.position.z = -view.m[14];
+            this._holographicViewMatrixChanged = true;
+        };
+
+        public getViewMatrix(): Matrix {
+            return this._holographicViewMatrix;
+        };
+
+        public getProjectionMatrix(): Matrix {
+            return this._identityProjection;
+        };
+
+        public computeFrustumPlanes(frustumPlanes: Plane[]) : void {
+            if (frustumPlanes) {
+                this.getFrustumPlanesToRef(frustumPlanes);
+            }
+        }
+        
+        private getFrustumPlanes(): Plane[] {
+            this._holographicViewMatrix.multiplyToRef(this._scriptProjection, this._scriptViewProjection);
+            return BABYLON.Frustum.GetPlanes(this._scriptViewProjection);
+        };
+
+        private getFrustumPlanesToRef(result: Plane[]): Plane[] {
+            this._holographicViewMatrix.multiplyToRef(this._scriptProjection, this._scriptViewProjection);
+            BABYLON.Frustum.GetPlanesToRef(this._scriptViewProjection, result);
+            return result;
+        };
+
+        public dispose(): void {
+            this.getScene().onBeforeRenderObservable.remove(this._onBeforeRenderObserver);
+
+            super.dispose();
+        }
+    }
+}

+ 4 - 0
src/Cameras/babylon.camera.ts

@@ -807,5 +807,9 @@
 
             return camera;
         }
+
+        public computeFrustumPlanes(frustumPlanes: Plane[]) { 
+            // NOP by default.
+        }
     }
 }

+ 13 - 3
src/babylon.engine.ts

@@ -3112,11 +3112,21 @@
         }
 
         private _canRenderToFloatTexture(): boolean {
-            return this._canRenderToTextureOfType(BABYLON.Engine.TEXTURETYPE_FLOAT, 'OES_texture_float');
+            try {
+                return this._canRenderToTextureOfType(BABYLON.Engine.TEXTURETYPE_FLOAT, 'OES_texture_float');
+            }
+            catch (e) {
+                return false;
+            }
         }
 
-        private _canRenderToHalfFloatTexture(): boolean {
-            return this._canRenderToTextureOfType(BABYLON.Engine.TEXTURETYPE_HALF_FLOAT, 'OES_texture_half_float');
+        private _canRenderToHalfFloatTexture(): boolean {            
+            try {
+                return this._canRenderToTextureOfType(BABYLON.Engine.TEXTURETYPE_HALF_FLOAT, 'OES_texture_half_float');
+            }
+            catch (e) {
+                return false;
+            }
         }
 
         // Thank you : http://stackoverflow.com/questions/28827511/webgl-ios-render-to-floating-point-texture

+ 2 - 0
src/babylon.scene.ts

@@ -1980,6 +1980,8 @@
             this._boundingBoxRenderer.reset();
             this._edgesRenderers.reset();
 
+            this.activeCamera.computeFrustumPlanes(this._frustumPlanes);
+
             // Meshes
             var meshes: AbstractMesh[];
             var len: number;