Shaders
usa_flag - A procedural shader for a U.S. Flag
/* shader usa_flag - dsward@webnation.com
* based on Steve May's Renderman notes at http://www.cgrg.ohio-state.edu/~smay/RManNotes
* and _Texturing and Modeling, A Procedural Approach_ by Darwyn Peachey, et al, ISBN 0-12-228760-6
*/
#define whichtile(x,freq) (floor((x) * (freq)))
#define repeat(x,freq) (mod((x) * (freq), 1.0))
#define pulse(a,b,fuzz,x) (smoothstep((a)-(fuzz),(a),(x)) - \
smoothstep((b)-(fuzz),(b),(x)))
#define odd(x) (mod((x), 2) == 1)
#define even(x) (mod((x), 2) == 0)
surface
usa_flag
(
float fuzz = 0.01;
float Ka = 0.5;
float Kd = 1;
float Ks = 0.0;
float roughness = 0.1;
color specular_color = 1;
)
{
color surface_color = color(1, 0 ,0);
color surface_opac = color(1);
float ss, tt, angle, r, a;
float in_out = 0;
float cell_s, cell_t;
point p0, p1;
point d0, d1;
float star_angle;
float mix_ratio;
float offset = 0;
float spin = 0;
float r_min = 0.36;
float r_max = 0.84;
float star_points = 5;
float blue_right = 0.4;
float blue_bottom = 7 / 13;
float cell_star = 1;
point Nf, V;
float star_fuzz;
float stripe_fuzz;
float blue_fuzz_s, blue_fuzz_t;
float stripe_idx;
/* overlay red stripes */
stripe_fuzz = fuzz * 13;
tt = repeat(t, 13);
stripe_idx = whichtile(t, 13);
mix_ratio = pulse(0, 1, stripe_fuzz, tt);
if (stripe_idx == 12) mix_ratio = 1.0;
if odd(stripe_idx)
{
surface_color = mix(surface_color, color(1, 1, 1), mix_ratio);
}
else
{
surface_color = mix( color(1, 1, 1), surface_color, mix_ratio);
}
/* overlay blue field */
blue_fuzz_s = fuzz / 3;
blue_fuzz_t = fuzz * 1.6;
mix_ratio = pulse(0, blue_right + blue_fuzz_s, blue_fuzz_s, s) *
pulse(0, blue_bottom + (blue_fuzz_t * 0.25), blue_fuzz_t, t);
surface_color = mix(surface_color, color(0, 0, 0.5), mix_ratio);
/* overlay stars */
cell_s = whichtile(s, 32);
cell_t = whichtile(t, 20);
if (cell_s < 1) cell_star = 0;
if (cell_t < 1) cell_star = 0;
if (cell_s > 11) cell_star = 0;
if (cell_t > 9) cell_star = 0;
if (cell_star == 1)
{
if even(cell_s)
{
if odd(cell_t) cell_star = 0;
}
else
{
if even(cell_t) cell_star = 0;
}
}
if (cell_star == 1)
{
ss = repeat(s, 32) - 0.5;
tt = repeat(t, 20) - 0.5;
star_angle = 2 * PI / star_points;
star_fuzz = 0.15;
p0 = r_max * (cos(0), sin(0), 0);
p1 = r_min * (cos(star_angle / 2), sin(star_angle / 2), 0);
d0 = p1 - p0;
angle = atan(ss, tt) + PI;
r = sqrt(ss * ss + tt * tt);
a = mod(angle, star_angle) / star_angle;
if (a >= 0.5) a = 1 - a;
d1 = r * (cos(a), sin(a), 0) - p0;
in_out += smoothstep(0, star_fuzz, zcomp(d0 ^ d1));
mix_ratio = smoothstep(0.5, 0.5 + star_fuzz, in_out);
surface_color = mix(surface_color, color(1, 1, 1), mix_ratio);
surface_opac = surface_opac + mix_ratio;
}
/* plastic method */
Nf = faceforward(normalize(N), I);
V = -normalize(I);
surface_color = surface_opac * (surface_color * (Ka * ambient() + Kd * diffuse(Nf)) +
specular_color * Ks * specular(Nf, V, roughness));
Oi = surface_opac;
Ci = surface_color;
}
Copyright © 1997 by
WebNation
All trademarks are the property of their respective holders