
vec3 RenderSunMoon( vec3 viewDir){
     vec3 color=vec3(0);   
        
        vec3 sunDir  = GetSunDirection();
        vec3 moonDir = GetMoonDirection();
        float rainFactor = rain_time();
        vec4 phases = GetAtmospherePhases();
        float sunVisibility   = phases.x + phases.y + phases.z;
        float sunRain   = RainVisibility(rainFactor, sunVisibility);
        
 
 #if VELPIX_SUN == VELPIX_SUN_VELPIX
        
        float outline = ProceduralArtSunOutline(viewDir, sunDir, frameTimeCounter) *sunVisibility*sunRain;
        float sun = ProceduralArtSun(viewDir, sunDir, frameTimeCounter)*sunVisibility*sunRain;

        color = mix(color, vec3(1.0, 0.3765, 0.3765), outline);
        color += sun * vec3(1.0, 0.651, 0.0);

        #elif VELPIX_SUN == VELPIX_SUN_VANILLA
        color = color;
        #endif

        #if VELPIX_MOON == VELPIX_MOON_VELPIX
        
        
        float moonVisibility  = phases.w;
        float moonRain  = RainVisibility(rainFactor, moonVisibility);

        vec2 MoonUV = GetMoonUV(viewDir, moonDir);

        float radius = 0.06;

        float moonMask = 1.0 - smoothstep(radius, radius + 0.01, length(MoonUV));

        vec2 MoonTexUv = MoonUV / radius;
        MoonTexUv = MoonTexUv * 0.5 + 0.5;

        vec4 moonTex = texture2D(colortex13, clamp(MoonTexUv, 0.0, 1.0));
        moonTex += texture2D(colortex12, clamp(MoonTexUv, 0.0, 1.0));
        float facing = step(0.0, dot(viewDir, moonDir));

        color += (moonTex.rgb * moonMask * facing) * moonVisibility * moonRain;

        #elif VELPIX_MOON == VELPIX_MOON_Vanilla
        color = color;
        #endif

        return color;
}



vec3 vpRenderSky(vec3 viewDir)
{
	float rainFactor = rain_time();

		float h = clamp(viewDir.y * 0.5 + 0.5, 0.0, 1.0);

		vec4 phases = GetAtmospherePhases();

		vec3 sunDir  = GetSunDirection();
    	vec3 moonDir = GetMoonDirection();



	
		vec3 sky = GetSkyPalette(phases, h);

       

		#ifdef Painted_Sky
		vec2 uv = normalize(viewDir).xy * 0.5 + 0.5;
		float brush = texture2D(colortex10, uv).r;
        brush += texture2D(colortex11, uv).r;
		brush = (brush - 0.5) * 0.05 * (1.0 - rainFactor);
		sky += brush;
		#endif

		#ifdef PseudoScattering
		sky += (GetPseudoScattering(viewDir, sunDir, phases) * (1.0 - rainFactor));
        sky += MoonScatter(viewDir, moonDir, phases) * (1.0 - rainFactor);
		#endif

		#ifdef Sky_SATURATION
		sky = vpSkySaturation(sky, Sky_SATURATION_AMOUNT);
		#endif

        vec3 caveSkyColor = vec3(0.02, 0.03, 0.05);

        float Cave = caveFactor(cameraPosition);

        sky = mix(sky, caveSkyColor, Cave);

        return sky;
}