Fix forward decal depth_fade_factor. (#18772)

Fixes #18758
This commit is contained in:
charlotte 2025-04-09 15:00:28 -07:00 committed by GitHub
parent e799625ea5
commit ee7b624024
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 28 additions and 5 deletions

View File

@ -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<B: Material> = ExtendedMaterial<B, ForwardDecalMat
/// The `FORWARD_DECAL` shader define will be made available to your shader so that you can gate
/// the forward decal code behind an ifdef.
#[derive(Asset, AsBindGroup, TypePath, Clone, Debug)]
#[uniform(200, ForwardDecalMaterialExtUniform)]
pub struct ForwardDecalMaterialExt {
/// Controls how far away a surface must be before the decal will stop blending with it, and instead render as opaque.
/// Controls the distance threshold for decal blending with surfaces.
///
/// Decreasing this value will cause the decal to blend only to surfaces closer to it.
/// This parameter determines how far away a surface can be before the decal no longer blends
/// with it and instead renders with full opacity.
///
/// Lower values cause the decal to only blend with close surfaces, while higher values allow
/// blending with more distant surfaces.
///
/// Units are in meters.
#[uniform(200)]
pub depth_fade_factor: f32,
}
#[derive(Clone, Default, ShaderType)]
pub struct ForwardDecalMaterialExtUniform {
pub inv_depth_fade_factor: f32,
}
impl AsBindGroupShaderType<ForwardDecalMaterialExtUniform> for ForwardDecalMaterialExt {
fn as_bind_group_shader_type(
&self,
_images: &RenderAssets<GpuImage>,
) -> ForwardDecalMaterialExtUniform {
ForwardDecalMaterialExtUniform {
inv_depth_fade_factor: 1.0 / self.depth_fade_factor.max(0.001),
}
}
}
impl MaterialExtension for ForwardDecalMaterialExt {
fn alpha_mode() -> Option<AlphaMode> {
Some(AlphaMode::Blend)

View File

@ -11,7 +11,7 @@
#import bevy_render::maths::project_onto
@group(2) @binding(200)
var<uniform> depth_fade_factor: f32;
var<uniform> inv_depth_fade_factor: f32;
struct ForwardDecalInformation {
world_position: vec4<f32>,
@ -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);
}