Blender V5.3
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_c.hh"
15#include "BLI_noise_c.hh"
16#include "BLI_rand_c.hh"
17#include "BLI_utildefines.hh"
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
35namespace blender {
36
38
43
45{
46 if (random_tex_array == nullptr) {
47 return;
48 }
50 random_tex_array = nullptr;
51}
52
53/* ------------------------------------------------------------------------- */
54
55static int blend(const Tex *tex, const float texvec[3], TexResult *texres)
56{
57 float x, y, t;
58
59 if (tex->flag & TEX_FLIPBLEND) {
60 x = texvec[1];
61 y = texvec[0];
62 }
63 else {
64 x = texvec[0];
65 y = texvec[1];
66 }
67
68 if (tex->stype == TEX_LIN) { /* Linear. */
69 texres->tin = (1.0f + x) / 2.0f;
70 }
71 else if (tex->stype == TEX_QUAD) { /* Quadratic. */
72 texres->tin = (1.0f + x) / 2.0f;
73 if (texres->tin < 0.0f) {
74 texres->tin = 0.0f;
75 }
76 else {
77 texres->tin *= texres->tin;
78 }
79 }
80 else if (tex->stype == TEX_EASE) { /* Ease. */
81 texres->tin = (1.0f + x) / 2.0f;
82 if (texres->tin <= 0.0f) {
83 texres->tin = 0.0f;
84 }
85 else if (texres->tin >= 1.0f) {
86 texres->tin = 1.0f;
87 }
88 else {
89 t = texres->tin * texres->tin;
90 texres->tin = (3.0f * t - 2.0f * t * texres->tin);
91 }
92 }
93 else if (tex->stype == TEX_DIAG) { /* Diagonal. */
94 texres->tin = (2.0f + x + y) / 4.0f;
95 }
96 else if (tex->stype == TEX_RAD) { /* Radial. */
97 texres->tin = (atan2f(y, x) / float(2 * M_PI) + 0.5f);
98 }
99 else { /* sphere TEX_SPHERE */
100 texres->tin = 1.0f - sqrtf(x * x + y * y + texvec[2] * texvec[2]);
101 texres->tin = std::max(texres->tin, 0.0f);
102 if (tex->stype == TEX_HALO) {
103 texres->tin *= texres->tin; /* Halo. */
104 }
105 }
106
107 BRICONT;
108
109 return TEX_INT;
110}
111
112/* ------------------------------------------------------------------------- */
113/* ************************************************************************* */
114
115/* newnoise: all noise-based types now have different noise-bases to choose from. */
116
117static int clouds(const Tex *tex, const float texvec[3], TexResult *texres)
118{
119 int rv = TEX_INT;
120
122 texvec[0],
123 texvec[1],
124 texvec[2],
125 tex->noisedepth,
126 (tex->noisetype != TEX_NOISESOFT),
127 tex->noisebasis);
128
129 if (tex->stype == TEX_COLOR) {
130 texres->trgba[0] = texres->tin;
132 texvec[1],
133 texvec[0],
134 texvec[2],
135 tex->noisedepth,
136 (tex->noisetype != TEX_NOISESOFT),
137 tex->noisebasis);
139 texvec[1],
140 texvec[2],
141 texvec[0],
142 tex->noisedepth,
143 (tex->noisetype != TEX_NOISESOFT),
144 tex->noisebasis);
146 texres->trgba[3] = 1.0;
147 return (rv | TEX_RGB);
148 }
149
150 BRICONT;
151
152 return rv;
153}
154
155/* creates a sine wave */
156static float tex_sin(float a)
157{
158 a = 0.5f + 0.5f * sinf(a);
159
160 return a;
161}
162
163/* creates a saw wave */
164static float tex_saw(float a)
165{
166 const float b = 2 * M_PI;
167
168 int n = int(a / b);
169 a -= n * b;
170 if (a < 0) {
171 a += b;
172 }
173 return a / b;
174}
175
176/* creates a triangle wave */
177static float tex_tri(float a)
178{
179 const float b = 2 * M_PI;
180 const float rmax = 1.0;
181
182 a = rmax - 2.0f * fabsf(floorf((a * (1.0f / b)) + 0.5f) - (a * (1.0f / b)));
183
184 return a;
185}
186
187/* computes basic wood intensity value at x,y,z */
188static float wood_int(const Tex *tex, float x, float y, float z)
189{
190 float wi = 0;
191 /* wave form: TEX_SIN=0, TEX_SAW=1, TEX_TRI=2. */
192 short wf = tex->noisebasis2;
193 /* wood type: TEX_BAND=0, TEX_RING=1, TEX_BANDNOISE=2, TEX_RINGNOISE=3. */
194 short wt = tex->stype;
195
196 float (*waveform[3])(float); /* create array of pointers to waveform functions */
197 waveform[0] = tex_sin; /* assign address of tex_sin() function to pointer array */
198 waveform[1] = tex_saw;
199 waveform[2] = tex_tri;
200
201 if ((wf > TEX_TRI) || (wf < TEX_SIN)) {
202 wf = 0; /* check to be sure noisebasis2 is initialized ahead of time */
203 }
204
205 if (wt == TEX_BAND) {
206 wi = waveform[wf]((x + y + z) * 10.0f);
207 }
208 else if (wt == TEX_RING) {
209 wi = waveform[wf](sqrtf(x * x + y * y + z * z) * 20.0f);
210 }
211 else if (wt == TEX_BANDNOISE) {
212 wi = tex->turbul *
214 tex->noisesize, x, y, z, (tex->noisetype != TEX_NOISESOFT), tex->noisebasis);
215 wi = waveform[wf]((x + y + z) * 10.0f + wi);
216 }
217 else if (wt == TEX_RINGNOISE) {
218 wi = tex->turbul *
220 tex->noisesize, x, y, z, (tex->noisetype != TEX_NOISESOFT), tex->noisebasis);
221 wi = waveform[wf](sqrtf(x * x + y * y + z * z) * 20.0f + wi);
222 }
223
224 return wi;
225}
226
227static int wood(const Tex *tex, const float texvec[3], TexResult *texres)
228{
229 int rv = TEX_INT;
230
231 texres->tin = wood_int(tex, texvec[0], texvec[1], texvec[2]);
232
233 BRICONT;
234
235 return rv;
236}
237
238/* computes basic marble intensity at x,y,z */
239static float marble_int(const Tex *tex, float x, float y, float z)
240{
241 float n, mi;
242 short wf = tex->noisebasis2; /* wave form: TEX_SIN=0, TEX_SAW=1, TEX_TRI=2 */
243 short mt = tex->stype; /* marble type: TEX_SOFT=0, TEX_SHARP=1, TEX_SHAPER=2 */
244
245 float (*waveform[3])(float); /* create array of pointers to waveform functions */
246 waveform[0] = tex_sin; /* assign address of tex_sin() function to pointer array */
247 waveform[1] = tex_saw;
248 waveform[2] = tex_tri;
249
250 if ((wf > TEX_TRI) || (wf < TEX_SIN)) {
251 wf = 0; /* check to be sure noisebasis2 isn't initialized ahead of time */
252 }
253
254 n = 5.0f * (x + y + z);
255
256 mi = n + tex->turbul * BLI_noise_generic_turbulence(tex->noisesize,
257 x,
258 y,
259 z,
260 tex->noisedepth,
261 (tex->noisetype != TEX_NOISESOFT),
262 tex->noisebasis);
263
264 if (mt >= TEX_SOFT) { /* TEX_SOFT always true */
265 mi = waveform[wf](mi);
266 if (mt == TEX_SHARP) {
267 mi = sqrtf(mi);
268 }
269 else if (mt == TEX_SHARPER) {
270 mi = sqrtf(sqrtf(mi));
271 }
272 }
273
274 return mi;
275}
276
277static int marble(const Tex *tex, const float texvec[3], TexResult *texres)
278{
279 int rv = TEX_INT;
280
281 texres->tin = marble_int(tex, texvec[0], texvec[1], texvec[2]);
282
283 BRICONT;
284
285 return rv;
286}
287
288/* ------------------------------------------------------------------------- */
289
290static int magic(const Tex *tex, const float texvec[3], TexResult *texres)
291{
292 float x, y, z, turb;
293 int n;
294
295 n = tex->noisedepth;
296 turb = tex->turbul / 5.0f;
297
298 x = sinf((texvec[0] + texvec[1] + texvec[2]) * 5.0f);
299 y = cosf((-texvec[0] + texvec[1] - texvec[2]) * 5.0f);
300 z = -cosf((-texvec[0] - texvec[1] + texvec[2]) * 5.0f);
301 if (n > 0) {
302 x *= turb;
303 y *= turb;
304 z *= turb;
305 y = -cosf(x - y + z);
306 y *= turb;
307 if (n > 1) {
308 x = cosf(x - y - z);
309 x *= turb;
310 if (n > 2) {
311 z = sinf(-x - y - z);
312 z *= turb;
313 if (n > 3) {
314 x = -cosf(-x + y - z);
315 x *= turb;
316 if (n > 4) {
317 y = -sinf(-x + y + z);
318 y *= turb;
319 if (n > 5) {
320 y = -cosf(-x + y + z);
321 y *= turb;
322 if (n > 6) {
323 x = cosf(x + y + z);
324 x *= turb;
325 if (n > 7) {
326 z = sinf(x + y - z);
327 z *= turb;
328 if (n > 8) {
329 x = -cosf(-x - y + z);
330 x *= turb;
331 if (n > 9) {
332 y = -sinf(x - y + z);
333 y *= turb;
334 }
335 }
336 }
337 }
338 }
339 }
340 }
341 }
342 }
343 }
344
345 if (turb != 0.0f) {
346 turb *= 2.0f;
347 x /= turb;
348 y /= turb;
349 z /= turb;
350 }
351 texres->trgba[0] = 0.5f - x;
352 texres->trgba[1] = 0.5f - y;
353 texres->trgba[2] = 0.5f - z;
354
355 texres->tin = (1.0f / 3.0f) * (texres->trgba[0] + texres->trgba[1] + texres->trgba[2]);
356
358 texres->trgba[3] = 1.0f;
359
360 return TEX_RGB;
361}
362
363/* ------------------------------------------------------------------------- */
364
365/* newnoise: stucci also modified to use different noisebasis */
366static int stucci(const Tex *tex, const float texvec[3], TexResult *texres)
367{
368 float b2, ofs;
369 int retval = TEX_INT;
370
372 texvec[0],
373 texvec[1],
374 texvec[2],
375 (tex->noisetype != TEX_NOISESOFT),
376 tex->noisebasis);
377
378 ofs = tex->turbul / 200.0f;
379
380 if (tex->stype) {
381 ofs *= (b2 * b2);
382 }
383
385 texvec[0],
386 texvec[1],
387 texvec[2] + ofs,
388 (tex->noisetype != TEX_NOISESOFT),
389 tex->noisebasis);
390
391 if (tex->stype == TEX_WALLOUT) {
392 texres->tin = 1.0f - texres->tin;
393 }
394
395 texres->tin = std::max(texres->tin, 0.0f);
396
397 return retval;
398}
399
400/* ------------------------------------------------------------------------- */
401/* newnoise: musgrave terrain noise types */
402
403static int mg_mFractalOrfBmTex(const Tex *tex, const float texvec[3], TexResult *texres)
404{
405 int rv = TEX_INT;
406 float (*mgravefunc)(float, float, float, float, float, float, int);
407
408 if (tex->stype == TEX_MFRACTAL) {
409 mgravefunc = BLI_noise_mg_multi_fractal;
410 }
411 else {
412 mgravefunc = BLI_noise_mg_fbm;
413 }
414
415 texres->tin = tex->ns_outscale * mgravefunc(texvec[0],
416 texvec[1],
417 texvec[2],
418 tex->mg_H,
419 tex->mg_lacunarity,
420 tex->mg_octaves,
421 tex->noisebasis);
422
423 BRICONT;
424
425 return rv;
426}
427
428static int mg_ridgedOrHybridMFTex(const Tex *tex, const float texvec[3], TexResult *texres)
429{
430 int rv = TEX_INT;
431 float (*mgravefunc)(float, float, float, float, float, float, float, float, int);
432
433 if (tex->stype == TEX_RIDGEDMF) {
435 }
436 else {
438 }
439
440 texres->tin = tex->ns_outscale * mgravefunc(texvec[0],
441 texvec[1],
442 texvec[2],
443 tex->mg_H,
444 tex->mg_lacunarity,
445 tex->mg_octaves,
446 tex->mg_offset,
447 tex->mg_gain,
448 tex->noisebasis);
449
450 BRICONT;
451
452 return rv;
453}
454
455static int mg_HTerrainTex(const Tex *tex, const float texvec[3], TexResult *texres)
456{
457 int rv = TEX_INT;
458
459 texres->tin = tex->ns_outscale * BLI_noise_mg_hetero_terrain(texvec[0],
460 texvec[1],
461 texvec[2],
462 tex->mg_H,
463 tex->mg_lacunarity,
464 tex->mg_octaves,
465 tex->mg_offset,
466 tex->noisebasis);
467
468 BRICONT;
469
470 return rv;
471}
472
473static int mg_distNoiseTex(const Tex *tex, const float texvec[3], TexResult *texres)
474{
475 int rv = TEX_INT;
476
478 texvec[0], texvec[1], texvec[2], tex->dist_amount, tex->noisebasis, tex->noisebasis2);
479
480 BRICONT;
481
482 return rv;
483}
484
485/* ------------------------------------------------------------------------- */
486/* newnoise: Voronoi texture type
487 *
488 * probably the slowest, especially with minkovsky, bump-mapping, could be done another way.
489 */
490
491static int voronoiTex(const Tex *tex, const float texvec[3], TexResult *texres)
492{
493 int rv = TEX_INT;
494 float da[4], pa[12]; /* distance and point coordinate arrays of 4 nearest neighbors */
495 float aw1 = fabsf(tex->vn_w1);
496 float aw2 = fabsf(tex->vn_w2);
497 float aw3 = fabsf(tex->vn_w3);
498 float aw4 = fabsf(tex->vn_w4);
499 float sc = (aw1 + aw2 + aw3 + aw4);
500 if (sc != 0.0f) {
501 sc = tex->ns_outscale / sc;
502 }
503
504 BLI_noise_voronoi(texvec[0], texvec[1], texvec[2], da, pa, tex->vn_mexp, tex->vn_distm);
505 texres->tin = sc * fabsf(dot_v4v4(&tex->vn_w1, da));
506
507 const bool is_color = ELEM(tex->vn_coltype, TEX_COL1, TEX_COL2, TEX_COL3);
508 if (is_color) {
509 float ca[3]; /* cell color */
510 BLI_noise_cell_v3(pa[0], pa[1], pa[2], ca);
511 texres->trgba[0] = aw1 * ca[0];
512 texres->trgba[1] = aw1 * ca[1];
513 texres->trgba[2] = aw1 * ca[2];
514 BLI_noise_cell_v3(pa[3], pa[4], pa[5], ca);
515 texres->trgba[0] += aw2 * ca[0];
516 texres->trgba[1] += aw2 * ca[1];
517 texres->trgba[2] += aw2 * ca[2];
518 BLI_noise_cell_v3(pa[6], pa[7], pa[8], ca);
519 texres->trgba[0] += aw3 * ca[0];
520 texres->trgba[1] += aw3 * ca[1];
521 texres->trgba[2] += aw3 * ca[2];
522 BLI_noise_cell_v3(pa[9], pa[10], pa[11], ca);
523 texres->trgba[0] += aw4 * ca[0];
524 texres->trgba[1] += aw4 * ca[1];
525 texres->trgba[2] += aw4 * ca[2];
526 if (ELEM(tex->vn_coltype, TEX_COL2, TEX_COL3)) {
527 float t1 = (da[1] - da[0]) * 10;
528 t1 = std::min<float>(t1, 1);
529 if (tex->vn_coltype == TEX_COL3) {
530 t1 *= texres->tin;
531 }
532 else {
533 t1 *= sc;
534 }
535 texres->trgba[0] *= t1;
536 texres->trgba[1] *= t1;
537 texres->trgba[2] *= t1;
538 }
539 else {
540 texres->trgba[0] *= sc;
541 texres->trgba[1] *= sc;
542 texres->trgba[2] *= sc;
543 }
544 }
545
546 if (is_color) {
548 texres->trgba[3] = 1.0;
549 return (rv | TEX_RGB);
550 }
551
552 BRICONT;
553
554 return rv;
555}
556
557/* ------------------------------------------------------------------------- */
558
559static int texnoise(const Tex *tex, TexResult *texres, int thread)
560{
561 float div = 3.0;
562 int val, ran, loop, shift = 29;
563
565
566 loop = tex->noisedepth;
567
568 /* start from top bits since they have more variance */
569 val = ((ran >> shift) & 3);
570
571 while (loop--) {
572 shift -= 2;
573 val *= ((ran >> shift) & 3);
574 div *= 3.0f;
575 }
576
577 texres->tin = float(val) / div;
578
579 BRICONT;
580 return TEX_INT;
581}
582
583/* ------------------------------------------------------------------------- */
584
585static int cubemap_glob(const float n[3], float x, float y, float z, float *adr1, float *adr2)
586{
587 float x1, y1, z1, nor[3];
588 int ret;
589
590 if (n == nullptr) {
591 nor[0] = x;
592 nor[1] = y;
593 nor[2] = z; /* use local render coord */
594 }
595 else {
596 copy_v3_v3(nor, n);
597 }
598
599 x1 = fabsf(nor[0]);
600 y1 = fabsf(nor[1]);
601 z1 = fabsf(nor[2]);
602
603 if (z1 >= x1 && z1 >= y1) {
604 *adr1 = (x + 1.0f) / 2.0f;
605 *adr2 = (y + 1.0f) / 2.0f;
606 ret = 0;
607 }
608 else if (y1 >= x1 && y1 >= z1) {
609 *adr1 = (x + 1.0f) / 2.0f;
610 *adr2 = (z + 1.0f) / 2.0f;
611 ret = 1;
612 }
613 else {
614 *adr1 = (y + 1.0f) / 2.0f;
615 *adr2 = (z + 1.0f) / 2.0f;
616 ret = 2;
617 }
618 return ret;
619}
620
621/* ------------------------------------------------------------------------- */
622
623static void do_2d_mapping(const MTex *mtex, float texvec[3], const float n[3])
624{
625 Tex *tex;
626 float fx, fy, fac1;
627 int wrap;
628
629 /* #MTex variables localized, only cube-map doesn't cooperate yet. */
630 wrap = mtex->mapping;
631 tex = mtex->tex;
632
633 {
634
635 if (wrap == MTEX_FLAT) {
636 fx = (texvec[0] + 1.0f) / 2.0f;
637 fy = (texvec[1] + 1.0f) / 2.0f;
638 }
639 else if (wrap == MTEX_TUBE) {
640 map_to_tube(&fx, &fy, texvec[0], texvec[1], texvec[2]);
641 }
642 else if (wrap == MTEX_SPHERE) {
643 map_to_sphere(&fx, &fy, texvec[0], texvec[1], texvec[2]);
644 }
645 else {
646 cubemap_glob(n, texvec[0], texvec[1], texvec[2], &fx, &fy);
647 }
648
649 /* repeat */
650 if (tex->extend == TEX_REPEAT) {
651 if (tex->xrepeat > 1) {
652 float origf = fx *= tex->xrepeat;
653
654 if (fx > 1.0f) {
655 fx -= int(fx);
656 }
657 else if (fx < 0.0f) {
658 fx += 1 - int(fx);
659 }
660
661 if (tex->flag & TEX_REPEAT_XMIR) {
662 int orig = int(floor(origf));
663 if (orig & 1) {
664 fx = 1.0f - fx;
665 }
666 }
667 }
668 if (tex->yrepeat > 1) {
669 float origf = fy *= tex->yrepeat;
670
671 if (fy > 1.0f) {
672 fy -= int(fy);
673 }
674 else if (fy < 0.0f) {
675 fy += 1 - int(fy);
676 }
677
678 if (tex->flag & TEX_REPEAT_YMIR) {
679 int orig = int(floor(origf));
680 if (orig & 1) {
681 fy = 1.0f - fy;
682 }
683 }
684 }
685 }
686 /* crop */
687 if (tex->cropxmin != 0.0f || tex->cropxmax != 1.0f) {
688 fac1 = tex->cropxmax - tex->cropxmin;
689 fx = tex->cropxmin + fx * fac1;
690 }
691 if (tex->cropymin != 0.0f || tex->cropymax != 1.0f) {
692 fac1 = tex->cropymax - tex->cropymin;
693 fy = tex->cropymin + fy * fac1;
694 }
695
696 texvec[0] = fx;
697 texvec[1] = fy;
698 }
699}
700
701/* ************************************** */
702
703static int multitex(Tex *tex,
704 const float texvec[3],
705 TexResult *texres,
706 const short thread,
707 const short which_output,
708 ImagePool *pool,
709 const bool skip_load_image,
710 const bool texnode_preview,
711 const bool use_nodes)
712{
713 float tmpvec[3];
714 int retval = 0; /* return value, TEX_INT or TEX_RGB. */
715
716 texres->talpha = false; /* Is set when image texture returns alpha (considered pre-multiplied) */
717
718 if (use_nodes && tex->use_nodes && tex->nodetree) {
719 const float cfra = 1.0f; /* This was only set for Blender Internal render before. */
720 retval = ntreeTexExecTree(
721 tex->nodetree, texres, texvec, thread, tex, which_output, cfra, texnode_preview, nullptr);
722 }
723 else {
724 if (tex->type == eTex_Type(0)) {
725 texres->tin = 0.0f;
726 return 0;
727 }
728 switch (tex->type) {
729 case TEX_CLOUDS:
730 retval = clouds(tex, texvec, texres);
731 break;
732 case TEX_WOOD:
733 retval = wood(tex, texvec, texres);
734 break;
735 case TEX_MARBLE:
736 retval = marble(tex, texvec, texres);
737 break;
738 case TEX_MAGIC:
739 retval = magic(tex, texvec, texres);
740 break;
741 case TEX_BLEND:
742 retval = blend(tex, texvec, texres);
743 break;
744 case TEX_STUCCI:
745 retval = stucci(tex, texvec, texres);
746 break;
747 case TEX_NOISE:
748 retval = texnoise(tex, texres, thread);
749 break;
750 case TEX_IMAGE:
751 retval = imagewrap(tex, tex->ima, texvec, texres, pool, skip_load_image);
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_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_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}
1109
1110} // namespace blender
#define ATTR_FALLTHROUGH
#define M_PI
Random number functions.
#define ELEM(...)
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
#define floor
BLI_INLINE Texture * wrap(MTLTexture *texture)
BMesh Iterators.
float texture_value_blend(float tex, float out, float fact, float facg, int blendtype)
MINLINE float min_ff(float a, float b)
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)
float BLI_noise_generic_turbulence(float noisesize, float x, float y, float z, int oct, bool hard, int noisebasis)
Definition noise_c.cc:1212
static int stucci(const Tex *tex, const float texvec[3], TexResult *texres)
bool map_to_tube(float *r_u, float *r_v, float x, float y, float z)
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:1537
MINLINE void copy_v3_v3(float r[3], const float a[3])
void RE_texture_rng_init()
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)
static void do_2d_mapping(const MTex *mtex, float texvec[3], const float n[3])
static float marble_int(const Tex *tex, float x, float y, float z)
static int blend(const Tex *tex, const float texvec[3], TexResult *texres)
float BLI_noise_generic_noise(float noisesize, float x, float y, float z, bool hard, int noisebasis)
Definition noise_c.cc:1155
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:1463
static int mg_mFractalOrfBmTex(const Tex *tex, const float texvec[3], TexResult *texres)
bool BKE_colorband_evaluate(const ColorBand *coba, float in, float out[4])
Definition colorband.cc:399
static float tex_tri(float a)
void RE_texture_rng_exit()
bool map_to_sphere(float *r_u, float *r_v, float x, float y, float z)
float BLI_noise_mg_multi_fractal(float x, float y, float z, float H, float lacunarity, float octaves, int noisebasis)
Definition noise_c.cc:1334
static float wood_int(const Tex *tex, float x, float y, float z)
int BLI_rng_thread_rand(RNG_THREAD_ARRAY *rngarr, int thread) ATTR_WARN_UNUSED_RESULT
Definition rand.cc:197
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:1393
MINLINE void mul_v3_fl(float r[3], float f)
static int marble(const Tex *tex, const float texvec[3], TexResult *texres)
void IMB_colormanagement_colorspace_to_scene_linear_v3(float pixel[3], const ColorSpace *colorspace)
static int mg_distNoiseTex(const Tex *tex, const float texvec[3], TexResult *texres)
void BLI_noise_voronoi(float x, float y, float z, float *da, float *pa, float me, int dtype)
Definition noise_c.cc:917
float BLI_noise_mg_fbm(float x, float y, float z, float H, float lacunarity, float octaves, int noisebasis)
Definition noise_c.cc:1274
static int cubemap_glob(const float n[3], float x, float y, float z, float *adr1, float *adr2)
static int mg_ridgedOrHybridMFTex(const Tex *tex, const float texvec[3], TexResult *texres)
static float tex_sin(float a)
float BLI_noise_mg_variable_lacunarity(float x, float y, float z, float distortion, int nbas1, int nbas2)
Definition noise_c.cc:1608
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 float tex_saw(float a)
static int magic(const Tex *tex, const float texvec[3], TexResult *texres)
BLI_INLINE float IMB_colormanagement_get_luminance(const float rgb[3])
RNG_THREAD_ARRAY * BLI_rng_threaded_new()
Definition rand.cc:180
MINLINE void copy_v3_fl3(float v[3], float x, float y, float z)
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)
static int mg_HTerrainTex(const Tex *tex, const float texvec[3], TexResult *texres)
int multitex_nodes(Tex *tex, const float texvec[3], TexResult *texres, const short thread, short which_output, const MTex *mtex, ImagePool *pool)
int multitex_ext_safe(Tex *tex, const float texvec[3], TexResult *texres, ImagePool *pool, bool scene_color_manage, const bool skip_load_image)
MINLINE float max_ff(float a, float b)
static RNG_THREAD_ARRAY * random_tex_array
void BLI_rng_threaded_free(struct RNG_THREAD_ARRAY *rngarr) ATTR_NONNULL(1)
Definition rand.cc:192
MINLINE float dot_v4v4(const float a[4], const float b[4]) ATTR_WARN_UNUSED_RESULT
void BLI_noise_cell_v3(float x, float y, float z, float r_ca[3])
Definition noise_c.cc:1133
static int clouds(const Tex *tex, const float texvec[3], TexResult *texres)
MINLINE void copy_v4_v4(float r[4], const float a[4])
static float turb(float x, float y, float z, int oct, int hard, int nb, float ampscale, float freqscale)
static int texnoise(const Tex *tex, TexResult *texres, int thread)
static int wood(const Tex *tex, const float texvec[3], TexResult *texres)
ImBuf * BKE_image_pool_acquire_ibuf(Image *ima, ImageUser *iuser, ImagePool *pool)
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 int voronoiTex(const Tex *tex, const float texvec[3], TexResult *texres)
void BKE_image_pool_release_ibuf(Image *ima, ImBuf *ibuf, ImagePool *pool)
int imagewrap(struct Tex *tex, struct Image *ima, const float texvec[3], struct TexResult *texres, struct ImagePool *pool, bool skip_load_image)
#define floorf
#define fabsf
#define sqrtf
#define sinf
#define cosf
#define atan2f
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
const ColorSpace * colorspace
ImBufByteBuffer byte_buffer
const float * float_data() const
eTex_Projection projz
eTex_Projection projy
eTex_Projection projx
eMTex_Mapping mapping
struct Object * object
eTex_VoronoiDistMetric vn_distm
struct ColorBand * coba
eTex_NoiseType noisetype
eTex_VoronoiColType vn_coltype
struct ImageUser iuser
struct Image * ima
eTex_Extend extend
struct bNodeTree * nodetree
eTex_NoiseBasis noisebasis
eTex_NoiseBasis noisebasis2
#define BRICONT
#define BRICONTRGB