" " " "

directx – Spot Light Directx12

Share post:

cbuffer cbPerObject : register(b0)
{
float4x4 gWorldView;
};

cbuffer cbPass : register(b1)
{
float4x4 gWorldViewProj;
};

struct VertexIn
{
float3 PosL : POSITION;
float3 Normal : NORMAL;
};

struct VertexOut
{
float4 PosH : SV_POSITION;
float3 tNormal : NORMAL;
float3 PosW : POSITION;
float3 LightPos: POSITION1;
};

static float3 LightPos = { 0.0f, 10.0f, 0.0f };
static float3 DiffuseLightColor = { 1.0f, 1.0f, 0.5f };
static float FallOfStart = 15;
static float FallOfEnd = 15;
static float SpotPower = 0.00001;
static float3 LightDirection = { 5.0f, -5.0f, 0.0f };

VertexOut VS(VertexIn vin)
{
VertexOut vout;

vout.PosH = mul(float4(vin.PosL, 1.0f), gWorldViewProj);

float4 PosW = mul(float4(vin.PosL, 1.0f), gWorldView);
vout.PosW = PosW.xyz;

vout.tNormal = mul(vin.Normal, (float3x3)gWorldView);

vout.LightPos = mul(float4(LightPos, 1.0f), gWorldView);

return vout;
}

float CalcAttenuation(float d, float falloffStart, float falloffEnd)
{
return saturate((falloffEnd-d) / (falloffEnd – falloffStart));
}

float3 Get_Spot_Light(float3 tNormal, float3 PosW, float3 LightPosW)
{

float3 ToLightPos = LightPosW – PosW;

float Dist = length(ToLightPos);

if(Dist > FallOfEnd)
return 0;

ToLightPos = normalize(ToLightPos);

float3 Normal = normalize(tNormal);
float Dot = max(dot(ToLightPos, Normal), 0.0f);

float AttVal = CalcAttenuation(Dist, FallOfStart, FallOfEnd);

LightDirection = normalize(LightDirection);

float SpotFactor = pow(max(dot(-ToLightPos, LightDirection), 0.0f), SpotPower);

return DiffuseLightColor * AttVal * SpotFactor * Dot;

}

float4 PS(VertexOut pin) : SV_Target
{

float3 ResColor = Get_Spot_Light(pin.tNormal, pin.PosW, pin.LightPos);

return float4(ResColor, 1.0f);

}

I’m making Spot light DirectX12, here’s a screenshot:
2024-06-11-111944
I don’t understand – the documentation says that a spot light is the light from a flashlight, that is, if the flashlight is located at an angle to the surface, then there should be an (approximately) oval spot of light. There is a non-oval spot of light on my screenshot. Here is my shader (works for other light sources, tested) explain where I made a mistake so that the light spot would be oval.

Related articles

Real Madrid’s Mbappe struggling with thigh injury

Real Madrid's French forward Kylian Mbappe has been diagnosed with an injury to...

A Night With the Best Baseball Team in New York

The Brooklyn Cyclones are leading their minor league division this summer, while New York’s major league teams...