~ubuntu-branches/ubuntu/trusty/blender/trusty

« back to all changes in this revision

Viewing changes to intern/cycles/kernel/shaders/node_texture.h

  • Committer: Package Import Robot
  • Author(s): Jeremy Bicha
  • Date: 2013-03-06 12:08:47 UTC
  • mfrom: (1.5.1) (14.1.8 experimental)
  • Revision ID: package-import@ubuntu.com-20130306120847-frjfaryb2zrotwcg
Tags: 2.66a-1ubuntu1
* Resynchronize with Debian (LP: #1076930, #1089256, #1052743, #999024,
  #1122888, #1147084)
* debian/control:
  - Lower build-depends on libavcodec-dev since we're not
    doing the libav9 transition in Ubuntu yet

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This program is free software; you can redistribute it and/or
 
3
 * modify it under the terms of the GNU General Public License
 
4
 * as published by the Free Software Foundation; either version 2
 
5
 * of the License, or (at your option) any later version.
 
6
 *
 
7
 * This program is distributed in the hope that it will be useful,
 
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
 * GNU General Public License for more details.
 
11
 *
 
12
 * You should have received a copy of the GNU General Public License
 
13
 * along with this program; if not, write to the Free Software Foundation,
 
14
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 
15
 */
 
16
 
 
17
/* Voronoi Distances */
 
18
 
 
19
float voronoi_distance(string distance_metric, vector d, float e)
 
20
{
 
21
        float result = 0.0;
 
22
 
 
23
        if (distance_metric == "Distance Squared")
 
24
                result = dot(d, d);
 
25
        if (distance_metric == "Actual Distance")
 
26
                result = length(d);
 
27
        if (distance_metric == "Manhattan")
 
28
                result = fabs(d[0]) + fabs(d[1]) + fabs(d[2]);
 
29
        if (distance_metric == "Chebychev")
 
30
                result = max(fabs(d[0]), max(fabs(d[1]), fabs(d[2])));
 
31
        if (distance_metric == "Minkovsky 1/2")
 
32
                result = sqrt(fabs(d[0])) + sqrt(fabs(d[1])) + sqrt(fabs(d[1]));
 
33
        if (distance_metric == "Minkovsky 4")
 
34
                result = sqrt(sqrt(dot(d * d, d * d)));
 
35
        if (distance_metric == "Minkovsky")
 
36
                result = pow(pow(fabs(d[0]), e) + pow(fabs(d[1]), e) + pow(fabs(d[2]), e), 1.0 / e);
 
37
        
 
38
        return result;
 
39
}
 
40
 
 
41
/* Voronoi / Worley like */
 
42
 
 
43
color cellnoise_color(point p)
 
44
{
 
45
        float r = cellnoise(p);
 
46
        float g = cellnoise(point(p[1], p[0], p[2]));
 
47
        float b = cellnoise(point(p[1], p[2], p[0]));
 
48
 
 
49
        return color(r, g, b);
 
50
}
 
51
 
 
52
void voronoi(point p, string distance_metric, float e, float da[4], point pa[4])
 
53
{
 
54
        /* returns distances in da and point coords in pa */
 
55
        int xx, yy, zz, xi, yi, zi;
 
56
 
 
57
        xi = (int)floor(p[0]);
 
58
        yi = (int)floor(p[1]);
 
59
        zi = (int)floor(p[2]);
 
60
 
 
61
        da[0] = 1e10;
 
62
        da[1] = 1e10;
 
63
        da[2] = 1e10;
 
64
        da[3] = 1e10;
 
65
 
 
66
        for (xx = xi - 1; xx <= xi + 1; xx++) {
 
67
                for (yy = yi - 1; yy <= yi + 1; yy++) {
 
68
                        for (zz = zi - 1; zz <= zi + 1; zz++) {
 
69
                                point ip = point(xx, yy, zz);
 
70
                                point vp = (point)cellnoise_color(ip);
 
71
                                point pd = p - (vp + ip);
 
72
                                float d = voronoi_distance(distance_metric, pd, e);
 
73
 
 
74
                                vp += point(xx, yy, zz);
 
75
 
 
76
                                if (d < da[0]) {
 
77
                                        da[3] = da[2];
 
78
                                        da[2] = da[1];
 
79
                                        da[1] = da[0];
 
80
                                        da[0] = d;
 
81
 
 
82
                                        pa[3] = pa[2];
 
83
                                        pa[2] = pa[1];
 
84
                                        pa[1] = pa[0];
 
85
                                        pa[0] = vp;
 
86
                                }
 
87
                                else if (d < da[1]) {
 
88
                                        da[3] = da[2];
 
89
                                        da[2] = da[1];
 
90
                                        da[1] = d;
 
91
 
 
92
                                        pa[3] = pa[2];
 
93
                                        pa[2] = pa[1];
 
94
                                        pa[1] = vp;
 
95
                                }
 
96
                                else if (d < da[2]) {
 
97
                                        da[3] = da[2];
 
98
                                        da[2] = d;
 
99
 
 
100
                                        pa[3] = pa[2];
 
101
                                        pa[2] = vp;
 
102
                                }
 
103
                                else if (d < da[3]) {
 
104
                                        da[3] = d;
 
105
                                        pa[3] = vp;
 
106
                                }
 
107
                        }
 
108
                }
 
109
        }
 
110
}
 
111
 
 
112
float voronoi_Fn(point p, int n)
 
113
{
 
114
        float da[4];
 
115
        point pa[4];
 
116
 
 
117
        voronoi(p, "Distance Squared", 0, da, pa);
 
118
 
 
119
        return da[n];
 
120
}
 
121
 
 
122
float voronoi_FnFn(point p, int n1, int n2)
 
123
{
 
124
        float da[4];
 
125
        point pa[4];
 
126
 
 
127
        voronoi(p, "Distance Squared", 0, da, pa);
 
128
 
 
129
        return da[n2] - da[n1];
 
130
}
 
131
 
 
132
float voronoi_F1(point p) { return voronoi_Fn(p, 0); }
 
133
float voronoi_F2(point p) { return voronoi_Fn(p, 1); }
 
134
float voronoi_F3(point p) { return voronoi_Fn(p, 2); }
 
135
float voronoi_F4(point p) { return voronoi_Fn(p, 3); }
 
136
float voronoi_F1F2(point p) { return voronoi_FnFn(p, 0, 1); }
 
137
 
 
138
float voronoi_Cr(point p)
 
139
{
 
140
        /* crackle type pattern, just a scale/clamp of F2-F1 */
 
141
        float t = 10.0 * voronoi_F1F2(p);
 
142
        return (t > 1.0) ? 1.0 : t;
 
143
}
 
144
 
 
145
float voronoi_F1S(point p) { return 2.0 * voronoi_F1(p) - 1.0; }
 
146
float voronoi_F2S(point p) { return 2.0 * voronoi_F2(p) - 1.0; }
 
147
float voronoi_F3S(point p) { return 2.0 * voronoi_F3(p) - 1.0; }
 
148
float voronoi_F4S(point p) { return 2.0 * voronoi_F4(p) - 1.0; }
 
149
float voronoi_F1F2S(point p) { return 2.0 * voronoi_F1F2(p) - 1.0; }
 
150
float voronoi_CrS(point p) { return 2.0 * voronoi_Cr(p) - 1.0; }
 
151
 
 
152
/* Noise Bases */
 
153
 
 
154
float safe_noise(point p, int type)
 
155
{
 
156
        float f = 0.0;
 
157
        
 
158
        /* Perlin noise in range -1..1 */
 
159
        if (type == 0)
 
160
                f = noise("perlin", p);
 
161
        
 
162
        /* Perlin noise in range 0..1 */
 
163
        else
 
164
                f = noise(p);
 
165
 
 
166
        /* can happen for big coordinates, things even out to 0.5 then anyway */
 
167
        if(!isfinite(f))
 
168
                return 0.5;
 
169
        
 
170
        return f;
 
171
}
 
172
 
 
173
float noise_basis(point p, string basis)
 
174
{
 
175
        float result = 0.0;
 
176
 
 
177
        if (basis == "Perlin")
 
178
                result = safe_noise(p, 1);
 
179
        if (basis == "Voronoi F1")
 
180
                result = voronoi_F1S(p);
 
181
        if (basis == "Voronoi F2")
 
182
                result = voronoi_F2S(p);
 
183
        if (basis == "Voronoi F3")
 
184
                result = voronoi_F3S(p);
 
185
        if (basis == "Voronoi F4")
 
186
                result = voronoi_F4S(p);
 
187
        if (basis == "Voronoi F2-F1")
 
188
                result = voronoi_F1F2S(p);
 
189
        if (basis == "Voronoi Crackle")
 
190
                result = voronoi_CrS(p);
 
191
        if (basis == "Cell Noise")
 
192
                result = cellnoise(p);
 
193
        
 
194
        return result;
 
195
}
 
196
 
 
197
/* Soft/Hard Noise */
 
198
 
 
199
float noise_basis_hard(point p, string basis, int hard)
 
200
{
 
201
        float t = noise_basis(p, basis);
 
202
        return (hard) ? fabs(2.0 * t - 1.0) : t;
 
203
}
 
204
 
 
205
/* Waves */
 
206
 
 
207
float noise_wave(string wave, float a)
 
208
{
 
209
        float result = 0.0;
 
210
 
 
211
        if (wave == "Sine") {
 
212
                result = 0.5 + 0.5 * sin(a);
 
213
        }
 
214
        else if (wave == "Saw") {
 
215
                float b = 2 * M_PI;
 
216
                int n = (int)(a / b);
 
217
                a -= n * b;
 
218
                if (a < 0) a += b;
 
219
 
 
220
                result = a / b;
 
221
        }
 
222
        else if (wave == "Tri") {
 
223
                float b = 2 * M_PI;
 
224
                float rmax = 1.0;
 
225
 
 
226
                result = rmax - 2.0 * fabs(floor((a * (1.0 / b)) + 0.5) - (a * (1.0 / b)));
 
227
        }
 
228
 
 
229
        return result;
 
230
}
 
231
 
 
232
/* Turbulence */
 
233
 
 
234
float noise_turbulence(point p, string basis, float details, int hard)
 
235
{
 
236
        float fscale = 1.0;
 
237
        float amp = 1.0;
 
238
        float sum = 0.0;
 
239
        int i, n;
 
240
        
 
241
        float octaves = clamp(details, 0.0, 16.0);
 
242
        n = (int)octaves;
 
243
 
 
244
        for (i = 0; i <= n; i++) {
 
245
                float t = noise_basis(fscale * p, basis);
 
246
 
 
247
                if (hard)
 
248
                        t = fabs(2.0 * t - 1.0);
 
249
 
 
250
                sum += t * amp;
 
251
                amp *= 0.5;
 
252
                fscale *= 2.0;
 
253
        }
 
254
        
 
255
        float rmd = octaves - floor(octaves);
 
256
 
 
257
        if (rmd != 0.0) {
 
258
                float t = noise_basis(fscale * p, basis);
 
259
 
 
260
                if (hard)
 
261
                        t = fabs(2.0 * t - 1.0);
 
262
 
 
263
                float sum2 = sum + t*amp;
 
264
 
 
265
                sum *= ((float)(1 << n) / (float)((1 << (n + 1)) - 1));
 
266
                sum2 *= ((float)(1 << (n + 1)) / (float)((1 << (n + 2)) - 1));
 
267
 
 
268
                return (1.0 - rmd)*sum + rmd*sum2;
 
269
        }
 
270
        else {
 
271
                sum *= ((float)(1 << n) / (float)((1 << (n + 1)) - 1));
 
272
                return sum;
 
273
        }
 
274
}
 
275
 
 
276
/* Utility */
 
277
 
 
278
float nonzero(float f, float eps)
 
279
{
 
280
        float r;
 
281
 
 
282
        if (abs(f) < eps)
 
283
                r = sign(f) * eps;
 
284
        else
 
285
                r = f;
 
286
        
 
287
        return r;
 
288
}
 
289