komadori / bevy_mod_outline

Apache License 2.0
119 stars 10 forks source link

Does this work in 2D #24

Closed tmayoff closed 6 months ago

tmayoff commented 12 months ago

I'm trying to add some outlines for sprites, some 2D meshes and even text. My attempts have not worked, am I doing something wrong?

use bevy::{
    prelude::{shape::RegularPolygon, *},
    window::close_on_esc,
};
use bevy_mod_outline::{OutlineBundle, OutlineMeshExt, OutlinePlugin, OutlineVolume};

#[derive(Component)]
struct Wobbles;

#[derive(Component)]
struct Orbits;

#[bevy_main]
fn main() {
    App::new()
        .insert_resource(Msaa::Sample4)
        .insert_resource(ClearColor(Color::BLACK))
        .add_plugins((DefaultPlugins, OutlinePlugin))
        .add_systems(Startup, setup)
        .add_systems(Update, (close_on_esc))
        .run();
}

fn setup(
    mut commands: Commands,
    mut meshes: ResMut<Assets<Mesh>>,
    mut materials: ResMut<Assets<ColorMaterial>>,
) {
    let mut mesh = Mesh::from(RegularPolygon::new(50.0, 6));
    mesh.generate_outline_normals().unwrap();

    commands
        .spawn(ColorMesh2dBundle {
            mesh: meshes.add(mesh).into(),
            material: materials.add(Color::BLUE.into()),
            ..default()
        })
        .insert(OutlineBundle {
            outline: OutlineVolume {
                visible: true,
                colour: Color::rgba(0.0, 1.0, 0.0, 1.0),
                width: 60.0,
            },
            ..default()
        });

    commands.spawn(Camera2dBundle::default());
}
tmayoff commented 11 months ago

I did some messing around with this plugin, and managed to get fairly far into getting it working for 2D elements, however I got stuck at getting this to run..

The view_entity was None because of the Read<ViewDepthTexture> in the OutlineNode, I guess the 2D pipeline doesn't have DepthTexture, and not sure how to remove the requirement on it (if it's possible since, this plugin seems to rely on the depth information)

impl Node for OutlineNode {
    fn update(&mut self, world: &mut World) {
        self.query.update_archetypes(world);
    }

    fn run(
        &self,
        graph: &mut RenderGraphContext,
        render_context: &mut RenderContext,
        world: &World,
    ) -> Result<(), NodeRunError> {
        // TODO This is where it goes wrong the view_entity seems to always be None
        let view_entity = graph.view_entity();
        let (camera, stencil_phase, opaque_phase, transparent_phase, _, target, depth) =
            match self.query.get_manual(world, view_entity) {
                Ok(query) => query,
                Err(_) => {
                    return Ok(());
                } // No window
            };
komadori commented 6 months ago

While the same principal could be applied to 2D meshes, the 2D pipeline is internally separate within Bevy so that AFAIK this plugin cannot (currently) affect 2D entities. I don't have any plans to support this given I don't use the 2D pipeline myself, but would be open to a PR.