| 1 | <h2>Full example</h2> |
| 2 | <pre><code>// Vertex shader |
| 3 | struct CameraUniform { |
| 4 | view_proj: mat4x4<f32>; |
| 5 | }; |
| 6 | [[group(1), binding(0)]] |
| 7 | var<uniform> camera: CameraUniform; |
| 8 | |
| 9 | struct InstanceInput { |
| 10 | [[location(5)]] model_matrix_0: vec4<f32>; |
| 11 | [[location(6)]] model_matrix_1: vec4<f32>; |
| 12 | [[location(7)]] model_matrix_2: vec4<f32>; |
| 13 | [[location(8)]] model_matrix_3: vec4<f32>; |
| 14 | }; |
| 15 | |
| 16 | struct VertexInput { |
| 17 | [[location(0)]] position: vec3<f32>; |
| 18 | [[location(1)]] tex_coords: vec2<f32>; |
| 19 | }; |
| 20 | |
| 21 | struct VertexOutput { |
| 22 | [[builtin(position)]] clip_position: vec4<f32>; |
| 23 | [[location(0)]] tex_coords: vec2<f32>; |
| 24 | }; |
| 25 | |
| 26 | @vertex |
| 27 | [[stage(vertex)]] |
| 28 | fn vs_main( |
| 29 | model: VertexInput, |
| 30 | instance: InstanceInput, |
| 31 | ) -> VertexOutput { |
| 32 | let model_matrix = mat4x4<f32>( |
| 33 | instance.model_matrix_0, |
| 34 | instance.model_matrix_1, |
| 35 | instance.model_matrix_2, |
| 36 | instance.model_matrix_3, |
| 37 | ); |
| 38 | |
| 39 | bool mybool1 = true; |
| 40 | bool mybool2 = false; |
| 41 | |
| 42 | var out: VertexOutput; |
| 43 | out.tex_coords = model.tex_coords; |
| 44 | out.clip_position = camera.view_proj * model_matrix * vec4<f32>(model.position, 1.0); |
| 45 | return out; |
| 46 | } |
| 47 | |
| 48 | // Fragment shader |
| 49 | |
| 50 | [[group(0), binding(0)]] |
| 51 | var t_diffuse: texture_2d<f32>; |
| 52 | [[group(0), binding(1)]] |
| 53 | var s_diffuse: sampler; |
| 54 | |
| 55 | @fragment |
| 56 | [[stage(fragment)]] |
| 57 | fn fs_main(in: VertexOutput) -> [[location(0)]] vec4<f32> { |
| 58 | return textureSample(t_diffuse, s_diffuse, in.tex_coords); |
| 59 | } |
| 60 | </code></pre> |