Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip for 0.13 #10

Draft
wants to merge 1 commit 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
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bevy_toon_shader"
version = "0.3.0"
version = "0.4.0"
edition = "2021"
categories = ["game-engines", "graphics", "rendering", "game-development"]
keywords = ["gamedev", "graphics", "bevy", "shader", "toon"]
Expand All @@ -11,12 +11,12 @@ repository = "https://github.com/tbillington/bevy_toon_shader"
homepage = "https://github.com/tbillington/bevy_toon_shader"

[dependencies]
bevy = { version = "0.12", default-features = false, features = [
bevy = { version = "0.13", default-features = false, features = [
"bevy_pbr",
"bevy_render",
"bevy_asset",
] }

[dev-dependencies]
bevy = "0.12"
bevy_egui = "0.23.0"
bevy = "0.13"
bevy_egui = "0.25.0"
99 changes: 53 additions & 46 deletions examples/scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::f32::consts::PI;

use bevy::{prelude::*, window::close_on_esc};
use bevy_egui::{egui, EguiContexts, EguiPlugin};
// use bevy_egui::{egui, EguiContexts, EguiPlugin};
use bevy_toon_shader::{ToonShaderMainCamera, ToonShaderMaterial, ToonShaderPlugin, ToonShaderSun};

fn main() {
Expand All @@ -20,9 +20,16 @@ fn main() {
}),
)
.add_plugins(ToonShaderPlugin)
.add_plugins(EguiPlugin)
// .add_plugins(EguiPlugin)
.add_systems(Startup, setup)
.add_systems(Update, (ui_example_system, rotate_shapes, close_on_esc))
.add_systems(
Update,
(
// ui_example_system,
rotate_shapes,
close_on_esc,
),
)
.run();
}

Expand Down Expand Up @@ -76,13 +83,12 @@ fn setup(
let toon_material = toon_materials.add(ToonShaderMaterial::default());

let shapes = [
meshes.add(shape::Cube::default().into()),
meshes.add(shape::Box::default().into()),
meshes.add(shape::Capsule::default().into()),
meshes.add(shape::Torus::default().into()),
meshes.add(shape::Cylinder::default().into()),
meshes.add(shape::Icosphere::default().try_into().unwrap()),
meshes.add(shape::UVSphere::default().into()),
meshes.add(Cuboid::default()),
meshes.add(Capsule3d::default()),
meshes.add(Torus::default()),
meshes.add(Cylinder::default()),
meshes.add(Sphere::default().mesh().ico(5).unwrap()),
meshes.add(Sphere::default().mesh().uv(32, 18)),
];

let num_shapes = shapes.len();
Expand Down Expand Up @@ -123,46 +129,46 @@ fn setup(
}

commands.spawn(PbrBundle {
mesh: meshes.add(shape::Plane::from_size(50.0).into()),
material: materials.add(Color::SILVER.into()),
mesh: meshes.add(Plane3d::default().mesh().size(50.0, 50.0)),
material: materials.add(Color::SILVER),
..default()
});
}

fn ui_example_system(
mut contexts: EguiContexts,
mut ambient_light: Option<ResMut<AmbientLight>>,
mut controls: ParamSet<(
// Camera position
Query<&Transform, With<ToonShaderMainCamera>>,
// Sun position
Query<(&mut Transform, &DirectionalLight), With<ToonShaderSun>>,
)>,
) {
egui::Window::new("Controls").show(contexts.ctx_mut(), |ui| {
if let Some(ambient_light) = ambient_light.as_mut() {
ui.heading("Ambient Light");
let mut orig = ambient_light.color.as_rgba_f32();
if ui.color_edit_button_rgba_unmultiplied(&mut orig).changed() {
ambient_light.color = Color::from(orig);
}
}

if let Ok((mut t, _)) = controls.p1().get_single_mut() {
ui.heading("Sun");
ui.horizontal(|ui| {
ui.label("Angle");

let (mut x, mut y, z) = t.rotation.to_euler(EulerRot::XYZ);
x = x.to_degrees();
y = y.to_degrees();
ui.add(egui::widgets::DragValue::new(&mut x).speed(1.));
ui.add(egui::widgets::DragValue::new(&mut y).speed(1.));
t.rotation = Quat::from_euler(EulerRot::XYZ, x.to_radians(), y.to_radians(), z);
});
}
});
}
// fn ui_example_system(
// mut contexts: EguiContexts,
// mut ambient_light: Option<ResMut<AmbientLight>>,
// mut controls: ParamSet<(
// // Camera position
// Query<&Transform, With<ToonShaderMainCamera>>,
// // Sun position
// Query<(&mut Transform, &DirectionalLight), With<ToonShaderSun>>,
// )>,
// ) {
// egui::Window::new("Controls").show(contexts.ctx_mut(), |ui| {
// if let Some(ambient_light) = ambient_light.as_mut() {
// ui.heading("Ambient Light");
// let mut orig = ambient_light.color.as_rgba_f32();
// if ui.color_edit_button_rgba_unmultiplied(&mut orig).changed() {
// ambient_light.color = Color::rgba_from_array(orig);
// }
// }

