camera.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /**
  2. * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  6. */
  7. import { Mat4, Vec3, Vec4 } from 'mol-math/linear-algebra'
  8. import { Viewport, cameraLookAt, cameraProject, cameraUnproject } from './camera/util';
  9. import { Object3D } from 'mol-gl/object3d';
  10. export { Camera }
  11. class Camera implements Object3D {
  12. readonly view: Mat4 = Mat4.identity();
  13. readonly projection: Mat4 = Mat4.identity();
  14. readonly projectionView: Mat4 = Mat4.identity();
  15. readonly inverseProjectionView: Mat4 = Mat4.identity();
  16. readonly viewport: Viewport;
  17. readonly state: Readonly<Camera.Snapshot> = Camera.createDefaultSnapshot();
  18. get position() { return this.state.position; }
  19. set position(v: Vec3) { Vec3.copy(this.state.position, v); }
  20. get direction() { return this.state.direction; }
  21. set direction(v: Vec3) { Vec3.copy(this.state.direction, v); }
  22. get up() { return this.state.up; }
  23. set up(v: Vec3) { Vec3.copy(this.state.up, v); }
  24. get target() { return this.state.target; }
  25. set target(v: Vec3) { Vec3.copy(this.state.target, v); }
  26. updateMatrices() {
  27. const snapshot = this.state as Camera.Snapshot;
  28. const height = 2 * Math.tan(snapshot.fov / 2) * Vec3.distance(snapshot.position, snapshot.target);
  29. snapshot.zoom = this.viewport.height / height;
  30. switch (this.state.mode) {
  31. case 'orthographic': updateOrtho(this); break;
  32. case 'perspective': updatePers(this); break;
  33. default: throw new Error('unknown camera mode');
  34. }
  35. Mat4.mul(this.projectionView, this.projection, this.view)
  36. Mat4.invert(this.inverseProjectionView, this.projectionView)
  37. }
  38. setState(snapshot?: Partial<Camera.Snapshot>) {
  39. Camera.copySnapshot(this.state, snapshot);
  40. }
  41. getSnapshot() {
  42. const ret = Camera.createDefaultSnapshot();
  43. Camera.copySnapshot(ret, this.state);
  44. return ret;
  45. }
  46. lookAt(target: Vec3) {
  47. cameraLookAt(this.direction, this.up, this.position, target);
  48. }
  49. translate(v: Vec3) {
  50. Vec3.add(this.position, this.position, v)
  51. }
  52. project(out: Vec4, point: Vec3) {
  53. return cameraProject(out, point, this.viewport, this.projectionView)
  54. }
  55. unproject(out: Vec3, point: Vec3) {
  56. return cameraUnproject(out, point, this.viewport, this.inverseProjectionView)
  57. }
  58. constructor(state?: Partial<Camera.Snapshot>, viewport = Viewport.create(-1, -1, 1, 1)) {
  59. this.viewport = viewport;
  60. Camera.copySnapshot(this.state, state);
  61. }
  62. }
  63. namespace Camera {
  64. export type Mode = 'perspective' | 'orthographic'
  65. export function createDefaultSnapshot(): Snapshot {
  66. return {
  67. mode: 'perspective',
  68. position: Vec3.zero(),
  69. direction: Vec3.create(0, 0, -1),
  70. up: Vec3.create(0, 1, 0),
  71. target: Vec3.create(0, 0, 0),
  72. near: 0.1,
  73. far: 10000,
  74. fogNear: 0.1,
  75. fogFar: 10000,
  76. fov: Math.PI / 4,
  77. zoom: 1
  78. };
  79. }
  80. export interface Snapshot {
  81. mode: Mode,
  82. position: Vec3,
  83. direction: Vec3,
  84. up: Vec3,
  85. target: Vec3,
  86. near: number,
  87. far: number,
  88. fogNear: number,
  89. fogFar: number,
  90. fov: number,
  91. zoom: number
  92. }
  93. export function copySnapshot(out: Snapshot, source?: Partial<Snapshot>) {
  94. if (!source) return;
  95. if (typeof source.mode !== 'undefined') out.mode = source.mode;
  96. if (typeof source.position !== 'undefined') Vec3.copy(out.position, source.position);
  97. if (typeof source.direction !== 'undefined') Vec3.copy(out.direction, source.direction);
  98. if (typeof source.up !== 'undefined') Vec3.copy(out.up, source.up);
  99. if (typeof source.target !== 'undefined') Vec3.copy(out.target, source.target);
  100. if (typeof source.near !== 'undefined') out.near = source.near;
  101. if (typeof source.far !== 'undefined') out.far = source.far;
  102. if (typeof source.fogNear !== 'undefined') out.fogNear = source.fogNear;
  103. if (typeof source.fogFar !== 'undefined') out.fogFar = source.fogFar;
  104. if (typeof source.fov !== 'undefined') out.fov = source.fov;
  105. if (typeof source.zoom !== 'undefined') out.zoom = source.zoom;
  106. }
  107. }
  108. const _center = Vec3.zero();
  109. function updateOrtho(camera: Camera) {
  110. const { viewport, state: { zoom, near, far } } = camera;
  111. const fullLeft = (viewport.width - viewport.x) / -2
  112. const fullRight = (viewport.width - viewport.x) / 2
  113. const fullTop = (viewport.height - viewport.y) / 2
  114. const fullBottom = (viewport.height - viewport.y) / -2
  115. const dx = (fullRight - fullLeft) / (2 * zoom)
  116. const dy = (fullTop - fullBottom) / (2 * zoom)
  117. const cx = (fullRight + fullLeft) / 2
  118. const cy = (fullTop + fullBottom) / 2
  119. const left = cx - dx
  120. const right = cx + dx
  121. const top = cy + dy
  122. const bottom = cy - dy
  123. // build projection matrix
  124. Mat4.ortho(camera.projection, left, right, bottom, top, Math.abs(near), Math.abs(far))
  125. // build view matrix
  126. Vec3.add(_center, camera.position, camera.direction)
  127. Mat4.lookAt(camera.view, camera.position, _center, camera.up)
  128. }
  129. function updatePers(camera: Camera) {
  130. const aspect = camera.viewport.width / camera.viewport.height
  131. const { fov, near, far } = camera.state;
  132. // build projection matrix
  133. Mat4.perspective(camera.projection, fov, aspect, Math.abs(near), Math.abs(far))
  134. // build view matrix
  135. Vec3.add(_center, camera.position, camera.direction)
  136. Mat4.lookAt(camera.view, camera.position, _center, camera.up)
  137. }