Files
gpd-925-en/GraphicsProgramming/D3D11/LightingPixelShader.hlsl
T
2026-10-20 18:27:15 +02:00

59 lines
1.9 KiB
HLSL

Texture2D MainTexture;
SamplerState MainSampler;
cbuffer LightBuffer
{
float3 LightVector;
float3 LightPosition;
float LightIntensity;
float3 LightAttenuation;
float4 DiffuseColor;
float4 SpecularColor;
float3 CameraPosition;
float Phong;
};
struct PixelInput
{
float4 position : SV_Position;
float3 positionWorld : POSITION1;
float2 uv : TEXCOORD;
float4 color : COLOR;
float3 normal : NORMAL;
};
float4 main(PixelInput INPUT) : SV_TARGET
{
float4 texColor = MainTexture.Sample(MainSampler, INPUT.uv) * INPUT.color;
float4 ambientColor = float4(0.2f, 0.2f, 0.2f, 1.0f);
float4 diffuseColor = 0;
float4 specularColor = 0;
float4 emissionColor = 0;
//float4 emissionColor = float4(0.7f, 0.0f, 0.0f, 1.0f);
// directional
//float lightIntensity = LightIntensity;
// point light
float3 light = LightPosition - INPUT.positionWorld;
float lightLength = length(light);
float attenuation = 1 / (LightAttenuation.x + LightAttenuation.y * lightLength + LightAttenuation.z * lightLength * lightLength); // 1 / (constant + linear + quadratic)
float lightIntensity = LightIntensity * attenuation;
// normalize vectors
float3 normal = normalize(INPUT.normal);
//float3 light = normalize(-LightVector); // directional light
light = light / lightLength; // point or spot light
// diffuse color
float diffuse = max(0, dot(normal, light));
diffuseColor = DiffuseColor * (diffuse * lightIntensity);
// specular color
float3 reflect = 2 * diffuse * normal - light;
float3 view = normalize(CameraPosition - INPUT.positionWorld);
float specular = pow(max(0, dot(reflect, view)), Phong);
specularColor = SpecularColor * (specular * lightIntensity);
return saturate(saturate(texColor * saturate(ambientColor + diffuseColor) + specularColor) + emissionColor);
}