00001 #ifndef _H_vsl_utils
00002 #define _H_vsl_utils
00003
00004
00005
00006
00007
00008 #define vslBimix(c0,c1,c2,c3,x,y) \
00009 mix (mix (c2,c0,y), mix (c3,c1,y), x)
00010
00011 color vslColourBias( color bias; color c; )
00012 {
00013 color cb, w = color(1,1,1);
00014 cb = c / ((w/max(bias,color(1e-4)) - 2) * (w - c) + w);
00015 return cb;
00016 }
00017
00018 float vslFloatBias( float bias; float t; )
00019 {
00020 float tb = t / ((1/max(bias,1e-4) - 2) * (1 - t) + 1);
00021 return tb;
00022 }
00023
00024 float vslFloatGain( float gg; float t; )
00025 {
00026 float tg, gain;
00027 gain = clamp(gg, .0001, .9999);
00028 if( t < .5 )
00029 {
00030 tg = t / ((1/gain - 2) * (1 - 2*t) + 1);
00031 }
00032 else
00033 {
00034 tg = ((1/gain - 2) * (1 - 2*t) - t) / ((1/gain - 2) * (1 - 2*t) - 1);
00035 }
00036 return tg;
00037 }
00038
00039 color vslColourGain( color gain; color c; )
00040 {
00041 color cb;
00042 setcomp(cb, 0, vslFloatGain( comp(gain,0), comp(c,0) ) );
00043 setcomp(cb, 1, vslFloatGain( comp(gain,1), comp(c,1) ) );
00044 setcomp(cb, 2, vslFloatGain( comp(gain,2), comp(c,2) ) );
00045 return cb;
00046 }
00047
00048 void vslColourAdjust(color C;
00049 float Saturation, MidPoint, Contrast, Clamp;
00050 color Tint;
00051 output color result;)
00052 {
00053 float lum = .2125*comp(C, 0) + .7154*comp(C, 1) + .0721*comp(C, 2);
00054 result = mix(color(lum), C, Saturation);
00055 result = mix(color(MidPoint), result, Contrast);
00056 result = mix(result, clamp(result, color(0,0,0), color(1,1,1)), Clamp);
00057 result *= Tint;
00058 }
00059
00060 void vlsMarble( uniform color cs;
00061 uniform float veining;
00062 uniform float variation;
00063 output color result;)
00064 {
00065 string noiseSpace = "object";
00066
00067 float pixelsize, twice, scale, weight, turbulence, sat, val;
00068 color cshsv, csrgb;
00069 point PP=transform("object",P);
00070
00071
00072 PP = PP * veining / 2;
00073 pixelsize = sqrt(area(PP));
00074 twice = 2 * pixelsize;
00075
00076
00077 turbulence = 0;
00078 for (scale = 1; scale > twice; scale /= 2) {
00079 if(variation == 0)
00080 turbulence += scale * abs(noise(PP/scale)-0.5);
00081 else
00082 turbulence += scale * abs(noise(PP/scale, variation)-0.5);
00083 }
00084
00085
00086 if (scale > pixelsize) {
00087 weight = (scale / pixelsize) - 1;
00088 weight = clamp(weight, 0, 1);
00089 turbulence += scale * abs(noise(PP/scale)-0.5);
00090
00091
00092 }
00093
00094
00095
00096
00097
00098
00099 cshsv = ctransform("hsv", cs);
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109 sat = comp(cshsv, 1);
00110 val = comp(cshsv, 2);
00111 sat *= float spline(turbulence,
00112 0.999, 0.999, 0.200, 0.060, 0.030, 0.020,
00113 0.010, 0.010, 0.000);
00114 val *= float spline(turbulence,
00115 0.999, 0.999, 0.400, 0.000,
00116 0.000, 0.000, 0.000,
00117 0.000, 0.000, 0.000, 0.000
00118 ) + float spline(turbulence,
00119 0.000, 0.000, 0.000, 0.800,
00120 0.800, 0.800, 0.800,
00121 0.999, 0.999, 0.999, 0.999);
00122 setcomp(cshsv, 1, sat);
00123 setcomp(cshsv, 2, val);
00124 result= ctransform("hsv", "rgb", cshsv);
00125 }
00126
00127 void
00128 vslCombineColors( float method; color f1; color f2;
00129 output color result;)
00130 {
00131 if (method == 0)
00132 {
00133
00134 result = f1 * f2;
00135 }
00136 else
00137 if (method == 1)
00138 {
00139
00140 result = f1 + f2 - f1 * f2;
00141 }
00142 else
00143 if (method == 2)
00144 {
00145
00146 result = f1 - f1 * f2;
00147 }
00148 }
00149
00150 void
00151 vsl_Fractal(
00152 uniform float fr_frequency;
00153 uniform float fr_layers;
00154 uniform float fr_lacunarity;
00155 uniform float fr_dimension;
00156 uniform float fr_erosion;
00157 uniform float fr_variation;
00158 point pp;
00159 output float fr_output;
00160 )
00161 {
00162
00163 float Noise (point Q)
00164 {
00165
00166 extern uniform float fr_variation;
00167 point qq=transform("object",Q);
00168 return (smoothstep (.2, .8, float noise (qq, fr_variation)));
00169 }
00170 float sum, j, mag;
00171
00172 fr_output = mix (Noise (fr_frequency*pp), .5, smoothstep (.25, 1, fr_frequency*filterwidthp(pp)));
00173 sum = 1;
00174
00175 float f = fr_frequency;
00176 mag = 0;
00177 for( j = 0; j < fr_layers; j += 1 ) {
00178 f *= fr_lacunarity;
00179 mag = 1/pow ( f, 3-2 * fr_dimension + mix ( -fr_erosion, fr_erosion, fr_output/sum ) );
00180 fr_output += mag*mix ( Noise ( f*pp ), .5, smoothstep ( 0.25, 1 , f*filterwidthp( pp ) ) );
00181 sum += mag;
00182 }
00183
00184 fr_output /= sum;
00185 }
00186
00187
00188 void
00189 vslWorley (float frequency;
00190 uniform string distancemetric;
00191 float jitter;
00192 uniform float clamp;
00193 float c1, c2;
00194 point p;
00195 output float g;
00196 )
00197 {
00198 float f1,f2;
00199 point pp = frequency * p;
00200 point thiscell = point (floor(xcomp(pp))+0.5,
00201 floor(ycomp(pp))+0.5,
00202 floor(zcomp(pp))+0.5);
00203
00204 f1 = f2 = 1000;
00205 uniform float i, j, k;
00206 for (i = -1; i <= 1; i += 1) {
00207 for (j = -1; j <= 1; j += 1) {
00208 for (k = -1; k <= 1; k += 1) {
00209 point testcell = thiscell + vector(i,j,k);
00210 point pos = testcell + jitter * (vector cellnoise (testcell) - 0.5);
00211 vector offset = pos - pp;
00212 float dist;
00213 if (distancemetric == "Euclidian")
00214 dist = offset.offset;
00215 else
00216 {
00217 dist = abs(xcomp(offset)) +
00218 abs(ycomp(offset)) +
00219 abs(zcomp(offset));
00220 }
00221 if (dist < f1) {
00222 f2 = f1;
00223 f1 = dist;
00224 }
00225 else if (dist < f2) {
00226 f2 = dist;
00227 }
00228 }
00229 }
00230 }
00231 if (distancemetric == "Euclidian")
00232 {
00233 f1 = sqrt(f1);
00234 f2 = sqrt(f2);
00235 }
00236 g = f1 * c1 + f2 * c2;
00237 if (clamp != 0)
00238 {
00239 g = clamp(g, 0, 1);
00240 }
00241 }
00242
00243
00244 void
00245 vslTurbulence(
00246 uniform float baseFreq;
00247 uniform float octaves, lacunarity, gain;
00248 uniform float invert;
00249 float fourthdimension;
00250 point p;
00251 output float result;)
00252 {
00253 uniform float amp = 1;
00254 uniform float sum = 0;
00255 varying point pp = baseFreq*p;
00256 varying float fw = filterwidthp(pp);
00257 uniform float i;
00258 result = 0;
00259
00260 for (i = 0; i < octaves; i += 1) {
00261 #pragma nolint
00262 float n = filteredsnoiset (pp, fw, fourthdimension);
00263 float dn = filterwidth(n);
00264 result += amp * filteredabs (n, dn);
00265 sum += amp; amp *= gain; pp *= lacunarity; fw *= lacunarity;
00266 }
00267 result /= .5*sum;
00268
00269 if (invert != 0)
00270 result = 1-result;
00271 }
00272
00273 void
00274 vslBrownian(
00275 uniform float baseFreq;
00276 uniform float octaves, lacunarity, gain;
00277 uniform float rescale;
00278 varying float fourthdimension;
00279 point p;
00280 output float result;)
00281 {
00282 uniform float amp = 1;
00283 varying point pp = baseFreq * p;
00284 varying float fw = filterwidthp(pp);
00285 uniform float i;
00286 result = 0;
00287 for (i = 0; i < octaves; i += 1) {
00288 #pragma nolint
00289 result += amp * filteredsnoiset(pp, fw, fourthdimension);
00290 amp *= gain; pp *= lacunarity; fw *= lacunarity;
00291 }
00292 if (rescale != 0)
00293 {
00294 result = .5 * (1 + result);
00295 if (rescale == 2)
00296 {
00297 result = clamp(result, 0, 1);
00298 }
00299 }
00300 }
00301
00302
00303 float vslLuminance(color c)
00304 {
00305 return 0.3 * comp(c, 0) + 0.59 * comp(c, 1) + 0.11 * comp(c, 2);
00306 }
00307 void vslConvertFloatToColour( float in; output color out; )
00308 {
00309 out = color(in);
00310 }
00311
00312 vslConvertColourToFloat( color in; output float out;)
00313 {
00314
00315 out = vslLuminance(in);
00316 }
00317
00318 void vslColourAdjust( color C;
00319 float Saturation, MidPoint, Contrast, Clamp;
00320 color Tint;
00321 output color result;
00322 )
00323 {
00324 float lum = .2125*comp(C, 0) + .7154*comp(C, 1) + .0721*comp(C, 2);
00325 result = mix(color(lum), C, Saturation);
00326 result = mix(color(MidPoint), result, Contrast);
00327 result = mix(result, clamp(result, color(0,0,0), color(1,1,1)), Clamp);
00328 result *= Tint;
00329 }
00330
00331 void vslRemapF( float f;
00332 float inputmin, inputmax;
00333 float outputmin, outputmax;
00334 float disablegain;
00335 float bias, gain;
00336 output float result;
00337 )
00338 {
00339 result = clamp(f, inputmin, inputmax);
00340 result = outputmin + (outputmax - outputmin) * (result - inputmin) / (inputmax - inputmin);
00341 if (disablegain == 0)
00342 {
00343 result = vslFloatBias(bias, result);
00344 result = vslFloatGain(gain, result);
00345 }
00346 }
00347
00348 void vslColourInvert(varying color input;
00349 uniform string cspace;
00350 uniform float inv1;
00351 uniform float inv2;
00352 uniform float inv3;
00353 output color cresult;
00354 )
00355 {
00356 uniform string rgb = "RGB";
00357 if( cspace != rgb)
00358 cresult = ctransform("RGB", cspace, input);
00359 else
00360 cresult = input;
00361 if( inv1 != 0 )
00362 {
00363 if( cspace != rgb)
00364 {
00365
00366 setcomp(cresult, 0, mod(comp(cresult, 0) + .5, 1));
00367 }
00368 else
00369 {
00370 setcomp(cresult, 0, 1 - comp(cresult, 0));
00371 }
00372 }
00373 if( inv2 != 0 )
00374 {
00375 setcomp(cresult, 1, 1 - comp(cresult, 1));
00376 }
00377 if( inv3 != 0 )
00378 {
00379 setcomp(cresult, 2, 1 - comp(cresult, 2));
00380 }
00381 if( cspace != rgb )
00382 cresult = ctransform( "RGB", rgb, cresult);
00383 }
00384
00385 void
00386 vslColorRamp( float RampType;
00387 float tile;
00388 float numKnots;
00389 float k0; color c0;
00390 float k1; color c1;
00391 float k2; color c2;
00392 float k3; color c3;
00393 float k4; color c4;
00394 float k5; color c5;
00395 point Q;
00396 vector dQu;
00397 vector dQv;
00398 output color result;
00399 )
00400 {
00401 float k, s, t;
00402
00403 if( RampType <= 1 )
00404 {
00405 if( RampType == 0 )
00406 {
00407 s = xcomp(Q);
00408 }
00409 else
00410 {
00411 s = ycomp(Q);
00412 }
00413 if( tile == 1 )
00414 s = s - floor(s);
00415 else
00416 s = clamp(s, 0, 1);
00417 k = float spline( "solvecatrom", s, k0, k1, k2, k3, k4, k5 );
00418 result = color spline( "catrom", k, c0, c1, c2, c3, c4, c5);
00419 }
00420 }
00421
00422 void vslST (
00423 uniform float angle;
00424 uniform float repeatS; uniform float repeatT;
00425 uniform float offsetS; uniform float offsetT;
00426 output point Q;
00427 output vector dQu;
00428 output vector dQv;
00429 )
00430 {
00431 extern float s, t, du, dv;
00432
00433 setxcomp(Q, repeatS * s + offsetS);
00434 setycomp(Q, repeatT * t + offsetT);
00435 setzcomp(Q, 0);
00436
00437 if(angle != 0)
00438 Q = rotate(Q, radians(angle), point(0,0,0), point(0,0,1));
00439 dQu = vector Du(Q)*du;
00440 dQv = vector Dv(Q)*dv;
00441 }
00442
00443
00444
00445 #endif