transition.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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._target.radius < 0.01) this._target.radius = 0.01;
  33. if (this._target.radiusMax < 0.01) this._target.radiusMax = 0.01;
  34. if (!this.inTransition && durationMs <= 0 || (typeof to.mode !== 'undefined' && to.mode !== this.camera.state.mode)) {
  35. this.finish(this._target);
  36. return;
  37. }
  38. this.inTransition = true;
  39. this.func = transition || CameraTransitionManager.defaultTransition;
  40. if (!this.inTransition || durationMs > 0) {
  41. this.start = this.t;
  42. this.durationMs = durationMs;
  43. }
  44. }
  45. tick(t: number) {
  46. this.t = t;
  47. this.update();
  48. }
  49. private finish(to: Partial<Camera.Snapshot>) {
  50. Camera.copySnapshot(this.camera.state, to);
  51. this.inTransition = false;
  52. }
  53. private update() {
  54. if (!this.inTransition) return;
  55. const normalized = Math.min((this.t - this.start) / this.durationMs, 1);
  56. if (normalized === 1) {
  57. this.finish(this._target!);
  58. return;
  59. }
  60. this.func(this._current, normalized, this._source, this._target);
  61. Camera.copySnapshot(this.camera.state, this._current);
  62. }
  63. constructor(private camera: Camera) {
  64. }
  65. }
  66. namespace CameraTransitionManager {
  67. export type TransitionFunc = (out: Camera.Snapshot, t: number, source: Camera.Snapshot, target: Camera.Snapshot) => void
  68. const _rot = Quat.identity();
  69. export function defaultTransition(out: Camera.Snapshot, t: number, source: Camera.Snapshot, target: Camera.Snapshot): void {
  70. Camera.copySnapshot(out, target);
  71. // Rotate up
  72. Quat.slerp(_rot, Quat.Identity, Quat.rotationTo(_rot, source.up, target.up), t);
  73. Vec3.transformQuat(out.up, source.up, _rot);
  74. // Lerp target, position & radius
  75. Vec3.lerp(out.target, source.target, target.target, t);
  76. Vec3.lerp(out.position, source.position, target.position, t);
  77. out.radius = lerp(source.radius, target.radius, t);
  78. // TODO take change of `clipFar` into account
  79. out.radiusMax = lerp(source.radiusMax, target.radiusMax, t);
  80. // Lerp fov & fog
  81. out.fov = lerp(source.fov, target.fov, t);
  82. out.fog = lerp(source.fog, target.fog, t);
  83. }
  84. }