Bläddra i källkod

fix(cockpit): 恢复进场时的地球放大动画

将起雾时机从点击改为接近主场景,并为入场单独使用倍率缩放;继续等待精细地图连续实绘后再退雾。
ChenJZ 4 timmar sedan
förälder
incheckning
d8cf9ee182

+ 9 - 0
src/modules/cockpit/domain/camera.js

@@ -117,6 +117,15 @@ export function returnFlightProgress(elapsedMs, durationMs = RETURN_FLIGHT_MS) {
   return clamp(eased * (1 - RETURN_ENDPOINT_BLEND) + t * RETURN_ENDPOINT_BLEND, 0, 1)
 }
 
+/** 入场按视距倍率推进,和保留低空停留的返回曲线分开。 */
+export const ENTRY_FLIGHT_MS = 1150
+
+export function entryFlightProgress(elapsedMs, durationMs = ENTRY_FLIGHT_MS) {
+    if (!(durationMs > 0)) return 1
+    const t = clamp(elapsedMs / durationMs, 0, 1)
+    return t * t * (3 - 2 * t)
+}
+
 /** 弹簧臂参数整体插值(heading 走最短弧);整段视角飞行用。 */
 export function lerpHpr(from, to, t) {
     const delta = shortestAngleDelta(from.heading, to.heading)

+ 13 - 4
src/modules/cockpit/services/cameraControl.js

@@ -36,6 +36,7 @@ import {
     clamp,
     clampPitch,
     dampFactor,
+    entryFlightProgress,
     flightProgress,
     isSettled,
     lerpHpr,
@@ -264,16 +265,23 @@ export function createCameraControl({
     }
 
     /**
-     * 整段视角飞行的逐帧推进(返回全球)。到点落位并结束。
+     * 整段视角飞行的逐帧推进(进入主场景 / 返回全球)。到点落位并结束。
      * @returns {boolean} 本次是否由它接管了 `shown`
      */
     function advanceViewFlight(now) {
         const flight = state.viewFlight
         if (!flight) return false
-        const t = returnFlightProgress(now - flight.startedAt, flight.durationMs)
+        const { from, to, startedAt, durationMs, entering } = flight
+        const elapsed = now - startedAt
+        const t = entering ? entryFlightProgress(elapsed, durationMs) : returnFlightProgress(elapsed, durationMs)
+        const hpr = lerpHpr(from.hpr, to.hpr, t)
+        if (entering && t < 1 && from.hpr.range > 0 && to.hpr.range > 0) {
+            hpr.range = from.hpr.range * (to.hpr.range / from.hpr.range) ** t
+        }
         state.shown = {
-            pivot: lerpPivot(flight.from.pivot, flight.to.pivot, t),
-            hpr: lerpHpr(flight.from.hpr, flight.to.hpr, t),
+            // 入场先把视线引向城市,再进入近处;返回全球维持既有轨迹。
+            pivot: lerpPivot(from.pivot, to.pivot, entering ? 1 - (1 - t) ** 4 : t),
+            hpr,
         }
         if (t >= 1) state.viewFlight = null
         return true
@@ -592,6 +600,7 @@ export function createCameraControl({
                 to: { pivot: { ...next.pivot }, hpr: { ...next.hpr } },
                 startedAt: engine.now(),
                 durationMs: duration,
+                entering: options.flightCurve === 'entry',
             }
             schedule()
             return true

+ 9 - 0
src/modules/cockpit/services/entryMapTransition.js

@@ -1,3 +1,12 @@
+// 地球/洲际缩放保持可见;到区域尺度才起雾,避免暴露近景尚未到齐的瓦片。
+const ENTRY_FOG_RANGE_M = 350000
+export const ENTRY_FOG_COVER_MS = 260 // 给地图宿主 180ms 淡入及一帧余量。
+
+export function entryFogStartedAt(startedAt, { range, settled }, now) {
+    if (startedAt !== null) return startedAt
+    return settled || !(range > ENTRY_FOG_RANGE_M) ? now : null
+}
+
 /** 只在落地视角的 postRender 里采样;写了 alpha 不等于这一帧画过精细层。 */
 export function stepEntryMap(entry, { readyToPaint, detailDrawn }) {
     if (!readyToPaint) return { ...entry, readyFrames: 0 }

+ 33 - 17
src/modules/cockpit/services/mapService.js

@@ -24,7 +24,7 @@ import { getCockpitConfig, loadCockpitConfig } from './cockpitConfig.js'
 import { attachCameraInput, createCameraControl } from './cameraControl.js'
 import { attachPointPick } from './pointPick.js'
 import { attachSceneFocus } from './sceneInputFocus.js'
-import { formatCameraState, INITIAL_PANORAMA, RETURN_FLIGHT_MS, SETTLE_EPSILON } from '../domain/camera.js'
+import { ENTRY_FLIGHT_MS, formatCameraState, INITIAL_PANORAMA, RETURN_FLIGHT_MS, SETTLE_EPSILON } from '../domain/camera.js'
 import { BASEMAP_STATE, basemapStatusText, planBasemap } from '../domain/basemap.js'
 import { isAtHome, planHome, planIntro } from '../domain/viewPresets.js'
 import { isStaleLocate, locateTierFor, planLocateView, planTerrainLocateTarget, TERRAIN_LOCATE_REASON } from '../domain/pointLocate.js'
@@ -104,7 +104,7 @@ import {
     stepHandover,
 } from '../domain/imageryHandover.js'
 import { planTileWarmup, runTileWarmup } from '../domain/tileWarmup.js'
-import { stepEntryMap } from './entryMapTransition.js'
+import { ENTRY_FOG_COVER_MS, entryFogStartedAt, stepEntryMap } from './entryMapTransition.js'
 
 const ENGINE_TEXT = {
     unavailable: 'Cesium 引擎未启动(无宿主要素)',
@@ -1716,14 +1716,15 @@ export function goHome(options = {}) {
     // **先换限位再飞**:否则飞行目标 12,000 m 会被"区外"的宽上限放行、落地后才被夹,
     // 画面会在到位瞬间跳一下。
     // 在区域状态通知与相机起飞之前先立起入场保护,避免同步回调提前放行。
-    const covered = fromGlobal && primaryAreaPlan.enabled && handoverEnabled()
+    const protectedEntry = fromGlobal && primaryAreaPlan.enabled && handoverEnabled()
         && overlayHandles.length > 0 && !handoverPrototypeActive
-    if (covered) beginEntryMap(record)
+    if (protectedEntry) beginEntryMap(record, options.immediate)
     setAreaState('enter')
     const home = getHomeView()
-    // 缩短雾内航程,尽快让 Cesium 开始处理落地视野,而不是加载沿途低精图。
-    const applied = applyViewPreset(home, home, covered ? { flightMs: 550, ...options } : options)
-    if (covered && !applied) clearEntryMap(record)
+    // 先让地球放大可见,再在近景遮挡中完成落地渲染。
+    const flight = protectedEntry ? { flightMs: ENTRY_FLIGHT_MS, flightCurve: 'entry', ...options } : options
+    const applied = applyViewPreset(home, home, flight)
+    if (protectedEntry && !applied) clearEntryMap(record)
     return applied
 }
 
@@ -2029,7 +2030,10 @@ export function setAreaState(action) {
     const handoverWillHandleFogDrop = areaState !== AREA_STATE.LOCKED && handoverHandlesFogDrop()
 
     if (action === 'returnGlobal') {
+        const entry = instance?.entryMap
         if (instance) clearEntryMap(instance)
+        // 取消尚未显露的视距雾,避免回全球时反而把它打开。
+        if (entry && !entry.coverReady) settleFog(FOG_PHASE.GLOBAL)
         // 先让交接进入"收起"(立刻排帧),雾暂不动。
         // `force=true`:进出是离散动作,必须**这一次调用就定下**目标相位,
         // 不能被稳态节流吞掉(否则按下返回后要等最多 120 ms 才开始收影像)。
@@ -2209,6 +2213,8 @@ function cameraAltitude() {
  * 画面上的雾都至少花 `altitudeSlewMs` 变化。
  */
 function fogEffectiveStrength() {
+    // 视距雾也不能把地球到区域的可见航段提前蒙住。
+    if (instance?.entryMap && !instance.entryMap.coverReady) return 0
     const base = Math.min(fogLifecycle.strength, fogDegrade.strengthCap)
     return Math.max(0, base * fogAltitudeShown)
 }
@@ -2876,23 +2882,25 @@ function isHardRevealBlock(block) {
         || block === HANDOVER_BLOCK.EDGE_RISK
 }
 
-const ENTRY_FOG_COVER_MS = 260 // 留足 MapCanvas 入场雾的淡入帧,再允许在雾后切换影像。
 const ENTRY_SLOW_MS = 10000
 
 export function isEntryMapPending() {
     return !!instance?.entryMap && !instance.entryMap.revealed
 }
 
-function beginEntryMap(record) {
+function beginEntryMap(record, immediate) {
     record.cancelTileWarmup?.()
     record.cancelTileWarmup = null
+    const since = engineNow()
+    const range = immediate ? null : record.cameraControl.getState().shownHpr.range
+    const coverStartedAt = entryFogStartedAt(null, { range }, since)
     record.entryMap = {
-        since: engineNow(), paintDetail: false, readyFrames: 0, revealed: false,
+        since, coverStartedAt, coverReady: false, paintDetail: false, readyFrames: 0, revealed: false,
         layers: new Set(overlayHandles.map((handle) => handle.layer)),
     }
-    record.host.dataset.mapEntryFog = 'covered'
+    record.host.dataset.mapEntryFog = coverStartedAt === null ? 'approach' : 'covered'
     record.entrySlowTimer = setTimeout(() => {
-        if (record !== instance || !isEntryMapPending()) return
+        if (record !== instance || !isEntryMapPending() || record.entryMap.coverStartedAt === null) return
         record.host.dataset.mapEntryFog = 'slow'
         record.viewer.scene.requestRender()
     }, ENTRY_SLOW_MS)
@@ -2913,15 +2921,23 @@ function advanceEntryMap(record) {
     const entry = record.entryMap
     if (!entry || entry.revealed || areaState !== AREA_STATE.LOCKED) return
     const { scene } = record.viewer
-    const elapsed = engineNow() - entry.since
-    const settled = record.cameraControl?.getState().settled === true
+    const now = engineNow()
+    const elapsed = now - entry.since
+    const camera = record.cameraControl.getState()
+    const settled = camera.settled === true
+    const coverStartedAt = entryFogStartedAt(entry.coverStartedAt, { range: camera.shownHpr.range, settled }, now)
+    if (entry.coverStartedAt === null && coverStartedAt !== null) {
+        record.host.dataset.mapEntryFog = elapsed >= ENTRY_SLOW_MS ? 'slow' : 'covered'
+    }
+    const coverReady = coverStartedAt !== null && now - coverStartedAt >= ENTRY_FOG_COVER_MS
     const tilesReady = scene.globe?.tilesLoaded === true
     const gate = revealGate({ areaState, entryCovered: true, tilesLoaded: tilesReady })
-    const readyToPaint = elapsed >= ENTRY_FOG_COVER_MS && settled && gate.allowed
+    const readyToPaint = coverReady && settled && gate.allowed
     const detailDrawn = readyToPaint && entry.paintDetail && record.engine.hasRenderedEntryImagery(record.viewer, entry.layers)
-    const next = stepEntryMap(entry, { readyToPaint, detailDrawn })
-    next.block = detailDrawn ? null : !settled ? 'camera' : !tilesReady ? 'tiles' : 'textures'
+    const next = stepEntryMap({ ...entry, coverStartedAt, coverReady }, { readyToPaint, detailDrawn })
+    next.block = detailDrawn ? null : !settled ? 'camera' : !coverReady ? 'cover' : !tilesReady ? 'tiles' : 'textures'
     record.entryMap = next
+    if (coverReady && !entry.coverReady) paintFog()
 
     if (!entry.paintDetail && next.paintDetail) {
         // 雾已经盖住画面:精细层立即全亮,给后续真实帧确认,不做两遍淡入。

+ 47 - 1
tests/entryMapTransition.test.js

@@ -1,6 +1,52 @@
 import { test } from 'node:test'
 import assert from 'node:assert/strict'
-import { hasEntryImageryTextures, stepEntryMap } from '../src/modules/cockpit/services/entryMapTransition.js'
+import { ENTRY_FLIGHT_MS } from '../src/modules/cockpit/domain/camera.js'
+import { createCameraControl } from '../src/modules/cockpit/services/cameraControl.js'
+import { ENTRY_FOG_COVER_MS, entryFogStartedAt, hasEntryImageryTextures, stepEntryMap } from '../src/modules/cockpit/services/entryMapTransition.js'
+
+test('入场镜头在前半程就明显放大地球,不沿用返回全球的慢起曲线', () => {
+    const from = { lon: 135, lat: 45, height: 0 }
+    const home = { lon: 110.39, lat: 28.44, height: 0 }
+    const homeHpr = { heading: 0, pitch: -55, range: 12000 }
+    let now = 0
+    let queued
+    const camera = createCameraControl({ engine: {
+        now: () => now,
+        requestFrame: (callback) => { queued = callback },
+        pickGroundAtCenter: () => from,
+        readView: () => ({ heading: 0, pitch: -60, range: 30e6 }),
+        applyView() {},
+    } })
+    camera.syncFromEngine()
+    camera.applyViewPreset(home, homeHpr, { flightMs: ENTRY_FLIGHT_MS, flightCurve: 'entry' })
+    function at(ms) {
+        now = ms
+        queued(now)
+        return camera.getState()
+    }
+    const mid = at(ENTRY_FLIGHT_MS / 2)
+    assert.ok(mid.shownHpr.range < 1e6, `半程仍离地 ${mid.shownHpr.range} m,看不到明显放大`)
+    assert.equal(mid.settled, false)
+    assert.equal(entryFogStartedAt(null, { range: mid.shownHpr.range, settled: mid.settled }, now), null, '半程仍能看到地球')
+
+    const near = at(ENTRY_FLIGHT_MS * 0.64)
+    assert.ok(near.shownHpr.range < mid.shownHpr.range)
+    assert.ok(near.shownHpr.range > homeHpr.range)
+    const fogStartedAt = entryFogStartedAt(null, { range: near.shownHpr.range, settled: near.settled }, now)
+    assert.equal(fogStartedAt, now)
+    assert.ok(fogStartedAt + ENTRY_FOG_COVER_MS < ENTRY_FLIGHT_MS, '镜头到县城前应有足够时间形成遮挡')
+
+    const landed = at(ENTRY_FLIGHT_MS)
+    assert.equal(landed.shownHpr.range, homeHpr.range)
+    assert.equal(landed.settled, true)
+    assert.equal(entryFogStartedAt(fogStartedAt, { range: landed.shownHpr.range, settled: true }, now), fogStartedAt)
+})
+
+test('已在近景或提前结束飞行时直接建立遮挡,随后不重置淡入时钟', () => {
+    assert.equal(entryFogStartedAt(null, { range: 12000, settled: false }, 5), 5)
+    assert.equal(entryFogStartedAt(null, { range: 30e6, settled: true }, 10), 10)
+    assert.equal(entryFogStartedAt(0, { range: 30e6, settled: false }, 10), 0)
+})
 
 const initial = { paintDetail: false, readyFrames: 0, revealed: false }
 const layer = { show: true, alpha: 1 }