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 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
17 * All rights reserved.
18 */
19
20 /** \file
21 * \ingroup bke
22 */
23
24 #include <math.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include "MEM_guardedalloc.h"
30
31 #include "BLI_kdopbvh.h"
32 #include "BLI_listbase.h"
33 #include "BLI_math.h"
34 #include "BLI_math_color.h"
35 #include "BLI_utildefines.h"
36
37 #include "BLT_translation.h"
38
39 /* Allow using deprecated functionality for .blend file I/O. */
40 #define DNA_DEPRECATED_ALLOW
41
42 #include "DNA_brush_types.h"
43 #include "DNA_color_types.h"
44 #include "DNA_defaults.h"
45 #include "DNA_key_types.h"
46 #include "DNA_linestyle_types.h"
47 #include "DNA_material_types.h"
48 #include "DNA_node_types.h"
49 #include "DNA_object_types.h"
50 #include "DNA_particle_types.h"
51
52 #include "IMB_imbuf.h"
53
54 #include "BKE_main.h"
55
56 #include "BKE_anim_data.h"
57 #include "BKE_colorband.h"
58 #include "BKE_colortools.h"
59 #include "BKE_icons.h"
60 #include "BKE_idtype.h"
61 #include "BKE_image.h"
62 #include "BKE_key.h"
63 #include "BKE_lib_id.h"
64 #include "BKE_lib_query.h"
65 #include "BKE_material.h"
66 #include "BKE_node.h"
67 #include "BKE_scene.h"
68 #include "BKE_texture.h"
69
70 #include "RE_shader_ext.h"
71
72 #include "BLO_read_write.h"
73
texture_init_data(ID * id)74 static void texture_init_data(ID *id)
75 {
76 Tex *texture = (Tex *)id;
77
78 BLI_assert(MEMCMP_STRUCT_AFTER_IS_ZERO(texture, id));
79
80 MEMCPY_STRUCT_AFTER(texture, DNA_struct_default_get(Tex), id);
81
82 BKE_imageuser_default(&texture->iuser);
83 }
84
texture_copy_data(Main * bmain,ID * id_dst,const ID * id_src,const int flag)85 static void texture_copy_data(Main *bmain, ID *id_dst, const ID *id_src, const int flag)
86 {
87 Tex *texture_dst = (Tex *)id_dst;
88 const Tex *texture_src = (const Tex *)id_src;
89
90 const bool is_localized = (flag & LIB_ID_CREATE_LOCAL) != 0;
91 /* We always need allocation of our private ID data. */
92 const int flag_private_id_data = flag & ~LIB_ID_CREATE_NO_ALLOCATE;
93
94 if (!BKE_texture_is_image_user(texture_src)) {
95 texture_dst->ima = NULL;
96 }
97
98 if (texture_dst->coba) {
99 texture_dst->coba = MEM_dupallocN(texture_dst->coba);
100 }
101 if (texture_src->nodetree) {
102 if (texture_src->nodetree->execdata) {
103 ntreeTexEndExecTree(texture_src->nodetree->execdata);
104 }
105
106 if (is_localized) {
107 texture_dst->nodetree = ntreeLocalize(texture_src->nodetree);
108 }
109 else {
110 BKE_id_copy_ex(
111 bmain, (ID *)texture_src->nodetree, (ID **)&texture_dst->nodetree, flag_private_id_data);
112 }
113 }
114
115 if ((flag & LIB_ID_COPY_NO_PREVIEW) == 0) {
116 BKE_previewimg_id_copy(&texture_dst->id, &texture_src->id);
117 }
118 else {
119 texture_dst->preview = NULL;
120 }
121 }
122
texture_free_data(ID * id)123 static void texture_free_data(ID *id)
124 {
125 Tex *texture = (Tex *)id;
126
127 /* is no lib link block, but texture extension */
128 if (texture->nodetree) {
129 ntreeFreeEmbeddedTree(texture->nodetree);
130 MEM_freeN(texture->nodetree);
131 texture->nodetree = NULL;
132 }
133
134 MEM_SAFE_FREE(texture->coba);
135
136 BKE_icon_id_delete((ID *)texture);
137 BKE_previewimg_free(&texture->preview);
138 }
139
texture_foreach_id(ID * id,LibraryForeachIDData * data)140 static void texture_foreach_id(ID *id, LibraryForeachIDData *data)
141 {
142 Tex *texture = (Tex *)id;
143 if (texture->nodetree) {
144 /* nodetree **are owned by IDs**, treat them as mere sub-data and not real ID! */
145 BKE_library_foreach_ID_embedded(data, (ID **)&texture->nodetree);
146 }
147 BKE_LIB_FOREACHID_PROCESS(data, texture->ima, IDWALK_CB_USER);
148 }
149
texture_blend_write(BlendWriter * writer,ID * id,const void * id_address)150 static void texture_blend_write(BlendWriter *writer, ID *id, const void *id_address)
151 {
152 Tex *tex = (Tex *)id;
153 if (tex->id.us > 0 || BLO_write_is_undo(writer)) {
154 /* write LibData */
155 BLO_write_id_struct(writer, Tex, id_address, &tex->id);
156 BKE_id_blend_write(writer, &tex->id);
157
158 if (tex->adt) {
159 BKE_animdata_blend_write(writer, tex->adt);
160 }
161
162 /* direct data */
163 if (tex->coba) {
164 BLO_write_struct(writer, ColorBand, tex->coba);
165 }
166
167 /* nodetree is integral part of texture, no libdata */
168 if (tex->nodetree) {
169 BLO_write_struct(writer, bNodeTree, tex->nodetree);
170 ntreeBlendWrite(writer, tex->nodetree);
171 }
172
173 BKE_previewimg_blend_write(writer, tex->preview);
174 }
175 }
176
texture_blend_read_data(BlendDataReader * reader,ID * id)177 static void texture_blend_read_data(BlendDataReader *reader, ID *id)
178 {
179 Tex *tex = (Tex *)id;
180 BLO_read_data_address(reader, &tex->adt);
181 BKE_animdata_blend_read_data(reader, tex->adt);
182
183 BLO_read_data_address(reader, &tex->coba);
184
185 BLO_read_data_address(reader, &tex->preview);
186 BKE_previewimg_blend_read(reader, tex->preview);
187
188 tex->iuser.ok = 1;
189 tex->iuser.scene = NULL;
190 }
191
texture_blend_read_lib(BlendLibReader * reader,ID * id)192 static void texture_blend_read_lib(BlendLibReader *reader, ID *id)
193 {
194 Tex *tex = (Tex *)id;
195 BLO_read_id_address(reader, tex->id.lib, &tex->ima);
196 BLO_read_id_address(reader, tex->id.lib, &tex->ipo); /* XXX deprecated - old animation system */
197 }
198
texture_blend_read_expand(BlendExpander * expander,ID * id)199 static void texture_blend_read_expand(BlendExpander *expander, ID *id)
200 {
201 Tex *tex = (Tex *)id;
202 BLO_expand(expander, tex->ima);
203 BLO_expand(expander, tex->ipo); /* XXX deprecated - old animation system */
204 }
205
206 IDTypeInfo IDType_ID_TE = {
207 .id_code = ID_TE,
208 .id_filter = FILTER_ID_TE,
209 .main_listbase_index = INDEX_ID_TE,
210 .struct_size = sizeof(Tex),
211 .name = "Texture",
212 .name_plural = "textures",
213 .translation_context = BLT_I18NCONTEXT_ID_TEXTURE,
214 .flags = 0,
215
216 .init_data = texture_init_data,
217 .copy_data = texture_copy_data,
218 .free_data = texture_free_data,
219 .make_local = NULL,
220 .foreach_id = texture_foreach_id,
221 .foreach_cache = NULL,
222
223 .blend_write = texture_blend_write,
224 .blend_read_data = texture_blend_read_data,
225 .blend_read_lib = texture_blend_read_lib,
226 .blend_read_expand = texture_blend_read_expand,
227 };
228
229 /* Utils for all IDs using those texture slots. */
BKE_texture_mtex_foreach_id(LibraryForeachIDData * data,MTex * mtex)230 void BKE_texture_mtex_foreach_id(LibraryForeachIDData *data, MTex *mtex)
231 {
232 BKE_LIB_FOREACHID_PROCESS(data, mtex->object, IDWALK_CB_NOP);
233 BKE_LIB_FOREACHID_PROCESS(data, mtex->tex, IDWALK_CB_USER);
234 }
235
236 /* ****************** Mapping ******************* */
237
BKE_texture_mapping_add(int type)238 TexMapping *BKE_texture_mapping_add(int type)
239 {
240 TexMapping *texmap = MEM_callocN(sizeof(TexMapping), "TexMapping");
241
242 BKE_texture_mapping_default(texmap, type);
243
244 return texmap;
245 }
246
BKE_texture_mapping_default(TexMapping * texmap,int type)247 void BKE_texture_mapping_default(TexMapping *texmap, int type)
248 {
249 memset(texmap, 0, sizeof(TexMapping));
250
251 texmap->size[0] = texmap->size[1] = texmap->size[2] = 1.0f;
252 texmap->max[0] = texmap->max[1] = texmap->max[2] = 1.0f;
253 unit_m4(texmap->mat);
254
255 texmap->projx = PROJ_X;
256 texmap->projy = PROJ_Y;
257 texmap->projz = PROJ_Z;
258 texmap->mapping = MTEX_FLAT;
259 texmap->type = type;
260 }
261
BKE_texture_mapping_init(TexMapping * texmap)262 void BKE_texture_mapping_init(TexMapping *texmap)
263 {
264 float smat[4][4], rmat[4][4], tmat[4][4], proj[4][4], size[3];
265
266 if (texmap->projx == PROJ_X && texmap->projy == PROJ_Y && texmap->projz == PROJ_Z &&
267 is_zero_v3(texmap->loc) && is_zero_v3(texmap->rot) && is_one_v3(texmap->size)) {
268 unit_m4(texmap->mat);
269
270 texmap->flag |= TEXMAP_UNIT_MATRIX;
271 }
272 else {
273 /* axis projection */
274 zero_m4(proj);
275 proj[3][3] = 1.0f;
276
277 if (texmap->projx != PROJ_N) {
278 proj[texmap->projx - 1][0] = 1.0f;
279 }
280 if (texmap->projy != PROJ_N) {
281 proj[texmap->projy - 1][1] = 1.0f;
282 }
283 if (texmap->projz != PROJ_N) {
284 proj[texmap->projz - 1][2] = 1.0f;
285 }
286
287 /* scale */
288 copy_v3_v3(size, texmap->size);
289
290 if (ELEM(texmap->type, TEXMAP_TYPE_TEXTURE, TEXMAP_TYPE_NORMAL)) {
291 /* keep matrix invertible */
292 if (fabsf(size[0]) < 1e-5f) {
293 size[0] = signf(size[0]) * 1e-5f;
294 }
295 if (fabsf(size[1]) < 1e-5f) {
296 size[1] = signf(size[1]) * 1e-5f;
297 }
298 if (fabsf(size[2]) < 1e-5f) {
299 size[2] = signf(size[2]) * 1e-5f;
300 }
301 }
302
303 size_to_mat4(smat, texmap->size);
304
305 /* rotation */
306 eul_to_mat4(rmat, texmap->rot);
307
308 /* translation */
309 unit_m4(tmat);
310 copy_v3_v3(tmat[3], texmap->loc);
311
312 if (texmap->type == TEXMAP_TYPE_TEXTURE) {
313 /* to transform a texture, the inverse transform needs
314 * to be applied to the texture coordinate */
315 mul_m4_series(texmap->mat, tmat, rmat, smat);
316 invert_m4(texmap->mat);
317 }
318 else if (texmap->type == TEXMAP_TYPE_POINT) {
319 /* forward transform */
320 mul_m4_series(texmap->mat, tmat, rmat, smat);
321 }
322 else if (texmap->type == TEXMAP_TYPE_VECTOR) {
323 /* no translation for vectors */
324 mul_m4_m4m4(texmap->mat, rmat, smat);
325 }
326 else if (texmap->type == TEXMAP_TYPE_NORMAL) {
327 /* no translation for normals, and inverse transpose */
328 mul_m4_m4m4(texmap->mat, rmat, smat);
329 invert_m4(texmap->mat);
330 transpose_m4(texmap->mat);
331 }
332
333 /* projection last */
334 mul_m4_m4m4(texmap->mat, texmap->mat, proj);
335
336 texmap->flag &= ~TEXMAP_UNIT_MATRIX;
337 }
338 }
339
BKE_texture_colormapping_add(void)340 ColorMapping *BKE_texture_colormapping_add(void)
341 {
342 ColorMapping *colormap = MEM_callocN(sizeof(ColorMapping), "ColorMapping");
343
344 BKE_texture_colormapping_default(colormap);
345
346 return colormap;
347 }
348
BKE_texture_colormapping_default(ColorMapping * colormap)349 void BKE_texture_colormapping_default(ColorMapping *colormap)
350 {
351 memset(colormap, 0, sizeof(ColorMapping));
352
353 BKE_colorband_init(&colormap->coba, true);
354
355 colormap->bright = 1.0;
356 colormap->contrast = 1.0;
357 colormap->saturation = 1.0;
358
359 colormap->blend_color[0] = 0.8f;
360 colormap->blend_color[1] = 0.8f;
361 colormap->blend_color[2] = 0.8f;
362 colormap->blend_type = MA_RAMP_BLEND;
363 colormap->blend_factor = 0.0f;
364 }
365
366 /* ******************* TEX ************************ */
367
368 /* ------------------------------------------------------------------------- */
369
BKE_texture_default(Tex * tex)370 void BKE_texture_default(Tex *tex)
371 {
372 texture_init_data(&tex->id);
373 }
374
BKE_texture_type_set(Tex * tex,int type)375 void BKE_texture_type_set(Tex *tex, int type)
376 {
377 tex->type = type;
378 }
379
380 /* ------------------------------------------------------------------------- */
381
BKE_texture_add(Main * bmain,const char * name)382 Tex *BKE_texture_add(Main *bmain, const char *name)
383 {
384 Tex *tex;
385
386 tex = BKE_id_new(bmain, ID_TE, name);
387
388 return tex;
389 }
390
391 /* ------------------------------------------------------------------------- */
392
BKE_texture_mtex_default(MTex * mtex)393 void BKE_texture_mtex_default(MTex *mtex)
394 {
395 memcpy(mtex, DNA_struct_default_get(MTex), sizeof(*mtex));
396 }
397
398 /* ------------------------------------------------------------------------- */
399
BKE_texture_mtex_add(void)400 MTex *BKE_texture_mtex_add(void)
401 {
402 MTex *mtex;
403
404 mtex = MEM_callocN(sizeof(MTex), "BKE_texture_mtex_add");
405
406 BKE_texture_mtex_default(mtex);
407
408 return mtex;
409 }
410
411 /* slot -1 for first free ID */
BKE_texture_mtex_add_id(ID * id,int slot)412 MTex *BKE_texture_mtex_add_id(ID *id, int slot)
413 {
414 MTex **mtex_ar;
415 short act;
416
417 give_active_mtex(id, &mtex_ar, &act);
418
419 if (mtex_ar == NULL) {
420 return NULL;
421 }
422
423 if (slot == -1) {
424 /* find first free */
425 int i;
426 for (i = 0; i < MAX_MTEX; i++) {
427 if (!mtex_ar[i]) {
428 slot = i;
429 break;
430 }
431 }
432 if (slot == -1) {
433 return NULL;
434 }
435 }
436 else {
437 /* make sure slot is valid */
438 if (slot < 0 || slot >= MAX_MTEX) {
439 return NULL;
440 }
441 }
442
443 if (mtex_ar[slot]) {
444 id_us_min((ID *)mtex_ar[slot]->tex);
445 MEM_freeN(mtex_ar[slot]);
446 mtex_ar[slot] = NULL;
447 }
448
449 mtex_ar[slot] = BKE_texture_mtex_add();
450
451 return mtex_ar[slot];
452 }
453
454 /* ------------------------------------------------------------------------- */
455
give_current_linestyle_texture(FreestyleLineStyle * linestyle)456 Tex *give_current_linestyle_texture(FreestyleLineStyle *linestyle)
457 {
458 MTex *mtex = NULL;
459 Tex *tex = NULL;
460
461 if (linestyle) {
462 mtex = linestyle->mtex[(int)(linestyle->texact)];
463 if (mtex) {
464 tex = mtex->tex;
465 }
466 }
467
468 return tex;
469 }
470
set_current_linestyle_texture(FreestyleLineStyle * linestyle,Tex * newtex)471 void set_current_linestyle_texture(FreestyleLineStyle *linestyle, Tex *newtex)
472 {
473 int act = linestyle->texact;
474
475 if (linestyle->mtex[act] && linestyle->mtex[act]->tex) {
476 id_us_min(&linestyle->mtex[act]->tex->id);
477 }
478
479 if (newtex) {
480 if (!linestyle->mtex[act]) {
481 linestyle->mtex[act] = BKE_texture_mtex_add();
482 linestyle->mtex[act]->texco = TEXCO_STROKE;
483 }
484
485 linestyle->mtex[act]->tex = newtex;
486 id_us_plus(&newtex->id);
487 }
488 else if (linestyle->mtex[act]) {
489 MEM_freeN(linestyle->mtex[act]);
490 linestyle->mtex[act] = NULL;
491 }
492 }
493
give_active_mtex(ID * id,MTex *** mtex_ar,short * act)494 bool give_active_mtex(ID *id, MTex ***mtex_ar, short *act)
495 {
496 switch (GS(id->name)) {
497 case ID_LS:
498 *mtex_ar = ((FreestyleLineStyle *)id)->mtex;
499 if (act) {
500 *act = (((FreestyleLineStyle *)id)->texact);
501 }
502 break;
503 case ID_PA:
504 *mtex_ar = ((ParticleSettings *)id)->mtex;
505 if (act) {
506 *act = (((ParticleSettings *)id)->texact);
507 }
508 break;
509 default:
510 *mtex_ar = NULL;
511 if (act) {
512 *act = 0;
513 }
514 return false;
515 }
516
517 return true;
518 }
519
set_active_mtex(ID * id,short act)520 void set_active_mtex(ID *id, short act)
521 {
522 if (act < 0) {
523 act = 0;
524 }
525 else if (act >= MAX_MTEX) {
526 act = MAX_MTEX - 1;
527 }
528
529 switch (GS(id->name)) {
530 case ID_LS:
531 ((FreestyleLineStyle *)id)->texact = act;
532 break;
533 case ID_PA:
534 ((ParticleSettings *)id)->texact = act;
535 break;
536 default:
537 break;
538 }
539 }
540
give_current_brush_texture(Brush * br)541 Tex *give_current_brush_texture(Brush *br)
542 {
543 return br->mtex.tex;
544 }
545
set_current_brush_texture(Brush * br,Tex * newtex)546 void set_current_brush_texture(Brush *br, Tex *newtex)
547 {
548 if (br->mtex.tex) {
549 id_us_min(&br->mtex.tex->id);
550 }
551
552 if (newtex) {
553 br->mtex.tex = newtex;
554 id_us_plus(&newtex->id);
555 }
556 }
557
give_current_particle_texture(ParticleSettings * part)558 Tex *give_current_particle_texture(ParticleSettings *part)
559 {
560 MTex *mtex = NULL;
561 Tex *tex = NULL;
562
563 if (!part) {
564 return NULL;
565 }
566
567 mtex = part->mtex[(int)(part->texact)];
568 if (mtex) {
569 tex = mtex->tex;
570 }
571
572 return tex;
573 }
574
set_current_particle_texture(ParticleSettings * part,Tex * newtex)575 void set_current_particle_texture(ParticleSettings *part, Tex *newtex)
576 {
577 int act = part->texact;
578
579 if (part->mtex[act] && part->mtex[act]->tex) {
580 id_us_min(&part->mtex[act]->tex->id);
581 }
582
583 if (newtex) {
584 if (!part->mtex[act]) {
585 part->mtex[act] = BKE_texture_mtex_add();
586 part->mtex[act]->texco = TEXCO_ORCO;
587 part->mtex[act]->blendtype = MTEX_MUL;
588 }
589
590 part->mtex[act]->tex = newtex;
591 id_us_plus(&newtex->id);
592 }
593 else if (part->mtex[act]) {
594 MEM_freeN(part->mtex[act]);
595 part->mtex[act] = NULL;
596 }
597 }
598
599 /* ------------------------------------------------------------------------- */
600
BKE_texture_pointdensity_init_data(PointDensity * pd)601 void BKE_texture_pointdensity_init_data(PointDensity *pd)
602 {
603 pd->flag = 0;
604 pd->radius = 0.3f;
605 pd->falloff_type = TEX_PD_FALLOFF_STD;
606 pd->falloff_softness = 2.0;
607 pd->source = TEX_PD_PSYS;
608 pd->point_tree = NULL;
609 pd->point_data = NULL;
610 pd->noise_size = 0.5f;
611 pd->noise_depth = 1;
612 pd->noise_fac = 1.0f;
613 pd->noise_influence = TEX_PD_NOISE_STATIC;
614 pd->coba = BKE_colorband_add(true);
615 pd->speed_scale = 1.0f;
616 pd->totpoints = 0;
617 pd->object = NULL;
618 pd->psys = 0;
619 pd->psys_cache_space = TEX_PD_WORLDSPACE;
620 pd->falloff_curve = BKE_curvemapping_add(1, 0, 0, 1, 1);
621
622 pd->falloff_curve->preset = CURVE_PRESET_LINE;
623 pd->falloff_curve->flag &= ~CUMA_EXTEND_EXTRAPOLATE;
624 BKE_curvemap_reset(pd->falloff_curve->cm,
625 &pd->falloff_curve->clipr,
626 pd->falloff_curve->preset,
627 CURVEMAP_SLOPE_POSITIVE);
628 BKE_curvemapping_changed(pd->falloff_curve, false);
629 }
630
BKE_texture_pointdensity_add(void)631 PointDensity *BKE_texture_pointdensity_add(void)
632 {
633 PointDensity *pd = MEM_callocN(sizeof(PointDensity), "pointdensity");
634 BKE_texture_pointdensity_init_data(pd);
635 return pd;
636 }
637
BKE_texture_pointdensity_copy(const PointDensity * pd,const int UNUSED (flag))638 PointDensity *BKE_texture_pointdensity_copy(const PointDensity *pd, const int UNUSED(flag))
639 {
640 PointDensity *pdn;
641
642 pdn = MEM_dupallocN(pd);
643 pdn->point_tree = NULL;
644 pdn->point_data = NULL;
645 if (pdn->coba) {
646 pdn->coba = MEM_dupallocN(pdn->coba);
647 }
648 pdn->falloff_curve = BKE_curvemapping_copy(pdn->falloff_curve); /* can be NULL */
649 return pdn;
650 }
651
BKE_texture_pointdensity_free_data(PointDensity * pd)652 void BKE_texture_pointdensity_free_data(PointDensity *pd)
653 {
654 if (pd->point_tree) {
655 BLI_bvhtree_free(pd->point_tree);
656 pd->point_tree = NULL;
657 }
658 if (pd->point_data) {
659 MEM_freeN(pd->point_data);
660 pd->point_data = NULL;
661 }
662 if (pd->coba) {
663 MEM_freeN(pd->coba);
664 pd->coba = NULL;
665 }
666
667 BKE_curvemapping_free(pd->falloff_curve); /* can be NULL */
668 }
669
BKE_texture_pointdensity_free(PointDensity * pd)670 void BKE_texture_pointdensity_free(PointDensity *pd)
671 {
672 BKE_texture_pointdensity_free_data(pd);
673 MEM_freeN(pd);
674 }
675 /* ------------------------------------------------------------------------- */
676
677 /**
678 * \returns true if this texture can use its #Texture.ima (even if its NULL)
679 */
BKE_texture_is_image_user(const struct Tex * tex)680 bool BKE_texture_is_image_user(const struct Tex *tex)
681 {
682 switch (tex->type) {
683 case TEX_IMAGE: {
684 return true;
685 }
686 }
687
688 return false;
689 }
690
691 /* ------------------------------------------------------------------------- */
BKE_texture_dependsOnTime(const struct Tex * texture)692 bool BKE_texture_dependsOnTime(const struct Tex *texture)
693 {
694 if (texture->ima && BKE_image_is_animated(texture->ima)) {
695 return true;
696 }
697 if (texture->adt) {
698 /* assume anything in adt means the texture is animated */
699 return true;
700 }
701 if (texture->type == TEX_NOISE) {
702 /* noise always varies with time */
703 return true;
704 }
705 return false;
706 }
707
708 /* ------------------------------------------------------------------------- */
709
BKE_texture_get_value_ex(const Scene * scene,Tex * texture,const float * tex_co,TexResult * texres,struct ImagePool * pool,bool use_color_management)710 void BKE_texture_get_value_ex(const Scene *scene,
711 Tex *texture,
712 const float *tex_co,
713 TexResult *texres,
714 struct ImagePool *pool,
715 bool use_color_management)
716 {
717 int result_type;
718 bool do_color_manage = false;
719
720 if (scene && use_color_management) {
721 do_color_manage = BKE_scene_check_color_management_enabled(scene);
722 }
723
724 /* no node textures for now */
725 result_type = multitex_ext_safe(texture, tex_co, texres, pool, do_color_manage, false);
726
727 /* if the texture gave an RGB value, we assume it didn't give a valid
728 * intensity, since this is in the context of modifiers don't use perceptual color conversion.
729 * if the texture didn't give an RGB value, copy the intensity across
730 */
731 if (result_type & TEX_RGB) {
732 texres->tin = (1.0f / 3.0f) * (texres->tr + texres->tg + texres->tb);
733 }
734 else {
735 copy_v3_fl(&texres->tr, texres->tin);
736 }
737 }
738
BKE_texture_get_value(const Scene * scene,Tex * texture,const float * tex_co,TexResult * texres,bool use_color_management)739 void BKE_texture_get_value(const Scene *scene,
740 Tex *texture,
741 const float *tex_co,
742 TexResult *texres,
743 bool use_color_management)
744 {
745 BKE_texture_get_value_ex(scene, texture, tex_co, texres, NULL, use_color_management);
746 }
747
texture_nodes_fetch_images_for_pool(Tex * texture,bNodeTree * ntree,struct ImagePool * pool)748 static void texture_nodes_fetch_images_for_pool(Tex *texture,
749 bNodeTree *ntree,
750 struct ImagePool *pool)
751 {
752 LISTBASE_FOREACH (bNode *, node, &ntree->nodes) {
753 if (node->type == SH_NODE_TEX_IMAGE && node->id != NULL) {
754 Image *image = (Image *)node->id;
755 BKE_image_pool_acquire_ibuf(image, &texture->iuser, pool);
756 }
757 else if (node->type == NODE_GROUP && node->id != NULL) {
758 /* TODO(sergey): Do we need to control recursion here? */
759 bNodeTree *nested_tree = (bNodeTree *)node->id;
760 texture_nodes_fetch_images_for_pool(texture, nested_tree, pool);
761 }
762 }
763 }
764
765 /* Make sure all images used by texture are loaded into pool. */
BKE_texture_fetch_images_for_pool(Tex * texture,struct ImagePool * pool)766 void BKE_texture_fetch_images_for_pool(Tex *texture, struct ImagePool *pool)
767 {
768 if (texture->nodetree != NULL) {
769 texture_nodes_fetch_images_for_pool(texture, texture->nodetree, pool);
770 }
771 else {
772 if (texture->type == TEX_IMAGE) {
773 if (texture->ima != NULL) {
774 BKE_image_pool_acquire_ibuf(texture->ima, &texture->iuser, pool);
775 }
776 }
777 }
778 }
779