import package::{
render::{
pbr_types,
pbr_functions::alpha_discard,
pbr_fragment::pbr_input_from_standard_material,
},
decal::clustered::apply_decals,
};
@if(PREPASS_PIPELINE)
import package::{
prepass::io::{VertexOutput, FragmentOutput},
deferred::functions::deferred_output,
};
@else
import package::render::{
forward_io::{VertexOutput, FragmentOutput},
pbr_functions::{apply_pbr_lighting, main_pass_post_lighting_processing},
pbr_types::STANDARD_MATERIAL_FLAGS_UNLIT_BIT,
};
@if(VISIBILITY_RANGE_DITHER)
import package::render::pbr_functions::visibility_range_dither;
@if(MESHLET_MESH_MATERIAL_PASS)
import package::meshlet::visibility_buffer_resolve::resolve_vertex_output;
@if(MATERIAL_OIT_ENABLED)
import bevy_core_pipeline::oit::draw::oit_draw;
@if(FORWARD_DECAL)
import package::decal::forward::get_forward_decal_info;
@fragment
fn fragment(
@if(MESHLET_MESH_MATERIAL_PASS)
@builtin(position) frag_coord: vec4<f32>,
@if(!MESHLET_MESH_MATERIAL_PASS)
vertex_output: VertexOutput,
@if(!MESHLET_MESH_MATERIAL_PASS)
@builtin(front_facing) is_front: bool,
) -> FragmentOutput {
@if(MESHLET_MESH_MATERIAL_PASS)
let vertex_output = resolve_vertex_output(frag_coord);
@if(MESHLET_MESH_MATERIAL_PASS)
let is_front = true;
var in = vertex_output;
// If we're in the crossfade section of a visibility range, conditionally
// discard the fragment according to the visibility pattern.
@if(VISIBILITY_RANGE_DITHER)
visibility_range_dither(in.position, in.visibility_range_dither);
@if(FORWARD_DECAL)
let forward_decal_info = get_forward_decal_info(in);
@if(FORWARD_DECAL)
in.world_position = forward_decal_info.world_position;
@if(FORWARD_DECAL)
in.uv = forward_decal_info.uv;
// generate a PbrInput struct from the StandardMaterial bindings
var pbr_input = pbr_input_from_standard_material(in, is_front);
// alpha discard
pbr_input.material.base_color = alpha_discard(
pbr_input.material.flags,
pbr_input.material.alpha_cutoff,
pbr_input.material.base_color
);
// clustered decals
apply_decals(&pbr_input);
// write the gbuffer, lighting pass id, and optionally normal and motion_vector textures
@if(PREPASS_PIPELINE)
let out = deferred_output(in, pbr_input);
// in forward mode, we calculate the lit color immediately, and then apply some post-lighting effects here.
// in deferred mode the lit color and these effects will be calculated in the deferred lighting shader
@if(!PREPASS_PIPELINE)
var out: FragmentOutput;
@if(!PREPASS_PIPELINE)
if (pbr_input.material.flags & STANDARD_MATERIAL_FLAGS_UNLIT_BIT) == 0u {
out.color = apply_pbr_lighting(pbr_input);
} else {
out.color = pbr_input.material.base_color;
}
// apply in-shader post processing (fog, alpha-premultiply, and also tonemapping, debanding if the camera is non-hdr)
// note this does not include fullscreen postprocessing effects like bloom.
@if(!PREPASS_PIPELINE)
out.color = main_pass_post_lighting_processing(pbr_input, out.color);
@if(MATERIAL_OIT_ENABLED) {
let alpha_mode = pbr_input.material.flags & pbr_types::STANDARD_MATERIAL_FLAGS_ALPHA_MODE_RESERVED_BITS;
if alpha_mode == pbr_types::STANDARD_MATERIAL_FLAGS_ALPHA_MODE_BLEND {
// The fragments will only be drawn during the oit resolve pass.
oit_draw(in.position, vec4(out.color.rgb * out.color.a, out.color.a));
discard;
}
// Both `Premultiplied` and `Add` colors are premultiplied in `premultiply_alpha()`
if alpha_mode == pbr_types::STANDARD_MATERIAL_FLAGS_ALPHA_MODE_PREMULTIPLIED
|| alpha_mode == pbr_types::STANDARD_MATERIAL_FLAGS_ALPHA_MODE_ADD {
// The fragments will only be drawn during the oit resolve pass.
oit_draw(in.position, out.color);
discard;
}
}
@if(FORWARD_DECAL)
out.color.a = min(forward_decal_info.alpha, out.color.a);
return out;
}