starfleet-ui
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#6 · main · 2 min 01 s
0Étoiles
0Forks
0Issues
| 1 | <script lang="ts"> |
| 2 | import { T, useTask } from '@threlte/core'; |
| 3 | import { OrbitControls } from '@threlte/extras'; |
| 4 | import { untrack } from 'svelte'; |
| 5 | import { cubicOut } from 'svelte/easing'; |
| 6 | import * as THREE from 'three'; |
| 7 | import type { OrbitControls as OrbitControlsImpl } from 'three/examples/jsm/controls/OrbitControls.js'; |
| 8 | import { flyDurationMs, type CameraFlight } from './camera.svelte'; |
| 9 | |
| 10 | interface Props { |
| 11 | flight: CameraFlight; |
| 12 | fov?: number; |
| 13 | minDistance?: number; |
| 14 | maxDistance?: number; |
| 15 | maxPolarAngle?: number; |
| 16 | } |
| 17 | |
| 18 | let { flight, fov = 55, minDistance = 10, maxDistance = 600, maxPolarAngle = Math.PI / 2.1 }: Props = |
| 19 | $props(); |
| 20 | |
| 21 | let camera = $state<THREE.PerspectiveCamera>(); |
| 22 | let controls = $state<OrbitControlsImpl>(); |
| 23 | |
| 24 | // Initial pose comes from the flight's home, read once: after that the camera is driven |
| 25 | // imperatively (flights below) or by the user (OrbitControls) — never by reactive props, |
| 26 | // which would snap the view back whenever they re-render. |
| 27 | const initialPos = untrack(() => flight.home); |
| 28 | const initialLook = untrack(() => flight.homeLook); |
| 29 | |
| 30 | interface Anim { |
| 31 | fromPos: THREE.Vector3; |
| 32 | toPos: THREE.Vector3; |
| 33 | fromLook: THREE.Vector3; |
| 34 | toLook: THREE.Vector3; |
| 35 | start: number; |
| 36 | duration: number; |
| 37 | } |
| 38 | let anim: Anim | null = null; |
| 39 | |
| 40 | $effect(() => { |
| 41 | const seq = flight.seq; |
| 42 | const to = $state.snapshot(flight.pos); |
| 43 | const look = $state.snapshot(flight.look); |
| 44 | if (seq === 0 || !camera || !controls) return; |
| 45 | anim = { |
| 46 | fromPos: camera.position.clone(), |
| 47 | toPos: new THREE.Vector3(...to), |
| 48 | fromLook: controls.target.clone(), |
| 49 | toLook: new THREE.Vector3(...look), |
| 50 | start: performance.now(), |
| 51 | duration: flyDurationMs() |
| 52 | }; |
| 53 | }); |
| 54 | |
| 55 | // The user grabbing the view cancels a flight in progress. |
| 56 | $effect(() => { |
| 57 | if (!controls) return; |
| 58 | const c = controls; |
| 59 | const cancel = () => (anim = null); |
| 60 | c.addEventListener('start', cancel); |
| 61 | return () => c.removeEventListener('start', cancel); |
| 62 | }); |
| 63 | |
| 64 | useTask(() => { |
| 65 | if (!anim || !camera || !controls) return; |
| 66 | const k = anim.duration > 0 ? Math.min(1, (performance.now() - anim.start) / anim.duration) : 1; |
| 67 | const e = cubicOut(k); |
| 68 | camera.position.lerpVectors(anim.fromPos, anim.toPos, e); |
| 69 | controls.target.lerpVectors(anim.fromLook, anim.toLook, e); |
| 70 | controls.update(); |
| 71 | if (k >= 1) anim = null; |
| 72 | }); |
| 73 | </script> |
| 74 | |
| 75 | <T.PerspectiveCamera makeDefault bind:ref={camera} position={initialPos} {fov} near={0.5} far={5000}> |
| 76 | <OrbitControls |
| 77 | bind:ref={controls} |
| 78 | enableDamping |
| 79 | target={initialLook} |
| 80 | {minDistance} |
| 81 | {maxDistance} |
| 82 | {maxPolarAngle} |
| 83 | /> |
| 84 | </T.PerspectiveCamera> |