From b03105016776ac4d91a92891be19570d0232aa0d Mon Sep 17 00:00:00 2001 From: Ziad Zananiri Date: Wed, 21 Jan 2026 19:02:41 -0500 Subject: [PATCH 1/6] Implemented standard functionality for spot light shader --- shaders/shader_lights.slang | 48 ++++++++++++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/shaders/shader_lights.slang b/shaders/shader_lights.slang index 6b91f97f..9a10ee38 100644 --- a/shaders/shader_lights.slang +++ b/shaders/shader_lights.slang @@ -33,7 +33,14 @@ struct DirLight { }; struct SpotLight { - // TODO: add required data + float3 position; + float intensity; + float3 direction; + float innerConeAngle; + float3 color; + float outerConeAngle; + float range; + float padding; }; ConstantBuffer pointLights; @@ -48,8 +55,43 @@ float3 computeDir(DirLight light) { // TODO: return light contribution from a given directional light } -float3 computeSpot(SpotLight light) { - // TODO: return light contribution from a given spot light +float3 computeSpot(SpotLight light, float3 fragPos, float3 normal, float3 viewPos) { + // not sure what "current fragment" is in the issue description + // assuming that the params will be determined at function call and that it isnt something defined globally + + // Direction from fragment to light + float3 lightDir = light.position - fragPos; + float distance = length(lightDir); + lightDir = normalize(lightDir); + + // Distance attenuation + float attenuation = 1.0 / max(distance * distance, 0.0001); // this is incase distance is 0 + attenuation *= smoothstep(light.range, 0.0, distance); + + // Spotlight cone attenuation + float theta = dot(lightDir, -normalize(light.direction)); + float cosInner = cos(light.innerConeAngle); + float cosOuter = cos(light.outerConeAngle); + float spotIntensity = smoothstep(cosOuter, cosInner, theta); + + // Blinn-Phong lighting components + float3 norm = normalize(normal); + float3 viewDir = normalize(viewPos - fragPos); + + // Diffuse + float diff = max(dot(norm, lightDir), 0.0); + float3 diffuse = diff * light.color; + + // Specular + float3 halfDir = normalize(lightDir + viewDir); + float spec = pow(max(dot(norm, halfDir), 0.0), 32.0); + float3 specular = spec * light.color; + + // Ambient + float3 ambient = 0.1 * light.color; + + float3 result = (ambient + diffuse + specular) * light.intensity * spotIntensity * attenuation; + return result; } [shader("fragment")] From 181c58d75e38ee7d16cba5458c5535120fcdd302 Mon Sep 17 00:00:00 2001 From: Ziad Zananiri Date: Sat, 24 Jan 2026 21:43:51 -0500 Subject: [PATCH 2/6] Small comment for question --- shaders/shader_lights.slang | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shaders/shader_lights.slang b/shaders/shader_lights.slang index 9a10ee38..1ec49966 100644 --- a/shaders/shader_lights.slang +++ b/shaders/shader_lights.slang @@ -84,7 +84,7 @@ float3 computeSpot(SpotLight light, float3 fragPos, float3 normal, float3 viewPo // Specular float3 halfDir = normalize(lightDir + viewDir); - float spec = pow(max(dot(norm, halfDir), 0.0), 32.0); + float spec = pow(max(dot(norm, halfDir), 0.0), 32.0); // not sure if this should just be constant 32 or a param float3 specular = spec * light.color; // Ambient From b3a0746b1d0ff161117cf7bd28302eda2cc54975 Mon Sep 17 00:00:00 2001 From: Ziad Zananiri Date: Mon, 26 Jan 2026 13:25:36 -0500 Subject: [PATCH 3/6] Changing light choking logic for spotlight --- shaders/shader_lights.slang | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/shaders/shader_lights.slang b/shaders/shader_lights.slang index 1ec49966..abf754ea 100644 --- a/shaders/shader_lights.slang +++ b/shaders/shader_lights.slang @@ -66,7 +66,9 @@ float3 computeSpot(SpotLight light, float3 fragPos, float3 normal, float3 viewPo // Distance attenuation float attenuation = 1.0 / max(distance * distance, 0.0001); // this is incase distance is 0 - attenuation *= smoothstep(light.range, 0.0, distance); + + // in this step, we start choking the light after it has travelled at least 80% of the distance + attenuation *= smoothstep(light.range, light.range * 0.8, distance); // Spotlight cone attenuation float theta = dot(lightDir, -normalize(light.direction)); From 67bec2ff7d2921a6f094fa935125423a92d565b9 Mon Sep 17 00:00:00 2001 From: su-ah Date: Mon, 26 Jan 2026 19:14:48 +0000 Subject: [PATCH 4/6] add note for future change for a param --- shaders/shader_lights.slang | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/shaders/shader_lights.slang b/shaders/shader_lights.slang index abf754ea..908747d3 100644 --- a/shaders/shader_lights.slang +++ b/shaders/shader_lights.slang @@ -86,7 +86,8 @@ float3 computeSpot(SpotLight light, float3 fragPos, float3 normal, float3 viewPo // Specular float3 halfDir = normalize(lightDir + viewDir); - float spec = pow(max(dot(norm, halfDir), 0.0), 32.0); // not sure if this should just be constant 32 or a param + // NOTE: change 32.0 to a 'shininess' param when switching to textures (blinn-phong only) + float spec = pow(max(dot(norm, halfDir), 0.0), 32.0); float3 specular = spec * light.color; // Ambient From 0c06c71a5c37bd03c7edafe1e4e7d035b2b2952b Mon Sep 17 00:00:00 2001 From: Ziad Zananiri Date: Fri, 30 Jan 2026 18:20:31 -0500 Subject: [PATCH 5/6] Slight modifications based on feedback --- shaders/shader_lights.slang | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/shaders/shader_lights.slang b/shaders/shader_lights.slang index abf754ea..87c1a1c8 100644 --- a/shaders/shader_lights.slang +++ b/shaders/shader_lights.slang @@ -40,7 +40,6 @@ struct SpotLight { float3 color; float outerConeAngle; float range; - float padding; }; ConstantBuffer pointLights; @@ -55,20 +54,17 @@ float3 computeDir(DirLight light) { // TODO: return light contribution from a given directional light } -float3 computeSpot(SpotLight light, float3 fragPos, float3 normal, float3 viewPos) { - // not sure what "current fragment" is in the issue description - // assuming that the params will be determined at function call and that it isnt something defined globally - +float3 computeSpot(SpotLight light, float3 fragPos, float3 normal, float3 viewDir) { // Direction from fragment to light float3 lightDir = light.position - fragPos; float distance = length(lightDir); lightDir = normalize(lightDir); // Distance attenuation - float attenuation = 1.0 / max(distance * distance, 0.0001); // this is incase distance is 0 + float attenuation = 1.0 / max(200 * 200 * distance * distance, 0.0001); // Incase distance is 0 - // in this step, we start choking the light after it has travelled at least 80% of the distance - attenuation *= smoothstep(light.range, light.range * 0.8, distance); + // We may start choking the light after it has travelled at least 80% of the distance + // attenuation *= smoothstep(light.range, light.range * 0.8, distance); // Spotlight cone attenuation float theta = dot(lightDir, -normalize(light.direction)); @@ -78,7 +74,6 @@ float3 computeSpot(SpotLight light, float3 fragPos, float3 normal, float3 viewPo // Blinn-Phong lighting components float3 norm = normalize(normal); - float3 viewDir = normalize(viewPos - fragPos); // Diffuse float diff = max(dot(norm, lightDir), 0.0); From d47c1499062f7ce34d9703eb5df996b325f31718 Mon Sep 17 00:00:00 2001 From: su-ah Date: Sun, 1 Feb 2026 17:26:03 +0000 Subject: [PATCH 6/6] re-add range into attenuation --- shaders/shader_lights.slang | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shaders/shader_lights.slang b/shaders/shader_lights.slang index c9fa3591..c55130be 100644 --- a/shaders/shader_lights.slang +++ b/shaders/shader_lights.slang @@ -79,7 +79,7 @@ float3 computeSpot(SpotLight light, float3 fragPos, float3 normal, float3 viewDi lightDir = normalize(lightDir); // Distance attenuation - float attenuation = 1.0 / max(200 * 200 * distance * distance, 0.0001); // Incase distance is 0 + float attenuation = (light.range * light.range) / max(200 * 200 * distance * distance, 0.0001); // Incase distance is 0 // We may start choking the light after it has travelled at least 80% of the distance // attenuation *= smoothstep(light.range, light.range * 0.8, distance);