Multiple point lights

This commit is contained in:
StratiX0
2024-03-28 10:56:57 +01:00
parent 16db21608a
commit e5c88797b0
17 changed files with 4143 additions and 208 deletions

View File

@@ -2,6 +2,10 @@
// Filename: light.ps
////////////////////////////////////////////////////////////////////////////////
/////////////
// DEFINES //
/////////////
#define NUM_LIGHTS 4
/////////////
// GLOBALS //
@@ -11,13 +15,17 @@ SamplerState SampleType : register(s0);
cbuffer LightBuffer
{
float4 ambientColor;
float4 diffuseColor;
float3 lightDirection;
float padding;
float specularPower;
float4 specularColor;
};
cbuffer LightColorBuffer
{
float4 diffuseColor[NUM_LIGHTS];
};
//////////////
// TYPEDEFS //
@@ -27,7 +35,7 @@ struct PixelInputType
float4 position : SV_POSITION;
float2 tex : TEXCOORD0;
float3 normal : NORMAL;
float3 viewDirection : TEXCOORD1;
float3 lightPos[NUM_LIGHTS] : TEXCOORD1;
};
@@ -38,48 +46,39 @@ float4 LightPixelShader(PixelInputType input) : SV_TARGET
{
float4 textureColor;
float3 lightDir;
float lightIntensity;
float4 color;
float3 reflection;
float4 specular;
float lightIntensity[NUM_LIGHTS];
float4 colorArray[NUM_LIGHTS];
float4 colorSum;
int i;
// Sample the pixel color from the texture using the sampler at this texture coordinate location.
textureColor = shaderTexture.Sample(SampleType, input.tex);
// Set the default output color to the ambient light value for all pixels.
color = ambientColor;
// Initialize the specular color.
specular = float4(0.0f, 0.0f, 0.0f, 0.0f);
// Invert the light direction for calculations.
lightDir = -lightDirection;
// Calculate the amount of light on this pixel.
lightIntensity = saturate(dot(input.normal, lightDir));
if(lightIntensity > 0.0f)
for(i=0; i<NUM_LIGHTS; i++)
{
// Determine the final diffuse color based on the diffuse color and the amount of light intensity.
color += (diffuseColor * lightIntensity);
// Calculate the different amounts of light on this pixel based on the positions of the lights.
lightIntensity[i] = saturate(dot(input.normal, input.lightPos[i]));
// Saturate the ambient and diffuse color.
color = saturate(color);
// Determine the diffuse color amount of each of the four lights.
colorArray[i] = diffuseColor[i] * lightIntensity[i];
}
// Calculate the reflection vector based on the light intensity, normal vector, and light direction.
reflection = normalize(2.0f * lightIntensity * input.normal - lightDir);
// Initialize the sum of colors.
colorSum = float4(0.0f, 0.0f, 0.0f, 1.0f);
// Determine the amount of specular light based on the reflection vector, viewing direction, and specular power.
specular = pow(saturate(dot(reflection, input.viewDirection)), specularPower);
// Add all of the light colors up.
for(i=0; i<NUM_LIGHTS; i++)
{
colorSum.r += colorArray[i].r;
colorSum.g += colorArray[i].g;
colorSum.b += colorArray[i].b;
}
// Multiply the texture pixel and the final diffuse color to get the final pixel color result.
color = color * textureColor;
// Add the specular component last to the output color.
color = saturate(color + specular);
// Multiply the texture pixel by the combination of all four light colors to get the final result.
color = saturate(colorSum) * textureColor;
return color;
}