| 1 | // Navegación: órbita (modo cursor), vuelo libre sin colisiones (modo |
| 2 | // videojuego, atraviesa paredes) y vuelos de cámara con easing. |
| 3 | import * as THREE from 'three'; |
| 4 | import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; |
| 5 | |
| 6 | const EASE = t => 1 - Math.pow(1 - t, 3); |
| 7 | |
| 8 | export function createNavigation(camera, dom, cityBox) { |
| 9 | const orbit = new OrbitControls(camera, dom); |
| 10 | orbit.enableDamping = true; |
| 11 | orbit.dampingFactor = 0.08; |
| 12 | orbit.maxPolarAngle = Math.PI * 0.495; |
| 13 | |
| 14 | const extent = Math.max(cityBox.sx, cityBox.sz); |
| 15 | const center = new THREE.Vector3(cityBox.cx, 0, cityBox.cz); |
| 16 | |
| 17 | // ── vuelos de cámara ──────────────────────────────────────────────── |
| 18 | let tween = null; |
| 19 | function flyTo(pos, target, ms = 1100) { |
| 20 | tween = { |
| 21 | t0: performance.now(), ms, |
| 22 | p0: camera.position.clone(), p1: pos.clone(), |
| 23 | l0: orbit.target.clone(), l1: target.clone(), |
| 24 | }; |
| 25 | } |
| 26 | |
| 27 | const VIEWS = { |
| 28 | reset: () => flyTo( |
| 29 | center.clone().add(new THREE.Vector3(extent * 0.55, extent * 0.42, extent * 0.62)), |
| 30 | center.clone(), |
| 31 | ), |
| 32 | aerial: () => flyTo( |
| 33 | center.clone().add(new THREE.Vector3(0.001, extent * 1.25, 0.001)), |
| 34 | center.clone(), |
| 35 | ), |
| 36 | skyline: () => flyTo( |
| 37 | center.clone().add(new THREE.Vector3(extent * 0.72, 14, extent * 0.1)), |
| 38 | center.clone().setY(12), |
| 39 | ), |
| 40 | focus: (box, dist = 2.5) => { |
| 41 | const target = new THREE.Vector3(box.cx, box.cy, box.cz); |
| 42 | const d = Math.max(Math.max(box.sx, box.sy, box.sz) * dist, 14); |
| 43 | const dir = camera.position.clone().sub(target); |
| 44 | dir.y = 0; |
| 45 | if (dir.lengthSq() < 1) dir.set(1, 0, 1); |
| 46 | dir.normalize(); |
| 47 | const pos = target.clone().addScaledVector(dir, d); |
| 48 | pos.y = box.cy + d * 0.45; |
| 49 | flyTo(pos, target, 900); |
| 50 | }, |
| 51 | }; |
| 52 | |
| 53 | // ── modo vuelo ────────────────────────────────────────────────────── |
| 54 | const fly = { |
| 55 | active: false, |
| 56 | keys: new Set(), |
| 57 | vel: new THREE.Vector3(), |
| 58 | yaw: 0, pitch: 0, |
| 59 | }; |
| 60 | |
| 61 | function enterFly() { |
| 62 | if (fly.active) return; |
| 63 | fly.active = true; |
| 64 | orbit.enabled = false; |
| 65 | tween = null; |
| 66 | const e = new THREE.Euler().setFromQuaternion(camera.quaternion, 'YXZ'); |
| 67 | fly.yaw = e.y; fly.pitch = e.x; |
| 68 | dom.requestPointerLock(); |
| 69 | dom.dispatchEvent(new CustomEvent('flymode', { detail: true })); |
| 70 | } |
| 71 | function exitFly() { |
| 72 | if (!fly.active) return; |
| 73 | fly.active = false; |
| 74 | fly.keys.clear(); |
| 75 | orbit.enabled = true; |
| 76 | // recoloca el target de la órbita delante de la cámara |
| 77 | const fwd = new THREE.Vector3(); |
| 78 | camera.getWorldDirection(fwd); |
| 79 | orbit.target.copy(camera.position).addScaledVector(fwd, 40); |
| 80 | if (document.pointerLockElement === dom) document.exitPointerLock(); |
| 81 | dom.dispatchEvent(new CustomEvent('flymode', { detail: false })); |
| 82 | } |
| 83 | |
| 84 | const onLockChange = () => { |
| 85 | if (document.pointerLockElement !== dom && fly.active) exitFly(); |
| 86 | }; |
| 87 | const onMouseMove = (e) => { |
| 88 | if (!fly.active) return; |
| 89 | fly.yaw -= e.movementX * 0.0022; |
| 90 | fly.pitch = Math.max(-1.45, Math.min(1.45, fly.pitch - e.movementY * 0.0022)); |
| 91 | }; |
| 92 | const onKeyDown = (e) => { |
| 93 | if (e.code === 'KeyF' && !e.repeat && document.activeElement?.tagName !== 'INPUT') { |
| 94 | fly.active ? exitFly() : enterFly(); |
| 95 | } |
| 96 | if (fly.active) fly.keys.add(e.code); |
| 97 | }; |
| 98 | const onKeyUp = (e) => fly.keys.delete(e.code); |
| 99 | document.addEventListener('pointerlockchange', onLockChange); |
| 100 | document.addEventListener('mousemove', onMouseMove); |
| 101 | document.addEventListener('keydown', onKeyDown); |
| 102 | document.addEventListener('keyup', onKeyUp); |
| 103 | |
| 104 | function dispose() { |
| 105 | exitFly(); |
| 106 | document.removeEventListener('pointerlockchange', onLockChange); |
| 107 | document.removeEventListener('mousemove', onMouseMove); |
| 108 | document.removeEventListener('keydown', onKeyDown); |
| 109 | document.removeEventListener('keyup', onKeyUp); |
| 110 | orbit.dispose(); |
| 111 | } |
| 112 | |
| 113 | // ── actualización por frame ───────────────────────────────────────── |
| 114 | function update(dt) { |
| 115 | if (tween) { |
| 116 | const t = Math.min(1, (performance.now() - tween.t0) / tween.ms); |
| 117 | const k = EASE(t); |
| 118 | camera.position.lerpVectors(tween.p0, tween.p1, k); |
| 119 | orbit.target.lerpVectors(tween.l0, tween.l1, k); |
| 120 | if (t >= 1) tween = null; |
| 121 | } |
| 122 | if (fly.active) { |
| 123 | camera.quaternion.setFromEuler(new THREE.Euler(fly.pitch, fly.yaw, 0, 'YXZ')); |
| 124 | const speed = (fly.keys.has('ShiftLeft') || fly.keys.has('ShiftRight') ? 90 : 28); |
| 125 | const dir = new THREE.Vector3(); |
| 126 | const fwd = new THREE.Vector3(); |
| 127 | camera.getWorldDirection(fwd); |
| 128 | const right = new THREE.Vector3().crossVectors(fwd, camera.up).normalize(); |
| 129 | if (fly.keys.has('KeyW') || fly.keys.has('ArrowUp')) dir.add(fwd); |
| 130 | if (fly.keys.has('KeyS') || fly.keys.has('ArrowDown')) dir.sub(fwd); |
| 131 | if (fly.keys.has('KeyD') || fly.keys.has('ArrowRight')) dir.add(right); |
| 132 | if (fly.keys.has('KeyA') || fly.keys.has('ArrowLeft')) dir.sub(right); |
| 133 | if (fly.keys.has('Space') || fly.keys.has('KeyE')) dir.y += 1; |
| 134 | if (fly.keys.has('KeyC') || fly.keys.has('KeyQ') || fly.keys.has('ControlLeft')) dir.y -= 1; |
| 135 | if (dir.lengthSq()) dir.normalize().multiplyScalar(speed); |
| 136 | fly.vel.lerp(dir, 1 - Math.pow(0.0001, dt)); // aceleración suave |
| 137 | camera.position.addScaledVector(fly.vel, dt); |
| 138 | } else { |
| 139 | orbit.update(); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | return { orbit, fly, update, flyTo, VIEWS, enterFly, exitFly, dispose }; |
| 144 | } |