transition.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. */
  6. import { Camera } from '../camera';
  7. import { Quat, Vec3 } from '../../mol-math/linear-algebra';
  8. import { lerp } from '../../mol-math/interpolate';
  9. export { CameraTransitionManager };
  10. class CameraTransitionManager {
  11. private t = 0;
  12. private func: CameraTransitionManager.TransitionFunc = CameraTransitionManager.defaultTransition;
  13. private start = 0;
  14. inTransition = false;
  15. private durationMs = 0;
  16. private _source: Camera.Snapshot = Camera.createDefaultSnapshot();
  17. private _target: Camera.Snapshot = Camera.createDefaultSnapshot();
  18. private _current = Camera.createDefaultSnapshot();
  19. get source(): Readonly<Camera.Snapshot> { return this._source; }
  20. get target(): Readonly<Camera.Snapshot> { return this._target; }
  21. apply(to: Partial<Camera.Snapshot>, durationMs: number = 0, transition?: CameraTransitionManager.TransitionFunc) {
  22. if (!this.inTransition || durationMs > 0) {
  23. Camera.copySnapshot(this._source, this.camera.state);
  24. }
  25. if (!this.inTransition) {
  26. Camera.copySnapshot(this._target, this.camera.state);
  27. }
  28. Camera.copySnapshot(this._target, to);
  29. if (this._target.radius > this._target.radiusMax) {
  30. this._target.radius = this._target.radiusMax;
  31. }
  32. if (!this.inTransition && durationMs <= 0 || (typeof to.mode !== 'undefined' && to.mode !== this.camera.state.mode)) {
  33. this.finish(this._target);
  34. return;
  35. }
  36. this.inTransition = true;
  37. this.func = transition || CameraTransitionManager.defaultTransition;
  38. if (!this.inTransition || durationMs > 0) {
  39. this.start = this.t;
  40. this.durationMs = durationMs;
  41. }
  42. }
  43. tick(t: number) {
  44. this.t = t;
  45. this.update();
  46. }
  47. private finish(to: Partial<Camera.Snapshot>) {
  48. Camera.copySnapshot(this.camera.state, to);
  49. this.inTransition = false;
  50. }
  51. private update() {
  52. if (!this.inTransition) return;
  53. const normalized = Math.min((this.t - this.start) / this.durationMs, 1);
  54. if (normalized === 1) {
  55. this.finish(this._target!);
  56. return;
  57. }
  58. this.func(this._current, normalized, this._source, this._target);
  59. Camera.copySnapshot(this.camera.state, this._current);
  60. }
  61. constructor(private camera: Camera) {
  62. }
  63. }
  64. namespace CameraTransitionManager {
  65. export type TransitionFunc = (out: Camera.Snapshot, t: number, source: Camera.Snapshot, target: Camera.Snapshot) => void
  66. const _rot = Quat.identity();
  67. export function defaultTransition(out: Camera.Snapshot, t: number, source: Camera.Snapshot, target: Camera.Snapshot): void {
  68. Camera.copySnapshot(out, target);
  69. // Rotate up
  70. Quat.slerp(_rot, Quat.Identity, Quat.rotationTo(_rot, source.up, target.up), t);
  71. Vec3.transformQuat(out.up, source.up, _rot);
  72. // Lerp target, position & radius
  73. Vec3.lerp(out.target, source.target, target.target, t);
  74. Vec3.lerp(out.position, source.position, target.position, t);
  75. out.radius = lerp(source.radius, target.radius, t);
  76. // TODO take change of `clipFar` into account
  77. out.radiusMax = lerp(source.radiusMax, target.radiusMax, t);
  78. // Lerp fov & fog
  79. out.fov = lerp(source.fov, target.fov, t);
  80. out.fog = lerp(source.fog, target.fog, t);
  81. }
  82. }