Blender V2.61 - r43446

node_texture.h

Go to the documentation of this file.
00001 /*
00002  * This program is free software; you can redistribute it and/or
00003  * modify it under the terms of the GNU General Public License
00004  * as published by the Free Software Foundation; either version 2
00005  * of the License, or (at your option) any later version.
00006  *
00007  * This program is distributed in the hope that it will be useful,
00008  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00009  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00010  * GNU General Public License for more details.
00011  *
00012  * You should have received a copy of the GNU General Public License
00013  * along with this program; if not, write to the Free Software Foundation,
00014  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00015  */
00016 
00017 /* Voronoi Distances */
00018 
00019 float voronoi_distance(string distance_metric, vector d, float e)
00020 {
00021     float result = 0.0;
00022 
00023     if(distance_metric == "Distance Squared")
00024         result = dot(d, d);
00025     if(distance_metric == "Actual Distance")
00026         result = length(d);
00027     if(distance_metric == "Manhattan")
00028         result = fabs(d[0]) + fabs(d[1]) + fabs(d[2]);
00029     if(distance_metric == "Chebychev")
00030         result = max(fabs(d[0]), max(fabs(d[1]), fabs(d[2])));
00031     if(distance_metric == "Minkovsky 1/2")
00032         result = sqrt(fabs(d[0])) + sqrt(fabs(d[1])) + sqrt(fabs(d[1]));
00033     if(distance_metric == "Minkovsky 4")
00034         result = sqrt(sqrt(dot(d*d, d*d)));
00035     if(distance_metric == "Minkovsky")
00036         result = pow(pow(fabs(d[0]), e) + pow(fabs(d[1]), e) + pow(fabs(d[2]), e), 1.0/e);
00037     
00038     return result;
00039 }
00040 
00041 /* Voronoi / Worley like */
00042 
00043 color cellnoise_color(point p)
00044 {
00045     float r = cellnoise(p);
00046     float g = cellnoise(point(p[1], p[0], p[2]));
00047     float b = cellnoise(point(p[1], p[2], p[0]));
00048 
00049     return color(r, g, b);
00050 }
00051 
00052 void voronoi(point p, string distance_metric, float e, float da[4], point pa[4])
00053 {
00054     /* returns distances in da and point coords in pa */
00055     int xx, yy, zz, xi, yi, zi;
00056 
00057     xi = (int)floor(p[0]);
00058     yi = (int)floor(p[1]);
00059     zi = (int)floor(p[2]);
00060 
00061     da[0] = 1e10;
00062     da[1] = 1e10;
00063     da[2] = 1e10;
00064     da[3] = 1e10;
00065 
00066     for(xx = xi-1; xx <= xi+1; xx++) {
00067         for(yy = yi-1; yy <= yi+1; yy++) {
00068             for(zz = zi-1; zz <= zi+1; zz++) {
00069                 point ip = point(xx, yy, zz);
00070                 point vp = (point)cellnoise_color(ip);
00071                 point pd = p - (vp + ip);
00072                 float d = voronoi_distance(distance_metric, pd, e);
00073 
00074                 vp += point(xx, yy, zz);
00075 
00076                 if(d < da[0]) {
00077                     da[3] = da[2];
00078                     da[2] = da[1];
00079                     da[1] = da[0];
00080                     da[0] = d;
00081 
00082                     pa[3] = pa[2];
00083                     pa[2] = pa[1];
00084                     pa[1] = pa[0];
00085                     pa[0] = vp;
00086                 }
00087                 else if(d < da[1]) {
00088                     da[3] = da[2];
00089                     da[2] = da[1];
00090                     da[1] = d;
00091 
00092                     pa[3] = pa[2];
00093                     pa[2] = pa[1];
00094                     pa[1] = vp;
00095                 }
00096                 else if(d < da[2]) {
00097                     da[3] = da[2];
00098                     da[2] = d;
00099 
00100                     pa[3] = pa[2];
00101                     pa[2] = vp;
00102                 }
00103                 else if(d < da[3]) {
00104                     da[3] = d;
00105                     pa[3] = vp;
00106                 }
00107             }
00108         }
00109     }
00110 }
00111 
00112 float voronoi_Fn(point p, int n)
00113 {
00114     float da[4];
00115     point pa[4];
00116 
00117     voronoi(p, "Distance Squared", 0, da, pa);
00118 
00119     return da[n];
00120 }
00121 
00122 float voronoi_FnFn(point p, int n1, int n2)
00123 {
00124     float da[4];
00125     point pa[4];
00126 
00127     voronoi(p, "Distance Squared", 0, da, pa);
00128 
00129     return da[n2] - da[n1];
00130 }
00131 
00132 float voronoi_F1(point p) { return voronoi_Fn(p, 0); }
00133 float voronoi_F2(point p) { return voronoi_Fn(p, 1); }
00134 float voronoi_F3(point p) { return voronoi_Fn(p, 2); }
00135 float voronoi_F4(point p) { return voronoi_Fn(p, 3); }
00136 float voronoi_F1F2(point p) { return voronoi_FnFn(p, 0, 1); }
00137 
00138 float voronoi_Cr(point p)
00139 {
00140     /* crackle type pattern, just a scale/clamp of F2-F1 */
00141     float t = 10.0*voronoi_F1F2(p);
00142     return (t > 1.0)? 1.0: t;
00143 }
00144 
00145 float voronoi_F1S(point p) { return 2.0*voronoi_F1(p) - 1.0; }
00146 float voronoi_F2S(point p) { return 2.0*voronoi_F2(p) - 1.0; }
00147 float voronoi_F3S(point p) { return 2.0*voronoi_F3(p) - 1.0; }
00148 float voronoi_F4S(point p) { return 2.0*voronoi_F4(p) - 1.0; }
00149 float voronoi_F1F2S(point p) { return 2.0*voronoi_F1F2(p) - 1.0; }
00150 float voronoi_CrS(point p) { return 2.0*voronoi_Cr(p) - 1.0; }
00151 
00152 /* Noise Bases */
00153 
00154 float noise_basis(point p, string basis)
00155 {
00156     float result = 0.0;
00157 
00158     if(basis == "Perlin")
00159         result = noise(p);
00160     if(basis == "Voronoi F1")
00161         result = voronoi_F1S(p);
00162     if(basis == "Voronoi F2")
00163         result = voronoi_F2S(p);
00164     if(basis == "Voronoi F3")
00165         result = voronoi_F3S(p);
00166     if(basis == "Voronoi F4")
00167         result = voronoi_F4S(p);
00168     if(basis == "Voronoi F2-F1")
00169         result = voronoi_F1F2S(p);
00170     if(basis == "Voronoi Crackle")
00171         result = voronoi_CrS(p);
00172     if(basis == "Cell Noise")
00173         result = cellnoise(p);
00174     
00175     return result;
00176 }
00177 
00178 /* Soft/Hard Noise */
00179 
00180 float noise_basis_hard(point p, string basis, int hard)
00181 {
00182     float t = noise_basis(p, basis);
00183     return (hard)? fabs(2.0*t - 1.0): t;
00184 }
00185 
00186 /* Waves */
00187 
00188 float noise_wave(string wave, float a)
00189 {
00190     float result = 0.0;
00191 
00192     if(wave == "Sine") {
00193         result = 0.5 + 0.5*sin(a);
00194     }
00195     else if(wave == "Saw") {
00196         float b = 2*M_PI;
00197         int n = (int)(a / b);
00198         a -= n*b;
00199         if(a < 0) a += b;
00200 
00201         result = a / b;
00202     }
00203     else if(wave == "Tri") {
00204         float b = 2*M_PI;
00205         float rmax = 1.0;
00206 
00207         result = rmax - 2.0*fabs(floor((a*(1.0/b))+0.5) - (a*(1.0/b)));
00208     }
00209 
00210     return result;
00211 }
00212 
00213 /* Turbulence */
00214 
00215 float noise_turbulence(point p, string basis, int octaves, int hard)
00216 {
00217     float fscale = 1.0;
00218     float amp = 1.0;
00219     float sum = 0.0;
00220     int i;
00221 
00222     for(i = 0; i <= octaves; i++) {
00223         float t = noise_basis(fscale*p, basis);
00224 
00225         if(hard)
00226             t = fabs(2.0*t - 1.0);
00227 
00228         sum += t*amp;
00229         amp *= 0.5;
00230         fscale *= 2.0;
00231     }
00232 
00233     sum *= ((float)(1 << octaves)/(float)((1 << (octaves+1)) - 1));
00234 
00235     return sum;
00236 }
00237 
00238 /* Utility */
00239 
00240 float nonzero(float f, float eps)
00241 {
00242     float r;
00243 
00244     if(abs(f) < eps)
00245         r = sign(f)*eps;
00246     else
00247         r = f;
00248     
00249     return r;
00250 }
00251