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