From 0a2659a125cc0fe5eb3fe3537926dcac4f3cbcc3 Mon Sep 17 00:00:00 2001 From: Andre-John Mas Date: Fri, 17 Nov 2023 15:31:05 -0500 Subject: [PATCH] adding captialised Sun.ts --- src/viewer/Sun.ts | 104 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 src/viewer/Sun.ts diff --git a/src/viewer/Sun.ts b/src/viewer/Sun.ts new file mode 100644 index 0000000..7d81ea9 --- /dev/null +++ b/src/viewer/Sun.ts @@ -0,0 +1,104 @@ +import { DateTime } from 'luxon'; +import { Object3D, SphereGeometry, PointLight, Mesh, Group } from '../utils/three'; +import SceneComponent from './interfaces/SceneComponent'; +import SatelliteOrbitScene from './SatelliteOrbitScene'; + +class Sun implements SceneComponent { + static deg2RadMult = (Math.PI / 180); + + speedy = false; + showGeometry = false; + lightSouce: Object3D | undefined; + lightSourceGeometery: Object3D | undefined; + hour = 0; + objectGroup?: Group; + scene?: SatelliteOrbitScene; + visible = true; + + degreesToReadians (degrees: number) { + return degrees * Sun.deg2RadMult; + } + + calculateSunLoc () { + // This is a simple sun location calculator. Here until I get + // original code back in + const distance = 25; + + let hour = this.hour; + // if we aren't overriding the hour, then calculate from actual time + if (hour === undefined || hour === -1) { + let time = DateTime.utc(); + + time = time.set({ hour: 18 }); + hour = time.hour; + } + + // adjust by 180, since left of texture is at 0 + const angle = ((hour / 24) * 360) + + 180; + + const point = { + x: distance * Math.cos( this.degreesToReadians(angle) ), + z: distance * Math.sin( this.degreesToReadians(angle) ), + }; + + return point; + } + + init (scene: SatelliteOrbitScene) { + this.calculateSunLoc(); + const sunLoc = this.calculateSunLoc(); + const coords = { x: sunLoc.x, y: 0, z: sunLoc.z}; + + this.scene = scene; + this.objectGroup = new Group(); + this.lightSouce = new PointLight(0xffffff, 2000); + this.lightSouce.position.set(coords.x, coords.y, coords.z); + this.objectGroup.add(this.lightSouce); + + if (this.showGeometry) { + const geometry = new SphereGeometry(0.1, 32, 32); + this.lightSourceGeometery = new Mesh( geometry ); + this.lightSourceGeometery.position.set(coords.x, coords.y, coords.z); + this.objectGroup.add( this.lightSourceGeometery ); + } + + scene.add(this.objectGroup); + + // Speedily rotate simulated sun + if (this.speedy) { + setInterval(() => { + this.hour += 0.05; + }, 100); + } else { + this.hour = -1; + } + } + + update (): void { + const sunLoc = this.calculateSunLoc(); + const coords = { x: sunLoc.x, y: 0, z: sunLoc.z}; + + if (this.lightSouce) { + this.lightSouce.position.set(coords.x, coords.y, coords.z); + } + + if (this.lightSourceGeometery) { + this.lightSourceGeometery.position.set(coords.x, coords.y, coords.z); + } + } + + setVisible (visible: boolean): void { + if (visible) { + if (!this.visible) { + this.scene?.add(this.objectGroup as Group); + } + } else { + if (this.visible) { + this.scene?.remove(this.objectGroup as Group); + } + } + this.visible = visible; + } +} + +export default Sun; \ No newline at end of file