恢复北朝上 roll 计算(关闭后确实又转又倾斜)

This commit is contained in:
2026-09-19 15:50:04 +08:00
parent 60988c5df8
commit 57747e38c7
+28 -2
View File
@@ -482,10 +482,36 @@ export class CesiumGlobe {
const heading = Math.atan2(sc.localDir.x, sc.localDir.y); const heading = Math.atan2(sc.localDir.x, sc.localDir.y);
const pitch = Math.asin(sc.localDir.z); const pitch = Math.asin(sc.localDir.z);
// 关闭北朝上限制:roll 固定 0(up 随倾斜自然转动) // 计算“北朝上”所需的 roll:把 roll=0 的 up(水平线方向)滚转到“世界 Z 投影”的 up,
// 这样倾斜时 up 不随方向转动(否则会又倾斜又旋转)
const dz = dir.z;
sc.un.x = -dir.x * dz;
sc.un.y = -dir.y * dz;
sc.un.z = 1 - dz * dz;
if (Cesium.Cartesian3.magnitudeSquared(sc.un) < 1e-12) {
Cesium.Cartesian3.clone(s.up, sc.un);
} else {
Cesium.Cartesian3.normalize(sc.un, sc.un);
}
Cesium.Cartesian3.normalize(sc.newPos, sc.enuUp); // 径向 = newPos 处的“上”
const d0 = Cesium.Cartesian3.dot(sc.enuUp, dir);
sc.u0.x = sc.enuUp.x - d0 * dir.x;
sc.u0.y = sc.enuUp.y - d0 * dir.y;
sc.u0.z = sc.enuUp.z - d0 * dir.z;
let roll = 0;
if (Cesium.Cartesian3.magnitudeSquared(sc.u0) > 1e-12) {
Cesium.Cartesian3.normalize(sc.u0, sc.u0);
Cesium.Cartesian3.cross(sc.u0, sc.un, sc.cross);
const sin =
Cesium.Cartesian3.magnitude(sc.cross) *
Math.sign(Cesium.Cartesian3.dot(sc.cross, dir));
const cos = Cesium.Cartesian3.dot(sc.u0, sc.un);
roll = Math.atan2(sin, cos);
}
camera.setView({ camera.setView({
destination: Cesium.Cartesian3.clone(sc.newPos), destination: Cesium.Cartesian3.clone(sc.newPos),
orientation: { heading, pitch, roll: 0 }, orientation: { heading, pitch, roll },
}); });
} }