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_functions::alpha_discard,
3 pbr_fragment::pbr_input_from_standard_material,
4 view_transformations::depth_ndc_to_view_z,
5}
7#ifdef PREPASS_PIPELINE
8#import bevy_pbr::{
9 prepass_io::{VertexOutput, FragmentOutput},
10 pbr_deferred_functions::deferred_output,
12#else
13#import bevy_pbr::{
14 forward_io::{VertexOutput, FragmentOutput},
15 pbr_functions,
16 pbr_functions::{apply_pbr_lighting, main_pass_post_lighting_processing},
17 pbr_types::STANDARD_MATERIAL_FLAGS_UNLIT_BIT,
19#endif
21#ifdef MESHLET_MESH_MATERIAL_PASS
22#import bevy_pbr::meshlet_visibility_buffer_resolve::resolve_vertex_output
23#endif
25#import bevy_water::water_bindings
26#import bevy_water::water_functions as water_fn
28@fragment
29fn fragment(
30#ifdef MESHLET_MESH_MATERIAL_PASS
31 @builtin(position) frag_coord: vec4<f32>,
32#else
33 p_in: VertexOutput,
34 @builtin(front_facing) is_front: bool,
35#endif
36) -> FragmentOutput {
37#ifdef MESHLET_MESH_MATERIAL_PASS
38 let p_in = resolve_vertex_output(frag_coord);
39 let is_front = true;
40#endif
42 var in = p_in;
43 var world_position: vec4<f32> = in.world_position;
44 let w_pos = water_fn::uv_to_coord(in.uv);
45 // Calculate normal.
46 let height = water_fn::get_wave_height(w_pos);
47#if QUALITY > 2
48 let delta = 0.5;
49 let height_dx = water_fn::get_wave_height(w_pos + vec2<f32>(delta, 0.0));
50 let height_dz = water_fn::get_wave_height(w_pos + vec2<f32>(0.0, delta));
51 in.world_normal = normalize(vec3<f32>(height - height_dx, delta, height - height_dz));
52#else
53 let pos = world_position.xyz + (in.world_normal * height);
54 let pos_dx = dpdx(pos);
55 let pos_dy = dpdy(pos);
56 in.world_normal = normalize(cross(pos_dy, pos_dx));
57#endif
59 // If we're in the crossfade section of a visibility range, conditionally
60 // discard the fragment according to the visibility pattern.
61#ifdef VISIBILITY_RANGE_DITHER
62 pbr_functions::visibility_range_dither(in.position, in.visibility_range_dither);
63#endif
65 // generate a PbrInput struct from the StandardMaterial bindings
66 var pbr_input = pbr_input_from_standard_material(in, is_front);
68 let deep_color = water_bindings::material.deep_color;
69 var water_color = deep_color;
70#ifdef DEPTH_PREPASS
71#ifndef PREPASS_PIPELINE
72#ifndef WEBGL2
73 let water_clarity = water_bindings::material.clarity;
74 let shallow_color = water_bindings::material.shallow_color;
75 let edge_scale = water_bindings::material.edge_scale;
76 let edge_color = water_bindings::material.edge_color;
78 let z_depth_buffer_ndc = bevy_pbr::prepass_utils::prepass_depth(in.position, 0u);
79 let z_depth_buffer_view = depth_ndc_to_view_z(z_depth_buffer_ndc);
80 let z_fragment_view = depth_ndc_to_view_z(in.position.z);
81 let depth_diff_view = z_fragment_view - z_depth_buffer_view;
82 let beers_law = exp(-depth_diff_view * water_clarity);
83 let depth_color = vec4<f32>(mix(deep_color.xyz, shallow_color.xyz, beers_law), 1.0 - beers_law);
84 water_color = mix(edge_color, depth_color, smoothstep(0.0, edge_scale, depth_diff_view));
85#endif
86#endif
87#endif
88 pbr_input.material.base_color *= water_color;
90 //let foam_color = water_bindings::material.edge_color;
91 //let foam = mix(foam_color, depth_color, smoothstep(0.0, edge_scale, depth_diff_view));
93 // alpha discard
94 pbr_input.material.base_color = alpha_discard(pbr_input.material, pbr_input.material.base_color);
96#ifdef PREPASS_PIPELINE
97 // write the gbuffer, lighting pass id, and optionally normal and motion_vector textures
98 let out = deferred_output(in, pbr_input);
99#else
100 // in forward mode, we calculate the lit color immediately, and then apply some post-lighting effects here.
101 // in deferred mode the lit color and these effects will be calculated in the deferred lighting shader
102 var out: FragmentOutput;
103 if (pbr_input.material.flags & STANDARD_MATERIAL_FLAGS_UNLIT_BIT) == 0u {
104 out.color = apply_pbr_lighting(pbr_input);
105 } else {
106 out.color = pbr_input.material.base_color;
107 }
109 // apply in-shader post processing (fog, alpha-premultiply, and also tonemapping, debanding if the camera is non-hdr)
110 // note this does not include fullscreen postprocessing effects like bloom.
111 out.color = main_pass_post_lighting_processing(pbr_input, out.color);
113 // show grid
114 // 3.938... = WATER_SIZE / ((WATER_SIZE / 4) + 1)
115 //let f_pos = step(fract((w_pos / 3.9384615384615)), vec2<f32>(0.995));
116 //let grid = step(f_pos.x + f_pos.y, 1.00);
117 //out.color += vec4<f32>(grid, grid, grid, 0.00);
118#endif
120 return out;
Contents