From f0ab528f131ea032a8c623615c75438495d81e90 Mon Sep 17 00:00:00 2001 From: Lightling Date: Sun, 3 Apr 2022 16:08:21 -0400 Subject: [PATCH] move light code from SimplePixelShader to Lights --- Lights.hlsli | 31 +++++++++++++++++++++++++++++++ SimplePixelShader.hlsl | 31 ------------------------------- 2 files changed, 31 insertions(+), 31 deletions(-) diff --git a/Lights.hlsli b/Lights.hlsli index 3fd4973..8aacb19 100644 --- a/Lights.hlsli +++ b/Lights.hlsli @@ -22,4 +22,35 @@ struct Light float3 Padding; }; +// Gets the specular value for any light +float calculateSpecular(float3 normal, float3 direction, float3 view, float roughness) +{ + return getSpecular( + view, + getReflection(direction, normal), + getSpecularExponent(roughness, MAX_SPECULAR_EXPONENT) + ); +} + +// Gets the RGB value of a pixel with a directional light +float3 calculateDirectionalLight(Light light, float3 normal, float3 view, float roughness, float3 surfaceColor, float specularValue) +{ + float3 lightDirection = normalize(light.Direction); + float diffuse = getDiffuse(normal, -lightDirection); + float specular = calculateSpecular(normal, lightDirection, view, roughness) * specularValue; + + return (diffuse * surfaceColor + specular) * light.Intensity * light.Color; +} + +// Gets the RGB value of a pixel with a point light +float3 calculatePointLight(Light light, float3 normal, float3 view, float3 worldPosition, float roughness, float3 surfaceColor, float specularValue) +{ + float3 lightDirection = normalize(light.Position - worldPosition); + float attenuation = getAttenuation(light.Position, worldPosition, light.Range); + float diffuse = getDiffuse(normal, lightDirection); + float specular = calculateSpecular(normal, lightDirection, view, roughness) * specularValue; + + return (diffuse * surfaceColor + specular) * attenuation * light.Intensity * light.Color; +} + #endif diff --git a/SimplePixelShader.hlsl b/SimplePixelShader.hlsl index a7c90d2..5d7e8a2 100644 --- a/SimplePixelShader.hlsl +++ b/SimplePixelShader.hlsl @@ -26,37 +26,6 @@ Texture2D Specular : register(t1); Texture2D Emissive : register(t2); SamplerState BasicSampler : register(s0); -// Gets the specular value for any light -float calculateSpecular(float3 normal, float3 direction, float3 view, float roughness) -{ - return getSpecular( - view, - getReflection(direction, normal), - getSpecularExponent(roughness, MAX_SPECULAR_EXPONENT) - ); -} - -// Gets the RGB value of a pixel with a directional light -float3 calculateDirectionalLight(Light light, float3 normal, float3 view, float roughness, float3 surfaceColor, float specularValue) -{ - float3 lightDirection = normalize(light.Direction); - float diffuse = getDiffuse(normal, -lightDirection); - float specular = calculateSpecular(normal, lightDirection, view, roughness) * specularValue; - - return (diffuse * surfaceColor + specular) * light.Intensity * light.Color; -} - -// Gets the RGB value of a pixel with a point light -float3 calculatePointLight(Light light, float3 normal, float3 view, float3 worldPosition, float roughness, float3 surfaceColor, float specularValue) -{ - float3 lightDirection = normalize(light.Position - worldPosition); - float attenuation = getAttenuation(light.Position, worldPosition, light.Range); - float diffuse = getDiffuse(normal, lightDirection); - float specular = calculateSpecular(normal, lightDirection, view, roughness) * specularValue; - - return (diffuse * surfaceColor + specular) * attenuation * light.Intensity * light.Color; -} - // shader entry point float4 main(VertexToPixel input) : SV_TARGET {