video / src /jiangnan /engine /camera.ts
Surf
暮归江南 · v3 步骤6:渲染全黑时主动重置显卡上下文并重画、降级更彻底、极少数坏帧用前一帧替代而不中断、GL 诊断信息、镜头5/8 近裁剪面
3a11707
Raw History Blame Contribute Delete
7.12 kB
// Camera language: every shot is a pure function of time. Each shot has a
// landscape (16:9) and a portrait (9:16) framing.
import * as THREE from 'three'
import { BRIDGE_S, GROUND_Y, PAVILION_POS, quayPoint, riverAt, riverPoint, riverSAtX, TERRACE, terrainHeight, TREE_POS, WHEEL_S, riverHalfWidth } from './layout'
import { clamp, easeInOut, easeSine, lerp, valueNoise2 } from './rng'
import { SHOTS, shotAt } from './timeline'
export interface CamPose { pos: THREE.Vector3; target: THREE.Vector3; fov: number; roll?: number; near?: number }
const V = (x: number, y: number, z: number) => new THREE.Vector3(x, y, z)
const mix = (a: THREE.Vector3, b: THREE.Vector3, f: number) => a.clone().lerp(b, f)
/** Horizontal field of view equivalence for portrait framing. */
function portraitFov(vfovLand: number, aspect: number, widen = 0.62) {
const hl = 2 * Math.atan(Math.tan(((vfovLand * Math.PI) / 180) / 2) * (16 / 9))
const hp = hl * widen
return (2 * Math.atan(Math.tan(hp / 2) / aspect) * 180) / Math.PI
}
type ShotFn = (u: number, t: number, portrait: boolean) => CamPose
const bridge = riverPoint(BRIDGE_S, 0)
const wheelP = riverPoint(WHEEL_S, riverHalfWidth(WHEEL_S) - 3)
const shots: Record<number, ShotFn> = {
// 1 · aerial establishing shot, slowly descending toward the village, sun ahead
1: (u, _t, p) => {
const e = easeSine(u)
const pos = mix(V(470, 132, 150), V(310, 70, 82), e)
const target = mix(V(-1100, 60, 160), V(-1000, 30, 130), e)
if (p) { pos.add(V(-40, 30, -10)); target.y -= 10 }
return { pos, target, fov: 38 }
},
// 2 · gliding low over the water toward the bridge
2: (u, _t, p) => {
const e = lerp(u, easeSine(u), 0.4)
const s = lerp(BRIDGE_S + 150, BRIDGE_S + 62, e)
const r = riverPoint(s, 1.2)
const ahead = riverPoint(s - 34, 0)
const pos = V(r.x, 2.0 + Math.sin(u * 3) * 0.15, r.z)
const target = V(ahead.x, 2.3, ahead.z)
if (p) { pos.y += 0.6; target.y += 1.2 }
return { pos, target, fov: 46, roll: Math.sin(u * 2.2) * 0.012 }
},
// 3 · stone arch bridge: low lateral slide, arch + reflection form a full circle
3: (u, _t, p) => {
const e = easeInOut(u)
const along = V(bridge.tx, 0, bridge.tz)
const across = V(bridge.nx, 0, bridge.nz)
const c = V(bridge.x, 0, bridge.z)
const dist = lerp(40, 33, e)
const pos = c.clone().addScaledVector(along, dist).addScaledVector(across, lerp(-3.2, 2.6, e))
pos.y = 1.05
const target = c.clone().addScaledVector(across, lerp(-1, 0.6, e))
target.y = 3.1
if (p) { pos.addScaledVector(along, 6); target.y = 3.6 }
return { pos, target, fov: 34 }
},
// 4 · water wheel close-up, slow push-in from the opposite bank
4: (u, _t, p) => {
const e = easeSine(u)
const c = V(wheelP.x, 2.2, wheelP.z)
const across = V(wheelP.nx, 0, wheelP.nz)
const along = V(wheelP.tx, 0, wheelP.tz)
const pos = c.clone().addScaledVector(across, lerp(-15.5, -12.2, e)).addScaledVector(along, lerp(7, 3.2, e))
pos.y = lerp(2.9, 2.4, e)
const target = c.clone().addScaledVector(along, lerp(-1.5, -0.8, e))
if (p) { pos.addScaledVector(across, -2.5); target.y += 0.6 }
return { pos, target, fov: 40 }
},
// 5 · market street: walking backwards ahead of the ox cart, sun behind it
5: (u, t, p) => {
const sOx = riverSAtX(-175) + (t - 28) * 0.95
const camS = sOx + lerp(15, 12.5, u)
const q = quayPoint(camS, -1.1)
const pos = V(q.x, GROUND_Y + 1.75, q.z)
const look = quayPoint(sOx - 6, 1.0)
const target = V(look.x, GROUND_Y + 1.6, look.z)
if (p) { pos.y += 0.35; target.y += 0.9 }
// the camera tracks along a busy quay at eye height: a wider near plane clips anything
// (stall awning, passer-by, hanging goods) that would otherwise fill the frame for a moment
return { pos, target, fov: 44, near: 1.6 }
},
// 6 · the ancient camphor tree at the village entrance, backlit by the low sun
6: (u, _t, p) => {
const e = easeSine(u)
const tp = V(TREE_POS.x, GROUND_Y, TREE_POS.z)
const pos = tp.clone().add(mix(V(25, 1.8, 16.5), V(19, 2.7, 12.5), e))
const target = tp.clone().add(V(-2, mix(V(0, 8, 0), V(0, 10, 0), e).y, -4))
if (p) { pos.add(V(6, 0.5, 5)); target.y += 3 }
return { pos, target, fov: 46 }
},
// 7 · telephoto pan across the terraces and bamboo hills to the pavilion
7: (u, _t, p) => {
const e = easeInOut(u)
const pos = V(lerp(110, 98, e), lerp(26, 30, e), lerp(-120, -126, e))
const terr = V(TERRACE.cx - 30, terrainHeight(TERRACE.cx - 30, TERRACE.cz + 60) + 6, TERRACE.cz + 60)
const pav = V(PAVILION_POS.x, terrainHeight(PAVILION_POS.x, PAVILION_POS.z) + 4, PAVILION_POS.z)
const target = mix(terr, pav, e)
if (p) { target.y += 6 }
return { pos, target, fov: 21 }
},
// 8 · blue hour on the market street: lanterns light up one after another
8: (u, t, p) => {
const s = riverSAtX(-150) + (t - 55) * 2.1
const q = quayPoint(s, -0.8)
const pos = V(q.x, GROUND_Y + 2.6, q.z)
const look = quayPoint(s + 26, -1.6)
const target = V(look.x, GROUND_Y + 2.4, look.z)
if (p) { pos.y += 0.2; target.y += 0.8 }
return { pos, target, fov: 48, roll: Math.sin(u * 3) * 0.006, near: 1.2 }
},
// 9 · night river: lantern reflections, the lit boat glides toward the bridge
9: (u, _t, p) => {
const e = easeSine(u)
const s = BRIDGE_S + lerp(52, 45, e)
const r = riverPoint(s, -4.6)
const pos = V(r.x, 1.25, r.z)
const target = V(bridge.x, 3.4, bridge.z).add(V(bridge.nx, 0, bridge.nz).multiplyScalar(1.5))
if (p) { pos.y += 0.5; target.y += 0.8 }
return { pos, target, fov: 42 }
},
// 10 · crane up and pull back over the glowing village under the first stars
10: (u, _t, p) => {
const e = easeInOut(clamp(u * 1.05))
const pos = mix(V(95, 9, 34), V(205, 88, 160), e)
const target = mix(V(-10, 3, -4), V(-330, 26, -150), e)
if (p) { pos.add(V(20, 25, 20)); target.y -= 8 }
return { pos, target, fov: lerp(44, 40, e) }
},
}
/** Camera pose at absolute time t. */
export function cameraAt(t: number, aspect: number): CamPose {
const s = shotAt(t)
const u = clamp((t - s.t0) / (s.t1 - s.t0))
const portrait = aspect < 1
const pose = shots[s.id](u, t, portrait)
// subtle handheld / crane drift so nothing feels perfectly static
const n = (k: number) => (valueNoise2(t * 0.35, k * 10.3, 5) - 0.5)
const shake = s.id === 7 ? 0.25 : s.id === 1 || s.id === 10 ? 0.4 : 0.05
pose.target.add(V(n(1), n(2), n(3)).multiplyScalar(shake))
if (portrait) pose.fov = portraitFov(pose.fov, aspect)
else if (aspect < 1.7) pose.fov = (2 * Math.atan(Math.tan((pose.fov * Math.PI) / 360) * (16 / 9) / aspect) * 180) / Math.PI * 0.86 + pose.fov * 0.14
return pose
}
export function applyPose(cam: THREE.PerspectiveCamera, pose: CamPose) {
cam.position.copy(pose.pos)
cam.up.set(0, 1, 0)
cam.lookAt(pose.target)
if (pose.roll) cam.rotateZ(pose.roll)
cam.fov = pose.fov
cam.near = pose.near ?? 0.4
cam.updateProjectionMatrix()
cam.updateMatrixWorld()
}
export { SHOTS, riverAt }