babylon.anaglyphCamera.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. module BABYLON {
  2. var buildCamera = (that, name) => {
  3. that._leftCamera.isIntermediate = true;
  4. that.subCameras.push(that._leftCamera);
  5. that.subCameras.push(that._rightCamera);
  6. that._leftTexture = new BABYLON.PassPostProcess(name + "_leftTexture", 1.0, that._leftCamera);
  7. that._anaglyphPostProcess = new BABYLON.AnaglyphPostProcess(name + "_anaglyph", 1.0, that._rightCamera);
  8. that._anaglyphPostProcess.onApply = effect => {
  9. effect.setTextureFromPostProcess("leftSampler", that._leftTexture);
  10. };
  11. that._update();
  12. };
  13. export class AnaglyphArcRotateCamera extends ArcRotateCamera {
  14. private _eyeSpace: number;
  15. private _leftCamera: ArcRotateCamera;
  16. private _rightCamera: ArcRotateCamera;
  17. // ANY
  18. constructor(name: string, alpha: number, beta: number, radius: number, target, eyeSpace: number, scene) {
  19. super(name, alpha, beta, radius, target, scene);
  20. this._eyeSpace = BABYLON.Tools.ToRadians(eyeSpace);
  21. this._leftCamera = new BABYLON.ArcRotateCamera(name + "_left", alpha - this._eyeSpace, beta, radius, target, scene);
  22. this._rightCamera = new BABYLON.ArcRotateCamera(name + "_right", alpha + this._eyeSpace, beta, radius, target, scene);
  23. buildCamera(this, name);
  24. }
  25. public _update(): void {
  26. this._updateCamera(this._leftCamera);
  27. this._updateCamera(this._rightCamera);
  28. this._leftCamera.alpha = this.alpha - this._eyeSpace;
  29. this._rightCamera.alpha = this.alpha + this._eyeSpace;
  30. super._update();
  31. }
  32. public _updateCamera(camera: ArcRotateCamera) {
  33. camera.beta = this.beta;
  34. camera.radius = this.radius;
  35. camera.minZ = this.minZ;
  36. camera.maxZ = this.maxZ;
  37. camera.fov = this.fov;
  38. camera.target = this.target;
  39. }
  40. }
  41. export class AnaglyphFreeCamera extends FreeCamera {
  42. private _eyeSpace: number;
  43. private _leftCamera: FreeCamera;
  44. private _rightCamera: FreeCamera;
  45. private _transformMatrix: Matrix;
  46. constructor(name: string, position: Vector3, eyeSpace: number, scene: Scene) {
  47. super(name, position, scene);
  48. this._eyeSpace = BABYLON.Tools.ToRadians(eyeSpace);
  49. this._transformMatrix = new BABYLON.Matrix();
  50. this._leftCamera = new BABYLON.FreeCamera(name + "_left", position.clone(), scene);
  51. this._rightCamera = new BABYLON.FreeCamera(name + "_right", position.clone(), scene);
  52. buildCamera(this, name);
  53. }
  54. public _getSubCameraPosition(eyeSpace, result) {
  55. var target = this.getTarget();
  56. BABYLON.Matrix.Translation(-target.x, -target.y, -target.z).multiplyToRef(BABYLON.Matrix.RotationY(eyeSpace), this._transformMatrix);
  57. this._transformMatrix = this._transformMatrix.multiply(BABYLON.Matrix.Translation(target.x, target.y, target.z));
  58. BABYLON.Vector3.TransformCoordinatesToRef(this.position, this._transformMatrix, result);
  59. }
  60. public _update(): void {
  61. this._getSubCameraPosition(-this._eyeSpace, this._leftCamera.position);
  62. this._getSubCameraPosition(this._eyeSpace, this._rightCamera.position);
  63. this._updateCamera(this._leftCamera);
  64. this._updateCamera(this._rightCamera);
  65. super._update();
  66. }
  67. public _updateCamera(camera: FreeCamera): void {
  68. camera.minZ = this.minZ;
  69. camera.maxZ = this.maxZ;
  70. camera.fov = this.fov;
  71. camera.setTarget(this.getTarget());
  72. }
  73. }
  74. }