upgrade to 0.7

main
annieversary 2022-07-07 00:12:08 +01:00
parent fb419df631
commit 4c4d9c1a7c
10 changed files with 809 additions and 1016 deletions

1474
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -1,11 +1,9 @@
[package]
name = "moria"
version = "0.1.0"
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
edition = "2021"
[dependencies]
bevy = "0.5.0"
bevy_mod_raycast = "0.2.2"
bevy_prototype_debug_lines = "0.3.3"
bevy = "0.7.0"
bevy_mod_raycast = "0.5.0"
bevy_prototype_debug_lines = { version = "0.7", features = ["3d"] }

View File

@ -5,6 +5,7 @@ use crate::{pillar::UnlitPillar, player::*};
pub struct MyRaycastSet;
#[derive(Component)]
pub struct Camera;
pub fn spawn_camera(commands: &mut Commands) {
commands

View File

@ -1,3 +1,6 @@
use bevy::prelude::*;
#[derive(Component)]
pub struct Illumination {
pub radius: f32,
}

View File

@ -4,6 +4,7 @@ use crate::{illumination::Illumination, player::Player};
const RANGE: f32 = 25.0;
#[derive(Component)]
pub struct LightBall;
pub fn light_up_ball_when_close_to_player(
mut commands: Commands,
@ -13,7 +14,7 @@ pub fn light_up_ball_when_close_to_player(
&Transform,
&LightBall,
&mut Handle<StandardMaterial>,
Option<&mut Light>,
Option<&mut PointLight>,
Option<&mut Illumination>,
)>,
materials: Res<LightBallMaterials>,
@ -34,7 +35,7 @@ pub fn light_up_ball_when_close_to_player(
if let Some(mut l) = light {
l.intensity = 300.0 * (RANGE - dis) / RANGE;
} else {
commands.entity(entity).insert(Light {
commands.entity(entity).insert(PointLight {
color: Color::rgb(15.0, 15.0, 15.0),
intensity: 300.0 * (RANGE - dis) / RANGE,
..Default::default()
@ -54,7 +55,7 @@ pub fn light_up_ball_when_close_to_player(
// remove light if there is one
if light.is_some() {
commands.entity(entity).remove::<Light>();
commands.entity(entity).remove::<PointLight>();
}
if illumination.is_some() {
commands.entity(entity).remove::<Illumination>();

View File

@ -1,12 +1,12 @@
use bevy::{
prelude::*,
render::texture::{AddressMode, FilterMode},
render::render_resource::{AddressMode, FilterMode},
};
use crate::AppState;
pub struct LoadedAssets {
pub floor: Handle<Texture>,
pub floor: Handle<Image>,
}
impl FromWorld for LoadedAssets {
fn from_world(world: &mut World) -> Self {
@ -19,14 +19,14 @@ impl FromWorld for LoadedAssets {
pub fn loading(
assets: Res<LoadedAssets>,
mut textures: ResMut<Assets<Texture>>,
mut textures: ResMut<Assets<Image>>,
mut state: ResMut<State<AppState>>,
) {
if let Some(t) = textures.get_mut(&assets.floor) {
t.sampler.address_mode_u = AddressMode::MirrorRepeat;
t.sampler.address_mode_v = AddressMode::Repeat;
t.sampler.address_mode_w = AddressMode::Repeat;
t.sampler.mipmap_filter = FilterMode::Linear;
t.sampler_descriptor.address_mode_u = AddressMode::MirrorRepeat;
t.sampler_descriptor.address_mode_v = AddressMode::Repeat;
t.sampler_descriptor.address_mode_w = AddressMode::Repeat;
t.sampler_descriptor.mipmap_filter = FilterMode::Linear;
state.set(AppState::Game).unwrap();
}

View File

@ -1,5 +1,5 @@
use bevy::{input::system::exit_on_esc_system, pbr::AmbientLight, prelude::*};
use bevy_mod_raycast::{build_rays, update_raycast, PluginState, RayCastMesh, RaycastSystem};
use bevy_mod_raycast::{DefaultRaycastingPlugin, RayCastMesh, RaycastSystem};
use bevy_prototype_debug_lines::*;
mod camera;
@ -10,7 +10,6 @@ mod light_balls;
mod loading;
mod pillar;
mod player;
mod rendering;
use camera::*;
use debug::*;
@ -25,66 +24,51 @@ pub enum AppState {
}
fn main() {
App::build()
App::new()
.insert_resource(Msaa { samples: 4 })
.insert_resource(ClearColor(Color::rgb(0.0, 0.0, 0.0)))
.add_plugins(rendering::CustomPlugins)
.add_plugin(DebugLinesPlugin)
.add_plugins(DefaultPlugins)
.add_plugin(DebugLinesPlugin::with_depth_test(true))
.init_resource::<Debug>()
.init_resource::<LightBallMaterials>()
.init_resource::<PillarMaterials>()
.init_resource::<MouseCoords>()
.init_resource::<FloatingOrbsInterpolationState>()
.init_resource::<PillarActivationProgress>()
.init_resource::<LightExcitationState>()
.init_resource::<loading::LoadedAssets>()
.add_state(AppState::Loading)
// raycasting
.init_resource::<PluginState<MyRaycastSet>>()
.add_system_to_stage(
CoreStage::PostUpdate,
build_rays::<MyRaycastSet>
.system()
.label(RaycastSystem::BuildRays),
)
.add_system_to_stage(
CoreStage::PostUpdate,
update_raycast::<MyRaycastSet>
.system()
.label(RaycastSystem::UpdateRaycast)
.after(RaycastSystem::BuildRays),
)
.add_plugin(DefaultRaycastingPlugin::<MyRaycastSet>::default())
.add_system_to_stage(
CoreStage::PreUpdate,
update_raycast_with_cursor
.system()
.before(RaycastSystem::BuildRays),
update_raycast_with_cursor.before(RaycastSystem::<MyRaycastSet>::BuildRays),
)
// loading
.add_system_set(
SystemSet::on_update(AppState::Loading).with_system(loading::loading.system()),
)
.add_system_set(SystemSet::on_update(AppState::Loading).with_system(loading::loading))
// game
.add_system_set(SystemSet::on_enter(AppState::Game).with_system(setup.system()))
.add_system_set(SystemSet::on_enter(AppState::Game).with_system(setup))
.add_system_set(
SystemSet::on_update(AppState::Game)
.with_system(update_raw_mouse_coords.system())
.with_system(update_processed_mouse_coords.system())
.with_system(exit_on_esc_system.system())
.with_system(light_movement.system())
.with_system(move_light_friends.system())
.with_system(update_floating_orbs_interpolation.system())
.with_system(move_player.system())
.with_system(camera_follow_player.system())
.with_system(light_up_ball_when_close_to_player.system())
.with_system(increase_progress_when_activating_pillar.system())
.with_system(activate_pillar_when_progress_is_1.system())
.with_system(decrease_pillar_strength.system())
.with_system(toggle_debug.system()),
.with_system(update_raw_mouse_coords)
.with_system(update_processed_mouse_coords)
.with_system(exit_on_esc_system)
.with_system(light_movement)
.with_system(move_light_friends)
.with_system(update_floating_orbs_interpolation)
.with_system(move_player)
.with_system(camera_follow_player)
.with_system(light_up_ball_when_close_to_player)
.with_system(increase_progress_when_activating_pillar)
.with_system(activate_pillar_when_progress_is_1)
.with_system(decrease_pillar_strength)
.with_system(update_light_excitation_state)
.with_system(toggle_debug),
)
.add_system_set(
SystemSet::on_update(AppState::Game)
.with_run_criteria(debug_run_criteria.system())
.with_system(debug_draw_illumination.system()),
.with_run_criteria(debug_run_criteria)
.with_system(debug_draw_illumination),
)
.run();
}
@ -127,7 +111,24 @@ fn setup(
// columns
// columns::spawn_columns(&mut commands, &mut meshes, &mut materials);
spawn_pillar(&mut commands, &pillar_materials, Vec3::new(0.0, 1.0, 30.0));
spawn_pillar(
&mut commands,
&pillar_materials,
&mut materials,
Vec3::new(0.0, 1.0, 50.0),
);
spawn_pillar(
&mut commands,
&pillar_materials,
&mut materials,
Vec3::new(-30.0, 1.0, 50.0),
);
spawn_pillar(
&mut commands,
&pillar_materials,
&mut materials,
Vec3::new(30.0, 1.0, 50.0),
);
// player
spawn_player(&mut commands, &mut meshes, &mut materials);

View File

@ -5,19 +5,27 @@ use crate::{camera::MouseCoords, player::LightFriends};
const SIZE: f32 = 10.0;
const HEIGHT: f32 = 20.0;
#[derive(Component)]
pub struct UnlitPillar;
#[derive(Component)]
pub struct LitPillar {
created_at: f64,
}
pub fn spawn_pillar(commands: &mut Commands, materials: &PillarMaterials, pos: Vec3) {
let mut column_material: StandardMaterial = Color::rgb(0.7, 0.7, 0.7).into();
column_material.metallic = 0.0;
column_material.reflectance = 0.0;
pub fn spawn_pillar(
commands: &mut Commands,
pillar_mats: &PillarMaterials,
materials: &mut Assets<StandardMaterial>,
pos: Vec3,
) {
let mut unlit: StandardMaterial = Color::rgb(0.9, 0.9, 0.9).into();
unlit.metallic = 0.0;
unlit.reflectance = 0.0;
let unlit = materials.add(unlit);
commands
.spawn_bundle(PbrBundle {
mesh: materials.mesh.clone(),
material: materials.unlit.clone(),
mesh: pillar_mats.mesh.clone(),
material: unlit,
transform: Transform::from_translation(pos),
..Default::default()
})
@ -64,30 +72,40 @@ pub fn increase_progress_when_activating_pillar(
pub fn activate_pillar_when_progress_is_1(
mut commands: Commands,
mut progress: ResMut<PillarActivationProgress>,
mut query: Query<(&mut Handle<StandardMaterial>, &UnlitPillar)>,
materials: Res<PillarMaterials>,
mut progress_res: ResMut<PillarActivationProgress>,
query: Query<(&Handle<StandardMaterial>, &UnlitPillar)>,
mut materials: ResMut<Assets<StandardMaterial>>,
time: Res<Time>,
) {
if let Some(p) = progress.0 {
if p.1 >= 1.0 {
progress.0 = None;
if let Some((entity, progress)) = progress_res.0 {
if progress >= 1.0 {
progress_res.0 = None;
// activate pillar
commands
.entity(p.0)
.entity(entity)
.remove::<UnlitPillar>()
.insert(LitPillar {
created_at: time.seconds_since_startup(),
})
.insert(Light {
.insert(PointLight {
color: Color::rgb(15.0, 15.0, 15.0),
intensity: 500.0,
..Default::default()
});
if let Ok((mut handle, _)) = query.get_mut(p.0) {
*handle = materials.lit.clone();
if let Ok((handle, _)) = query.get(entity) {
if let Some(mat) = materials.get_mut(handle) {
mat.base_color = Color::rgb(15.0, 15.0, 15.0);
mat.emissive = Color::rgb(15.0, 15.0, 15.0);
}
}
} else {
if let Ok((handle, _)) = query.get(entity) {
if let Some(mat) = materials.get_mut(handle) {
mat.base_color = Color::rgb(15.0, 15.0, 15.0) * progress;
mat.emissive = Color::rgb(15.0, 15.0, 15.0) * progress;
}
}
}
}
@ -95,61 +113,54 @@ pub fn activate_pillar_when_progress_is_1(
pub fn decrease_pillar_strength(
mut commands: Commands,
materials: Res<PillarMaterials>,
mut query: Query<(
Entity,
&mut Light,
&mut Handle<StandardMaterial>,
&mut PointLight,
&Handle<StandardMaterial>,
&LitPillar,
)>,
mut materials: ResMut<Assets<StandardMaterial>>,
time: Res<Time>,
) {
let t = time.seconds_since_startup();
for (entity, mut light, mut handle, pillar) in query.iter_mut() {
for (entity, mut light, handle, pillar) in query.iter_mut() {
let d = t - pillar.created_at;
const DUR: f32 = 10.0;
let i = (DUR - d as f32) / DUR;
if d > 30.0 {
if i < 0.0 {
// deactivate pillar
commands
.entity(entity)
.remove::<LitPillar>()
.remove::<Light>()
.remove::<PointLight>()
.insert(UnlitPillar);
*handle = materials.unlit.clone();
if let Some(mat) = materials.get_mut(handle) {
mat.base_color = Color::rgb(0.9, 0.9, 0.9);
mat.emissive = Color::rgb(0., 0., 0.);
}
} else {
let i = (30.0 - d as f32) / 30.0;
light.intensity = i * 500.0;
if let Some(mat) = materials.get_mut(handle) {
mat.base_color = Color::rgb(15.0, 15.0, 15.0) * i.powf(1.3);
mat.emissive = Color::rgb(15.0, 15.0, 15.0) * i.powf(1.3);
}
}
}
}
pub struct PillarMaterials {
unlit: Handle<StandardMaterial>,
lit: Handle<StandardMaterial>,
mesh: Handle<Mesh>,
}
impl FromWorld for PillarMaterials {
fn from_world(world: &mut World) -> Self {
let world = world.cell();
let mut materials = world
.get_resource_mut::<Assets<StandardMaterial>>()
.unwrap();
let mut meshes = world.get_resource_mut::<Assets<Mesh>>().unwrap();
let mut unlit: StandardMaterial = Color::rgb(0.9, 0.9, 0.9).into();
unlit.metallic = 0.0;
unlit.reflectance = 0.0;
let unlit = materials.add(unlit);
let mut lit: StandardMaterial = Color::rgb(15.0, 15.0, 15.0).into();
lit.metallic = 0.5;
lit.reflectance = 0.5;
lit.emissive = Color::rgb(15.0, 15.0, 15.0);
let lit = materials.add(lit);
let mesh = meshes.add(Mesh::from(shape::Box::new(SIZE, HEIGHT, SIZE)));
Self { lit, unlit, mesh }
Self { mesh }
}
}

View File

@ -2,7 +2,9 @@ use bevy::prelude::*;
use crate::{camera::MouseCoords, illumination::Illumination, pillar::PillarActivationProgress};
#[derive(Component)]
pub struct Player;
#[derive(Component)]
pub struct PlayerLight {
i: u8,
}
@ -51,9 +53,9 @@ pub fn spawn_player(
transform: Transform::from_xyz(0.0, 0.5, 0.0),
..Default::default()
})
.insert_bundle(LightBundle {
.insert_bundle(PointLightBundle {
transform: Transform::from_xyz(0.0, 0.0, 0.0),
light: Light {
point_light: PointLight {
color: color.as_rgba() * 10.0,
..Default::default()
},
@ -65,6 +67,7 @@ pub fn spawn_player(
});
}
#[derive(Component)]
pub struct LightFriends;
pub fn move_light_friends(
mut query: Query<(&mut Transform, &LightFriends)>,
@ -90,27 +93,42 @@ pub fn move_light_friends(
}
}
#[derive(Default)]
pub struct LightExcitationState(f32);
pub fn update_light_excitation_state(
pillar_progress: Res<PillarActivationProgress>,
mut excitation: ResMut<LightExcitationState>,
) {
// light pillar activation progress
let p = pillar_progress.0.map(|(_, p)| p).unwrap_or(0.0);
if p > excitation.0 {
excitation.0 = (excitation.0 + 0.008).clamp(0.0, 1.0);
} else {
excitation.0 = (excitation.0 - 0.008).clamp(0.0, 1.0);
}
}
pub fn light_movement(
mut query: Query<(&mut Transform, &PlayerLight)>,
time: Res<Time>,
interpolation: Res<FloatingOrbsInterpolationState>,
pillar_progress: Res<PillarActivationProgress>,
excitation: Res<LightExcitationState>,
) {
let t = time.seconds_since_startup();
// light pillar activation progress
let p = pillar_progress.0.map(|(_, p)| p).unwrap_or(0.0);
let p = excitation.0;
// make the friends go further when the button is pressed, but close in when activating a pillar
let width = (5.0 + interpolation.0 * 7.0) * (1.0 - p);
let p = p as f64 / 3.0;
let p = 1.0 + 0.8 * p as f64;
for (mut trans, light) in query.iter_mut() {
let i = light.i as f64;
let i2 = i / 2.0;
trans.translation = Vec3::new(
width * i2 as f32 * (t * (0.4 + p) * i2 + i).cos() as f32,
3.0 * (t * (1.1 + p) * i2 + i).sin() as f32 + 4.0,
width * (t * (0.4 + p) * i + i).sin() as f32,
width * i2 as f32 * (t * 0.4 * i2 + i * p).cos() as f32,
3.0 * (t * 1.1 * i2 + i * p).sin() as f32 + 4.0,
width * (t * 0.4 * i + i * p).sin() as f32,
);
}
}

View File

@ -1,102 +0,0 @@
use bevy::app::{PluginGroup, PluginGroupBuilder};
use bevy::pbr::render_graph::{LightsNode, PBR_PIPELINE_HANDLE};
use bevy::prelude::*;
use bevy::render::{
pipeline::PipelineDescriptor,
render_graph::{base, AssetRenderResourcesNode, RenderGraph, RenderResourcesNode},
shader::Shader,
};
mod pipeline;
use pipeline::build_pbr_pipeline;
pub struct CustomPlugins;
impl PluginGroup for CustomPlugins {
fn build(&mut self, group: &mut PluginGroupBuilder) {
group.add(bevy::log::LogPlugin::default());
group.add(bevy::core::CorePlugin::default());
group.add(bevy::transform::TransformPlugin::default());
group.add(bevy::diagnostic::DiagnosticsPlugin::default());
group.add(bevy::input::InputPlugin::default());
group.add(bevy::window::WindowPlugin::default());
group.add(bevy::asset::AssetPlugin::default());
group.add(bevy::scene::ScenePlugin::default());
group.add(bevy::render::RenderPlugin::default());
group.add(bevy::sprite::SpritePlugin::default());
group.add(CustomPbrPlugin::default());
group.add(bevy::ui::UiPlugin::default());
group.add(bevy::text::TextPlugin::default());
group.add(bevy::gilrs::GilrsPlugin::default());
group.add(bevy::gltf::GltfPlugin::default());
group.add(bevy::winit::WinitPlugin::default());
group.add(bevy::wgpu::WgpuPlugin::default());
}
}
#[derive(Default)]
pub struct CustomPbrPlugin;
impl Plugin for CustomPbrPlugin {
fn build(&self, app: &mut AppBuilder) {
app.add_asset::<StandardMaterial>()
.register_type::<Light>()
.add_system_to_stage(
CoreStage::PostUpdate,
bevy::render::shader::asset_shader_defs_system::<StandardMaterial>.system(),
)
.init_resource::<bevy::pbr::AmbientLight>();
add_pbr_graph(app.world_mut());
// add default StandardMaterial
let mut materials = app
.world_mut()
.get_resource_mut::<Assets<StandardMaterial>>()
.unwrap();
materials.set_untracked(
Handle::<StandardMaterial>::default(),
StandardMaterial {
base_color: Color::PINK,
unlit: true,
..Default::default()
},
);
}
}
/// the names of pbr graph nodes
mod node {
pub const TRANSFORM: &str = "transform";
pub const STANDARD_MATERIAL: &str = "standard_material";
pub const LIGHTS: &str = "lights";
}
fn add_pbr_graph(world: &mut World) {
{
let mut graph = world.get_resource_mut::<RenderGraph>().unwrap();
graph.add_system_node(
node::TRANSFORM,
RenderResourcesNode::<GlobalTransform>::new(true),
);
graph.add_system_node(
node::STANDARD_MATERIAL,
AssetRenderResourcesNode::<StandardMaterial>::new(true),
);
graph.add_system_node(node::LIGHTS, LightsNode::new(30));
// TODO: replace these with "autowire" groups
graph
.add_node_edge(node::STANDARD_MATERIAL, base::node::MAIN_PASS)
.unwrap();
graph
.add_node_edge(node::TRANSFORM, base::node::MAIN_PASS)
.unwrap();
graph
.add_node_edge(node::LIGHTS, base::node::MAIN_PASS)
.unwrap();
}
let pipeline = build_pbr_pipeline(&mut world.get_resource_mut::<Assets<Shader>>().unwrap());
let mut pipelines = world
.get_resource_mut::<Assets<PipelineDescriptor>>()
.unwrap();
pipelines.set_untracked(PBR_PIPELINE_HANDLE, pipeline);
}