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.
1import bevy_render::bindless::{bindless_samplers_filtering, bindless_textures_2d};
3import package::{
4 prepass::io::VertexOutput,
5 prepass::bindings::previous_view_uniforms,
6 render::{
7 mesh_bindings::mesh,
8 mesh_view_bindings::view,
9 pbr_bindings,
10 pbr_types,
11 },
12};
14@if(BINDLESS)
15import package::render::pbr_bindings::material_indices;
17/// Cutoff used for the premultiplied alpha modes BLEND, ADD, and ALPHA_TO_COVERAGE.
18const PREMULTIPLIED_ALPHA_CUTOFF = 0.05;
20/// Reusable alpha discard logic suitable for use by custom materials in the prepass.
21/// We can use a simplified version of alpha_discard() here since we only need to handle the alpha_cutoff
22fn prepass_alpha_discard(material_flags: u32, alpha_cutoff: f32, output_color: vec4<f32>) {
23 let alpha_mode = material_flags & pbr_types::STANDARD_MATERIAL_FLAGS_ALPHA_MODE_RESERVED_BITS;
24 if alpha_mode == pbr_types::STANDARD_MATERIAL_FLAGS_ALPHA_MODE_MASK {
25 if output_color.a < alpha_cutoff {
26 discard;
27 }
28 } else if (alpha_mode == pbr_types::STANDARD_MATERIAL_FLAGS_ALPHA_MODE_BLEND ||
29 alpha_mode == pbr_types::STANDARD_MATERIAL_FLAGS_ALPHA_MODE_ADD ||
30 alpha_mode == pbr_types::STANDARD_MATERIAL_FLAGS_ALPHA_MODE_ALPHA_TO_COVERAGE) {
31 if output_color.a < PREMULTIPLIED_ALPHA_CUTOFF {
32 discard;
33 }
34 } else if alpha_mode == pbr_types::STANDARD_MATERIAL_FLAGS_ALPHA_MODE_PREMULTIPLIED {
35 if all(output_color < vec4(PREMULTIPLIED_ALPHA_CUTOFF)) {
36 discard;
37 }
38 }
41/// Samples the StandardMaterial's base color (including the base color texture, if any) and then
42/// invokes `prepass_alpha_discard` with the material's flags and alpha cutoff.
43fn prepass_sample_color_and_alpha_discard(in: VertexOutput) {
45@if(MAY_DISCARD) {
46@if(BINDLESS) {
47 let slot = mesh[in.instance_index].material_and_lightmap_bind_group_slot & 0xffffu;
48 var output_color: vec4<f32> = pbr_bindings::material_array[material_indices[slot].material].base_color;
49 let flags = pbr_bindings::material_array[material_indices[slot].material].flags;
50 let alpha_cutoff = pbr_bindings::material_array[material_indices[slot].material].alpha_cutoff;
52@else {
53 var output_color: vec4<f32> = pbr_bindings::material.base_color;
54 let flags = pbr_bindings::material.flags;
55 let alpha_cutoff = pbr_bindings::material.alpha_cutoff;
58@if(VERTEX_UVS) {
59@if(STANDARD_MATERIAL_BASE_COLOR_UV_B)
60 var uv = in.uv_b;
61@else // STANDARD_MATERIAL_BASE_COLOR_UV_B
62 var uv = in.uv;
64@if(BINDLESS)
65 let uv_transform = pbr_bindings::material_array[material_indices[slot].material].uv_transform;
66@else
67 let uv_transform = pbr_bindings::material.uv_transform;
69 uv = (uv_transform * vec3(uv, 1.0)).xy;
70 if (flags & pbr_types::STANDARD_MATERIAL_FLAGS_BASE_COLOR_TEXTURE_BIT) != 0u {
71@if(BINDLESS)
72 let texture = bindless_textures_2d[material_indices[slot].base_color_texture];
73@else
74 let texture = pbr_bindings::base_color_texture;
75@if(BINDLESS)
76 let sampler_ = bindless_samplers_filtering[material_indices[slot].base_color_sampler];
77@else
78 let sampler_ = pbr_bindings::base_color_sampler;
79 output_color = output_color * textureSampleBias(
80 texture,
81 sampler_,
82 uv,
83 view.mip_bias
84 );
85 }
88 prepass_alpha_discard(flags, alpha_cutoff, output_color);
92@if(MOTION_VECTOR_PREPASS)
93fn calculate_motion_vector(world_position: vec4<f32>, previous_world_position: vec4<f32>) -> vec2<f32> {
94 let clip_position_t = view.unjittered_clip_from_world * world_position;
95 let clip_position = clip_position_t.xy / clip_position_t.w;
96 let previous_clip_position_t = previous_view_uniforms.unjittered_clip_from_world * previous_world_position;
97 let previous_clip_position = previous_clip_position_t.xy / previous_clip_position_t.w;
98 // These motion vectors are used as offsets to UV positions and are stored
99 // in the range -1,1 to allow offsetting from the one corner to the
100 // diagonally-opposite corner in UV coordinates, in either direction.
101 // A difference between diagonally-opposite corners of clip space is in the
102 // range -2,2, so this needs to be scaled by 0.5. And the V direction goes
103 // down where clip space y goes up, so y needs to be flipped.
104 return (clip_position - previous_clip_position) * vec2(0.5, -0.5);
Contents