Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [SOLVED] GLSL Interpolate between 2 colours  (Read 1430 times)

0 Members and 1 Guest are viewing this topic.

bradgate

  • Newbie
  • *
  • Posts: 7
    • View Profile
[SOLVED] GLSL Interpolate between 2 colours
« 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
« Last Edit: July 16, 2018, 05:58:09 pm by bradgate »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: GLSL Interpolate between 2 colours
« Reply #1 on: July 16, 2018, 01:15:13 pm »
Seems like the mix function might help you.
Laurent Gomila - SFML developer

bradgate

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: GLSL Interpolate between 2 colours
« Reply #2 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);
}