add first draft of aurora shader

This commit is contained in:
yuni 2024-10-30 05:57:29 +01:00
parent cb9fe1d500
commit c87a68b778
4 changed files with 68 additions and 0 deletions

View file

@ -0,0 +1,25 @@
#import bevy_pbr::{
mesh_view_bindings::globals,
forward_io::VertexOutput,
}
const latitude_cutoff = 50.0;
const tau = 6.283185307179586;
@fragment
fn fragment(in: VertexOutput) -> @location(0) vec4<f32> {
let color = vec3<f32>(2.0, 6.0, 20.0);
var alpha = 0.0;
let lat = 180.0 * abs(in.uv[1] - 0.5);
let phase = in.uv[0] * 2.0 * 3.14159;
if (lat > latitude_cutoff) {
let y = (lat - latitude_cutoff) / (90.0 - latitude_cutoff);
//alpha = sin(phase * 5.0) + sin(5.0 * y);
alpha = 0.1 * clamp(-4.0 * cos(tau * sqrt(y + 0.01 * sin(phase * 20.0) + 0.1 * sin(phase))) - 3.0, 0.0, 1.0);
alpha += 0.3 * clamp(-1000.0 * cos(tau * sqrt(y + 0.01 * sin(phase * 30.0) + 0.1 * sin(phase + 0.3))) - 992.0, 0.0, 1.0);
alpha += 0.2 * clamp(-1000.0 * cos(tau * sqrt(-0.13 + y + 0.01 * sin(phase * 20.0) + 0.2 * sin(phase + 0.4))) - 992.0, 0.0, 1.0);
}
return vec4<f32>(color, clamp(alpha, 0.0, 1.0));
}

View file

@ -106,6 +106,7 @@ struct ParserState {
orbit_phase: Option<f64>, orbit_phase: Option<f64>,
has_physics: bool, has_physics: bool,
has_ring: bool, has_ring: bool,
has_aurora: bool,
wants_maxrotation: Option<f64>, wants_maxrotation: Option<f64>,
wants_maxvelocity: Option<f64>, wants_maxvelocity: Option<f64>,
wants_tolookat_id: Option<String>, wants_tolookat_id: Option<String>,
@ -169,6 +170,7 @@ impl Default for ParserState {
orbit_phase: None, orbit_phase: None,
has_physics: true, has_physics: true,
has_ring: false, has_ring: false,
has_aurora: false,
wants_maxrotation: None, wants_maxrotation: None,
wants_maxvelocity: None, wants_maxvelocity: None,
wants_tolookat_id: None, wants_tolookat_id: None,
@ -473,6 +475,9 @@ pub fn load_defs(mut ew_spawn: EventWriter<SpawnEvent>) {
["ring", "yes"] => { ["ring", "yes"] => {
state.has_ring = true; state.has_ring = true;
} }
["aurora", "yes"] => {
state.has_aurora = true;
}
["tidally", "locked"] => { ["tidally", "locked"] => {
state.is_tidally_locked = true; state.is_tidally_locked = true;
} }
@ -907,6 +912,7 @@ fn spawn_entities(
mut meshes: ResMut<Assets<Mesh>>, mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>, mut materials: ResMut<Assets<StandardMaterial>>,
mut materials_jupiter: ResMut<Assets<load::JupitersRing>>, mut materials_jupiter: ResMut<Assets<load::JupitersRing>>,
mut materials_aurora: ResMut<Assets<load::JupitersAurora>>,
mut id2pos: ResMut<game::Id2Pos>, mut id2pos: ResMut<game::Id2Pos>,
mut achievement_tracker: ResMut<var::AchievementTracker>, mut achievement_tracker: ResMut<var::AchievementTracker>,
mut ew_updateavatar: EventWriter<hud::UpdateAvatarEvent>, mut ew_updateavatar: EventWriter<hud::UpdateAvatarEvent>,
@ -1301,6 +1307,26 @@ fn spawn_entities(
NotShadowReceiver, NotShadowReceiver,
)); ));
} }
if state.has_aurora {
let radius = state.model_scale * 1.01;
commands.spawn((
world::DespawnOnPlayerDeath,
MaterialMeshBundle {
mesh: meshes.add(Sphere::new(radius).mesh().uv(128, 128)),
material: materials_aurora.add(load::JupitersAurora {
alpha_mode: AlphaMode::Blend,
}),
transform: Transform::from_translation(absolute_pos.as_vec3()),
..default()
},
Position::new(absolute_pos),
//Rotation::default(),
Rotation::from(Quat::from_rotation_x(-90f32.to_radians())),
NotShadowCaster,
NotShadowReceiver,
));
}
} }
} }

View file

@ -89,6 +89,7 @@ actor 0 0 0
planet yes planet yes
sphere yes sphere yes
ring yes ring yes
aurora yes
physics off physics off
rotationy 3.13 rotationy 3.13
rotationz 135 rotationz 135

View file

@ -19,6 +19,7 @@ pub struct LoadPlugin;
impl Plugin for LoadPlugin { impl Plugin for LoadPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_plugins(MaterialPlugin::<JupitersRing>::default()); app.add_plugins(MaterialPlugin::<JupitersRing>::default());
app.add_plugins(MaterialPlugin::<JupitersAurora>::default());
app.add_plugins(MaterialPlugin::<SkyBox>::default()); app.add_plugins(MaterialPlugin::<SkyBox>::default());
app.add_plugins(MaterialPlugin::< app.add_plugins(MaterialPlugin::<
ExtendedMaterial<StandardMaterial, AsteroidSurface>, ExtendedMaterial<StandardMaterial, AsteroidSurface>,
@ -103,6 +104,21 @@ impl Material for JupitersRing {
} }
} }
// Jupiter's Aurora
#[derive(Asset, TypePath, AsBindGroup, Debug, Clone)]
pub struct JupitersAurora {
pub alpha_mode: AlphaMode,
}
impl Material for JupitersAurora {
fn fragment_shader() -> ShaderRef {
"shaders/aurora.wgsl".into()
}
fn alpha_mode(&self) -> AlphaMode {
self.alpha_mode
}
}
// Sky Box // Sky Box
#[derive(Asset, TypePath, AsBindGroup, Debug, Clone)] #[derive(Asset, TypePath, AsBindGroup, Debug, Clone)]
pub struct SkyBox {} pub struct SkyBox {}