add starry sky box with shader
This commit is contained in:
parent
f95a69c095
commit
6d12033e23
11
assets/shaders/skybox.wgsl
Normal file
11
assets/shaders/skybox.wgsl
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
#import bevy_pbr::{
|
||||||
|
mesh_view_bindings::globals,
|
||||||
|
forward_io::VertexOutput,
|
||||||
|
}
|
||||||
|
|
||||||
|
@fragment
|
||||||
|
fn fragment(in: VertexOutput) -> @location(0) vec4<f32> {
|
||||||
|
let pos: vec2<f32> = 10 * floor(3500 * in.uv);
|
||||||
|
let brightness: vec3<f32> = vec3(max((fract(dot(sin(pos),pos)) - 0.995) * 90.0, 0.0));
|
||||||
|
return vec4<f32>(0.01 * brightness, 1.0);
|
||||||
|
}
|
|
@ -6,10 +6,12 @@ pub struct ShadingPlugin;
|
||||||
impl Plugin for ShadingPlugin {
|
impl Plugin for ShadingPlugin {
|
||||||
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::<SkyBox>::default());
|
||||||
app.add_plugins(MaterialPlugin::<ExtendedMaterial<StandardMaterial, AsteroidSurface, >>::default());
|
app.add_plugins(MaterialPlugin::<ExtendedMaterial<StandardMaterial, AsteroidSurface, >>::default());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Jupiter's Ring
|
||||||
#[derive(Asset, TypePath, AsBindGroup, Debug, Clone)]
|
#[derive(Asset, TypePath, AsBindGroup, Debug, Clone)]
|
||||||
pub struct JupitersRing {
|
pub struct JupitersRing {
|
||||||
pub alpha_mode: AlphaMode,
|
pub alpha_mode: AlphaMode,
|
||||||
|
@ -28,7 +30,16 @@ impl Material for JupitersRing {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Sky Box
|
||||||
|
#[derive(Asset, TypePath, AsBindGroup, Debug, Clone)]
|
||||||
|
pub struct SkyBox {}
|
||||||
|
impl Material for SkyBox {
|
||||||
|
fn fragment_shader() -> ShaderRef {
|
||||||
|
"shaders/skybox.wgsl".into()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Asteroid Surface
|
||||||
#[derive(Asset, Reflect, AsBindGroup, Debug, Clone)]
|
#[derive(Asset, Reflect, AsBindGroup, Debug, Clone)]
|
||||||
pub struct AsteroidSurface {
|
pub struct AsteroidSurface {
|
||||||
#[uniform(100)]
|
#[uniform(100)]
|
||||||
|
|
27
src/world.rs
27
src/world.rs
|
@ -2,6 +2,7 @@ use crate::{actor, audio, hud, nature, shading, var};
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
use bevy::math::{DVec3, I64Vec3};
|
use bevy::math::{DVec3, I64Vec3};
|
||||||
use bevy::scene::{InstanceId, SceneInstance};
|
use bevy::scene::{InstanceId, SceneInstance};
|
||||||
|
use bevy::render::mesh::Indices;
|
||||||
use bevy_xpbd_3d::prelude::*;
|
use bevy_xpbd_3d::prelude::*;
|
||||||
use bevy_xpbd_3d::plugins::sync;
|
use bevy_xpbd_3d::plugins::sync;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
@ -102,7 +103,8 @@ pub fn setup(
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
mut meshes: ResMut<Assets<Mesh>>,
|
mut meshes: ResMut<Assets<Mesh>>,
|
||||||
mut materials: ResMut<Assets<StandardMaterial>>,
|
mut materials: ResMut<Assets<StandardMaterial>>,
|
||||||
mut materials_custom: ResMut<Assets<shading::JupitersRing>>,
|
mut materials_jupiter: ResMut<Assets<shading::JupitersRing>>,
|
||||||
|
mut materials_skybox: ResMut<Assets<shading::SkyBox>>,
|
||||||
asset_server: Res<AssetServer>,
|
asset_server: Res<AssetServer>,
|
||||||
) {
|
) {
|
||||||
// Load assets
|
// Load assets
|
||||||
|
@ -179,13 +181,34 @@ pub fn setup(
|
||||||
}
|
}
|
||||||
info!("Generated {starcount} stars");
|
info!("Generated {starcount} stars");
|
||||||
|
|
||||||
|
// Add shaded skybox
|
||||||
|
//let mut mesh = Mesh::from(Sphere::new(1e9).mesh().uv(50, 50));
|
||||||
|
let mut mesh = Mesh::from(Sphere::new(1e10).mesh().uv(5, 5));
|
||||||
|
//let mut mesh = Mesh::from(Cuboid::from_size(Vec3::splat(2e10)));
|
||||||
|
if let Some(Indices::U32(indices)) = mesh.indices_mut() {
|
||||||
|
// Reverse the order of each triangle to avoid backface culling
|
||||||
|
for slice in indices.chunks_mut(3) {
|
||||||
|
slice.reverse();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
commands.spawn((
|
||||||
|
MaterialMeshBundle {
|
||||||
|
mesh: meshes.add(mesh),
|
||||||
|
material: materials_skybox.add(shading::SkyBox {}),
|
||||||
|
transform: Transform::from_translation(Vec3::new(0.0, 0.0, 0.0)),
|
||||||
|
..default()
|
||||||
|
},
|
||||||
|
Position::from_xyz(0.0, 0.0, 0.0),
|
||||||
|
Rotation::from(Quat::IDENTITY),
|
||||||
|
));
|
||||||
|
|
||||||
// Add shaded ring
|
// Add shaded ring
|
||||||
let ring_radius = 229_000_000.0;
|
let ring_radius = 229_000_000.0;
|
||||||
let jupiter_radius = 71_492_000.0;
|
let jupiter_radius = 71_492_000.0;
|
||||||
commands.spawn((
|
commands.spawn((
|
||||||
MaterialMeshBundle {
|
MaterialMeshBundle {
|
||||||
mesh: meshes.add(Mesh::from(Cylinder::new(ring_radius, 1.0))),
|
mesh: meshes.add(Mesh::from(Cylinder::new(ring_radius, 1.0))),
|
||||||
material: materials_custom.add(shading::JupitersRing {
|
material: materials_jupiter.add(shading::JupitersRing {
|
||||||
alpha_mode: AlphaMode::Blend,
|
alpha_mode: AlphaMode::Blend,
|
||||||
ring_radius: ring_radius,
|
ring_radius: ring_radius,
|
||||||
jupiter_radius: jupiter_radius,
|
jupiter_radius: jupiter_radius,
|
||||||
|
|
Loading…
Reference in a new issue