# Day/Night cycle with GLSL - blend from one colour to another along Sin curve

I am attempting to implement a day/night cycle using a GLSL fragment shader in a 2D game i'm developing in SFML. I am a novice with shaders with this one being my first.

Currently I am using a lightmap to achieve ambient lighting and spotlights from lightsources (e.g. campfire) which is working nicely. The lightmap shader I am using I got from following an old Alcovegames tutorial, see below:

uniform sampler2D texture;
uniform sampler2D lightmap;

uniform vec4 ambient;
uniform vec2 resolution;
uniform int theta;

float PI_180 = 3.141592653589793 / 180;

vec3 ToGlColour3(in vec3 colour)
{
float dv = (1.0/255);
return vec3
(
dv * colour.r,
dv * colour.g,
dv * colour.b
);
}

vec4 ToGlColour4(in vec4 colour)
{
return vec4
(
ToGlColour3(colour.rgb),
(1.0/255) * colour.a
);
}

void main()
{
vec4 diffuse = texture2D(texture, gl_TexCoord[0].xy);

vec2 lightcoord = (gl_FragCoord.xy / resolution.xy);
vec4 light = texture2D(lightmap, lightcoord);

vec4 ambientLight = ToGlColour4(ambient);

float ambientIntensity = ambientLight.a * (sin(theta * PI_180) + 0.5);
vec3 ambientColour = ambientLight.rgb * ambientIntensity;
vec3 intensity = (ambientColour + light.rgb);

vec3 result = diffuse.rgb * intensity;

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


The ambientIntesity calculation is my attempt at cycling from night to day. My issue is that it doesn't cycle bright enough to give an adequate daytime effect.

I was thinking I could use the current value of sine(theta) and interpolate from my ambient colour to white (representing fully lit daytime).

I'm just not sure how to achieve this in GLSL. Ideally a solution within GLSL would be possible to help develop my knowledge of shaders.

I appreciate any help, Thanks.

• Why are you multiplying your ambient color's alpha and color channels by 1/255.0? That's going to make them very small. Also, it's wasteful as you're doing that calculation on every fragment. You should just scale it before passing setting the uniform. – user1118321 Jul 14 '18 at 3:17
• @user1118321 I use those methods for normalising the colours that i pass into the shader as they are between 0-255. I understand that it is wasteful, but for now while i'm learning, i'm not too interested in optimisations until i have the shader working as intended, but thanks for pointing it out, i'll make the change now – cmpbedes Jul 16 '18 at 7:52

vec3 result = (ambientLight.rgb + n*(sin(...)*0.5+0.5)*light.rgb;