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_render::bindless::{bindless_samplers_filtering, bindless_textures_2d}
5#import bevy_pbr::{
6 prepass_io::VertexOutput,
7 prepass_bindings::previous_view_uniforms,
8 mesh_bindings::mesh,
9 mesh_view_bindings::view,
10 pbr_bindings,
11 pbr_types,
14#ifdef BINDLESS
15#import bevy_pbr::pbr_bindings::material_indices
16#endif // BINDLESS
18// Cutoff used for the premultiplied alpha modes BLEND, ADD, and ALPHA_TO_COVERAGE.
19const PREMULTIPLIED_ALPHA_CUTOFF = 0.05;
21// We can use a simplified version of alpha_discard() here since we only need to handle the alpha_cutoff
22fn prepass_alpha_discard(in: VertexOutput) {
24#ifdef MAY_DISCARD
25#ifdef BINDLESS
26 let slot = mesh[in.instance_index].material_and_lightmap_bind_group_slot & 0xffffu;
27 var output_color: vec4<f32> = pbr_bindings::material_array[material_indices[slot].material].base_color;
28 let flags = pbr_bindings::material_array[material_indices[slot].material].flags;
29#else // BINDLESS
30 var output_color: vec4<f32> = pbr_bindings::material.base_color;
31 let flags = pbr_bindings::material.flags;
32#endif // BINDLESS
34#ifdef VERTEX_UVS
35#ifdef STANDARD_MATERIAL_BASE_COLOR_UV_B
36 var uv = in.uv_b;
37#else // STANDARD_MATERIAL_BASE_COLOR_UV_B
38 var uv = in.uv;
39#endif // STANDARD_MATERIAL_BASE_COLOR_UV_B
41#ifdef BINDLESS
42 let uv_transform = pbr_bindings::material_array[material_indices[slot].material].uv_transform;
43#else // BINDLESS
44 let uv_transform = pbr_bindings::material.uv_transform;
45#endif // BINDLESS
47 uv = (uv_transform * vec3(uv, 1.0)).xy;
48 if (flags & pbr_types::STANDARD_MATERIAL_FLAGS_BASE_COLOR_TEXTURE_BIT) != 0u {
49 output_color = output_color * textureSampleBias(
50#ifdef BINDLESS
51 bindless_textures_2d[material_indices[slot].base_color_texture],
52 bindless_samplers_filtering[material_indices[slot].base_color_sampler],
53#else // BINDLESS
54 pbr_bindings::base_color_texture,
55 pbr_bindings::base_color_sampler,
56#endif // BINDLESS
57 uv,
58 view.mip_bias
59 );
60 }
61#endif // VERTEX_UVS
63 let alpha_mode = flags & pbr_types::STANDARD_MATERIAL_FLAGS_ALPHA_MODE_RESERVED_BITS;
64 if alpha_mode == pbr_types::STANDARD_MATERIAL_FLAGS_ALPHA_MODE_MASK {
65#ifdef BINDLESS
66 let alpha_cutoff = pbr_bindings::material_array[material_indices[slot].material].alpha_cutoff;
67#else // BINDLESS
68 let alpha_cutoff = pbr_bindings::material.alpha_cutoff;
69#endif // BINDLESS
70 if output_color.a < alpha_cutoff {
71 discard;
72 }
73 } else if (alpha_mode == pbr_types::STANDARD_MATERIAL_FLAGS_ALPHA_MODE_BLEND ||
74 alpha_mode == pbr_types::STANDARD_MATERIAL_FLAGS_ALPHA_MODE_ADD ||
75 alpha_mode == pbr_types::STANDARD_MATERIAL_FLAGS_ALPHA_MODE_ALPHA_TO_COVERAGE) {
76 if output_color.a < PREMULTIPLIED_ALPHA_CUTOFF {
77 discard;
78 }
79 } else if alpha_mode == pbr_types::STANDARD_MATERIAL_FLAGS_ALPHA_MODE_PREMULTIPLIED {
80 if all(output_color < vec4(PREMULTIPLIED_ALPHA_CUTOFF)) {
81 discard;
82 }
83 }
85#endif // MAY_DISCARD
88#ifdef MOTION_VECTOR_PREPASS
89fn calculate_motion_vector(world_position: vec4<f32>, previous_world_position: vec4<f32>) -> vec2<f32> {
90 let clip_position_t = view.unjittered_clip_from_world * world_position;
91 let clip_position = clip_position_t.xy / clip_position_t.w;
92 let previous_clip_position_t = previous_view_uniforms.clip_from_world * previous_world_position;
93 let previous_clip_position = previous_clip_position_t.xy / previous_clip_position_t.w;
94 // These motion vectors are used as offsets to UV positions and are stored
95 // in the range -1,1 to allow offsetting from the one corner to the
96 // diagonally-opposite corner in UV coordinates, in either direction.
97 // A difference between diagonally-opposite corners of clip space is in the
98 // range -2,2, so this needs to be scaled by 0.5. And the V direction goes
99 // down where clip space y goes up, so y needs to be flipped.
100 return (clip_position - previous_clip_position) * vec2(0.5, -0.5);
102#endif // MOTION_VECTOR_PREPASS
Contents