trackball.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /**
  2. * Copyright (c) 2018-2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. * @author David Sehnal <david.sehnal@gmail.com>
  6. *
  7. * This code has been modified from https://github.com/mrdoob/three.js/,
  8. * copyright (c) 2010-2018 three.js authors. MIT License
  9. */
  10. import { Quat, Vec2, Vec3, EPSILON } from '../../mol-math/linear-algebra';
  11. import { Viewport } from '../camera/util';
  12. import { InputObserver, DragInput, WheelInput, PinchInput, ButtonsType, ModifiersKeys } from '../../mol-util/input/input-observer';
  13. import { ParamDefinition as PD } from '../../mol-util/param-definition';
  14. import { Camera } from '../camera';
  15. import { absMax } from '../../mol-math/misc';
  16. import { Binding } from '../../mol-util/binding';
  17. const B = ButtonsType;
  18. const M = ModifiersKeys;
  19. const Trigger = Binding.Trigger;
  20. export const DefaultTrackballBindings = {
  21. dragRotate: Binding([Trigger(B.Flag.Primary, M.create())], 'Rotate', 'Drag using ${triggers}'),
  22. dragRotateZ: Binding([Trigger(B.Flag.Primary, M.create({ shift: true }))], 'Rotate around z-axis', 'Drag using ${triggers}'),
  23. dragPan: Binding([Trigger(B.Flag.Secondary, M.create()), Trigger(B.Flag.Primary, M.create({ control: true }))], 'Pan', 'Drag using ${triggers}'),
  24. dragZoom: Binding.Empty,
  25. dragFocus: Binding([Trigger(B.Flag.Forth, M.create())], 'Focus', 'Drag using ${triggers}'),
  26. dragFocusZoom: Binding([Trigger(B.Flag.Auxilary, M.create())], 'Focus and zoom', 'Drag using ${triggers}'),
  27. scrollZoom: Binding([Trigger(B.Flag.Auxilary, M.create())], 'Zoom', 'Scroll using ${triggers}'),
  28. scrollFocus: Binding([Trigger(B.Flag.Auxilary, M.create({ shift: true }))], 'Clip', 'Scroll using ${triggers}'),
  29. scrollFocusZoom: Binding.Empty,
  30. };
  31. export const TrackballControlsParams = {
  32. noScroll: PD.Boolean(true, { isHidden: true }),
  33. rotateSpeed: PD.Numeric(5.0, { min: 1, max: 10, step: 1 }),
  34. zoomSpeed: PD.Numeric(7.0, { min: 1, max: 15, step: 1 }),
  35. panSpeed: PD.Numeric(1.0, { min: 0.1, max: 5, step: 0.1 }),
  36. spin: PD.Boolean(false, { description: 'Spin the 3D scene around the x-axis in view space' }),
  37. spinSpeed: PD.Numeric(1, { min: -20, max: 20, step: 1 }),
  38. staticMoving: PD.Boolean(true, { isHidden: true }),
  39. dynamicDampingFactor: PD.Numeric(0.2, {}, { isHidden: true }),
  40. minDistance: PD.Numeric(0.01, {}, { isHidden: true }),
  41. maxDistance: PD.Numeric(1e150, {}, { isHidden: true }),
  42. bindings: PD.Value(DefaultTrackballBindings, { isHidden: true }),
  43. /**
  44. * minDistance = minDistanceFactor * boundingSphere.radius + minDistancePadding
  45. * maxDistance = max(maxDistanceFactor * boundingSphere.radius, maxDistanceMin)
  46. */
  47. autoAdjustMinMaxDistance: PD.MappedStatic('on', {
  48. off: PD.EmptyGroup(),
  49. on: PD.Group({
  50. minDistanceFactor: PD.Numeric(0),
  51. minDistancePadding: PD.Numeric(5),
  52. maxDistanceFactor: PD.Numeric(10),
  53. maxDistanceMin: PD.Numeric(20)
  54. })
  55. }, { isHidden: true })
  56. };
  57. export type TrackballControlsProps = PD.Values<typeof TrackballControlsParams>
  58. export { TrackballControls };
  59. interface TrackballControls {
  60. viewport: Viewport
  61. readonly props: Readonly<TrackballControlsProps>
  62. setProps: (props: Partial<TrackballControlsProps>) => void
  63. start: (t: number) => void
  64. update: (t: number) => void
  65. reset: () => void
  66. dispose: () => void
  67. }
  68. namespace TrackballControls {
  69. export function create(input: InputObserver, camera: Camera, props: Partial<TrackballControlsProps> = {}): TrackballControls {
  70. const p = { ...PD.getDefaultValues(TrackballControlsParams), ...props };
  71. const viewport = Viewport.clone(camera.viewport);
  72. let disposed = false;
  73. const dragSub = input.drag.subscribe(onDrag);
  74. const interactionEndSub = input.interactionEnd.subscribe(onInteractionEnd);
  75. const wheelSub = input.wheel.subscribe(onWheel);
  76. const pinchSub = input.pinch.subscribe(onPinch);
  77. let _isInteracting = false;
  78. // For internal use
  79. const lastPosition = Vec3();
  80. const _eye = Vec3();
  81. const _rotPrev = Vec2();
  82. const _rotCurr = Vec2();
  83. const _rotLastAxis = Vec3();
  84. let _rotLastAngle = 0;
  85. const _zRotPrev = Vec2();
  86. const _zRotCurr = Vec2();
  87. let _zRotLastAngle = 0;
  88. const _zoomStart = Vec2();
  89. const _zoomEnd = Vec2();
  90. const _focusStart = Vec2();
  91. const _focusEnd = Vec2();
  92. const _panStart = Vec2();
  93. const _panEnd = Vec2();
  94. // Initial values for reseting
  95. const target0 = Vec3.clone(camera.target);
  96. const position0 = Vec3.clone(camera.position);
  97. const up0 = Vec3.clone(camera.up);
  98. const mouseOnScreenVec2 = Vec2();
  99. function getMouseOnScreen(pageX: number, pageY: number) {
  100. return Vec2.set(
  101. mouseOnScreenVec2,
  102. (pageX - viewport.x) / viewport.width,
  103. (pageY - viewport.y) / viewport.height
  104. );
  105. }
  106. const mouseOnCircleVec2 = Vec2();
  107. function getMouseOnCircle(pageX: number, pageY: number) {
  108. return Vec2.set(
  109. mouseOnCircleVec2,
  110. (pageX - viewport.width * 0.5 - viewport.x) / (viewport.width * 0.5),
  111. (viewport.height + 2 * (viewport.y - pageY)) / viewport.width // screen.width intentional
  112. );
  113. }
  114. const rotAxis = Vec3();
  115. const rotQuat = Quat();
  116. const rotEyeDir = Vec3();
  117. const rotObjUpDir = Vec3();
  118. const rotObjSideDir = Vec3();
  119. const rotMoveDir = Vec3();
  120. function rotateCamera() {
  121. const dx = _rotCurr[0] - _rotPrev[0];
  122. const dy = _rotCurr[1] - _rotPrev[1];
  123. Vec3.set(rotMoveDir, dx, dy, 0);
  124. const aspectRatio = input.width / input.height;
  125. const angle = Vec3.magnitude(rotMoveDir) * p.rotateSpeed * input.pixelRatio * aspectRatio;
  126. if (angle) {
  127. Vec3.sub(_eye, camera.position, camera.target);
  128. Vec3.normalize(rotEyeDir, _eye);
  129. Vec3.normalize(rotObjUpDir, camera.up);
  130. Vec3.normalize(rotObjSideDir, Vec3.cross(rotObjSideDir, rotObjUpDir, rotEyeDir));
  131. Vec3.setMagnitude(rotObjUpDir, rotObjUpDir, dy);
  132. Vec3.setMagnitude(rotObjSideDir, rotObjSideDir, dx);
  133. Vec3.add(rotMoveDir, rotObjUpDir, rotObjSideDir);
  134. Vec3.normalize(rotAxis, Vec3.cross(rotAxis, rotMoveDir, _eye));
  135. Quat.setAxisAngle(rotQuat, rotAxis, angle);
  136. Vec3.transformQuat(_eye, _eye, rotQuat);
  137. Vec3.transformQuat(camera.up, camera.up, rotQuat);
  138. Vec3.copy(_rotLastAxis, rotAxis);
  139. _rotLastAngle = angle;
  140. } else if (!p.staticMoving && _rotLastAngle) {
  141. _rotLastAngle *= Math.sqrt(1.0 - p.dynamicDampingFactor);
  142. Vec3.sub(_eye, camera.position, camera.target);
  143. Quat.setAxisAngle(rotQuat, _rotLastAxis, _rotLastAngle);
  144. Vec3.transformQuat(_eye, _eye, rotQuat);
  145. Vec3.transformQuat(camera.up, camera.up, rotQuat);
  146. }
  147. Vec2.copy(_rotPrev, _rotCurr);
  148. }
  149. const zRotQuat = Quat();
  150. function zRotateCamera() {
  151. const dx = _zRotCurr[0] - _zRotPrev[0];
  152. const dy = _zRotCurr[1] - _zRotPrev[1];
  153. const angle = p.rotateSpeed * (-dx + dy) * -0.05;
  154. if (angle) {
  155. Vec3.sub(_eye, camera.position, camera.target);
  156. Quat.setAxisAngle(zRotQuat, _eye, angle);
  157. Vec3.transformQuat(camera.up, camera.up, zRotQuat);
  158. _zRotLastAngle = angle;
  159. } else if (!p.staticMoving && _zRotLastAngle) {
  160. _zRotLastAngle *= Math.sqrt(1.0 - p.dynamicDampingFactor);
  161. Vec3.sub(_eye, camera.position, camera.target);
  162. Quat.setAxisAngle(zRotQuat, _eye, _zRotLastAngle);
  163. Vec3.transformQuat(camera.up, camera.up, zRotQuat);
  164. }
  165. Vec2.copy(_zRotPrev, _zRotCurr);
  166. }
  167. function zoomCamera() {
  168. const factor = 1.0 + (_zoomEnd[1] - _zoomStart[1]) * p.zoomSpeed;
  169. if (factor !== 1.0 && factor > 0.0) {
  170. Vec3.scale(_eye, _eye, factor);
  171. }
  172. if (p.staticMoving) {
  173. Vec2.copy(_zoomStart, _zoomEnd);
  174. } else {
  175. _zoomStart[1] += (_zoomEnd[1] - _zoomStart[1]) * p.dynamicDampingFactor;
  176. }
  177. }
  178. function focusCamera() {
  179. const factor = (_focusEnd[1] - _focusStart[1]) * p.zoomSpeed;
  180. if (factor !== 0.0) {
  181. const radius = Math.max(1, camera.state.radius + camera.state.radius * factor);
  182. camera.setState({ radius });
  183. }
  184. if (p.staticMoving) {
  185. Vec2.copy(_focusStart, _focusEnd);
  186. } else {
  187. _focusStart[1] += (_focusEnd[1] - _focusStart[1]) * p.dynamicDampingFactor;
  188. }
  189. }
  190. const panMouseChange = Vec2();
  191. const panObjUp = Vec3();
  192. const panOffset = Vec3();
  193. function panCamera() {
  194. Vec2.sub(panMouseChange, Vec2.copy(panMouseChange, _panEnd), _panStart);
  195. if (Vec2.squaredMagnitude(panMouseChange)) {
  196. const factor = input.pixelRatio * p.panSpeed;
  197. panMouseChange[0] *= (1 / camera.zoom) * camera.viewport.width * factor;
  198. panMouseChange[1] *= (1 / camera.zoom) * camera.viewport.height * factor;
  199. Vec3.cross(panOffset, Vec3.copy(panOffset, _eye), camera.up);
  200. Vec3.setMagnitude(panOffset, panOffset, panMouseChange[0]);
  201. Vec3.setMagnitude(panObjUp, camera.up, panMouseChange[1]);
  202. Vec3.add(panOffset, panOffset, panObjUp);
  203. Vec3.add(camera.position, camera.position, panOffset);
  204. Vec3.add(camera.target, camera.target, panOffset);
  205. if (p.staticMoving) {
  206. Vec2.copy(_panStart, _panEnd);
  207. } else {
  208. Vec2.sub(panMouseChange, _panEnd, _panStart);
  209. Vec2.scale(panMouseChange, panMouseChange, p.dynamicDampingFactor);
  210. Vec2.add(_panStart, _panStart, panMouseChange);
  211. }
  212. }
  213. }
  214. /**
  215. * Ensure the distance between object and target is within the min/max distance
  216. * and not too large compared to `camera.state.radiusMax`
  217. */
  218. function checkDistances() {
  219. const maxDistance = Math.min(Math.max(camera.state.radiusMax * 1000, 0.01), p.maxDistance);
  220. if (Vec3.squaredMagnitude(_eye) > maxDistance * maxDistance) {
  221. Vec3.setMagnitude(_eye, _eye, maxDistance);
  222. Vec3.add(camera.position, camera.target, _eye);
  223. Vec2.copy(_zoomStart, _zoomEnd);
  224. Vec2.copy(_focusStart, _focusEnd);
  225. }
  226. if (Vec3.squaredMagnitude(_eye) < p.minDistance * p.minDistance) {
  227. Vec3.setMagnitude(_eye, _eye, p.minDistance);
  228. Vec3.add(camera.position, camera.target, _eye);
  229. Vec2.copy(_zoomStart, _zoomEnd);
  230. Vec2.copy(_focusStart, _focusEnd);
  231. }
  232. }
  233. function outsideViewport(x: number, y: number) {
  234. x *= input.pixelRatio;
  235. y *= input.pixelRatio;
  236. return (
  237. x > viewport.x + viewport.width ||
  238. input.height - y > viewport.y + viewport.height ||
  239. x < viewport.x ||
  240. input.height - y < viewport.y
  241. );
  242. }
  243. let lastUpdated = -1;
  244. /** Update the object's position, direction and up vectors */
  245. function update(t: number) {
  246. if (lastUpdated === t) return;
  247. if (p.spin && lastUpdated > 0) spin(t - lastUpdated);
  248. Vec3.sub(_eye, camera.position, camera.target);
  249. rotateCamera();
  250. zRotateCamera();
  251. zoomCamera();
  252. focusCamera();
  253. panCamera();
  254. Vec3.add(camera.position, camera.target, _eye);
  255. checkDistances();
  256. if (Vec3.squaredDistance(lastPosition, camera.position) > EPSILON) {
  257. Vec3.copy(lastPosition, camera.position);
  258. }
  259. lastUpdated = t;
  260. }
  261. /** Reset object's vectors and the target vector to their initial values */
  262. function reset() {
  263. Vec3.copy(camera.target, target0);
  264. Vec3.copy(camera.position, position0);
  265. Vec3.copy(camera.up, up0);
  266. Vec3.sub(_eye, camera.position, camera.target);
  267. Vec3.copy(lastPosition, camera.position);
  268. }
  269. // listeners
  270. function onDrag({ x, y, pageX, pageY, buttons, modifiers, isStart }: DragInput) {
  271. const isOutside = outsideViewport(x, y);
  272. if (isStart && isOutside) return;
  273. if (!isStart && !_isInteracting) return;
  274. _isInteracting = true;
  275. const dragRotate = Binding.match(p.bindings.dragRotate, buttons, modifiers);
  276. const dragRotateZ = Binding.match(p.bindings.dragRotateZ, buttons, modifiers);
  277. const dragPan = Binding.match(p.bindings.dragPan, buttons, modifiers);
  278. const dragZoom = Binding.match(p.bindings.dragZoom, buttons, modifiers);
  279. const dragFocus = Binding.match(p.bindings.dragFocus, buttons, modifiers);
  280. const dragFocusZoom = Binding.match(p.bindings.dragFocusZoom, buttons, modifiers);
  281. getMouseOnCircle(pageX, pageY);
  282. getMouseOnScreen(pageX, pageY);
  283. if (isStart) {
  284. if (dragRotate) {
  285. Vec2.copy(_rotCurr, mouseOnCircleVec2);
  286. Vec2.copy(_rotPrev, _rotCurr);
  287. }
  288. if (dragRotateZ) {
  289. Vec2.copy(_zRotCurr, mouseOnCircleVec2);
  290. Vec2.copy(_zRotPrev, _zRotCurr);
  291. }
  292. if (dragZoom || dragFocusZoom) {
  293. Vec2.copy(_zoomStart, mouseOnScreenVec2);
  294. Vec2.copy(_zoomEnd, _zoomStart);
  295. }
  296. if (dragFocus) {
  297. Vec2.copy(_focusStart, mouseOnScreenVec2);
  298. Vec2.copy(_focusEnd, _focusStart);
  299. }
  300. if (dragPan) {
  301. Vec2.copy(_panStart, mouseOnScreenVec2);
  302. Vec2.copy(_panEnd, _panStart);
  303. }
  304. }
  305. if (dragRotate) Vec2.copy(_rotCurr, mouseOnCircleVec2);
  306. if (dragRotateZ) Vec2.copy(_zRotCurr, mouseOnCircleVec2);
  307. if (dragZoom || dragFocusZoom) Vec2.copy(_zoomEnd, mouseOnScreenVec2);
  308. if (dragFocus) Vec2.copy(_focusEnd, mouseOnScreenVec2);
  309. if (dragFocusZoom) {
  310. const dist = Vec3.distance(camera.state.position, camera.state.target);
  311. camera.setState({ radius: dist / 5 });
  312. }
  313. if (dragPan) Vec2.copy(_panEnd, mouseOnScreenVec2);
  314. }
  315. function onInteractionEnd() {
  316. _isInteracting = false;
  317. }
  318. function onWheel({ x, y, dx, dy, dz, buttons, modifiers }: WheelInput) {
  319. if (outsideViewport(x, y)) return;
  320. const delta = absMax(dx, dy, dz);
  321. if (Binding.match(p.bindings.scrollZoom, buttons, modifiers)) {
  322. _zoomEnd[1] += delta * 0.0001;
  323. }
  324. if (Binding.match(p.bindings.scrollFocus, buttons, modifiers)) {
  325. _focusEnd[1] += delta * 0.0001;
  326. }
  327. }
  328. function onPinch({ fraction, buttons, modifiers }: PinchInput) {
  329. if (Binding.match(p.bindings.scrollZoom, buttons, modifiers)) {
  330. _isInteracting = true;
  331. _zoomEnd[1] += (fraction - 1) * 0.1;
  332. }
  333. }
  334. function dispose() {
  335. if (disposed) return;
  336. disposed = true;
  337. dragSub.unsubscribe();
  338. wheelSub.unsubscribe();
  339. pinchSub.unsubscribe();
  340. interactionEndSub.unsubscribe();
  341. }
  342. const _spinSpeed = Vec2.create(0.005, 0);
  343. function spin(deltaT: number) {
  344. if (p.spinSpeed === 0) return;
  345. const frameSpeed = (p.spinSpeed || 0) / 1000;
  346. _spinSpeed[0] = 60 * Math.min(Math.abs(deltaT), 1000 / 8) / 1000 * frameSpeed;
  347. if (!_isInteracting) Vec2.add(_rotCurr, _rotPrev, _spinSpeed);
  348. }
  349. function start(t: number) {
  350. lastUpdated = -1;
  351. update(t);
  352. }
  353. return {
  354. viewport,
  355. get props() { return p as Readonly<TrackballControlsProps>; },
  356. setProps: (props: Partial<TrackballControlsProps>) => {
  357. Object.assign(p, props);
  358. },
  359. start,
  360. update,
  361. reset,
  362. dispose
  363. };
  364. }
  365. }