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.
1import bevy_pbr::render::{
2 forward_io::VertexOutput,
3 mesh_functions,
4 mesh_view_bindings::view,
5 pbr_types::{STANDARD_MATERIAL_FLAGS_DOUBLE_SIDED_BIT, PbrInput, pbr_input_new},
6 pbr_functions as fns,
7 pbr_bindings,
8};
9import bevy_core_pipeline::tonemapping::tone_mapping;
11@group(constants::MATERIAL_BIND_GROUP) @binding(0) var my_array_texture: texture_2d_array<f32>;
12@group(constants::MATERIAL_BIND_GROUP) @binding(1) var my_array_texture_sampler: sampler;
14@fragment
15fn fragment(
16 @builtin(front_facing) is_front: bool,
17 mesh: VertexOutput,
18) -> @location(0) vec4<f32> {
19 // Determine which layer of the array texture to sample from based on the
20 // mesh tag which originates from the MeshTag component on the entity.
21 let layer = mesh_functions::get_tag(mesh.instance_index);
23 // Prepare a 'processed' StandardMaterial by sampling all textures to resolve
24 // the material members
25 var pbr_input: PbrInput = pbr_input_new();
27 pbr_input.material.base_color = textureSample(my_array_texture, my_array_texture_sampler, mesh.uv, layer);
28 @if(VERTEX_COLORS)
29 pbr_input.material.base_color = pbr_input.material.base_color * mesh.color;
31 let double_sided = (pbr_input.material.flags & STANDARD_MATERIAL_FLAGS_DOUBLE_SIDED_BIT) != 0u;
33 pbr_input.frag_coord = mesh.position;
34 pbr_input.world_position = mesh.world_position;
35 pbr_input.world_normal = fns::prepare_world_normal(
36 mesh.world_normal,
37 double_sided,
38 is_front,
39 );
41 pbr_input.is_orthographic = view.clip_from_view[3].w == 1.0;
43 pbr_input.N = normalize(pbr_input.world_normal);
45@if(VERTEX_TANGENTS) {
46 let Nt = textureSampleBias(pbr_bindings::normal_map_texture, pbr_bindings::normal_map_sampler, mesh.uv, view.mip_bias).rgb;
47 let TBN = fns::calculate_tbn_mikktspace(mesh.world_normal, mesh.world_tangent);
48 pbr_input.N = fns::apply_normal_mapping(
49 pbr_input.material.flags,
50 TBN,
51 double_sided,
52 is_front,
53 Nt,
54 );
57 pbr_input.V = fns::calculate_view(mesh.world_position, pbr_input.is_orthographic);
59 return tone_mapping(fns::apply_pbr_lighting(pbr_input), view.color_grading);
Contents