Jump to content
  • Advertisement
Sign in to follow this  

Shader issue with distance()

This topic is 5215 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I am trying to calculate my own lighting by taking the distance from a light source to the current pixel and then modulating it with shades of gray. Here is my vertex shader:
    float3 InPos  : POSITION, 
    float3 InNormal : NORMAL,
    float2 InTexCoord : TEXCOORD0

    // Transform the position and normal
    float3 Pos = mul(float4(InPos, 1), (float4x3)World);         
    float3 Normal = normalize(mul(InNormal, (float3x3)World));   

    Out.Pos  = mul(float4(Pos, 1), ViewProjection);
    Out.TextureUV = InTexCoord;

    //calculate lighting term (clamped to 0,1)
    Out.LightTerm = distance (DirFromLight, Pos);

    Out.LightTerm = lerp(0.0, 1.0, (Out.LightTerm - NearLightRange)/ (FarLightRange - NearLightRange));
    return Out;

Near and far Lightrange simply specify a distance to calculate the color. DirFromLight is defined as:
float3 DirFromLight < string UIDirectional = "Light Direction"; > = {0.577, -0.577, 0.577};
The pixel shader is simple:
    PS_OUTPUT Output;

    // Lookup mesh texel
    Output.RGBColor = tex2D(MeshTextureSampler, In.TextureUV);
    //shade lighting falloff based on lighting term    
    Output.RGBColor.r *= (In.LightTerm);
    Output.RGBColor.g *= (In.LightTerm);
    Output.RGBColor.b *= (In.LightTerm);
    return Output;

I declare LightTerm to a TEXCOORD1 semantic (clamped to 0,1) My problem right now is this: it works, I can see the lighting falloff to black, however if I move the camera closer, the lighting changes (sortof follows the camera around). I have not defined any Camera Position semantic, neither do I pass the shader my camera location. More of what's happening is that everything to the left of my viewspace is bright and everything to the right is dark. No matter where I move this happens. I would like it to be fixed to the light position DirFromLight (and the ranges specified). Any ideas? EDIT: omg I figured it out a second after hitting post, I change the distance call to InPos rather than the transformed Pos. wee! hope this helps some poor sod (like I was a second ago) =)

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!