From 1c06343fa64b6c8a4e4b8126456dbc1e5bbc3fd6 Mon Sep 17 00:00:00 2001 From: hut Date: Mon, 18 Mar 2024 23:47:03 +0100 Subject: [PATCH] dye stars in their color (doesn't work too well yet) --- src/main.rs | 3 +++ src/nature.rs | 27 +++++++++++++++++++++++++++ src/world.rs | 7 ++++--- 3 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 src/nature.rs diff --git a/src/main.rs b/src/main.rs index 68e22c7..d9f09e4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,9 @@ mod hud; mod actor; mod starchart; +#[allow(dead_code)] +mod nature; + use bevy::window::{Window, WindowMode, PrimaryWindow, CursorGrabMode}; use bevy::diagnostic::FrameTimeDiagnosticsPlugin; use bevy::prelude::*; diff --git a/src/nature.rs b/src/nature.rs new file mode 100644 index 0000000..bffd1d5 --- /dev/null +++ b/src/nature.rs @@ -0,0 +1,27 @@ +// This stuff here, this stuff is messy. Nobody wants to deal with this, +// nobody cares how it works, but I guess we need it as an ingredient for +// the universe *sigh* so here we go. + +pub fn star_color_index_to_rgb(color_index: f32) -> (f32, f32, f32) { + let temperature = 4600.0 * ((1.0 / (0.92 * color_index + 1.7)) + (1.0 / (0.92 * color_index + 0.62))); + + let (red, green, blue) = if temperature <= 6600.0 { + let red = 255.0; + let green = 99.4708025861 * (temperature / 100.0).ln() - 161.1195681661; + let blue = if temperature <= 2000.0 { + 0.0 + } else { + 138.5177312231 * ((temperature / 100.0) - 10.0).ln() - 305.0447927307 + }; + (red, green, blue) + } else { + let red = 329.698727446 * ((temperature / 100.0 - 60.0).powf(-0.1332047592)); + let green = 288.1221695283 * ((temperature / 100.0 - 60.0).powf(-0.0755148492)); + let blue = 255.0; + (red, green, blue) + }; + + let clamp = |x: f32| -> f32 { (x / 255.0).max(0.0).min(1.0) }; + + return (clamp(red), clamp(green), clamp(blue)) +} diff --git a/src/world.rs b/src/world.rs index 6ff3c25..0e5d74d 100644 --- a/src/world.rs +++ b/src/world.rs @@ -1,4 +1,4 @@ -use crate::{actor, camera, starchart}; +use crate::{actor, camera, starchart, nature}; use bevy::prelude::*; //use bevy::core_pipeline::Skybox; //use bevy::asset::LoadState; @@ -169,9 +169,10 @@ pub fn setup( // Generate starmap for star in starchart::STARS { let brightness = star[3] * 2000.0; + let (r, g, b) = nature::star_color_index_to_rgb(star[4]); 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), + base_color: Color::rgb(r, g, b), + emissive: Color::rgb_linear(r*brightness, g*brightness, b*brightness), ..default() }); let dist = 1e6;