GLSL片段着色器

GLSL片段着色器,glsl,shader,slowdown,Glsl,Shader,Slowdown,在过去的几天里,我一直在写一个片段着色器,它将处理im制作的2d游戏的背景。但是,今天我注意到,在渲染时,我的云渲染速度非常慢(从3000 fps下降到300 fps) 起初我认为这是因为我在生成云时做了一些愚蠢的事情,但经过一些实验后,我注意到只有当我将云添加到gl_FragColor中时,才会出现减速。计算它们似乎对性能没有任何影响 我像这样生成云: float rand(vec2 co){ return fract(sin(dot(co.xy ,vec2(12.9898,78.233)

在过去的几天里,我一直在写一个片段着色器,它将处理im制作的2d游戏的背景。但是,今天我注意到,在渲染时,我的云渲染速度非常慢(从3000 fps下降到300 fps)

起初我认为这是因为我在生成云时做了一些愚蠢的事情,但经过一些实验后,我注意到只有当我将云添加到gl_FragColor中时,才会出现减速。计算它们似乎对性能没有任何影响

我像这样生成云:

float rand(vec2 co){
  return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
}

float hash( float n ) //Borrowed from voltage
{
    return fract(sin(n)*43758.5453);
}

float fBmWRand( vec2 p )//Borroowed from Mark Sleith
{
    float f = 0.0;
    f += 0.50000*rand( p ); p = p*2.02;
    f += 0.25000*rand( p ); p = p*2.03;
    f += 0.12500*rand( p ); p = p*2.01;
    f += 0.06250*rand( p ); p = p*2.04;
    f += 0.03125*rand( p );
    return f/0.984375;
}

float noise( in vec2 x )//Borroowed from Mark Sleith
{
vec2 p = floor(x);
vec2 f = fract(x);
    f = f*f*(3.0-2.0*f);
    float n = p.x + p.y*57.0;
    float res = mix(mix( hash(n+  0.0), hash(n+  1.0),f.x), mix( hash(n+ 57.0), hash(n+ 58.0),f.x),f.y);
    return res;
}

float fbm( vec2 p ) //Borroowed from Mark Sleith
{
        float f = 0.0;
        f += 0.50000*noise( p ); p = p*2.02;
        f += 0.25000*noise( p ); p = p*2.03;
        f += 0.12500*noise( p ); p = p*2.01;
        f += 0.06250*noise( p ); p = p*2.04;
        f += 0.03125*noise( p );
        return f/0.984375;
}

vec3 bgGradient()
{
    //Getting the height of the current pixel
    float height = gl_FragCoord.y / iResolution.y;

    //Calculating the brightness of the pixel
    float brightness = 1.0 - 0.4 * height;

    //Combining everything into a background
    vec3 grad = vec3(1., 1., 1.);// * brightness;
    return grad;
}

bool star()
{
/*//Getting a position to run random calculations with
float pos = (gl_FragCoord.x / iResolution.x) * (gl_FragCoord.y / iResolution.y) + 0.5;

if(hash(pos) < 0.001)
{
    return true;
}
return false*/;

if(fBmWRand(gl_FragCoord.xy / iResolution.xy) < 0.08)
{
    return true;
}
return false;
}

float cloudFadeDist = 0.1; //The distance at which the clouds will start fading away

vec4 clouds( vec2 point )
{
vec4 result = vec4(0., 0., 0., 0.);

//Checking if the cloud is above
float fbmResult = fbm(point * 5.);
if(fbmResult > overcast)
{
    result = vec4(fbmResult, fbmResult, fbmResult, 1.0);
    //result = vec4(1., 1., 1., 1.);
}
else if(fbmResult > overcast - (cloudFadeDist / resFact)) //Outlining the clouds
{
    float dist = overcast - fbmResult;
    float colorFac = 1.0 - dist / (cloudFadeDist / resFact);

    if(colorFac > 0.0001)
    {
        result = vec4(fbmResult, fbmResult, fbmResult, colorFac);
    }
}

//Finer details
float fbmDetail = fbm(point * 20.);
vec4 details = vec4( 0.7 + fbmDetail, 0.7 + fbmDetail, 0.7 + fbmDetail, 1.0);

//result = mix(result, details, result.a);
result = result * details;
result = result * details;

return result;
}
如果我删除云生成的细节部分,我还将FPS从300增加到600

是我做错了什么,还是当我做这样的事情时,着色器的这种性能密集型是很自然的

作为参考,这是整个着色器

    uniform vec2 iResolution;
uniform vec2 iMouse;

uniform float time;

uniform float overcast;
uniform float posX;
uniform float posY;

uniform vec4 dSky; //The color of the sky during the day
uniform vec4 nSky; //The color of the sky during the night

uniform vec4 dCloud; //The color of the clouds at day
uniform vec4 nCloud; //The color of the clouds at night

float resFact = iResolution.x / 500;

//float overcast = iMouse.y / iResolution.y;
//float posX = iMouse.x / iResolution.x;

/*float nSkyR = 0.05;
float nSkyG = 0.05;
float nSkyB = 0.39;*/

float rand(vec2 co){
  return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
}

float hash( float n ) //Borrowed from voltage
{
    return fract(sin(n)*43758.5453);
}

float fBmWRand( vec2 p )//Borroowed from Mark Sleith
{
        float f = 0.0;
        f += 0.50000*rand( p ); p = p*2.02;
        f += 0.25000*rand( p ); p = p*2.03;
        f += 0.12500*rand( p ); p = p*2.01;
        f += 0.06250*rand( p ); p = p*2.04;
        f += 0.03125*rand( p );
        return f/0.984375;
}

float noise( in vec2 x )//Borroowed from Mark Sleith
{
    vec2 p = floor(x);
    vec2 f = fract(x);
        f = f*f*(3.0-2.0*f);
        float n = p.x + p.y*57.0;
        float res = mix(mix( hash(n+  0.0), hash(n+  1.0),f.x), mix( hash(n+ 57.0), hash(n+ 58.0),f.x),f.y);
        return res;
}

float fbm( vec2 p ) //Borroowed from Mark Sleith
{
        float f = 0.0;
        f += 0.50000*noise( p ); p = p*2.02;
        f += 0.25000*noise( p ); p = p*2.03;
        f += 0.12500*noise( p ); p = p*2.01;
        f += 0.06250*noise( p ); p = p*2.04;
        f += 0.03125*noise( p );
        return f/0.984375;
}

vec3 bgGradient()
{
    //Getting the height of the current pixel
    float height = gl_FragCoord.y / iResolution.y;

    //Calculating the brightness of the pixel
    float brightness = 1.0 - 0.4 * height;

    //Combining everything into a background
    vec3 grad = vec3(1., 1., 1.);// * brightness;
    return grad;
}

bool star()
{
    /*//Getting a position to run random calculations with
    float pos = (gl_FragCoord.x / iResolution.x) * (gl_FragCoord.y / iResolution.y) + 0.5;

    if(hash(pos) < 0.001)
    {
        return true;
    }
    return false*/;

    if(fBmWRand(gl_FragCoord.xy / iResolution.xy) < 0.08)
    {
        return true;
    }
    return false;
}

float cloudFadeDist = 0.1; //The distance at which the clouds will start fading away

vec4 clouds( vec2 point )
{
    vec4 result = vec4(0., 0., 0., 0.);

    //Checking if the cloud is above
    float fbmResult = fbm(point * 5.);
    if(fbmResult > overcast)
    {
        result = vec4(fbmResult, fbmResult, fbmResult, 1.0);
        //result = vec4(1., 1., 1., 1.);
    }
    else if(fbmResult > overcast - (cloudFadeDist / resFact)) //Outlining the clouds
    {
        float dist = overcast - fbmResult;
        float colorFac = 1.0 - dist / (cloudFadeDist / resFact);

        if(colorFac > 0.0001)
        {
            result = vec4(fbmResult, fbmResult, fbmResult, colorFac);
        }
    }

    //Finer details
    float fbmDetail = fbm(point * 20.);
    vec4 details = vec4( 0.7 + fbmDetail, 0.7 + fbmDetail, 0.7 + fbmDetail, 1.0);

    //result = mix(result, details, result.a);
    result = result * details;
    result = result * details;

    return result;
}

vec2 sunPos = vec2(0.15, 0.1);

float sunWidth = 0.03;
float sunGlow = 0.015;

float sunR = 1.;
float sunG = 1.;
float sunB = 0.8;

vec4 sun()
{
    vec4 result = vec4(0., 0., 0., 0.);
    float xPos = gl_FragCoord.x / iResolution.x;
    float yPos = gl_FragCoord.y / iResolution.x;

    float xDist = xPos - sunPos.x;
    float yDist = yPos - sunPos.y;

    float dist = sqrt(pow(xDist, 2.) + pow(yDist, 2.));


    if(dist < sunWidth)
    {
        result = vec4(sunR, sunG, sunB, 1.);
    }
    else if(dist < sunWidth + sunGlow)
    {
        float distFact = (dist - sunWidth) / sunGlow;

        result = vec4(sunR, sunG, sunB , 1. - distFact);
    }

    return result;
}

float nStart = 2200;
float nEnd = 600;
float dStart = 800;
float dEnd = 2000;

void main(void)
{
    //Cretaing the final color variable and adding the gradient
    vec4 finalColor = vec4(bgGradient(), 1.0);

    //Creating stars
    vec4 starLayer = vec4(0., 0., 0., 0.);
    if(star() == true)
    {
        starLayer = vec4(1., 1., 1., 1.);   //Make the pixel very bright
    }

    //Generating the clouds
    vec4 cloudLayer = vec4(0., 0., 0., 0.);
    for(int i = 0; i < 4; i++)
    {
        //clouds( (15.0 * float(i)) + gl_FragCoord.xy / iResolution.xy + posX * float(i + 1));
        /*vec4 cloud = clouds( vec2((15. * float(i)) + gl_FragCoord.x / iResolution.x + posX * float(i + 1)),
                            (15. * float(i)) + gl_FragCoord.y / iResoulution.y);*/

        vec4 cloud = clouds( vec2( (15. * float(i)) + gl_FragCoord.x / iResolution.x + posX * float(i + 1),
            (15. * float(i)) + gl_FragCoord.y / iResolution.y + posY * float(i + 1)));

        //finalColor = finalColor + vec4(cloud, 1.);
        cloudLayer = mix(cloudLayer, cloud, cloud.a);
    }

    if(time > nStart || time < nEnd) //Nighttime
    {
        finalColor = finalColor * nSky;

        finalColor = mix(finalColor, starLayer, starLayer.a);

        cloudLayer = cloudLayer * nCloud;
        //finalColor = mix(finalColor, cloudLayer, cloudLayer.a);
        vec4 genericColor = cloudLayer;
        finalColor = mix(finalColor, cloudLayer, cloudLayer.a);
        //finalColor = vec4(nSky.r, nSky.g, nSky.b, 1.0);
    }
    else if(time > dStart && time < dEnd)
    {
        finalColor = finalColor * dSky;

        //cloudLayer = cloudLayer * dCloud;
        finalColor = mix(finalColor, cloudLayer, cloudLayer.a);
    }
    else if(time > dEnd && time < nStart) //Evening
    {
        float timeFact = (time - dEnd) / (nStart - dEnd);

        //Calculating the diffirence between night and day
        vec4 skyDiff = vec4(nSky.r - dSky.r, nSky.g - dSky.g, nSky.b - dSky.b, 1.);
        vec4 skyColor = vec4(dSky.r + (skyDiff.r * timeFact), dSky.g + (skyDiff.g * timeFact), dSky.b + (skyDiff.b * timeFact), 1.);

        finalColor = skyColor;

        //Stars
        finalColor = mix(finalColor, starLayer, starLayer * timeFact);

        //Clouds
        vec4 cloudDiff = vec4(nCloud.r - dCloud.r, nCloud.g - dCloud.g, nCloud.b - dCloud.b, 1.);
        vec4 cloudColor = vec4(dCloud.r + (cloudDiff.r * timeFact), dCloud.g + (cloudDiff.g * timeFact), dCloud.b + (cloudDiff.b * timeFact), 1.);
        vec4 cloudLayer = cloudLayer * cloudColor;
        finalColor = mix(finalColor, cloudLayer, cloudLayer.a);
    }
    else if(time > nEnd && time < dStart) //Evening
    {
        float timeFact = (time - nEnd) / (dStart - nEnd);

        //Calculating the diffirence between night and day
        vec4 skyDiff = vec4(dSky.r - nSky.r, dSky.g - nSky.g, dSky.b - nSky.b, 1.);
        vec4 skyColor = vec4(nSky.r + (skyDiff.r * timeFact), nSky.g + (skyDiff.g * timeFact), nSky.b + (skyDiff.b * timeFact), 1.);

        finalColor = skyColor;

        //Stars
        finalColor = mix(finalColor, starLayer, starLayer * 1. - timeFact);

        //Clouds
        vec4 cloudDiff = vec4(dCloud.r - nCloud.r, dCloud.g - nCloud.g, dCloud.b - nCloud.b, 1.);
        vec4 cloudColor = vec4(nCloud.r + (cloudDiff.r * timeFact), nCloud.g + (cloudDiff.g * timeFact), nCloud.b + (cloudDiff.b * timeFact), 1.);
        vec4 cloudLayer = cloudLayer * cloudColor;
        finalColor = mix(finalColor, cloudLayer, cloudLayer.a);
    }

    //finalColor = vec4(1., 0., 0., 1.);

    //vec4 sunColor = sun();
    //finalColor = mix(finalColor, sunColor, sunColor.a);
    //If there is a star
    /*if(star() == true)
    {
        finalColor = vec4(1., 1., 1., 1.);  //Make the pixel very bright
    }*/

    /*for(int i = 0; i < 4; i++)
    {
        //clouds( (15.0 * float(i)) + gl_FragCoord.xy / iResolution.xy + posX * float(i + 1));
        //vec4 cloud = clouds( vec2((15. * float(i)) + gl_FragCoord.x / iResolution.x + posX * float(i + 1)),
                            (15. * float(i)) + gl_FragCoord.y / iResoulution.y);

        vec4 cloud = clouds( vec2( (15. * float(i)) + gl_FragCoord.x / iResolution.x + posX * float(i + 1),
            (15. * float(i)) + gl_FragCoord.y / iResolution.y + posY * float(i + 1)));

        //finalColor = finalColor + vec4(cloud, 1.);
        finalColor = mix(finalColor, cloud, cloud.a);
    }*/

    gl_FragColor = finalColor;
}
统一的vec2解决方案;
均匀载体;
均匀浮动时间;
均匀浮云;
均匀浮点数;
均匀浮点数;
均匀vec4-dSky//白天天空的颜色
均匀vec4-nSky//夜晚天空的颜色
均匀矢量云//白天云彩的颜色
均匀矢量场//夜晚云彩的颜色
float resFact=iResolution.x/500;
//浮云云=i使用.y/i解决方案.y;
//float posX=imuse.x/iResolution.x;
/*浮动nSkyR=0.05;
浮动nSkyG=0.05;
浮动nSkyB=0.39*/
浮动兰德(vec2公司){
返回分数(sin(dot(co.xy,vec2(12.9898,78.233)))*43758.5453);
}
float hash(float n)//从电压中借用
{
返回分数(sin(n)*43758.5453);
}
浮动fBmWRand(vec2 p)//Borroowed自Mark Sleith
{
浮动f=0.0;
f+=0.50000*兰特(p);p=p*2.02;
f+=0.25000*兰特(p);p=p*2.03;
f+=0.12500*兰特(p);p=p*2.01;
f+=0.06250*兰特(p);p=p*2.04;
f+=0.03125*兰特(p);
返回f/0.984375;
}
浮动噪声(以vec2 x为单位)//Borroowed来自Mark Sleith
{
vec2p=地板(x);
vec2f=fract(x);
f=f*f*(3.0-2.0*f);
浮动n=p.x+p.y*57.0;
float res=混合(混合(散列(n+0.0)、散列(n+1.0)、f.x)、混合(散列(n+57.0)、散列(n+58.0)、f.x)、f.y);
返回res;
}
浮动fbm(vec2 p)//从Mark Sleith处删除
{
浮动f=0.0;
f+=0.50000*噪声(p);p=p*2.02;
f+=0.25000*噪声(p);p=p*2.03;
f+=0.12500*噪声(p);p=p*2.01;
f+=0.06250*噪声(p);p=p*2.04;
f+=0.03125*噪声(p);
返回f/0.984375;
}
vec3梯度()
{
//获取当前像素的高度
浮动高度=gl_FragCoord.y/iResolution.y;
//计算像素的亮度
浮动亮度=1.0-0.4*高度;
//将所有内容合并到背景中
vec3梯度=vec3(1,1,1.);/*亮度;
返回梯度;
}
布尔之星()
{
/*//获得一个位置来运行随机计算
浮动位置=(gl_FragCoord.x/iResolution.x)*(gl_FragCoord.y/iResolution.y)+0.5;
如果(散列(位置)<0.001)
{
返回true;
}
返回false*/;
如果(fBmWRand(gl_FragCoord.xy/iResolution.xy)<0.08)
{
返回true;
}
返回false;
}
浮云FADEDIST=0.1//云层开始消失的距离
vec4云(vec2点)
{
vec4结果=vec4(0,0,0,0.);
//检查云是否在上面
浮动fbmResult=fbm(点*5.);
如果(fbmResult>阴天)
{
结果=vec4(fbmResult,fbmResult,fbmResult,1.0);
//结果=vec4(1,1,1,1.);
}
else if(fbmResult>cloud-(cloudFadeDist/resFact))//勾勒出云的轮廓
{
浮动距离=阴天-fbmResult;
float colorFac=1.0-距离/(cloudFadeDist/resFact);
如果(colorFac>0.0001)
{
结果=vec4(fbmResult,fbmResult,fbmResult,colorFac);
}
}
//更精细的细节
浮点数fbmDetail=fbm(点*20.);
vec4详细信息=vec4(0.7+fbmDetail,0.7+fbmDetail,0.7+fbmDetail,1.0);
//结果=混合(结果、细节、结果a);
结果=结果*详细信息;
结果=结果*详细信息;
返回结果;
}
vec2-sunPos=vec2(0.15,0.1);
浮动太阳宽度=0.03;
浮动太阳光低=0.015;
浮点数sunR=1。;
浮点数=1。;
浮动sunB=0.8;
vec4 sun()
{
vec4结果=vec4(0,0,0,0.);
float xPos=gl_FragCoord.x/iResolution.x;
浮动yPos=gl_FragCoord.y/iResolution.x;
float xDist=xPos-sunPos.x;
浮动yDist=yPos-太阳位置y;
浮动距离=sqrt(功率(xDist,2.)+功率(yDist,2.));
if(距离<太阳宽度)
{
结果=vec4(sunR,sunG,sunB,1.);
}
否则如果(距离<太阳宽度+太阳低)
{
float distFact=(dist-sunWidth)/sunGlow;
结果=vec4(sunR,sunG,sunB,1.-distFact);
}
返回结果;
}
浮点数nStart=2200;
浮动nEnd=600;
浮点数dStart=800;
浮点数=2000;
真空总管(真空)
{
//cretain最终颜色变量并添加渐变
vec4 finalColor=vec4(bgGradient(),1.0);
//创造明星
vec4星层=vec4(0,0,0,0.);
如果(星形()==true)
{
starLayer=vec4(1,1,1,1.);//使像素非常明亮
}
//生成云
vec4云层=vec4(0,0,0,0.);
对于(int i=0;i<4;i++)
{
//云((15.0*float(i))+gl_FragCoord.xy/iResolution.xy+posX*float(i+1));
/*vec4云=云(vec2((15.*float(i))+gl_FragCoord.x/iResolution.x+posX*float(i+1)),
(15.*浮动(i))+gl_FragCoord.y/iResolution.y)*/
vec4云=云(vec2((15.*float(i))+gl_FragCoord.x/iResolution.x+posX*float(i+1),
(15.*float(i))+gl_FragCoord.y/iResolution.y+posY*float(i+1));
//最终颜色=最终颜色+vec4(云,1.);
cloudLayer=混合(cloudLayer,cloud,cloud.a);
}
if(time>nStart | | timegl_FragColor = finalColor;
    uniform vec2 iResolution;
uniform vec2 iMouse;

uniform float time;

uniform float overcast;
uniform float posX;
uniform float posY;

uniform vec4 dSky; //The color of the sky during the day
uniform vec4 nSky; //The color of the sky during the night

uniform vec4 dCloud; //The color of the clouds at day
uniform vec4 nCloud; //The color of the clouds at night

float resFact = iResolution.x / 500;

//float overcast = iMouse.y / iResolution.y;
//float posX = iMouse.x / iResolution.x;

/*float nSkyR = 0.05;
float nSkyG = 0.05;
float nSkyB = 0.39;*/

float rand(vec2 co){
  return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
}

float hash( float n ) //Borrowed from voltage
{
    return fract(sin(n)*43758.5453);
}

float fBmWRand( vec2 p )//Borroowed from Mark Sleith
{
        float f = 0.0;
        f += 0.50000*rand( p ); p = p*2.02;
        f += 0.25000*rand( p ); p = p*2.03;
        f += 0.12500*rand( p ); p = p*2.01;
        f += 0.06250*rand( p ); p = p*2.04;
        f += 0.03125*rand( p );
        return f/0.984375;
}

float noise( in vec2 x )//Borroowed from Mark Sleith
{
    vec2 p = floor(x);
    vec2 f = fract(x);
        f = f*f*(3.0-2.0*f);
        float n = p.x + p.y*57.0;
        float res = mix(mix( hash(n+  0.0), hash(n+  1.0),f.x), mix( hash(n+ 57.0), hash(n+ 58.0),f.x),f.y);
        return res;
}

float fbm( vec2 p ) //Borroowed from Mark Sleith
{
        float f = 0.0;
        f += 0.50000*noise( p ); p = p*2.02;
        f += 0.25000*noise( p ); p = p*2.03;
        f += 0.12500*noise( p ); p = p*2.01;
        f += 0.06250*noise( p ); p = p*2.04;
        f += 0.03125*noise( p );
        return f/0.984375;
}

vec3 bgGradient()
{
    //Getting the height of the current pixel
    float height = gl_FragCoord.y / iResolution.y;

    //Calculating the brightness of the pixel
    float brightness = 1.0 - 0.4 * height;

    //Combining everything into a background
    vec3 grad = vec3(1., 1., 1.);// * brightness;
    return grad;
}

bool star()
{
    /*//Getting a position to run random calculations with
    float pos = (gl_FragCoord.x / iResolution.x) * (gl_FragCoord.y / iResolution.y) + 0.5;

    if(hash(pos) < 0.001)
    {
        return true;
    }
    return false*/;

    if(fBmWRand(gl_FragCoord.xy / iResolution.xy) < 0.08)
    {
        return true;
    }
    return false;
}

float cloudFadeDist = 0.1; //The distance at which the clouds will start fading away

vec4 clouds( vec2 point )
{
    vec4 result = vec4(0., 0., 0., 0.);

    //Checking if the cloud is above
    float fbmResult = fbm(point * 5.);
    if(fbmResult > overcast)
    {
        result = vec4(fbmResult, fbmResult, fbmResult, 1.0);
        //result = vec4(1., 1., 1., 1.);
    }
    else if(fbmResult > overcast - (cloudFadeDist / resFact)) //Outlining the clouds
    {
        float dist = overcast - fbmResult;
        float colorFac = 1.0 - dist / (cloudFadeDist / resFact);

        if(colorFac > 0.0001)
        {
            result = vec4(fbmResult, fbmResult, fbmResult, colorFac);
        }
    }

    //Finer details
    float fbmDetail = fbm(point * 20.);
    vec4 details = vec4( 0.7 + fbmDetail, 0.7 + fbmDetail, 0.7 + fbmDetail, 1.0);

    //result = mix(result, details, result.a);
    result = result * details;
    result = result * details;

    return result;
}

vec2 sunPos = vec2(0.15, 0.1);

float sunWidth = 0.03;
float sunGlow = 0.015;

float sunR = 1.;
float sunG = 1.;
float sunB = 0.8;

vec4 sun()
{
    vec4 result = vec4(0., 0., 0., 0.);
    float xPos = gl_FragCoord.x / iResolution.x;
    float yPos = gl_FragCoord.y / iResolution.x;

    float xDist = xPos - sunPos.x;
    float yDist = yPos - sunPos.y;

    float dist = sqrt(pow(xDist, 2.) + pow(yDist, 2.));


    if(dist < sunWidth)
    {
        result = vec4(sunR, sunG, sunB, 1.);
    }
    else if(dist < sunWidth + sunGlow)
    {
        float distFact = (dist - sunWidth) / sunGlow;

        result = vec4(sunR, sunG, sunB , 1. - distFact);
    }

    return result;
}

float nStart = 2200;
float nEnd = 600;
float dStart = 800;
float dEnd = 2000;

void main(void)
{
    //Cretaing the final color variable and adding the gradient
    vec4 finalColor = vec4(bgGradient(), 1.0);

    //Creating stars
    vec4 starLayer = vec4(0., 0., 0., 0.);
    if(star() == true)
    {
        starLayer = vec4(1., 1., 1., 1.);   //Make the pixel very bright
    }

    //Generating the clouds
    vec4 cloudLayer = vec4(0., 0., 0., 0.);
    for(int i = 0; i < 4; i++)
    {
        //clouds( (15.0 * float(i)) + gl_FragCoord.xy / iResolution.xy + posX * float(i + 1));
        /*vec4 cloud = clouds( vec2((15. * float(i)) + gl_FragCoord.x / iResolution.x + posX * float(i + 1)),
                            (15. * float(i)) + gl_FragCoord.y / iResoulution.y);*/

        vec4 cloud = clouds( vec2( (15. * float(i)) + gl_FragCoord.x / iResolution.x + posX * float(i + 1),
            (15. * float(i)) + gl_FragCoord.y / iResolution.y + posY * float(i + 1)));

        //finalColor = finalColor + vec4(cloud, 1.);
        cloudLayer = mix(cloudLayer, cloud, cloud.a);
    }

    if(time > nStart || time < nEnd) //Nighttime
    {
        finalColor = finalColor * nSky;

        finalColor = mix(finalColor, starLayer, starLayer.a);

        cloudLayer = cloudLayer * nCloud;
        //finalColor = mix(finalColor, cloudLayer, cloudLayer.a);
        vec4 genericColor = cloudLayer;
        finalColor = mix(finalColor, cloudLayer, cloudLayer.a);
        //finalColor = vec4(nSky.r, nSky.g, nSky.b, 1.0);
    }
    else if(time > dStart && time < dEnd)
    {
        finalColor = finalColor * dSky;

        //cloudLayer = cloudLayer * dCloud;
        finalColor = mix(finalColor, cloudLayer, cloudLayer.a);
    }
    else if(time > dEnd && time < nStart) //Evening
    {
        float timeFact = (time - dEnd) / (nStart - dEnd);

        //Calculating the diffirence between night and day
        vec4 skyDiff = vec4(nSky.r - dSky.r, nSky.g - dSky.g, nSky.b - dSky.b, 1.);
        vec4 skyColor = vec4(dSky.r + (skyDiff.r * timeFact), dSky.g + (skyDiff.g * timeFact), dSky.b + (skyDiff.b * timeFact), 1.);

        finalColor = skyColor;

        //Stars
        finalColor = mix(finalColor, starLayer, starLayer * timeFact);

        //Clouds
        vec4 cloudDiff = vec4(nCloud.r - dCloud.r, nCloud.g - dCloud.g, nCloud.b - dCloud.b, 1.);
        vec4 cloudColor = vec4(dCloud.r + (cloudDiff.r * timeFact), dCloud.g + (cloudDiff.g * timeFact), dCloud.b + (cloudDiff.b * timeFact), 1.);
        vec4 cloudLayer = cloudLayer * cloudColor;
        finalColor = mix(finalColor, cloudLayer, cloudLayer.a);
    }
    else if(time > nEnd && time < dStart) //Evening
    {
        float timeFact = (time - nEnd) / (dStart - nEnd);

        //Calculating the diffirence between night and day
        vec4 skyDiff = vec4(dSky.r - nSky.r, dSky.g - nSky.g, dSky.b - nSky.b, 1.);
        vec4 skyColor = vec4(nSky.r + (skyDiff.r * timeFact), nSky.g + (skyDiff.g * timeFact), nSky.b + (skyDiff.b * timeFact), 1.);

        finalColor = skyColor;

        //Stars
        finalColor = mix(finalColor, starLayer, starLayer * 1. - timeFact);

        //Clouds
        vec4 cloudDiff = vec4(dCloud.r - nCloud.r, dCloud.g - nCloud.g, dCloud.b - nCloud.b, 1.);
        vec4 cloudColor = vec4(nCloud.r + (cloudDiff.r * timeFact), nCloud.g + (cloudDiff.g * timeFact), nCloud.b + (cloudDiff.b * timeFact), 1.);
        vec4 cloudLayer = cloudLayer * cloudColor;
        finalColor = mix(finalColor, cloudLayer, cloudLayer.a);
    }

    //finalColor = vec4(1., 0., 0., 1.);

    //vec4 sunColor = sun();
    //finalColor = mix(finalColor, sunColor, sunColor.a);
    //If there is a star
    /*if(star() == true)
    {
        finalColor = vec4(1., 1., 1., 1.);  //Make the pixel very bright
    }*/

    /*for(int i = 0; i < 4; i++)
    {
        //clouds( (15.0 * float(i)) + gl_FragCoord.xy / iResolution.xy + posX * float(i + 1));
        //vec4 cloud = clouds( vec2((15. * float(i)) + gl_FragCoord.x / iResolution.x + posX * float(i + 1)),
                            (15. * float(i)) + gl_FragCoord.y / iResoulution.y);

        vec4 cloud = clouds( vec2( (15. * float(i)) + gl_FragCoord.x / iResolution.x + posX * float(i + 1),
            (15. * float(i)) + gl_FragCoord.y / iResolution.y + posY * float(i + 1)));

        //finalColor = finalColor + vec4(cloud, 1.);
        finalColor = mix(finalColor, cloud, cloud.a);
    }*/

    gl_FragColor = finalColor;
}