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_pbr::decal::forward
3#import bevy_pbr::{
4 forward_io::VertexOutput,
5 mesh_functions::get_world_from_local,
6 mesh_view_bindings::view,
7 pbr_functions::calculate_tbn_mikktspace,
8 prepass_utils::prepass_depth,
9 view_transformations::depth_ndc_to_view_z,
11#import bevy_render::maths::project_onto
13@group(#{MATERIAL_BIND_GROUP}) @binding(200)
14var<uniform> inv_depth_fade_factor: f32;
16struct ForwardDecalInformation {
17 world_position: vec4<f32>,
18 uv: vec2<f32>,
19 alpha: f32,
22fn get_forward_decal_info(in: VertexOutput) -> ForwardDecalInformation {
23 let world_from_local = get_world_from_local(in.instance_index);
24 let scale = (world_from_local * vec4(1.0, 1.0, 1.0, 0.0)).xyz;
25 let scaled_tangent = vec4(in.world_tangent.xyz / scale, in.world_tangent.w);
27 let V = normalize(view.world_position - in.world_position.xyz);
29 // Transform V from fragment to camera in world space to tangent space.
30 let TBN = calculate_tbn_mikktspace(in.world_normal, scaled_tangent);
31 let T = TBN[0];
32 let B = TBN[1];
33 let N = TBN[2];
34 let Vt = vec3(dot(V, T), dot(V, B), dot(V, N));
36 let frag_depth = depth_ndc_to_view_z(in.position.z);
37 let depth_pass_depth = depth_ndc_to_view_z(prepass_depth(in.position, 0u));
38 let diff_depth = frag_depth - depth_pass_depth;
39 let diff_depth_abs = abs(diff_depth);
41 // Apply UV parallax
42 let contact_on_decal = project_onto(V * diff_depth, in.world_normal);
43 let normal_depth = length(contact_on_decal);
44 let view_steepness = abs(Vt.z);
45 let delta_uv = normal_depth * Vt.xy * vec2(1.0, -1.0) / view_steepness;
46 let uv = in.uv + delta_uv;
48 let world_position = vec4(in.world_position.xyz + V * diff_depth_abs, in.world_position.w);
49 let alpha = saturate(1.0 - (normal_depth * inv_depth_fade_factor));
51 return ForwardDecalInformation(world_position, uv, alpha);
Contents