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#define_import_path bevy_feronia::noise
2#import bevy_feronia::types::{SampledNoise, InstanceInfo}
3#import bevy_feronia::wind::{Wind}
4#import bevy_pbr::utils::rand_f
5#import bevy_pbr::mesh_bindings::mesh
7#ifdef BINDLESS
8#import bevy_render::bindless::{bindless_samplers_filtering, bindless_textures_2d}
9#endif
11#ifdef BINDLESS
12#import bevy_feronia::bindings::{wind_indices, wind_material}
13#else
14#import bevy_feronia::bindings::{wind, noise_texture, noise_texture_sampler}
15#endif
17fn sample_noise(instance: InstanceInfo, local_vertex_pos: vec3<f32>) -> SampledNoise {
18 var noise: SampledNoise;
20#ifdef WIND_AFFECTED
21#ifdef BINDLESS
22 let slot = mesh[instance.instance_index].material_and_lightmap_bind_group_slot & 0xffffu;
23 let wind = wind_material[wind_indices[slot].material];
24 let noise_texture = bindless_textures_2d[wind_indices[slot].noise_texture];
25 let noise_texture_sampler = bindless_samplers_filtering[wind_indices[slot].noise_texture_sampler];
26#endif
28 // Need to use local_vertex_pos here.
29 // Sampling the same noise for neighbors, breaks motion vectors / DLSS / TAA
30 let world_pos = (instance.world_from_local * vec4<f32>(local_vertex_pos, 1.0)).xyz;
32 let base_uv = world_pos.xz * wind.noise_scale;
33 let uv_scroll_offset = instance.wrapped_time * wind.scroll_speed * wind.direction;
34 let tex_coord = base_uv + uv_scroll_offset;
36#ifdef WIND_LOW_QUALITY
37 noise.macro_noise = textureSampleLevel(noise_texture, noise_texture_sampler, tex_coord, 0.0).r;
38 noise.micro_noise = 0.0;
39 noise.phase_noise = vec2<f32>(0.0);
40#else // NOT WIND_LOW_QUALITY
41 let packed_noise = textureSampleLevel(noise_texture, noise_texture_sampler, tex_coord, 0.0);
43 noise.macro_noise = packed_noise.r;
44 noise.micro_noise = packed_noise.g;
46 let seed_x = bitcast<u32>(instance.instance_position.x);
47 let seed_y = bitcast<u32>(instance.instance_position.y);
48 let seed_z = bitcast<u32>(instance.instance_position.z);
49 var seed = seed_x ^ seed_y ^ seed_z;
51 noise.phase_noise.x = rand_f(&seed);
52 noise.phase_noise.y = rand_f(&seed);
53#endif // WIND_LOW_QUALITY
55#else // NOT WIND_AFFECTED
56 noise.macro_noise = 0.0;
57 noise.micro_noise = 0.0;
58 noise.phase_noise = vec2<f32>(0.0);
59#endif // WIND_AFFECTED
61 return noise;
Contents