// Information from Oolite.
uniform sampler2D      uColorMap; // Difuse map
uniform sampler2D      uEffectsMap; // Illum Map
uniform float          uTime;

// Information from vertex shader.
varying vec2         TexScrollcoords;
varying vec2         vTexCoord;

// Irregular flickering function
   #ifdef OO_REDUCED_COMPLEXITY 
   #define Pulse(v, ts) ((v) * 0.95) 
   #else

     float Pulse(float value, float timeScale)
     {
        float t = uTime * timeScale;   

        float s0 = t;
        s0 -= floor(s0);
        float sum = abs( s0 - 0.5);
   
        float s1 = t * 0.7 - 0.05;
        s1 -= floor(s1);
        sum += abs(s1 - 0.5) - 0.25;
   
        float s2 = t * 1.3 - 0.3;
        s2 -= floor(s2);
        sum += abs(s2 - 0.5) - 0.25;
   
        float s3 = t * 5.09 - 0.6;
        s3 -= floor(s3);
        sum += abs(s3 - 0.5) - 0.25;

        return (sum * 0.1 + 0.9) * value;

      }
      #endif

void main(void)
{

   // Load texture data
   vec4 diffuseMap = texture2D(uColorMap, vTexCoord);
   vec4 IllumMap = texture2D(uEffectsMap, TexScrollcoords.st);
   vec4 IllumMap2 = texture2D(uEffectsMap, vTexCoord);
   vec4 color = diffuseMap * 0.3;
        color += diffuseMap * IllumMap.r * IllumMap2.b * vec4(1.0, 0.9219, 0.6436, 1.0) * 2.0;
        color += IllumMap2.b * IllumMap.g * vec4(0.8696, 1.0, 1.0, 1.0);
   color += diffuseMap.a * diffuseMap * 4.0 * vec4(0.7, 0.1, 0.0, 1.0) * Pulse(min(1.0, 1.0), 1.0);
  
   gl_FragColor = vec4(color.rgb, 0.0);
}
