1
\$\begingroup\$
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:

https://ibb.co/zh3zqPk

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.

\$\endgroup\$
3
  • 3
    \$\begingroup\$ What you've shown looks like an oval to me (technically an ellipse, the intersection of a plane cutting across a cone), it just has diminishing brightness on the side farther from the light due to distance attenuation. What would you like to be different about this image? \$\endgroup\$
    – DMGregory
    Commented Jun 11 at 10:31
  • 2
    \$\begingroup\$ There is a non-oval spot of light on my screenshot ... what leads you to believe this? \$\endgroup\$
    – jsotola
    Commented Jun 11 at 15:41
  • 1
    \$\begingroup\$ What documentation? \$\endgroup\$
    – agone
    Commented Jun 15 at 22:50

0

You must log in to answer this question.

Browse other questions tagged .