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

mol-canvas3d: fixed camera focus

parent 4635bdff
No related branches found
No related tags found
No related merge requests found
......@@ -90,30 +90,21 @@ class Camera implements Object3D {
return ret;
}
getFocus(target: Vec3, radius: number, dir?: Vec3): Partial<Camera.Snapshot> {
getFocus(target: Vec3, radius: number): Partial<Camera.Snapshot> {
const fov = this.state.fov
const { width, height } = this.viewport
const aspect = width / height
const aspectFactor = (height < width ? 1 : aspect)
const currentDistance = Vec3.distance(this.state.position, target)
const targetDistance = Math.abs((radius / aspectFactor) / Math.sin(fov / 2))
const deltaDistance = Math.abs(currentDistance - targetDistance)
Vec3.setMagnitude(this.deltaDirection, this.state.direction, targetDistance)
Vec3.sub(this.newPosition, target, this.deltaDirection)
if (dir) {
Vec3.setMagnitude(this.deltaDirection, dir, targetDistance)
Vec3.add(this.newPosition, target, this.deltaDirection)
} else {
Vec3.setMagnitude(this.deltaDirection, this.state.direction, deltaDistance)
if (currentDistance < targetDistance) Vec3.negate(this.deltaDirection, this.deltaDirection)
Vec3.add(this.newPosition, this.state.position, this.deltaDirection)
}
return { target, position: Vec3.clone(this.newPosition) };
return { target: Vec3.clone(target), position: Vec3.clone(this.newPosition) };
}
focus(target: Vec3, radius: number, dir?: Vec3) {
if (radius > 0) this.setState(this.getFocus(target, radius, dir));
focus(target: Vec3, radius: number) {
if (radius > 0) this.setState(this.getFocus(target, radius));
}
// lookAt(target: Vec3) {
......@@ -202,7 +193,7 @@ namespace Camera {
mode: Mode,
position: Vec3,
// Normalized camera direction
// Normalized camera direction, from Target to Position, for some reason?
direction: Vec3,
up: Vec3,
target: Vec3,
......
......@@ -140,7 +140,7 @@ namespace Canvas3D {
const multiSample = new MultiSamplePass(webgl, camera, drawPass, postprocessing, p.multiSample)
let drawPending = false
let cameraResetRequested: boolean | Vec3 = false
let cameraResetRequested = false
function getLoci(pickingId: PickingId) {
let loci: Loci = EmptyLoci
......@@ -271,8 +271,7 @@ namespace Canvas3D {
runTask(scene.commit()).then(() => {
if (cameraResetRequested && !scene.isCommiting) {
const dir = typeof cameraResetRequested === 'boolean' ? undefined : cameraResetRequested
camera.focus(scene.boundingSphere.center, scene.boundingSphere.radius, dir)
camera.focus(scene.boundingSphere.center, scene.boundingSphere.radius)
cameraResetRequested = false
}
if (debugHelper.isEnabled) debugHelper.update()
......@@ -345,11 +344,11 @@ namespace Canvas3D {
getLoci,
handleResize,
resetCamera: (dir?: Vec3) => {
resetCamera: (/*dir?: Vec3*/) => {
if (scene.isCommiting) {
cameraResetRequested = dir || true
cameraResetRequested = true
} else {
camera.focus(scene.boundingSphere.center, scene.boundingSphere.radius, dir)
camera.focus(scene.boundingSphere.center, scene.boundingSphere.radius)
requestDraw(true);
}
},
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment