From ee7b624024bcc32fd34a0467bbf0af9ecc9f457e Mon Sep 17 00:00:00 2001 From: charlotte Date: Wed, 9 Apr 2025 15:00:28 -0700 Subject: [PATCH] Fix forward decal depth_fade_factor. (#18772) Fixes #18758 --- crates/bevy_pbr/src/decal/forward.rs | 29 ++++++++++++++++++-- crates/bevy_pbr/src/decal/forward_decal.wgsl | 4 +-- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/crates/bevy_pbr/src/decal/forward.rs b/crates/bevy_pbr/src/decal/forward.rs index 862d4b6019..2445c3e723 100644 --- a/crates/bevy_pbr/src/decal/forward.rs +++ b/crates/bevy_pbr/src/decal/forward.rs @@ -7,6 +7,9 @@ use bevy_asset::{load_internal_asset, weak_handle, Asset, Assets, Handle}; use bevy_ecs::component::Component; use bevy_math::{prelude::Rectangle, Quat, Vec2, Vec3}; use bevy_reflect::{Reflect, TypePath}; +use bevy_render::render_asset::RenderAssets; +use bevy_render::render_resource::{AsBindGroupShaderType, ShaderType}; +use bevy_render::texture::GpuImage; use bevy_render::{ alpha::AlphaMode, mesh::{Mesh, Mesh3d, MeshBuilder, MeshVertexBufferLayoutRef, Meshable}, @@ -86,16 +89,36 @@ pub type ForwardDecalMaterial = ExtendedMaterial for ForwardDecalMaterialExt { + fn as_bind_group_shader_type( + &self, + _images: &RenderAssets, + ) -> ForwardDecalMaterialExtUniform { + ForwardDecalMaterialExtUniform { + inv_depth_fade_factor: 1.0 / self.depth_fade_factor.max(0.001), + } + } +} + impl MaterialExtension for ForwardDecalMaterialExt { fn alpha_mode() -> Option { Some(AlphaMode::Blend) diff --git a/crates/bevy_pbr/src/decal/forward_decal.wgsl b/crates/bevy_pbr/src/decal/forward_decal.wgsl index dbc6bbc1c4..ce24d57bf5 100644 --- a/crates/bevy_pbr/src/decal/forward_decal.wgsl +++ b/crates/bevy_pbr/src/decal/forward_decal.wgsl @@ -11,7 +11,7 @@ #import bevy_render::maths::project_onto @group(2) @binding(200) -var depth_fade_factor: f32; +var inv_depth_fade_factor: f32; struct ForwardDecalInformation { world_position: vec4, @@ -46,7 +46,7 @@ fn get_forward_decal_info(in: VertexOutput) -> ForwardDecalInformation { let uv = in.uv + delta_uv; let world_position = vec4(in.world_position.xyz + V * diff_depth_abs, in.world_position.w); - let alpha = saturate(1.0 - normal_depth * depth_fade_factor); + let alpha = saturate(1.0 - (normal_depth * inv_depth_fade_factor)); return ForwardDecalInformation(world_position, uv, alpha); }