Skip to content
Snippets Groups Projects
Commit f2457228 authored by David Sehnal's avatar David Sehnal
Browse files

mol-canvas3d: camera spin fix

parent 0bb2fce0
No related branches found
No related tags found
No related merge requests found
...@@ -192,7 +192,7 @@ namespace Canvas3D { ...@@ -192,7 +192,7 @@ namespace Canvas3D {
if (isIdentifying || isUpdating) return false if (isIdentifying || isUpdating) return false
let didRender = false let didRender = false
controls.update() controls.update(currentTime);
// TODO: is this a good fix? Also, setClipping does not work if the user has manually set a clipping plane. // TODO: is this a good fix? Also, setClipping does not work if the user has manually set a clipping plane.
if (!camera.transition.inTransition) setClipping(); if (!camera.transition.inTransition) setClipping();
const cameraChanged = camera.updateMatrices(); const cameraChanged = camera.updateMatrices();
...@@ -230,6 +230,7 @@ namespace Canvas3D { ...@@ -230,6 +230,7 @@ namespace Canvas3D {
} }
let forceNextDraw = false; let forceNextDraw = false;
let currentTime = 0;
function draw(force?: boolean) { function draw(force?: boolean) {
if (render('draw', !!force || forceNextDraw)) { if (render('draw', !!force || forceNextDraw)) {
...@@ -246,8 +247,8 @@ namespace Canvas3D { ...@@ -246,8 +247,8 @@ namespace Canvas3D {
} }
function animate() { function animate() {
const t = now(); currentTime = now();
camera.transition.tick(t); camera.transition.tick(currentTime);
draw(false) draw(false)
window.requestAnimationFrame(animate) window.requestAnimationFrame(animate)
} }
......
...@@ -39,7 +39,7 @@ interface TrackballControls { ...@@ -39,7 +39,7 @@ interface TrackballControls {
readonly props: Readonly<TrackballControlsProps> readonly props: Readonly<TrackballControlsProps>
setProps: (props: Partial<TrackballControlsProps>) => void setProps: (props: Partial<TrackballControlsProps>) => void
update: () => void update: (t: number) => void
reset: () => void reset: () => void
dispose: () => void dispose: () => void
} }
...@@ -207,9 +207,11 @@ namespace TrackballControls { ...@@ -207,9 +207,11 @@ namespace TrackballControls {
} }
} }
let lastUpdated = -1;
/** Update the object's position, direction and up vectors */ /** Update the object's position, direction and up vectors */
function update() { function update(t: number) {
if (p.spin) spin(); if (lastUpdated === t) return;
if (p.spin) spin(t - lastUpdated);
Vec3.sub(_eye, object.position, target) Vec3.sub(_eye, object.position, target)
...@@ -226,6 +228,8 @@ namespace TrackballControls { ...@@ -226,6 +228,8 @@ namespace TrackballControls {
if (Vec3.squaredDistance(lastPosition, object.position) > EPSILON.Value) { if (Vec3.squaredDistance(lastPosition, object.position) > EPSILON.Value) {
Vec3.copy(lastPosition, object.position) Vec3.copy(lastPosition, object.position)
} }
lastUpdated = t;
} }
/** Reset object's vectors and the target vector to their initial values */ /** Reset object's vectors and the target vector to their initial values */
...@@ -299,15 +303,14 @@ namespace TrackballControls { ...@@ -299,15 +303,14 @@ namespace TrackballControls {
} }
const _spinSpeed = Vec2.create(0.005, 0); const _spinSpeed = Vec2.create(0.005, 0);
function spin() { function spin(deltaT: number) {
_spinSpeed[0] = (p.spinSpeed || 0) / 1000; const frameSpeed = (p.spinSpeed || 0) / 1000;
_spinSpeed[0] = 60 * Math.min(Math.abs(deltaT), 1000 / 8) / 1000 * frameSpeed;
if (!_isInteracting) Vec2.add(_moveCurr, _movePrev, _spinSpeed); if (!_isInteracting) Vec2.add(_moveCurr, _movePrev, _spinSpeed);
} }
// force an update at start // force an update at start
update(); update(0);
if (props.spin) { spin(); }
return { return {
viewport, viewport,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment