/*nightsky surface shader * Final Assignment: Shader with turbulence, perturbed patterns, bombing. * Tom Suchan * This shader generates a night sky with a wash from blue to black and a randomized star field. It also includes an optional gas nebula to simulate other planetary environments. The user has the option to input "Venus" which creates a bluish space cloud, "Mercury" to create a volatile fire like backdrop, or space fans can create their own by changing colors--good luck spacecadets. */ #include "rmannotes.sl" surface nightsky(float Ka = 2, Kd = 2, Ks = 1, roughness = 0.8; color specularcolor = 1; float starfreq = .5; string location ="") { color paper, ink; float paper_opac = 1, ink_opac; point center; float radius, ss, tt; float col, row, noi,noi2; float freq = 6; float d; float fuzz = .1; float circle1, circle2; point Nf, V; float noifreq = 5; float noiscale = 0.8; float scale, turb = 0; float turbfreq = 2; point PP; float pixelsize, twice; /*layer one horizon: creates a color wash*/ paper_opac = 1; paper = color(0, 0, 0); ink_opac = 1; ink =spline(t,color(0,0,0),color(0,0,0),color(0,0,0), color(.5 ,0,.5), color(0, 0,1), color(0, 0, 1)); paper= mix(paper, ink, ink_opac); /* Optional layer two: cloud nebulas */ if(location == "venus"){ noi2 = noise(s * noifreq, t * noifreq); /* perturb y */ t = t + snoise(noi2 + 3752) * noiscale; ink = spline(t,color(1,1,1),color(1,1,1),color(0,1,.2), color(1,1,1), color(0, 0,1), color(0, 0, 1)); ink_opac = pulse(0.85, 0.95, fuzz, t); paper = mix(paper, ink, ink_opac); } if(location == "mercury"){ PP = transform("shader", P) * turbfreq; /* creates trubulence based on P */ pixelsize = sqrt(area(PP)); twice = pixelsize * 2; for(scale = 1; scale > twice; scale /= 2) turb += abs(noise(PP/scale) - 0.5) * scale; turb = turb * 2; ink = spline(turb, color(1,1,0), color(1,1,0), color(1,0,0), color(1,1,1), color(1,1,1)); ink_opac = pulse(.6, 1, .25, t); paper = mix(paper, ink, ink_opac); } /*layer three star field base-seed to udn on current row and column*/ col = whichtile(s, freq); row = whichtile(t, freq); noi = noise(col * 10 + 0.5, row * 10 + 0.5); /* repeat texture coords, jitter tiles by plus or minus 0.35, and rotate by 45 */ tt = repeat(s, freq) + udn(noi * 1183, .1, 0.6); ss = repeat(t, freq) + udn(noi * 999, .1, 0.6); /* generates star-filed tiles, sets radius, creates % of stars */ ink = color(1, 1, 1); center = (0.5, 0.5, 0); radius = udn(noi * 4362, .005,.05); d = distance(center, (ss, tt, 0)); if(udn(noi * 314, 0,1) > starfreq) { freq = 8; fuzz = .05; ink_opac = 1 - smoothstep(radius - fuzz, radius + fuzz, d); paper = mix(paper, ink, ink_opac); } /* illumination */ paper = paper * Ka * ambient() + paper * Kd * diffuse(Nf) + specularcolor * Ks * specular(Nf, V, roughness); /*output*/ Oi=paper_opac; Ci=paper_opac * paper; }