Cesium模拟火星昼夜阴影变化

问题:让光源绕球体旋转即可,关键在于计算实时的光源位置,光源位置的变化由时间决定。

 Cesium中,可以通过viewer.scene.light来创建点光源,设置其位置(x,y,z)及光强。

       viewer.scene.light = new Cesium.DirectionalLight({
            color:new Cesium.Color (1.0, 1.0, 1.0, 0.5), 
            direction:direction,
        })

1. 背景知识

火星基本参数

参数

参数

直径/km

6779

自转周期

24小时37分22秒

24.62278 h

距离太阳平均距离/km

2.29亿

倾角

25.19度

公转/

687

自转方向

自西向东

 地球基本参数

参数

参数

直径/km

12742

自转周期

23小时56分4秒

23.93444 h

距离太阳平均距离/km

1.5 亿

倾角

23.44度

公转/

365

自转方向

自西向东

2. 原理

关键在于计算太阳直射点的经纬度的变化。

eq?Latitude%20%3D%20tilt%20*%20sinkt

%28687*24*3600%29

%20246.2

其中,tilt为球体倾角,k为每秒的公转的角度,687对应公转周期,t为距离上一次直射(0°,0°)(类似地球的春分点)的时差,246.2对应每自转1度需要的秒数,

太阳直射点纬度变化图如下所示:

25f65b6410c746a1bff6a41f7e26c2fc.png

 然而,改变光源位置,需要让光源反向于球体自转方向旋转。

        longitude = 0 - difference / 246.6
        latitude = 0 - 25.19 * Math.sin(6.28/(687*24*3600) * difference)

3. 核心代码

    viewerRef.current = new Cesium.Viewer('cesiumContainer',{
        animation: true, 
        shouldAnimate:true,
        baseLayerPicker: true, 
        fullscreenButton: false, 
        vrButton: false, 
        geocoder: false,
        homeButton: true, 
        infoBox: true, 
        sceneModePicker: false,
        selectionIndicator: true, 
        timeline: false, 
        navigationHelpButton: false, 
        navigationInstructionsInitiallyVisible: false,
    });
    var viewer = viewerRef.current
    viewer._cesiumWidget._creditContainer.style.display = "none";
    viewer.scene.moon.show = false;
    viewer.scene.fog.enabled = false;
    viewer.scene.sun.show = false;
    viewer.scene.skyBox.show = false;
    viewer.imageryLayers.removeAll();
    viewer.scene.globe.enableLighting = true;// 开启全球光照
    viewer.shadows = true
    var longitude
    var latitude
    // 火星的春分点
    var startTime = '2022-12-26T00:00:00Z'; 
    startTime = Cesium.JulianDate.fromIso8601(startTime);
    var direction
    //实时计算新的太阳直射点,difference为与春分时间的时差
    viewer.clock.onTick.addEventListener(function(clock) {
        var currentTime = clock.currentTime;
        var difference = Cesium.JulianDate.secondsDifference(currentTime, startTime);
        longitude = 0 - difference / 246.6
        latitude = 0 - 25.19 * Math.sin(6.28/(687*24*3600) * difference)
        direction = new Cesium.Cartesian3.fromDegrees(longitude,latitude,2.29e11)
        viewer.scene.light = new Cesium.DirectionalLight({
            color:new Cesium.Color (1.0, 1.0, 1.0, 0.5), 
            direction:direction,
        })
    });
)
    viewer.camera.setView({
        destination:Cesium.Cartesian3.fromDegrees(120,0,20000000.0),   // 经纬度和高度
    });
    // 添加火星图层
    var marsUrl = 'https://planetarymaps.usgs.gov/cgi-bin/mapserv?map=/maps/mars/mars_simp_cyl.map&service=WMS';
    const marsLayer = new Cesium.ImageryLayer(
        new Cesium.WebMapServiceImageryProvider({
            url: marsUrl,
            layers:'MDIM21_color',
        }),
    );
    marsLayer.name = 'Mars'
    viewer.imageryLayers.add(marsLayer);

4. 实现效果

对比地球时间的同一时刻Cesium自带的地球昼夜阴影和本文实现的火星昼夜阴影,用同一个图层,便于观察阴影的差异,如下图:

5daa647bcd0a457c8b745b8b281afcb9.png38d67fe537a64c12b8adc3dc06606447.png

地球昼夜阴影(左图)与火星昼夜阴影(右图)

演示视频:

火星昼夜变化-阴影

 

 

  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wfq0007

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值