babylon.holographicCamera.ts 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. module BABYLON {
  2. export class HolographicCamera extends Camera {
  3. private _identityProjection: Matrix;
  4. private _scriptProjection: Matrix;
  5. private _scriptViewProjection: Matrix;
  6. private _holographicViewMatrix: Matrix;
  7. private _onBeforeRenderObserver: Observer<Scene>;
  8. private _onBeforeCameraRenderObserver: Observer<Camera>;
  9. constructor(name: string, position: Vector3, scene: Scene) {
  10. super(name, position, scene);
  11. scene.clearColor = new BABYLON.Color4(0, 0, 0, 0);
  12. this._holographicViewMatrix = new Matrix();
  13. this._identityProjection = BABYLON.Matrix.Identity();
  14. this._scriptProjection = BABYLON.Matrix.Transpose(BABYLON.Matrix.PerspectiveFovLH(30, window.innerWidth / window.innerHeight, 1, 20));
  15. this._scriptViewProjection = BABYLON.Matrix.Identity();
  16. this.fov = 30;
  17. this.minZ = 1.0;
  18. this.maxZ = 20;
  19. this.mode = BABYLON.Camera.PERSPECTIVE_CAMERA;
  20. this.isIntermediate = false;
  21. this.viewport = new BABYLON.Viewport(0, 0, 1.0, 1.0);
  22. this.layerMask = 0x0FFFFFFF;
  23. this.fovMode = BABYLON.Camera.FOVMODE_VERTICAL_FIXED;
  24. this.cameraRigMode = BABYLON.Camera.RIG_MODE_NONE;
  25. var self = this;
  26. this._onBeforeRenderObserver = scene.onBeforeRenderObservable.add(function (scene) {
  27. self._holographicViewMatrix.m = (<any>window).getViewMatrix();
  28. self.setViewMatrix(self._holographicViewMatrix);
  29. })
  30. this._onBeforeCameraRenderObserver = scene.onBeforeCameraRenderObservable.add(function() {
  31. if (scene.frustumPlanes) {
  32. self.getFrustumPlanesToRef(scene.frustumPlanes);
  33. }
  34. })
  35. scene.addCamera(this);
  36. if (!scene.activeCamera) {
  37. scene.activeCamera = this;
  38. }
  39. }
  40. public getTypeName(): string {
  41. return "HolographicCamera";
  42. };
  43. public getProjectionMatrix(): Matrix {
  44. return this._identityProjection;
  45. };
  46. public getViewMatrix(): Matrix {
  47. return this._holographicViewMatrix;
  48. };
  49. public setViewMatrix(view: Matrix) : void {
  50. this._holographicViewMatrix = view;
  51. this.position.x = view.m[12];
  52. this.position.y = view.m[13];
  53. this.position.z = -view.m[14];
  54. };
  55. public _initCache(): void { };
  56. public _updateCache(): void { };
  57. public _updateFromScene(): void { };
  58. // Synchronized
  59. public _isSynchronizedViewMatrix() : boolean {
  60. return true;
  61. };
  62. public _isSynchronizedProjectionMatrix() : boolean {
  63. return true;
  64. };
  65. private getFrustumPlanesToRef(result: Plane[]): Plane[] {
  66. this._holographicViewMatrix.multiplyToRef(this._scriptProjection, this._scriptViewProjection);
  67. BABYLON.Frustum.GetPlanesToRef(this._scriptViewProjection, result);
  68. return result;
  69. };
  70. public dispose(): void {
  71. this.getScene().onBeforeRenderObservable.remove(this._onBeforeRenderObserver);
  72. this.getScene().onBeforeCameraRenderObservable.remove(this._onBeforeCameraRenderObserver);
  73. super.dispose();
  74. }
  75. }
  76. }