XNA Creators Club Online
Page 1 of 1 (2 items)
Sort Posts: Previous Next

HLSL Spotlight problem

Last post 5/21/2009 6:26 AM by nearo. 1 replies.
  • 7/11/2007 7:54 AM

    HLSL Spotlight problem

    Hi.

    Now it's about spotlight I can't understand why it dosn't work. I did it based on the information here: http://msdn2.microsoft.com/en-us/library/bb172279.aspx, but it shows only dark cube. It seems I coded something wrong. I've made my shader based on this :

     

     

    Where rho = norm(Ldcs).norm(Ldir) , and

    Ldcs - The negative of the light direction in camera space;

    Ldir - Direction vector from vertex position to the light position;


    The code of the shader is as follows (the GetAngleIntensity always returns 0???):

    struct VS_INPUT
    {
    float4 Pos : POSITION;
    float3 Normal : NORMAL;
    };

    struct PS_INPUT
    {
    float4 Pos : SV_POSITION;
    float3 InterPos : TEXTCOORD0;
    float3 Normal : TEXTCOORD1;
    };
    //-----------------------------------------------------------------------
    // Globals
    //-----------------------------------------------------------------------
    matrix World;
    matrix View;
    matrix Projection;

    float4 LightColor;
    float4 LightPosition;
    float4 LightDirection;
    float Phi;
    float Theta;

    //-----------------------------------------------------------------------
    // Vertex shader function
    //-----------------------------------------------------------------------
    PS_INPUT VS( VS_INPUT Data )
    {
    PS_INPUT Out;
    Out = (PS_INPUT)0;


    Out.Pos = mul (Data.Pos, World);

    Out.InterPos = Out.Pos.xyz;

    Out.Pos = mul (Out.Pos, View);
    Out.Pos = mul (Out.Pos, Projection);

    Out.Normal = mul (Data.Normal, World);

    return Out;
    }

    // HELP FUNCTION TO GET
    // LIGHT INTENSITY DEPENDING ON
    // ANGLE ALPHA (the problem is here, I think)
    float GetAngleIntensity ( float3 LightDir, float3 PixelToLight )
    {
    float F = 1.0;
    float AngleIntensity = 0.0;
    Phi = radians(Phi);
    Theta = radians (Theta);

    float rho = dot((float3)(LightDir), PixelToLight);

    if ( (rho<cos(Theta/2)) && (rho>cos(Phi/2)) )
    AngleIntensity = pow( (rho-cos(Phi/2))/(cos(Theta/2)-cos(Phi/2)), F );

    if (rho <= cos(Phi/2))
    AngleIntensity = 0.0;

    if (rho > cos(Theta/2))
    AngleIntensity = 1.0;

    return AngleIntensity;
    }

    //-----------------------------------------------------------------------
    // Pixel shader function
    //-----------------------------------------------------------------------
    float4 PS_SpotLight( PS_INPUT Vertex ) : SV_Target
    {
    float A = 1.0;
    float B = 0.0;
    float C = 0.0;

    float3 PixelToLight = (float3)LightPosition - Vertex.InterPos;

    float3 NewNormal = normalize(Vertex.Normal);
    float3 NewDirection = normalize(PixelToLight);

    float LightAtten =1/( A*pow(length(PixelToLight),2.0)+B*length(PixelToLight)+C );

    float LightAngle = GetAngleIntensity (normalize(LightDirection), NewDirection);

    float4 LightIntensity = LightColor*LightAngle*LightAtten;

    float4 FinalColor = saturate(0.3+max(LightIntensity*dot(NewNormal, NewDirection),0) );
    FinalColor.a = 1;

    return FinalColor;
    }
    PLEASE HELP TO FIGURE IT OUT
  • 5/21/2009 6:26 AM In reply to

    Re: HLSL Spotlight problem

    Here is my simple code....
    it haven't completed it, but it has simple spot light effect!
    and calculate in simple way...hope it help for you...

    struct SpotLight
    {
     float3 position;
     float3 direction;
     float angle;
     float strength;
     float decay;
     float3 color;
    };

    float4 CalculateSingleSpotlight(SpotLight light, float3 worldPosition, float3 worldNormal,
                                float4 diffuseColor, float3 specularColor )
    {
     float3 directionToLight = normalize(light.position - worldPosition);
     float coneDot = dot(directionToLight, light.direction);
     
     float shading=0;
     if(coneDot > light.angle)
     {
      float coneAttenuation = pow(coneDot, light.decay);
      shading = dot(worldNormal, -directionToLight) * coneAttenuation * light.strength;
     }
     return shading * float4(light.color, 1) * diffuseColor;
    }
Page 1 of 1 (2 items) Previous Next