Turn off skyAtmosphere after sunset?

I’m using Cesium for out-the-window visualization in a flight simulator. I’d like to hide/destroy the skyAtmosphere at night.

Any suggestions on how to determine if the sun has set?

Thanks, Matt.

We actually had some code laying around to do something close to this. Here’s a Sandcastle example that uses the camera position to determine whether the sun is visible at that location and turns on/off atmosphere. Just replace viewer.scene.camera.position with whatever position you want. Eventually, we’ll probably have some cool built in visualization for this in Cesium itself.

var viewer = new Cesium.Viewer(‘cesiumContainer’);

var transformMatrix = new Cesium.Matrix4();

var toSun = new Cesium.Cartesian3();

var toMoon = new Cesium.Cartesian3();

viewer.clock.onTick.addEventListener(function(clock) {

var position = viewer.scene.camera.position;

var up = viewer.scene.globe.ellipsoid.geodeticSurfaceNormal(position);

var time = clock.currentTime;

Cesium.Transforms.computeTemeToPseudoFixedMatrix(time, transformMatrix);

Cesium.Simon1994PlanetaryPositions.computeSunPositionInEarthInertialFrame(time, toSun);

Cesium.Matrix3.multiplyByVector(transformMatrix, toSun, toSun);

Cesium.Cartesian3.normalize(toSun, toSun);

viewer.scene.skyAtmosphere.show = Cesium.Cartesian3.dot(up, toSun) > 0.0;



I also found a nice little library for calculating the sun position:


my code:

function updateSkyAtmosphere() {

var dateNow = Cesium.JulianDate.toDate(viewer.clock.currentTime);

var sunPos = SunCalc.getPosition(dateNow, acState.lat, acState.lng);

viewer.scene.skyAtmosphere.show = sunPos.altitude > 0;