use crate::{Material2d, Material2dKey, Material2dPlugin, Mesh2dHandle};
use bevy_app::{Plugin, Startup, Update};
use bevy_asset::{load_internal_asset, Asset, Assets, Handle};
use bevy_color::{LinearRgba, Srgba};
use bevy_ecs::prelude::*;
use bevy_reflect::{std_traits::ReflectDefault, Reflect, TypePath};
use bevy_render::{
extract_resource::ExtractResource, mesh::MeshVertexBufferLayoutRef, prelude::*,
render_resource::*,
};
pub const WIREFRAME_2D_SHADER_HANDLE: Handle<Shader> = Handle::weak_from_u128(6920362697190520314);
#[derive(Debug, Default)]
pub struct Wireframe2dPlugin;
impl Plugin for Wireframe2dPlugin {
fn build(&self, app: &mut bevy_app::App) {
load_internal_asset!(
app,
WIREFRAME_2D_SHADER_HANDLE,
"wireframe2d.wgsl",
Shader::from_wgsl
);
app.register_type::<Wireframe2d>()
.register_type::<NoWireframe2d>()
.register_type::<Wireframe2dConfig>()
.register_type::<Wireframe2dColor>()
.init_resource::<Wireframe2dConfig>()
.add_plugins(Material2dPlugin::<Wireframe2dMaterial>::default())
.add_systems(Startup, setup_global_wireframe_material)
.add_systems(
Update,
(
global_color_changed.run_if(resource_changed::<Wireframe2dConfig>),
wireframe_color_changed,
(apply_wireframe_material, apply_global_wireframe_material).chain(),
),
);
}
}
#[derive(Component, Debug, Clone, Default, Reflect, Eq, PartialEq)]
#[reflect(Component, Default)]
pub struct Wireframe2d;
#[derive(Component, Debug, Clone, Default, Reflect)]
#[reflect(Component, Default)]
pub struct Wireframe2dColor {
pub color: Srgba,
}
#[derive(Component, Debug, Clone, Default, Reflect, Eq, PartialEq)]
#[reflect(Component, Default)]
pub struct NoWireframe2d;
#[derive(Resource, Debug, Clone, Default, ExtractResource, Reflect)]
#[reflect(Resource)]
pub struct Wireframe2dConfig {
pub global: bool,
pub default_color: Srgba,
}
#[derive(Resource)]
struct GlobalWireframe2dMaterial {
handle: Handle<Wireframe2dMaterial>,
}
fn setup_global_wireframe_material(
mut commands: Commands,
mut materials: ResMut<Assets<Wireframe2dMaterial>>,
config: Res<Wireframe2dConfig>,
) {
commands.insert_resource(GlobalWireframe2dMaterial {
handle: materials.add(Wireframe2dMaterial {
color: config.default_color.into(),
}),
});
}
fn global_color_changed(
config: Res<Wireframe2dConfig>,
mut materials: ResMut<Assets<Wireframe2dMaterial>>,
global_material: Res<GlobalWireframe2dMaterial>,
) {
if let Some(global_material) = materials.get_mut(&global_material.handle) {
global_material.color = config.default_color.into();
}
}
#[allow(clippy::type_complexity)]
fn wireframe_color_changed(
mut materials: ResMut<Assets<Wireframe2dMaterial>>,
mut colors_changed: Query<
(&mut Handle<Wireframe2dMaterial>, &Wireframe2dColor),
(With<Wireframe2d>, Changed<Wireframe2dColor>),
>,
) {
for (mut handle, wireframe_color) in &mut colors_changed {
*handle = materials.add(Wireframe2dMaterial {
color: wireframe_color.color.into(),
});
}
}
fn apply_wireframe_material(
mut commands: Commands,
mut materials: ResMut<Assets<Wireframe2dMaterial>>,
wireframes: Query<
(Entity, Option<&Wireframe2dColor>),
(With<Wireframe2d>, Without<Handle<Wireframe2dMaterial>>),
>,
no_wireframes: Query<Entity, (With<NoWireframe2d>, With<Handle<Wireframe2dMaterial>>)>,
mut removed_wireframes: RemovedComponents<Wireframe2d>,
global_material: Res<GlobalWireframe2dMaterial>,
) {
for e in removed_wireframes.read().chain(no_wireframes.iter()) {
if let Some(mut commands) = commands.get_entity(e) {
commands.remove::<Handle<Wireframe2dMaterial>>();
}
}
let mut wireframes_to_spawn = vec![];
for (e, wireframe_color) in &wireframes {
let material = if let Some(wireframe_color) = wireframe_color {
materials.add(Wireframe2dMaterial {
color: wireframe_color.color.into(),
})
} else {
global_material.handle.clone()
};
wireframes_to_spawn.push((e, material));
}
commands.insert_or_spawn_batch(wireframes_to_spawn);
}
type Wireframe2dFilter = (
With<Mesh2dHandle>,
Without<Wireframe2d>,
Without<NoWireframe2d>,
);
fn apply_global_wireframe_material(
mut commands: Commands,
config: Res<Wireframe2dConfig>,
meshes_without_material: Query<
Entity,
(Wireframe2dFilter, Without<Handle<Wireframe2dMaterial>>),
>,
meshes_with_global_material: Query<
Entity,
(Wireframe2dFilter, With<Handle<Wireframe2dMaterial>>),
>,
global_material: Res<GlobalWireframe2dMaterial>,
) {
if config.global {
let mut material_to_spawn = vec![];
for e in &meshes_without_material {
material_to_spawn.push((e, global_material.handle.clone()));
}
commands.insert_or_spawn_batch(material_to_spawn);
} else {
for e in &meshes_with_global_material {
commands.entity(e).remove::<Handle<Wireframe2dMaterial>>();
}
}
}
#[derive(Default, AsBindGroup, TypePath, Debug, Clone, Asset)]
pub struct Wireframe2dMaterial {
#[uniform(0)]
pub color: LinearRgba,
}
impl Material2d for Wireframe2dMaterial {
fn fragment_shader() -> ShaderRef {
WIREFRAME_2D_SHADER_HANDLE.into()
}
fn depth_bias(&self) -> f32 {
1.0
}
fn specialize(
descriptor: &mut RenderPipelineDescriptor,
_layout: &MeshVertexBufferLayoutRef,
_key: Material2dKey<Self>,
) -> Result<(), SpecializedMeshPipelineError> {
descriptor.primitive.polygon_mode = PolygonMode::Line;
Ok(())
}
}