From 9bd7f84d26066ebca392afe031d08a9d58505b7e Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Sat, 12 Dec 2020 02:36:53 -0800 Subject: [PATCH] flat sprite lighting in deferred pipeline --- Effects/DeferredPBR_PointLightEffect.cs | 14 +-------- Effects/FXB/DeferredPBR_PointLightEffect.fxb | 4 +-- Effects/HLSL/DeferredPBR_PointLightEffect.fx | 31 ++++++++++++-------- 3 files changed, 22 insertions(+), 27 deletions(-) diff --git a/Effects/DeferredPBR_PointLightEffect.cs b/Effects/DeferredPBR_PointLightEffect.cs index 6a85806..63e4af2 100644 --- a/Effects/DeferredPBR_PointLightEffect.cs +++ b/Effects/DeferredPBR_PointLightEffect.cs @@ -18,8 +18,6 @@ namespace Kav EffectParameter farPlaneParam; - EffectParameter worldParam; - EffectParameter worldInverseParam; EffectParameter worldViewProjectionParam; public Texture2D GPosition { get; set; } @@ -47,7 +45,7 @@ namespace Kav set { world = value; - dirtyFlags |= EffectDirtyFlags.WorldViewProj | EffectDirtyFlags.World; + dirtyFlags |= EffectDirtyFlags.WorldViewProj; } } @@ -114,14 +112,6 @@ namespace Kav farPlaneParam.SetValue(FarPlane); - if ((dirtyFlags & EffectDirtyFlags.World) != 0) - { - worldParam.SetValue(world); - worldInverseParam.SetValue(Matrix.Invert(world)); - - dirtyFlags &= ~EffectDirtyFlags.World; - } - if ((dirtyFlags & EffectDirtyFlags.WorldViewProj) != 0) { worldViewProjectionParam.SetValue(world * view * projection); @@ -145,8 +135,6 @@ namespace Kav farPlaneParam = Parameters["FarPlane"]; - worldParam = Parameters["World"]; - worldInverseParam = Parameters["WorldInverse"]; worldViewProjectionParam = Parameters["WorldViewProjection"]; } } diff --git a/Effects/FXB/DeferredPBR_PointLightEffect.fxb b/Effects/FXB/DeferredPBR_PointLightEffect.fxb index f7cfc76..010f5d6 100644 --- a/Effects/FXB/DeferredPBR_PointLightEffect.fxb +++ b/Effects/FXB/DeferredPBR_PointLightEffect.fxb @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:19dd54568190283d2d0137af4489bc1478fc4901f62dd64421bceb4f2980645c -size 5192 +oid sha256:b98c96aaebb45d196293e1be3716517eaca204dd83eb17768653bd060954ec45 +size 4144 diff --git a/Effects/HLSL/DeferredPBR_PointLightEffect.fx b/Effects/HLSL/DeferredPBR_PointLightEffect.fx index c9d5643..5e7eed1 100644 --- a/Effects/HLSL/DeferredPBR_PointLightEffect.fx +++ b/Effects/HLSL/DeferredPBR_PointLightEffect.fx @@ -21,8 +21,6 @@ BEGIN_CONSTANTS MATRIX_CONSTANTS - float4x4 WorldInverse _ps(c4) _cb(c8); - float4x4 World _ps(c8) _cb(c12); float4x4 WorldViewProjection _vs(c0) _cb(c4); END_CONSTANTS @@ -88,17 +86,26 @@ float4 main_ps(PixelInput input) : SV_TARGET0 float3 albedo = SAMPLE_TEXTURE(gAlbedo, texCoord).rgb; float2 metallicRoughness = SAMPLE_TEXTURE(gMetallicRoughness, texCoord).rg; - float3 objectZNormal = mul(normal, WorldInverse); - objectZNormal.xz *= -1; - float3 invertedZNormalWorld = mul(objectZNormal, World); + if (isSprite == 1.0) + { + float3 lightDir = PointLightPosition - worldPosition; + float3 L = normalize(lightDir); + float distance = length(lightDir); + float attenuation = 1.0 / (distance * distance); + float3 radiance = PointLightColor * attenuation; - return ComputeColor( - worldPosition, - normal, - albedo, - metallicRoughness.r, - metallicRoughness.g - ) + (isSprite * ComputeColor(worldPosition, invertedZNormalWorld, albedo, metallicRoughness.r, metallicRoughness.g)); + return float4(albedo * radiance * 0.1, 1.0); + } + else + { + return ComputeColor( + worldPosition, + normal, + albedo, + metallicRoughness.r, + metallicRoughness.g + ); + } } Technique DeferredPBR_Point