Support lighting using a graphics buffer that is kep open!
This commit is contained in:
@@ -1,10 +1,46 @@
|
||||
#include "RootConstants.hlsl"
|
||||
|
||||
float4 main(float4 Color : TEXCOORD0, float3 WorldNormal : TEXCOORD1) : SV_Target0
|
||||
struct Input
|
||||
{
|
||||
float3 N = normalize(WorldNormal);
|
||||
float NdotL = saturate(dot(N, -LightDirection));
|
||||
float3 diffuse = Color.rgb * LightColor * NdotL;
|
||||
float3 ambient = Color.rgb * AmbientIntensity;
|
||||
return float4(diffuse + ambient, Color.a);
|
||||
float4 Color : TEXCOORD0;
|
||||
float3 WorldNormal : TEXCOORD1;
|
||||
float3 WorldPosition : TEXCOORD2;
|
||||
};
|
||||
|
||||
float4 main(Input input) : SV_Target0
|
||||
{
|
||||
float3 normal = normalize(input.WorldNormal);
|
||||
|
||||
// Initial ambient component
|
||||
float3 result = input.Color.rgb * LightColor * AmbientIntensity;
|
||||
|
||||
// Directional light contribution
|
||||
float ndotl = max(dot(normal, -LightDirection), 0.0);
|
||||
result += input.Color.rgb * LightColor * ndotl;
|
||||
|
||||
// Point lights
|
||||
if (ActiveLightCount > 0)
|
||||
{
|
||||
StructuredBuffer<PointLight> pointLights = ResourceDescriptorHeap[LightBufferIndex];
|
||||
|
||||
for (uint i = 0; i < ActiveLightCount; ++i)
|
||||
{
|
||||
PointLight light = pointLights[i];
|
||||
|
||||
float3 lightDir = light.Position - input.WorldPosition;
|
||||
float dist = length(lightDir);
|
||||
|
||||
if (dist < light.Radius)
|
||||
{
|
||||
lightDir = normalize(lightDir);
|
||||
float attenuation = 1.0 - (dist / light.Radius);
|
||||
attenuation = max(attenuation, 0.0);
|
||||
|
||||
float pndotl = max(dot(normal, lightDir), 0.0);
|
||||
result += light.Color * input.Color.rgb * pndotl * attenuation * light.Intensity;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return float4(result, input.Color.a);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user