Search across packages, versions, files, declarations, and comments. Use multiple terms to narrow results (AND), and add @vertex, @fragment, or @compute to filter by shader stage.
1#import bevy_pbr::mesh_vertex_output as OutputTypes
2#import bevy_pbr::pbr_functions as PbrCore
3#import bevy_pbr::pbr_bindings as MaterialBindings
4#import bevy_pbr::pbr_types as PbrTypes
5#import bevy_pbr::mesh_view_bindings as ViewBindings
7@fragment
8fn fragment(
9 mesh: OutputTypes::MeshVertexOutput,
10 @builtin(front_facing) is_front: bool,
11 @builtin(position) frag_coord: vec4<f32>,
12) -> @location(0) vec4<f32> {
13 var output_color: vec4<f32> = MaterialBindings::material.base_color;
15#ifdef VERTEX_COLORS
16 output_color = output_color * mesh.color;
17#endif
18#ifdef VERTEX_UVS
19 if ((MaterialBindings::material.flags & PbrTypes::STANDARD_MATERIAL_FLAGS_BASE_COLOR_TEXTURE_BIT) != 0u) {
20 output_color = output_color * textureSample(MaterialBindings::base_color_texture, MaterialBindings::base_color_sampler, mesh.uv);
21 }
22#endif
24 // NOTE: Unlit bit not set means == 0 is true, so the true case is if lit
25 if ((MaterialBindings::material.flags & PbrTypes::STANDARD_MATERIAL_FLAGS_UNLIT_BIT) == 0u) {
26 // Prepare a 'processed' StandardMaterial by sampling all textures to resolve
27 // the material members
28 var pbr_input: PbrCore::PbrInput;
30 pbr_input.material.base_color = output_color;
31 pbr_input.material.reflectance = MaterialBindings::material.reflectance;
32 pbr_input.material.flags = MaterialBindings::material.flags;
33 pbr_input.material.alpha_cutoff = MaterialBindings::material.alpha_cutoff;
35 // TODO use .a for exposure compensation in HDR
36 var emissive: vec4<f32> = MaterialBindings::material.emissive;
37#ifdef VERTEX_UVS
38 if ((MaterialBindings::material.flags & PbrTypes::STANDARD_MATERIAL_FLAGS_EMISSIVE_TEXTURE_BIT) != 0u) {
39 emissive = vec4<f32>(emissive.rgb * textureSample(MaterialBindings::emissive_texture, MaterialBindings::emissive_sampler, mesh.uv).rgb, 1.0);
40 }
41#endif
42 pbr_input.material.emissive = emissive;
44 var metallic: f32 = MaterialBindings::material.metallic;
45 var perceptual_roughness: f32 = MaterialBindings::material.perceptual_roughness;
46#ifdef VERTEX_UVS
47 if ((MaterialBindings::material.flags & PbrTypes::STANDARD_MATERIAL_FLAGS_METALLIC_ROUGHNESS_TEXTURE_BIT) != 0u) {
48 let metallic_roughness = textureSample(MaterialBindings::metallic_roughness_texture, MaterialBindings::metallic_roughness_sampler, mesh.uv);
49 // Sampling from GLTF standard channels for now
50 metallic = metallic * metallic_roughness.b;
51 perceptual_roughness = perceptual_roughness * metallic_roughness.g;
52 }
53#endif
54 pbr_input.material.metallic = metallic;
55 pbr_input.material.perceptual_roughness = perceptual_roughness;
57 var occlusion: f32 = 1.0;
58#ifdef VERTEX_UVS
59 if ((MaterialBindings::material.flags & PbrTypes::STANDARD_MATERIAL_FLAGS_OCCLUSION_TEXTURE_BIT) != 0u) {
60 occlusion = textureSample(MaterialBindings::occlusion_texture, MaterialBindings::occlusion_sampler, mesh.uv).r;
61 }
62#endif
63 pbr_input.occlusion = occlusion;
65 pbr_input.frag_coord = frag_coord;
66 pbr_input.world_position = mesh.world_position;
67 pbr_input.world_normal = mesh.world_normal;
69 pbr_input.is_orthographic = ViewBindings::view.projection[3].w == 1.0;
71 pbr_input.N = PbrCore::prepare_normal(
72 MaterialBindings::material.flags,
73 mesh.world_normal,
74#ifdef VERTEX_TANGENTS
75 #ifdef STANDARDMATERIAL_NORMAL_MAP
76 mesh.world_tangent,
77 #endif
78#endif
79#ifdef VERTEX_UVS
80 mesh.uv,
81#endif
82 is_front,
83 );
84 pbr_input.V = PbrCore::calculate_view(mesh.world_position, pbr_input.is_orthographic);
86 output_color = PbrCore::tone_mapping(PbrCore::pbr(pbr_input));
87 }
89 return output_color;
Contents