/* weave.sl - weave shader by Everett Staley */
surface
weave (float Ka=1,Kd=.3,rough=.5,skale=2.5,soft=.01;)

{
    color Cc,specolor=0,dark=0,lite=1,vert=color(1,.6,.2)*(.5+noise(s*60)),horiz=color(.7,.4,.2)*(.5+noise(t*60));
	float ss=mod(s*skale,1);
	float tt=mod(t*skale,1);
	float test1=0,test2=0;

	if ((.15.5) {s3-=.5;test1=1;}
	float t3=mod(tt,1);if (t3>.5) {t3-=.5;test2=1;}

	if (test1==test2)
		{	
	if (.15-soft.35+soft))
	   {Os=mix(lite,dark,(1/soft)*(.15-s3));
		if (t3<.15-soft) {Cc=mix(vert,mix(vert,horiz,(1/soft)*(.15-s3)),(1/soft)*((.15-soft)-t3));}
		if (t3>.35+soft) {Cc=mix(vert,mix(vert,horiz,(1/soft)*(.15-s3)),(1/soft)*(t3-(.35+soft)));}}

	if (.35.35+soft))
	   {Os=mix(lite,dark,(1/soft)*(s3-.35));
		if (t3<.15-soft) {Cc=mix(vert,mix(vert,horiz,(1/soft)*(s3-.35)),(1/soft)*((.15-soft)-t3));}
		if (t3>.35+soft) {Cc=mix(vert,mix(vert,horiz,(1/soft)*(s3-.35)),(1/soft)*(t3-(.35+soft)));}}

	if (.15-soft.35+soft))
	   {Os=mix(lite,dark,(1/soft)*(.15-t3));
		if (s3<.15-soft) {Cc=mix(mix(horiz,vert,(1/soft)*(.15-t3)),horiz,(1/soft)*((.15-soft)-s3));}
		if (s3>.35+soft) {Cc=mix(mix(horiz,vert,(1/soft)*(.15-t3)),horiz,(1/soft)*(s3-(.35+soft)));}}

	if (.35.35+soft))
	   {Os=mix(lite,dark,(1/soft)*(t3-.35));
		if (s3<.15-soft) {Cc=mix(mix(horiz,vert,(1/soft)*(t3-.35)),horiz,(1/soft)*((.15-soft)-s3));}
		if (s3>.35+soft) {Cc=mix(mix(horiz,vert,(1/soft)*(t3-.35)),horiz,(1/soft)*(s3-(.35+soft)));}}

		if (.35.35+soft))
	   {Os=mix(lite,dark,(1/soft)*(.15-s3));Cc=mix(vert,horiz,(1/soft)*(.15-s3));}
	if (.35.35+soft))
	   {Os=mix(lite,dark,(1/soft)*(s3-.35));Cc=mix(vert,horiz,(1/soft)*(s3-.35));}
	if (.15-soft.35+soft)) {Os=mix(lite,dark,(1/soft)*(.15-t3));Cc=horiz;}
	if (.35.35+soft)) {Os=mix(lite,dark,(1/soft)*(t3-.35));Cc=horiz;}

		if (.35.35+soft) && (t3<.15-soft || t3>.35+soft)) Os=0;

    normal Nf = faceforward (normalize(N),I);
    Ci=Cc*(Ka*ambient()+Kd*diffuse(Nf))+specolor*specular(Nf,-normalize(I),rough);
    Oi=Os;Ci*=Oi;
}