1#define_import_path bevy_pbr::mesh_functions 7 VISIBILITY_RANGE_UNIFORM_BUFFER_SIZE 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> { 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, 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), 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) 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; 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> { 76 if any(vertex_normal != vec3<f32>(0.0)) { 78 mat2x4_f32_to_mat3x3_unpack( 79 mesh[instance_index].local_from_world_transpose_a, 80 mesh[instance_index].local_from_world_transpose_b, 88#endif // MESHLET_MESH_MATERIAL_PASS 92fn sign_determinant_model_3x3m(mesh_flags: u32) -> f32 { 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> { 109 if any(vertex_tangent != vec4<f32>(0.0)) { 113 world_from_local[0].xyz, 114 world_from_local[1].xyz, 115 world_from_local[2].xyz, 116 ) * vertex_tangent.xyz 120 vertex_tangent.w * sign_determinant_model_3x3m(mesh[instance_index].flags) 123 return vertex_tangent; 127#endif // MESHLET_MESH_MATERIAL_PASS 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 137 let visibility_buffer_array_len = arrayLength(&visibility_ranges); 138#else // AVAILABLE_STORAGE_BUFFER_BINDINGS >= 6 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) { 148 let lod_range = visibility_ranges[visibility_buffer_index]; 149 let camera_distance = length(view.lod_view_world_position.xyz - world_position.xyz); 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); 164#ifndef MESHLET_MESH_MATERIAL_PASS 165fn get_tag(instance_index: u32) -> u32 { 166 return mesh[instance_index].tag;