Galactica/src/render/shaders/shader.wgsl

60 lines
1.3 KiB
WebGPU Shading Language
Raw Normal View History

2023-12-22 16:51:21 -08:00
struct InstanceInput {
@location(5) transform_matrix_0: vec4<f32>,
@location(6) transform_matrix_1: vec4<f32>,
@location(7) transform_matrix_2: vec4<f32>,
@location(8) transform_matrix_3: vec4<f32>,
@location(9) texture_idx: u32,
};
// Vertex shader
struct VertexInput {
@location(0) position: vec3<f32>,
@location(1) tex_coords: vec2<f32>,
}
struct VertexOutput {
@builtin(position) clip_position: vec4<f32>,
@location(0) tex_coords: vec2<f32>,
@location(1) index: u32,
}
@vertex
fn vertex_shader_main(
model: VertexInput,
instance: InstanceInput,
) -> VertexOutput {
let transform_matrix = mat4x4<f32>(
instance.transform_matrix_0,
instance.transform_matrix_1,
instance.transform_matrix_2,
instance.transform_matrix_3,
);
var out: VertexOutput;
out.tex_coords = model.tex_coords;
out.clip_position = transform_matrix * vec4<f32>(model.position, 1.0);
out.index = instance.texture_idx;
return out;
}
// Fragment shader
@group(0) @binding(0)
var texture_array: binding_array<texture_2d<f32>>;
@group(0) @binding(1)
var sampler_array: binding_array<sampler>;
@fragment
fn fragment_shader_main(in: VertexOutput) -> @location(0) vec4<f32> {
return textureSampleLevel(
texture_array[in.index],
sampler_array[in.index],
in.tex_coords,
0.0
).rgba;
}