2 float exp_blender(float f)
4 return pow(2.71828182846, f);
7 void rgb_to_hsv(vec4 rgb, out vec4 outcol)
9 float cmax, cmin, h, s, v, cdelta;
12 cmax = max(rgb[0], max(rgb[1], rgb[2]));
13 cmin = min(rgb[0], min(rgb[1], rgb[2]));
28 c = (vec3(cmax, cmax, cmax) - rgb.xyz)/cdelta;
30 if (rgb.x==cmax) h = c[2] - c[1];
31 else if (rgb.y==cmax) h = 2.0 + c[0] - c[2];
32 else h = 4.0 + c[1] - c[0];
40 outcol = vec4(h, s, v, rgb.w);
43 void hsv_to_rgb(vec4 hsv, out vec4 outcol)
45 float i, f, p, q, t, h, s, v;
65 t = v*(1.0-(s*(1.0-f)));
67 if (i == 0.0) rgb = vec3(v, t, p);
68 else if (i == 1.0) rgb = vec3(q, v, p);
69 else if (i == 2.0) rgb = vec3(p, v, t);
70 else if (i == 3.0) rgb = vec3(p, q, v);
71 else if (i == 4.0) rgb = vec3(t, p, v);
72 else rgb = vec3(v, p, q);
75 outcol = vec4(rgb, hsv.w);
78 float srgb_to_linearrgb(float c)
81 return (c < 0.0)? 0.0: c * (1.0/12.92);
83 return pow((c + 0.055)*(1.0/1.055), 2.4);
86 float linearrgb_to_srgb(float c)
89 return (c < 0.0)? 0.0: c * 12.92;
91 return 1.055 * pow(c, 1.0/2.4) - 0.055;
94 void srgb_to_linearrgb(vec4 col_from, out vec4 col_to)
96 col_to.r = srgb_to_linearrgb(col_from.r);
97 col_to.g = srgb_to_linearrgb(col_from.g);
98 col_to.b = srgb_to_linearrgb(col_from.b);
99 col_to.a = col_from.a;
102 void linearrgb_to_srgb(vec4 col_from, out vec4 col_to)
104 col_to.r = linearrgb_to_srgb(col_from.r);
105 col_to.g = linearrgb_to_srgb(col_from.g);
106 col_to.b = linearrgb_to_srgb(col_from.b);
107 col_to.a = col_from.a;
110 #define M_PI 3.14159265358979323846
111 #define M_1_PI 0.31830988618379069
113 /*********** SHADER NODES ***************/
115 void vcol_attribute(vec4 attvcol, out vec4 vcol)
117 vcol = vec4(attvcol.x/255.0, attvcol.y/255.0, attvcol.z/255.0, 1.0);
120 void uv_attribute(vec2 attuv, out vec3 uv)
122 uv = vec3(attuv*2.0 - vec2(1.0, 1.0), 0.0);
125 void geom(vec3 co, vec3 nor, mat4 viewinvmat, vec3 attorco, vec2 attuv, vec4 attvcol, out vec3 global, out vec3 local, out vec3 view, out vec3 orco, out vec3 uv, out vec3 normal, out vec4 vcol, out float vcol_alpha, out float frontback)
128 view = normalize(local);
129 global = (viewinvmat*vec4(local, 1.0)).xyz;
131 uv_attribute(attuv, uv);
132 normal = -normalize(nor); /* blender render normal is negated */
133 vcol_attribute(attvcol, vcol);
134 vcol_alpha = attvcol.a;
138 void mapping(vec3 vec, mat4 mat, vec3 minvec, vec3 maxvec, float domin, float domax, out vec3 outvec)
140 outvec = (mat * vec4(vec, 1.0)).xyz;
142 outvec = max(outvec, minvec);
144 outvec = min(outvec, maxvec);
147 void camera(vec3 co, out vec3 outview, out float outdepth, out float outdist)
149 outdepth = abs(co.z);
150 outdist = length(co);
151 outview = normalize(co);
154 void math_add(float val1, float val2, out float outval)
156 outval = val1 + val2;
159 void math_subtract(float val1, float val2, out float outval)
161 outval = val1 - val2;
164 void math_multiply(float val1, float val2, out float outval)
166 outval = val1 * val2;
169 void math_divide(float val1, float val2, out float outval)
174 outval = val1 / val2;
177 void math_sine(float val, out float outval)
182 void math_cosine(float val, out float outval)
187 void math_tangent(float val, out float outval)
192 void math_asin(float val, out float outval)
194 if (val <= 1.0 && val >= -1.0)
200 void math_acos(float val, out float outval)
202 if (val <= 1.0 && val >= -1.0)
208 void math_atan(float val, out float outval)
213 void math_pow(float val1, float val2, out float outval)
216 outval = pow(val1, val2);
221 void math_log(float val1, float val2, out float outval)
223 if(val1 > 0.0 && val2 > 0.0)
224 outval= log2(val1) / log2(val2);
229 void math_max(float val1, float val2, out float outval)
231 outval = max(val1, val2);
234 void math_min(float val1, float val2, out float outval)
236 outval = min(val1, val2);
239 void math_round(float val, out float outval)
241 outval= floor(val + 0.5);
244 void math_less_than(float val1, float val2, out float outval)
252 void math_greater_than(float val1, float val2, out float outval)
260 void squeeze(float val, float width, float center, out float outval)
262 outval = 1.0/(1.0 + pow(2.71828183, -((val-center)*width)));
265 void vec_math_add(vec3 v1, vec3 v2, out vec3 outvec, out float outval)
268 outval = (abs(outvec[0]) + abs(outvec[1]) + abs(outvec[2]))/3.0;
271 void vec_math_sub(vec3 v1, vec3 v2, out vec3 outvec, out float outval)
274 outval = (abs(outvec[0]) + abs(outvec[1]) + abs(outvec[2]))/3.0;
277 void vec_math_average(vec3 v1, vec3 v2, out vec3 outvec, out float outval)
280 outval = length(outvec);
281 outvec = normalize(outvec);
284 void vec_math_dot(vec3 v1, vec3 v2, out vec3 outvec, out float outval)
286 outvec = vec3(0, 0, 0);
287 outval = dot(v1, v2);
290 void vec_math_cross(vec3 v1, vec3 v2, out vec3 outvec, out float outval)
292 outvec = cross(v1, v2);
293 outval = length(outvec);
296 void vec_math_normalize(vec3 v, out vec3 outvec, out float outval)
299 outvec = normalize(v);
302 void vec_math_negate(vec3 v, out vec3 outv)
307 void normal(vec3 dir, vec3 nor, out vec3 outnor, out float outdot)
310 outdot = -dot(dir, nor);
313 void curves_vec(float fac, vec3 vec, sampler2D curvemap, out vec3 outvec)
315 outvec.x = texture2D(curvemap, vec2((vec.x + 1.0)*0.5, 0.0)).x;
316 outvec.y = texture2D(curvemap, vec2((vec.y + 1.0)*0.5, 0.0)).y;
317 outvec.z = texture2D(curvemap, vec2((vec.z + 1.0)*0.5, 0.0)).z;
320 outvec = (outvec*fac) + (vec*(1.0-fac));
324 void curves_rgb(float fac, vec4 col, sampler2D curvemap, out vec4 outcol)
326 outcol.r = texture2D(curvemap, vec2(texture2D(curvemap, vec2(col.r, 0.0)).a, 0.0)).r;
327 outcol.g = texture2D(curvemap, vec2(texture2D(curvemap, vec2(col.g, 0.0)).a, 0.0)).g;
328 outcol.b = texture2D(curvemap, vec2(texture2D(curvemap, vec2(col.b, 0.0)).a, 0.0)).b;
331 outcol = (outcol*fac) + (col*(1.0-fac));
336 void set_value(float val, out float outval)
341 void set_rgb(vec3 col, out vec3 outcol)
346 void set_rgba(vec4 col, out vec4 outcol)
351 void set_value_zero(out float outval)
356 void set_value_one(out float outval)
361 void set_rgb_zero(out vec3 outval)
366 void set_rgba_zero(out vec4 outval)
371 void copy_raw(vec4 val, out vec4 outval)
376 void copy_raw(vec3 val, out vec3 outval)
381 void copy_raw(vec2 val, out vec2 outval)
386 void copy_raw(float val, out float outval)
391 void mix_blend(float fac, vec4 col1, vec4 col2, out vec4 outcol)
393 fac = clamp(fac, 0.0, 1.0);
394 outcol = mix(col1, col2, fac);
398 void mix_add(float fac, vec4 col1, vec4 col2, out vec4 outcol)
400 fac = clamp(fac, 0.0, 1.0);
401 outcol = mix(col1, col1 + col2, fac);
405 void mix_mult(float fac, vec4 col1, vec4 col2, out vec4 outcol)
407 fac = clamp(fac, 0.0, 1.0);
408 outcol = mix(col1, col1 * col2, fac);
412 void mix_screen(float fac, vec4 col1, vec4 col2, out vec4 outcol)
414 fac = clamp(fac, 0.0, 1.0);
415 float facm = 1.0 - fac;
417 outcol = vec4(1.0) - (vec4(facm) + fac*(vec4(1.0) - col2))*(vec4(1.0) - col1);
421 void mix_overlay(float fac, vec4 col1, vec4 col2, out vec4 outcol)
423 fac = clamp(fac, 0.0, 1.0);
424 float facm = 1.0 - fac;
429 outcol.r *= facm + 2.0*fac*col2.r;
431 outcol.r = 1.0 - (facm + 2.0*fac*(1.0 - col2.r))*(1.0 - outcol.r);
434 outcol.g *= facm + 2.0*fac*col2.g;
436 outcol.g = 1.0 - (facm + 2.0*fac*(1.0 - col2.g))*(1.0 - outcol.g);
439 outcol.b *= facm + 2.0*fac*col2.b;
441 outcol.b = 1.0 - (facm + 2.0*fac*(1.0 - col2.b))*(1.0 - outcol.b);
444 void mix_sub(float fac, vec4 col1, vec4 col2, out vec4 outcol)
446 fac = clamp(fac, 0.0, 1.0);
447 outcol = mix(col1, col1 - col2, fac);
451 void mix_div(float fac, vec4 col1, vec4 col2, out vec4 outcol)
453 fac = clamp(fac, 0.0, 1.0);
454 float facm = 1.0 - fac;
458 if(col2.r != 0.0) outcol.r = facm*outcol.r + fac*outcol.r/col2.r;
459 if(col2.g != 0.0) outcol.g = facm*outcol.g + fac*outcol.g/col2.g;
460 if(col2.b != 0.0) outcol.b = facm*outcol.b + fac*outcol.b/col2.b;
463 void mix_diff(float fac, vec4 col1, vec4 col2, out vec4 outcol)
465 fac = clamp(fac, 0.0, 1.0);
466 outcol = mix(col1, abs(col1 - col2), fac);
470 void mix_dark(float fac, vec4 col1, vec4 col2, out vec4 outcol)
472 fac = clamp(fac, 0.0, 1.0);
473 outcol.rgb = min(col1.rgb, col2.rgb*fac);
477 void mix_light(float fac, vec4 col1, vec4 col2, out vec4 outcol)
479 fac = clamp(fac, 0.0, 1.0);
480 outcol.rgb = max(col1.rgb, col2.rgb*fac);
484 void mix_dodge(float fac, vec4 col1, vec4 col2, out vec4 outcol)
486 fac = clamp(fac, 0.0, 1.0);
489 if(outcol.r != 0.0) {
490 float tmp = 1.0 - fac*col2.r;
493 else if((tmp = outcol.r/tmp) > 1.0)
498 if(outcol.g != 0.0) {
499 float tmp = 1.0 - fac*col2.g;
502 else if((tmp = outcol.g/tmp) > 1.0)
507 if(outcol.b != 0.0) {
508 float tmp = 1.0 - fac*col2.b;
511 else if((tmp = outcol.b/tmp) > 1.0)
518 void mix_burn(float fac, vec4 col1, vec4 col2, out vec4 outcol)
520 fac = clamp(fac, 0.0, 1.0);
521 float tmp, facm = 1.0 - fac;
525 tmp = facm + fac*col2.r;
528 else if((tmp = (1.0 - (1.0 - outcol.r)/tmp)) < 0.0)
535 tmp = facm + fac*col2.g;
538 else if((tmp = (1.0 - (1.0 - outcol.g)/tmp)) < 0.0)
545 tmp = facm + fac*col2.b;
548 else if((tmp = (1.0 - (1.0 - outcol.b)/tmp)) < 0.0)
556 void mix_hue(float fac, vec4 col1, vec4 col2, out vec4 outcol)
558 fac = clamp(fac, 0.0, 1.0);
559 float facm = 1.0 - fac;
564 rgb_to_hsv(col2, hsv2);
567 rgb_to_hsv(outcol, hsv);
569 hsv_to_rgb(hsv, tmp);
571 outcol = mix(outcol, tmp, fac);
576 void mix_sat(float fac, vec4 col1, vec4 col2, out vec4 outcol)
578 fac = clamp(fac, 0.0, 1.0);
579 float facm = 1.0 - fac;
584 rgb_to_hsv(outcol, hsv);
587 rgb_to_hsv(col2, hsv2);
589 hsv.y = facm*hsv.y + fac*hsv2.y;
590 hsv_to_rgb(hsv, outcol);
594 void mix_val(float fac, vec4 col1, vec4 col2, out vec4 outcol)
596 fac = clamp(fac, 0.0, 1.0);
597 float facm = 1.0 - fac;
600 rgb_to_hsv(col1, hsv);
601 rgb_to_hsv(col2, hsv2);
603 hsv.z = facm*hsv.z + fac*hsv2.z;
604 hsv_to_rgb(hsv, outcol);
607 void mix_color(float fac, vec4 col1, vec4 col2, out vec4 outcol)
609 fac = clamp(fac, 0.0, 1.0);
610 float facm = 1.0 - fac;
615 rgb_to_hsv(col2, hsv2);
618 rgb_to_hsv(outcol, hsv);
621 hsv_to_rgb(hsv, tmp);
623 outcol = mix(outcol, tmp, fac);
628 void mix_soft(float fac, vec4 col1, vec4 col2, out vec4 outcol)
630 fac = clamp(fac, 0.0, 1.0);
631 float facm = 1.0 - fac;
634 vec4 scr= one - (one - col2)*(one - col1);
635 outcol = facm*col1 + fac*((one - col1)*col2*col1 + col1*scr);
638 void mix_linear(float fac, vec4 col1, vec4 col2, out vec4 outcol)
640 fac = clamp(fac, 0.0, 1.0);
645 outcol.r= col1.r + fac*(2.0*(col2.r - 0.5));
647 outcol.r= col1.r + fac*(2.0*(col2.r) - 1.0);
650 outcol.g= col1.g + fac*(2.0*(col2.g - 0.5));
652 outcol.g= col1.g + fac*(2.0*(col2.g) - 1.0);
655 outcol.b= col1.b + fac*(2.0*(col2.b - 0.5));
657 outcol.b= col1.b + fac*(2.0*(col2.b) - 1.0);
660 void valtorgb(float fac, sampler2D colormap, out vec4 outcol, out float outalpha)
662 outcol = texture2D(colormap, vec2(fac, 0.0));
666 void rgbtobw(vec4 color, out float outval)
668 outval = color.r*0.35 + color.g*0.45 + color.b*0.2; /* keep these factors in sync with texture.h:RGBTOBW */
671 void invert(float fac, vec4 col, out vec4 outcol)
673 outcol.xyz = mix(col.xyz, vec3(1.0, 1.0, 1.0) - col.xyz, fac);
677 void hue_sat(float hue, float sat, float value, float fac, vec4 col, out vec4 outcol)
681 rgb_to_hsv(col, hsv);
683 hsv[0] += (hue - 0.5);
684 if(hsv[0]>1.0) hsv[0]-=1.0; else if(hsv[0]<0.0) hsv[0]+= 1.0;
686 if(hsv[1]>1.0) hsv[1]= 1.0; else if(hsv[1]<0.0) hsv[1]= 0.0;
688 if(hsv[2]>1.0) hsv[2]= 1.0; else if(hsv[2]<0.0) hsv[2]= 0.0;
690 hsv_to_rgb(hsv, outcol);
692 outcol = mix(col, outcol, fac);
695 void separate_rgb(vec4 col, out float r, out float g, out float b)
702 void combine_rgb(float r, float g, float b, out vec4 col)
704 col = vec4(r, g, b, 1.0);
707 void output_node(vec4 rgb, float alpha, out vec4 outrgb)
709 outrgb = vec4(rgb.rgb, alpha);
712 /*********** TEXTURES ***************/
714 void texture_flip_blend(vec3 vec, out vec3 outvec)
719 void texture_blend_lin(vec3 vec, out float outval)
721 outval = (1.0+vec.x)/2.0;
724 void texture_blend_quad(vec3 vec, out float outval)
726 outval = max((1.0+vec.x)/2.0, 0.0);
730 void texture_wood_sin(vec3 vec, out float value, out vec4 color, out vec3 normal)
732 float a = sqrt(vec.x*vec.x + vec.y*vec.y + vec.z*vec.z)*20.0;
733 float wi = 0.5 + 0.5*sin(a);
736 color = vec4(wi, wi, wi, 1.0);
737 normal = vec3(0.0, 0.0, 0.0);
740 void texture_image(vec3 vec, sampler2D ima, out float value, out vec4 color, out vec3 normal)
742 color = texture2D(ima, (vec.xy + vec2(1.0, 1.0))*0.5);
745 normal.x = 2.0*(color.r - 0.5);
746 normal.y = 2.0*(0.5 - color.g);
747 normal.z = 2.0*(color.b - 0.5);
750 /************* MTEX *****************/
752 void texco_orco(vec3 attorco, out vec3 orco)
757 void texco_uv(vec2 attuv, out vec3 uv)
759 /* disabled for now, works together with leaving out mtex_2d_mapping
760 uv = vec3(attuv*2.0 - vec2(1.0, 1.0), 0.0); */
761 uv = vec3(attuv, 0.0);
764 void texco_norm(vec3 normal, out vec3 outnormal)
766 /* corresponds to shi->orn, which is negated so cancels
767 out blender normal negation */
768 outnormal = normalize(normal);
771 void texco_tangent(vec4 tangent, out vec3 outtangent)
773 outtangent = normalize(tangent.xyz);
776 void texco_global(mat4 viewinvmat, vec3 co, out vec3 global)
778 global = (viewinvmat*vec4(co, 1.0)).xyz;
781 void texco_object(mat4 viewinvmat, mat4 obinvmat, vec3 co, out vec3 object)
783 object = (obinvmat*(viewinvmat*vec4(co, 1.0))).xyz;
786 void texco_refl(vec3 vn, vec3 view, out vec3 ref)
788 ref = view - 2.0*dot(vn, view)*vn;
791 void shade_norm(vec3 normal, out vec3 outnormal)
793 /* blender render normal is negated */
794 outnormal = -normalize(normal);
797 void mtex_rgb_blend(vec3 outcol, vec3 texcol, float fact, float facg, out vec3 incol)
804 incol = fact*texcol + facm*outcol;
807 void mtex_rgb_mul(vec3 outcol, vec3 texcol, float fact, float facg, out vec3 incol)
814 incol = (facm + fact*texcol)*outcol;
817 void mtex_rgb_screen(vec3 outcol, vec3 texcol, float fact, float facg, out vec3 incol)
824 incol = vec3(1.0) - (vec3(facm) + fact*(vec3(1.0) - texcol))*(vec3(1.0) - outcol);
827 void mtex_rgb_overlay(vec3 outcol, vec3 texcol, float fact, float facg, out vec3 incol)
835 incol.r = outcol.r*(facm + 2.0*fact*texcol.r);
837 incol.r = 1.0 - (facm + 2.0*fact*(1.0 - texcol.r))*(1.0 - outcol.r);
840 incol.g = outcol.g*(facm + 2.0*fact*texcol.g);
842 incol.g = 1.0 - (facm + 2.0*fact*(1.0 - texcol.g))*(1.0 - outcol.g);
845 incol.b = outcol.b*(facm + 2.0*fact*texcol.b);
847 incol.b = 1.0 - (facm + 2.0*fact*(1.0 - texcol.b))*(1.0 - outcol.b);
850 void mtex_rgb_sub(vec3 outcol, vec3 texcol, float fact, float facg, out vec3 incol)
852 incol = -fact*facg*texcol + outcol;
855 void mtex_rgb_add(vec3 outcol, vec3 texcol, float fact, float facg, out vec3 incol)
857 incol = fact*facg*texcol + outcol;
860 void mtex_rgb_div(vec3 outcol, vec3 texcol, float fact, float facg, out vec3 incol)
867 if(texcol.r != 0.0) incol.r = facm*outcol.r + fact*outcol.r/texcol.r;
868 if(texcol.g != 0.0) incol.g = facm*outcol.g + fact*outcol.g/texcol.g;
869 if(texcol.b != 0.0) incol.b = facm*outcol.b + fact*outcol.b/texcol.b;
872 void mtex_rgb_diff(vec3 outcol, vec3 texcol, float fact, float facg, out vec3 incol)
879 incol = facm*outcol + fact*abs(texcol - outcol);
882 void mtex_rgb_dark(vec3 outcol, vec3 texcol, float fact, float facg, out vec3 incol)
890 if(col < outcol.r) incol.r = col; else incol.r = outcol.r;
892 if(col < outcol.g) incol.g = col; else incol.g = outcol.g;
894 if(col < outcol.b) incol.b = col; else incol.b = outcol.b;
897 void mtex_rgb_light(vec3 outcol, vec3 texcol, float fact, float facg, out vec3 incol)
905 if(col > outcol.r) incol.r = col; else incol.r = outcol.r;
907 if(col > outcol.g) incol.g = col; else incol.g = outcol.g;
909 if(col > outcol.b) incol.b = col; else incol.b = outcol.b;
912 void mtex_rgb_hue(vec3 outcol, vec3 texcol, float fact, float facg, out vec3 incol)
916 mix_hue(fact*facg, vec4(outcol, 1.0), vec4(texcol, 1.0), col);
920 void mtex_rgb_sat(vec3 outcol, vec3 texcol, float fact, float facg, out vec3 incol)
924 mix_sat(fact*facg, vec4(outcol, 1.0), vec4(texcol, 1.0), col);
928 void mtex_rgb_val(vec3 outcol, vec3 texcol, float fact, float facg, out vec3 incol)
932 mix_val(fact*facg, vec4(outcol, 1.0), vec4(texcol, 1.0), col);
936 void mtex_rgb_color(vec3 outcol, vec3 texcol, float fact, float facg, out vec3 incol)
940 mix_color(fact*facg, vec4(outcol, 1.0), vec4(texcol, 1.0), col);
944 void mtex_value_vars(inout float fact, float facg, out float facm)
956 void mtex_value_blend(float outcol, float texcol, float fact, float facg, out float incol)
959 mtex_value_vars(fact, facg, facm);
961 incol = fact*texcol + facm*outcol;
964 void mtex_value_mul(float outcol, float texcol, float fact, float facg, out float incol)
967 mtex_value_vars(fact, facg, facm);
970 incol = (facm + fact*texcol)*outcol;
973 void mtex_value_screen(float outcol, float texcol, float fact, float facg, out float incol)
976 mtex_value_vars(fact, facg, facm);
979 incol = 1.0 - (facm + fact*(1.0 - texcol))*(1.0 - outcol);
982 void mtex_value_sub(float outcol, float texcol, float fact, float facg, out float incol)
985 mtex_value_vars(fact, facg, facm);
988 incol = fact*texcol + outcol;
991 void mtex_value_add(float outcol, float texcol, float fact, float facg, out float incol)
994 mtex_value_vars(fact, facg, facm);
997 incol = fact*texcol + outcol;
1000 void mtex_value_div(float outcol, float texcol, float fact, float facg, out float incol)
1003 mtex_value_vars(fact, facg, facm);
1006 incol = facm*outcol + fact*outcol/texcol;
1011 void mtex_value_diff(float outcol, float texcol, float fact, float facg, out float incol)
1014 mtex_value_vars(fact, facg, facm);
1016 incol = facm*outcol + fact*abs(texcol - outcol);
1019 void mtex_value_dark(float outcol, float texcol, float fact, float facg, out float incol)
1022 mtex_value_vars(fact, facg, facm);
1024 float col = fact*texcol;
1025 if(col < outcol) incol = col; else incol = outcol;
1028 void mtex_value_light(float outcol, float texcol, float fact, float facg, out float incol)
1031 mtex_value_vars(fact, facg, facm);
1033 float col = fact*texcol;
1034 if(col > outcol) incol = col; else incol = outcol;
1037 void mtex_value_clamp_positive(float fac, out float outfac)
1039 outfac = max(fac, 0.0);
1042 void mtex_value_clamp(float fac, out float outfac)
1044 outfac = clamp(fac, 0.0, 1.0);
1047 void mtex_har_divide(float har, out float outhar)
1052 void mtex_har_multiply_clamp(float har, out float outhar)
1056 if(har < 1.0) outhar = 1.0;
1057 else if(har > 511.0) outhar = 511.0;
1061 void mtex_alpha_from_col(vec4 col, out float alpha)
1066 void mtex_alpha_to_col(vec4 col, float alpha, out vec4 outcol)
1068 outcol = vec4(col.rgb, alpha);
1071 void mtex_rgbtoint(vec4 rgb, out float intensity)
1073 intensity = dot(vec3(0.35, 0.45, 0.2), rgb.rgb);
1076 void mtex_value_invert(float invalue, out float outvalue)
1078 outvalue = 1.0 - invalue;
1081 void mtex_rgb_invert(vec4 inrgb, out vec4 outrgb)
1083 outrgb = vec4(vec3(1.0) - inrgb.rgb, inrgb.a);
1086 void mtex_value_stencil(float stencil, float intensity, out float outstencil, out float outintensity)
1088 float fact = intensity;
1089 outintensity = intensity*stencil;
1090 outstencil = stencil*fact;
1093 void mtex_rgb_stencil(float stencil, vec4 rgb, out float outstencil, out vec4 outrgb)
1096 outrgb = vec4(rgb.rgb, rgb.a*stencil);
1097 outstencil = stencil*fact;
1100 void mtex_mapping_ofs(vec3 texco, vec3 ofs, out vec3 outtexco)
1102 outtexco = texco + ofs;
1105 void mtex_mapping_size(vec3 texco, vec3 size, out vec3 outtexco)
1107 outtexco = size*texco;
1110 void mtex_2d_mapping(vec3 vec, out vec3 outvec)
1112 outvec = vec3(vec.xy*0.5 + vec2(0.5, 0.5), vec.z);
1115 void mtex_image(vec3 texco, sampler2D ima, out float value, out vec4 color)
1117 color = texture2D(ima, texco.xy);
1121 void mtex_normal(vec3 texco, sampler2D ima, out vec3 normal)
1123 // The invert of the red channel is to make
1124 // the normal map compliant with the outside world.
1125 // It needs to be done because in Blender
1126 // the normal used points inward.
1127 // Should this ever change this negate must be removed.
1128 vec4 color = texture2D(ima, texco.xy);
1129 normal = 2.0*(vec3(-color.r, color.g, color.b) - vec3(-0.5, 0.5, 0.5));
1132 void mtex_bump_normals_init( vec3 vN, out vec3 vNorg, out vec3 vNacc, out float fPrevMagnitude )
1136 fPrevMagnitude = 1.0;
1139 /** helper method to extract the upper left 3x3 matrix from a 4x4 matrix */
1140 mat3 to_mat3(mat4 m4)
1149 void mtex_bump_init_objspace( vec3 surf_pos, vec3 surf_norm,
1150 mat4 mView, mat4 mViewInv, mat4 mObj, mat4 mObjInv,
1151 float fPrevMagnitude_in, vec3 vNacc_in,
1152 out float fPrevMagnitude_out, out vec3 vNacc_out,
1153 out vec3 vR1, out vec3 vR2, out float fDet )
1155 mat3 obj2view = to_mat3(gl_ModelViewMatrix);
1156 mat3 view2obj = to_mat3(gl_ModelViewMatrixInverse);
1158 vec3 vSigmaS = view2obj * dFdx( surf_pos );
1159 vec3 vSigmaT = view2obj * dFdy( surf_pos );
1160 vec3 vN = normalize( surf_norm * obj2view );
1162 vR1 = cross( vSigmaT, vN );
1163 vR2 = cross( vN, vSigmaS ) ;
1164 fDet = dot ( vSigmaS, vR1 );
1166 /* pretransform vNacc (in mtex_bump_apply) using the inverse transposed */
1167 vR1 = vR1 * view2obj;
1168 vR2 = vR2 * view2obj;
1171 float fMagnitude = abs(fDet) * length(vN);
1172 vNacc_out = vNacc_in * (fMagnitude / fPrevMagnitude_in);
1173 fPrevMagnitude_out = fMagnitude;
1176 void mtex_bump_init_texturespace( vec3 surf_pos, vec3 surf_norm,
1177 float fPrevMagnitude_in, vec3 vNacc_in,
1178 out float fPrevMagnitude_out, out vec3 vNacc_out,
1179 out vec3 vR1, out vec3 vR2, out float fDet )
1181 vec3 vSigmaS = dFdx( surf_pos );
1182 vec3 vSigmaT = dFdy( surf_pos );
1183 vec3 vN = surf_norm; /* normalized interpolated vertex normal */
1185 vR1 = normalize( cross( vSigmaT, vN ) );
1186 vR2 = normalize( cross( vN, vSigmaS ) );
1187 fDet = sign( dot(vSigmaS, vR1) );
1189 float fMagnitude = abs(fDet);
1190 vNacc_out = vNacc_in * (fMagnitude / fPrevMagnitude_in);
1191 fPrevMagnitude_out = fMagnitude;
1194 void mtex_bump_init_viewspace( vec3 surf_pos, vec3 surf_norm,
1195 float fPrevMagnitude_in, vec3 vNacc_in,
1196 out float fPrevMagnitude_out, out vec3 vNacc_out,
1197 out vec3 vR1, out vec3 vR2, out float fDet )
1199 vec3 vSigmaS = dFdx( surf_pos );
1200 vec3 vSigmaT = dFdy( surf_pos );
1201 vec3 vN = surf_norm; /* normalized interpolated vertex normal */
1203 vR1 = cross( vSigmaT, vN );
1204 vR2 = cross( vN, vSigmaS ) ;
1205 fDet = dot ( vSigmaS, vR1 );
1207 float fMagnitude = abs(fDet);
1208 vNacc_out = vNacc_in * (fMagnitude / fPrevMagnitude_in);
1209 fPrevMagnitude_out = fMagnitude;
1212 void mtex_bump_tap3( vec3 texco, sampler2D ima, float hScale,
1213 out float dBs, out float dBt )
1215 vec2 STll = texco.xy;
1216 vec2 STlr = texco.xy + dFdx(texco.xy) ;
1217 vec2 STul = texco.xy + dFdy(texco.xy) ;
1220 rgbtobw( texture2D(ima, STll), Hll );
1221 rgbtobw( texture2D(ima, STlr), Hlr );
1222 rgbtobw( texture2D(ima, STul), Hul );
1224 dBs = hScale * (Hlr - Hll);
1225 dBt = hScale * (Hul - Hll);
1230 void mtex_bump_bicubic( vec3 texco, sampler2D ima, float hScale,
1231 out float dBs, out float dBt )
1233 vec2 TexDx = dFdx(texco.xy);
1234 vec2 TexDy = dFdy(texco.xy);
1236 vec2 STl = texco.xy - 0.5 * TexDx ;
1237 vec2 STr = texco.xy + 0.5 * TexDx ;
1238 vec2 STd = texco.xy - 0.5 * TexDy ;
1239 vec2 STu = texco.xy + 0.5 * TexDy ;
1241 float Hl = texture2D(ima, STl).x;
1242 float Hr = texture2D(ima, STr).x;
1243 float Hd = texture2D(ima, STd).x;
1244 float Hu = texture2D(ima, STu).x;
1246 vec2 dHdxy = vec2(Hr - Hl, Hu - Hd);
1247 float fBlend = clamp(1.0-textureQueryLOD(ima, texco.xy).x, 0.0, 1.0);
1250 // the derivative of the bicubic sampling of level 0
1252 vDim = textureSize(ima, 0);
1254 vec2 fTexLoc = vDim*texco.xy-vec2(0.5,0.5);
1255 ivec2 iTexLoc = ivec2(floor(fTexLoc));
1256 vec2 t = clamp(fTexLoc - iTexLoc, 0.0, 1.0); // sat just to be pedantic
1258 ivec2 iTexLocMod = iTexLoc + ivec2(-1, -1);
1260 /*******************************************************************************************
1261 * This block will replace the one below when one channel textures are properly supported. *
1262 *******************************************************************************************
1263 vec4 vSamplesUL = textureGather(ima, (iTexLoc+ivec2(-1,-1) + vec2(0.5,0.5))/vDim );
1264 vec4 vSamplesUR = textureGather(ima, (iTexLoc+ivec2(1,-1) + vec2(0.5,0.5))/vDim );
1265 vec4 vSamplesLL = textureGather(ima, (iTexLoc+ivec2(-1,1) + vec2(0.5,0.5))/vDim );
1266 vec4 vSamplesLR = textureGather(ima, (iTexLoc+ivec2(1,1) + vec2(0.5,0.5))/vDim );
1268 mat4 H = mat4(vSamplesUL.w, vSamplesUL.x, vSamplesLL.w, vSamplesLL.x,
1269 vSamplesUL.z, vSamplesUL.y, vSamplesLL.z, vSamplesLL.y,
1270 vSamplesUR.w, vSamplesUR.x, vSamplesLR.w, vSamplesLR.x,
1271 vSamplesUR.z, vSamplesUR.y, vSamplesLR.z, vSamplesLR.y);
1275 for(int i = 0; i < 4; i++){
1276 for(int j = 0; j < 4; j++){
1277 mtex_rgbtoint(texelFetch(ima, (iTexLocMod + ivec2(i,j)), 0), H[i][j]);
1281 float x = t.x, y = t.y;
1282 float x2 = x * x, x3 = x2 * x, y2 = y * y, y3 = y2 * y;
1284 vec4 X = vec4(-0.5*(x3+x)+x2, 1.5*x3-2.5*x2+1, -1.5*x3+2*x2+0.5*x, 0.5*(x3-x2));
1285 vec4 Y = vec4(-0.5*(y3+y)+y2, 1.5*y3-2.5*y2+1, -1.5*y3+2*y2+0.5*y, 0.5*(y3-y2));
1286 vec4 dX = vec4(-1.5*x2+2*x-0.5, 4.5*x2-5*x, -4.5*x2+4*x+0.5, 1.5*x2-x);
1287 vec4 dY = vec4(-1.5*y2+2*y-0.5, 4.5*y2-5*y, -4.5*y2+4*y+0.5, 1.5*y2-y);
1289 // complete derivative in normalized coordinates (mul by vDim)
1290 vec2 dHdST = vDim * vec2(dot(Y, H * dX), dot(dY, H * X));
1292 // transform derivative to screen-space
1293 vec2 dHdxy_bicubic = vec2( dHdST.x * TexDx.x + dHdST.y * TexDx.y,
1294 dHdST.x * TexDy.x + dHdST.y * TexDy.y );
1296 // blend between the two
1297 dHdxy = dHdxy*(1-fBlend) + dHdxy_bicubic*fBlend;
1300 dBs = hScale * dHdxy.x;
1301 dBt = hScale * dHdxy.y;
1306 void mtex_bump_tap5( vec3 texco, sampler2D ima, float hScale,
1307 out float dBs, out float dBt )
1309 vec2 TexDx = dFdx(texco.xy);
1310 vec2 TexDy = dFdy(texco.xy);
1312 vec2 STc = texco.xy;
1313 vec2 STl = texco.xy - 0.5 * TexDx ;
1314 vec2 STr = texco.xy + 0.5 * TexDx ;
1315 vec2 STd = texco.xy - 0.5 * TexDy ;
1316 vec2 STu = texco.xy + 0.5 * TexDy ;
1318 float Hc,Hl,Hr,Hd,Hu;
1319 rgbtobw( texture2D(ima, STc), Hc );
1320 rgbtobw( texture2D(ima, STl), Hl );
1321 rgbtobw( texture2D(ima, STr), Hr );
1322 rgbtobw( texture2D(ima, STd), Hd );
1323 rgbtobw( texture2D(ima, STu), Hu );
1325 dBs = hScale * (Hr - Hl);
1326 dBt = hScale * (Hu - Hd);
1329 void mtex_bump_deriv( vec3 texco, sampler2D ima, float ima_x, float ima_y, float hScale,
1330 out float dBs, out float dBt )
1332 float s = 1.0; // negate this if flipped texture coordinate
1333 vec2 TexDx = dFdx(texco.xy);
1334 vec2 TexDy = dFdy(texco.xy);
1336 // this variant using a derivative map is described here
1337 // http://mmikkelsen3d.blogspot.com/2011/07/derivative-maps.html
1338 vec2 dim = vec2(ima_x, ima_y);
1339 vec2 dBduv = hScale*dim*(2.0*texture2D(ima, texco.xy).xy-1.0);
1341 dBs = dBduv.x*TexDx.x + s*dBduv.y*TexDx.y;
1342 dBt = dBduv.x*TexDy.x + s*dBduv.y*TexDy.y;
1345 void mtex_bump_apply( float fDet, float dBs, float dBt, vec3 vR1, vec3 vR2, vec3 vNacc_in,
1346 out vec3 vNacc_out, out vec3 perturbed_norm )
1348 vec3 vSurfGrad = sign(fDet) * ( dBs * vR1 + dBt * vR2 );
1350 vNacc_out = vNacc_in - vSurfGrad;
1351 perturbed_norm = normalize( vNacc_out );
1354 void mtex_bump_apply_texspace( float fDet, float dBs, float dBt, vec3 vR1, vec3 vR2,
1355 sampler2D ima, vec3 texco, float ima_x, float ima_y, vec3 vNacc_in,
1356 out vec3 vNacc_out, out vec3 perturbed_norm )
1358 vec2 TexDx = dFdx(texco.xy);
1359 vec2 TexDy = dFdy(texco.xy);
1361 vec3 vSurfGrad = sign(fDet) * (
1362 dBs / length( vec2(ima_x*TexDx.x, ima_y*TexDx.y) ) * vR1 +
1363 dBt / length( vec2(ima_x*TexDy.x, ima_y*TexDy.y) ) * vR2 );
1365 vNacc_out = vNacc_in - vSurfGrad;
1366 perturbed_norm = normalize( vNacc_out );
1369 void mtex_negate_texnormal(vec3 normal, out vec3 outnormal)
1371 outnormal = vec3(-normal.x, -normal.y, normal.z);
1374 void mtex_nspace_tangent(vec4 tangent, vec3 normal, vec3 texnormal, out vec3 outnormal)
1376 vec3 B = tangent.w * cross(normal, tangent.xyz);
1378 outnormal = texnormal.x*tangent.xyz + texnormal.y*B + texnormal.z*normal;
1379 outnormal = normalize(outnormal);
1382 void mtex_blend_normal(float norfac, vec3 normal, vec3 newnormal, out vec3 outnormal)
1384 outnormal = (1.0 - norfac)*normal + norfac*newnormal;
1385 outnormal = normalize(outnormal);
1388 /******* MATERIAL *********/
1390 void lamp_visibility_sun_hemi(vec3 lampvec, out vec3 lv, out float dist, out float visifac)
1397 void lamp_visibility_other(vec3 co, vec3 lampco, out vec3 lv, out float dist, out float visifac)
1405 void lamp_falloff_invlinear(float lampdist, float dist, out float visifac)
1407 visifac = lampdist/(lampdist + dist);
1410 void lamp_falloff_invsquare(float lampdist, float dist, out float visifac)
1412 visifac = lampdist/(lampdist + dist*dist);
1415 void lamp_falloff_sliders(float lampdist, float ld1, float ld2, float dist, out float visifac)
1417 float lampdistkw = lampdist*lampdist;
1419 visifac = lampdist/(lampdist + ld1*dist);
1420 visifac *= lampdistkw/(lampdistkw + ld2*dist*dist);
1423 void lamp_falloff_curve(float lampdist, sampler2D curvemap, float dist, out float visifac)
1425 visifac = texture2D(curvemap, vec2(dist/lampdist, 0.0)).x;
1428 void lamp_visibility_sphere(float lampdist, float dist, float visifac, out float outvisifac)
1430 float t= lampdist - dist;
1432 outvisifac= visifac*max(t, 0.0)/lampdist;
1435 void lamp_visibility_spot_square(vec3 lampvec, mat4 lampimat, vec3 lv, out float inpr)
1437 if(dot(lv, lampvec) > 0.0) {
1438 vec3 lvrot = (lampimat*vec4(lv, 0.0)).xyz;
1439 float x = max(abs(lvrot.x/lvrot.z), abs(lvrot.y/lvrot.z));
1441 inpr = 1.0/sqrt(1.0 + x*x);
1447 void lamp_visibility_spot_circle(vec3 lampvec, vec3 lv, out float inpr)
1449 inpr = dot(lv, lampvec);
1452 void lamp_visibility_spot(float spotsi, float spotbl, float inpr, float visifac, out float outvisifac)
1464 inpr *= smoothstep(0.0, 1.0, t/spotbl);
1466 outvisifac = visifac*inpr;
1470 void lamp_visibility_clamp(float visifac, out float outvisifac)
1472 outvisifac = (visifac < 0.001)? 0.0: visifac;
1475 void shade_view(vec3 co, out vec3 view)
1477 /* handle perspective/orthographic */
1478 view = (gl_ProjectionMatrix[3][3] == 0.0)? normalize(co): vec3(0.0, 0.0, -1.0);
1481 void shade_tangent_v(vec3 lv, vec3 tang, out vec3 vn)
1483 vec3 c = cross(lv, tang);
1484 vec3 vnor = cross(c, tang);
1486 vn = -normalize(vnor);
1489 void shade_inp(vec3 vn, vec3 lv, out float inp)
1494 void shade_is_no_diffuse(out float is)
1499 void shade_is_hemi(float inp, out float is)
1504 float area_lamp_energy(mat4 area, vec3 co, vec3 vn)
1509 vec[0] = normalize(co - area[0].xyz);
1510 vec[1] = normalize(co - area[1].xyz);
1511 vec[2] = normalize(co - area[2].xyz);
1512 vec[3] = normalize(co - area[3].xyz);
1514 c[0] = normalize(cross(vec[0], vec[1]));
1515 c[1] = normalize(cross(vec[1], vec[2]));
1516 c[2] = normalize(cross(vec[2], vec[3]));
1517 c[3] = normalize(cross(vec[3], vec[0]));
1519 rad[0] = acos(dot(vec[0], vec[1]));
1520 rad[1] = acos(dot(vec[1], vec[2]));
1521 rad[2] = acos(dot(vec[2], vec[3]));
1522 rad[3] = acos(dot(vec[3], vec[0]));
1524 fac= rad[0]*dot(vn, c[0]);
1525 fac+= rad[1]*dot(vn, c[1]);
1526 fac+= rad[2]*dot(vn, c[2]);
1527 fac+= rad[3]*dot(vn, c[3]);
1529 return max(fac, 0.0);
1532 void shade_inp_area(vec3 position, vec3 lampco, vec3 lampvec, vec3 vn, mat4 area, float areasize, float k, out float inp)
1535 vec3 vec = co - lampco;
1537 if(dot(vec, lampvec) < 0.0) {
1541 float intens = area_lamp_energy(area, co, vn);
1543 inp = pow(intens*areasize, k);
1547 void shade_diffuse_oren_nayer(float nl, vec3 n, vec3 l, vec3 v, float rough, out float is)
1549 vec3 h = normalize(v + l);
1550 float nh = max(dot(n, h), 0.0);
1551 float nv = max(dot(n, v), 0.0);
1552 float realnl = dot(n, l);
1561 float vh = max(dot(v, h), 0.0);
1562 float Lit_A = acos(realnl);
1563 float View_A = acos(nv);
1565 vec3 Lit_B = normalize(l - realnl*n);
1566 vec3 View_B = normalize(v - nv*n);
1568 float t = max(dot(Lit_B, View_B), 0.0);
1572 if(Lit_A > View_A) {
1581 float A = 1.0 - (0.5*((rough*rough)/((rough*rough) + 0.33)));
1582 float B = 0.45*((rough*rough)/((rough*rough) + 0.09));
1585 is = nl*(A + (B * t * sin(a) * tan(b)));
1589 void shade_diffuse_toon(vec3 n, vec3 l, vec3 v, float size, float tsmooth, out float is)
1591 float rslt = dot(n, l);
1592 float ang = acos(rslt);
1594 if(ang < size) is = 1.0;
1595 else if(ang > (size + tsmooth) || tsmooth == 0.0) is = 0.0;
1596 else is = 1.0 - ((ang - size)/tsmooth);
1599 void shade_diffuse_minnaert(float nl, vec3 n, vec3 v, float darkness, out float is)
1605 float nv = max(dot(n, v), 0.0);
1608 is = nl*pow(max(nv*nl, 0.1), darkness - 1.0);
1610 is = nl*pow(1.0001 - nv, darkness - 1.0);
1614 float fresnel_fac(vec3 view, vec3 vn, float grad, float fac)
1624 if(t1>0.0) t2= 1.0+t1;
1627 t2= grad + (1.0-grad)*pow(t2, fac);
1629 if(t2<0.0) ffac = 0.0;
1630 else if(t2>1.0) ffac = 1.0;
1637 void shade_diffuse_fresnel(vec3 vn, vec3 lv, vec3 view, float fac_i, float fac, out float is)
1639 is = fresnel_fac(lv, vn, fac_i, fac);
1642 void shade_cubic(float is, out float outis)
1644 if(is>0.0 && is<1.0)
1645 outis= smoothstep(0.0, 1.0, is);
1650 void shade_visifac(float i, float visifac, float refl, out float outi)
1653 outi = max(i*visifac*refl, 0.0);
1658 void shade_tangent_v_spec(vec3 tang, out vec3 vn)
1663 void shade_add_to_diffuse(float i, vec3 lampcol, vec3 col, out vec3 outcol)
1666 outcol = i*lampcol*col;
1668 outcol = vec3(0.0, 0.0, 0.0);
1671 void shade_hemi_spec(vec3 vn, vec3 lv, vec3 view, float spec, float hard, float visifac, out float t)
1679 t = visifac*spec*pow(t, hard);
1682 void shade_phong_spec(vec3 n, vec3 l, vec3 v, float hard, out float specfac)
1684 vec3 h = normalize(l + v);
1685 float rslt = max(dot(h, n), 0.0);
1687 specfac = pow(rslt, hard);
1690 void shade_cooktorr_spec(vec3 n, vec3 l, vec3 v, float hard, out float specfac)
1692 vec3 h = normalize(v + l);
1693 float nh = dot(n, h);
1699 float nv = max(dot(n, v), 0.0);
1700 float i = pow(nh, hard);
1707 void shade_blinn_spec(vec3 n, vec3 l, vec3 v, float refrac, float spec_power, out float specfac)
1712 else if(spec_power == 0.0) {
1716 if(spec_power<100.0)
1717 spec_power= sqrt(1.0/spec_power);
1719 spec_power= 10.0/spec_power;
1721 vec3 h = normalize(v + l);
1722 float nh = dot(n, h);
1727 float nv = max(dot(n, v), 0.01);
1728 float nl = dot(n, l);
1733 float vh = max(dot(v, h), 0.01);
1736 float b = (2.0*nh*nv)/vh;
1737 float c = (2.0*nh*nl)/vh;
1741 if(a < b && a < c) g = a;
1742 else if(b < a && b < c) g = b;
1743 else if(c < a && c < b) g = c;
1745 float p = sqrt(((refrac * refrac)+(vh*vh)-1.0));
1746 float f = (((p-vh)*(p-vh))/((p+vh)*(p+vh)))*(1.0+((((vh*(p+vh))-1.0)*((vh*(p+vh))-1.0))/(((vh*(p-vh))+1.0)*((vh*(p-vh))+1.0))));
1747 float ang = acos(nh);
1749 specfac = max(f*g*exp_blender((-(ang*ang)/(2.0*spec_power*spec_power))), 0.0);
1755 void shade_wardiso_spec(vec3 n, vec3 l, vec3 v, float rms, out float specfac)
1757 vec3 h = normalize(l + v);
1758 float nh = max(dot(n, h), 0.001);
1759 float nv = max(dot(n, v), 0.001);
1760 float nl = max(dot(n, l), 0.001);
1761 float angle = tan(acos(nh));
1762 float alpha = max(rms, 0.001);
1764 specfac= nl * (1.0/(4.0*M_PI*alpha*alpha))*(exp_blender(-(angle*angle)/(alpha*alpha))/(sqrt(nv*nl)));
1767 void shade_toon_spec(vec3 n, vec3 l, vec3 v, float size, float tsmooth, out float specfac)
1769 vec3 h = normalize(l + v);
1770 float rslt = dot(h, n);
1771 float ang = acos(rslt);
1773 if(ang < size) rslt = 1.0;
1774 else if(ang >= (size + tsmooth) || tsmooth == 0.0) rslt = 0.0;
1775 else rslt = 1.0 - ((ang - size)/tsmooth);
1780 void shade_spec_area_inp(float specfac, float inp, out float outspecfac)
1782 outspecfac = specfac*inp;
1785 void shade_spec_t(float shadfac, float spec, float visifac, float specfac, out float t)
1787 t = shadfac*spec*visifac*specfac;
1790 void shade_add_spec(float t, vec3 lampcol, vec3 speccol, out vec3 outcol)
1792 outcol = t*lampcol*speccol;
1795 void shade_add(vec4 col1, vec4 col2, out vec4 outcol)
1797 outcol = col1 + col2;
1800 void shade_madd(vec4 col, vec4 col1, vec4 col2, out vec4 outcol)
1802 outcol = col + col1*col2;
1805 void shade_add_clamped(vec4 col1, vec4 col2, out vec4 outcol)
1807 outcol = col1 + max(col2, vec4(0.0, 0.0, 0.0, 0.0));
1810 void shade_madd_clamped(vec4 col, vec4 col1, vec4 col2, out vec4 outcol)
1812 outcol = col + max(col1*col2, vec4(0.0, 0.0, 0.0, 0.0));
1815 void shade_maddf(vec4 col, float f, vec4 col1, out vec4 outcol)
1817 outcol = col + f*col1;
1820 void shade_mul(vec4 col1, vec4 col2, out vec4 outcol)
1825 void shade_mul_value(float fac, vec4 col, out vec4 outcol)
1830 void shade_obcolor(vec4 col, vec4 obcol, out vec4 outcol)
1832 outcol = vec4(col.rgb*obcol.rgb, col.a);
1835 void ramp_rgbtobw(vec3 color, out float outval)
1837 outval = color.r*0.3 + color.g*0.58 + color.b*0.12;
1840 void shade_only_shadow(float i, float shadfac, float energy, out float outshadfac)
1842 outshadfac = i*energy*(1.0 - shadfac);
1845 void shade_only_shadow_diffuse(float shadfac, vec3 rgb, vec4 diff, out vec4 outdiff)
1847 outdiff = diff - vec4(rgb*shadfac, 0.0);
1850 void shade_only_shadow_specular(float shadfac, vec3 specrgb, vec4 spec, out vec4 outspec)
1852 outspec = spec - vec4(specrgb*shadfac, 0.0);
1855 void test_shadowbuf(vec3 rco, sampler2DShadow shadowmap, mat4 shadowpersmat, float shadowbias, float inp, out float result)
1861 vec4 co = shadowpersmat*vec4(rco, 1.0);
1863 //float bias = (1.5 - inp*inp)*shadowbias;
1864 co.z -= shadowbias*co.w;
1866 result = shadow2DProj(shadowmap, co).x;
1870 void shade_exposure_correct(vec3 col, float linfac, float logfac, out vec3 outcol)
1872 outcol = linfac*(1.0 - exp(col*logfac));
1875 void shade_mist_factor(vec3 co, float miststa, float mistdist, float misttype, float misi, out float outfac)
1879 zcor = (gl_ProjectionMatrix[3][3] == 0.0)? length(co): -co[2];
1881 fac = clamp((zcor-miststa)/mistdist, 0.0, 1.0);
1882 if(misttype == 0.0) fac *= fac;
1883 else if(misttype == 1.0);
1884 else fac = sqrt(fac);
1886 outfac = 1.0 - (1.0-fac)*(1.0-misi);
1889 void shade_world_mix(vec3 hor, vec4 col, out vec4 outcol)
1891 float fac = clamp(col.a, 0.0, 1.0);
1892 outcol = vec4(mix(hor, col.rgb, fac), col.a);
1895 void shade_alpha_opaque(vec4 col, out vec4 outcol)
1897 outcol = vec4(col.rgb, 1.0);
1900 void shade_alpha_obcolor(vec4 col, vec4 obcol, out vec4 outcol)
1902 outcol = vec4(col.rgb, col.a*obcol.a);
1905 /*********** NEW SHADER UTILITIES **************/
1907 float fresnel_dielectric(vec3 Incoming, vec3 Normal, float eta)
1909 /* compute fresnel reflectance without explicitly computing
1910 the refracted direction */
1911 float c = abs(dot(Incoming, Normal));
1912 float g = eta * eta - 1.0 + c * c;
1917 float A =(g - c)/(g + c);
1918 float B =(c *(g + c)- 1.0)/(c *(g - c)+ 1.0);
1919 result = 0.5 * A * A *(1.0 + B * B);
1922 result = 1.0; /* TIR (no refracted component) */
1927 float hypot(float x, float y)
1929 return sqrt(x*x + y*y);
1932 /*********** NEW SHADER NODES ***************/
1934 #define NUM_LIGHTS 3
1938 void node_bsdf_diffuse(vec4 color, float roughness, vec3 N, out vec4 result)
1943 /* directional lights */
1944 for(int i = 0; i < NUM_LIGHTS; i++) {
1945 vec3 light_position = gl_LightSource[i].position.xyz;
1946 vec3 light_diffuse = gl_LightSource[i].diffuse.rgb;
1948 float bsdf = max(dot(N, light_position), 0.0);
1949 L += light_diffuse*bsdf;
1952 result = vec4(L*color.rgb, 1.0);
1955 void node_bsdf_glossy(vec4 color, float roughness, vec3 N, vec3 I, out vec4 result)
1960 /* directional lights */
1961 for(int i = 0; i < NUM_LIGHTS; i++) {
1962 vec3 light_position = gl_LightSource[i].position.xyz;
1963 vec3 H = gl_LightSource[i].halfVector.xyz;
1964 vec3 light_diffuse = gl_LightSource[i].diffuse.rgb;
1965 vec3 light_specular = gl_LightSource[i].specular.rgb;
1967 /* we mix in some diffuse so low roughness still shows up */
1968 float bsdf = 0.5*pow(max(dot(N, H), 0.0), 1.0/roughness);
1969 bsdf += 0.5*max(dot(N, light_position), 0.0);
1970 L += light_specular*bsdf;
1973 result = vec4(L*color.rgb, 1.0);
1976 void node_bsdf_anisotropic(vec4 color, float roughnessU, float roughnessV, vec3 N, vec3 I, out vec4 result)
1978 node_bsdf_diffuse(color, 0.0, N, result);
1981 void node_bsdf_glass(vec4 color, float roughness, float ior, vec3 N, vec3 I, out vec4 result)
1983 node_bsdf_diffuse(color, 0.0, N, result);
1986 void node_bsdf_translucent(vec4 color, vec3 N, out vec4 result)
1988 node_bsdf_diffuse(color, 0.0, N, result);
1991 void node_bsdf_transparent(vec4 color, out vec4 result)
1993 /* this isn't right */
2000 void node_bsdf_velvet(vec4 color, float sigma, vec3 N, out vec4 result)
2002 node_bsdf_diffuse(color, 0.0, N, result);
2007 void node_emission(vec4 color, float strength, vec3 N, out vec4 result)
2009 result = color*strength;
2014 void node_mix_shader(float fac, vec4 shader1, vec4 shader2, out vec4 shader)
2016 shader = mix(shader1, shader2, fac);
2019 void node_add_shader(vec4 shader1, vec4 shader2, out vec4 shader)
2021 shader = shader1 + shader2;
2026 void node_fresnel(float ior, vec3 N, vec3 I, out float result)
2028 float eta = max(ior, 0.00001);
2029 result = fresnel_dielectric(I, N, eta); //backfacing()? 1.0/eta: eta);
2034 void node_geometry(vec3 I, vec3 N, mat4 toworld,
2035 out vec3 position, out vec3 normal, out vec3 tangent,
2036 out vec3 true_normal, out vec3 incoming, out vec3 parametric,
2037 out float backfacing)
2039 position = (toworld*vec4(I, 1.0)).xyz;
2041 tangent = vec3(0.0);
2044 parametric = vec3(0.0);
2048 void node_tex_coord(vec3 I, vec3 N, mat4 toworld,
2049 vec3 attr_orco, vec3 attr_uv,
2050 out vec3 generated, out vec3 uv, out vec3 object,
2051 out vec3 camera, out vec3 window, out vec3 reflection)
2053 generated = attr_orco;
2057 window = gl_FragCoord.xyz;
2058 reflection = reflect(N, I);
2064 void node_tex_blend(vec3 co, out float fac)
2069 void node_tex_clouds(vec3 co, float size, out vec4 color, out float fac)
2075 void node_tex_distnoise(vec3 co, float size, float distortion, out float fac)
2080 void node_tex_environment(vec3 co, sampler2D ima, out vec4 color)
2082 float u = (atan(co.y, co.x) + M_PI)/(2.0*M_PI);
2083 float v = atan(co.z, hypot(co.x, co.y))/M_PI + 0.5;
2085 color = texture2D(ima, vec2(u, v));
2088 void node_tex_image(vec3 co, sampler2D ima, out vec4 color)
2090 color = texture2D(ima, co.xy);
2093 void node_tex_magic(vec3 p, float turbulence, float n, out vec4 color)
2095 float turb = turbulence/5.0;
2097 float x = sin((p.x + p.y + p.z)*5.0);
2098 float y = cos((-p.x + p.y - p.z)*5.0);
2099 float z = -cos((-p.x - p.y + p.z)*5.0);
2161 color = vec4(0.5 - x, 0.5 - y, 0.5 - z, 1.0);
2164 void node_tex_marble(vec3 co, float size, float turbulence, out float fac)
2169 void node_tex_musgrave(vec3 co, float size, float dimension, float lacunarity, float octaves, float offset, float gain, out float fac)
2174 void node_tex_noise(vec3 co, out vec4 color, out float fac)
2180 void node_tex_sky(vec3 co, out vec4 color)
2185 void node_tex_stucci(vec3 co, float size, float turbulence, out float fac)
2190 void node_tex_voronoi(vec3 co, float size, float weight1, float weight2, float weight3, float weight4, float exponent, out vec4 color, out float fac)
2196 void node_tex_wood(vec3 co, float size, float turbulence, out float fac)
2203 void node_light_path(
2204 out float is_camera_ray,
2205 out float is_shadow_ray,
2206 out float is_diffuse_ray,
2207 out float is_glossy_ray,
2208 out float is_singular_ray,
2209 out float is_reflection_ray,
2210 out float is_transmission_ray)
2212 is_camera_ray = 1.0;
2213 is_shadow_ray = 0.0;
2214 is_diffuse_ray = 0.0;
2215 is_glossy_ray = 0.0;
2216 is_singular_ray = 0.0;
2217 is_reflection_ray = 0.0;
2218 is_transmission_ray = 0.0;
2223 void node_output_material(vec4 surface, vec4 volume, float displacement, out vec4 result)