Examples/DeferredShading: Optimize rendering with light meshes
This commit is contained in:
@@ -4,24 +4,38 @@ struct PointLight
|
||||
color: vec3<f32>,
|
||||
position: vec3<f32>,
|
||||
|
||||
constant: f32,
|
||||
linear: f32,
|
||||
quadratic: f32,
|
||||
radius: f32,
|
||||
invRadius: f32,
|
||||
}
|
||||
|
||||
[layout(std140)]
|
||||
struct SpotLight
|
||||
{
|
||||
transformMatrix: mat4<f32>,
|
||||
|
||||
color: vec3<f32>,
|
||||
position: vec3<f32>,
|
||||
direction: vec3<f32>,
|
||||
|
||||
constant: f32,
|
||||
linear: f32,
|
||||
quadratic: f32,
|
||||
radius: f32,
|
||||
invRadius: f32,
|
||||
|
||||
innerAngle: f32,
|
||||
outerAngle: f32,
|
||||
outerAngle: f32
|
||||
}
|
||||
|
||||
[layout(std140)]
|
||||
struct ViewerData
|
||||
{
|
||||
projectionMatrix: mat4<f32>,
|
||||
invProjectionMatrix: mat4<f32>,
|
||||
viewMatrix: mat4<f32>,
|
||||
invViewMatrix: mat4<f32>,
|
||||
viewProjMatrix: mat4<f32>,
|
||||
invViewProjMatrix: mat4<f32>,
|
||||
renderTargetSize: vec2<f32>,
|
||||
invRenderTargetSize: vec2<f32>,
|
||||
eyePosition: vec3<f32>
|
||||
}
|
||||
|
||||
external
|
||||
@@ -29,40 +43,41 @@ external
|
||||
[binding(0)] colorTexture: sampler2D<f32>,
|
||||
[binding(1)] normalTexture: sampler2D<f32>,
|
||||
[binding(2)] positionTexture: sampler2D<f32>,
|
||||
[binding(3)] lightParameters: uniform<SpotLight>
|
||||
//[binding(3)] lightParameters: uniform<PointLight>
|
||||
[binding(3)] lightParameters: uniform<SpotLight>,
|
||||
[binding(4)] viewerData: uniform<ViewerData>
|
||||
}
|
||||
|
||||
struct FragIn
|
||||
{
|
||||
[builtin(fragcoord)] fragcoord: vec4<f32>
|
||||
}
|
||||
|
||||
[layout(std140)]
|
||||
struct FragOut
|
||||
{
|
||||
[location(0)] color: vec4<f32>
|
||||
}
|
||||
|
||||
[layout(std140)]
|
||||
struct VertIn
|
||||
{
|
||||
[location(0)] pos: vec3<f32>,
|
||||
[location(1)] uv: vec2<f32>
|
||||
[location(0)] pos: vec3<f32>
|
||||
}
|
||||
|
||||
[layout(std140)]
|
||||
struct VertOut
|
||||
{
|
||||
[location(0)] uv: vec2<f32>,
|
||||
[builtin(position)] position: vec4<f32>
|
||||
}
|
||||
|
||||
[entry(frag)]
|
||||
fn main(input: VertOut) -> FragOut
|
||||
fn main(input: FragIn) -> 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 fragcoord = (input.fragcoord).xy * viewerData.invRenderTargetSize;
|
||||
let normal = normalTexture.Sample(fragcoord).xyz * 2.0 - vec3<f32>(1.0, 1.0, 1.0);
|
||||
let position = positionTexture.Sample(fragcoord).xyz;
|
||||
|
||||
let attenuation = compute_attenuation(position, normal);
|
||||
|
||||
let output: FragOut;
|
||||
output.color = vec4<f32>(lightParameters.color, 1.0) * attenuation * colorTexture.Sample(input.uv);
|
||||
output.color = vec4<f32>(lightParameters.color, 1.0) * attenuation * colorTexture.Sample(fragcoord);
|
||||
|
||||
return output;
|
||||
}
|
||||
@@ -71,8 +86,7 @@ fn main(input: VertOut) -> FragOut
|
||||
fn main(input: VertIn) -> VertOut
|
||||
{
|
||||
let output: VertOut;
|
||||
output.uv = input.uv;
|
||||
output.position = vec4<f32>(input.pos, 1.0);
|
||||
output.position = viewerData.projectionMatrix * viewerData.viewMatrix * lightParameters.transformMatrix * vec4<f32>(input.pos, 1.0);
|
||||
|
||||
return output;
|
||||
}
|
||||
@@ -87,7 +101,7 @@ fn compute_attenuation(worldPos: vec3<f32>, normal: vec3<f32>) -> f32
|
||||
let curAngle = dot(lightParameters.direction, -posToLight);
|
||||
let innerMinusOuterAngle = lightParameters.innerAngle - lightParameters.outerAngle;
|
||||
|
||||
let attenuation = 1.0 / (lightParameters.constant + lightParameters.linear * distance + lightParameters.quadratic * (distance * distance));
|
||||
let attenuation = max(1.0 - distance * lightParameters.invRadius, 0.0);
|
||||
attenuation = attenuation * lambert * max((curAngle - lightParameters.outerAngle) / innerMinusOuterAngle, 0.0);
|
||||
|
||||
return attenuation;
|
||||
|
||||
Reference in New Issue
Block a user