diff --git a/Cargo.toml b/Cargo.toml index e51a1c88e7784..af4f416784218 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -419,10 +419,6 @@ path = "examples/shader/shader_defs.rs" name = "shader_material" path = "examples/shader/shader_material.rs" -[[example]] -name = "custom_shader_manual" -path = "examples/shader/custom_shader_manual.rs" - [[example]] name = "shader_material_glsl" path = "examples/shader/shader_material_glsl.rs" diff --git a/examples/README.md b/examples/README.md index babf17b54c54a..84d61d12be916 100644 --- a/examples/README.md +++ b/examples/README.md @@ -216,7 +216,6 @@ Example | File | Description --- | --- | --- `shader_material` | [`shader/shader_material.rs`](./shader/shader_material.rs) | Illustrates creating a custom material and a shader that uses it `shader_material_glsl` | [`shader/shader_material_glsl.rs`](./shader/shader_material_glsl.rs) | A custom shader using the GLSL shading language. -`custom_shader_manual` | [`shader/custom_shader_manual.rs`](./shader/custom_shader_manual.rs) | Example of a custom shader which doesn't use the `MaterialPlugin` `shader_instancing` | [`shader/shader_instancing.rs`](./shader/shader_instancing.rs) | A custom shader showing off rendering a mesh multiple times in one draw call. `animate_shader` | [`shader/animate_shader.rs`](./shader/animate_shader.rs) | Shows how to pass changing data like the time since startup into a shader. `compute_shader_game_of_life` | [`shader/compute_shader_game_of_life.rs`](./shader/compute_shader_game_of_life.rs) | A compute shader simulating Conway's Game of Life diff --git a/examples/shader/custom_shader_manual.rs b/examples/shader/custom_shader_manual.rs deleted file mode 100644 index a0b098896e407..0000000000000 --- a/examples/shader/custom_shader_manual.rs +++ /dev/null @@ -1,143 +0,0 @@ -use bevy::{ - core_pipeline::Transparent3d, - pbr::{ - DrawMesh, MeshPipeline, MeshPipelineKey, MeshUniform, SetMeshBindGroup, - SetMeshViewBindGroup, - }, - prelude::*, - render::{ - render_phase::{AddRenderCommand, DrawFunctions, RenderPhase, SetItemPipeline}, - render_resource::*, - view::{ComputedVisibility, ExtractedView, Msaa, Visibility}, - RenderApp, RenderStage, - }, -}; - -fn main() { - App::new() - .add_plugins(DefaultPlugins) - .add_plugin(CustomMaterialPlugin) - .add_startup_system(setup) - .run(); -} - -fn setup(mut commands: Commands, mut meshes: ResMut>) { - // cube - commands.spawn().insert_bundle(( - meshes.add(Mesh::from(shape::Cube { size: 1.0 })), - Transform::from_xyz(0.0, 0.5, 0.0), - GlobalTransform::default(), - CustomMaterial, - Visibility::default(), - ComputedVisibility::default(), - )); - - // camera - commands.spawn_bundle(PerspectiveCameraBundle { - transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y), - ..Default::default() - }); -} - -#[derive(Component)] -struct CustomMaterial; - -pub struct CustomMaterialPlugin; - -impl Plugin for CustomMaterialPlugin { - fn build(&self, app: &mut App) { - app.sub_app(RenderApp) - .add_render_command::() - .init_resource::() - .init_resource::>() - .add_system_to_stage(RenderStage::Extract, extract_custom_material) - .add_system_to_stage(RenderStage::Queue, queue_custom); - } -} - -// extract the `CustomMaterial` component into the render world -fn extract_custom_material( - mut commands: Commands, - mut previous_len: Local, - mut query: Query>, -) { - let mut values = Vec::with_capacity(*previous_len); - for entity in query.iter_mut() { - values.push((entity, (CustomMaterial,))); - } - *previous_len = values.len(); - commands.insert_or_spawn_batch(values); -} - -// add each entity with a mesh and a `CustomMaterial` to every view's `Transparent3d` render phase using the `CustomPipeline` -fn queue_custom( - transparent_3d_draw_functions: Res>, - custom_pipeline: Res, - msaa: Res, - mut pipelines: ResMut>, - mut pipeline_cache: ResMut, - material_meshes: Query<(Entity, &MeshUniform), (With>, With)>, - mut views: Query<(&ExtractedView, &mut RenderPhase)>, -) { - let draw_custom = transparent_3d_draw_functions - .read() - .get_id::() - .unwrap(); - - let key = MeshPipelineKey::from_msaa_samples(msaa.samples); - let pipeline = pipelines.specialize(&mut pipeline_cache, &custom_pipeline, key); - - for (view, mut transparent_phase) in views.iter_mut() { - let view_matrix = view.transform.compute_matrix(); - let view_row_2 = view_matrix.row(2); - for (entity, mesh_uniform) in material_meshes.iter() { - transparent_phase.add(Transparent3d { - entity, - pipeline, - draw_function: draw_custom, - distance: view_row_2.dot(mesh_uniform.transform.col(3)), - }); - } - } -} - -pub struct CustomPipeline { - shader: Handle, - mesh_pipeline: MeshPipeline, -} - -impl FromWorld for CustomPipeline { - fn from_world(world: &mut World) -> Self { - let world = world.cell(); - let asset_server = world.get_resource::().unwrap(); - let shader = asset_server.load("shaders/custom_material_minimal.wgsl"); - let mesh_pipeline = world.get_resource::().unwrap(); - - CustomPipeline { - shader, - mesh_pipeline: mesh_pipeline.clone(), - } - } -} - -impl SpecializedPipeline for CustomPipeline { - type Key = MeshPipelineKey; - - fn specialize(&self, key: Self::Key) -> RenderPipelineDescriptor { - let mut descriptor = self.mesh_pipeline.specialize(key); - descriptor.vertex.shader = self.shader.clone(); - descriptor.fragment.as_mut().unwrap().shader = self.shader.clone(); - descriptor.layout = Some(vec![ - self.mesh_pipeline.view_layout.clone(), - self.mesh_pipeline.mesh_layout.clone(), - ]); - descriptor - } -} - -type DrawCustom = ( - SetItemPipeline, - SetMeshViewBindGroup<0>, - SetMeshBindGroup<1>, - DrawMesh, -);