camera.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /**
  2. * Copyright (C) 2022, Protein Bioinformatics Research Group, RCNS
  3. *
  4. * Licensed under CC BY-NC 4.0, see LICENSE file for more info.
  5. *
  6. * @author Gabor Tusnady <tusnady.gabor@ttk.hu>
  7. * @author Csongor Gerdan <gerdan.csongor@ttk.hu>
  8. */
  9. import { PluginUIContext } from 'molstar/lib/mol-plugin-ui/context';
  10. import { Quat, Vec3 } from 'molstar/lib/mol-math/linear-algebra';
  11. import { PluginCommands } from 'molstar/lib/mol-plugin/commands';
  12. import { Camera } from 'molstar/lib/mol-canvas3d/camera';
  13. import { DebugUtil } from './debug-utils';
  14. let initialSnapshot: Camera.Snapshot;
  15. export function storeCameraSnapshot(plugin: PluginUIContext): void {
  16. if (!initialSnapshot) {
  17. initialSnapshot = plugin.canvas3d!.camera.getSnapshot();
  18. DebugUtil.log('initialSnapshot stored:', initialSnapshot);
  19. }
  20. }
  21. export function loadInitialSnapshot(plugin: PluginUIContext): void {
  22. if (!initialSnapshot) {
  23. DebugUtil.log('initialSnapshot is undefined');
  24. } else {
  25. DebugUtil.log('Loading initial snapshot:', initialSnapshot);
  26. PluginCommands.Camera.Reset(plugin, { snapshot: initialSnapshot });
  27. }
  28. }
  29. export async function rotateCamera(plugin: PluginUIContext) {
  30. function rot90q(v: Vec3, axis: Vec3 = Vec3.create(1, 0, 0)): Vec3 {
  31. const q = Quat.setAxisAngle(Quat(), axis, -Math.PI/2);
  32. return Vec3.transformQuat(Vec3(), v, q);
  33. }
  34. function sub(v: Vec3, u: Vec3): Vec3 {
  35. return Vec3.sub(Vec3(), v, u);
  36. }
  37. function add(v: Vec3, u: Vec3): Vec3 {
  38. return Vec3.add(Vec3(), v, u);
  39. }
  40. if (!plugin.canvas3d) {
  41. return;
  42. }
  43. const cam = plugin.canvas3d!.camera;
  44. const snapshot = cam.getSnapshot();
  45. const newSnapshot = {
  46. ...snapshot,
  47. // target + rotateBy90(postition - target)
  48. position: add(snapshot.target, rot90q(sub(snapshot.position, snapshot.target))),
  49. target: snapshot.target,
  50. up: Vec3.negUnitZ
  51. };
  52. const duration = 100;
  53. PluginCommands.Camera.Reset(plugin, { snapshot: newSnapshot, durationMs: duration }).then(() => {
  54. setTimeout(()=> {
  55. requestAnimationFrame(() => plugin.canvas3d?.requestCameraReset());
  56. }, duration + 50); // The 2nd reset needs some delay
  57. });
  58. }