@group(0) @binding(0) var gBufferNormal: texture_2d; @group(0) @binding(1) var gBufferAlbedo: texture_2d; @group(0) @binding(2) var gBufferDepth: texture_depth_2d; struct LightData { position : vec4, // TODO - vec3 alignment color_x : f32, color_y : f32, color_z : f32, radius : f32, } struct LightsBuffer { lights: array, } @group(1) @binding(0) var lightsBuffer: LightsBuffer; struct Config { numLights : u32, } struct Camera { viewProjectionMatrix : mat4x4, invViewProjectionMatrix : mat4x4, } @group(1) @binding(1) var config: Config; @group(1) @binding(2) var camera: Camera; fn world_from_screen_coord(coord : vec2, depth_sample: f32) -> vec3 { // reconstruct world-space position from the screen coordinate. let posClip = vec4(coord.x * 2.0 - 1.0, (1.0 - coord.y) * 2.0 - 1.0, depth_sample, 1.0); let posWorldW = camera.invViewProjectionMatrix * posClip; let posWorld = posWorldW.xyz / posWorldW.www; return posWorld; } @fragment fn main( @builtin(position) coord : vec4 ) -> @location(0) vec4 { // TODO - variable initialization var result = vec3(0, 0, 0); let depth = textureLoad( gBufferDepth, vec2(floor(coord.xy)), 0 ); // Don't light the sky. if (depth >= 1.0) { discard; } let bufferSize = textureDimensions(gBufferDepth); let coordUV = coord.xy / vec2(bufferSize); let position = world_from_screen_coord(coordUV, depth); let normal = textureLoad( gBufferNormal, vec2(floor(coord.xy)), 0 ).xyz; let albedo = textureLoad( gBufferAlbedo, vec2(floor(coord.xy)), 0 ).rgb; for (var i = 0u; i < config.numLights; i++) { // TODO - vec3 alignment let lightColor = vec3(lightsBuffer.lights[i].color_x, lightsBuffer.lights[i].color_y, lightsBuffer.lights[i].color_z); let L = lightsBuffer.lights[i].position.xyz - position; let distance = length(L); if (distance > lightsBuffer.lights[i].radius) { continue; } let lambert = max(dot(normal, normalize(L)), 0.0); result += vec3( lambert * pow(1.0 - distance / lightsBuffer.lights[i].radius, 2.0) * lightColor * albedo ); } // some manual ambient result += vec3(0.2); return vec4(result, 1.0); }