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#import bevy_pbr::{
2 pbr_prepass_functions,
3 pbr_bindings,
4 pbr_bindings::material,
5 pbr_types,
6 pbr_functions,
7 pbr_functions::SampleBias,
8 prepass_io,
9 mesh_bindings::mesh,
10 mesh_view_bindings::view,
13#import bevy_render::bindless::{bindless_samplers_filtering, bindless_textures_2d}
15#ifdef MESHLET_MESH_MATERIAL_PASS
16#import bevy_pbr::meshlet_visibility_buffer_resolve::resolve_vertex_output
17#endif
19#ifdef BINDLESS
20#import bevy_pbr::pbr_bindings::material_indices
21#endif // BINDLESS
23#ifdef PREPASS_FRAGMENT
24@fragment
25fn fragment(
26#ifdef MESHLET_MESH_MATERIAL_PASS
27 @builtin(position) frag_coord: vec4<f32>,
28#else
29 in: prepass_io::VertexOutput,
30 @builtin(front_facing) is_front: bool,
31#endif
32) -> prepass_io::FragmentOutput {
33#ifdef MESHLET_MESH_MATERIAL_PASS
34 let in = resolve_vertex_output(frag_coord);
35 let is_front = true;
36#else // MESHLET_MESH_MATERIAL_PASS
38#ifdef BINDLESS
39 let slot = mesh[in.instance_index].material_and_lightmap_bind_group_slot & 0xffffu;
40 let flags = pbr_bindings::material_array[material_indices[slot].material].flags;
41 let uv_transform = pbr_bindings::material_array[material_indices[slot].material].uv_transform;
42#else // BINDLESS
43 let flags = pbr_bindings::material.flags;
44 let uv_transform = pbr_bindings::material.uv_transform;
45#endif // BINDLESS
47 // If we're in the crossfade section of a visibility range, conditionally
48 // discard the fragment according to the visibility pattern.
49#ifdef VISIBILITY_RANGE_DITHER
50 pbr_functions::visibility_range_dither(in.position, in.visibility_range_dither);
51#endif // VISIBILITY_RANGE_DITHER
53 pbr_prepass_functions::prepass_alpha_discard(in);
54#endif // MESHLET_MESH_MATERIAL_PASS
56 var out: prepass_io::FragmentOutput;
58#ifdef UNCLIPPED_DEPTH_ORTHO_EMULATION
59 out.frag_depth = in.unclipped_depth;
60#endif // UNCLIPPED_DEPTH_ORTHO_EMULATION
62#ifdef NORMAL_PREPASS
63 // NOTE: Unlit bit not set means == 0 is true, so the true case is if lit
64 if (flags & pbr_types::STANDARD_MATERIAL_FLAGS_UNLIT_BIT) == 0u {
65 let double_sided = (flags & pbr_types::STANDARD_MATERIAL_FLAGS_DOUBLE_SIDED_BIT) != 0u;
67 let world_normal = pbr_functions::prepare_world_normal(
68 in.world_normal,
69 double_sided,
70 is_front,
71 );
73 var normal = world_normal;
75#ifdef VERTEX_UVS
76#ifdef VERTEX_TANGENTS
77#ifdef STANDARD_MATERIAL_NORMAL_MAP
79// TODO: Transforming UVs mean we need to apply derivative chain rule for meshlet mesh material pass
80#ifdef STANDARD_MATERIAL_NORMAL_MAP_UV_B
81 let uv = (uv_transform * vec3(in.uv_b, 1.0)).xy;
82#else
83 let uv = (uv_transform * vec3(in.uv, 1.0)).xy;
84#endif
86 // Fill in the sample bias so we can sample from textures.
87 var bias: SampleBias;
88#ifdef MESHLET_MESH_MATERIAL_PASS
89 bias.ddx_uv = in.ddx_uv;
90 bias.ddy_uv = in.ddy_uv;
91#else // MESHLET_MESH_MATERIAL_PASS
92 bias.mip_bias = view.mip_bias;
93#endif // MESHLET_MESH_MATERIAL_PASS
95 let Nt =
96#ifdef MESHLET_MESH_MATERIAL_PASS
97 textureSampleGrad(
98#else // MESHLET_MESH_MATERIAL_PASS
99 textureSampleBias(
100#endif // MESHLET_MESH_MATERIAL_PASS
101#ifdef BINDLESS
102 bindless_textures_2d[material_indices[slot].normal_map_texture],
103 bindless_samplers_filtering[material_indices[slot].normal_map_sampler],
104#else // BINDLESS
105 pbr_bindings::normal_map_texture,
106 pbr_bindings::normal_map_sampler,
107#endif // BINDLESS
108 uv,
109#ifdef MESHLET_MESH_MATERIAL_PASS
110 bias.ddx_uv,
111 bias.ddy_uv,
112#else // MESHLET_MESH_MATERIAL_PASS
113 bias.mip_bias,
114#endif // MESHLET_MESH_MATERIAL_PASS
115 ).rgb;
116 let TBN = pbr_functions::calculate_tbn_mikktspace(normal, in.world_tangent);
118 normal = pbr_functions::apply_normal_mapping(
119 flags,
120 TBN,
121 double_sided,
122 is_front,
123 Nt,
124 );
126#endif // STANDARD_MATERIAL_NORMAL_MAP
127#endif // VERTEX_TANGENTS
128#endif // VERTEX_UVS
130 out.normal = vec4(normal * 0.5 + vec3(0.5), 1.0);
131 } else {
132 out.normal = vec4(in.world_normal * 0.5 + vec3(0.5), 1.0);
133 }
134#endif // NORMAL_PREPASS
136#ifdef MOTION_VECTOR_PREPASS
137#ifdef MESHLET_MESH_MATERIAL_PASS
138 out.motion_vector = in.motion_vector;
139#else
140 out.motion_vector = pbr_prepass_functions::calculate_motion_vector(in.world_position, in.previous_world_position);
141#endif
142#endif
144 return out;
146#else
147@fragment
148fn fragment(in: prepass_io::VertexOutput) {
149 pbr_prepass_functions::prepass_alpha_discard(in);
151#endif // PREPASS_FRAGMENT
Contents