render starmap based on HYG dataset [http://www.astronexus.com/hyg]

This commit is contained in:
yuni 2024-03-18 22:13:37 +01:00
parent 2786129499
commit 6b0f9c671a
4 changed files with 15650 additions and 22 deletions

View file

@ -4,6 +4,7 @@ mod world;
mod settings;
mod hud;
mod actor;
mod starchart;
use bevy::window::{Window, WindowMode, PrimaryWindow, CursorGrabMode};
use bevy::diagnostic::FrameTimeDiagnosticsPlugin;

15550
src/starchart.rs Normal file

File diff suppressed because it is too large Load diff

View file

@ -1,4 +1,4 @@
use crate::{actor, camera};
use crate::{actor, camera, starchart};
use bevy::prelude::*;
//use bevy::core_pipeline::Skybox;
//use bevy::asset::LoadState;
@ -10,10 +10,8 @@ use std::f32::consts::PI;
const ASTEROID_SIZE: f32 = 100.0;
const MOON_SIZE: f32 = 50.0;
const MARS_SIZE: f32 = 10.0;
const SUN_SIZE: f32 = 5000.0;
const ASTRONAUT_SIZE: f32 = 5.0;
const SUN_BRIGHTNESS: f32 = 1e5;
//const SKYBOX_BRIGHTNESS: f32 = 300.0;
//const SKYBOX_BRIGHTNESS_AR: f32 = 100.0;
@ -31,6 +29,9 @@ impl Plugin for WorldPlugin {
}
}
#[derive(Component)]
pub struct Star;
//#[derive(Resource)]
//pub struct WorldState {
// is_loaded: bool,
@ -140,11 +141,12 @@ pub fn setup(
perceptual_roughness: 1.0,
..default()
});
for i in -12..12 {
for j in -13..13 {
for k in -14..14 {
let maxdist = 10;
for i in -maxdist..maxdist {
for j in -maxdist..maxdist {
for k in -maxdist..maxdist {
let offset = 500.0;
let dist = 18000.0;
let dist = 3e4;
let wobble = dist/2.0;
let (i, j, k) = (i as f32, j as f32, k as f32);
commands.spawn((
@ -164,21 +166,30 @@ pub fn setup(
}
}
// Add THE SUN
let hydrogenfusion_handle = materials.add(StandardMaterial {
emissive: Color::rgb_linear(SUN_BRIGHTNESS, 0.9 * SUN_BRIGHTNESS, SUN_BRIGHTNESS),
..default()
});
commands.spawn(PbrBundle {
mesh: sphere_handle.clone(),
material: hydrogenfusion_handle.clone(),
transform: Transform::from_xyz(
0.0,
30000.0,
-500000.0,
).with_scale(Vec3::splat(SUN_SIZE)),
..default()
});
// Generate starmap
for star in starchart::STARS {
let brightness = star[3] * 2000.0;
let star_color_handle = materials.add(StandardMaterial {
base_color: Color::rgb(0.0, 0.0, 0.0),
emissive: Color::rgb_linear(brightness, 0.9 * brightness, brightness),
..default()
});
let dist = 1e6;
commands.spawn((
Star,
PbrBundle {
mesh: sphere_handle.clone(),
material: star_color_handle.clone(),
transform: Transform::from_xyz(
dist * star[0],
dist * star[1],
dist * star[2],
)
.with_scale(Vec3::splat((1000.0*star[3]).clamp(500.0, 2000.0))),
..default()
}
));
}
// Add alien
commands.spawn(SceneBundle {

66
tools/generate_starchart.py Executable file
View file

@ -0,0 +1,66 @@
#!/usr/bin/env python
# This script requires the following file in the current directory:
# https://github.com/astronexus/HYG-Database/blob/cbd21013d2bb89732b893be357a6f41836dbe614/hyg/CURRENT/hygdata_v41.csv
import csv
import sys
import math
# Lower apparent magnitude = brighter.
# Sun = -26.7
# Sirius = -1.44
# Betelgeuse = 0.45
# Polaris (north star) = 1.97
# Meissa (orion's head, 8th brightest star in orion) = 3.33
MAX_APPARENT_MAGNITUDE = 7.0
print("// This file was autogenerated by 'genrate_starchart.py' using data from")
print("// the HYG database: https://github.com/astronexus/HYG-Database/tree/main/hyg")
print("// License: CC BY-SA 4.0: https://creativecommons.org/licenses/by-sa/4.0/")
print("")
print("pub const STARS: &[[f32; 5]] = &[")
def render(ra, dec, mag, proper):
ra = float(ra)
dec = float(dec)
mag = float(mag)
distance = 1.0
ra_radians = math.radians(ra * 15) # ra is in [0, 24], multiplying by 15 gives degrees in [0, 360]
dec_radians = math.radians(dec)
#print(f"ra_radians={ra_radians}, dec_radians={dec_radians}, dec={dec}, ra={ra}", file=sys.stderr)
x = distance * math.cos(dec_radians) * math.cos(-ra_radians)
y = distance * math.cos(dec_radians) * math.sin(-ra_radians)
z = distance * math.sin(dec_radians)
# Correct for differences in coordinate system axes
x, y, z = x, z, y
comment = f" // {proper}" if proper else ""
brightness = 2.512 ** (0 - mag)
print(f" [{x:.04}, {y:.04}, {z:.04}, {brightness:.04}, {ci}],{comment}")
total = 0
count = 0
with open("hygdata_v41.csv", "r", encoding="utf-8") as f:
for entry in csv.DictReader(f):
total += 1
ra = entry['ra']
dec = entry['dec']
mag = entry['mag']
ci = entry['ci']
proper = entry['proper']
proper = "".join([c for c in proper if c.isalnum() or c in ' -_'])
if not all([ra, dec, mag, ci]):
continue
if float(mag) > MAX_APPARENT_MAGNITUDE:
continue
render(ra, dec, mag, proper)
count += 1
print("];")
print(f"Wrote {count} stars (total={total}).", file=sys.stderr)