From 2aa76309dd1ec4d3a5c66577b683ad791d7936b0 Mon Sep 17 00:00:00 2001 From: Robin KAY Date: Fri, 5 Aug 2022 02:40:04 +0100 Subject: [PATCH] Move outline rendering to dedicated pass. --- Cargo.toml | 5 +- examples/{torus.rs => shapes.rs} | 57 +++-- src/common.wgsl | 15 ++ src/draw.rs | 143 +++++++++++ src/lib.rs | 422 +++++++------------------------ src/node.rs | 264 +++++++++++++++++++ src/outline.wgsl | 28 +- src/pipeline.rs | 180 +++++++++++++ src/stencil.wgsl | 22 ++ src/uniforms.rs | 104 ++++++++ src/view_uniforms.rs | 85 +++++++ 11 files changed, 963 insertions(+), 362 deletions(-) rename examples/{torus.rs => shapes.rs} (59%) create mode 100644 src/common.wgsl create mode 100644 src/draw.rs create mode 100644 src/node.rs create mode 100644 src/pipeline.rs create mode 100644 src/stencil.wgsl create mode 100644 src/uniforms.rs create mode 100644 src/view_uniforms.rs diff --git a/Cargo.toml b/Cargo.toml index 76876a6..18382c9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,7 +15,6 @@ bevy = { version = "0.8", default-features = false, features = [ "bevy_asset", "render", ] } -libm = "0.2" [dev-dependencies] bevy = { version = "0.8", default-features = false, features = [ @@ -24,5 +23,5 @@ bevy = { version = "0.8", default-features = false, features = [ ] } [[example]] -name = "torus" -path = "examples/torus.rs" +name = "shapes" +path = "examples/shapes.rs" \ No newline at end of file diff --git a/examples/torus.rs b/examples/shapes.rs similarity index 59% rename from examples/torus.rs rename to examples/shapes.rs index 3514773..34a2af5 100644 --- a/examples/torus.rs +++ b/examples/shapes.rs @@ -1,4 +1,4 @@ -use std::f32::consts::TAU; +use std::f32::consts::{PI, TAU}; use bevy::prelude::{shape::Torus, *}; @@ -12,20 +12,22 @@ fn main() { .add_plugins(DefaultPlugins) .add_plugin(OutlinePlugin) .add_startup_system(setup) - .add_system(rotate_cube) + .add_system(wobble) + .add_system(orbit) .run(); } #[derive(Component)] -struct TheCube(); +struct Wobbles; + +#[derive(Component)] +struct Orbits; fn setup( mut commands: Commands, mut meshes: ResMut>, - mut outlines: ResMut>, mut materials: ResMut>, ) { - // Spawn cube et al. commands.spawn_bundle(PbrBundle { mesh: meshes.add(Mesh::from(bevy::prelude::shape::Plane { size: 5.0 })), material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()), @@ -43,11 +45,31 @@ fn setup( transform: Transform::from_xyz(0.0, 1.0, 0.0), ..default() }) - .insert(outlines.add(Outline { - colour: Color::rgba(0.0, 1.0, 0.0, 0.5), + .insert(Outline { + colour: Color::rgba(0.0, 1.0, 0.0, 1.0), width: 25.0, - })) - .insert(TheCube()); + }) + .insert(OutlineStencil) + .insert(Wobbles); + commands + .spawn_bundle(PbrBundle { + mesh: meshes.add(Mesh::from(Torus { + radius: 0.3, + ring_radius: 0.1, + subdivisions_segments: 20, + subdivisions_sides: 10, + })), + material: materials.add(Color::rgb(0.9, 0.1, 0.1).into()), + transform: Transform::from_xyz(0.0, 1.2, 2.0) + .with_rotation(Quat::from_rotation_x(0.5 * PI)), + ..default() + }) + .insert(Outline { + colour: Color::rgba(1.0, 0.0, 1.0, 0.3), + width: 15.0, + }) + .insert(OutlineStencil) + .insert(Orbits); commands.spawn_bundle(PointLightBundle { point_light: PointLight { intensity: 1500.0, @@ -63,17 +85,13 @@ fn setup( }); } -fn rotate_cube( - mut cubes: Query<&mut Transform, With>, - timer: Res