Quantcast
Channel: Question and Answer » hlsl
Viewing all articles
Browse latest Browse all 69

How can I add sphere falloff to my specular lighting implementation?

$
0
0

I’m using point light in my game and tried to add specular lighting. It looks good but when standing close to wall player can clearly see where lights end.

Image from front:
Image from front

Image when near wall:
Image when near wall

I have simillar effect in Blender after putting a point light near a wall. In the light options, there is toggle for “sphere falloff.” Maybe I need something similar? How could I add such a falloff to my implementation?

Here is my shader code where I compute the lighting:

struct MESH_OUTPUT
{
    float4 pos : POSITION;
    float2 tex : TEXCOORD0;
    float3 normal : TEXCOORD1;
    float3 posWorld : TEXCOORD3;
    float3 viewDir : TEXCOORD6;
};
//******************************************************************************
float4 ps_mesh(in MESH_OUTPUT In) : COLOR0
{
    // some removed code here
float4 tex;
float3 normal;

    float specularIntensity = tex2D(samplerSpecular, In.tex);
    float specular = 0;
    float lightIntensity = 0;

        float3 diffuse = float3(0,0,0);
        for(int i=0; i<1; ++i) // only 1 light for testing
        {
            float3 lightVec = normalize(lights[i].pos - In.posWorld);
            float dist = distance(lights[i].pos, In.posWorld);
            float light = clamp(dot(lightVec, normal),0,1) * clamp((1-(dist/lights[i].pos.w)),0,1);
                if(light > 0)
                {
                    float3 reflection = normalize(light*2*normal - lightVec);
                    specular += pow(saturate(dot(reflection, normalize(In.viewDir))), 10) * specularIntensity;
                }           
            diffuse += light * lights[i].color;
            lightIntensity += light;
        }
        lightIntensity = saturate(lightIntensity);
        specular = saturate(specular);

    tex = float4(saturate((tex.xyz * (ambientColor + diffuse) * 0.6) + float3(1,1,1) * specular * 0.5), tex.w);

    return tex;
}

Viewing all articles
Browse latest Browse all 69

Latest Images

Trending Articles



Latest Images