feat: set correct sun angle from current time

Thanks ChatGPT for that one!!

How to find the current sun angle from Earth's rotation in javascript?
A hint: Where is the sun shining on the earth at the time I visit the site? I guess the time zone that's closest to noon.

the dayTime variable needs to be from 0 to 1

const d = degToRad(360 / dayTime)
const sunPos = new Vec3(
    Math.cos(d),
    Math.sin(d) * Math.sin(0),
    Math.sin(d) * Math.cos(0)
)
This commit is contained in:
2023-02-10 15:35:36 +01:00
parent 83510bda2d
commit 7efc1842bd
2 changed files with 14 additions and 26 deletions

View File

@@ -16,21 +16,10 @@ export class Globe {
this.markers = options.markers || [] this.markers = options.markers || []
this.zoom = 1.3075 this.zoom = 1.3075
// Calculate the current sun position from a given location // Calculate local time for sun position
// const locations = [ const date = new Date()
// { const localHour = date.getHours() + date.getTimezoneOffset() / 60
// lat: -37.840935, this.options.sunAngle = (localHour - 12) / 12
// lng: 144.946457,
// tz: 'Australia/Melbourne',
// },
// {
// lat: 48.856614,
// lng: 2.3522219,
// tz: 'Europe/Paris',
// }
// ]
// const location = locations[1]
// const localDate = new Date(new Date().toLocaleString('en-US', { timeZone: location.tz }))
// Parameters // Parameters
this.params = { this.params = {
@@ -39,7 +28,6 @@ export class Globe {
enableMarkers: options.enableMarkers, enableMarkers: options.enableMarkers,
enableMarkersLinks: options.enableMarkersLinks, enableMarkersLinks: options.enableMarkersLinks,
sunAngle: options.sunAngle || 0, sunAngle: options.sunAngle || 0,
sunAngleDelta: 1.8,
} }
// Misc // Misc
@@ -121,11 +109,10 @@ export class Globe {
imgDark.src = this.options.mapFileDark imgDark.src = this.options.mapFileDark
// Create light // Create light
const lightD = degToRad(7 * 360 / 24)
const sunPosition = new Vec3( const sunPosition = new Vec3(
Math.cos(lightD), Math.cos(this.params.sunAngle),
Math.sin(lightD) * Math.sin(0), Math.sin(this.params.sunAngle) * Math.sin(0),
Math.sin(lightD) * Math.cos(0) Math.sin(this.params.sunAngle) * Math.cos(0)
) )
// Create program // Create program
@@ -362,7 +349,7 @@ export type Marker = {
/** /**
* Detect WebGL support * Detect WebGL support
*/ */
function WebGLSupport () { const WebGLSupport = (): boolean => {
try { try {
const canvas = document.createElement('canvas') const canvas = document.createElement('canvas')
return !!window.WebGLRenderingContext && (canvas.getContext('webgl') || canvas.getContext('experimental-webgl')) return !!window.WebGLRenderingContext && (canvas.getContext('webgl') || canvas.getContext('experimental-webgl'))

View File

@@ -47,9 +47,10 @@ export const createPane = (ctx: any) => {
title: 'Misc', title: 'Misc',
}) })
// Sun position // Sun position
misc.addInput(ctx.params, 'sunAngleDelta', { misc.addInput(ctx.params, 'sunAngle', {
label: 'Sun angle delta', label: 'Sun angle',
min: 0, min: 0,
max: 2 * Math.PI, max: 1,
step: 0.01,
}) })
} }