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::mesh_view_bindings::view
2#import bevy_pbr::mesh_functions::{
3 get_world_from_local,
4 get_visibility_range_dither_level,
5 get_model_matrix,
6 mesh_tangent_local_to_world,
7 get_previous_world_from_local,
8 mesh_normal_local_to_world
9}
10#import bevy_pbr::view_transformations::position_world_to_clip
11#import bevy_pbr::pbr_fragment::pbr_input_from_standard_material
12#import bevy_pbr::pbr_fragment::pbr_material_from_standard_material
13#import bevy_pbr::pbr_functions::alpha_discard
14#import bevy_pbr::pbr_functions::apply_pbr_lighting
15#import bevy_pbr::pbr_functions::main_pass_post_lighting_processing
16#import bevy_pbr::prepass_io::Vertex
17#import bevy_pbr::prepass_io::VertexOutput
18#import bevy_render::{
19 globals::Globals,
21#import bevy_pbr::mesh_bindings::mesh
23#import bevy_feronia::wind::Wind
24#import bevy_feronia::types::{SampledNoise, DisplacedVertex, InstanceInfo}
26#ifdef BINDLESS
27#import bevy_render::bindless::{bindless_samplers_filtering, bindless_textures_2d}
28#endif
30#ifdef BINDLESS
31#import bevy_feronia::bindings::{wind_indices, wind_material}
32#else
33#import bevy_feronia::bindings::{wind, noise_texture, noise_texture_sampler}
34#endif
36#import bevy_feronia::displace::{displace_vertex_and_calc_normal}
37#import bevy_feronia::noise::sample_noise
39@group(0) @binding(1) var<uniform> globals: Globals;
41@vertex
42fn vertex(vertex: Vertex) -> VertexOutput {
43 var out: VertexOutput;
45#ifdef BINDLESS
46 let slot = mesh[vertex.instance_index].material_and_lightmap_bind_group_slot & 0xffffu;
47 let wind = wind_material[wind_indices[slot].material];
48#endif
50 // --- CURRENT FRAME ---
51 {
52 let world_from_local = get_world_from_local(vertex.instance_index);
54 // --- INSTANCE ---
55 var instance: InstanceInfo;
56 let camera_world_pos = view.world_position.xyz;
57 instance.world_from_local = world_from_local;
58 instance.instance_position = instance.world_from_local[3];
59 instance.wrapped_time = globals.time;
60 instance.instance_index = vertex.instance_index;
62 let noise = sample_noise(instance, vertex.position);
64 var static_shadows = false;
66 #ifdef STATIC_SHADOW
67 #ifndef MOTION_VECTOR_PREPASS
68 static_shadows = true;
69 #endif
70 #endif
72 // --- DISPLACEMENT ---
73 var displaced: DisplacedVertex;
74 if static_shadows {
75 displaced.world_position = (instance.world_from_local * vec4<f32>(vertex.position, 1.0));
77 #ifdef VERTEX_NORMALS
78 displaced.world_normal = mesh_normal_local_to_world(vertex.normal, instance.instance_index);
79 #else
80 displaced.world_normal = vec3<f32>(0.0, 0.0, 1.0);
81 #endif
83 #ifdef VERTEX_TANGENTS
84 displaced.world_tangent = mesh_tangent_local_to_world(instance.world_from_local, vertex.tangent, instance.instance_index);
85 #else
86 const local_fallback_tangent: vec4<f32> = vec4<f32>(1.0, 0.0, 0.0, 1.0);
87 let world_tangent_xyz = (instance.world_from_local * vec4<f32>(local_fallback_tangent.xyz, 0.0)).xyz;
89 displaced.world_tangent = vec4<f32>(normalize(world_tangent_xyz), local_fallback_tangent.w);
90 #endif
91 } else {
92 displaced = displace_vertex_and_calc_normal(
93 wind,
94 noise,
95 vertex.position,
96 instance,
97 #ifdef VERTEX_NORMALS
98 vertex.normal,
99 #endif
100 #ifdef VERTEX_TANGENTS
101 vertex.tangent,
102 #endif
103 #ifdef VERTEX_UVS_A
104 vertex.uv
105 #endif
106 );
107 }
109#ifdef VERTEX_POSITIONS
110 out.position = position_world_to_clip(displaced.world_position.xyz);
111 out.world_position = displaced.world_position;
112#endif
114#ifdef VERTEX_NORMALS
115 out.world_normal = displaced.world_normal;
116#endif
118#ifdef VERTEX_UVS_A
119 out.uv = vertex.uv;
120#endif
121#ifdef VERTEX_UVS_B
122 out.uv_b = vertex.uv_b;
123#endif
125#ifdef VERTEX_TANGENTS
126 out.world_tangent = displaced.world_tangent;
127#endif
129#ifdef VERTEX_OUTPUT_INSTANCE_INDEX
130 out.instance_index = vertex.instance_index;
131#endif
133#ifdef VISIBILITY_RANGE_DITHER
134 out.visibility_range_dither = get_visibility_range_dither_level(
135 vertex.instance_index, world_from_local[3]);
136#endif
138#ifdef UNCLIPPED_DEPTH_ORTHO_EMULATION
139 out.unclipped_depth = view.projection[2][2] * out.world_position.z + view.projection[3][2];
140#endif
143#ifdef MOTION_VECTOR_PREPASS
144 // --- PREVIOUS FRAME ---
145 {
146 // --- INSTANCE ---
147 var instance_prev: InstanceInfo;
150 instance_prev.world_from_local = get_previous_world_from_local(vertex.instance_index);
151 instance_prev.instance_position = instance_prev.world_from_local[3];
152 instance_prev.wrapped_time = (globals.time - globals.delta_time);
153 instance_prev.instance_index = vertex.instance_index;
155 let noise_prev = sample_noise(instance_prev, vertex.position);
157 /// --- DISPLACEMENT ---
158 let displaced_prev = displace_vertex_and_calc_normal(
159 wind,
160 noise_prev,
161 vertex.position,
162 instance_prev,
163#ifdef VERTEX_NORMALS
164 vertex.normal,
165#endif
166#ifdef VERTEX_TANGENTS
167 vertex.tangent,
168#endif
169#ifdef VERTEX_UVS_A
170 vertex.uv
171#endif
172 );
174 out.previous_world_position = displaced_prev.world_position;
175 }
176#endif // MOTION_VECTOR_PREPASS
178 return out;
Contents