fix animation (#1426)

* fix animation to vertical quaternion

* test tweak
This commit is contained in:
Kurt Hutten
2024-02-15 20:32:59 +11:00
committed by GitHub
parent 19f669b94c
commit d9bcadb062
2 changed files with 44 additions and 7 deletions

View File

@ -306,9 +306,10 @@ test('Can create sketches on all planes and their back sides', async ({
const codeTemplate = ( const codeTemplate = (
plane = 'XY', plane = 'XY',
rounded = false rounded = false,
otherThing = '1'
) => `const part001 = startSketchOn('${plane}') ) => `const part001 = startSketchOn('${plane}')
|> startProfileAt([28.91, -39${rounded ? '' : '.01'}], %)` |> startProfileAt([28.9${otherThing}, -39${rounded ? '' : '.01'}], %)`
await TestSinglePlane({ await TestSinglePlane({
viewCmd: camPos, viewCmd: camPos,
expectedCode: codeTemplate('XY'), expectedCode: codeTemplate('XY'),
@ -328,7 +329,7 @@ test('Can create sketches on all planes and their back sides', async ({
const camCmdBackSide: [number, number, number] = [-100, -100, -100] const camCmdBackSide: [number, number, number] = [-100, -100, -100]
await TestSinglePlane({ await TestSinglePlane({
viewCmd: camCmdBackSide, viewCmd: camCmdBackSide,
expectedCode: codeTemplate('-XY', true), expectedCode: codeTemplate('-XY', false, '3'),
clickCoords: { x: 601, y: 118 }, // back of red plane clickCoords: { x: 601, y: 118 }, // back of red plane
}) })
await TestSinglePlane({ await TestSinglePlane({

View File

@ -453,9 +453,27 @@ class SceneInfra {
if (!this.isFovAnimationInProgress) if (!this.isFovAnimationInProgress)
this.renderer.render(this.scene, this.camera) this.renderer.render(this.scene, this.camera)
} }
tweenCameraToQuaternion( async tweenCameraToQuaternion(
targetQuaternion: Quaternion, targetQuaternion: Quaternion,
duration: number = 500 duration = 500,
toOrthographic = true
): Promise<void> {
const isVertical = isQuaternionVertical(targetQuaternion)
let _duration = duration
if (isVertical) {
_duration = duration * 0.6
await this._tweenCameraToQuaternion(new Quaternion(), _duration, false)
}
await this._tweenCameraToQuaternion(
targetQuaternion,
_duration,
toOrthographic
)
}
_tweenCameraToQuaternion(
targetQuaternion: Quaternion,
duration = 500,
toOrthographic = false
): Promise<void> { ): Promise<void> {
return new Promise((resolve) => { return new Promise((resolve) => {
const camera = this.camera const camera = this.camera
@ -489,10 +507,10 @@ class SceneInfra {
} }
const onComplete = async () => { const onComplete = async () => {
if (isReducedMotion()) { if (isReducedMotion() && toOrthographic) {
cameraAtTime(0.99) cameraAtTime(0.99)
this.useOrthographicCamera() this.useOrthographicCamera()
} else { } else if (toOrthographic) {
await this.animateToOrthographic() await this.animateToOrthographic()
} }
if (isVertical) cameraAtTime(1) if (isVertical) cameraAtTime(1)
@ -1056,6 +1074,24 @@ function calculateNearFarFromFOV(fov: number) {
return { z_near: 0.1, z_far } return { z_near: 0.1, z_far }
} }
export function getSceneScale(
camera: PerspectiveCamera | OrthographicCamera,
target: Vector3
): number {
const distance =
camera instanceof PerspectiveCamera
? camera.position.distanceTo(target)
: 63.7942123 / camera.zoom
if (distance <= 20) return 0.1
else if (distance > 20 && distance <= 200) return 1
else if (distance > 200 && distance <= 2000) return 10
else if (distance > 2000 && distance <= 20000) return 100
else if (distance > 20000) return 1000
return 1
}
export function isQuaternionVertical(q: Quaternion) { export function isQuaternionVertical(q: Quaternion) {
const v = new Vector3(0, 0, 1).applyQuaternion(q) const v = new Vector3(0, 0, 1).applyQuaternion(q)
// no x or y components means it's vertical // no x or y components means it's vertical