let viewer = undefined
function icrf() {
if (!viewer || viewer.scene.mode !== Cesium.SceneMode.SCENE3D) {
return;
}
const icrfToFixed = Cesium.Transforms.computeIcrfToFixedMatrix(
viewer.clock.currentTime
);
if (Cesium.defined(icrfToFixed)) {
const camera = viewer.camera;
const offset = Cesium.Cartesian3.clone(camera.position);
const transform = Cesium.Matrix4.fromRotationTranslation(icrfToFixed);
camera.lookAtTransform(transform, offset);
}
}
//option可以设置时钟的一些参数,比如自转开始的时间,自转的速度等
const start = function (cesiumViewer,option={multiplier:1}) {
viewer = cesiumViewer
viewer.scene.postUpdate.addEventListener(icrf);
if(viewer.clock){
const keys=Object.keys(option)
for(let k of keys){
viewer.clock[k]=option[k]
}
}
}
const stop = function () {
if(!viewer){
return
}
viewer.clock.multiplier=1
viewer.scene.postUpdate.removeEventListener(icrf);
viewer = undefined
}
//开始自转
const viewer=new Cesium.Viewer('map')
//为了能明显的看到,将时针速度设为10倍
start(viewer,{multiplier:10})
//停止自转
stop()