# Color attenuation problem on terrain

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

## Recommended Posts

My GLSL shader is not outputting the correct attenuation onto my terrain. My terrain is green(0.1,0.6,0.0), and I want the color of the terrain to go from dark green to bright green as the light gets closer to each on the terrain, as it should in attenuation.

When my terrain's color = {0.1, 0.6, 0.0}, I see a very rapid color change from green->yellow as pixels on the terrain get closer to the light, when it should be going green->bright green instead of yellow.

*light is located above the player's head(camera).

When my terrain's color = {0.0, 0.0, 0.0}, the terrain color is perfectly calculated with attenuation, no problems.

*light is located above the player's head(camera).

Question: I can't understand why the 0.1 in the RED color spot has so much impact when terrain pixels get closer to the light(ie: attenuation value gets closer to 1.0)?

Here's the basis of the final generation of color in my pixel shader:

varying vec4 color;

float constantAttenuationPlayer=0.0;
float linearAttenuationPlayer=0.0;

float dist = length(gl_LightSource[2].position - v);
float att = 1.0/(constantAttenuationPlayer + (linearAttenuationPlayer * dist) + (quadraticAttenuationPlayer * dist * dist));

gl_FragColor = color * att;


##### Share on other sites

Assume the distance is 1. That means that the attenuation factor would be 1.0 / (0 + (0*1) + (0.009*1*1)) as per your shader snippet. This equals (1.0 / 0.009), or 111.111. This is then used to scale the light color. When the light color is scaled, components that go over 1 are clamped to 1. This means that, with such a drastic attenuation factor (which, in this case, is actually more like an amplification factor) the green component very quickly goes over 1 in value, and the red value approaches 1 somewhat less quickly, but still quite rapidly, eventually topping 1 as well. When both red and green are clamped at 1, you get yellow.

Now, look at how the attenuation factor changes when distance is 0.5:

1.0 / (0.0 + (0.0*0.5) + (0.009*0.5*0.5) = 444.44

As calculated, your "attenuation" factor is actually getting larger as the distance decreases.

##### Share on other sites

Thank you for taking the time to respond so quickly.

I would like to propose a solution and have your feedback, or your solution if you have a better one.

I think I should prevent attenuation factor from being added if any value of 'color' is already at it's maximum(1):

final_color=color*att; // no components have reached maximum

if (att factor>1 AND any component of 'color' is already at maximum){ then

final_color=color; // do nothing(instead of turning yellow)

}

EDIT*

My solution failed and creates a blob where yellow starts to get added. I definitely didn't think my solution through.

Edited by Shawn619

##### Share on other sites

Solved.

My final solution involved taking the highest attenuation value before any color component reaches 1.0(the point at which other RGB values may also reach, thus losing the 'green' ratio and turning into the yellow ratio(ie:1.0,1.0,z)), then taking that highest attenuation value and distributing across the entire zone at which I would have lost my green-ratio and turned yellow, if that makes sense.

Thanks!

• 9
• 17
• 10
• 11
• 18