Kav/Effects/HLSL/DeferredPBR_PointLightEffec...

119 lines
3.0 KiB
Plaintext
Raw Normal View History

2020-12-10 00:10:53 +00:00
// Assumes you are drawing a sphere!!
#include "Macros.fxh" //from FNA
2020-12-12 09:09:44 +00:00
#include "Lighting.fxh"
2020-10-19 10:01:37 +00:00
#include "Shadow.fxh"
DECLARE_TEXTURE(gPosition, 0);
DECLARE_TEXTURE(gAlbedo, 1);
DECLARE_TEXTURE(gNormal, 2);
DECLARE_TEXTURE(gMetallicRoughness, 3);
2020-10-19 10:01:37 +00:00
DECLARE_CUBEMAP(shadowMap, 4);
BEGIN_CONSTANTS
float3 EyePosition _ps(c0) _cb(c0);
2020-12-12 09:09:44 +00:00
float3 PointLightPosition _ps(c1) _cb(c1);
float3 PointLightColor _ps(c2) _cb(c2);
2020-10-19 10:01:37 +00:00
float FarPlane _ps(c3) _cb(c3);
MATRIX_CONSTANTS
2020-12-10 00:10:53 +00:00
float4x4 WorldViewProjection _vs(c0) _cb(c4);
END_CONSTANTS
struct VertexInput
{
float4 Position : POSITION;
};
struct PixelInput
{
float4 Position : SV_POSITION;
2020-12-10 00:10:53 +00:00
float4 ScreenPosition : TEXCOORD0;
};
PixelInput main_vs(VertexInput input)
{
PixelInput output;
2020-12-10 00:10:53 +00:00
output.Position = mul(input.Position, WorldViewProjection);
output.ScreenPosition = output.Position;
2020-12-12 09:09:44 +00:00
return output;
}
// Pixel Shader
float4 ComputeColor(
float3 worldPosition,
float3 worldNormal,
float3 albedo,
float metallic,
float roughness
) {
float3 V = normalize(EyePosition - worldPosition);
float3 N = normalize(worldNormal);
float3 F0 = float3(0.04, 0.04, 0.04);
F0 = lerp(F0, albedo, metallic);
float3 lightDir = PointLightPosition - worldPosition;
float3 L = normalize(lightDir);
float distance = length(lightDir);
float attenuation = 1.0 / (distance * distance);
float3 radiance = PointLightColor * attenuation;
2020-10-19 10:01:37 +00:00
float shadow = HardPointShadow(worldPosition, N, L, PointLightPosition, SAMPLER(shadowMap), FarPlane);
float3 color = ComputeLight(L, radiance, F0, V, N, albedo, metallic, roughness, shadow);
return float4(color, 1.0);
2020-10-19 10:01:37 +00:00
//return float4(shadow, shadow, shadow, 1.0);
}
float4 main_ps(PixelInput input) : SV_TARGET0
{
2020-12-10 00:10:53 +00:00
input.ScreenPosition.xy /= input.ScreenPosition.w;
float2 texCoord = 0.5f * float2(input.ScreenPosition.x,-input.ScreenPosition.y) + 0.5f;
2020-12-12 09:09:44 +00:00
2020-12-10 00:10:53 +00:00
float3 worldPosition = SAMPLE_TEXTURE(gPosition, texCoord).rgb;
2020-12-12 09:09:44 +00:00
float4 normalSample = SAMPLE_TEXTURE(gNormal, texCoord);
float3 normal = normalSample.xyz;
float isSprite = normalSample.a;
2020-12-10 00:10:53 +00:00
float3 albedo = SAMPLE_TEXTURE(gAlbedo, texCoord).rgb;
float2 metallicRoughness = SAMPLE_TEXTURE(gMetallicRoughness, texCoord).rg;
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 float4(albedo * radiance * 0.1, 1.0);
}
else
{
return ComputeColor(
worldPosition,
normal,
albedo,
metallicRoughness.r,
metallicRoughness.g
);
}
}
Technique DeferredPBR_Point
{
Pass
{
VertexShader = compile vs_3_0 main_vs();
PixelShader = compile ps_3_0 main_ps();
}
}