Blender V5.0
texture_procedural.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <algorithm>
10#include <cmath>
11#include <cstdlib>
12#include <cstring>
13
14#include "BLI_math_geom.h"
15#include "BLI_noise.h"
16#include "BLI_rand.h"
17#include "BLI_utildefines.h"
18
19#include "DNA_material_types.h"
20#include "DNA_node_types.h"
21#include "DNA_texture_types.h"
22
24#include "IMB_imbuf_types.hh"
25
26#include "BKE_colorband.hh"
27#include "BKE_image.hh"
28
29#include "NOD_texture.h"
30
31#include "texture_common.h"
32
33#include "RE_texture.h"
34
36
41
43{
44 if (random_tex_array == nullptr) {
45 return;
46 }
48 random_tex_array = nullptr;
49}
50
51/* ------------------------------------------------------------------------- */
52
53static int blend(const Tex *tex, const float texvec[3], TexResult *texres)
54{
55 float x, y, t;
56
57 if (tex->flag & TEX_FLIPBLEND) {
58 x = texvec[1];
59 y = texvec[0];
60 }
61 else {
62 x = texvec[0];
63 y = texvec[1];
64 }
65
66 if (tex->stype == TEX_LIN) { /* Linear. */
67 texres->tin = (1.0f + x) / 2.0f;
68 }
69 else if (tex->stype == TEX_QUAD) { /* Quadratic. */
70 texres->tin = (1.0f + x) / 2.0f;
71 if (texres->tin < 0.0f) {
72 texres->tin = 0.0f;
73 }
74 else {
75 texres->tin *= texres->tin;
76 }
77 }
78 else if (tex->stype == TEX_EASE) { /* Ease. */
79 texres->tin = (1.0f + x) / 2.0f;
80 if (texres->tin <= 0.0f) {
81 texres->tin = 0.0f;
82 }
83 else if (texres->tin >= 1.0f) {
84 texres->tin = 1.0f;
85 }
86 else {
87 t = texres->tin * texres->tin;
88 texres->tin = (3.0f * t - 2.0f * t * texres->tin);
89 }
90 }
91 else if (tex->stype == TEX_DIAG) { /* Diagonal. */
92 texres->tin = (2.0f + x + y) / 4.0f;
93 }
94 else if (tex->stype == TEX_RAD) { /* Radial. */
95 texres->tin = (atan2f(y, x) / float(2 * M_PI) + 0.5f);
96 }
97 else { /* sphere TEX_SPHERE */
98 texres->tin = 1.0f - sqrtf(x * x + y * y + texvec[2] * texvec[2]);
99 texres->tin = std::max(texres->tin, 0.0f);
100 if (tex->stype == TEX_HALO) {
101 texres->tin *= texres->tin; /* Halo. */
102 }
103 }
104
105 BRICONT;
106
107 return TEX_INT;
108}
109
110/* ------------------------------------------------------------------------- */
111/* ************************************************************************* */
112
113/* newnoise: all noise-based types now have different noise-bases to choose from. */
114
115static int clouds(const Tex *tex, const float texvec[3], TexResult *texres)
116{
117 int rv = TEX_INT;
118
120 texvec[0],
121 texvec[1],
122 texvec[2],
123 tex->noisedepth,
124 (tex->noisetype != TEX_NOISESOFT),
125 tex->noisebasis);
126
127 if (tex->stype == TEX_COLOR) {
128 texres->trgba[0] = texres->tin;
130 texvec[1],
131 texvec[0],
132 texvec[2],
133 tex->noisedepth,
134 (tex->noisetype != TEX_NOISESOFT),
135 tex->noisebasis);
137 texvec[1],
138 texvec[2],
139 texvec[0],
140 tex->noisedepth,
141 (tex->noisetype != TEX_NOISESOFT),
142 tex->noisebasis);
144 texres->trgba[3] = 1.0;
145 return (rv | TEX_RGB);
146 }
147
148 BRICONT;
149
150 return rv;
151}
152
153/* creates a sine wave */
154static float tex_sin(float a)
155{
156 a = 0.5f + 0.5f * sinf(a);
157
158 return a;
159}
160
161/* creates a saw wave */
162static float tex_saw(float a)
163{
164 const float b = 2 * M_PI;
165
166 int n = int(a / b);
167 a -= n * b;
168 if (a < 0) {
169 a += b;
170 }
171 return a / b;
172}
173
174/* creates a triangle wave */
175static float tex_tri(float a)
176{
177 const float b = 2 * M_PI;
178 const float rmax = 1.0;
179
180 a = rmax - 2.0f * fabsf(floorf((a * (1.0f / b)) + 0.5f) - (a * (1.0f / b)));
181
182 return a;
183}
184
185/* computes basic wood intensity value at x,y,z */
186static float wood_int(const Tex *tex, float x, float y, float z)
187{
188 float wi = 0;
189 /* wave form: TEX_SIN=0, TEX_SAW=1, TEX_TRI=2. */
190 short wf = tex->noisebasis2;
191 /* wood type: TEX_BAND=0, TEX_RING=1, TEX_BANDNOISE=2, TEX_RINGNOISE=3. */
192 short wt = tex->stype;
193
194 float (*waveform[3])(float); /* create array of pointers to waveform functions */
195 waveform[0] = tex_sin; /* assign address of tex_sin() function to pointer array */
196 waveform[1] = tex_saw;
197 waveform[2] = tex_tri;
198
199 if ((wf > TEX_TRI) || (wf < TEX_SIN)) {
200 wf = 0; /* check to be sure noisebasis2 is initialized ahead of time */
201 }
202
203 if (wt == TEX_BAND) {
204 wi = waveform[wf]((x + y + z) * 10.0f);
205 }
206 else if (wt == TEX_RING) {
207 wi = waveform[wf](sqrtf(x * x + y * y + z * z) * 20.0f);
208 }
209 else if (wt == TEX_BANDNOISE) {
210 wi = tex->turbul *
212 tex->noisesize, x, y, z, (tex->noisetype != TEX_NOISESOFT), tex->noisebasis);
213 wi = waveform[wf]((x + y + z) * 10.0f + wi);
214 }
215 else if (wt == TEX_RINGNOISE) {
216 wi = tex->turbul *
218 tex->noisesize, x, y, z, (tex->noisetype != TEX_NOISESOFT), tex->noisebasis);
219 wi = waveform[wf](sqrtf(x * x + y * y + z * z) * 20.0f + wi);
220 }
221
222 return wi;
223}
224
225static int wood(const Tex *tex, const float texvec[3], TexResult *texres)
226{
227 int rv = TEX_INT;
228
229 texres->tin = wood_int(tex, texvec[0], texvec[1], texvec[2]);
230
231 BRICONT;
232
233 return rv;
234}
235
236/* computes basic marble intensity at x,y,z */
237static float marble_int(const Tex *tex, float x, float y, float z)
238{
239 float n, mi;
240 short wf = tex->noisebasis2; /* wave form: TEX_SIN=0, TEX_SAW=1, TEX_TRI=2 */
241 short mt = tex->stype; /* marble type: TEX_SOFT=0, TEX_SHARP=1, TEX_SHAPER=2 */
242
243 float (*waveform[3])(float); /* create array of pointers to waveform functions */
244 waveform[0] = tex_sin; /* assign address of tex_sin() function to pointer array */
245 waveform[1] = tex_saw;
246 waveform[2] = tex_tri;
247
248 if ((wf > TEX_TRI) || (wf < TEX_SIN)) {
249 wf = 0; /* check to be sure noisebasis2 isn't initialized ahead of time */
250 }
251
252 n = 5.0f * (x + y + z);
253
254 mi = n + tex->turbul * BLI_noise_generic_turbulence(tex->noisesize,
255 x,
256 y,
257 z,
258 tex->noisedepth,
259 (tex->noisetype != TEX_NOISESOFT),
260 tex->noisebasis);
261
262 if (mt >= TEX_SOFT) { /* TEX_SOFT always true */
263 mi = waveform[wf](mi);
264 if (mt == TEX_SHARP) {
265 mi = sqrtf(mi);
266 }
267 else if (mt == TEX_SHARPER) {
268 mi = sqrtf(sqrtf(mi));
269 }
270 }
271
272 return mi;
273}
274
275static int marble(const Tex *tex, const float texvec[3], TexResult *texres)
276{
277 int rv = TEX_INT;
278
279 texres->tin = marble_int(tex, texvec[0], texvec[1], texvec[2]);
280
281 BRICONT;
282
283 return rv;
284}
285
286/* ------------------------------------------------------------------------- */
287
288static int magic(const Tex *tex, const float texvec[3], TexResult *texres)
289{
290 float x, y, z, turb;
291 int n;
292
293 n = tex->noisedepth;
294 turb = tex->turbul / 5.0f;
295
296 x = sinf((texvec[0] + texvec[1] + texvec[2]) * 5.0f);
297 y = cosf((-texvec[0] + texvec[1] - texvec[2]) * 5.0f);
298 z = -cosf((-texvec[0] - texvec[1] + texvec[2]) * 5.0f);
299 if (n > 0) {
300 x *= turb;
301 y *= turb;
302 z *= turb;
303 y = -cosf(x - y + z);
304 y *= turb;
305 if (n > 1) {
306 x = cosf(x - y - z);
307 x *= turb;
308 if (n > 2) {
309 z = sinf(-x - y - z);
310 z *= turb;
311 if (n > 3) {
312 x = -cosf(-x + y - z);
313 x *= turb;
314 if (n > 4) {
315 y = -sinf(-x + y + z);
316 y *= turb;
317 if (n > 5) {
318 y = -cosf(-x + y + z);
319 y *= turb;
320 if (n > 6) {
321 x = cosf(x + y + z);
322 x *= turb;
323 if (n > 7) {
324 z = sinf(x + y - z);
325 z *= turb;
326 if (n > 8) {
327 x = -cosf(-x - y + z);
328 x *= turb;
329 if (n > 9) {
330 y = -sinf(x - y + z);
331 y *= turb;
332 }
333 }
334 }
335 }
336 }
337 }
338 }
339 }
340 }
341 }
342
343 if (turb != 0.0f) {
344 turb *= 2.0f;
345 x /= turb;
346 y /= turb;
347 z /= turb;
348 }
349 texres->trgba[0] = 0.5f - x;
350 texres->trgba[1] = 0.5f - y;
351 texres->trgba[2] = 0.5f - z;
352
353 texres->tin = (1.0f / 3.0f) * (texres->trgba[0] + texres->trgba[1] + texres->trgba[2]);
354
356 texres->trgba[3] = 1.0f;
357
358 return TEX_RGB;
359}
360
361/* ------------------------------------------------------------------------- */
362
363/* newnoise: stucci also modified to use different noisebasis */
364static int stucci(const Tex *tex, const float texvec[3], TexResult *texres)
365{
366 float b2, ofs;
367 int retval = TEX_INT;
368
370 texvec[0],
371 texvec[1],
372 texvec[2],
373 (tex->noisetype != TEX_NOISESOFT),
374 tex->noisebasis);
375
376 ofs = tex->turbul / 200.0f;
377
378 if (tex->stype) {
379 ofs *= (b2 * b2);
380 }
381
383 texvec[0],
384 texvec[1],
385 texvec[2] + ofs,
386 (tex->noisetype != TEX_NOISESOFT),
387 tex->noisebasis);
388
389 if (tex->stype == TEX_WALLOUT) {
390 texres->tin = 1.0f - texres->tin;
391 }
392
393 texres->tin = std::max(texres->tin, 0.0f);
394
395 return retval;
396}
397
398/* ------------------------------------------------------------------------- */
399/* newnoise: musgrave terrain noise types */
400
401static int mg_mFractalOrfBmTex(const Tex *tex, const float texvec[3], TexResult *texres)
402{
403 int rv = TEX_INT;
404 float (*mgravefunc)(float, float, float, float, float, float, int);
405
406 if (tex->stype == TEX_MFRACTAL) {
407 mgravefunc = BLI_noise_mg_multi_fractal;
408 }
409 else {
410 mgravefunc = BLI_noise_mg_fbm;
411 }
412
413 texres->tin = tex->ns_outscale * mgravefunc(texvec[0],
414 texvec[1],
415 texvec[2],
416 tex->mg_H,
417 tex->mg_lacunarity,
418 tex->mg_octaves,
419 tex->noisebasis);
420
421 BRICONT;
422
423 return rv;
424}
425
426static int mg_ridgedOrHybridMFTex(const Tex *tex, const float texvec[3], TexResult *texres)
427{
428 int rv = TEX_INT;
429 float (*mgravefunc)(float, float, float, float, float, float, float, float, int);
430
431 if (tex->stype == TEX_RIDGEDMF) {
433 }
434 else {
436 }
437
438 texres->tin = tex->ns_outscale * mgravefunc(texvec[0],
439 texvec[1],
440 texvec[2],
441 tex->mg_H,
442 tex->mg_lacunarity,
443 tex->mg_octaves,
444 tex->mg_offset,
445 tex->mg_gain,
446 tex->noisebasis);
447
448 BRICONT;
449
450 return rv;
451}
452
453static int mg_HTerrainTex(const Tex *tex, const float texvec[3], TexResult *texres)
454{
455 int rv = TEX_INT;
456
457 texres->tin = tex->ns_outscale * BLI_noise_mg_hetero_terrain(texvec[0],
458 texvec[1],
459 texvec[2],
460 tex->mg_H,
461 tex->mg_lacunarity,
462 tex->mg_octaves,
463 tex->mg_offset,
464 tex->noisebasis);
465
466 BRICONT;
467
468 return rv;
469}
470
471static int mg_distNoiseTex(const Tex *tex, const float texvec[3], TexResult *texres)
472{
473 int rv = TEX_INT;
474
476 texvec[0], texvec[1], texvec[2], tex->dist_amount, tex->noisebasis, tex->noisebasis2);
477
478 BRICONT;
479
480 return rv;
481}
482
483/* ------------------------------------------------------------------------- */
484/* newnoise: Voronoi texture type
485 *
486 * probably the slowest, especially with minkovsky, bump-mapping, could be done another way.
487 */
488
489static int voronoiTex(const Tex *tex, const float texvec[3], TexResult *texres)
490{
491 int rv = TEX_INT;
492 float da[4], pa[12]; /* distance and point coordinate arrays of 4 nearest neighbors */
493 float aw1 = fabsf(tex->vn_w1);
494 float aw2 = fabsf(tex->vn_w2);
495 float aw3 = fabsf(tex->vn_w3);
496 float aw4 = fabsf(tex->vn_w4);
497 float sc = (aw1 + aw2 + aw3 + aw4);
498 if (sc != 0.0f) {
499 sc = tex->ns_outscale / sc;
500 }
501
502 BLI_noise_voronoi(texvec[0], texvec[1], texvec[2], da, pa, tex->vn_mexp, tex->vn_distm);
503 texres->tin = sc * fabsf(dot_v4v4(&tex->vn_w1, da));
504
505 const bool is_color = ELEM(tex->vn_coltype, TEX_COL1, TEX_COL2, TEX_COL3);
506 if (is_color) {
507 float ca[3]; /* cell color */
508 BLI_noise_cell_v3(pa[0], pa[1], pa[2], ca);
509 texres->trgba[0] = aw1 * ca[0];
510 texres->trgba[1] = aw1 * ca[1];
511 texres->trgba[2] = aw1 * ca[2];
512 BLI_noise_cell_v3(pa[3], pa[4], pa[5], ca);
513 texres->trgba[0] += aw2 * ca[0];
514 texres->trgba[1] += aw2 * ca[1];
515 texres->trgba[2] += aw2 * ca[2];
516 BLI_noise_cell_v3(pa[6], pa[7], pa[8], ca);
517 texres->trgba[0] += aw3 * ca[0];
518 texres->trgba[1] += aw3 * ca[1];
519 texres->trgba[2] += aw3 * ca[2];
520 BLI_noise_cell_v3(pa[9], pa[10], pa[11], ca);
521 texres->trgba[0] += aw4 * ca[0];
522 texres->trgba[1] += aw4 * ca[1];
523 texres->trgba[2] += aw4 * ca[2];
524 if (ELEM(tex->vn_coltype, TEX_COL2, TEX_COL3)) {
525 float t1 = (da[1] - da[0]) * 10;
526 t1 = std::min<float>(t1, 1);
527 if (tex->vn_coltype == TEX_COL3) {
528 t1 *= texres->tin;
529 }
530 else {
531 t1 *= sc;
532 }
533 texres->trgba[0] *= t1;
534 texres->trgba[1] *= t1;
535 texres->trgba[2] *= t1;
536 }
537 else {
538 texres->trgba[0] *= sc;
539 texres->trgba[1] *= sc;
540 texres->trgba[2] *= sc;
541 }
542 }
543
544 if (is_color) {
546 texres->trgba[3] = 1.0;
547 return (rv | TEX_RGB);
548 }
549
550 BRICONT;
551
552 return rv;
553}
554
555/* ------------------------------------------------------------------------- */
556
557static int texnoise(const Tex *tex, TexResult *texres, int thread)
558{
559 float div = 3.0;
560 int val, ran, loop, shift = 29;
561
563
564 loop = tex->noisedepth;
565
566 /* start from top bits since they have more variance */
567 val = ((ran >> shift) & 3);
568
569 while (loop--) {
570 shift -= 2;
571 val *= ((ran >> shift) & 3);
572 div *= 3.0f;
573 }
574
575 texres->tin = float(val) / div;
576
577 BRICONT;
578 return TEX_INT;
579}
580
581/* ------------------------------------------------------------------------- */
582
583static int cubemap_glob(const float n[3], float x, float y, float z, float *adr1, float *adr2)
584{
585 float x1, y1, z1, nor[3];
586 int ret;
587
588 if (n == nullptr) {
589 nor[0] = x;
590 nor[1] = y;
591 nor[2] = z; /* use local render coord */
592 }
593 else {
594 copy_v3_v3(nor, n);
595 }
596
597 x1 = fabsf(nor[0]);
598 y1 = fabsf(nor[1]);
599 z1 = fabsf(nor[2]);
600
601 if (z1 >= x1 && z1 >= y1) {
602 *adr1 = (x + 1.0f) / 2.0f;
603 *adr2 = (y + 1.0f) / 2.0f;
604 ret = 0;
605 }
606 else if (y1 >= x1 && y1 >= z1) {
607 *adr1 = (x + 1.0f) / 2.0f;
608 *adr2 = (z + 1.0f) / 2.0f;
609 ret = 1;
610 }
611 else {
612 *adr1 = (y + 1.0f) / 2.0f;
613 *adr2 = (z + 1.0f) / 2.0f;
614 ret = 2;
615 }
616 return ret;
617}
618
619/* ------------------------------------------------------------------------- */
620
621static void do_2d_mapping(const MTex *mtex, float texvec[3], const float n[3])
622{
623 Tex *tex;
624 float fx, fy, fac1;
625 int wrap;
626
627 /* #MTex variables localized, only cube-map doesn't cooperate yet. */
628 wrap = mtex->mapping;
629 tex = mtex->tex;
630
631 {
632
633 if (wrap == MTEX_FLAT) {
634 fx = (texvec[0] + 1.0f) / 2.0f;
635 fy = (texvec[1] + 1.0f) / 2.0f;
636 }
637 else if (wrap == MTEX_TUBE) {
638 map_to_tube(&fx, &fy, texvec[0], texvec[1], texvec[2]);
639 }
640 else if (wrap == MTEX_SPHERE) {
641 map_to_sphere(&fx, &fy, texvec[0], texvec[1], texvec[2]);
642 }
643 else {
644 cubemap_glob(n, texvec[0], texvec[1], texvec[2], &fx, &fy);
645 }
646
647 /* repeat */
648 if (tex->extend == TEX_REPEAT) {
649 if (tex->xrepeat > 1) {
650 float origf = fx *= tex->xrepeat;
651
652 if (fx > 1.0f) {
653 fx -= int(fx);
654 }
655 else if (fx < 0.0f) {
656 fx += 1 - int(fx);
657 }
658
659 if (tex->flag & TEX_REPEAT_XMIR) {
660 int orig = int(floor(origf));
661 if (orig & 1) {
662 fx = 1.0f - fx;
663 }
664 }
665 }
666 if (tex->yrepeat > 1) {
667 float origf = fy *= tex->yrepeat;
668
669 if (fy > 1.0f) {
670 fy -= int(fy);
671 }
672 else if (fy < 0.0f) {
673 fy += 1 - int(fy);
674 }
675
676 if (tex->flag & TEX_REPEAT_YMIR) {
677 int orig = int(floor(origf));
678 if (orig & 1) {
679 fy = 1.0f - fy;
680 }
681 }
682 }
683 }
684 /* crop */
685 if (tex->cropxmin != 0.0f || tex->cropxmax != 1.0f) {
686 fac1 = tex->cropxmax - tex->cropxmin;
687 fx = tex->cropxmin + fx * fac1;
688 }
689 if (tex->cropymin != 0.0f || tex->cropymax != 1.0f) {
690 fac1 = tex->cropymax - tex->cropymin;
691 fy = tex->cropymin + fy * fac1;
692 }
693
694 texvec[0] = fx;
695 texvec[1] = fy;
696 }
697}
698
699/* ************************************** */
700
701static int multitex(Tex *tex,
702 const float texvec[3],
703 TexResult *texres,
704 const short thread,
705 const short which_output,
706 ImagePool *pool,
707 const bool skip_load_image,
708 const bool texnode_preview,
709 const bool use_nodes)
710{
711 float tmpvec[3];
712 int retval = 0; /* return value, TEX_INT or TEX_RGB. */
713
714 texres->talpha = false; /* Is set when image texture returns alpha (considered pre-multiplied) */
715
716 if (use_nodes && tex->use_nodes && tex->nodetree) {
717 const float cfra = 1.0f; /* This was only set for Blender Internal render before. */
718 retval = ntreeTexExecTree(
719 tex->nodetree, texres, texvec, thread, tex, which_output, cfra, texnode_preview, nullptr);
720 }
721 else {
722 switch (tex->type) {
723 case 0:
724 texres->tin = 0.0f;
725 return 0;
726 case TEX_CLOUDS:
727 retval = clouds(tex, texvec, texres);
728 break;
729 case TEX_WOOD:
730 retval = wood(tex, texvec, texres);
731 break;
732 case TEX_MARBLE:
733 retval = marble(tex, texvec, texres);
734 break;
735 case TEX_MAGIC:
736 retval = magic(tex, texvec, texres);
737 break;
738 case TEX_BLEND:
739 retval = blend(tex, texvec, texres);
740 break;
741 case TEX_STUCCI:
742 retval = stucci(tex, texvec, texres);
743 break;
744 case TEX_NOISE:
745 retval = texnoise(tex, texres, thread);
746 break;
747 case TEX_IMAGE:
748 retval = imagewrap(tex, tex->ima, texvec, texres, pool, skip_load_image);
749 if (tex->ima) {
751 }
752 break;
753 case TEX_MUSGRAVE:
754 /* newnoise: musgrave types */
755
756 /* NOTE(@ton): added this, for Blender convention reason.
757 * NOTE(@artificer): added the use of tmpvec to avoid scaling texvec. */
758 copy_v3_v3(tmpvec, texvec);
759 mul_v3_fl(tmpvec, 1.0f / tex->noisesize);
760
761 switch (tex->stype) {
762 case TEX_MFRACTAL:
763 case TEX_FBM:
764 retval = mg_mFractalOrfBmTex(tex, tmpvec, texres);
765 break;
766 case TEX_RIDGEDMF:
767 case TEX_HYBRIDMF:
768 retval = mg_ridgedOrHybridMFTex(tex, tmpvec, texres);
769 break;
770 case TEX_HTERRAIN:
771 retval = mg_HTerrainTex(tex, tmpvec, texres);
772 break;
773 }
774 break;
775 /* newnoise: voronoi type */
776 case TEX_VORONOI:
777 /* NOTE(@ton): added this, for Blender convention reason.
778 * NOTE(@artificer): added the use of tmpvec to avoid scaling texvec. */
779 copy_v3_v3(tmpvec, texvec);
780 mul_v3_fl(tmpvec, 1.0f / tex->noisesize);
781
782 retval = voronoiTex(tex, tmpvec, texres);
783 break;
784 case TEX_DISTNOISE:
785 /* NOTE(@ton): added this, for Blender convention reason.
786 * NOTE(@artificer): added the use of tmpvec to avoid scaling texvec. */
787 copy_v3_v3(tmpvec, texvec);
788 mul_v3_fl(tmpvec, 1.0f / tex->noisesize);
789
790 retval = mg_distNoiseTex(tex, tmpvec, texres);
791 break;
792 }
793 }
794
795 if (tex->flag & TEX_COLORBAND) {
796 float col[4];
797 if (BKE_colorband_evaluate(tex->coba, texres->tin, col)) {
798 texres->talpha = true;
799 copy_v4_v4(texres->trgba, col);
800 retval |= TEX_RGB;
801 }
802 }
803 return retval;
804}
805
807 const float texvec[3],
808 TexResult *texres,
809 const short thread,
810 short which_output,
811 const MTex *mtex,
812 ImagePool *pool,
813 const bool scene_color_manage,
814 const bool skip_load_image,
815 const bool texnode_preview,
816 const bool use_nodes)
817{
818 if (tex == nullptr) {
819 memset(texres, 0, sizeof(TexResult));
820 return 0;
821 }
822
823 if (mtex) {
824 which_output = mtex->which_output;
825 }
826
827 if (tex->type == TEX_IMAGE) {
828 int retval;
829
830 if (mtex) {
831 float texvec_l[3];
832 copy_v3_v3(texvec_l, texvec);
833 /* we have mtex, use it for 2d mapping images only */
834 do_2d_mapping(mtex, texvec_l, nullptr);
835 retval = multitex(tex,
836 texvec_l,
837 texres,
838 thread,
839 which_output,
840 pool,
841 skip_load_image,
842 texnode_preview,
843 use_nodes);
844
845 if (mtex->mapto & MAP_COL) {
846 ImBuf *ibuf = BKE_image_pool_acquire_ibuf(tex->ima, &tex->iuser, pool);
847
848 /* don't linearize float buffers, assumed to be linear */
849 if (ibuf != nullptr && ibuf->float_buffer.data == nullptr && (retval & TEX_RGB) &&
850 scene_color_manage)
851 {
853 ibuf->byte_buffer.colorspace);
854 }
855
856 BKE_image_pool_release_ibuf(tex->ima, ibuf, pool);
857 }
858 }
859 else {
860 /* we don't have mtex, do default flat 2d projection */
861 MTex localmtex;
862 float texvec_l[3];
863
864 localmtex.mapping = MTEX_FLAT;
865 localmtex.tex = tex;
866 localmtex.object = nullptr;
867 localmtex.texco = TEXCO_ORCO;
868
869 copy_v3_v3(texvec_l, texvec);
870
871 do_2d_mapping(&localmtex, texvec_l, nullptr);
872 retval = multitex(tex,
873 texvec_l,
874 texres,
875 thread,
876 which_output,
877 pool,
878 skip_load_image,
879 texnode_preview,
880 use_nodes);
881
882 {
883 ImBuf *ibuf = BKE_image_pool_acquire_ibuf(tex->ima, &tex->iuser, pool);
884
885 /* don't linearize float buffers, assumed to be linear */
886 if (ibuf != nullptr && ibuf->float_buffer.data == nullptr && (retval & TEX_RGB) &&
887 scene_color_manage)
888 {
890 ibuf->byte_buffer.colorspace);
891 }
892
893 BKE_image_pool_release_ibuf(tex->ima, ibuf, pool);
894 }
895 }
896
897 return retval;
898 }
899
900 return multitex(tex,
901 texvec,
902 texres,
903 thread,
904 which_output,
905 pool,
906 skip_load_image,
907 texnode_preview,
908 use_nodes);
909}
910
912 const float texvec[3],
913 TexResult *texres,
914 const short thread,
915 short which_output,
916 const MTex *mtex,
917 ImagePool *pool)
918{
920 tex, texvec, texres, thread, which_output, mtex, pool, true, false, false, true);
921}
922
924 const float texvec[3],
925 TexResult *texres,
926 const short thread,
927 ImagePool *pool,
928 bool scene_color_manage,
929 const bool skip_load_image)
930{
931 return multitex_nodes_intern(tex,
932 texvec,
933 texres,
934 thread,
935 0,
936 nullptr,
937 pool,
938 scene_color_manage,
939 skip_load_image,
940 false,
941 true);
942}
943
945 const float texvec[3],
946 TexResult *texres,
947 ImagePool *pool,
948 bool scene_color_manage,
949 const bool skip_load_image)
950{
952 tex, texvec, texres, 0, 0, nullptr, pool, scene_color_manage, skip_load_image, false, false);
953}
954
955/* ------------------------------------------------------------------------- */
956
957float texture_value_blend(float tex, float out, float fact, float facg, int blendtype)
958{
959 float in = 0.0, facm, scf;
960 int flip = (facg < 0.0f);
961
962 facg = fabsf(facg);
963
964 fact *= facg;
965 facm = 1.0f - fact;
966 if (flip) {
967 std::swap(fact, facm);
968 }
969
970 switch (blendtype) {
971 case MTEX_BLEND:
972 in = fact * tex + facm * out;
973 break;
974
975 case MTEX_MUL:
976 facm = 1.0f - facg;
977 in = (facm + fact * tex) * out;
978 break;
979
980 case MTEX_SCREEN:
981 facm = 1.0f - facg;
982 in = 1.0f - (facm + fact * (1.0f - tex)) * (1.0f - out);
983 break;
984
985 case MTEX_OVERLAY:
986 facm = 1.0f - facg;
987 if (out < 0.5f) {
988 in = out * (facm + 2.0f * fact * tex);
989 }
990 else {
991 in = 1.0f - (facm + 2.0f * fact * (1.0f - tex)) * (1.0f - out);
992 }
993 break;
994
995 case MTEX_SUB:
996 fact = -fact;
998 case MTEX_ADD:
999 in = fact * tex + out;
1000 break;
1001
1002 case MTEX_DIV:
1003 if (tex != 0.0f) {
1004 in = facm * out + fact * out / tex;
1005 }
1006 break;
1007
1008 case MTEX_DIFF:
1009 in = facm * out + fact * fabsf(tex - out);
1010 break;
1011
1012 case MTEX_DARK:
1013 in = min_ff(out, tex) * fact + out * facm;
1014 break;
1015
1016 case MTEX_LIGHT:
1017 in = max_ff(out, tex) * fact + out * facm;
1018 break;
1019
1020 case MTEX_SOFT_LIGHT:
1021 scf = 1.0f - (1.0f - tex) * (1.0f - out);
1022 in = facm * out + fact * ((1.0f - out) * tex * out) + (out * scf);
1023 break;
1024
1025 case MTEX_LIN_LIGHT:
1026 if (tex > 0.5f) {
1027 in = out + fact * (2.0f * (tex - 0.5f));
1028 }
1029 else {
1030 in = out + fact * (2.0f * tex - 1.0f);
1031 }
1032 break;
1033 }
1034
1035 return in;
1036}
1037
1038/* ------------------------------------------------------------------------- */
1039
1040bool RE_texture_evaluate(const MTex *mtex,
1041 const float vec[3],
1042 const int thread,
1043 ImagePool *pool,
1044 const bool skip_load_image,
1045 const bool texnode_preview,
1046 /* Return arguments. */
1047 float *r_intensity,
1048 float r_rgba[4])
1049{
1050 Tex *tex;
1051 TexResult texr;
1052 float texvec[3];
1053 int rgb;
1054
1055 tex = mtex->tex;
1056 if (tex == nullptr) {
1057 return false;
1058 }
1059
1060 /* placement */
1061 if (mtex->projx) {
1062 texvec[0] = mtex->size[0] * (vec[mtex->projx - 1] + mtex->ofs[0]);
1063 }
1064 else {
1065 texvec[0] = mtex->size[0] * (mtex->ofs[0]);
1066 }
1067
1068 if (mtex->projy) {
1069 texvec[1] = mtex->size[1] * (vec[mtex->projy - 1] + mtex->ofs[1]);
1070 }
1071 else {
1072 texvec[1] = mtex->size[1] * (mtex->ofs[1]);
1073 }
1074
1075 if (mtex->projz) {
1076 texvec[2] = mtex->size[2] * (vec[mtex->projz - 1] + mtex->ofs[2]);
1077 }
1078 else {
1079 texvec[2] = mtex->size[2] * (mtex->ofs[2]);
1080 }
1081
1082 /* texture */
1083 if (tex->type == TEX_IMAGE) {
1084 do_2d_mapping(mtex, texvec, nullptr);
1085 }
1086
1087 rgb = multitex(tex,
1088 texvec,
1089 &texr,
1090 thread,
1091 mtex->which_output,
1092 pool,
1093 skip_load_image,
1094 texnode_preview,
1095 true);
1096
1097 if (rgb) {
1099 }
1100 else {
1101 copy_v3_fl3(texr.trgba, mtex->r, mtex->g, mtex->b);
1102 }
1103
1104 *r_intensity = texr.tin;
1105 copy_v4_v4(r_rgba, texr.trgba);
1106
1107 return (rgb != 0);
1108}
bool BKE_colorband_evaluate(const ColorBand *coba, float in, float out[4])
Definition colorband.cc:396
void BKE_image_tag_time(Image *ima)
ImBuf * BKE_image_pool_acquire_ibuf(Image *ima, ImageUser *iuser, ImagePool *pool)
void BKE_image_pool_release_ibuf(Image *ima, ImBuf *ibuf, ImagePool *pool)
#define ATTR_FALLTHROUGH
MINLINE float max_ff(float a, float b)
MINLINE float min_ff(float a, float b)
#define M_PI
bool map_to_sphere(float *r_u, float *r_v, float x, float y, float z)
bool map_to_tube(float *r_u, float *r_v, float x, float y, float z)
MINLINE void copy_v4_v4(float r[4], const float a[4])
MINLINE void mul_v3_fl(float r[3], float f)
MINLINE void copy_v3_v3(float r[3], const float a[3])
MINLINE float dot_v4v4(const float a[4], const float b[4]) ATTR_WARN_UNUSED_RESULT
MINLINE void copy_v3_fl3(float v[3], float x, float y, float z)
float BLI_noise_mg_hetero_terrain(float x, float y, float z, float H, float lacunarity, float octaves, float offset, int noisebasis)
Definition noise_c.cc:1391
float BLI_noise_mg_multi_fractal(float x, float y, float z, float H, float lacunarity, float octaves, int noisebasis)
Definition noise_c.cc:1332
float BLI_noise_mg_ridged_multi_fractal(float x, float y, float z, float H, float lacunarity, float octaves, float offset, float gain, int noisebasis)
Definition noise_c.cc:1535
float BLI_noise_mg_variable_lacunarity(float x, float y, float z, float distortion, int nbas1, int nbas2)
Definition noise_c.cc:1606
float BLI_noise_generic_noise(float noisesize, float x, float y, float z, bool hard, int noisebasis)
Definition noise_c.cc:1153
void BLI_noise_voronoi(float x, float y, float z, float *da, float *pa, float me, int dtype)
Definition noise_c.cc:915
float BLI_noise_mg_fbm(float x, float y, float z, float H, float lacunarity, float octaves, int noisebasis)
Definition noise_c.cc:1272
void BLI_noise_cell_v3(float x, float y, float z, float r_ca[3])
Definition noise_c.cc:1131
float BLI_noise_mg_hybrid_multi_fractal(float x, float y, float z, float H, float lacunarity, float octaves, float offset, float gain, int noisebasis)
Definition noise_c.cc:1461
float BLI_noise_generic_turbulence(float noisesize, float x, float y, float z, int oct, bool hard, int noisebasis)
Definition noise_c.cc:1210
Random number functions.
void BLI_rng_threaded_free(struct RNG_THREAD_ARRAY *rngarr) ATTR_NONNULL(1)
Definition rand.cc:190
RNG_THREAD_ARRAY * BLI_rng_threaded_new(void)
Definition rand.cc:178
int BLI_rng_thread_rand(RNG_THREAD_ARRAY *rngarr, int thread) ATTR_WARN_UNUSED_RESULT
Definition rand.cc:195
#define ELEM(...)
@ TEXCO_ORCO
@ TEX_NOISESOFT
@ MTEX_DIFF
@ MTEX_DIV
@ MTEX_MUL
@ MTEX_DARK
@ MTEX_LIGHT
@ MTEX_BLEND
@ MTEX_SOFT_LIGHT
@ MTEX_LIN_LIGHT
@ MTEX_SUB
@ MTEX_ADD
@ MTEX_OVERLAY
@ MTEX_SCREEN
@ TEX_INT
@ TEX_RGB
@ TEX_SHARPER
@ TEX_SOFT
@ TEX_SHARP
@ TEX_SIN
@ TEX_TRI
@ TEX_REPEAT
@ TEX_BANDNOISE
@ TEX_RINGNOISE
@ TEX_RING
@ TEX_BAND
@ TEX_DIAG
@ TEX_EASE
@ TEX_HALO
@ TEX_QUAD
@ TEX_RAD
@ TEX_LIN
@ TEX_BLEND
@ TEX_MARBLE
@ TEX_NOISE
@ TEX_IMAGE
@ TEX_WOOD
@ TEX_CLOUDS
@ TEX_DISTNOISE
@ TEX_VORONOI
@ TEX_STUCCI
@ TEX_MAGIC
@ TEX_MUSGRAVE
@ TEX_HYBRIDMF
@ TEX_MFRACTAL
@ TEX_HTERRAIN
@ TEX_FBM
@ TEX_RIDGEDMF
@ TEX_COL2
@ TEX_COL1
@ TEX_COL3
@ TEX_WALLOUT
@ TEX_REPEAT_YMIR
@ TEX_FLIPBLEND
@ TEX_COLORBAND
@ TEX_REPEAT_XMIR
@ TEX_COLOR
@ MTEX_FLAT
@ MTEX_SPHERE
@ MTEX_TUBE
void IMB_colormanagement_colorspace_to_scene_linear_v3(float pixel[3], const ColorSpace *colorspace)
BLI_INLINE float IMB_colormanagement_get_luminance(const float rgb[3])
int ntreeTexExecTree(struct bNodeTree *ntree, struct TexResult *target, const float co[3], short thread, const struct Tex *tex, short which_output, int cfra, int preview, struct MTex *mtex)
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
nullptr float
uint nor
uint col
#define in
#define out
#define floor
static float turb(float x, float y, float z, int oct, int hard, int nb, float ampscale, float freqscale)
float wrap(float value, float max, float min)
Definition node_math.h:103
return ret
#define floorf
#define fabsf
#define sqrtf
#define sinf
#define cosf
#define atan2f
const ColorSpace * colorspace
ImBufFloatBuffer float_buffer
ImBufByteBuffer byte_buffer
short texco
short which_output
float ofs[3]
short mapto
struct Object * object
float size[3]
struct Tex * tex
float tin
Definition RE_texture.h:59
float trgba[4]
Definition RE_texture.h:60
float cropymin
float dist_amount
float noisesize
short xrepeat
float vn_w4
short noisedepth
float ns_outscale
short noisetype
float vn_w2
float cropxmax
float cropymax
float mg_lacunarity
float mg_offset
char use_nodes
float vn_mexp
float mg_gain
short noisebasis2
short vn_coltype
short stype
float mg_octaves
struct ImageUser iuser
struct ColorBand * coba
short vn_distm
float cropxmin
float mg_H
float vn_w3
struct bNodeTree * nodetree
short noisebasis
float vn_w1
struct Image * ima
short extend
short yrepeat
float turbul
#define BRICONT
#define BRICONTRGB
int imagewrap(struct Tex *tex, struct Image *ima, const float texvec[3], struct TexResult *texres, struct ImagePool *pool, bool skip_load_image)
static float tex_sin(float a)
static int mg_mFractalOrfBmTex(const Tex *tex, const float texvec[3], TexResult *texres)
static int marble(const Tex *tex, const float texvec[3], TexResult *texres)
static int stucci(const Tex *tex, const float texvec[3], TexResult *texres)
static int voronoiTex(const Tex *tex, const float texvec[3], TexResult *texres)
static RNG_THREAD_ARRAY * random_tex_array
static float tex_saw(float a)
static int multitex_nodes_intern(Tex *tex, const float texvec[3], TexResult *texres, const short thread, short which_output, const MTex *mtex, ImagePool *pool, const bool scene_color_manage, const bool skip_load_image, const bool texnode_preview, const bool use_nodes)
static int mg_distNoiseTex(const Tex *tex, const float texvec[3], TexResult *texres)
static int multitex(Tex *tex, const float texvec[3], TexResult *texres, const short thread, const short which_output, ImagePool *pool, const bool skip_load_image, const bool texnode_preview, const bool use_nodes)
static float wood_int(const Tex *tex, float x, float y, float z)
static int mg_ridgedOrHybridMFTex(const Tex *tex, const float texvec[3], TexResult *texres)
static int wood(const Tex *tex, const float texvec[3], TexResult *texres)
void RE_texture_rng_init()
static int mg_HTerrainTex(const Tex *tex, const float texvec[3], TexResult *texres)
bool RE_texture_evaluate(const MTex *mtex, const float vec[3], const int thread, ImagePool *pool, const bool skip_load_image, const bool texnode_preview, float *r_intensity, float r_rgba[4])
static void do_2d_mapping(const MTex *mtex, float texvec[3], const float n[3])
static int cubemap_glob(const float n[3], float x, float y, float z, float *adr1, float *adr2)
static int magic(const Tex *tex, const float texvec[3], TexResult *texres)
int multitex_ext(Tex *tex, const float texvec[3], TexResult *texres, const short thread, ImagePool *pool, bool scene_color_manage, const bool skip_load_image)
int multitex_nodes(Tex *tex, const float texvec[3], TexResult *texres, const short thread, short which_output, const MTex *mtex, ImagePool *pool)
static int blend(const Tex *tex, const float texvec[3], TexResult *texres)
static float marble_int(const Tex *tex, float x, float y, float z)
static int texnoise(const Tex *tex, TexResult *texres, int thread)
void RE_texture_rng_exit()
int multitex_ext_safe(Tex *tex, const float texvec[3], TexResult *texres, ImagePool *pool, bool scene_color_manage, const bool skip_load_image)
float texture_value_blend(float tex, float out, float fact, float facg, int blendtype)
static float tex_tri(float a)
static int clouds(const Tex *tex, const float texvec[3], TexResult *texres)