1//! Very simple shader used to demonstrate how to get the world position and pass data2//! between the vertex and fragment shader. Also shows the custom vertex layout.4// First we import everything we need from bevy_pbr5// A 2D shader would be very similar but import from bevy_sprite_render instead6import bevy_pbr::render::{7 mesh_functions,8 view_transformations::position_world_to_clip9};11struct Vertex {12 // This is needed if you are using batching and/or gpu preprocessing13 // It's a built in so you don't need to define it in the vertex layout14 @builtin(instance_index) instance_index: u32,15 // Like we defined for the vertex layout16 // position is at location 017 @if(VERTEX_POSITIONS_COMPRESSED)18 @location(0) compressed_position: vec4<f32>,19 @else20 @location(0) position: vec3<f32>,21 // and color at location 122 @if(VERTEX_COLORS)23 @location(1) color: vec4<f32>,24};26/// This is the output of the vertex shader and we also use it as the input for the fragment shader27struct VertexOutput {28 @builtin(position) clip_position: vec4<f32>,29 @location(0) world_position: vec4<f32>,30 @if(VERTEX_COLORS)31 @location(1) color: vec3<f32>,32};34@vertex35fn vertex(vertex: Vertex) -> VertexOutput {36 var out: VertexOutput;37 // This is how bevy computes the world position38 // The vertex.instance_index is very important. Especially if you are using batching and gpu preprocessing39 var world_from_local = mesh_functions::get_world_from_local(vertex.instance_index);40@if(VERTEX_POSITIONS_COMPRESSED) {41 let mesh_metadata = bevy_pbr::render::mesh_functions::get_metadata(vertex.instance_index);42 let vertex_position = bevy_render::utils::decompress_vertex_position(vertex.compressed_position, mesh_metadata.aabb_center, mesh_metadata.aabb_half_extents);43}44@else {45 let vertex_position = vertex.position;46}47 out.world_position = mesh_functions::mesh_position_local_to_world(world_from_local, vec4(vertex_position, 1.0));48 out.clip_position = position_world_to_clip(out.world_position.xyz);50 @if(VERTEX_COLORS)51 out.color = vertex.color.rgb;53 return out;54}56@fragment57fn fragment(in: VertexOutput) -> @location(0) vec4<f32> {58 // We just use the raw vertex color if the mesh provides one, otherwise59 // we fall back to a hardcoded color (e.g. for the rectangle mesh,60 // which has no vertex color attribute).61 @if(VERTEX_COLORS)62 let color = in.color;63 @else64 let color = vec3<f32>(1.0, 1.0, 1.0);66 // output the color directly67 return vec4(color, 1.0);68}