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#define_import_path bevy_pbr::mesh_functions
3#import bevy_pbr::{
4 mesh_view_bindings::{
5 view,
6 visibility_ranges,
7 VISIBILITY_RANGE_UNIFORM_BUFFER_SIZE
8 },
9 mesh_bindings::mesh,
10 mesh_types::MESH_FLAGS_SIGN_DETERMINANT_MODEL_3X3_BIT,
11 view_transformations::position_world_to_clip,
13#import bevy_render::maths::{affine3_to_square, mat2x4_f32_to_mat3x3_unpack}
15#ifndef MESHLET_MESH_MATERIAL_PASS
17fn get_world_from_local(instance_index: u32) -> mat4x4<f32> {
18 return affine3_to_square(mesh[instance_index].world_from_local);
21fn get_previous_world_from_local(instance_index: u32) -> mat4x4<f32> {
22 return affine3_to_square(mesh[instance_index].previous_world_from_local);
25fn get_local_from_world(instance_index: u32) -> mat4x4<f32> {
26 // the model matrix is translation * rotation * scale
27 // the inverse is then scale^-1 * rotation ^-1 * translation^-1
28 // the 3x3 matrix only contains the information for the rotation and scale
29 let inverse_model_3x3 = transpose(mat2x4_f32_to_mat3x3_unpack(
30 mesh[instance_index].local_from_world_transpose_a,
31 mesh[instance_index].local_from_world_transpose_b,
32 ));
33 // construct scale^-1 * rotation^-1 from the 3x3
34 let inverse_model_4x4_no_trans = mat4x4<f32>(
35 vec4(inverse_model_3x3[0], 0.0),
36 vec4(inverse_model_3x3[1], 0.0),
37 vec4(inverse_model_3x3[2], 0.0),
38 vec4(0.0,0.0,0.0,1.0)
39 );
40 // we can get translation^-1 by negating the translation of the model
41 let model = get_world_from_local(instance_index);
42 let inverse_model_4x4_only_trans = mat4x4<f32>(
43 vec4(1.0,0.0,0.0,0.0),
44 vec4(0.0,1.0,0.0,0.0),
45 vec4(0.0,0.0,1.0,0.0),
46 vec4(-model[3].xyz, 1.0)
47 );
49 return inverse_model_4x4_no_trans * inverse_model_4x4_only_trans;
52#endif // MESHLET_MESH_MATERIAL_PASS
54fn mesh_position_local_to_world(world_from_local: mat4x4<f32>, vertex_position: vec4<f32>) -> vec4<f32> {
55 return world_from_local * vertex_position;
58// NOTE: The intermediate world_position assignment is important
59// for precision purposes when using the 'equals' depth comparison
60// function.
61fn mesh_position_local_to_clip(world_from_local: mat4x4<f32>, vertex_position: vec4<f32>) -> vec4<f32> {
62 let world_position = mesh_position_local_to_world(world_from_local, vertex_position);
63 return position_world_to_clip(world_position.xyz);
66#ifndef MESHLET_MESH_MATERIAL_PASS
68fn mesh_normal_local_to_world(vertex_normal: vec3<f32>, instance_index: u32) -> vec3<f32> {
69 // NOTE: The mikktspace method of normal mapping requires that the world normal is
70 // re-normalized in the vertex shader to match the way mikktspace bakes vertex tangents
71 // and normal maps so that the exact inverse process is applied when shading. Blender, Unity,
72 // Unreal Engine, Godot, and more all use the mikktspace method.
73 // We only skip normalization for invalid normals so that they don't become NaN.
74 // Do not change this code unless you really know what you are doing.
75 // http://www.mikktspace.com/
76 if any(vertex_normal != vec3<f32>(0.0)) {
77 return normalize(
78 mat2x4_f32_to_mat3x3_unpack(
79 mesh[instance_index].local_from_world_transpose_a,
80 mesh[instance_index].local_from_world_transpose_b,
81 ) * vertex_normal
82 );
83 } else {
84 return vertex_normal;
85 }
88#endif // MESHLET_MESH_MATERIAL_PASS
90// Calculates the sign of the determinant of the 3x3 model matrix based on a
91// mesh flag
92fn sign_determinant_model_3x3m(mesh_flags: u32) -> f32 {
93 // bool(u32) is false if 0u else true
94 // f32(bool) is 1.0 if true else 0.0
95 // * 2.0 - 1.0 remaps 0.0 or 1.0 to -1.0 or 1.0 respectively
96 return f32(bool(mesh_flags & MESH_FLAGS_SIGN_DETERMINANT_MODEL_3X3_BIT)) * 2.0 - 1.0;
99#ifndef MESHLET_MESH_MATERIAL_PASS
101fn mesh_tangent_local_to_world(world_from_local: mat4x4<f32>, vertex_tangent: vec4<f32>, instance_index: u32) -> vec4<f32> {
102 // NOTE: The mikktspace method of normal mapping requires that the world tangent is
103 // re-normalized in the vertex shader to match the way mikktspace bakes vertex tangents
104 // and normal maps so that the exact inverse process is applied when shading. Blender, Unity,
105 // Unreal Engine, Godot, and more all use the mikktspace method.
106 // We only skip normalization for invalid tangents so that they don't become NaN.
107 // Do not change this code unless you really know what you are doing.
108 // http://www.mikktspace.com/
109 if any(vertex_tangent != vec4<f32>(0.0)) {
110 return vec4<f32>(
111 normalize(
112 mat3x3<f32>(
113 world_from_local[0].xyz,
114 world_from_local[1].xyz,
115 world_from_local[2].xyz,
116 ) * vertex_tangent.xyz
117 ),
118 // NOTE: Multiplying by the sign of the determinant of the 3x3 model matrix accounts for
119 // situations such as negative scaling.
120 vertex_tangent.w * sign_determinant_model_3x3m(mesh[instance_index].flags)
121 );
122 } else {
123 return vertex_tangent;
124 }
127#endif // MESHLET_MESH_MATERIAL_PASS
129// Returns an appropriate dither level for the current mesh instance.
130//
131// This looks up the LOD range in the `visibility_ranges` table and compares the
132// camera distance to determine the dithering level.
133#ifdef VISIBILITY_RANGE_DITHER
134fn get_visibility_range_dither_level(instance_index: u32, world_position: vec4<f32>) -> i32 {
135#if AVAILABLE_STORAGE_BUFFER_BINDINGS >= 6
136 // If we're using a storage buffer, then the length is variable.
137 let visibility_buffer_array_len = arrayLength(&visibility_ranges);
138#else // AVAILABLE_STORAGE_BUFFER_BINDINGS >= 6
139 // If we're using a uniform buffer, then the length is constant
140 let visibility_buffer_array_len = VISIBILITY_RANGE_UNIFORM_BUFFER_SIZE;
141#endif // AVAILABLE_STORAGE_BUFFER_BINDINGS >= 6
143 let visibility_buffer_index = mesh[instance_index].flags & 0xffffu;
144 if (visibility_buffer_index > visibility_buffer_array_len) {
145 return -16;
146 }
148 let lod_range = visibility_ranges[visibility_buffer_index];
149 let camera_distance = length(view.world_position.xyz - world_position.xyz);
151 // This encodes the following mapping:
152 //
153 // `lod_range.` x y z w camera distance
154 // ←───────┼────────┼────────┼────────┼────────→
155 // LOD level -16 -16 0 0 16 16 LOD level
156 let offset = select(-16, 0, camera_distance >= lod_range.z);
157 let bounds = select(lod_range.xy, lod_range.zw, camera_distance >= lod_range.z);
158 let level = i32(round((camera_distance - bounds.x) / (bounds.y - bounds.x) * 16.0));
159 return offset + clamp(level, 0, 16);
161#endif
164#ifndef MESHLET_MESH_MATERIAL_PASS
165fn get_tag(instance_index: u32) -> u32 {
166 return mesh[instance_index].tag;
168#endif
Contents