// if let Ok((mut t, _)) = controls.p1().get_single_mut() {
// ui.heading("Sun");
// ui.horizontal(|ui| {
// ui.label("Angle");

// let (mut x, mut y, z) = t.rotation.to_euler(EulerRot::XYZ);
// x = x.to_degrees();
// y = y.to_degrees();
// ui.add(egui::widgets::DragValue::new(&mut x).speed(1.));
// ui.add(egui::widgets::DragValue::new(&mut y).speed(1.));
// t.rotation = Quat::from_euler(EulerRot::XYZ, x.to_radians(), y.to_radians(), z);
// });
// }
// });
// }

#[derive(Component)]
struct Shape;
Expand Down Expand Up @@ -198,5 +204,6 @@ fn uv_debug_texture() -> Image {
bevy::render::render_resource::TextureDimension::D2,
&texture_data,
bevy::render::render_resource::TextureFormat::Rgba8UnormSrgb,
bevy::render::render_asset::RenderAssetUsages::default(),
)
}
15 changes: 8 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use bevy::{
asset::load_internal_asset,
pbr::{MaterialPipeline, MaterialPipelineKey},
prelude::*,
reflect::{TypePath, TypeUuid},
reflect::TypePath,
render::{
mesh::MeshVertexBufferLayout,
render_resource::{
Expand Down Expand Up @@ -30,8 +30,9 @@ impl Plugin for ToonShaderPlugin {
}
}

#[derive(Asset, AsBindGroup, TypeUuid, TypePath, Debug, Clone, Default)]
#[uuid = "7b033895-875f-4cb5-97ae-8601fcc37053"]
#[derive(Asset, AsBindGroup, TypePath, Debug, Clone, Default)]
// #[uuid = "7b033895-875f-4cb5-97ae-8601fcc37053"]
#[type_path = "bevy_toon_shader::ToonShaderMaterial"]
#[uniform(0, ToonShaderMaterialUniform)]
pub struct ToonShaderMaterial {
pub color: Color,
Expand Down Expand Up @@ -65,11 +66,11 @@ impl AsBindGroupShaderType<ToonShaderMaterialUniform> for ToonShaderMaterial {
_images: &bevy::render::render_asset::RenderAssets<Image>,
) -> ToonShaderMaterialUniform {
ToonShaderMaterialUniform {
color: self.color.into(),
color: self.color.rgba_to_vec4(),
sun_dir: self.sun_dir,
sun_color: self.sun_color.into(),
sun_color: self.sun_color.rgba_to_vec4(),
camera_pos: self.camera_pos,
ambient_color: self.ambient_color.into(),
ambient_color: self.ambient_color.rgba_to_vec4(),
}
}
}
Expand Down Expand Up @@ -109,7 +110,7 @@ pub fn update_toon_shader(
toon_mat.camera_pos = cam_t.translation;
}
if let Ok((sun_t, dir_light)) = sun.get_single() {
toon_mat.sun_dir = sun_t.back();
toon_mat.sun_dir = sun_t.back().into();
toon_mat.sun_color = dir_light.color;
}
if let Some(light) = &ambient_light {
Expand Down
6 changes: 3 additions & 3 deletions src/toon_shader.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ struct ToonShaderMaterial {
ambient_color: vec4<f32>,
};

@group(1) @binding(0)
@group(2) @binding(0)
var<uniform> material: ToonShaderMaterial;
@group(1) @binding(1)
@group(2) @binding(1)
var base_color_texture: texture_2d<f32>;
@group(1) @binding(2)
@group(2) @binding(2)
var base_color_sampler: sampler;

#import bevy_pbr::forward_io::VertexOutput
Expand Down