Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rebased "Add a custom render phase example" #17410

13 changes: 13 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,7 @@ http-body-util = "0.1"
anyhow = "1"
macro_rules_attribute = "0.2"
accesskit = "0.17"
nonmax = "0.5"

[target.'cfg(not(target_family = "wasm"))'.dev-dependencies]
smol = "2"
Expand Down Expand Up @@ -2650,6 +2651,18 @@ description = "A shader that renders a mesh multiple times in one draw call usin
category = "Shaders"
wasm = true

[[example]]
name = "custom_render_phase"
path = "examples/shader/custom_render_phase.rs"
doc-scrape-examples = true

[package.metadata.example.custom_render_phase]
name = "Custom Render Phase"
description = "Shows how to make a complete render phase"
category = "Shaders"
wasm = true


[[example]]
name = "automatic_instancing"
path = "examples/shader/automatic_instancing.rs"
Expand Down
41 changes: 41 additions & 0 deletions assets/shaders/custom_stencil.wgsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//! A shader showing how to use the vertex position data to output the
//! stencil in the right position

// First we import everything we need from bevy_pbr
// A 2d shader would be vevry similar but import from bevy_sprite instead
#import bevy_pbr::{
mesh_functions,
view_transformations::position_world_to_clip
}

struct Vertex {
// This is needed if you are using batching and/or gpu preprocessing
// It's a built in so you don't need to define it in the vertex layout
@builtin(instance_index) instance_index: u32,
// Like we defined for the vertex layout
// position is at location 0
@location(0) position: vec3<f32>,
};

// This is the output of the vertex shader and we also use it as the input for the fragment shader
struct VertexOutput {
@builtin(position) clip_position: vec4<f32>,
@location(0) world_position: vec4<f32>,
};

@vertex
fn vertex(vertex: Vertex) -> VertexOutput {
var out: VertexOutput;
// This is how bevy computes the world position
// The vertex.instance_index is very important. Especially if you are using batching and gpu preprocessing
var world_from_local = mesh_functions::get_world_from_local(vertex.instance_index);
out.world_position = mesh_functions::mesh_position_local_to_world(world_from_local, vec4(vertex.position, 1.0));
out.clip_position = position_world_to_clip(out.world_position.xyz);
return out;
}

@fragment
fn fragment(in: VertexOutput) -> @location(0) vec4<f32> {
// Output a red color to represent the stencil of the mesh
return vec4(1.0, 0.0, 0.0, 1.0);
}
2 changes: 1 addition & 1 deletion crates/bevy_pbr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ mod light;
mod light_probe;
mod lightmap;
mod material;
mod material_bind_groups;
pub mod material_bind_groups;
mod mesh_material;
mod parallax;
mod pbr_material;
Expand Down
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ Example | Description
[Animated](../examples/shader/animate_shader.rs) | A shader that uses dynamic data like the time since startup
[Array Texture](../examples/shader/array_texture.rs) | A shader that shows how to reuse the core bevy PBR shading functionality in a custom material that obtains the base color from an array texture.
[Compute - Game of Life](../examples/shader/compute_shader_game_of_life.rs) | A compute shader that simulates Conway's Game of Life
[Custom Render Phase](../examples/shader/custom_render_phase.rs) | Shows how to make a complete render phase
[Custom Vertex Attribute](../examples/shader/custom_vertex_attribute.rs) | A shader that reads a mesh's custom vertex attribute
[Custom phase item](../examples/shader/custom_phase_item.rs) | Demonstrates how to enqueue custom draw commands in a render phase
[Extended Material](../examples/shader/extended_material.rs) | A custom shader that builds on the standard material
Expand Down
Loading
Loading