Search across packages, versions, files, declarations, and comments. Use multiple terms to narrow results (AND), and add @vertex, @fragment, or @compute to filter by shader stage.
1//! A shader showing how to use the vertex position data to output the
2//! stencil in the right position
4// First we import everything we need from bevy_pbr
5// A 2d shader would be vevry similar but import from bevy_sprite_render instead
6import bevy_pbr::render::{
7 mesh_functions,
8 view_transformations::position_world_to_clip
9};
11struct Vertex {
12 // This is needed if you are using batching and/or gpu preprocessing
13 // It's a built in so you don't need to define it in the vertex layout
14 @builtin(instance_index) instance_index: u32,
15 // Like we defined for the vertex layout
16 // position is at location 0
17 @if(VERTEX_POSITIONS_COMPRESSED)
18 @location(0) compressed_position: vec4<f32>,
19 @else
20 @location(0) position: vec3<f32>,
21};
23/// This is the output of the vertex shader and we also use it as the input for the fragment shader
24struct VertexOutput {
25 @builtin(position) clip_position: vec4<f32>,
26 @location(0) world_position: vec4<f32>,
27};
29@vertex
30fn vertex(vertex: Vertex) -> VertexOutput {
31 var out: VertexOutput;
32 // This is how bevy computes the world position
33 // The vertex.instance_index is very important. Especially if you are using batching and gpu preprocessing
34 var world_from_local = mesh_functions::get_world_from_local(vertex.instance_index);
35@if(VERTEX_POSITIONS_COMPRESSED) {
36 let mesh_metadata = bevy_pbr::render::mesh_functions::get_metadata(vertex.instance_index);
37 let vertex_position = bevy_render::utils::decompress_vertex_position(vertex.compressed_position, mesh_metadata.aabb_center, mesh_metadata.aabb_half_extents);
39@else {
40 let vertex_position = vertex.position;
42 out.world_position = mesh_functions::mesh_position_local_to_world(world_from_local, vec4(vertex_position, 1.0));
43 out.clip_position = position_world_to_clip(out.world_position.xyz);
44 return out;
47@fragment
48fn fragment(in: VertexOutput) -> @location(0) vec4<f32> {
49 // Output a red color to represent the stencil of the mesh
50 return vec4(1.0, 0.0, 0.0, 1.0);
Contents