Skip to content

Commit

Permalink
fix(viewer-lib): Updates to WASD and orthographic projection:
Browse files Browse the repository at this point in the history
- Pressing WASDQE while in orthographic mode will no longer work. WASD-ing is disabled while in ortho
- Fixed an issue with toggling to ortho after dropping out of WASD caused by the origin beingvery close to the camera, making the orthographic size to be huge. Now when this happens, we get the minimum distance from the camera to the geometry and use that distance to compute the orthographic size. If no intersection is found, we just take the halfwy between min and max radius
  • Loading branch information
AlexandruPopovici committed Feb 3, 2025
1 parent 4ee2783 commit 75fcfa3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { PerspectiveCamera } from 'three'
import { IViewer } from '../../IViewer.js'
import { CameraController } from './CameraController.js'
type MoveType = 'forward' | 'back' | 'left' | 'right' | 'up' | 'down'
Expand Down Expand Up @@ -66,6 +67,7 @@ export class HybridCameraController extends CameraController {
}
if (
!this._flyControls.enabled &&
this._renderingCamera instanceof PerspectiveCamera &&
Object.values(this.keyMap).some((v) => v === true)
)
this.toggleControls()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ import {
Quaternion,
Euler,
Mesh,
SphereGeometry
SphereGeometry,
Ray
} from 'three'

import { Damper, SETTLING_TIME } from '../../utils/Damper.js'
Expand Down Expand Up @@ -257,6 +258,39 @@ export class SmoothOrbitControls extends SpeckleControls {
}

set targetCamera(value: PerspectiveCamera | OrthographicCamera) {
/** When you drop out of WASD, the radius is very close to the camera
* And if you switch to orthogrtaphic you will get a vey large orthographic size
* This is a workaround where we try to get a more reasonable radius, by getting
* the closest intersection distance to the scene geometry from the camera
*/
if (value instanceof OrthographicCamera) {
if (this.goalSpherical.radius < this._minDist) {
/** Fallback radius, in the middle of the allowed radius range */
let recomputedRadius =
this.options.minimumRadius +
(this._options.maximumRadius - this.options.minimumRadius) * 0.5
const ray = new Ray(
this._targetCamera.position,
this._targetCamera.getWorldDirection(new Vector3())
)
const res = this.renderer.intersections.intersectRay(
this.renderer.scene,
this._targetCamera as PerspectiveCamera,
ray,
ObjectLayers.STREAM_CONTENT_MESH,
false,
this.renderer.clippingVolume,
false,
false
)
if (res && res.length) {
recomputedRadius = res[0].distance
}
this.spherical.radius = recomputedRadius
this.goalSpherical.radius = recomputedRadius
}
}

this._targetCamera = value
this.usePivotal = this._options.orbitAroundCursor

Expand All @@ -266,6 +300,7 @@ export class SmoothOrbitControls extends SpeckleControls {
this.world.worldOrigin.y + this.world.worldSize.y,
this.world.worldOrigin.z + this.world.worldSize.z
)

this.moveCamera()
}

Expand Down

0 comments on commit 75fcfa3

Please sign in to comment.