/* cloud_background.sl -- procedural surface shader to puts cloud texture and wispiness on a patch
 */

surface
cloud_background (float Km = 0.35,
												lightness = 1.0,
									 	    cloud_density = .05){
		 float size = 0.7,
					 magnitude = 0.0,
					 i, balance;


     color cs, ct;
     Oi = 1.0;

		 v = 1 - v;

		 for(i=0; i<20.0; i+= 1.0){
				magnitude += (.5 - noise(P * size)) / size;
				size *= 2.0;
		 }

     /* Apply the background texture */
     cs = color "rgb" (.3*lightness, .5*lightness, 1.0);					/** Initial sky blue color **/
		 cs = cs * .6 * exp(1.1*(v));																 /** Exponential vertical gradient **/

		 ct = (1-magnitude*Km*v);					/** Add noise and gradient since clouds exists at lower altitude **/

		 balance = exp(1-v-cloud_density)/2.7182;							/** Exponential vertical gradient **/
		 cs = mix(ct,cs,balance);		/** Mix clouds with background sky **/ 

     Ci = cs;


}