From a543a65b4cd0a92916aad5e263c2b3cf3b0db8bf Mon Sep 17 00:00:00 2001 From: Alice Cecile Date: Wed, 19 Apr 2023 11:18:25 -0400 Subject: [PATCH 1/2] Add bevy_toon_shader to Cargo.toml --- Cargo.lock | 10 ++++++++++ emergence_lib/Cargo.toml | 1 + 2 files changed, 11 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 41854c47c..b96c2a88e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -989,6 +989,15 @@ dependencies = [ "thiserror", ] +[[package]] +name = "bevy_toon_shader" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a314af8d43fd4241aa248f6ebb6d37410a8a78175db86434b7e19f4f016584b2" +dependencies = [ + "bevy", +] + [[package]] name = "bevy_transform" version = "0.10.1" @@ -1693,6 +1702,7 @@ dependencies = [ "bevy_mod_billboard", "bevy_mod_raycast", "bevy_screen_diagnostics", + "bevy_toon_shader", "criterion", "debug_tools", "derive_more", diff --git a/emergence_lib/Cargo.toml b/emergence_lib/Cargo.toml index 154a7d263..bd7e27cac 100644 --- a/emergence_lib/Cargo.toml +++ b/emergence_lib/Cargo.toml @@ -32,6 +32,7 @@ anyhow = "1.0.69" serde_json = "1.0.94" hashbrown = { version = "0.12", features = ["rayon"] } rayon = "1.7.0" +bevy_toon_shader = "0.1.0" [dev-dependencies] criterion = "0.4" From 092d559c58565b5b96875ec0bf8d4765a54ee1c3 Mon Sep 17 00:00:00 2001 From: Alice Cecile Date: Wed, 19 Apr 2023 11:43:59 -0400 Subject: [PATCH 2/2] Set everything up and make a test cube --- emergence_lib/src/graphics/lighting.rs | 5 ++- emergence_lib/src/graphics/mod.rs | 41 +++++++++++++++++-- .../src/player_interaction/camera.rs | 2 + 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/emergence_lib/src/graphics/lighting.rs b/emergence_lib/src/graphics/lighting.rs index 81a59922f..c0159e81a 100644 --- a/emergence_lib/src/graphics/lighting.rs +++ b/emergence_lib/src/graphics/lighting.rs @@ -3,6 +3,7 @@ use std::f32::consts::PI; use bevy::prelude::*; +use bevy_toon_shader::ToonShaderSun; use crate::{ graphics::palette::lighting::{LIGHT_MOON, LIGHT_STARS, LIGHT_SUN}, @@ -146,7 +147,9 @@ fn spawn_celestial_bodies(mut commands: Commands) { ..default() }) .insert(sun) - .insert(PrimaryCelestialBody); + .insert(PrimaryCelestialBody) + // This causes the light direction to be updated automatically for the toon shaded materials + .insert(ToonShaderSun); let moon = CelestialBody::moon(); commands diff --git a/emergence_lib/src/graphics/mod.rs b/emergence_lib/src/graphics/mod.rs index 9ee28b3af..1a89ffb85 100644 --- a/emergence_lib/src/graphics/mod.rs +++ b/emergence_lib/src/graphics/mod.rs @@ -1,11 +1,15 @@ //! Rendering and animation logic. use bevy::prelude::*; +use bevy_toon_shader::{ToonShaderMaterial, ToonShaderPlugin}; use crate::asset_management::AssetState; use self::{ - atmosphere::AtmospherePlugin, lighting::LightingPlugin, structures::remove_ghostly_shadows, + atmosphere::AtmospherePlugin, + lighting::LightingPlugin, + palette::lighting::{LIGHT_STARS, LIGHT_SUN}, + structures::remove_ghostly_shadows, terrain::manage_litter_piles, }; @@ -23,13 +27,15 @@ pub struct GraphicsPlugin; impl Plugin for GraphicsPlugin { fn build(&self, app: &mut App) { - app.add_plugin(LightingPlugin) + app.add_plugin(ToonShaderPlugin) + .add_plugin(LightingPlugin) .add_plugin(AtmospherePlugin) .add_system(manage_litter_piles.run_if(in_state(AssetState::FullyLoaded))) // Run these after Update to avoid panics due to despawned entities .add_systems( (inherit_materials, remove_ghostly_shadows).in_base_set(CoreSet::PostUpdate), - ); + ) + .add_startup_system(spawn_debug_toon_shaded_cube); } } @@ -51,3 +57,32 @@ pub(super) fn inherit_materials( } } } + +fn spawn_debug_toon_shaded_cube( + mut commands: Commands, + mut mesh_assets: ResMut>, + mut material_assets: ResMut>, +) { + let mesh = Mesh::from(shape::Cube { size: 80.0 }); + let material = ToonShaderMaterial { + color: Color::rgb(0.5, 0.5, 0.5), + sun_color: LIGHT_SUN, + ambient_color: LIGHT_STARS, + // Automatically updated + sun_dir: Vec3::default(), + // Automatically updated + camera_pos: Vec3::default(), + base_color_texture: None, + }; + + // Store the generated assets and get a handle to them + let mesh = mesh_assets.add(mesh); + let material = material_assets.add(material); + + commands.spawn(MaterialMeshBundle { + mesh, + material, + transform: Transform::default(), + ..Default::default() + }); +} diff --git a/emergence_lib/src/player_interaction/camera.rs b/emergence_lib/src/player_interaction/camera.rs index 3e761faea..337eba210 100644 --- a/emergence_lib/src/player_interaction/camera.rs +++ b/emergence_lib/src/player_interaction/camera.rs @@ -9,6 +9,7 @@ use bevy::input::mouse::MouseMotion; use bevy::input::mouse::MouseWheel; use bevy::prelude::*; use bevy_mod_raycast::RaycastSource; +use bevy_toon_shader::ToonShaderMainCamera; use leafwing_input_manager::orientation::Rotation; use leafwing_input_manager::prelude::ActionState; @@ -78,6 +79,7 @@ fn setup_camera(mut commands: Commands) { }) .insert(settings) .insert(focus) + .insert(ToonShaderMainCamera) .insert(RaycastSource::::new()) .insert(RaycastSource::::new()) .insert(RaycastSource::::new())