t03-camera-contract.mjs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. /**
  2. * T03 逻辑检查:三键相机契约、旋转中心规则与阻尼(票 03 / ADR 0008)。
  3. *
  4. * 仓库没有测试运行器,且项目约定不为验证引入新框架,所以这里用零依赖 Node 断言脚本:
  5. * 相机语义全在 `domain/camera.js`(纯数学)与 `services/cameraControl.js`(状态迁移,引擎依赖注入),
  6. * 因此可以用**假引擎**驱动整套交互,不需要浏览器、不需要 cesium。
  7. * 真机行为(Cesium 默认输入确实被替换、中心确实落在屏幕中心、阻尼真的在真机上生效)
  8. * 由 t03-camera-walkthrough.mjs 覆盖。
  9. *
  10. * 运行:node .scratch/cockpit-shell-panel-layout/verify/t03-camera-contract.mjs
  11. */
  12. import assert from 'node:assert/strict'
  13. import { DAMPING_TAU_MS, FOCUS_FLIGHT_MS, PITCH_LIMITS, RANGE_LIMITS } from '../../../src/modules/cockpit/domain/camera.js'
  14. import * as camera from '../../../src/modules/cockpit/domain/camera.js'
  15. import { attachCameraInput, createCameraControl } from '../../../src/modules/cockpit/services/cameraControl.js'
  16. const GROUND = { lon: 110.39, lat: 28.46, height: 0 }
  17. const XIANGLING = { lon: 110.396, lat: 28.455, height: 0 } // 沅陵某监测点
  18. const FRAME_MS = 1000 / 60
  19. function createFakeEngine({ viewport = { width: 1680, height: 900 }, fovY = Math.PI / 3, ground = GROUND } = {}) {
  20. const applied = []
  21. let clock = 1000
  22. let pendingFrame = null
  23. let groundAt = { ...ground }
  24. return {
  25. applied,
  26. engine: {
  27. applyView(pivot, hpr) {
  28. applied.push({ pivot: { ...pivot }, hpr: { ...hpr } })
  29. },
  30. readView() {
  31. return { heading: 0, pitch: -60, range: 10000 }
  32. },
  33. pickGroundAtCenter() {
  34. return { ...groundAt }
  35. },
  36. projectPivot() {
  37. return { x: viewport.width / 2, y: viewport.height / 2 }
  38. },
  39. inputTarget() {
  40. return null
  41. },
  42. viewport() {
  43. return { ...viewport }
  44. },
  45. fovY() {
  46. return fovY
  47. },
  48. now() {
  49. return clock
  50. },
  51. requestFrame(callback) {
  52. pendingFrame = callback
  53. },
  54. },
  55. /** 换掉「屏幕中心拾取到哪」——用来验证松手时的重锚规则。 */
  56. setGround(next) {
  57. groundAt = { lon: next.lon, lat: next.lat, height: next.height || 0 }
  58. },
  59. get pendingFrame() {
  60. return pendingFrame
  61. },
  62. advance(ms) {
  63. clock += ms
  64. },
  65. /** 跑一帧:假的 rAF 不带时间戳,控制器会退回 `engine.now()`(真机用真时间戳)。 */
  66. runFrame(dtMs = FRAME_MS) {
  67. const callback = pendingFrame
  68. pendingFrame = null
  69. clock += dtMs
  70. if (callback) callback()
  71. }, last() {
  72. return applied[applied.length - 1]
  73. },
  74. }
  75. }
  76. function newControl(options) {
  77. const fake = createFakeEngine(options)
  78. const control = createCameraControl({ engine: fake.engine, ...(options && options.control) })
  79. control.syncFromEngine()
  80. fake.applied.length = 0
  81. return { fake, control }
  82. }
  83. /** 一直跑到停稳(阻尼 + 换中心飞行都收敛),返回跑过的帧数。 */
  84. function settle(fake, control, maxFrames = 400) {
  85. let frames = 0
  86. while (!control.getState().settled && frames < maxFrames) {
  87. fake.runFrame()
  88. frames += 1
  89. }
  90. assert.ok(frames < maxFrames, `阻尼/飞行应在有限帧内停稳(已跑 ${frames} 帧仍未 settled)`)
  91. return frames
  92. }
  93. /** 假元素:记录监听器,供 attachCameraInput 的绑定/解绑断言。 */
  94. function createFakeElement() {
  95. const handlers = new Map()
  96. return {
  97. handlers,
  98. defaultPrevented: 0,
  99. addEventListener(type, fn) {
  100. if (!handlers.has(type)) handlers.set(type, new Set())
  101. handlers.get(type).add(fn)
  102. },
  103. removeEventListener(type, fn) {
  104. if (handlers.has(type)) handlers.get(type).delete(fn)
  105. },
  106. setPointerCapture() {},
  107. releasePointerCapture() {},
  108. fire(type, payload = {}) {
  109. const set = handlers.get(type)
  110. if (!set) return
  111. const event = {
  112. preventDefault() {
  113. this.defaultPrevented = true
  114. },
  115. ...payload,
  116. }
  117. set.forEach((fn) => fn(event))
  118. return event
  119. },
  120. }
  121. }
  122. const near = (a, b, tolerance, message) =>
  123. assert.ok(Math.abs(a - b) <= tolerance, `${message}:期望 ≈${b}(±${tolerance}),实际 ${a}`)
  124. const round3 = (n) => Number(n.toFixed(3))
  125. const metersPerPxAt = (range) => camera.metersPerPixel({ range, fovYRad: Math.PI / 3, viewportHeight: 900 })
  126. const degreesLonFor = (meters, lat) => meters / (111320 * Math.cos((lat * Math.PI) / 180))
  127. // ---------- 1. 左键拖拽只平移 ----------
  128. {
  129. const { fake, control } = newControl()
  130. const before = control.getState()
  131. control.beginDrag({ button: 0, x: 0, y: 0 })
  132. control.moveDrag({ x: 100, y: 0 })
  133. const after = control.getState()
  134. assert.deepEqual(after.hpr, before.hpr, '平移不得改 heading / pitch / range')
  135. // 维护者 2026-09-17 裁定:**抓着地图拖** —— 拖右 → 相机左移、画面右移,所以中心往西(lon 减小)。
  136. const expectedLon = before.pivot.lon - degreesLonFor(100 * metersPerPxAt(before.hpr.range), GROUND.lat)
  137. near(after.pivot.lon, expectedLon, Math.abs(expectedLon - before.pivot.lon) * 0.02, '拖右应把中心按像素比例西移(相机左移)')
  138. near(after.pivot.lat, before.pivot.lat, 1e-6, 'heading=0 时右拖不应产生南北位移')
  139. settle(fake, control)
  140. near(fake.last().pivot.lon, expectedLon, 1e-9, '阻尼收敛后下发给相机的中心应等于目标')
  141. console.log('PASS 1 左键拖拽只平移(heading/pitch/range 原样,中心按像素比例随「抓地图」方向西移)')
  142. }
  143. // ---------- 2. 平移方向 = 抓着地图拖(维护者裁定的语义钉子) ----------
  144. {
  145. const { fake, control } = newControl()
  146. control.beginDrag({ button: 0, x: 0, y: 0 })
  147. control.moveDrag({ x: 0, y: 100 })
  148. const down = control.getState()
  149. assert.ok(
  150. down.pivot.lat > GROUND.lat,
  151. `heading=0 时下拖应把中心往北带(画面下移、相机上移):${JSON.stringify(down.pivot)}`,
  152. )
  153. const { fake: fake2, control: control2 } = newControl()
  154. control2.beginDrag({ button: 2, x: 0, y: 0 })
  155. control2.moveDrag({ x: 90 / camera.ORBIT_SENSITIVITY.heading, y: 0 }) // 先把 heading 拖到 90°(向东看)
  156. control2.endDrag()
  157. settle(fake2, control2)
  158. near(control2.getState().hpr.heading, 90, 1e-6, '轨道旋转应把 heading 拖到 90°')
  159. const beforePan = control2.getState()
  160. control2.beginDrag({ button: 0, x: 0, y: 0 })
  161. control2.moveDrag({ x: 100, y: 0 })
  162. const afterPan = control2.getState()
  163. assert.ok(
  164. afterPan.pivot.lat > beforePan.pivot.lat,
  165. `heading=90°(向东看)时拖右应把中心往北带(画面右移=相机往左=北):${JSON.stringify(afterPan.pivot)}`,
  166. )
  167. near(afterPan.pivot.lon, beforePan.pivot.lon, 1e-6, 'heading=90° 时右拖不应产生东西位移')
  168. console.log('PASS 2 平移方向 = 抓着地图拖(拖右→画面右移/相机左移,heading=0/90 两点验证)')
  169. }
  170. // ---------- 3. 右键拖拽只做轨道旋转 ----------
  171. {
  172. const { fake, control } = newControl()
  173. const before = control.getState()
  174. control.beginDrag({ button: 2, x: 0, y: 0 })
  175. control.moveDrag({ x: 100, y: 50 })
  176. const after = control.getState()
  177. near(after.hpr.heading, 28, 1e-6, 'heading 变化 = dx × 灵敏度')
  178. near(after.hpr.pitch, -60 - 50 * 0.22, 1e-6, 'pitch 变化 = -dy × 灵敏度')
  179. assert.equal(after.hpr.range, before.hpr.range, '轨道旋转不得改弹簧臂长度')
  180. assert.deepEqual(after.pivot, before.pivot, '轨道旋转不得移动中心')
  181. settle(fake, control)
  182. near(fake.last().hpr.range, before.hpr.range, 1e-9, '阻尼收敛后下发的 range 仍应原样')
  183. console.log('PASS 3 右键拖拽只做轨道旋转(heading/pitch 按像素变化,range 与中心不动)')
  184. }
  185. // ---------- 4. pitch 夹在上下限内 ----------
  186. {
  187. const { fake, control } = newControl()
  188. control.beginDrag({ button: 2, x: 0, y: 0 })
  189. control.moveDrag({ x: 0, y: 5000 })
  190. near(control.getState().hpr.pitch, PITCH_LIMITS.ground.min, 1e-9, '一直下拖应夹在下限')
  191. control.moveDrag({ x: 0, y: -10000 })
  192. near(control.getState().hpr.pitch, PITCH_LIMITS.ground.max, 1e-9, '一直上拖应夹在上限')
  193. settle(fake, control)
  194. near(fake.last().hpr.pitch, PITCH_LIMITS.ground.max, 1e-9, '阻尼收敛后下发的 pitch 也应在限内')
  195. console.log(`PASS 4 pitch 夹在 [${PITCH_LIMITS.ground.min}, ${PITCH_LIMITS.ground.max}](地面视图)`)
  196. }
  197. // ---------- 5. 右键按下锁定中心:拖拽中不重新拾取 ----------
  198. {
  199. const { fake, control } = newControl()
  200. const locked = control.getState().pivot
  201. control.beginDrag({ button: 2, x: 0, y: 0 })
  202. // 拖拽进行中来了「选中某对象」:中心不许被改,飞行也不许插进来。
  203. control.setFocus(XIANGLING)
  204. control.moveDrag({ x: 40, y: 0 })
  205. assert.deepEqual(control.getState().pivot, locked, '拖拽中下发的中心必须仍是按下时锁定的那个')
  206. control.endDrag()
  207. assert.equal(control.getState().flying, true, '松手后应开始平滑换中心')
  208. settle(fake, control)
  209. const final = control.getState().pivot
  210. near(final.lon, XIANGLING.lon, 1e-9, '飞行结束后中心应为选中对象')
  211. near(final.lat, XIANGLING.lat, 1e-9, '飞行结束后中心应为选中对象')
  212. console.log('PASS 5 右键按下锁定中心(拖拽中的选中不插队;松手后才开始换中心)')
  213. }
  214. // ---------- 6. 滚轮只改弹簧臂长度 ----------
  215. {
  216. const { fake, control } = newControl()
  217. const before = control.getState()
  218. const handled = control.wheel({ deltaY: -100 })
  219. const after = control.getState()
  220. assert.equal(handled, true, '滚轮应被相机契约接管(调用方据此 preventDefault)')
  221. near(after.hpr.range, 10000 * Math.exp(-100 * 0.0012), 1e-6, '上滚拉近:range 按指数缩')
  222. assert.equal(after.hpr.heading, before.hpr.heading, '滚轮不得转 heading')
  223. assert.equal(after.hpr.pitch, before.hpr.pitch, '滚轮不得改 pitch')
  224. assert.deepEqual(after.pivot, before.pivot, '滚轮不得移动中心')
  225. control.wheel({ deltaY: -1e6 })
  226. near(control.getState().hpr.range, RANGE_LIMITS.min, 1e-9, '拉近到极限应夹住下限')
  227. control.wheel({ deltaY: 1e6 })
  228. near(control.getState().hpr.range, RANGE_LIMITS.max, 1e-9, '推远到极限应夹住上限')
  229. settle(fake, control)
  230. near(fake.last().hpr.range, RANGE_LIMITS.max, 1e-9, '阻尼收敛后下发的 range 应等于目标')
  231. console.log(`PASS 6 滚轮只改弹簧臂长度(并夹在 [${RANGE_LIMITS.min}, ${RANGE_LIMITS.max}] 米)`)
  232. }
  233. // ---------- 7. 中心规则:选中优先 / 取消不回退 / 平移才回退 ----------
  234. {
  235. const { fake, control } = newControl()
  236. assert.deepEqual(control.getState().pivot, GROUND, '未选中时中心 = 屏幕中心的地面拾取点')
  237. control.setFocus(XIANGLING)
  238. settle(fake, control)
  239. assert.deepEqual(control.getState().pivot, XIANGLING, '选中后中心 = 对象地表点')
  240. control.setFocus(null)
  241. assert.deepEqual(control.getState().pivot, XIANGLING, '取消选中不得回退中心')
  242. assert.equal(control.getState().focus, null, '取消选中应清掉选中态')
  243. control.beginDrag({ button: 0, x: 0, y: 0 })
  244. control.moveDrag({ x: 10, y: 0 })
  245. // 真实引擎里「屏幕中心拾取」与平移推算值应当几乎重合(lookAt 保证中心射线落在中心点上),
  246. // 所以这里让拾取点跟上推算值,验证「平移把中心交回地面拾取点」这条语义仍然成立。
  247. const panned = control.getState().pivot
  248. fake.setGround(panned)
  249. control.endDrag()
  250. assert.deepEqual(control.getState().pivot, panned, '左键平移后中心应是地面拾取点(不再是选中对象)')
  251. assert.notDeepEqual(control.getState().pivot, XIANGLING, '平移后中心不再跟随选中对象')
  252. console.log('PASS 7 中心规则:选中优先 → 取消不回退 → 平移才更新为地面拾取点')
  253. }
  254. // ---------- 8. 换中心平滑、且全程保持 heading 与 range ----------
  255. {
  256. const { fake, control } = newControl()
  257. const before = control.getState()
  258. control.setFocus(XIANGLING)
  259. // 起手第一帧(elapsed = 0)必须仍停在原中心 —— 换中心不许瞬移。
  260. fake.runFrame(0)
  261. near(control.getState().pivot.lon, before.pivot.lon, 1e-12, '换中心第一帧应从原中心出发')
  262. const samples = []
  263. let frames = 0
  264. while (control.getState().flying && frames++ < 200) {
  265. fake.runFrame(FOCUS_FLIGHT_MS / 8)
  266. const now = control.getState()
  267. assert.equal(now.hpr.heading, before.hpr.heading, '换中心过程中 heading 必须保持')
  268. assert.equal(now.hpr.range, before.hpr.range, '换中心过程中 range 必须保持')
  269. samples.push(now.pivot.lon)
  270. }
  271. assert.ok(samples.length >= 4, `换中心应有多帧(实际 ${samples.length})`)
  272. assert.equal(
  273. samples.every((lon, i) => i === 0 || lon >= samples[i - 1] - 1e-12),
  274. true,
  275. `中心应朝目标单调推进:${samples.join(' → ')}`,
  276. )
  277. settle(fake, control)
  278. near(control.getState().pivot.lon, XIANGLING.lon, 1e-9, '末帧应到达目标中心')
  279. assert.equal(control.getState().flying, false, '到达后应结束飞行')
  280. console.log(`PASS 8 换中心平滑(${samples.length} 帧单调推进、heading/range 全程不变)`)
  281. }
  282. // ---------- 9. 阻尼:渐入、不超调、停稳、且与帧率无关 ----------
  283. {
  284. const { fake, control } = newControl()
  285. const before = control.getState()
  286. control.beginDrag({ button: 0, x: 0, y: 0 })
  287. control.moveDrag({ x: 400, y: 0 })
  288. const target = control.getState()
  289. // 目标立刻到位(输入不丢),实际态还在起点附近 —— 这就是「丝滑」的来源。
  290. assert.notDeepEqual(target.pivot, before.pivot, '目标态应当立刻跟上输入')
  291. assert.deepEqual(control.getState().shownPivot, before.pivot, '首个阻尼帧之前,实际态还在起点')
  292. const targetShift = target.pivot.lon - before.pivot.lon
  293. fake.runFrame(FRAME_MS)
  294. const oneFrame = control.getState()
  295. const oneFrameShift = oneFrame.shownPivot.lon - before.pivot.lon
  296. assert.ok(
  297. Math.abs(oneFrameShift) > 0 && Math.abs(oneFrameShift) < Math.abs(targetShift) * 0.6,
  298. `一帧内应只走一小部分(实际 ${oneFrameShift} / 目标 ${targetShift})`,
  299. )
  300. assert.equal(oneFrame.settled, false, '一帧后还没停稳')
  301. // 逼近过程不得超调(单调朝目标,不越过)。
  302. let previous = Math.abs(target.pivot.lon - oneFrame.shownPivot.lon)
  303. let frames = 1
  304. while (!control.getState().settled && frames < 400) {
  305. fake.runFrame(FRAME_MS)
  306. const gap = Math.abs(target.pivot.lon - control.getState().shownPivot.lon)
  307. assert.ok(gap <= previous + 1e-12, `阻尼不得超调:缺口从 ${previous} 变成 ${gap}`)
  308. previous = gap
  309. frames += 1
  310. }
  311. assert.equal(control.getState().settled, true, '有限帧内应停稳')
  312. assert.deepEqual(control.getState().shownPivot, target.pivot, '停稳后实际态应吸附到目标')
  313. console.log(`PASS 9a 阻尼渐入、不超调、${frames} 帧内停稳并吸附(τ=${DAMPING_TAU_MS}ms)`)
  314. // 帧率无关:同样的真实时长,60fps 与 120fps 收敛到同一个位置(±1%)。
  315. const walk = (dtMs, totalMs) => {
  316. const { fake: f, control: c } = newControl()
  317. const start = c.getState()
  318. c.beginDrag({ button: 0, x: 0, y: 0 })
  319. c.moveDrag({ x: 400, y: 0 })
  320. // 固定帧数,别用浮点累加判停:累加误差会多跑一帧,把「帧率无关」测成假的偏差。
  321. const frames = Math.round(totalMs / dtMs)
  322. for (let i = 0; i < frames; i += 1) f.runFrame(dtMs)
  323. const shift = c.getState().shownPivot.lon - start.pivot.lon
  324. const full = c.getState().pivot.lon - start.pivot.lon
  325. return shift / full
  326. }
  327. const at60 = walk(1000 / 60, 200)
  328. const at120 = walk(1000 / 120, 200)
  329. near(at60, at120, 0.01, '同样 200ms 内 60fps 与 120fps 的收敛比例应一致(±1%)')
  330. assert.ok(at60 > 0.6 && at60 < 0.95, `200ms(≈1.8τ)应走掉大半但未走完:实际 ${at60}`)
  331. console.log(`PASS 9b 阻尼与帧率无关(60fps ${(at60 * 100).toFixed(1)}% vs 120fps ${(at120 * 100).toFixed(1)}%)`)
  332. }
  333. // ---------- 9c. 松手有惯性:平移与滚轮/右键手感一致(维护者 2026-09-17 复验反馈) ----------
  334. {
  335. // 平移:拖拽中一直有滞后(实际态追不上目标态),松手后必须**继续滑行**到目标。
  336. const { fake, control } = newControl()
  337. control.beginDrag({ button: 0, x: 0, y: 0 })
  338. for (let i = 1; i <= 6; i += 1) {
  339. control.moveDrag({ x: i * 80, y: 0 })
  340. fake.runFrame(FRAME_MS)
  341. }
  342. const atRelease = control.getState()
  343. const gapAtRelease = Math.abs(atRelease.pivot.lon - atRelease.shownPivot.lon)
  344. assert.ok(gapAtRelease > 0, '快速拖动时实际态应落后于目标态(这正是惯性来源)')
  345. control.endDrag()
  346. fake.runFrame(FRAME_MS)
  347. const justAfter = control.getState()
  348. const gapAfterOneFrame = Math.abs(justAfter.pivot.lon - justAfter.shownPivot.lon)
  349. assert.ok(
  350. gapAfterOneFrame > gapAtRelease * 0.5,
  351. `松手后目标不许被改写(否则滞后被抹平、惯性消失):松手前缺口 ${gapAtRelease},一帧后 ${gapAfterOneFrame}`,
  352. )
  353. // 继续滑行:实际态继续朝目标推进,直到吸附。
  354. const before = justAfter.shownPivot.lon
  355. let frames = 1
  356. while (!control.getState().settled && frames < 400) {
  357. fake.runFrame(FRAME_MS)
  358. frames += 1
  359. }
  360. const glided = Math.abs(control.getState().shownPivot.lon - before)
  361. assert.ok(glided > 0, `松手后应继续滑行一段(实际滑了 ${glided})`)
  362. assert.deepEqual(control.getState().shownPivot, control.getState().pivot, '滑行结束后吸附到目标')
  363. console.log(
  364. `PASS 9c 平移松手有惯性(松手时滞后 ${gapAtRelease.toFixed(6)}°,之后又滑行 ${glided.toFixed(6)}°、${frames} 帧停稳)`,
  365. )
  366. // 滚轮与右键:滚完/松手后同样继续收敛 —— 三种交互手感一致(按各自动的维度量)。
  367. const gliding = (act) => {
  368. const { fake: f, control: c } = newControl()
  369. const read = () => c.getState()
  370. const lagOf = (s) => (act === 'wheel' ? Math.abs(s.hpr.range - s.shownHpr.range) : Math.abs(s.hpr.heading - s.shownHpr.heading))
  371. if (act === 'wheel') {
  372. c.wheel({ deltaY: -600 })
  373. } else {
  374. c.beginDrag({ button: 2, x: 0, y: 0 })
  375. c.moveDrag({ x: 200, y: 0 })
  376. c.endDrag()
  377. }
  378. f.runFrame(FRAME_MS)
  379. const lag = lagOf(read())
  380. let frames = 1
  381. while (!c.getState().settled && frames < 400) {
  382. f.runFrame(FRAME_MS)
  383. frames += 1
  384. }
  385. return { lag, frames }
  386. }
  387. const wheel = gliding('wheel')
  388. const orbit = gliding('orbit')
  389. assert.ok(wheel.lag > 0, `滚轮滚完应有余韵(range 滞后 ${wheel.lag})`)
  390. assert.ok(orbit.lag > 0, `右键松手应有余韵(heading 滞后 ${orbit.lag})`)
  391. console.log(
  392. `PASS 9c2 滚轮与右键同样有余韵(一帧后 range 滞后 ${wheel.lag.toFixed(1)}m / heading 滞后 ${orbit.lag.toFixed(3)}°)`,
  393. )
  394. }
  395. // ---------- 9d. 松手重锚只清微小误差,绝不把画面拽回去 ----------
  396. {
  397. // 偏差可忽略(比 range 的 1% 小得多):允许重锚,中心落到真实地表拾取点。
  398. const { fake, control } = newControl()
  399. control.beginDrag({ button: 0, x: 0, y: 0 })
  400. control.moveDrag({ x: 200, y: 0 })
  401. const target = control.getState().pivot
  402. fake.setGround({ lon: target.lon + 1e-6, lat: target.lat }) // 偏差 ≈ 0.1m,远小于阈值
  403. const shownBefore = control.getState().shownPivot.lon
  404. control.endDrag()
  405. near(control.getState().pivot.lon, target.lon + 1e-6, 1e-12, '偏差可忽略时应重锚到拾取点')
  406. assert.equal(control.getState().shownPivot.lon, shownBefore, '重锚不得动实际态(不许把画面拽回去)')
  407. // 偏差过大(等距近似在该尺度已失效):宁可不重锚,也不让画面跳。
  408. const { fake: f2, control: c2 } = newControl()
  409. c2.beginDrag({ button: 0, x: 0, y: 0 })
  410. c2.moveDrag({ x: 200, y: 0 })
  411. const before2 = c2.getState().pivot
  412. f2.setGround({ lon: before2.lon + 5, lat: before2.lat }) // 偏差几百公里 ≫ range*1%
  413. c2.endDrag()
  414. assert.deepEqual(c2.getState().pivot, before2, '偏差过大时不得重锚(避免画面瞬移)')
  415. console.log('PASS 9d 松手重锚只在偏差可忽略时发生(大偏差保持推算值,不跳画面)')
  416. }
  417. // ---------- 10. 用户操作打断换中心飞行 ----------
  418. {
  419. const { fake, control } = newControl()
  420. control.setFocus(XIANGLING)
  421. assert.equal(control.getState().flying, true, '应处于换中心飞行中')
  422. control.beginDrag({ button: 0, x: 0, y: 0 })
  423. assert.equal(control.getState().flying, false, '一上手就应打断飞行(输入永远优先)')
  424. console.log('PASS 10 用户操作打断换中心飞行')
  425. }
  426. // ---------- 10b. 阻尼可现场拖调(手感是主观的,维护者要能在控制台试) ----------
  427. {
  428. const { fake, control } = newControl()
  429. assert.equal(control.getState().dampingTauMs, DAMPING_TAU_MS, '默认应取 domain 里的 τ')
  430. // 调大 τ:同样一帧走得更少(更飘)。
  431. const shiftWith = (tau) => {
  432. const { fake: f, control: c } = newControl()
  433. c.setDampingTau(tau)
  434. const start = c.getState().pivot.lon
  435. c.beginDrag({ button: 0, x: 0, y: 0 })
  436. c.moveDrag({ x: 400, y: 0 })
  437. f.runFrame(FRAME_MS)
  438. return Math.abs(c.getState().shownPivot.lon - start)
  439. }
  440. const loose = shiftWith(400)
  441. const tight = shiftWith(40)
  442. assert.ok(tight > loose * 1.5, `τ 越小越跟手:τ=40 走 ${tight},τ=400 只走 ${loose}`)
  443. // τ = 0:不做阻尼(一帧内直接贴到目标),给「要完全即时手感」的场合留的档。
  444. const { fake: f2, control: c2 } = newControl()
  445. c2.setDampingTau(0)
  446. c2.beginDrag({ button: 0, x: 0, y: 0 })
  447. c2.moveDrag({ x: 400, y: 0 })
  448. f2.runFrame(FRAME_MS)
  449. assert.deepEqual(c2.getState().shownPivot, c2.getState().pivot, 'τ=0 时一帧内实际态应贴到目标态')
  450. assert.equal(c2.getState().settled, true, 'τ=0 时下完指令一帧即停稳')
  451. assert.equal(f2.pendingFrame, null, 'τ=0 停稳后不应再排帧')
  452. // 非法值不生效(守住旋钮)。
  453. assert.equal(control.setDampingTau(-5), DAMPING_TAU_MS, '负数应被拒绝并保持原值')
  454. assert.equal(control.setDampingTau('abc'), DAMPING_TAU_MS, '非数字应被拒绝并保持原值')
  455. console.log(`PASS 10b 阻尼可拖调(τ=40 走 ${round3(tight)}° > τ=400 走 ${round3(loose)}°;τ=0 直接跟手)`)
  456. }
  457. // ---------- 11. DOM 绑定:三类输入都接上,右键菜单与滚轮默认行为被吃掉 ----------
  458. {
  459. const { fake, control } = newControl()
  460. const element = createFakeElement()
  461. const detach = attachCameraInput(element, control)
  462. assert.deepEqual(
  463. [...element.handlers.keys()].sort(),
  464. ['contextmenu', 'pointercancel', 'pointerdown', 'pointermove', 'pointerup', 'wheel'],
  465. '应绑定左右键拖拽与滚轮(并吃掉右键菜单)',
  466. )
  467. assert.equal(element.fire('contextmenu').defaultPrevented, true, '右键菜单必须被 preventDefault')
  468. element.fire('pointerdown', { button: 0, pointerId: 1, clientX: 500, clientY: 400 })
  469. element.fire('pointermove', { pointerId: 1, clientX: 560, clientY: 400 })
  470. assert.equal(control.getState().dragging, 'pan', '左键按下后应处于平移态')
  471. element.fire('pointerup', { pointerId: 1, clientX: 560, clientY: 400 })
  472. assert.equal(control.getState().dragging, null, '松手后应结束拖拽')
  473. element.fire('pointerdown', { button: 2, pointerId: 2, clientX: 500, clientY: 400 })
  474. element.fire('pointermove', { pointerId: 2, clientX: 540, clientY: 400 })
  475. assert.equal(control.getState().dragging, 'orbit', '右键按下后应处于轨道旋转态')
  476. element.fire('pointerup', { pointerId: 2, clientX: 540, clientY: 400 })
  477. assert.equal(element.fire('wheel', { deltaY: -120 }).defaultPrevented, true, '滚轮默认行为(页面滚动)必须被吃掉')
  478. detach()
  479. assert.equal(element.handlers.get('pointerdown').size, 0, 'detach 应摘掉监听器')
  480. assert.equal(element.handlers.get('wheel').size, 0, 'detach 应摘掉滚轮监听器')
  481. console.log('PASS 11 DOM 绑定:三类输入接上、右键菜单与滚轮默认行为被吃掉、可完整摘除')
  482. }
  483. // ---------- 12. 状态串(走查与人工排查读的那一行) ----------
  484. {
  485. const { control } = newControl()
  486. const text = camera.formatCameraState(control.getState())
  487. ;['h=', 'p=', 'r=', 'pivot=', 'focus=ground', 'drag=none', 'settled='].forEach((part) =>
  488. assert.ok(text.includes(part), `状态串应含 ${part}:${text}`),
  489. )
  490. assert.equal(
  491. camera.formatCameraState({ hpr: { heading: 0, pitch: -60, range: 1 }, pivot: null }),
  492. 'h=0.0 p=-60.0 r=1 pivot=none',
  493. )
  494. console.log(`PASS 12 状态串可读:${text}`)
  495. }
  496. console.log('\nT03 逻辑检查通过:三键契约、中心规则、换中心动画、阻尼(帧率无关)、DOM 绑定均符合契约与维护者裁定。')