83 lines
2.0 KiB
WebGPU Shading Language
83 lines
2.0 KiB
WebGPU Shading Language
struct CameraUniform {
|
|
vp: mat4x4<f32>;
|
|
};
|
|
|
|
struct MeshInstance {
|
|
transform: mat4x4<f32>;
|
|
};
|
|
|
|
struct MeshData {
|
|
instances: array<MeshInstance>;
|
|
};
|
|
|
|
struct PointLight {
|
|
center: vec4<f32>;
|
|
intensity: vec4<f32>;
|
|
};
|
|
|
|
struct PointLightData {
|
|
num: i32;
|
|
lights: array<PointLight>;
|
|
};
|
|
|
|
struct VertexInput {
|
|
[[location(0)]] position: vec3<f32>;
|
|
[[location(1)]] normal: vec3<f32>;
|
|
[[location(2)]] tex_coords: vec2<f32>;
|
|
};
|
|
|
|
struct VertexOutput {
|
|
[[builtin(position)]] clip_position: vec4<f32>;
|
|
[[location(0)]] position: vec3<f32>;
|
|
[[location(1)]] normal: vec3<f32>;
|
|
[[location(2)]] tex_coords: vec2<f32>;
|
|
};
|
|
|
|
[[group(0), binding(0)]]
|
|
var<uniform> camera: CameraUniform;
|
|
|
|
[[group(0), binding(1)]]
|
|
var<storage,read> point_lights: PointLightData;
|
|
|
|
[[group(1), binding(0)]]
|
|
var<storage,read> meshes: MeshData;
|
|
|
|
[[group(2), binding(0)]] var t_albedo: texture_2d<f32>;
|
|
[[group(2), binding(1)]] var s_albedo: sampler;
|
|
|
|
[[stage(vertex)]]
|
|
fn vs_main(
|
|
[[builtin(instance_index)]] mesh_idx: u32,
|
|
vertex: VertexInput,
|
|
) -> VertexOutput {
|
|
let transform = meshes.instances[mesh_idx].transform;
|
|
let world_pos = transform * vec4<f32>(vertex.position, 1.0);
|
|
let world_normal = transform * vec4<f32>(vertex.normal, 0.0);
|
|
|
|
var out: VertexOutput;
|
|
out.clip_position = camera.vp * world_pos;
|
|
out.position = world_pos.xyz;
|
|
out.normal = world_normal.xyz;
|
|
out.tex_coords = vertex.tex_coords;
|
|
return out;
|
|
}
|
|
|
|
[[stage(fragment)]]
|
|
fn fs_main(
|
|
frag: VertexOutput,
|
|
) -> [[location(0)]] vec4<f32> {
|
|
let albedo = textureSample(t_albedo, s_albedo, frag.tex_coords).rgb;
|
|
let normal = normalize(frag.normal);
|
|
|
|
var lum = vec3<f32>(0.0);
|
|
for(var i = 0; i < 4; i = i + 1) {
|
|
let light = point_lights.lights[i];
|
|
let light_relative = light.center.xyz - frag.position;
|
|
let received = dot(normal, normalize(light_relative));
|
|
let diffuse = (albedo * received) / dot(light_relative, light_relative);
|
|
lum = lum + (diffuse * light.intensity.rgb);
|
|
}
|
|
|
|
return vec4<f32>(lum, 1.0);
|
|
}
|