2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19 * All rights reserved.
21 * The Original Code is: all of this file.
23 * Contributor(s): none yet.
25 * ***** END GPL LICENSE BLOCK *****
28 /** \file blender/blenkernel/intern/texture.c
38 #include "MEM_guardedalloc.h"
41 #include "BLI_kdopbvh.h"
42 #include "BLI_utildefines.h"
43 #include "BLI_math_color.h"
45 #include "DNA_key_types.h"
46 #include "DNA_object_types.h"
47 #include "DNA_lamp_types.h"
48 #include "DNA_material_types.h"
49 #include "DNA_world_types.h"
50 #include "DNA_brush_types.h"
51 #include "DNA_node_types.h"
52 #include "DNA_color_types.h"
53 #include "DNA_particle_types.h"
54 #include "DNA_linestyle_types.h"
56 #include "IMB_imbuf.h"
58 #include "BKE_global.h"
61 #include "BKE_library.h"
62 #include "BKE_library_query.h"
63 #include "BKE_library_remap.h"
64 #include "BKE_image.h"
65 #include "BKE_material.h"
66 #include "BKE_texture.h"
68 #include "BKE_icons.h"
70 #include "BKE_animsys.h"
71 #include "BKE_colortools.h"
72 #include "BKE_scene.h"
74 #include "RE_shader_ext.h"
76 /* ****************** Mapping ******************* */
78 TexMapping *BKE_texture_mapping_add(int type)
80 TexMapping *texmap = MEM_callocN(sizeof(TexMapping), "TexMapping");
82 BKE_texture_mapping_default(texmap, type);
87 void BKE_texture_mapping_default(TexMapping *texmap, int type)
89 memset(texmap, 0, sizeof(TexMapping));
91 texmap->size[0] = texmap->size[1] = texmap->size[2] = 1.0f;
92 texmap->max[0] = texmap->max[1] = texmap->max[2] = 1.0f;
95 texmap->projx = PROJ_X;
96 texmap->projy = PROJ_Y;
97 texmap->projz = PROJ_Z;
98 texmap->mapping = MTEX_FLAT;
102 void BKE_texture_mapping_init(TexMapping *texmap)
104 float smat[4][4], rmat[4][4], tmat[4][4], proj[4][4], size[3];
106 if (texmap->projx == PROJ_X && texmap->projy == PROJ_Y && texmap->projz == PROJ_Z &&
107 is_zero_v3(texmap->loc) && is_zero_v3(texmap->rot) && is_one_v3(texmap->size))
109 unit_m4(texmap->mat);
111 texmap->flag |= TEXMAP_UNIT_MATRIX;
114 /* axis projection */
118 if (texmap->projx != PROJ_N)
119 proj[texmap->projx - 1][0] = 1.0f;
120 if (texmap->projy != PROJ_N)
121 proj[texmap->projy - 1][1] = 1.0f;
122 if (texmap->projz != PROJ_N)
123 proj[texmap->projz - 1][2] = 1.0f;
126 copy_v3_v3(size, texmap->size);
128 if (ELEM(texmap->type, TEXMAP_TYPE_TEXTURE, TEXMAP_TYPE_NORMAL)) {
129 /* keep matrix invertible */
130 if (fabsf(size[0]) < 1e-5f)
131 size[0] = signf(size[0]) * 1e-5f;
132 if (fabsf(size[1]) < 1e-5f)
133 size[1] = signf(size[1]) * 1e-5f;
134 if (fabsf(size[2]) < 1e-5f)
135 size[2] = signf(size[2]) * 1e-5f;
138 size_to_mat4(smat, texmap->size);
141 eul_to_mat4(rmat, texmap->rot);
145 copy_v3_v3(tmat[3], texmap->loc);
147 if (texmap->type == TEXMAP_TYPE_TEXTURE) {
148 /* to transform a texture, the inverse transform needs
149 * to be applied to the texture coordinate */
150 mul_m4_series(texmap->mat, tmat, rmat, smat);
151 invert_m4(texmap->mat);
153 else if (texmap->type == TEXMAP_TYPE_POINT) {
154 /* forward transform */
155 mul_m4_series(texmap->mat, tmat, rmat, smat);
157 else if (texmap->type == TEXMAP_TYPE_VECTOR) {
158 /* no translation for vectors */
159 mul_m4_m4m4(texmap->mat, rmat, smat);
161 else if (texmap->type == TEXMAP_TYPE_NORMAL) {
162 /* no translation for normals, and inverse transpose */
163 mul_m4_m4m4(texmap->mat, rmat, smat);
164 invert_m4(texmap->mat);
165 transpose_m4(texmap->mat);
168 /* projection last */
169 mul_m4_m4m4(texmap->mat, texmap->mat, proj);
171 texmap->flag &= ~TEXMAP_UNIT_MATRIX;
175 ColorMapping *BKE_texture_colormapping_add(void)
177 ColorMapping *colormap = MEM_callocN(sizeof(ColorMapping), "ColorMapping");
179 BKE_texture_colormapping_default(colormap);
184 void BKE_texture_colormapping_default(ColorMapping *colormap)
186 memset(colormap, 0, sizeof(ColorMapping));
188 init_colorband(&colormap->coba, true);
190 colormap->bright = 1.0;
191 colormap->contrast = 1.0;
192 colormap->saturation = 1.0;
194 colormap->blend_color[0] = 0.8f;
195 colormap->blend_color[1] = 0.8f;
196 colormap->blend_color[2] = 0.8f;
197 colormap->blend_type = MA_RAMP_BLEND;
198 colormap->blend_factor = 0.0f;
201 /* ****************** COLORBAND ******************* */
203 void init_colorband(ColorBand *coba, bool rangetype)
207 coba->data[0].pos = 0.0;
208 coba->data[1].pos = 1.0;
210 if (rangetype == 0) {
211 coba->data[0].r = 0.0;
212 coba->data[0].g = 0.0;
213 coba->data[0].b = 0.0;
214 coba->data[0].a = 0.0;
216 coba->data[1].r = 1.0;
217 coba->data[1].g = 1.0;
218 coba->data[1].b = 1.0;
219 coba->data[1].a = 1.0;
222 coba->data[0].r = 0.0;
223 coba->data[0].g = 0.0;
224 coba->data[0].b = 0.0;
225 coba->data[0].a = 1.0;
227 coba->data[1].r = 1.0;
228 coba->data[1].g = 1.0;
229 coba->data[1].b = 1.0;
230 coba->data[1].a = 1.0;
233 for (a = 2; a < MAXCOLORBAND; a++) {
234 coba->data[a].r = 0.5;
235 coba->data[a].g = 0.5;
236 coba->data[a].b = 0.5;
237 coba->data[a].a = 1.0;
238 coba->data[a].pos = 0.5;
242 coba->color_mode = COLBAND_BLEND_RGB;
245 ColorBand *add_colorband(bool rangetype)
249 coba = MEM_callocN(sizeof(ColorBand), "colorband");
250 init_colorband(coba, rangetype);
255 /* ------------------------------------------------------------------------- */
257 static float colorband_hue_interp(
258 const int ipotype_hue,
259 const float mfac, const float fac,
265 #define HUE_INTERP(h_a, h_b) ((mfac * (h_a)) + (fac * (h_b)))
266 #define HUE_MOD(h) (((h) < 1.0f) ? (h) : (h) - 1.0f)
271 BLI_assert(h1 >= 0.0f && h1 < 1.0f);
272 BLI_assert(h2 >= 0.0f && h2 < 1.0f);
274 switch (ipotype_hue) {
275 case COLBAND_HUE_NEAR:
277 if ((h1 < h2) && (h2 - h1) > +0.5f) mode = 1;
278 else if ((h1 > h2) && (h2 - h1) < -0.5f) mode = 2;
282 case COLBAND_HUE_FAR:
284 if ((h1 < h2) && (h2 - h1) < +0.5f) mode = 1;
285 else if ((h1 > h2) && (h2 - h1) > -0.5f) mode = 2;
289 case COLBAND_HUE_CCW:
291 if (h1 > h2) mode = 2;
297 if (h1 < h2) mode = 1;
305 h_interp = HUE_INTERP(h1, h2);
308 h_interp = HUE_INTERP(h1 + 1.0f, h2);
309 h_interp = HUE_MOD(h_interp);
312 h_interp = HUE_INTERP(h1, h2 + 1.0f);
313 h_interp = HUE_MOD(h_interp);
317 BLI_assert(h_interp >= 0.0f && h_interp < 1.0f);
325 bool do_colorband(const ColorBand *coba, float in, float out[4])
327 const CBData *cbd1, *cbd2, *cbd0, *cbd3;
332 if (coba == NULL || coba->tot == 0) return false;
336 ipotype = (coba->color_mode == COLBAND_BLEND_RGB) ? coba->ipotype : COLBAND_INTERP_LINEAR;
338 if (coba->tot == 1) {
344 else if ((in <= cbd1->pos) && ELEM(ipotype, COLBAND_INTERP_LINEAR, COLBAND_INTERP_EASE)) {
353 /* we're looking for first pos > in */
354 for (a = 0; a < coba->tot; a++, cbd1++) {
355 if (cbd1->pos > in) {
360 if (a == coba->tot) {
375 if ((in >= cbd1->pos) && ELEM(ipotype, COLBAND_INTERP_LINEAR, COLBAND_INTERP_EASE)) {
383 if (cbd2->pos != cbd1->pos) {
384 fac = (in - cbd1->pos) / (cbd2->pos - cbd1->pos);
387 /* was setting to 0.0 in 2.56 & previous, but this
388 * is incorrect for the last element, see [#26732] */
389 fac = (a != coba->tot) ? 0.0f : 1.0f;
392 if (ipotype == COLBAND_INTERP_CONSTANT) {
399 else if (ipotype >= COLBAND_INTERP_B_SPLINE) {
400 /* ipo from right to left: 3 2 1 0 */
403 if (a >= coba->tot - 1) cbd0 = cbd1;
404 else cbd0 = cbd1 + 1;
405 if (a < 2) cbd3 = cbd2;
406 else cbd3 = cbd2 - 1;
408 CLAMP(fac, 0.0f, 1.0f);
410 if (ipotype == COLBAND_INTERP_CARDINAL) {
411 key_curve_position_weights(fac, t, KEY_CARDINAL);
414 key_curve_position_weights(fac, t, KEY_BSPLINE);
417 out[0] = t[3] * cbd3->r + t[2] * cbd2->r + t[1] * cbd1->r + t[0] * cbd0->r;
418 out[1] = t[3] * cbd3->g + t[2] * cbd2->g + t[1] * cbd1->g + t[0] * cbd0->g;
419 out[2] = t[3] * cbd3->b + t[2] * cbd2->b + t[1] * cbd1->b + t[0] * cbd0->b;
420 out[3] = t[3] * cbd3->a + t[2] * cbd2->a + t[1] * cbd1->a + t[0] * cbd0->a;
421 CLAMP(out[0], 0.0f, 1.0f);
422 CLAMP(out[1], 0.0f, 1.0f);
423 CLAMP(out[2], 0.0f, 1.0f);
424 CLAMP(out[3], 0.0f, 1.0f);
429 if (ipotype == COLBAND_INTERP_EASE) {
431 fac = 3.0f * mfac - 2.0f * mfac * fac;
436 if (UNLIKELY(coba->color_mode == COLBAND_BLEND_HSV)) {
437 float col1[3], col2[3];
439 rgb_to_hsv_v(&cbd1->r, col1);
440 rgb_to_hsv_v(&cbd2->r, col2);
442 out[0] = colorband_hue_interp(coba->ipotype_hue, mfac, fac, col1[0], col2[0]);
443 out[1] = mfac * col1[1] + fac * col2[1];
444 out[2] = mfac * col1[2] + fac * col2[2];
445 out[3] = mfac * cbd1->a + fac * cbd2->a;
447 hsv_to_rgb_v(out, out);
449 else if (UNLIKELY(coba->color_mode == COLBAND_BLEND_HSL)) {
450 float col1[3], col2[3];
452 rgb_to_hsl_v(&cbd1->r, col1);
453 rgb_to_hsl_v(&cbd2->r, col2);
455 out[0] = colorband_hue_interp(coba->ipotype_hue, mfac, fac, col1[0], col2[0]);
456 out[1] = mfac * col1[1] + fac * col2[1];
457 out[2] = mfac * col1[2] + fac * col2[2];
458 out[3] = mfac * cbd1->a + fac * cbd2->a;
460 hsl_to_rgb_v(out, out);
463 /* COLBAND_BLEND_RGB */
464 out[0] = mfac * cbd1->r + fac * cbd2->r;
465 out[1] = mfac * cbd1->g + fac * cbd2->g;
466 out[2] = mfac * cbd1->b + fac * cbd2->b;
467 out[3] = mfac * cbd1->a + fac * cbd2->a;
472 return true; /* OK */
475 void colorband_table_RGBA(ColorBand *coba, float **array, int *size)
479 *size = CM_TABLE + 1;
480 *array = MEM_callocN(sizeof(float) * (*size) * 4, "ColorBand");
482 for (a = 0; a < *size; a++)
483 do_colorband(coba, (float)a / (float)CM_TABLE, &(*array)[a * 4]);
486 static int vergcband(const void *a1, const void *a2)
488 const CBData *x1 = a1, *x2 = a2;
490 if (x1->pos > x2->pos) return 1;
491 else if (x1->pos < x2->pos) return -1;
495 void colorband_update_sort(ColorBand *coba)
502 for (a = 0; a < coba->tot; a++)
503 coba->data[a].cur = a;
505 qsort(coba->data, coba->tot, sizeof(CBData), vergcband);
507 for (a = 0; a < coba->tot; a++) {
508 if (coba->data[a].cur == coba->cur) {
515 CBData *colorband_element_add(struct ColorBand *coba, float position)
517 if (coba->tot == MAXCOLORBAND) {
523 xnew = &coba->data[coba->tot];
524 xnew->pos = position;
526 if (coba->tot != 0) {
527 do_colorband(coba, position, &xnew->r);
535 coba->cur = coba->tot - 1;
537 colorband_update_sort(coba);
539 return coba->data + coba->cur;
542 int colorband_element_remove(struct ColorBand *coba, int index)
549 if (index < 0 || index >= coba->tot)
553 for (a = index; a < coba->tot; a++) {
554 coba->data[a] = coba->data[a + 1];
556 if (coba->cur) coba->cur--;
560 /* ******************* TEX ************************ */
562 /** Free (or release) any data used by this texture (does not free the texure itself). */
563 void BKE_texture_free(Tex *tex)
565 BKE_animdata_free((ID *)tex, false);
567 /* is no lib link block, but texture extension */
569 ntreeFreeTree(tex->nodetree);
570 MEM_freeN(tex->nodetree);
571 tex->nodetree = NULL;
574 MEM_SAFE_FREE(tex->coba);
576 BKE_texture_envmap_free(tex->env);
580 BKE_texture_pointdensity_free(tex->pd);
584 BKE_texture_voxeldata_free(tex->vd);
588 BKE_texture_ocean_free(tex->ot);
592 BKE_icon_id_delete((ID *)tex);
593 BKE_previewimg_free(&tex->preview);
596 /* ------------------------------------------------------------------------- */
598 void BKE_texture_default(Tex *tex)
600 /* BLI_assert(MEMCMP_STRUCT_OFS_IS_ZERO(tex, id)); */ /* Not here, can be called with some pointers set. :/ */
602 tex->type = TEX_IMAGE;
605 tex->flag = TEX_CHECKER_ODD;
606 tex->imaflag = TEX_INTERPOL | TEX_MIPMAP | TEX_USEALPHA;
607 tex->extend = TEX_REPEAT;
608 tex->cropxmin = tex->cropymin = 0.0;
609 tex->cropxmax = tex->cropymax = 1.0;
610 tex->texfilter = TXF_EWA;
612 tex->xrepeat = tex->yrepeat = 1;
617 tex->noisesize = 0.25;
620 tex->nabla = 0.025; // also in do_versions
623 tex->saturation = 1.0;
624 tex->filtersize = 1.0;
628 /* newnoise: init. */
630 tex->noisebasis2 = 0;
633 tex->mg_lacunarity = 2.0;
634 tex->mg_octaves = 2.0;
635 tex->mg_offset = 1.0;
637 tex->ns_outscale = 1.0;
639 tex->dist_amount = 1.0;
642 tex->vn_w2 = tex->vn_w3 = tex->vn_w4 = 0.0;
648 tex->env->stype = ENV_ANIM;
649 tex->env->clipsta = 0.1;
650 tex->env->clipend = 100;
651 tex->env->cuberes = 512;
656 tex->pd->radius = 0.3f;
657 tex->pd->falloff_type = TEX_PD_FALLOFF_STD;
661 tex->vd->resol[0] = tex->vd->resol[1] = tex->vd->resol[2] = 0;
662 tex->vd->interp_type = TEX_VD_LINEAR;
663 tex->vd->file_format = TEX_VD_SMOKE;
667 tex->ot->output = TEX_OCN_DISPLACEMENT;
668 tex->ot->object = NULL;
671 tex->iuser.fie_ima = 2;
673 tex->iuser.frames = 100;
679 void BKE_texture_type_set(Tex *tex, int type)
685 tex->vd = BKE_texture_voxeldata_add();
687 case TEX_POINTDENSITY:
689 tex->pd = BKE_texture_pointdensity_add();
692 if (tex->env == NULL)
693 tex->env = BKE_texture_envmap_add();
697 tex->ot = BKE_texture_ocean_add();
704 /* ------------------------------------------------------------------------- */
706 Tex *BKE_texture_add(Main *bmain, const char *name)
710 tex = BKE_libblock_alloc(bmain, ID_TE, name);
712 BKE_texture_default(tex);
717 /* ------------------------------------------------------------------------- */
719 void BKE_texture_mtex_default(MTex *mtex)
721 mtex->texco = TEXCO_UV;
722 mtex->mapto = MAP_COL;
724 mtex->projx = PROJ_X;
725 mtex->projy = PROJ_Y;
726 mtex->projz = PROJ_Z;
727 mtex->mapping = MTEX_FLAT;
735 mtex->texflag = MTEX_3TAP_BUMP | MTEX_BUMP_OBJECTSPACE | MTEX_MAPTO_BOUNDS;
736 mtex->colormodel = 0;
742 mtex->blendtype = MTEX_BLEND;
747 mtex->colspecfac = 1.0f;
748 mtex->mirrfac = 1.0f;
749 mtex->alphafac = 1.0f;
750 mtex->difffac = 1.0f;
751 mtex->specfac = 1.0f;
752 mtex->emitfac = 1.0f;
753 mtex->hardfac = 1.0f;
754 mtex->raymirrfac = 1.0f;
755 mtex->translfac = 1.0f;
757 mtex->colemitfac = 1.0f;
758 mtex->colreflfac = 1.0f;
759 mtex->coltransfac = 1.0f;
760 mtex->densfac = 1.0f;
761 mtex->scatterfac = 1.0f;
762 mtex->reflfac = 1.0f;
763 mtex->shadowfac = 1.0f;
764 mtex->zenupfac = 1.0f;
765 mtex->zendownfac = 1.0f;
766 mtex->blendfac = 1.0f;
767 mtex->timefac = 1.0f;
768 mtex->lengthfac = 1.0f;
769 mtex->clumpfac = 1.0f;
770 mtex->kinkfac = 1.0f;
771 mtex->kinkampfac = 1.0f;
772 mtex->roughfac = 1.0f;
773 mtex->padensfac = 1.0f;
774 mtex->lifefac = 1.0f;
775 mtex->sizefac = 1.0f;
776 mtex->ivelfac = 1.0f;
777 mtex->dampfac = 1.0f;
778 mtex->gravityfac = 1.0f;
779 mtex->fieldfac = 1.0f;
780 mtex->normapspace = MTEX_NSPACE_TANGENT;
781 mtex->brush_map_mode = MTEX_MAP_MODE_TILED;
782 mtex->random_angle = 2.0f * (float)M_PI;
783 mtex->brush_angle_mode = 0;
787 /* ------------------------------------------------------------------------- */
789 MTex *BKE_texture_mtex_add(void)
793 mtex = MEM_callocN(sizeof(MTex), "BKE_texture_mtex_add");
795 BKE_texture_mtex_default(mtex);
800 /* slot -1 for first free ID */
801 MTex *BKE_texture_mtex_add_id(ID *id, int slot)
806 give_active_mtex(id, &mtex_ar, &act);
808 if (mtex_ar == NULL) {
813 /* find first free */
815 for (i = 0; i < MAX_MTEX; i++) {
826 /* make sure slot is valid */
827 if (slot < 0 || slot >= MAX_MTEX) {
833 id_us_min((ID *)mtex_ar[slot]->tex);
834 MEM_freeN(mtex_ar[slot]);
835 mtex_ar[slot] = NULL;
837 else if (GS(id->name) == ID_MA) {
838 /* Reset this slot's ON/OFF toggle, for materials, when slot was empty. */
839 ((Material *)id)->septex &= ~(1 << slot);
842 mtex_ar[slot] = BKE_texture_mtex_add();
844 return mtex_ar[slot];
847 /* ------------------------------------------------------------------------- */
849 Tex *BKE_texture_copy(Main *bmain, Tex *tex)
853 texn = BKE_libblock_copy(bmain, &tex->id);
854 if (BKE_texture_is_image_user(tex)) {
855 id_us_plus((ID *)texn->ima);
861 if (texn->coba) texn->coba = MEM_dupallocN(texn->coba);
862 if (texn->env) texn->env = BKE_texture_envmap_copy(texn->env);
863 if (texn->pd) texn->pd = BKE_texture_pointdensity_copy(texn->pd);
864 if (texn->vd) texn->vd = MEM_dupallocN(texn->vd);
865 if (texn->ot) texn->ot = BKE_texture_ocean_copy(texn->ot);
868 if (tex->nodetree->execdata) {
869 ntreeTexEndExecTree(tex->nodetree->execdata);
871 texn->nodetree = ntreeCopyTree(bmain, tex->nodetree);
874 BKE_previewimg_id_copy(&texn->id, &tex->id);
876 BKE_id_copy_ensure_local(bmain, &tex->id, &texn->id);
881 /* texture copy without adding to main dbase */
882 Tex *BKE_texture_localize(Tex *tex)
886 texn = BKE_libblock_copy_nolib(&tex->id, false);
888 /* image texture: BKE_texture_free also doesn't decrease */
890 if (texn->coba) texn->coba = MEM_dupallocN(texn->coba);
892 texn->env = BKE_texture_envmap_copy(texn->env);
893 id_us_min(&texn->env->ima->id);
895 if (texn->pd) texn->pd = BKE_texture_pointdensity_copy(texn->pd);
897 texn->vd = MEM_dupallocN(texn->vd);
898 if (texn->vd->dataset)
899 texn->vd->dataset = MEM_dupallocN(texn->vd->dataset);
902 texn->ot = BKE_texture_ocean_copy(tex->ot);
905 texn->preview = NULL;
908 texn->nodetree = ntreeLocalize(tex->nodetree);
915 /* ------------------------------------------------------------------------- */
917 void BKE_texture_make_local(Main *bmain, Tex *tex, const bool lib_local)
919 BKE_id_make_local_generic(bmain, &tex->id, true, lib_local);
922 Tex *give_current_object_texture(Object *ob)
924 Material *ma, *node_ma;
927 if (ob == NULL) return NULL;
928 if (ob->totcol == 0 && !(ob->type == OB_LAMP)) return NULL;
930 if (ob->type == OB_LAMP) {
931 tex = give_current_lamp_texture(ob->data);
934 ma = give_current_material(ob, ob->actcol);
936 if ((node_ma = give_node_material(ma)))
939 tex = give_current_material_texture(ma);
945 Tex *give_current_lamp_texture(Lamp *la)
951 mtex = la->mtex[(int)(la->texact)];
952 if (mtex) tex = mtex->tex;
958 void set_current_lamp_texture(Lamp *la, Tex *newtex)
960 int act = la->texact;
962 if (la->mtex[act] && la->mtex[act]->tex)
963 id_us_min(&la->mtex[act]->tex->id);
966 if (!la->mtex[act]) {
967 la->mtex[act] = BKE_texture_mtex_add();
968 la->mtex[act]->texco = TEXCO_GLOB;
971 la->mtex[act]->tex = newtex;
972 id_us_plus(&newtex->id);
974 else if (la->mtex[act]) {
975 MEM_freeN(la->mtex[act]);
976 la->mtex[act] = NULL;
980 Tex *give_current_linestyle_texture(FreestyleLineStyle *linestyle)
986 mtex = linestyle->mtex[(int)(linestyle->texact)];
987 if (mtex) tex = mtex->tex;
993 void set_current_linestyle_texture(FreestyleLineStyle *linestyle, Tex *newtex)
995 int act = linestyle->texact;
997 if (linestyle->mtex[act] && linestyle->mtex[act]->tex)
998 id_us_min(&linestyle->mtex[act]->tex->id);
1001 if (!linestyle->mtex[act]) {
1002 linestyle->mtex[act] = BKE_texture_mtex_add();
1003 linestyle->mtex[act]->texco = TEXCO_STROKE;
1006 linestyle->mtex[act]->tex = newtex;
1007 id_us_plus(&newtex->id);
1009 else if (linestyle->mtex[act]) {
1010 MEM_freeN(linestyle->mtex[act]);
1011 linestyle->mtex[act] = NULL;
1015 bNode *give_current_material_texture_node(Material *ma)
1017 if (ma && ma->use_nodes && ma->nodetree)
1018 return nodeGetActiveID(ma->nodetree, ID_TE);
1023 Tex *give_current_material_texture(Material *ma)
1029 if (ma && ma->use_nodes && ma->nodetree) {
1030 /* first check texture, then material, this works together
1031 * with a hack that clears the active ID flag for textures on
1032 * making a material node active */
1033 node = nodeGetActiveID(ma->nodetree, ID_TE);
1036 tex = (Tex *)node->id;
1042 mtex = ma->mtex[(int)(ma->texact)];
1043 if (mtex) tex = mtex->tex;
1049 bool give_active_mtex(ID *id, MTex ***mtex_ar, short *act)
1051 switch (GS(id->name)) {
1053 *mtex_ar = ((Material *)id)->mtex;
1054 if (act) *act = (((Material *)id)->texact);
1057 *mtex_ar = ((World *)id)->mtex;
1058 if (act) *act = (((World *)id)->texact);
1061 *mtex_ar = ((Lamp *)id)->mtex;
1062 if (act) *act = (((Lamp *)id)->texact);
1065 *mtex_ar = ((FreestyleLineStyle *)id)->mtex;
1066 if (act) *act = (((FreestyleLineStyle *)id)->texact);
1069 *mtex_ar = ((ParticleSettings *)id)->mtex;
1070 if (act) *act = (((ParticleSettings *)id)->texact);
1081 void set_active_mtex(ID *id, short act)
1083 if (act < 0) act = 0;
1084 else if (act >= MAX_MTEX) act = MAX_MTEX - 1;
1086 switch (GS(id->name)) {
1088 ((Material *)id)->texact = act;
1091 ((World *)id)->texact = act;
1094 ((Lamp *)id)->texact = act;
1097 ((FreestyleLineStyle *)id)->texact = act;
1100 ((ParticleSettings *)id)->texact = act;
1105 void set_current_material_texture(Material *ma, Tex *newtex)
1110 if ((ma->use_nodes && ma->nodetree) &&
1111 (node = nodeGetActiveID(ma->nodetree, ID_TE)))
1113 tex = (Tex *)node->id;
1114 id_us_min(&tex->id);
1116 node->id = &newtex->id;
1117 id_us_plus(&newtex->id);
1124 int act = (int)ma->texact;
1126 tex = (ma->mtex[act]) ? ma->mtex[act]->tex : NULL;
1127 id_us_min(&tex->id);
1130 if (!ma->mtex[act]) {
1131 ma->mtex[act] = BKE_texture_mtex_add();
1132 /* Reset this slot's ON/OFF toggle, for materials, when slot was empty. */
1133 ma->septex &= ~(1 << act);
1136 ma->mtex[act]->tex = newtex;
1137 id_us_plus(&newtex->id);
1139 else if (ma->mtex[act]) {
1140 MEM_freeN(ma->mtex[act]);
1141 ma->mtex[act] = NULL;
1146 bool has_current_material_texture(Material *ma)
1150 if (ma && ma->use_nodes && ma->nodetree) {
1151 node = nodeGetActiveID(ma->nodetree, ID_TE);
1157 return (ma != NULL);
1160 Tex *give_current_world_texture(World *world)
1165 if (!world) return NULL;
1167 mtex = world->mtex[(int)(world->texact)];
1168 if (mtex) tex = mtex->tex;
1173 void set_current_world_texture(World *wo, Tex *newtex)
1175 int act = wo->texact;
1177 if (wo->mtex[act] && wo->mtex[act]->tex)
1178 id_us_min(&wo->mtex[act]->tex->id);
1181 if (!wo->mtex[act]) {
1182 wo->mtex[act] = BKE_texture_mtex_add();
1183 wo->mtex[act]->texco = TEXCO_VIEW;
1186 wo->mtex[act]->tex = newtex;
1187 id_us_plus(&newtex->id);
1189 else if (wo->mtex[act]) {
1190 MEM_freeN(wo->mtex[act]);
1191 wo->mtex[act] = NULL;
1195 Tex *give_current_brush_texture(Brush *br)
1197 return br->mtex.tex;
1200 void set_current_brush_texture(Brush *br, Tex *newtex)
1203 id_us_min(&br->mtex.tex->id);
1206 br->mtex.tex = newtex;
1207 id_us_plus(&newtex->id);
1211 Tex *give_current_particle_texture(ParticleSettings *part)
1216 if (!part) return NULL;
1218 mtex = part->mtex[(int)(part->texact)];
1219 if (mtex) tex = mtex->tex;
1224 void set_current_particle_texture(ParticleSettings *part, Tex *newtex)
1226 int act = part->texact;
1228 if (part->mtex[act] && part->mtex[act]->tex)
1229 id_us_min(&part->mtex[act]->tex->id);
1232 if (!part->mtex[act]) {
1233 part->mtex[act] = BKE_texture_mtex_add();
1234 part->mtex[act]->texco = TEXCO_ORCO;
1235 part->mtex[act]->blendtype = MTEX_MUL;
1238 part->mtex[act]->tex = newtex;
1239 id_us_plus(&newtex->id);
1241 else if (part->mtex[act]) {
1242 MEM_freeN(part->mtex[act]);
1243 part->mtex[act] = NULL;
1247 /* ------------------------------------------------------------------------- */
1249 EnvMap *BKE_texture_envmap_add(void)
1253 env = MEM_callocN(sizeof(EnvMap), "envmap");
1254 env->type = ENV_CUBE;
1255 env->stype = ENV_ANIM;
1257 env->clipend = 100.0;
1259 env->viewscale = 0.5;
1264 /* ------------------------------------------------------------------------- */
1266 EnvMap *BKE_texture_envmap_copy(EnvMap *env)
1271 envn = MEM_dupallocN(env);
1273 for (a = 0; a < 6; a++) envn->cube[a] = NULL;
1274 if (envn->ima) id_us_plus((ID *)envn->ima);
1279 /* ------------------------------------------------------------------------- */
1281 void BKE_texture_envmap_free_data(EnvMap *env)
1285 for (part = 0; part < 6; part++) {
1286 if (env->cube[part])
1287 IMB_freeImBuf(env->cube[part]);
1288 env->cube[part] = NULL;
1293 /* ------------------------------------------------------------------------- */
1295 void BKE_texture_envmap_free(EnvMap *env)
1298 BKE_texture_envmap_free_data(env);
1303 /* ------------------------------------------------------------------------- */
1305 void BKE_texture_pointdensity_init_data(PointDensity *pd)
1309 pd->falloff_type = TEX_PD_FALLOFF_STD;
1310 pd->falloff_softness = 2.0;
1311 pd->source = TEX_PD_PSYS;
1312 pd->point_tree = NULL;
1313 pd->point_data = NULL;
1314 pd->noise_size = 0.5f;
1315 pd->noise_depth = 1;
1316 pd->noise_fac = 1.0f;
1317 pd->noise_influence = TEX_PD_NOISE_STATIC;
1318 pd->coba = add_colorband(true);
1319 pd->speed_scale = 1.0f;
1323 pd->psys_cache_space = TEX_PD_WORLDSPACE;
1324 pd->falloff_curve = curvemapping_add(1, 0, 0, 1, 1);
1326 pd->falloff_curve->preset = CURVE_PRESET_LINE;
1327 pd->falloff_curve->cm->flag &= ~CUMA_EXTEND_EXTRAPOLATE;
1328 curvemap_reset(pd->falloff_curve->cm, &pd->falloff_curve->clipr, pd->falloff_curve->preset, CURVEMAP_SLOPE_POSITIVE);
1329 curvemapping_changed(pd->falloff_curve, false);
1332 PointDensity *BKE_texture_pointdensity_add(void)
1334 PointDensity *pd = MEM_callocN(sizeof(PointDensity), "pointdensity");
1335 BKE_texture_pointdensity_init_data(pd);
1339 PointDensity *BKE_texture_pointdensity_copy(PointDensity *pd)
1343 pdn = MEM_dupallocN(pd);
1344 pdn->point_tree = NULL;
1345 pdn->point_data = NULL;
1346 if (pdn->coba) pdn->coba = MEM_dupallocN(pdn->coba);
1347 pdn->falloff_curve = curvemapping_copy(pdn->falloff_curve); /* can be NULL */
1351 void BKE_texture_pointdensity_free_data(PointDensity *pd)
1353 if (pd->point_tree) {
1354 BLI_bvhtree_free(pd->point_tree);
1355 pd->point_tree = NULL;
1357 if (pd->point_data) {
1358 MEM_freeN(pd->point_data);
1359 pd->point_data = NULL;
1362 MEM_freeN(pd->coba);
1366 curvemapping_free(pd->falloff_curve); /* can be NULL */
1369 void BKE_texture_pointdensity_free(PointDensity *pd)
1371 BKE_texture_pointdensity_free_data(pd);
1375 /* ------------------------------------------------------------------------- */
1377 void BKE_texture_voxeldata_free_data(VoxelData *vd)
1380 MEM_freeN(vd->dataset);
1386 void BKE_texture_voxeldata_free(VoxelData *vd)
1388 BKE_texture_voxeldata_free_data(vd);
1392 VoxelData *BKE_texture_voxeldata_add(void)
1396 vd = MEM_callocN(sizeof(VoxelData), "voxeldata");
1398 vd->resol[0] = vd->resol[1] = vd->resol[2] = 1;
1399 vd->interp_type = TEX_VD_LINEAR;
1400 vd->file_format = TEX_VD_SMOKE;
1401 vd->int_multiplier = 1.0;
1402 vd->extend = TEX_CLIP;
1404 vd->cachedframe = -1;
1410 VoxelData *BKE_texture_voxeldata_copy(VoxelData *vd)
1414 vdn = MEM_dupallocN(vd);
1415 vdn->dataset = NULL;
1420 /* ------------------------------------------------------------------------- */
1422 OceanTex *BKE_texture_ocean_add(void)
1426 ot = MEM_callocN(sizeof(struct OceanTex), "ocean texture");
1427 ot->output = TEX_OCN_DISPLACEMENT;
1433 OceanTex *BKE_texture_ocean_copy(struct OceanTex *ot)
1435 OceanTex *otn = MEM_dupallocN(ot);
1440 void BKE_texture_ocean_free(struct OceanTex *ot)
1446 * \returns true if this texture can use its #Texture.ima (even if its NULL)
1448 bool BKE_texture_is_image_user(const struct Tex *tex)
1450 switch (tex->type) {
1458 if (tex->env->stype == ENV_LOAD) {
1469 /* ------------------------------------------------------------------------- */
1470 bool BKE_texture_dependsOnTime(const struct Tex *texture)
1472 if (texture->ima && BKE_image_is_animated(texture->ima)) {
1475 else if (texture->adt) {
1476 /* assume anything in adt means the texture is animated */
1479 else if (texture->type == TEX_NOISE) {
1480 /* noise always varies with time */
1486 /* ------------------------------------------------------------------------- */
1488 void BKE_texture_get_value_ex(
1489 const Scene *scene, Tex *texture,
1490 float *tex_co, TexResult *texres,
1491 struct ImagePool *pool,
1492 bool use_color_management)
1495 bool do_color_manage = false;
1497 if (scene && use_color_management) {
1498 do_color_manage = BKE_scene_check_color_management_enabled(scene);
1501 /* no node textures for now */
1502 result_type = multitex_ext_safe(texture, tex_co, texres, pool, do_color_manage, false);
1504 /* if the texture gave an RGB value, we assume it didn't give a valid
1505 * intensity, since this is in the context of modifiers don't use perceptual color conversion.
1506 * if the texture didn't give an RGB value, copy the intensity across
1508 if (result_type & TEX_RGB) {
1509 texres->tin = (1.0f / 3.0f) * (texres->tr + texres->tg + texres->tb);
1512 copy_v3_fl(&texres->tr, texres->tin);
1516 void BKE_texture_get_value(
1517 const Scene *scene, Tex *texture,
1518 float *tex_co, TexResult *texres, bool use_color_management)
1520 BKE_texture_get_value_ex(scene, texture, tex_co, texres, NULL, use_color_management);