Skip to content

Commit

Permalink
Moon lighting
Browse files Browse the repository at this point in the history
  • Loading branch information
ggetz committed May 22, 2024
1 parent 65a1951 commit 39485e0
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 23 deletions.
18 changes: 10 additions & 8 deletions Apps/Sandcastle/gallery/Moon.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,10 @@
Cesium.Ion.defaultAccessToken =
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJhZDUyNTc2YS1hMmQyLTQ2NGUtYmE2Ny01ZmQ5MmRmNGEyOGQiLCJpZCI6MTQsInNjb3BlcyI6WyJhc3IiLCJnYyJdLCJpYXQiOjE1ODgxODAxNDB9.q3KJVaTTtaMs404afZYo2RQSbYglE09Xfrm7BkZP694";

Cesium.Ellipsoid.default = Cesium.Ellipsoid.MOON;
const moonOptions = {
ellipsoid: Cesium.Ellipsoid.MOON,
terrainProvider: await Cesium.CesiumTerrainProvider.fromIonAssetId(
1238,
{
ellipsoid: Cesium.Ellipsoid.MOON,
}
1238
),
baseLayer: Cesium.ImageryLayer.fromProviderAsync(
Cesium.IonImageryProvider.fromAssetId(1208)
Expand All @@ -53,12 +50,17 @@
const viewer = new Cesium.Viewer("cesiumContainer", {
...moonOptions,
useBrowserRecommendedResolution: false,
timeline: false,
animation: false,
//timeline: false,
//animation: false,
baseLayerPicker: false,
geocoder: false,
});
viewer.scene.msaaSamples = 4;
viewer.clock.multiplier = 3 * 60 * 60 * 24;
viewer.clock.shouldAnimate = true;

const scene = viewer.scene;
scene.msaaSamples = 4;
scene.globe.enableLighting = true;

// Positions courtesy of https://www.sciencedirect.com/science/article/abs/pii/S0019103516301518?via%3Dihub
const pointsOfInterest = [
Expand Down
10 changes: 6 additions & 4 deletions packages/engine/Source/Core/Simon1994PlanetaryPositions.js
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,7 @@ const axesTransformation = new Matrix3(
0.9174820620691819
);
let translation = new Cartesian3();

/**
* Computes the position of the Sun in the Earth-centered inertial frame
*
Expand Down Expand Up @@ -662,7 +663,7 @@ Simon1994PlanetaryPositions.computeSunPositionInEarthInertialFrame = function (
/**
* Computes the position of the Moon in the Earth-centered inertial frame
*
* @param {JulianDate} [julianDate] The time at which to compute the Sun's position, if not provided the current system time is used.
* @param {JulianDate} [julianDate] The time at which to compute the Moon's position, if not provided the current system time is used.
* @param {Cartesian3} [result] The object onto which to store the result.
* @returns {Cartesian3} Calculated moon position
*/
Expand All @@ -681,11 +682,11 @@ Simon1994PlanetaryPositions.computeMoonPositionInEarthInertialFrame = function (
};

/**
* Computes the position of the Sun in the Moon-centered inertial frame
* Computes the position of the Sun in the Moon-centered fixed frame
*
* @param {JulianDate} [julianDate] The time at which to compute the Sun's position, if not provided the current system time is used.
* @param {Cartesian3} [result] The object onto which to store the result.
* @returns {Cartesian3} Calculated sun position
* @returns {Cartesian3} Calculated Sun position
*/
Simon1994PlanetaryPositions.computeSunPositionInMoonInertialFrame = function (
julianDate,
Expand All @@ -707,7 +708,8 @@ Simon1994PlanetaryPositions.computeSunPositionInMoonInertialFrame = function (
computeSimonMoon(julianDate, translation);

Cartesian3.subtract(result, translation, result);
Matrix3.multiplyByVector(axesTransformation, result, result);

//Matrix3.multiplyByVector(rotation, result, result); TODO: ?

return result;
};
Expand Down
71 changes: 71 additions & 0 deletions packages/engine/Source/Core/Transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,77 @@ Transforms.computeIcrfToFixedMatrix = function (date, result) {
return Matrix3.transpose(fixedToIcrfMtx, result);
};

const TdtMinusTai = 32.184;
const J2000d = 2451545;
const scratchHpr = new HeadingPitchRoll();
const scratchRotationMatrix = new Matrix3();
const dateScratch = new JulianDate();
Transforms.computeMoonFixedToIcrfMatrix = function (date, result) {
//>>includeStart('debug', pragmas.debug);
if (!defined(date)) {
throw new DeveloperError("date is required.");
}
//>>includeEnd('debug');

if (!defined(result)) {
result = new Matrix3();
}

// Converts TAI to TT
const secondsTT = JulianDate.addSeconds(date, TdtMinusTai, dateScratch);

// Converts TT to TDB, interval in days since the standard epoch
const d = JulianDate.totalDays(secondsTT) - J2000d;

// Compute the approximate rotation, using https://articles.adsabs.harvard.edu//full/1980CeMec..22..205D/0000209.000.html
const e1 = CesiumMath.toRadians(12.112) - CesiumMath.toRadians(0.052992) * d;
const e2 = CesiumMath.toRadians(24.224) - CesiumMath.toRadians(0.105984) * d;
const e3 = CesiumMath.toRadians(227.645) + CesiumMath.toRadians(13.012) * d;
const e4 =
CesiumMath.toRadians(261.105) + CesiumMath.toRadians(13.340716) * d;
const e5 = CesiumMath.toRadians(358.0) + CesiumMath.toRadians(0.9856) * d;

scratchHpr.heading =
CesiumMath.toRadians(270.0) -
CesiumMath.toRadians(3.878) * Math.sin(e1) -
CesiumMath.toRadians(0.12) * Math.sin(e2) +
CesiumMath.toRadians(0.07) * Math.sin(e3) -
CesiumMath.toRadians(0.017) * Math.sin(e4);
scratchHpr.pitch =
CesiumMath.toRadians(66.534) +
CesiumMath.toRadians(1.543) * Math.cos(e1) +
CesiumMath.toRadians(0.24) * Math.cos(e2) -
CesiumMath.toRadians(0.028) * Math.cos(e3) +
CesiumMath.toRadians(0.007) * Math.cos(e4);
scratchHpr.roll =
CesiumMath.toRadians(244.375) +
CesiumMath.toRadians(13.17635831) * d +
CesiumMath.toRadians(3.558) * Math.sin(e1) +
CesiumMath.toRadians(0.121) * Math.sin(e2) -
CesiumMath.toRadians(0.064) * Math.sin(e3) +
CesiumMath.toRadians(0.016) * Math.sin(e4) +
CesiumMath.toRadians(0.025) * Math.sin(e5);
return Matrix3.fromHeadingPitchRoll(scratchHpr, scratchRotationMatrix);
};

Transforms.computeIcrfToMoonFixedMatrix = function (date, result) {
//>>includeStart('debug', pragmas.debug);
if (!defined(date)) {
throw new DeveloperError("date is required.");
}
//>>includeEnd('debug');
if (!defined(result)) {
result = new Matrix3();
}

const fixedToIcrfMtx = Transforms.computeMoonFixedToIcrfMatrix(date, result);
if (!defined(fixedToIcrfMtx)) {
return undefined;
}

return Matrix3.transpose(fixedToIcrfMtx, result);
};

const xysScratch = new Iau2006XysSample(0.0, 0.0, 0.0);
const eopScratch = new EarthOrientationParametersSample(
0.0,
Expand Down
24 changes: 13 additions & 11 deletions packages/engine/Source/Renderer/UniformState.js
Original file line number Diff line number Diff line change
Expand Up @@ -1305,19 +1305,21 @@ function setCamera(uniformState, camera) {
);
}

let transformMatrix = new Matrix3();
const transformMatrix = new Matrix3();
const sunCartographicScratch = new Cartographic();
function setSunAndMoonDirections(uniformState, frameState) {
if (
!defined(
Transforms.computeIcrfToFixedMatrix(frameState.time, transformMatrix)
)
) {
transformMatrix = Transforms.computeTemeToPseudoFixedMatrix(
frameState.time,
transformMatrix
);
}
// if (
// !defined(
// Transforms.computeIcrfToFixedMatrix(frameState.time, transformMatrix)
// )
// ) {
// transformMatrix = Transforms.computeTemeToPseudoFixedMatrix(
// frameState.time,
// transformMatrix
// );
// }

Transforms.computeIcrfToMoonFixedMatrix(frameState.time, transformMatrix);

let position = Simon1994PlanetaryPositions.computeSunPositionInMoonInertialFrame(
frameState.time,
Expand Down

0 comments on commit 39485e0

Please sign in to comment.