SFML community forums

Help => Graphics => Topic started by: bradgate on July 16, 2018, 11:43:02 am

Title: [SOLVED] GLSL Interpolate between 2 colours
Post by: bradgate on July 16, 2018, 11:43:02 am
Hello,

This is somewhat related to my recent question regarding lightmap shaders.

I have a value of ambient lighting to represent a night-time colour.

I would like to fade this over time following to white representing a peak daytime colour.

I want to use the value of 0.5 * sin(theta) + 0.5 to fade between the two colours inside my shader; in order to implement a simple day/night cycle, my intention:

void main()
{
    vec3 white = vec3(1, 1, 1);
    vec3 ambientLight = vec3(0.2, 0.2, 0.2);
   
    //interpolate based on current value of 0.5 * sin(theta) + 0.5
    vec3 timeOfDayColor = InterpolateColour(ambientLight, WHITE);

    //rest of shader...
}

I have been trying to find a straightforward tutorial on how to interpolate between white and my ambient light colour to implement the
InterpolateColour
method above, however i have had no success, could anyone help me or point me in the right direction?

Thanks
Title: Re: GLSL Interpolate between 2 colours
Post by: Laurent on July 16, 2018, 01:15:13 pm
Seems like the mix function might help you.
Title: Re: GLSL Interpolate between 2 colours
Post by: bradgate on July 16, 2018, 04:20:02 pm
Hi Laurent,

Thanks for pointing me towards the mix function, it was exactly what i was looking for to implement this functionality. I feel like i'm finally starting to pick up on some shader techniques :D Thanks a lot for your help.

Just in case it might help anyone, here's the shader i'm using for my day/night cycle.

uniform sampler2D texture;
uniform sampler2D lightmap;

uniform vec4 nighttime;
uniform vec2 resolution;
uniform float theta;
uniform float ambientIntensity;

uniform vec3 daytime;

void main()
{
    float timeOfDay = (0.5 * sin(theta) + 0.5);
    vec4 diffuse = texture2D(texture, gl_TexCoord[0].xy);

    vec2 lightcoord = (gl_FragCoord.xy / resolution.xy);
    vec4 light = texture2D(lightmap, lightcoord);
       
    //offset by 0.2 so ambientColour is never 0
    vec3 ambientColour = mix(nighttime.rgb, daytime, timeOfDay + 0.2);
    vec3 intensity = (ambientColour + (light.rgb * mix(ambientIntensity, 0, timeOfDay)));

    vec3 result = diffuse.rgb * intensity;

    gl_FragColor = vec4(result, diffuse.a);
}