2020-07-17 00:20:42 +00:00
|
|
|
use bevy::{
|
2021-12-14 03:58:23 +00:00
|
|
|
core_pipeline::Transparent3d,
|
|
|
|
pbr::{
|
|
|
|
DrawMesh, MeshPipeline, MeshPipelineKey, MeshUniform, SetMeshBindGroup,
|
|
|
|
SetMeshViewBindGroup,
|
|
|
|
},
|
2020-07-17 00:20:42 +00:00
|
|
|
prelude::*,
|
|
|
|
render::{
|
2021-12-20 20:33:39 +00:00
|
|
|
render_asset::RenderAssets,
|
2021-12-14 03:58:23 +00:00
|
|
|
render_component::{ExtractComponent, ExtractComponentPlugin},
|
|
|
|
render_phase::{AddRenderCommand, DrawFunctions, RenderPhase, SetItemPipeline},
|
|
|
|
render_resource::{
|
|
|
|
RenderPipelineCache, RenderPipelineDescriptor, SpecializedPipeline,
|
|
|
|
SpecializedPipelines,
|
|
|
|
},
|
|
|
|
view::ExtractedView,
|
|
|
|
RenderApp, RenderStage,
|
2020-07-17 00:20:42 +00:00
|
|
|
},
|
2020-07-10 08:37:06 +00:00
|
|
|
};
|
2020-03-29 08:34:08 +00:00
|
|
|
|
2021-12-14 03:58:23 +00:00
|
|
|
pub struct IsRedPlugin;
|
|
|
|
|
|
|
|
impl Plugin for IsRedPlugin {
|
|
|
|
fn build(&self, app: &mut App) {
|
|
|
|
app.add_plugin(ExtractComponentPlugin::<IsRed>::default());
|
2021-12-24 06:57:30 +00:00
|
|
|
app.sub_app_mut(RenderApp)
|
2021-12-14 03:58:23 +00:00
|
|
|
.add_render_command::<Transparent3d, DrawIsRed>()
|
|
|
|
.init_resource::<IsRedPipeline>()
|
|
|
|
.init_resource::<SpecializedPipelines<IsRedPipeline>>()
|
|
|
|
.add_system_to_stage(RenderStage::Queue, queue_custom);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-29 08:34:08 +00:00
|
|
|
fn main() {
|
2021-07-27 20:21:06 +00:00
|
|
|
App::new()
|
2020-11-03 03:01:17 +00:00
|
|
|
.add_plugins(DefaultPlugins)
|
2021-12-14 03:58:23 +00:00
|
|
|
.add_plugin(IsRedPlugin)
|
2021-07-27 23:42:36 +00:00
|
|
|
.add_startup_system(setup)
|
2020-04-12 03:27:46 +00:00
|
|
|
.run();
|
2020-03-29 08:34:08 +00:00
|
|
|
}
|
|
|
|
|
2021-12-14 03:58:23 +00:00
|
|
|
#[derive(Component, Hash, PartialEq, Eq, Copy, Clone)]
|
|
|
|
struct IsRed(bool);
|
2020-03-29 08:34:08 +00:00
|
|
|
|
2021-12-14 03:58:23 +00:00
|
|
|
impl ExtractComponent for IsRed {
|
|
|
|
type Query = &'static IsRed;
|
2020-06-05 07:13:18 +00:00
|
|
|
|
2021-12-14 03:58:23 +00:00
|
|
|
type Filter = ();
|
2020-06-05 07:13:18 +00:00
|
|
|
|
2021-12-14 03:58:23 +00:00
|
|
|
fn extract_component(item: bevy::ecs::query::QueryItem<Self::Query>) -> Self {
|
|
|
|
*item
|
|
|
|
}
|
2020-06-05 07:13:18 +00:00
|
|
|
}
|
2020-06-18 00:27:49 +00:00
|
|
|
|
2021-12-14 03:58:23 +00:00
|
|
|
/// set up a simple 3D scene
|
|
|
|
fn setup(mut commands: Commands, mut meshes: ResMut<Assets<Mesh>>) {
|
|
|
|
// red cube
|
|
|
|
commands.spawn().insert_bundle((
|
|
|
|
meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
|
|
|
|
IsRed(true),
|
|
|
|
Transform::from_xyz(-1.0, 0.5, 0.0),
|
|
|
|
GlobalTransform::default(),
|
|
|
|
Visibility::default(),
|
|
|
|
ComputedVisibility::default(),
|
|
|
|
));
|
2020-03-29 08:34:08 +00:00
|
|
|
|
2021-12-14 03:58:23 +00:00
|
|
|
// blue cube
|
|
|
|
commands.spawn().insert_bundle((
|
|
|
|
meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
|
|
|
|
IsRed(false),
|
|
|
|
Transform::from_xyz(1.0, 0.5, 0.0),
|
|
|
|
GlobalTransform::default(),
|
|
|
|
Visibility::default(),
|
|
|
|
ComputedVisibility::default(),
|
|
|
|
));
|
2020-03-29 08:34:08 +00:00
|
|
|
|
2021-03-23 00:23:40 +00:00
|
|
|
// camera
|
|
|
|
commands.spawn_bundle(PerspectiveCameraBundle {
|
2021-12-14 03:58:23 +00:00
|
|
|
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
|
2021-03-23 00:23:40 +00:00
|
|
|
..Default::default()
|
|
|
|
});
|
2020-03-29 08:34:08 +00:00
|
|
|
}
|
2021-12-14 03:58:23 +00:00
|
|
|
|
|
|
|
struct IsRedPipeline {
|
2022-01-24 23:44:08 +00:00
|
|
|
mesh_pipeline: MeshPipeline,
|
2021-12-14 03:58:23 +00:00
|
|
|
shader: Handle<Shader>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromWorld for IsRedPipeline {
|
|
|
|
fn from_world(world: &mut World) -> Self {
|
|
|
|
let asset_server = world.get_resource::<AssetServer>().unwrap();
|
|
|
|
let mesh_pipeline = world.get_resource::<MeshPipeline>().unwrap();
|
|
|
|
let shader = asset_server.load("shaders/shader_defs.wgsl");
|
|
|
|
IsRedPipeline {
|
2022-01-24 23:44:08 +00:00
|
|
|
mesh_pipeline: mesh_pipeline.clone(),
|
2021-12-14 03:58:23 +00:00
|
|
|
shader,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SpecializedPipeline for IsRedPipeline {
|
|
|
|
type Key = (IsRed, MeshPipelineKey);
|
|
|
|
|
|
|
|
fn specialize(&self, (is_red, pbr_pipeline_key): Self::Key) -> RenderPipelineDescriptor {
|
|
|
|
let mut shader_defs = Vec::new();
|
|
|
|
if is_red.0 {
|
|
|
|
shader_defs.push("IS_RED".to_string());
|
|
|
|
}
|
2022-01-24 23:44:08 +00:00
|
|
|
let mut descriptor = self.mesh_pipeline.specialize(pbr_pipeline_key);
|
2021-12-14 03:58:23 +00:00
|
|
|
descriptor.vertex.shader = self.shader.clone();
|
|
|
|
descriptor.vertex.shader_defs = shader_defs.clone();
|
|
|
|
let fragment = descriptor.fragment.as_mut().unwrap();
|
|
|
|
fragment.shader = self.shader.clone();
|
|
|
|
fragment.shader_defs = shader_defs;
|
|
|
|
descriptor.layout = Some(vec![
|
2022-01-24 23:44:08 +00:00
|
|
|
self.mesh_pipeline.view_layout.clone(),
|
|
|
|
self.mesh_pipeline.mesh_layout.clone(),
|
2021-12-14 03:58:23 +00:00
|
|
|
]);
|
|
|
|
descriptor
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type DrawIsRed = (
|
|
|
|
SetItemPipeline,
|
|
|
|
SetMeshViewBindGroup<0>,
|
|
|
|
SetMeshBindGroup<1>,
|
|
|
|
DrawMesh,
|
|
|
|
);
|
|
|
|
|
2021-12-20 20:33:39 +00:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2021-12-14 03:58:23 +00:00
|
|
|
fn queue_custom(
|
|
|
|
transparent_3d_draw_functions: Res<DrawFunctions<Transparent3d>>,
|
2021-12-20 20:33:39 +00:00
|
|
|
render_meshes: Res<RenderAssets<Mesh>>,
|
2021-12-14 03:58:23 +00:00
|
|
|
custom_pipeline: Res<IsRedPipeline>,
|
|
|
|
msaa: Res<Msaa>,
|
|
|
|
mut pipelines: ResMut<SpecializedPipelines<IsRedPipeline>>,
|
|
|
|
mut pipeline_cache: ResMut<RenderPipelineCache>,
|
2021-12-20 20:33:39 +00:00
|
|
|
material_meshes: Query<(Entity, &Handle<Mesh>, &MeshUniform, &IsRed)>,
|
2021-12-14 03:58:23 +00:00
|
|
|
mut views: Query<(&ExtractedView, &mut RenderPhase<Transparent3d>)>,
|
|
|
|
) {
|
|
|
|
let draw_custom = transparent_3d_draw_functions
|
|
|
|
.read()
|
|
|
|
.get_id::<DrawIsRed>()
|
|
|
|
.unwrap();
|
|
|
|
let key = MeshPipelineKey::from_msaa_samples(msaa.samples);
|
|
|
|
for (view, mut transparent_phase) in views.iter_mut() {
|
|
|
|
let view_matrix = view.transform.compute_matrix();
|
|
|
|
let view_row_2 = view_matrix.row(2);
|
2021-12-20 20:33:39 +00:00
|
|
|
for (entity, mesh_handle, mesh_uniform, is_red) in material_meshes.iter() {
|
|
|
|
if let Some(mesh) = render_meshes.get(mesh_handle) {
|
|
|
|
let key = key | MeshPipelineKey::from_primitive_topology(mesh.primitive_topology);
|
|
|
|
let pipeline =
|
|
|
|
pipelines.specialize(&mut pipeline_cache, &custom_pipeline, (*is_red, key));
|
|
|
|
transparent_phase.add(Transparent3d {
|
|
|
|
entity,
|
|
|
|
pipeline,
|
|
|
|
draw_function: draw_custom,
|
|
|
|
distance: view_row_2.dot(mesh_uniform.transform.col(3)),
|
|
|
|
});
|
|
|
|
}
|
2021-12-14 03:58:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|