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 pbr_input.material.base_color = clustered::apply_decal_base_color(
26 in.world_position.xyz,
27 in.position.xy,
28 pbr_input.material.base_color
29 );
31 // Here we tint the color based on the tag of the decal.
32 // We could optionally do other things, such as adjust the normal based on a normal map.
33 let view_z = clustered::get_view_z(in.world_position.xyz);
34 let is_orthographic = clustered::view_is_orthographic();
35 let cluster_index =
36 clustered_forward::fragment_cluster_index(in.position.xy, view_z, is_orthographic);
37 var clusterable_object_index_ranges =
38 clustered_forward::unpack_clusterable_object_index_ranges(cluster_index);
39 var decal_iterator = clustered::clustered_decal_iterator_new(
40 in.world_position.xyz,
41 &clusterable_object_index_ranges
42 );
43 while (clustered::clustered_decal_iterator_next(&decal_iterator)) {
44 var decal_base_color = textureSampleLevel(
45 mesh_view_bindings::clustered_decal_textures[decal_iterator.texture_index],
46 mesh_view_bindings::clustered_decal_sampler,
47 decal_iterator.uv,
48 0.0
49 );
51 switch (decal_iterator.tag) {
52 case 1u: {
53 // Tint with red.
54 decal_base_color = vec4(
55 mix(pbr_input.material.base_color.rgb, vec3(1.0, 0.0, 0.0), 0.5),
56 decal_base_color.a,
57 );
58 }
59 case 2u: {
60 // Tint with blue.
61 decal_base_color = vec4(
62 mix(pbr_input.material.base_color.rgb, vec3(0.0, 0.0, 1.0), 0.5),
63 decal_base_color.a,
64 );
65 }
66 default: {}
67 }
69 pbr_input.material.base_color = vec4(
70 mix(pbr_input.material.base_color.rgb, decal_base_color.rgb, decal_base_color.a),
71 pbr_input.material.base_color.a + decal_base_color.a
72 );
73 }
75 // Apply lighting.
76 var out: FragmentOutput;
77 out.color = apply_pbr_lighting(pbr_input);
79 // Apply in-shader post processing (fog, alpha-premultiply, and also
80 // tonemapping, debanding if the camera is non-HDR). Note this does not
81 // include fullscreen postprocessing effects like bloom.
82 out.color = main_pass_post_lighting_processing(pbr_input, out.color);
84 return out;
Contents