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// This shader, a part of the `clustered_decals` example, shows how to use the
2// decal `tag` field to apply arbitrary decal effects.
4#import bevy_pbr::{
5 clustered_forward,
6 decal::clustered,
7 forward_io::{VertexOutput, FragmentOutput},
8 mesh_view_bindings,
9 pbr_fragment::pbr_input_from_standard_material,
10 pbr_functions::{alpha_discard, apply_pbr_lighting, main_pass_post_lighting_processing},
13@fragment
14fn fragment(
15 in: VertexOutput,
16 @builtin(front_facing) is_front: bool,
17) -> FragmentOutput {
18 // Generate a `PbrInput` struct from the `StandardMaterial` bindings.
19 var pbr_input = pbr_input_from_standard_material(in, is_front);
21 // Alpha discard.
22 pbr_input.material.base_color = alpha_discard(pbr_input.material, pbr_input.material.base_color);
24 // Apply the normal decals.
25 clustered::apply_decals(&pbr_input);
27 // Here we tint the color based on the tag of the decal.
28 // We could optionally do other things, such as adjust the normal based on a normal map.
29 let view_z = clustered::get_view_z(in.world_position.xyz);
30 let is_orthographic = clustered::view_is_orthographic();
31 let cluster_index =
32 clustered_forward::view_fragment_cluster_index(in.position.xy, view_z, is_orthographic);
33 var clusterable_object_index_ranges =
34 clustered_forward::unpack_clusterable_object_index_ranges(cluster_index);
35 var decal_iterator = clustered::clustered_decal_iterator_new(
36 in.world_position.xyz,
37 &clusterable_object_index_ranges
38 );
39 while (clustered::clustered_decal_iterator_next(&decal_iterator)) {
40 var decal_base_color = textureSampleLevel(
41 mesh_view_bindings::clustered_decal_textures[decal_iterator.base_color_texture_index],
42 mesh_view_bindings::clustered_decal_sampler,
43 decal_iterator.uv,
44 0.0
45 );
47 switch (decal_iterator.tag) {
48 case 1u: {
49 // Tint with red.
50 decal_base_color = vec4(
51 mix(pbr_input.material.base_color.rgb, vec3(1.0, 0.0, 0.0), 0.5),
52 decal_base_color.a,
53 );
54 }
55 case 2u: {
56 // Tint with blue.
57 decal_base_color = vec4(
58 mix(pbr_input.material.base_color.rgb, vec3(0.0, 0.0, 1.0), 0.5),
59 decal_base_color.a,
60 );
61 }
62 default: {}
63 }
65 pbr_input.material.base_color = vec4(
66 mix(pbr_input.material.base_color.rgb, decal_base_color.rgb, decal_base_color.a),
67 pbr_input.material.base_color.a + decal_base_color.a
68 );
69 }
71 // Apply lighting.
72 var out: FragmentOutput;
73 out.color = apply_pbr_lighting(pbr_input);
75 // Apply in-shader post processing (fog, alpha-premultiply, and also
76 // tonemapping, debanding if the camera is non-HDR). Note this does not
77 // include fullscreen postprocessing effects like bloom.
78 out.color = main_pass_post_lighting_processing(pbr_input, out.color);
80 return out;
Contents