Back home 🏡 WGSL Spec Bevy WGSL Functions

pbr_functions.wgsl

Import path

Structures

SampleBias

#
Biasing info needed to sample from a texture when calling sample_texture. How this is done depends on whether we're rendering meshlets or regular meshes.
Name Type Shader Def
ddx_uv vec2<f32> 🟢 MESHLET_MESH_MATERIAL_PASS
ddy_uv vec2<f32> 🟢 MESHLET_MESH_MATERIAL_PASS
mip_bias f32 🔴 MESHLET_MESH_MATERIAL_PASS

Functions

visibility_range_dither

#

Shader defs:

🟢 VISIBILITY_RANGE_DITHER

fn visibility_range_dither(frag_coord: vec4<f32>, dither: i32)
Parameter Type
frag_coord vec4<f32>
dither i32

alpha_discard

#
fn alpha_discard(material: StandardMaterial, output_color: vec4<f32>) -> vec4<f32>
Parameter Type
material StandardMaterial
output_color vec4<f32>
Returns: vec4<f32>

sample_texture

#
Samples a texture using the appropriate biasing metric for the type of mesh in use (mesh vs. meshlet).
fn sample_texture(texture: texture_2d<f32>, samp: sampler, uv: vec2<f32>, bias: SampleBias) -> vec4<f32>
Parameter Type
texture texture_2d<f32>
samp sampler
uv vec2<f32>
bias SampleBias
Returns: vec4<f32>

prepare_world_normal

#
fn prepare_world_normal(world_normal: vec3<f32>, double_sided: bool, is_front: bool) -> vec3<f32>
Parameter Type
world_normal vec3<f32>
double_sided bool
is_front bool
Returns: vec3<f32>

calculate_tbn_mikktspace

#
Calculates the three TBN vectors according to [mikktspace]. Returns a matrix with T, B, N columns in that order. [mikktspace]: http://www.mikktspace.com/
fn calculate_tbn_mikktspace(world_normal: vec3<f32>, world_tangent: vec4<f32>) -> mat3x3<f32>
Parameter Type
world_normal vec3<f32>
world_tangent vec4<f32>
Returns: mat3x3<f32>

apply_normal_mapping

#
fn apply_normal_mapping(standard_material_flags: u32, TBN: mat3x3<f32>, double_sided: bool, is_front: bool, in_Nt: vec3<f32>) -> vec3<f32>
Parameter Type
standard_material_flags u32
TBN mat3x3<f32>
double_sided bool
is_front bool
in_Nt vec3<f32>
Returns: vec3<f32>

bend_normal_for_anisotropy

#

Shader defs:

🟢 STANDARD_MATERIAL_ANISOTROPY

Modifies the normal to achieve a better approximate direction from the environment map when using anisotropy. This follows the suggested implementation in the KHR_materials_anisotropy specification: https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_materials_anisotropy/README.md#image-based-lighting
fn bend_normal_for_anisotropy(lighting_input: LightingInput>)
Parameter Type
lighting_input LightingInput>

calculate_view

#
NOTE: Correctly calculates the view vector depending on whether the projection is orthographic or perspective.
fn calculate_view(world_position: vec4<f32>, is_orthographic: bool) -> vec3<f32>
Parameter Type
world_position vec4<f32>
is_orthographic bool
Returns: vec3<f32>

calculate_diffuse_color

#
Diffuse strength is inversely related to metallicity, specular and diffuse transmission
fn calculate_diffuse_color(base_color: vec3<f32>, metallic: f32, specular_transmission: f32, diffuse_transmission: f32) -> vec3<f32>
Parameter Type
base_color vec3<f32>
metallic f32
specular_transmission f32
diffuse_transmission f32
Returns: vec3<f32>

calculate_F0

#
Remapping [0,1] reflectance to F0 See https://google.github.io/filament/Filament.html#materialsystem/parameterization/remapping
fn calculate_F0(base_color: vec3<f32>, metallic: f32, reflectance: f32) -> vec3<f32>
Parameter Type
base_color vec3<f32>
metallic f32
reflectance f32
Returns: vec3<f32>

apply_pbr_lighting

#
fn apply_pbr_lighting(in: PbrInput) -> vec4<f32>
Parameter Type
in PbrInput
Returns: vec4<f32>

apply_fog

#
fn apply_fog(fog_params: Fog, input_color: vec4<f32>, fragment_world_position: vec3<f32>, view_world_position: vec3<f32>) -> vec4<f32>
Parameter Type
fog_params Fog
input_color vec4<f32>
fragment_world_position vec3<f32>
view_world_position vec3<f32>
Returns: vec4<f32>

premultiply_alpha

#

Shader defs:

🟢 PREMULTIPLY_ALPHA

fn premultiply_alpha(standard_material_flags: u32, color: vec4<f32>) -> vec4<f32>
Parameter Type
standard_material_flags u32
color vec4<f32>
Returns: vec4<f32>

main_pass_post_lighting_processing

#
fog, alpha premultiply for non-hdr cameras, tonemapping and debanding
fn main_pass_post_lighting_processing(pbr_input: PbrInput, input_color: vec4<f32>) -> vec4<f32>
Parameter Type
pbr_input PbrInput
input_color vec4<f32>
Returns: vec4<f32>