2023-10-12 22:10:38 +00:00
|
|
|
#define_import_path bevy_pbr::pbr_prepass_functions
|
|
|
|
|
2023-10-21 11:51:58 +00:00
|
|
|
#import bevy_pbr::{
|
|
|
|
prepass_io::VertexOutput,
|
|
|
|
prepass_bindings::previous_view_proj,
|
|
|
|
mesh_view_bindings::view,
|
|
|
|
pbr_bindings,
|
|
|
|
pbr_types,
|
|
|
|
}
|
2024-02-21 01:11:28 +00:00
|
|
|
#import bevy_render::maths::affine2_to_square
|
2023-10-12 22:10:38 +00:00
|
|
|
|
|
|
|
// Cutoff used for the premultiplied alpha modes BLEND and ADD.
|
|
|
|
const PREMULTIPLIED_ALPHA_CUTOFF = 0.05;
|
|
|
|
|
|
|
|
// We can use a simplified version of alpha_discard() here since we only need to handle the alpha_cutoff
|
2023-10-13 19:12:40 +00:00
|
|
|
fn prepass_alpha_discard(in: VertexOutput) {
|
2023-10-12 22:10:38 +00:00
|
|
|
|
|
|
|
#ifdef MAY_DISCARD
|
|
|
|
var output_color: vec4<f32> = pbr_bindings::material.base_color;
|
|
|
|
|
|
|
|
#ifdef VERTEX_UVS
|
2024-02-21 01:11:28 +00:00
|
|
|
let uv_transform = affine2_to_square(pbr_bindings::material.uv_transform);
|
|
|
|
let uv = (uv_transform * vec3(in.uv, 1.0)).xy;
|
2023-10-12 22:10:38 +00:00
|
|
|
if (pbr_bindings::material.flags & pbr_types::STANDARD_MATERIAL_FLAGS_BASE_COLOR_TEXTURE_BIT) != 0u {
|
2024-02-21 01:11:28 +00:00
|
|
|
output_color = output_color * textureSampleBias(pbr_bindings::base_color_texture, pbr_bindings::base_color_sampler, uv, view.mip_bias);
|
2023-10-12 22:10:38 +00:00
|
|
|
}
|
|
|
|
#endif // VERTEX_UVS
|
|
|
|
|
|
|
|
let alpha_mode = pbr_bindings::material.flags & pbr_types::STANDARD_MATERIAL_FLAGS_ALPHA_MODE_RESERVED_BITS;
|
|
|
|
if alpha_mode == pbr_types::STANDARD_MATERIAL_FLAGS_ALPHA_MODE_MASK {
|
|
|
|
if output_color.a < pbr_bindings::material.alpha_cutoff {
|
|
|
|
discard;
|
|
|
|
}
|
|
|
|
} else if (alpha_mode == pbr_types::STANDARD_MATERIAL_FLAGS_ALPHA_MODE_BLEND || alpha_mode == pbr_types::STANDARD_MATERIAL_FLAGS_ALPHA_MODE_ADD) {
|
|
|
|
if output_color.a < PREMULTIPLIED_ALPHA_CUTOFF {
|
|
|
|
discard;
|
|
|
|
}
|
|
|
|
} else if alpha_mode == pbr_types::STANDARD_MATERIAL_FLAGS_ALPHA_MODE_PREMULTIPLIED {
|
|
|
|
if all(output_color < vec4(PREMULTIPLIED_ALPHA_CUTOFF)) {
|
|
|
|
discard;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // MAY_DISCARD
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef MOTION_VECTOR_PREPASS
|
|
|
|
fn calculate_motion_vector(world_position: vec4<f32>, previous_world_position: vec4<f32>) -> vec2<f32> {
|
|
|
|
let clip_position_t = view.unjittered_view_proj * world_position;
|
|
|
|
let clip_position = clip_position_t.xy / clip_position_t.w;
|
|
|
|
let previous_clip_position_t = previous_view_proj * previous_world_position;
|
|
|
|
let previous_clip_position = previous_clip_position_t.xy / previous_clip_position_t.w;
|
|
|
|
// These motion vectors are used as offsets to UV positions and are stored
|
|
|
|
// in the range -1,1 to allow offsetting from the one corner to the
|
|
|
|
// diagonally-opposite corner in UV coordinates, in either direction.
|
|
|
|
// A difference between diagonally-opposite corners of clip space is in the
|
|
|
|
// range -2,2, so this needs to be scaled by 0.5. And the V direction goes
|
|
|
|
// down where clip space y goes up, so y needs to be flipped.
|
|
|
|
return (clip_position - previous_clip_position) * vec2(0.5, -0.5);
|
|
|
|
}
|
2023-10-13 19:12:40 +00:00
|
|
|
#endif // MOTION_VECTOR_PREPASS
|