35 lines
985 B
Plaintext
35 lines
985 B
Plaintext
[layout(std140)]
|
|
struct PointLight
|
|
{
|
|
color: vec3<f32>,
|
|
position: vec3<f32>,
|
|
|
|
constant: f32,
|
|
linear: f32,
|
|
quadratic: f32,
|
|
}
|
|
|
|
external
|
|
{
|
|
[binding(0)] colorTexture: sampler2D<f32>,
|
|
[binding(1)] normalTexture: sampler2D<f32>,
|
|
[binding(2)] positionTexture: sampler2D<f32>,
|
|
[binding(3)] lightParameters: uniform<PointLight>
|
|
}
|
|
|
|
[entry(frag)]
|
|
fn main(input: VertOut) -> FragOut
|
|
{
|
|
let normal = normalTexture.Sample(input.uv).xyz * 2.0 - vec3<f32>(1.0, 1.0, 1.0);
|
|
let position = positionTexture.Sample(input.uv).xyz;
|
|
|
|
let distance = length(lightParameters.position - position);
|
|
let lightDir = (lightParameters.position - position) / distance;
|
|
let lightFactor = dot(normal, lightDir);
|
|
let attenuation = 1.0 / (lightParameters.constant + lightParameters.linear * distance + lightParameters.quadratic * (distance * distance));
|
|
|
|
let output: FragOut;
|
|
output.color = vec4<f32>(lightParameters.color, 1.0) * lightFactor * attenuation * colorTexture.Sample(input.uv);
|
|
|
|
return output;
|
|
} |