camera.ts 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /**
  2. * Copyright (c) 2018-2019 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, EPSILON } from '../mol-math/linear-algebra'
  8. import { Viewport, cameraProject, cameraUnproject } from './camera/util';
  9. import { Object3D } from '../mol-gl/object3d';
  10. import { BehaviorSubject } from 'rxjs';
  11. import { CameraTransitionManager } from './camera/transition';
  12. export { Camera }
  13. // TODO: slab controls that modify near/far planes?
  14. class Camera implements Object3D {
  15. readonly updatedViewProjection = new BehaviorSubject<Camera>(this);
  16. readonly view: Mat4 = Mat4.identity();
  17. readonly projection: Mat4 = Mat4.identity();
  18. readonly projectionView: Mat4 = Mat4.identity();
  19. readonly inverseProjectionView: Mat4 = Mat4.identity();
  20. readonly viewport: Viewport;
  21. readonly state: Readonly<Camera.Snapshot> = Camera.createDefaultSnapshot();
  22. readonly viewOffset: Camera.ViewOffset = {
  23. enabled: false,
  24. fullWidth: 1, fullHeight: 1,
  25. offsetX: 0, offsetY: 0,
  26. width: 1, height: 1
  27. }
  28. readonly transition: CameraTransitionManager = new CameraTransitionManager(this);
  29. get position() { return this.state.position; }
  30. set position(v: Vec3) { Vec3.copy(this.state.position, v); }
  31. get direction() { return this.state.direction; }
  32. set direction(v: Vec3) { Vec3.copy(this.state.direction, v); }
  33. get up() { return this.state.up; }
  34. set up(v: Vec3) { Vec3.copy(this.state.up, v); }
  35. get target() { return this.state.target; }
  36. set target(v: Vec3) { Vec3.copy(this.state.target, v); }
  37. private prevProjection = Mat4.identity();
  38. private prevView = Mat4.identity();
  39. private deltaDirection = Vec3.zero();
  40. private newPosition = Vec3.zero();
  41. updateMatrices() {
  42. const snapshot = this.state as Camera.Snapshot;
  43. const height = 2 * Math.tan(snapshot.fov / 2) * Vec3.distance(snapshot.position, snapshot.target);
  44. snapshot.zoom = this.viewport.height / height;
  45. switch (this.state.mode) {
  46. case 'orthographic': updateOrtho(this); break;
  47. case 'perspective': updatePers(this); break;
  48. default: throw new Error('unknown camera mode');
  49. }
  50. const changed = !Mat4.areEqual(this.projection, this.prevProjection, EPSILON.Value) || !Mat4.areEqual(this.view, this.prevView, EPSILON.Value);
  51. Mat4.mul(this.projectionView, this.projection, this.view)
  52. Mat4.invert(this.inverseProjectionView, this.projectionView)
  53. if (changed) {
  54. Mat4.mul(this.projectionView, this.projection, this.view)
  55. Mat4.invert(this.inverseProjectionView, this.projectionView)
  56. Mat4.copy(this.prevView, this.view);
  57. Mat4.copy(this.prevProjection, this.projection);
  58. this.updatedViewProjection.next(this);
  59. }
  60. return changed;
  61. }
  62. setState(snapshot: Partial<Camera.Snapshot>) {
  63. this.transition.apply(snapshot);
  64. }
  65. getSnapshot() {
  66. const ret = Camera.createDefaultSnapshot();
  67. Camera.copySnapshot(ret, this.state);
  68. return ret;
  69. }
  70. getFocus(target: Vec3, radius: number): Partial<Camera.Snapshot> {
  71. const fov = this.state.fov
  72. const { width, height } = this.viewport
  73. const aspect = width / height
  74. const aspectFactor = (height < width ? 1 : aspect)
  75. const currentDistance = Vec3.distance(this.state.position, target)
  76. const targetDistance = Math.abs((radius / aspectFactor) / Math.sin(fov / 2))
  77. const deltaDistance = Math.abs(currentDistance - targetDistance)
  78. Vec3.sub(this.deltaDirection, this.state.position, target)
  79. Vec3.setMagnitude(this.deltaDirection, this.state.direction, deltaDistance)
  80. if (currentDistance < targetDistance) Vec3.negate(this.deltaDirection, this.deltaDirection)
  81. Vec3.add(this.newPosition, this.state.position, this.deltaDirection)
  82. return { target, position: Vec3.clone(this.newPosition) };
  83. }
  84. focus(target: Vec3, radius: number) {
  85. if (radius > 0) this.setState(this.getFocus(target, radius));
  86. }
  87. // lookAt(target: Vec3) {
  88. // cameraLookAt(this.position, this.up, this.direction, target);
  89. // }
  90. // translate(v: Vec3) {
  91. // Vec3.add(this.position, this.position, v);
  92. // cameraLookAt(this.position, this.up, this.direction, this.target);
  93. // }
  94. project(out: Vec4, point: Vec3) {
  95. return cameraProject(out, point, this.viewport, this.projectionView)
  96. }
  97. unproject(out: Vec3, point: Vec3) {
  98. return cameraUnproject(out, point, this.viewport, this.inverseProjectionView)
  99. }
  100. dispose() {
  101. this.updatedViewProjection.complete();
  102. }
  103. constructor(state?: Partial<Camera.Snapshot>, viewport = Viewport.create(-1, -1, 1, 1)) {
  104. this.viewport = viewport;
  105. Camera.copySnapshot(this.state, state);
  106. }
  107. }
  108. namespace Camera {
  109. export type Mode = 'perspective' | 'orthographic'
  110. export interface ClippingInfo {
  111. near: number,
  112. far: number,
  113. fogNear: number,
  114. fogFar: number
  115. }
  116. /**
  117. * Sets an offseted view in a larger frustum. This is useful for
  118. * - multi-window or multi-monitor/multi-machine setups
  119. * - jittering the camera position for
  120. */
  121. export interface ViewOffset {
  122. enabled: boolean,
  123. fullWidth: number,
  124. fullHeight: number,
  125. offsetX: number,
  126. offsetY: number,
  127. width: number,
  128. height: number
  129. }
  130. export function setViewOffset(out: ViewOffset, fullWidth: number, fullHeight: number, offsetX: number, offsetY: number, width: number, height: number) {
  131. out.fullWidth = fullWidth
  132. out.fullHeight = fullHeight
  133. out.offsetX = offsetX
  134. out.offsetY = offsetY
  135. out.width = width
  136. out.height = height
  137. }
  138. export function createDefaultSnapshot(): Snapshot {
  139. return {
  140. mode: 'perspective',
  141. position: Vec3.create(0, 0, 100),
  142. direction: Vec3.create(0, 0, 1),
  143. up: Vec3.create(0, 1, 0),
  144. target: Vec3.create(0, 0, 0),
  145. near: 1,
  146. far: 10000,
  147. fogNear: 1,
  148. fogFar: 10000,
  149. fov: Math.PI / 4,
  150. zoom: 1,
  151. };
  152. }
  153. export interface Snapshot {
  154. mode: Mode,
  155. position: Vec3,
  156. // Normalized camera direction
  157. direction: Vec3,
  158. up: Vec3,
  159. target: Vec3,
  160. near: number,
  161. far: number,
  162. fogNear: number,
  163. fogFar: number,
  164. fov: number,
  165. zoom: number,
  166. }
  167. export function copySnapshot(out: Snapshot, source?: Partial<Snapshot>) {
  168. if (!source) return;
  169. if (typeof source.mode !== 'undefined') out.mode = source.mode;
  170. if (typeof source.position !== 'undefined') Vec3.copy(out.position, source.position);
  171. if (typeof source.direction !== 'undefined') Vec3.copy(out.direction, source.direction);
  172. if (typeof source.up !== 'undefined') Vec3.copy(out.up, source.up);
  173. if (typeof source.target !== 'undefined') Vec3.copy(out.target, source.target);
  174. if (typeof source.near !== 'undefined') out.near = source.near;
  175. if (typeof source.far !== 'undefined') out.far = source.far;
  176. if (typeof source.fogNear !== 'undefined') out.fogNear = source.fogNear;
  177. if (typeof source.fogFar !== 'undefined') out.fogFar = source.fogFar;
  178. if (typeof source.fov !== 'undefined') out.fov = source.fov;
  179. if (typeof source.zoom !== 'undefined') out.zoom = source.zoom;
  180. }
  181. }
  182. const _center = Vec3.zero();
  183. function updateOrtho(camera: Camera) {
  184. const { viewport, state: { zoom, near, far }, viewOffset } = camera
  185. const fullLeft = -(viewport.width - viewport.x) / 2
  186. const fullRight = (viewport.width - viewport.x) / 2
  187. const fullTop = (viewport.height - viewport.y) / 2
  188. const fullBottom = -(viewport.height - viewport.y) / 2
  189. const dx = (fullRight - fullLeft) / (2 * zoom)
  190. const dy = (fullTop - fullBottom) / (2 * zoom)
  191. const cx = (fullRight + fullLeft) / 2
  192. const cy = (fullTop + fullBottom) / 2
  193. let left = cx - dx
  194. let right = cx + dx
  195. let top = cy + dy
  196. let bottom = cy - dy
  197. if (viewOffset.enabled) {
  198. const zoomW = zoom / (viewOffset.width / viewOffset.fullWidth)
  199. const zoomH = zoom / (viewOffset.height / viewOffset.fullHeight)
  200. const scaleW = (fullRight - fullLeft) / viewOffset.width
  201. const scaleH = (fullTop - fullBottom) / viewOffset.height
  202. left += scaleW * (viewOffset.offsetX / zoomW)
  203. right = left + scaleW * (viewOffset.width / zoomW)
  204. top -= scaleH * (viewOffset.offsetY / zoomH)
  205. bottom = top - scaleH * (viewOffset.height / zoomH)
  206. }
  207. // build projection matrix
  208. Mat4.ortho(camera.projection, left, right, top, bottom, near, far)
  209. // build view matrix
  210. Vec3.add(_center, camera.position, camera.direction)
  211. Mat4.lookAt(camera.view, camera.position, _center, camera.up)
  212. }
  213. function updatePers(camera: Camera) {
  214. const aspect = camera.viewport.width / camera.viewport.height
  215. const { state: { fov, near, far }, viewOffset } = camera
  216. let top = near * Math.tan(0.5 * fov)
  217. let height = 2 * top
  218. let width = aspect * height
  219. let left = -0.5 * width
  220. if (viewOffset.enabled) {
  221. left += viewOffset.offsetX * width / viewOffset.fullWidth
  222. top -= viewOffset.offsetY * height / viewOffset.fullHeight
  223. width *= viewOffset.width / viewOffset.fullWidth
  224. height *= viewOffset.height / viewOffset.fullHeight
  225. }
  226. // build projection matrix
  227. Mat4.perspective(camera.projection, left, left + width, top, top - height, near, far)
  228. // build view matrix
  229. Vec3.add(_center, camera.position, camera.direction)
  230. Mat4.lookAt(camera.view, camera.position, _center, camera.up)
  231. }