1//! This shader is used for the compute_mesh example2//! The actual work it does is not important for the example and3//! has been hardcoded to return a cube mesh5struct DataRanges {6 /// the starting offset of the mesh data in the *vertex_data* storage buffer7 vertex_start: u32,8 vertex_end: u32,9 /// the starting offset of the index data in the *index_data* storage buffer10 index_start: u32,11 index_end: u32,12}14@group(0) @binding(0) var<uniform> data_range: DataRanges;15@group(0) @binding(1) var<storage, read_write> vertex_data: array<f32>;16@group(0) @binding(2) var<storage, read_write> index_data: array<u32>;18@compute @workgroup_size(1)19fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {20 // this loop is iterating over the full list of (position, normal, uv)21 // data what we have in `vertices`.22 // `192` is used because arrayLength on const arrays doesn't work23 for (var i = 0u; i < 192; i++) {24 // The vertex_data buffer is bigger than just the mesh we're25 // processing because Bevy stores meshes in the mesh_allocator26 // which allocates slabs that each can contain multiple meshes.27 // This buffer is one slab, and data_range.vertex_start is the28 // starting offset for the mesh we care about.29 // So the 0 starting value in the for loop is added to30 // data_range.vertex_start which means we start writing at the31 // correct offset.32 //33 // The "end" of the available space to write into is known by us34 // ahead of time in this example, so we know this has enough space,35 // but you may wish to also check to make sure you are not writing36 // past the end of the range *because you should not write past the37 // end of the range ever*. Doing this can overwrite a different38 // mesh's data.39 vertex_data[i + data_range.vertex_start] = vertices[i];40 }41 // `36` is the length of the `indices` array42 for (var i = 0u; i < 36; i++) {43 // This is doing the same as the vertex_data offset described above44 index_data[i + data_range.index_start] = u32(indices[i]);45 }46}48// hardcoded compute shader data.49/// half the size of the cube50const half_size = vec3(1.5);51const min = -half_size;52const max = half_size;54// Suppose Y-up right hand, and camera look from +Z to -Z55const vertices = array(56 // xyz, normal.xyz, uv.xy57 // Front58 min.x, min.y, max.z, 0.0, 0.0, 1.0, 0.0, 0.0,59 max.x, min.y, max.z, 0.0, 0.0, 1.0, 1.0, 0.0,60 max.x, max.y, max.z, 0.0, 0.0, 1.0, 1.0, 1.0,61 min.x, max.y, max.z, 0.0, 0.0, 1.0, 0.0, 1.0,62 // Back63 min.x, max.y, min.z, 0.0, 0.0, -1.0, 1.0, 0.0,64 max.x, max.y, min.z, 0.0, 0.0, -1.0, 0.0, 0.0,65 max.x, min.y, min.z, 0.0, 0.0, -1.0, 0.0, 1.0,66 min.x, min.y, min.z, 0.0, 0.0, -1.0, 1.0, 1.0,67 // Right68 max.x, min.y, min.z, 1.0, 0.0, 0.0, 0.0, 0.0,69 max.x, max.y, min.z, 1.0, 0.0, 0.0, 1.0, 0.0,70 max.x, max.y, max.z, 1.0, 0.0, 0.0, 1.0, 1.0,71 max.x, min.y, max.z, 1.0, 0.0, 0.0, 0.0, 1.0,72 // Left73 min.x, min.y, max.z, -1.0, 0.0, 0.0, 1.0, 0.0,74 min.x, max.y, max.z, -1.0, 0.0, 0.0, 0.0, 0.0,75 min.x, max.y, min.z, -1.0, 0.0, 0.0, 0.0, 1.0,76 min.x, min.y, min.z, -1.0, 0.0, 0.0, 1.0, 1.0,77 // Top78 max.x, max.y, min.z, 0.0, 1.0, 0.0, 1.0, 0.0,79 min.x, max.y, min.z, 0.0, 1.0, 0.0, 0.0, 0.0,80 min.x, max.y, max.z, 0.0, 1.0, 0.0, 0.0, 1.0,81 max.x, max.y, max.z, 0.0, 1.0, 0.0, 1.0, 1.0,82 // Bottom83 max.x, min.y, max.z, 0.0, -1.0, 0.0, 0.0, 0.0,84 min.x, min.y, max.z, 0.0, -1.0, 0.0, 1.0, 0.0,85 min.x, min.y, min.z, 0.0, -1.0, 0.0, 1.0, 1.0,86 max.x, min.y, min.z, 0.0, -1.0, 0.0, 0.0, 1.087);89const indices = array(90 0, 1, 2, 2, 3, 0, // front91 4, 5, 6, 6, 7, 4, // back92 8, 9, 10, 10, 11, 8, // right93 12, 13, 14, 14, 15, 12, // left94 16, 17, 18, 18, 19, 16, // top95 20, 21, 22, 22, 23, 20, // bottom96);