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#define_import_path bevy_pbr::pbr_prepass_functions
3#import bevy_pbr::{
4 prepass_io::VertexOutput,
5 prepass_bindings::previous_view_uniforms,
6 mesh_view_bindings::view,
7 pbr_bindings,
8 pbr_types,
9}
11// Cutoff used for the premultiplied alpha modes BLEND, ADD, and ALPHA_TO_COVERAGE.
12const PREMULTIPLIED_ALPHA_CUTOFF = 0.05;
14// We can use a simplified version of alpha_discard() here since we only need to handle the alpha_cutoff
15fn prepass_alpha_discard(in: VertexOutput) {
17#ifdef MAY_DISCARD
18 var output_color: vec4<f32> = pbr_bindings::material.base_color;
20#ifdef VERTEX_UVS
21#ifdef STANDARD_MATERIAL_BASE_COLOR_UV_B
22 var uv = in.uv_b;
23#else // STANDARD_MATERIAL_BASE_COLOR_UV_B
24 var uv = in.uv;
25#endif // STANDARD_MATERIAL_BASE_COLOR_UV_B
27 let uv_transform = pbr_bindings::material.uv_transform;
28 uv = (uv_transform * vec3(uv, 1.0)).xy;
29 if (pbr_bindings::material.flags & pbr_types::STANDARD_MATERIAL_FLAGS_BASE_COLOR_TEXTURE_BIT) != 0u {
30 output_color = output_color * textureSampleBias(pbr_bindings::base_color_texture, pbr_bindings::base_color_sampler, uv, view.mip_bias);
31 }
32#endif // VERTEX_UVS
34 let alpha_mode = pbr_bindings::material.flags & pbr_types::STANDARD_MATERIAL_FLAGS_ALPHA_MODE_RESERVED_BITS;
35 if alpha_mode == pbr_types::STANDARD_MATERIAL_FLAGS_ALPHA_MODE_MASK {
36 if output_color.a < pbr_bindings::material.alpha_cutoff {
37 discard;
38 }
39 } else if (alpha_mode == pbr_types::STANDARD_MATERIAL_FLAGS_ALPHA_MODE_BLEND ||
40 alpha_mode == pbr_types::STANDARD_MATERIAL_FLAGS_ALPHA_MODE_ADD ||
41 alpha_mode == pbr_types::STANDARD_MATERIAL_FLAGS_ALPHA_MODE_ALPHA_TO_COVERAGE) {
42 if output_color.a < PREMULTIPLIED_ALPHA_CUTOFF {
43 discard;
44 }
45 } else if alpha_mode == pbr_types::STANDARD_MATERIAL_FLAGS_ALPHA_MODE_PREMULTIPLIED {
46 if all(output_color < vec4(PREMULTIPLIED_ALPHA_CUTOFF)) {
47 discard;
48 }
49 }
51#endif // MAY_DISCARD
54#ifdef MOTION_VECTOR_PREPASS
55fn calculate_motion_vector(world_position: vec4<f32>, previous_world_position: vec4<f32>) -> vec2<f32> {
56 let clip_position_t = view.unjittered_clip_from_world * world_position;
57 let clip_position = clip_position_t.xy / clip_position_t.w;
58 let previous_clip_position_t = previous_view_uniforms.clip_from_world * previous_world_position;
59 let previous_clip_position = previous_clip_position_t.xy / previous_clip_position_t.w;
60 // These motion vectors are used as offsets to UV positions and are stored
61 // in the range -1,1 to allow offsetting from the one corner to the
62 // diagonally-opposite corner in UV coordinates, in either direction.
63 // A difference between diagonally-opposite corners of clip space is in the
64 // range -2,2, so this needs to be scaled by 0.5. And the V direction goes
65 // down where clip space y goes up, so y needs to be flipped.
66 return (clip_position - previous_clip_position) * vec2(0.5, -0.5);
68#endif // MOTION_VECTOR_PREPASS
Contents