3 forward_io::VertexOutput, 4 mesh_functions::get_world_from_local, 5 mesh_view_bindings::view, 6 pbr_functions::calculate_tbn_mikktspace, 7 view_transformations::depth_ndc_to_view_z, 9 prepass::utils::prepass_depth, 11import bevy_render::maths::project_onto; 13@group(constants::MATERIAL_BIND_GROUP) @binding(200) 14var<uniform> inv_depth_fade_factor: f32; 16struct ForwardDecalInformation { 17 world_position: vec4<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); 30 let TBN = calculate_tbn_mikktspace(in.world_normal, scaled_tangent); 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); 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);