4
\$\begingroup\$

I'm currently trying to implement the lighting technique described in this article.

I've done pretty much exactly what is done in the article. My technique is the same, and my shader is the same. Here's my problem:

The lighting looks great when everything is kind of dark, but when it's not dark, the lights are far too bright.

This what it looks like when it's dark:

dark

And here's what it looks like when it's light:

light

As you can probably tell, the light in the second image is far too bright. I'm looking for something about as intense as the first picture, but with the overall lighting like the second picture.

My fragment shader code is as follows: (Vertex shader is not relevant)

#define LOWP lowp
precision mediump float;
#else
#define LOWP  
#endif 
varying LOWP vec4 v_color; 
varying vec2 v_texCoords;
uniform sampler2D u_texture;
uniform sampler2D u_lightmap;
uniform LOWP vec4 ambientColor;
uniform vec2 u_resolution;
void main()
{
    vec4 diffuseColor = texture2D(u_texture, v_texCoords);
    vec2 lightCoord = gl_FragCoord.xy / u_resolution.xy;
    vec4 lightColor = texture2D(u_lightmap, lightCoord);

    vec3 ambient = ambientColor.rgb * ambientColor.a;
    vec3 intensity = ambient + lightColor.rgb;
    vec3 finalColor = diffuseColor.rgb * intensity;

    gl_FragColor = v_color * vec4(finalColor, diffuseColor.a);
}

ambientColor is set to (1, 1, 1, alpha) or (white, alpha), where the higher the alpha, the lighter it is. The dark image is 0.1f, and light is 0.9f.

I think the problem is with ambient + lightColor.rgb, but I am not sure how else I could write it and maintain the same effect for when it is dark.

\$\endgroup\$
2
  • \$\begingroup\$ @Bálint you mean ambient * lightColor.rgb instead of +? \$\endgroup\$ Oct 17, 2016 at 5:50
  • \$\begingroup\$ @Bálint I changed ambient + lightColor.rgb to ambient * lightColor.rgb and I get something like this: i.stack.imgur.com/reG5c.png. Now the alpha value of ambient.a is completely ignored, so it is always totally dark everywhere. I have no way of controlling the global light now. \$\endgroup\$ Oct 17, 2016 at 5:54

2 Answers 2

0
\$\begingroup\$

ambientColor.a could be the intensity? did u try turning that down?

I'm work with HLSL but could this work for you? play with intensity and ambientColor.a to change how intense each component is

varying  float intensity = 0.5;
vec3 ambient = ambientColor.rgb * ambientColor.a;    
vec3 finalColor =ambient + (diffuseColor.rgb *(lightColor.rgb * intensity));

For my lighting I do this, its in 3d and I have a bit more to it but it may help.

 DiffuseAndAmbient = (Ambientcolor * CurrentMaterial.ambientpower) + (cubecolor.rgb * CurrentMaterial.cubepower) + AccumDiffuse;

 float3 OutputColor = 0;       
 OutputColor += BGColor.rgb;
 OutputColor += Color.rgb * (DiffuseAndAmbient + AccumSpecular);
\$\endgroup\$
4
  • \$\begingroup\$ ambientColor.a is the general lightness of the scene. I set it to 0.9 to make it light, and 0.1 to make it dark. I will try the above change and see what happens. \$\endgroup\$ Oct 17, 2016 at 5:10
  • \$\begingroup\$ changed my answer to include my own code for lighting \$\endgroup\$ Oct 17, 2016 at 5:14
  • \$\begingroup\$ This is what I get: i.stack.imgur.com/eafLg.png. I will try something like you have done. In your code, what is AmbientColor, AmbientPower, CubeColor, CubePower and AccumDiffuse and AccumSpecular? \$\endgroup\$ Oct 17, 2016 at 5:22
  • \$\begingroup\$ Like I said I use a bit more in my lighting, did u play with the intensity value and the ambientColor.a value because the should make it darker or lighter? In my code, AmbientColor comes from Spherical harmonics, AmbientPower controls how strong it is, CubeColor comes from a dynamic cube map, CubePower controls how strong it is, AccumDiffuse is the NdotL stuff and AccumSpecular is the Specular light term. In my editor I play with AmbientPower and CubePower to control the ambient strength \$\endgroup\$ Oct 17, 2016 at 5:28
0
\$\begingroup\$

I had the exact same problem. I solved it by multiplying the alpha value of the ambient color to the lights color. Your shader should look like this:

#define LOWP lowp
precision mediump float;
#else
#define LOWP  
#endif 
varying LOWP vec4 v_color; 
varying vec2 v_texCoords;
uniform sampler2D u_texture;
uniform sampler2D u_lightmap;
uniform LOWP vec4 ambientColor;
uniform vec2 u_resolution;
void main()
{
    vec4 diffuseColor = texture2D(u_texture, v_texCoords);
    vec2 lightCoord = gl_FragCoord.xy / u_resolution.xy;
    vec4 lightColor = texture2D(u_lightmap, lightCoord);

    vec3 ambient = ambientColor.rgb * ambientColor.a;
    vec3 intensity = ambient + lightColor.rgb * (1.0f - ambientColor.a);
    vec3 finalColor = diffuseColor.rgb * intensity;

    gl_FragColor = v_color * vec4(finalColor, diffuseColor.a);
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .