Skip to content

Commit

Permalink
Merge pull request #16 from Carifio24/eclipse-fixups
Browse files Browse the repository at this point in the history
Fix some solar eclipse issues
  • Loading branch information
patudom authored Jan 29, 2024
2 parents 407d71e + d8e802d commit 55351d0
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 16 deletions.
54 changes: 39 additions & 15 deletions src/SolarEclipse2024.vue
Original file line number Diff line number Diff line change
Expand Up @@ -1195,12 +1195,12 @@ import { getTimezoneOffset, formatInTimeZone } from "date-fns-tz";
import tzlookup from "tz-lookup";
import { v4 } from "uuid";
import { drawSkyOverlays, makeAltAzGridText, layerManagerDraw, updateViewParameters, renderOneFrame } from "./wwt-hacks";
import { drawPlanets, drawSkyOverlays, makeAltAzGridText, layerManagerDraw, updateViewParameters, renderOneFrame } from "./wwt-hacks";
type SheetType = "text" | "video" | null;
type LearnerPath = "Location" | "Clouds" | "Learn";
type ViewerMode = "Horizon" | "SunScope";
type MoonImageFile = "moon.png" | "moon-dark-gray-overlay.png" | "moon-sky-blue-overlay.png";
type MoonImageFile = "moon.png" | "moon-dark-gray-overlay.png" | `moon-sky-blue-overlay-${number}.png` | "empty.png";
const D2R = Math.PI / 180;
const R2D = 180 / Math.PI;
Expand Down Expand Up @@ -1598,7 +1598,7 @@ export default defineComponent({
}
} as Record<string, EclipseLocation>,
currentPercentEclipsed: 0,
currentFractionEclipsed: 0,
places: [] as (LocationRad & { name: string })[],
Expand Down Expand Up @@ -1763,6 +1763,12 @@ export default defineComponent({
// @ts-ignore
this.wwtControl.renderFrameCallback = this.onWWTRenderFrame;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
Planets.drawPlanets = (renderContext: RenderContext, opacity: number) => {
drawPlanets(renderContext, opacity, this.currentFractionEclipsed);
};
/* eslint-disable @typescript-eslint/no-var-requires */
Planets['_planetTextures'][0] = Texture.fromUrl(require("./assets/2023-09-19-SDO-Sun.png"));
this.setForegroundImageByName("Digitized Sky Survey (Color)");
Expand Down Expand Up @@ -2010,7 +2016,10 @@ export default defineComponent({
},
percentEclipsedText(): string {
const percentEclipsed = Math.abs(this.currentPercentEclipsed * 100).toFixed(0);
let percentEclipsed = Math.abs(this.currentFractionEclipsed * 100).toFixed(0);
if (this.currentFractionEclipsed < 1 && percentEclipsed === "100") {
percentEclipsed = "99";
}
return `Eclipsed: ${percentEclipsed}%`;
},
Expand Down Expand Up @@ -2144,7 +2153,7 @@ export default defineComponent({
// If there's no sun/moon intersection, no need to continue
if (sunMoonDistance > rMoonPx + rSunPx) {
this.currentPercentEclipsed = 0;
this.currentFractionEclipsed = 0;
return;
}
Expand All @@ -2157,8 +2166,9 @@ export default defineComponent({
const moonArea = Math.PI * rMoonSq;
const sunArea = Math.PI * rSunSq;
let fractionEclipsed = 0;
if (moonInsideSun || sunInsideMoon) {
this.currentPercentEclipsed = moonArea / sunArea;
fractionEclipsed = moonArea / sunArea;
} else {
// See https://mathworld.wolfram.com/Circle-CircleIntersection.html
const intersectionArea =
Expand All @@ -2167,9 +2177,9 @@ export default defineComponent({
0.5 * Math.sqrt(
(rSunPx + rMoonPx - sunMoonDistance) * (sunMoonDistance + rMoonPx - rSunPx) * (sunMoonDistance - rMoonPx + rSunPx) * (sunMoonDistance + rSunPx + rMoonPx)
);
const percentEclipsed = intersectionArea / sunArea;
this.currentPercentEclipsed = isNaN(percentEclipsed) ? 1 : percentEclipsed;
fractionEclipsed = intersectionArea / sunArea;
}
this.currentFractionEclipsed = isNaN(fractionEclipsed) ? 1 : Math.max(Math.min(fractionEclipsed, 1), 0);
// If we're using the regular WWT moon, or in sun scope mode, we don't want the overlay but did want the percentage eclipsed
if (this.useRegularMoon || this.viewerMode === "SunScope") {
Expand All @@ -2193,14 +2203,16 @@ export default defineComponent({
if (sunPoint.x === 0) {
const ysh = 0.5 * sunPoint.y;
let ysh = 0.5 * sunPoint.y;
if (ysh >= rMoonPx) {
return;
} else if (ysh === 0) {
ysh = Math.min(rMoonPx, rSunPx);
}
x1 = Math.sqrt(rMoonPx * rMoonPx - ysh * ysh);
if (isNaN(x1)) {
console.error("x1 is NaN");
this.currentPercentEclipsed = 0;
this.currentFractionEclipsed = 0;
return;
}
y1 = ysh;
Expand All @@ -2223,7 +2235,7 @@ export default defineComponent({
const sqrDisc = Math.sqrt(b * b - 4 * a * c);
if (isNaN(sqrDisc)) {
console.error("sqrDisc is NaN");
this.currentPercentEclipsed = 0;
this.currentFractionEclipsed = 0;
return;
}
x1 = (-b + sqrDisc) / (2 * a);
Expand Down Expand Up @@ -2255,7 +2267,6 @@ export default defineComponent({
points.push({ x: rMoonPx * Math.cos(angle), y: rMoonPx * Math.sin(angle) });
}
// We now need to somewhat repeat this analysis in the Sun frame
let thetaS1 = Math.atan2((y1 - sunPoint.y) / rSunPx, (x1 - sunPoint.x) / rSunPx);
Expand All @@ -2277,6 +2288,7 @@ export default defineComponent({
const angle = thetaS1 + (i / n) * rangeSizeS;
points.push({ x: rSunPx * Math.cos(angle) + sunPoint.x, y: rSunPx * Math.sin(angle) + sunPoint.y });
}
}
// We made a translation into the moon's frame, so undo that
Expand All @@ -2285,6 +2297,8 @@ export default defineComponent({
points[i].y += moonPoint.y;
}
this.updateMoonTexture();
const centroidX = points.reduce((s, p) => s + p.x, 0) / points.length;
const centroidY = points.reduce((s, p) => s + p.y, 0) / points.length;
Expand Down Expand Up @@ -2320,7 +2334,13 @@ export default defineComponent({
const blueMoon = (this.showHorizon && this.showSky) &&
this.moonPosition.altRad > 0 &&
this.viewerMode !== 'SunScope';
filename = blueMoon ? 'moon-sky-blue-overlay.png' : 'moon-dark-gray-overlay.png';
if (!blueMoon) {
filename = "moon-dark-gray-overlay.png";
} else {
const skyOpacity = Math.max(Math.min(this.skyOpacity, 1), 0);
const opacityToUse = Math.round(skyOpacity * 2) * 50;
filename = `moon-sky-blue-overlay-${opacityToUse}.png`;
}
}
if (force || (filename !== this.moonTexture && Planets._planetTextures)) {
Planets._planetTextures[9] = this.textureFromAssetImage(filename);
Expand Down Expand Up @@ -2835,7 +2855,7 @@ export default defineComponent({
const sunAlt = altRad;
this.skyOpacity = (1 + Math.atan(Math.PI * sunAlt / (-astronomicalTwilight))) / 2;
this.skyOpacity = this.skyOpacity * (1 - 0.75 * Math.pow(Math.E,-Math.pow((this.currentPercentEclipsed -1),2)/(0.09)));
this.skyOpacity = this.skyOpacity * (1 - 0.75 * Math.pow(Math.E,-Math.pow((this.currentFractionEclipsed -1),2)/(0.09)));
this.updateMoonTexture();
const dssOpacity = sunAlt > 0 ? 0 : 1 - (1 + Math.atan(Math.PI * sunAlt / (-astronomicalTwilight))) / 2;
Expand Down Expand Up @@ -2968,6 +2988,10 @@ export default defineComponent({
}
},
wwtZoomDeg(_zoom: number) {
this.updateIntersection();
},
useRegularMoon(_show: boolean) {
this.updateMoonTexture();
this.updateFrontAnnotations(this.dateTime);
Expand Down Expand Up @@ -3078,7 +3102,7 @@ export default defineComponent({
return;
},
currentPercentEclipsed(_frac: number) {
currentFractionEclipsed(_frac: number) {
// this.skyOpacity = 1 - frac;
this.updateFrontAnnotations();
},
Expand Down
Binary file added src/assets/empty.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
Binary file added src/assets/moon-sky-blue-overlay-50.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions src/shims-wwt.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ declare module "@wwtelescope/engine" {
// Technically this is a list of AstroRaDec objects, but this is a good enough definition
// eslint-disable-next-line @typescript-eslint/naming-convention
static _planetLocations: { RA: number; dec: number }[];

static drawPlanets(renderContext: RenderContext, opacity: number): void;
}

export class CAAMoon {
Expand Down
36 changes: 35 additions & 1 deletion src/wwt-hacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Annotation2 } from "./Annotation2";

import {
Annotation, Color, Colors, Constellations, Coordinates, Grids,
LayerManager, Planets, PushPin, RenderTriangle, Settings, SpaceTimeController,
LayerManager, Planets, PushPin, RenderContext, RenderTriangle, Settings, SpaceTimeController,
SpreadSheetLayer, Text3d, Text3dBatch, Tile, TileCache, TourPlayer, URLHelpers,
Vector3d, WWTControl
} from "@wwtelescope/engine";
Expand Down Expand Up @@ -448,3 +448,37 @@ export function renderOneFrame() {
}

}

export function drawPlanets(renderContext: RenderContext, opacity: number, fraction: number) {
if (Planets._planetTextures == null) {
Planets._loadPlanetTextures();
}
var elong = Planets._geocentricElongation(Planets._planetLocations[9].RA, Planets._planetLocations[9].dec, Planets._planetLocations[0].RA, Planets._planetLocations[0].dec);
var raDif = Planets._planetLocations[9].RA - Planets._planetLocations[0].RA;
if (Planets._planetLocations[9].RA < Planets._planetLocations[0].RA) {
raDif += 24;
}
var phaseAngle = Planets._phaseAngle(elong, Planets._planetLocations[9].distance, Planets._planetLocations[0].distance);
var limbAngle = Planets._positionAngle(Planets._planetLocations[9].RA, Planets._planetLocations[9].dec, Planets._planetLocations[0].RA, Planets._planetLocations[0].dec);
if (raDif < 12) {
phaseAngle += 180;
}
var dista = (Math.abs(Planets._planetLocations[9].RA - Planets._planetLocations[0].RA) * 15) * Math.cos(Coordinates.degreesToRadians(Planets._planetLocations[0].dec));
var distb = Math.abs(Planets._planetLocations[9].dec - Planets._planetLocations[0].dec);
var sunMoonDist = Math.sqrt(dista * dista + distb * distb);
var eclipse = false;
var coronaOpacity = 0;
var moonEffect = (Planets._planetScales[9] / 2 - sunMoonDist);
var darkLimb = Math.min(32, sunMoonDist * 32);
if (fraction == 1) {
eclipse = true;
coronaOpacity = Math.min(1, (moonEffect - (Planets._planetScales[0] / 2)) / 0.001);
Planets._drawPlanet(renderContext, 18, coronaOpacity);
}
for (const key in Planets._planetDrawOrder) {
// 0: Sun, 9: Moon, 19: Earth
var planetId = Planets._planetDrawOrder[key];
Planets._drawPlanet(renderContext, planetId, 1);
}
return true;
}

0 comments on commit 55351d0

Please sign in to comment.