Skip to content
This repository has been archived by the owner on Nov 22, 2023. It is now read-only.

Add toon shader from bevy_toon_shader #745

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions emergence_lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 4 additions & 1 deletion emergence_lib/src/graphics/lighting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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
Expand Down
41 changes: 38 additions & 3 deletions emergence_lib/src/graphics/mod.rs
Original file line number Diff line number Diff line change
@@ -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,
};

Expand All @@ -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);
}
}

Expand All @@ -51,3 +57,32 @@ pub(super) fn inherit_materials(
}
}
}

fn spawn_debug_toon_shaded_cube(
mut commands: Commands,
mut mesh_assets: ResMut<Assets<Mesh>>,
mut material_assets: ResMut<Assets<ToonShaderMaterial>>,
) {
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()
});
}
2 changes: 2 additions & 0 deletions emergence_lib/src/player_interaction/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -78,6 +79,7 @@ fn setup_camera(mut commands: Commands) {
})
.insert(settings)
.insert(focus)
.insert(ToonShaderMainCamera)
.insert(RaycastSource::<Terrain>::new())
.insert(RaycastSource::<Structure>::new())
.insert(RaycastSource::<Unit>::new())
Expand Down