Generate Sky & Sun in Ray Marching algorithm is very simple to process. When the map procedure don’t intersect a scene obiect (In my shader Hit variable is false) gl_FragColor set to background color. Is simple way to create it:

  1. First define te Sun structure:
    struct Sun {
        vec3 posistion;
        vec3 color;
        float haloSize;
        float haloPower;
        float discSize;
        float discPower;
        float discArea;
        float horizontal;
    };
    Sun sunLight=Sun(vec3(-0.2, 0.2,  -0.4),vec3(1.0,1.0,1.0), 100.0, 0.3, 100.0, 1.5, 0.9, 5.0);
  2. Test the intersect a scene object:
        Object sceneObject=rayMarching(ray, rayDir);
        vec3 outColor=vec3(0.0, 0.0, 0.0);
        if (sceneObject.hit){
    ...
            outColor=outColor * lightColor * baseShadow + sceneObject.mat.ambient;
        } else {
            outColor=getSky(sceneObject, ray, rayDir);  
        }
    
        gl_FragColor=vec4(outColor.r, outColor.g, outColor.b, 1.0);
  3. If intersect is false result generate background color in the getSky function:
    vec3 getSky(Object sceneObject, vec3 ray, vec3 rayDir){
    
        vec3 skyColor=vec3(0.0);
        float horizontHeight = pow(1.0-max(rayDir.y,0.0), sunLight.horizontal);
        skyColor=mix(vec3(0.0, 0.0, 0.5), vec3(1.0, 0.0, 0.0), horizontHeight);
    
        #if ENABLE_SUN==1
            float sunAmount = max(dot(rayDir, normalize(sunLight.posistion)), 0.0);
            // Sun Halo
            skyColor = skyColor + sunLight.color * pow(sunAmount, sunLight.haloSize) * sunLight.haloPower;
            // Sun Disc
            skyColor = skyColor + sunLight.color * min(pow(sunAmount, sunLight.discSize) * sunLight.discPower, sunLight.discArea);
        #endif
    
        return clamp(skyColor, 0.0, 1.0);
    }
  4. To create more funny effect – where intersect if true, create a halo effect in post processing (in future).

Example of this sun effect: