ccbccac85cf8c3038208736d05df3e884068479c
[blender.git] / source / blender / blenkernel / intern / texture.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
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.
8  *
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.
13  *
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.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/blenkernel/intern/texture.c
29  *  \ingroup bke
30  */
31
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <math.h>
37
38 #include "MEM_guardedalloc.h"
39
40 #include "BLI_blenlib.h"
41 #include "BLI_dynlib.h"
42 #include "BLI_math.h"
43 #include "BLI_kdopbvh.h"
44 #include "BLI_utildefines.h"
45
46 #include "DNA_key_types.h"
47 #include "DNA_object_types.h"
48 #include "DNA_lamp_types.h"
49 #include "DNA_material_types.h"
50 #include "DNA_world_types.h"
51 #include "DNA_brush_types.h"
52 #include "DNA_node_types.h"
53 #include "DNA_color_types.h"
54 #include "DNA_particle_types.h"
55
56 #include "IMB_imbuf.h"
57
58 #include "BKE_global.h"
59 #include "BKE_main.h"
60 #include "BKE_ocean.h"
61
62 #include "BKE_library.h"
63 #include "BKE_image.h"
64 #include "BKE_material.h"
65 #include "BKE_texture.h"
66 #include "BKE_key.h"
67 #include "BKE_icons.h"
68 #include "BKE_node.h"
69 #include "BKE_animsys.h"
70 #include "BKE_colortools.h"
71 #include "BKE_scene.h"
72
73 #include "RE_shader_ext.h"
74
75 /* ****************** Mapping ******************* */
76
77 TexMapping *add_tex_mapping(int type)
78 {
79         TexMapping *texmap = MEM_callocN(sizeof(TexMapping), "TexMapping");
80         
81         default_tex_mapping(texmap, type);
82         
83         return texmap;
84 }
85
86 void default_tex_mapping(TexMapping *texmap, int type)
87 {
88         memset(texmap, 0, sizeof(TexMapping));
89
90         texmap->size[0] = texmap->size[1] = texmap->size[2] = 1.0f;
91         texmap->max[0] = texmap->max[1] = texmap->max[2] = 1.0f;
92         unit_m4(texmap->mat);
93
94         texmap->projx = PROJ_X;
95         texmap->projy = PROJ_Y;
96         texmap->projz = PROJ_Z;
97         texmap->mapping = MTEX_FLAT;
98         texmap->type = type;
99 }
100
101 void init_tex_mapping(TexMapping *texmap)
102 {
103         float smat[4][4], rmat[4][4], tmat[4][4], proj[4][4], size[3];
104
105         if (texmap->projx == PROJ_X && texmap->projy == PROJ_Y && texmap->projz == PROJ_Z &&
106             is_zero_v3(texmap->loc) && is_zero_v3(texmap->rot) && is_one_v3(texmap->size))
107         {
108                 unit_m4(texmap->mat);
109
110                 texmap->flag |= TEXMAP_UNIT_MATRIX;
111         }
112         else {
113                 /* axis projection */
114                 zero_m4(proj);
115                 proj[3][3] = 1.0f;
116
117                 if (texmap->projx != PROJ_N)
118                         proj[texmap->projx - 1][0] = 1.0f;
119                 if (texmap->projy != PROJ_N)
120                         proj[texmap->projy - 1][1] = 1.0f;
121                 if (texmap->projz != PROJ_N)
122                         proj[texmap->projz - 1][2] = 1.0f;
123
124                 /* scale */
125                 copy_v3_v3(size, texmap->size);
126
127                 if (ELEM(texmap->type, TEXMAP_TYPE_TEXTURE, TEXMAP_TYPE_NORMAL)) {
128                         /* keep matrix invertible */
129                         if (fabsf(size[0]) < 1e-5f)
130                                 size[0] = signf(size[0]) * 1e-5f;
131                         if (fabsf(size[1]) < 1e-5f)
132                                 size[1] = signf(size[1]) * 1e-5f;
133                         if (fabsf(size[2]) < 1e-5f)
134                                 size[2] = signf(size[2]) * 1e-5f;
135                 }
136                 
137                 size_to_mat4(smat, texmap->size);
138
139                 /* rotation */
140                 eul_to_mat4(rmat, texmap->rot);
141
142                 /* translation */
143                 unit_m4(tmat);
144                 copy_v3_v3(tmat[3], texmap->loc);
145
146                 if (texmap->type == TEXMAP_TYPE_TEXTURE) {
147                         /* to transform a texture, the inverse transform needs
148                          * to be applied to the texture coordinate */
149                         mul_serie_m4(texmap->mat, tmat, rmat, smat, 0, 0, 0, 0, 0);
150                         invert_m4(texmap->mat);
151                 }
152                 else if (texmap->type == TEXMAP_TYPE_POINT) {
153                         /* forward transform */
154                         mul_serie_m4(texmap->mat, tmat, rmat, smat, 0, 0, 0, 0, 0);
155                 }
156                 else if (texmap->type == TEXMAP_TYPE_VECTOR) {
157                         /* no translation for vectors */
158                         mul_m4_m4m4(texmap->mat, rmat, smat);
159                 }
160                 else if (texmap->type == TEXMAP_TYPE_NORMAL) {
161                         /* no translation for normals, and inverse transpose */
162                         mul_m4_m4m4(texmap->mat, rmat, smat);
163                         invert_m4(texmap->mat);
164                         transpose_m4(texmap->mat);
165                 }
166
167                 /* projection last */
168                 mul_m4_m4m4(texmap->mat, texmap->mat, proj);
169
170                 texmap->flag &= ~TEXMAP_UNIT_MATRIX;
171         }
172 }
173
174 ColorMapping *add_color_mapping(void)
175 {
176         ColorMapping *colormap = MEM_callocN(sizeof(ColorMapping), "ColorMapping");
177         
178         default_color_mapping(colormap);
179         
180         return colormap;
181 }
182
183 void default_color_mapping(ColorMapping *colormap)
184 {
185         memset(colormap, 0, sizeof(ColorMapping));
186
187         init_colorband(&colormap->coba, true);
188
189         colormap->bright = 1.0;
190         colormap->contrast = 1.0;
191         colormap->saturation = 1.0;
192
193         colormap->blend_color[0] = 0.8f;
194         colormap->blend_color[1] = 0.8f;
195         colormap->blend_color[2] = 0.8f;
196         colormap->blend_type = MA_RAMP_BLEND;
197         colormap->blend_factor = 0.0f;
198 }
199
200 /* ****************** COLORBAND ******************* */
201
202 void init_colorband(ColorBand *coba, bool rangetype)
203 {
204         int a;
205         
206         coba->data[0].pos = 0.0;
207         coba->data[1].pos = 1.0;
208         
209         if (rangetype == 0) {
210                 coba->data[0].r = 0.0;
211                 coba->data[0].g = 0.0;
212                 coba->data[0].b = 0.0;
213                 coba->data[0].a = 0.0;
214
215                 coba->data[1].r = 1.0;
216                 coba->data[1].g = 1.0;
217                 coba->data[1].b = 1.0;
218                 coba->data[1].a = 1.0;
219         }
220         else {
221                 coba->data[0].r = 0.0;
222                 coba->data[0].g = 0.0;
223                 coba->data[0].b = 0.0;
224                 coba->data[0].a = 1.0;
225
226                 coba->data[1].r = 1.0;
227                 coba->data[1].g = 1.0;
228                 coba->data[1].b = 1.0;
229                 coba->data[1].a = 1.0;
230         }
231
232         for (a = 2; a < MAXCOLORBAND; a++) {
233                 coba->data[a].r = 0.5;
234                 coba->data[a].g = 0.5;
235                 coba->data[a].b = 0.5;
236                 coba->data[a].a = 1.0;
237                 coba->data[a].pos = 0.5;
238         }
239         
240         coba->tot = 2;
241         
242 }
243
244 ColorBand *add_colorband(bool rangetype)
245 {
246         ColorBand *coba;
247         
248         coba = MEM_callocN(sizeof(ColorBand), "colorband");
249         init_colorband(coba, rangetype);
250         
251         return coba;
252 }
253
254 /* ------------------------------------------------------------------------- */
255
256 int do_colorband(const ColorBand *coba, float in, float out[4])
257 {
258         const CBData *cbd1, *cbd2, *cbd0, *cbd3;
259         float fac, mfac, t[4];
260         int a;
261         
262         if (coba == NULL || coba->tot == 0) return 0;
263         
264         cbd1 = coba->data;
265         if (coba->tot == 1) {
266                 out[0] = cbd1->r;
267                 out[1] = cbd1->g;
268                 out[2] = cbd1->b;
269                 out[3] = cbd1->a;
270         }
271         else {
272                 if (in <= cbd1->pos && coba->ipotype < 2) {
273                         out[0] = cbd1->r;
274                         out[1] = cbd1->g;
275                         out[2] = cbd1->b;
276                         out[3] = cbd1->a;
277                 }
278                 else {
279                         CBData left, right;
280                         
281                         /* we're looking for first pos > in */
282                         for (a = 0; a < coba->tot; a++, cbd1++) if (cbd1->pos > in) break;
283                                 
284                         if (a == coba->tot) {
285                                 cbd2 = cbd1 - 1;
286                                 right = *cbd2;
287                                 right.pos = 1.0f;
288                                 cbd1 = &right;
289                         }
290                         else if (a == 0) {
291                                 left = *cbd1;
292                                 left.pos = 0.0f;
293                                 cbd2 = &left;
294                         }
295                         else {
296                                 cbd2 = cbd1 - 1;
297                         }
298                         
299                         if (in >= cbd1->pos && coba->ipotype < 2) {
300                                 out[0] = cbd1->r;
301                                 out[1] = cbd1->g;
302                                 out[2] = cbd1->b;
303                                 out[3] = cbd1->a;
304                         }
305                         else {
306                 
307                                 if (cbd2->pos != cbd1->pos)
308                                         fac = (in - cbd1->pos) / (cbd2->pos - cbd1->pos);
309                                 else {
310                                         /* was setting to 0.0 in 2.56 & previous, but this
311                                          * is incorrect for the last element, see [#26732] */
312                                         fac = (a != coba->tot) ? 0.0f : 1.0f;
313                                 }
314                                 
315                                 if (coba->ipotype == 4) {
316                                         /* constant */
317                                         out[0] = cbd2->r;
318                                         out[1] = cbd2->g;
319                                         out[2] = cbd2->b;
320                                         out[3] = cbd2->a;
321                                         return 1;
322                                 }
323                                 
324                                 if (coba->ipotype >= 2) {
325                                         /* ipo from right to left: 3 2 1 0 */
326                                         
327                                         if (a >= coba->tot - 1) cbd0 = cbd1;
328                                         else cbd0 = cbd1 + 1;
329                                         if (a < 2) cbd3 = cbd2;
330                                         else cbd3 = cbd2 - 1;
331                                         
332                                         CLAMP(fac, 0.0f, 1.0f);
333                                         
334                                         if (coba->ipotype == 3)
335                                                 key_curve_position_weights(fac, t, KEY_CARDINAL);
336                                         else
337                                                 key_curve_position_weights(fac, t, KEY_BSPLINE);
338
339                                         out[0] = t[3] * cbd3->r + t[2] * cbd2->r + t[1] * cbd1->r + t[0] * cbd0->r;
340                                         out[1] = t[3] * cbd3->g + t[2] * cbd2->g + t[1] * cbd1->g + t[0] * cbd0->g;
341                                         out[2] = t[3] * cbd3->b + t[2] * cbd2->b + t[1] * cbd1->b + t[0] * cbd0->b;
342                                         out[3] = t[3] * cbd3->a + t[2] * cbd2->a + t[1] * cbd1->a + t[0] * cbd0->a;
343                                         CLAMP(out[0], 0.0f, 1.0f);
344                                         CLAMP(out[1], 0.0f, 1.0f);
345                                         CLAMP(out[2], 0.0f, 1.0f);
346                                         CLAMP(out[3], 0.0f, 1.0f);
347                                 }
348                                 else {
349                                 
350                                         if (coba->ipotype == 1) { /* EASE */
351                                                 mfac = fac * fac;
352                                                 fac = 3.0f * mfac - 2.0f * mfac * fac;
353                                         }
354                                         mfac = 1.0f - fac;
355                                         
356                                         out[0] = mfac * cbd1->r + fac * cbd2->r;
357                                         out[1] = mfac * cbd1->g + fac * cbd2->g;
358                                         out[2] = mfac * cbd1->b + fac * cbd2->b;
359                                         out[3] = mfac * cbd1->a + fac * cbd2->a;
360                                 }
361                         }
362                 }
363         }
364         return 1;   /* OK */
365 }
366
367 void colorband_table_RGBA(ColorBand *coba, float **array, int *size)
368 {
369         int a;
370         
371         *size = CM_TABLE + 1;
372         *array = MEM_callocN(sizeof(float) * (*size) * 4, "ColorBand");
373
374         for (a = 0; a < *size; a++)
375                 do_colorband(coba, (float)a / (float)CM_TABLE, &(*array)[a * 4]);
376 }
377
378 static int vergcband(const void *a1, const void *a2)
379 {
380         const CBData *x1 = a1, *x2 = a2;
381
382         if (x1->pos > x2->pos) return 1;
383         else if (x1->pos < x2->pos) return -1;
384         return 0;
385 }
386
387 void colorband_update_sort(ColorBand *coba)
388 {
389         int a;
390         
391         if (coba->tot < 2)
392                 return;
393         
394         for (a = 0; a < coba->tot; a++)
395                 coba->data[a].cur = a;
396
397         qsort(coba->data, coba->tot, sizeof(CBData), vergcband);
398
399         for (a = 0; a < coba->tot; a++) {
400                 if (coba->data[a].cur == coba->cur) {
401                         coba->cur = a;
402                         break;
403                 }
404         }
405 }
406
407 CBData *colorband_element_add(struct ColorBand *coba, float position)
408 {
409         if (coba->tot == MAXCOLORBAND) {
410                 return NULL;
411         }
412         else if (coba->tot > 0) {
413                 CBData *xnew;
414                 float col[4];
415
416                 do_colorband(coba, position, col);
417
418                 xnew = &coba->data[coba->tot];
419                 xnew->pos = position;
420
421                 xnew->r = col[0];
422                 xnew->g = col[1];
423                 xnew->b = col[2];
424                 xnew->a = col[3];
425         }
426
427         coba->tot++;
428         coba->cur = coba->tot - 1;
429
430         colorband_update_sort(coba);
431
432         return coba->data + coba->cur;
433 }
434
435 int colorband_element_remove(struct ColorBand *coba, int index)
436 {
437         int a;
438
439         if (coba->tot < 2)
440                 return 0;
441
442         if (index < 0 || index >= coba->tot)
443                 return 0;
444
445         for (a = index; a < coba->tot; a++) {
446                 coba->data[a] = coba->data[a + 1];
447         }
448         if (coba->cur) coba->cur--;
449         coba->tot--;
450         return 1;
451 }
452
453 /* ******************* TEX ************************ */
454
455 void BKE_texture_free(Tex *tex)
456 {
457         if (tex->coba) MEM_freeN(tex->coba);
458         if (tex->env) BKE_free_envmap(tex->env);
459         if (tex->pd) BKE_free_pointdensity(tex->pd);
460         if (tex->vd) BKE_free_voxeldata(tex->vd);
461         if (tex->ot) BKE_free_oceantex(tex->ot);
462         BKE_free_animdata((struct ID *)tex);
463         
464         BKE_previewimg_free(&tex->preview);
465         BKE_icon_delete((struct ID *)tex);
466         tex->id.icon_id = 0;
467         
468         if (tex->nodetree) {
469                 ntreeFreeTree(tex->nodetree);
470                 MEM_freeN(tex->nodetree);
471         }
472 }
473
474 /* ------------------------------------------------------------------------- */
475
476 void default_tex(Tex *tex)
477 {
478         tex->type = TEX_CLOUDS;
479         tex->stype = 0;
480         tex->flag = TEX_CHECKER_ODD;
481         tex->imaflag = TEX_INTERPOL | TEX_MIPMAP | TEX_USEALPHA;
482         tex->extend = TEX_REPEAT;
483         tex->cropxmin = tex->cropymin = 0.0;
484         tex->cropxmax = tex->cropymax = 1.0;
485         tex->texfilter = TXF_EWA;
486         tex->afmax = 8;
487         tex->xrepeat = tex->yrepeat = 1;
488         tex->fie_ima = 2;
489         tex->sfra = 1;
490         tex->frames = 0;
491         tex->offset = 0;
492         tex->noisesize = 0.25;
493         tex->noisedepth = 2;
494         tex->turbul = 5.0;
495         tex->nabla = 0.025;  // also in do_versions
496         tex->bright = 1.0;
497         tex->contrast = 1.0;
498         tex->saturation = 1.0;
499         tex->filtersize = 1.0;
500         tex->rfac = 1.0;
501         tex->gfac = 1.0;
502         tex->bfac = 1.0;
503         /* newnoise: init. */
504         tex->noisebasis = 0;
505         tex->noisebasis2 = 0;
506         /* musgrave */
507         tex->mg_H = 1.0;
508         tex->mg_lacunarity = 2.0;
509         tex->mg_octaves = 2.0;
510         tex->mg_offset = 1.0;
511         tex->mg_gain = 1.0;
512         tex->ns_outscale = 1.0;
513         /* distnoise */
514         tex->dist_amount = 1.0;
515         /* voronoi */
516         tex->vn_w1 = 1.0;
517         tex->vn_w2 = tex->vn_w3 = tex->vn_w4 = 0.0;
518         tex->vn_mexp = 2.5;
519         tex->vn_distm = 0;
520         tex->vn_coltype = 0;
521
522         if (tex->env) {
523                 tex->env->stype = ENV_ANIM;
524                 tex->env->clipsta = 0.1;
525                 tex->env->clipend = 100;
526                 tex->env->cuberes = 600;
527                 tex->env->depth = 0;
528         }
529
530         if (tex->pd) {
531                 tex->pd->radius = 0.3f;
532                 tex->pd->falloff_type = TEX_PD_FALLOFF_STD;
533         }
534         
535         if (tex->vd) {
536                 tex->vd->resol[0] = tex->vd->resol[1] = tex->vd->resol[2] = 0;
537                 tex->vd->interp_type = TEX_VD_LINEAR;
538                 tex->vd->file_format = TEX_VD_SMOKE;
539         }
540         
541         if (tex->ot) {
542                 tex->ot->output = TEX_OCN_DISPLACEMENT;
543                 tex->ot->object = NULL;
544         }
545         
546         tex->iuser.fie_ima = 2;
547         tex->iuser.ok = 1;
548         tex->iuser.frames = 100;
549         tex->iuser.sfra = 1;
550         
551         tex->preview = NULL;
552 }
553
554 void tex_set_type(Tex *tex, int type)
555 {
556         switch (type) {
557                         
558                 case TEX_VOXELDATA:
559                         if (tex->vd == NULL)
560                                 tex->vd = BKE_add_voxeldata();
561                         break;
562                 case TEX_POINTDENSITY:
563                         if (tex->pd == NULL)
564                                 tex->pd = BKE_add_pointdensity();
565                         break;
566                 case TEX_ENVMAP:
567                         if (tex->env == NULL)
568                                 tex->env = BKE_add_envmap();
569                         break;
570                 case TEX_OCEAN:
571                         if (tex->ot == NULL)
572                                 tex->ot = BKE_add_oceantex();
573                         break;
574         }
575         
576         tex->type = type;
577 }
578
579 /* ------------------------------------------------------------------------- */
580
581 Tex *add_texture(Main *bmain, const char *name)
582 {
583         Tex *tex;
584
585         tex = BKE_libblock_alloc(&bmain->tex, ID_TE, name);
586         
587         default_tex(tex);
588         
589         return tex;
590 }
591
592 /* ------------------------------------------------------------------------- */
593
594 void default_mtex(MTex *mtex)
595 {
596         mtex->texco = TEXCO_ORCO;
597         mtex->mapto = MAP_COL;
598         mtex->object = NULL;
599         mtex->projx = PROJ_X;
600         mtex->projy = PROJ_Y;
601         mtex->projz = PROJ_Z;
602         mtex->mapping = MTEX_FLAT;
603         mtex->ofs[0] = 0.0;
604         mtex->ofs[1] = 0.0;
605         mtex->ofs[2] = 0.0;
606         mtex->size[0] = 1.0;
607         mtex->size[1] = 1.0;
608         mtex->size[2] = 1.0;
609         mtex->tex = NULL;
610         mtex->texflag = MTEX_3TAP_BUMP | MTEX_BUMP_OBJECTSPACE | MTEX_MAPTO_BOUNDS;
611         mtex->colormodel = 0;
612         mtex->r = 1.0;
613         mtex->g = 0.0;
614         mtex->b = 1.0;
615         mtex->k = 1.0;
616         mtex->def_var = 1.0;
617         mtex->blendtype = MTEX_BLEND;
618         mtex->colfac = 1.0;
619         mtex->norfac = 1.0;
620         mtex->varfac = 1.0;
621         mtex->dispfac = 0.2;
622         mtex->colspecfac = 1.0f;
623         mtex->mirrfac = 1.0f;
624         mtex->alphafac = 1.0f;
625         mtex->difffac = 1.0f;
626         mtex->specfac = 1.0f;
627         mtex->emitfac = 1.0f;
628         mtex->hardfac = 1.0f;
629         mtex->raymirrfac = 1.0f;
630         mtex->translfac = 1.0f;
631         mtex->ambfac = 1.0f;
632         mtex->colemitfac = 1.0f;
633         mtex->colreflfac = 1.0f;
634         mtex->coltransfac = 1.0f;
635         mtex->densfac = 1.0f;
636         mtex->scatterfac = 1.0f;
637         mtex->reflfac = 1.0f;
638         mtex->shadowfac = 1.0f;
639         mtex->zenupfac = 1.0f;
640         mtex->zendownfac = 1.0f;
641         mtex->blendfac = 1.0f;
642         mtex->timefac = 1.0f;
643         mtex->lengthfac = 1.0f;
644         mtex->clumpfac = 1.0f;
645         mtex->kinkfac = 1.0f;
646         mtex->roughfac = 1.0f;
647         mtex->padensfac = 1.0f;
648         mtex->lifefac = 1.0f;
649         mtex->sizefac = 1.0f;
650         mtex->ivelfac = 1.0f;
651         mtex->dampfac = 1.0f;
652         mtex->gravityfac = 1.0f;
653         mtex->fieldfac = 1.0f;
654         mtex->normapspace = MTEX_NSPACE_TANGENT;
655         mtex->brush_map_mode = MTEX_MAP_MODE_TILED;
656 }
657
658
659 /* ------------------------------------------------------------------------- */
660
661 MTex *add_mtex(void)
662 {
663         MTex *mtex;
664         
665         mtex = MEM_callocN(sizeof(MTex), "add_mtex");
666         
667         default_mtex(mtex);
668         
669         return mtex;
670 }
671
672 /* slot -1 for first free ID */
673 MTex *add_mtex_id(ID *id, int slot)
674 {
675         MTex **mtex_ar;
676         short act;
677
678         give_active_mtex(id, &mtex_ar, &act);
679
680         if (mtex_ar == NULL) {
681                 return NULL;
682         }
683         
684         if (slot == -1) {
685                 /* find first free */
686                 int i;
687                 for (i = 0; i < MAX_MTEX; i++) {
688                         if (!mtex_ar[i]) {
689                                 slot = i;
690                                 break;
691                         }
692                 }
693                 if (slot == -1) {
694                         return NULL;
695                 }
696         }
697         else {
698                 /* make sure slot is valid */
699                 if (slot < 0 || slot >= MAX_MTEX) {
700                         return NULL;
701                 }
702         }
703
704         if (mtex_ar[slot]) {
705                 id_us_min((ID *)mtex_ar[slot]->tex);
706                 MEM_freeN(mtex_ar[slot]);
707                 mtex_ar[slot] = NULL;
708         }
709
710         mtex_ar[slot] = add_mtex();
711
712         return mtex_ar[slot];
713 }
714
715 /* ------------------------------------------------------------------------- */
716
717 Tex *BKE_texture_copy(Tex *tex)
718 {
719         Tex *texn;
720         
721         texn = BKE_libblock_copy(&tex->id);
722         if (texn->type == TEX_IMAGE) id_us_plus((ID *)texn->ima);
723         else texn->ima = NULL;
724         
725         if (texn->coba) texn->coba = MEM_dupallocN(texn->coba);
726         if (texn->env) texn->env = BKE_copy_envmap(texn->env);
727         if (texn->pd) texn->pd = BKE_copy_pointdensity(texn->pd);
728         if (texn->vd) texn->vd = MEM_dupallocN(texn->vd);
729         if (texn->ot) texn->ot = BKE_copy_oceantex(texn->ot);
730         if (tex->preview) texn->preview = BKE_previewimg_copy(tex->preview);
731
732         if (tex->nodetree) {
733                 if (tex->nodetree->execdata) {
734                         ntreeTexEndExecTree(tex->nodetree->execdata);
735                 }
736                 texn->nodetree = ntreeCopyTree(tex->nodetree);
737         }
738         
739         return texn;
740 }
741
742 /* texture copy without adding to main dbase */
743 Tex *localize_texture(Tex *tex)
744 {
745         Tex *texn;
746         
747         texn = BKE_libblock_copy(&tex->id);
748         BLI_remlink(&G.main->tex, texn);
749         
750         /* image texture: BKE_texture_free also doesn't decrease */
751         
752         if (texn->coba) texn->coba = MEM_dupallocN(texn->coba);
753         if (texn->env) {
754                 texn->env = BKE_copy_envmap(texn->env);
755                 id_us_min(&texn->env->ima->id);
756         }
757         if (texn->pd) texn->pd = BKE_copy_pointdensity(texn->pd);
758         if (texn->vd) {
759                 texn->vd = MEM_dupallocN(texn->vd);
760                 if (texn->vd->dataset)
761                         texn->vd->dataset = MEM_dupallocN(texn->vd->dataset);
762         }
763         if (texn->ot) {
764                 texn->ot = BKE_copy_oceantex(tex->ot);
765         }
766         
767         texn->preview = NULL;
768         
769         if (tex->nodetree) {
770                 texn->nodetree = ntreeLocalize(tex->nodetree);
771         }
772         
773         return texn;
774 }
775
776
777 /* ------------------------------------------------------------------------- */
778
779 static void extern_local_texture(Tex *tex)
780 {
781         id_lib_extern((ID *)tex->ima);
782 }
783
784 void BKE_texture_make_local(Tex *tex)
785 {
786         Main *bmain = G.main;
787         Material *ma;
788         World *wrld;
789         Lamp *la;
790         Brush *br;
791         ParticleSettings *pa;
792         int a, is_local = FALSE, is_lib = FALSE;
793
794         /* - only lib users: do nothing
795          * - only local users: set flag
796          * - mixed: make copy
797          */
798         
799         if (tex->id.lib == NULL) return;
800
801         if (tex->id.us == 1) {
802                 id_clear_lib_data(bmain, &tex->id);
803                 extern_local_texture(tex);
804                 return;
805         }
806         
807         ma = bmain->mat.first;
808         while (ma) {
809                 for (a = 0; a < MAX_MTEX; a++) {
810                         if (ma->mtex[a] && ma->mtex[a]->tex == tex) {
811                                 if (ma->id.lib) is_lib = TRUE;
812                                 else is_local = TRUE;
813                         }
814                 }
815                 ma = ma->id.next;
816         }
817         la = bmain->lamp.first;
818         while (la) {
819                 for (a = 0; a < MAX_MTEX; a++) {
820                         if (la->mtex[a] && la->mtex[a]->tex == tex) {
821                                 if (la->id.lib) is_lib = TRUE;
822                                 else is_local = TRUE;
823                         }
824                 }
825                 la = la->id.next;
826         }
827         wrld = bmain->world.first;
828         while (wrld) {
829                 for (a = 0; a < MAX_MTEX; a++) {
830                         if (wrld->mtex[a] && wrld->mtex[a]->tex == tex) {
831                                 if (wrld->id.lib) is_lib = TRUE;
832                                 else is_local = TRUE;
833                         }
834                 }
835                 wrld = wrld->id.next;
836         }
837         br = bmain->brush.first;
838         while (br) {
839                 if (br->mtex.tex == tex) {
840                         if (br->id.lib) is_lib = TRUE;
841                         else is_local = TRUE;
842                 }
843                 if (br->mask_mtex.tex == tex) {
844                         if (br->id.lib) is_lib = TRUE;
845                         else is_local = TRUE;
846                 }
847                 br = br->id.next;
848         }
849         pa = bmain->particle.first;
850         while (pa) {
851                 for (a = 0; a < MAX_MTEX; a++) {
852                         if (pa->mtex[a] && pa->mtex[a]->tex == tex) {
853                                 if (pa->id.lib) is_lib = TRUE;
854                                 else is_local = TRUE;
855                         }
856                 }
857                 pa = pa->id.next;
858         }
859         
860         if (is_local && is_lib == FALSE) {
861                 id_clear_lib_data(bmain, &tex->id);
862                 extern_local_texture(tex);
863         }
864         else if (is_local && is_lib) {
865                 Tex *tex_new = BKE_texture_copy(tex);
866
867                 tex_new->id.us = 0;
868
869                 /* Remap paths of new ID using old library as base. */
870                 BKE_id_lib_local_paths(bmain, tex->id.lib, &tex_new->id);
871                 
872                 ma = bmain->mat.first;
873                 while (ma) {
874                         for (a = 0; a < MAX_MTEX; a++) {
875                                 if (ma->mtex[a] && ma->mtex[a]->tex == tex) {
876                                         if (ma->id.lib == NULL) {
877                                                 ma->mtex[a]->tex = tex_new;
878                                                 tex_new->id.us++;
879                                                 tex->id.us--;
880                                         }
881                                 }
882                         }
883                         ma = ma->id.next;
884                 }
885                 la = bmain->lamp.first;
886                 while (la) {
887                         for (a = 0; a < MAX_MTEX; a++) {
888                                 if (la->mtex[a] && la->mtex[a]->tex == tex) {
889                                         if (la->id.lib == NULL) {
890                                                 la->mtex[a]->tex = tex_new;
891                                                 tex_new->id.us++;
892                                                 tex->id.us--;
893                                         }
894                                 }
895                         }
896                         la = la->id.next;
897                 }
898                 wrld = bmain->world.first;
899                 while (wrld) {
900                         for (a = 0; a < MAX_MTEX; a++) {
901                                 if (wrld->mtex[a] && wrld->mtex[a]->tex == tex) {
902                                         if (wrld->id.lib == NULL) {
903                                                 wrld->mtex[a]->tex = tex_new;
904                                                 tex_new->id.us++;
905                                                 tex->id.us--;
906                                         }
907                                 }
908                         }
909                         wrld = wrld->id.next;
910                 }
911                 br = bmain->brush.first;
912                 while (br) {
913                         if (br->mtex.tex == tex) {
914                                 if (br->id.lib == NULL) {
915                                         br->mtex.tex = tex_new;
916                                         tex_new->id.us++;
917                                         tex->id.us--;
918                                 }
919                         }
920                         if (br->mask_mtex.tex == tex) {
921                                 if (br->id.lib == NULL) {
922                                         br->mask_mtex.tex = tex_new;
923                                         tex_new->id.us++;
924                                         tex->id.us--;
925                                 }
926                         }
927                         br = br->id.next;
928                 }
929                 pa = bmain->particle.first;
930                 while (pa) {
931                         for (a = 0; a < MAX_MTEX; a++) {
932                                 if (pa->mtex[a] && pa->mtex[a]->tex == tex) {
933                                         if (pa->id.lib == NULL) {
934                                                 pa->mtex[a]->tex = tex_new;
935                                                 tex_new->id.us++;
936                                                 tex->id.us--;
937                                         }
938                                 }
939                         }
940                         pa = pa->id.next;
941                 }
942         }
943 }
944
945 Tex *give_current_object_texture(Object *ob)
946 {
947         Material *ma, *node_ma;
948         Tex *tex = NULL;
949         
950         if (ob == NULL) return NULL;
951         if (ob->totcol == 0 && !(ob->type == OB_LAMP)) return NULL;
952         
953         if (ob->type == OB_LAMP) {
954                 tex = give_current_lamp_texture(ob->data);
955         }
956         else {
957                 ma = give_current_material(ob, ob->actcol);
958
959                 if ((node_ma = give_node_material(ma)))
960                         ma = node_ma;
961
962                 tex = give_current_material_texture(ma);
963         }
964         
965         return tex;
966 }
967
968 Tex *give_current_lamp_texture(Lamp *la)
969 {
970         MTex *mtex = NULL;
971         Tex *tex = NULL;
972
973         if (la) {
974                 mtex = la->mtex[(int)(la->texact)];
975                 if (mtex) tex = mtex->tex;
976         }
977
978         return tex;
979 }
980
981 void set_current_lamp_texture(Lamp *la, Tex *newtex)
982 {
983         int act = la->texact;
984
985         if (la->mtex[act] && la->mtex[act]->tex)
986                 id_us_min(&la->mtex[act]->tex->id);
987
988         if (newtex) {
989                 if (!la->mtex[act]) {
990                         la->mtex[act] = add_mtex();
991                         la->mtex[act]->texco = TEXCO_GLOB;
992                 }
993                 
994                 la->mtex[act]->tex = newtex;
995                 id_us_plus(&newtex->id);
996         }
997         else if (la->mtex[act]) {
998                 MEM_freeN(la->mtex[act]);
999                 la->mtex[act] = NULL;
1000         }
1001 }
1002
1003 bNode *give_current_material_texture_node(Material *ma)
1004 {
1005         if (ma && ma->use_nodes && ma->nodetree)
1006                 return nodeGetActiveID(ma->nodetree, ID_TE);
1007         
1008         return NULL;
1009 }
1010
1011 Tex *give_current_material_texture(Material *ma)
1012 {
1013         MTex *mtex = NULL;
1014         Tex *tex = NULL;
1015         bNode *node;
1016         
1017         if (ma && ma->use_nodes && ma->nodetree) {
1018                 /* first check texture, then material, this works together
1019                  * with a hack that clears the active ID flag for textures on
1020                  * making a material node active */
1021                 node = nodeGetActiveID(ma->nodetree, ID_TE);
1022
1023                 if (node) {
1024                         tex = (Tex *)node->id;
1025                         ma = NULL;
1026                 }
1027         }
1028
1029         if (ma) {
1030                 mtex = ma->mtex[(int)(ma->texact)];
1031                 if (mtex) tex = mtex->tex;
1032         }
1033         
1034         return tex;
1035 }
1036
1037 int give_active_mtex(ID *id, MTex ***mtex_ar, short *act)
1038 {
1039         switch (GS(id->name)) {
1040                 case ID_MA:
1041                         *mtex_ar =       ((Material *)id)->mtex;
1042                         if (act) *act =  (((Material *)id)->texact);
1043                         break;
1044                 case ID_WO:
1045                         *mtex_ar =       ((World *)id)->mtex;
1046                         if (act) *act =  (((World *)id)->texact);
1047                         break;
1048                 case ID_LA:
1049                         *mtex_ar =       ((Lamp *)id)->mtex;
1050                         if (act) *act =  (((Lamp *)id)->texact);
1051                         break;
1052                 case ID_PA:
1053                         *mtex_ar =       ((ParticleSettings *)id)->mtex;
1054                         if (act) *act =  (((ParticleSettings *)id)->texact);
1055                         break;
1056                 default:
1057                         *mtex_ar = NULL;
1058                         if (act) *act =  0;
1059                         return FALSE;
1060         }
1061
1062         return TRUE;
1063 }
1064
1065 void set_active_mtex(ID *id, short act)
1066 {
1067         if (act < 0) act = 0;
1068         else if (act >= MAX_MTEX) act = MAX_MTEX - 1;
1069
1070         switch (GS(id->name)) {
1071                 case ID_MA:
1072                         ((Material *)id)->texact = act;
1073                         break;
1074                 case ID_WO:
1075                         ((World *)id)->texact = act;
1076                         break;
1077                 case ID_LA:
1078                         ((Lamp *)id)->texact = act;
1079                         break;
1080                 case ID_PA:
1081                         ((ParticleSettings *)id)->texact = act;
1082                         break;
1083         }
1084 }
1085
1086 void set_current_material_texture(Material *ma, Tex *newtex)
1087 {
1088         Tex *tex = NULL;
1089         bNode *node;
1090
1091         if ((ma->use_nodes && ma->nodetree) &&
1092             (node = nodeGetActiveID(ma->nodetree, ID_TE)))
1093         {
1094                 tex = (Tex *)node->id;
1095                 id_us_min(&tex->id);
1096                 if (newtex) {
1097                         node->id = &newtex->id;
1098                         id_us_plus(&newtex->id);
1099                 }
1100                 else {
1101                         node->id = NULL;
1102                 }
1103         }
1104         else {
1105                 int act = (int)ma->texact;
1106
1107                 tex = (ma->mtex[act]) ? ma->mtex[act]->tex : NULL;
1108                 id_us_min(&tex->id);
1109
1110                 if (newtex) {
1111                         if (!ma->mtex[act])
1112                                 ma->mtex[act] = add_mtex();
1113                         
1114                         ma->mtex[act]->tex = newtex;
1115                         id_us_plus(&newtex->id);
1116                 }
1117                 else if (ma->mtex[act]) {
1118                         MEM_freeN(ma->mtex[act]);
1119                         ma->mtex[act] = NULL;
1120                 }
1121         }
1122 }
1123
1124 int has_current_material_texture(Material *ma)
1125 {
1126         bNode *node;
1127
1128         if (ma && ma->use_nodes && ma->nodetree) {
1129                 node = nodeGetActiveID(ma->nodetree, ID_TE);
1130
1131                 if (node)
1132                         return 1;
1133         }
1134
1135         return (ma != NULL);
1136 }
1137
1138 Tex *give_current_world_texture(World *world)
1139 {
1140         MTex *mtex = NULL;
1141         Tex *tex = NULL;
1142         
1143         if (!world) return NULL;
1144         
1145         mtex = world->mtex[(int)(world->texact)];
1146         if (mtex) tex = mtex->tex;
1147         
1148         return tex;
1149 }
1150
1151 void set_current_world_texture(World *wo, Tex *newtex)
1152 {
1153         int act = wo->texact;
1154
1155         if (wo->mtex[act] && wo->mtex[act]->tex)
1156                 id_us_min(&wo->mtex[act]->tex->id);
1157
1158         if (newtex) {
1159                 if (!wo->mtex[act]) {
1160                         wo->mtex[act] = add_mtex();
1161                         wo->mtex[act]->texco = TEXCO_VIEW;
1162                 }
1163                 
1164                 wo->mtex[act]->tex = newtex;
1165                 id_us_plus(&newtex->id);
1166         }
1167         else if (wo->mtex[act]) {
1168                 MEM_freeN(wo->mtex[act]);
1169                 wo->mtex[act] = NULL;
1170         }
1171 }
1172
1173 Tex *give_current_brush_texture(Brush *br)
1174 {
1175         return br->mtex.tex;
1176 }
1177
1178 void set_current_brush_texture(Brush *br, Tex *newtex)
1179 {
1180         if (br->mtex.tex)
1181                 id_us_min(&br->mtex.tex->id);
1182
1183         if (newtex) {
1184                 br->mtex.tex = newtex;
1185                 id_us_plus(&newtex->id);
1186         }
1187 }
1188
1189 Tex *give_current_particle_texture(ParticleSettings *part)
1190 {
1191         MTex *mtex = NULL;
1192         Tex *tex = NULL;
1193         
1194         if (!part) return NULL;
1195         
1196         mtex = part->mtex[(int)(part->texact)];
1197         if (mtex) tex = mtex->tex;
1198         
1199         return tex;
1200 }
1201
1202 void set_current_particle_texture(ParticleSettings *part, Tex *newtex)
1203 {
1204         int act = part->texact;
1205
1206         if (part->mtex[act] && part->mtex[act]->tex)
1207                 id_us_min(&part->mtex[act]->tex->id);
1208
1209         if (newtex) {
1210                 if (!part->mtex[act]) {
1211                         part->mtex[act] = add_mtex();
1212                         part->mtex[act]->texco = TEXCO_ORCO;
1213                         part->mtex[act]->blendtype = MTEX_MUL;
1214                 }
1215                 
1216                 part->mtex[act]->tex = newtex;
1217                 id_us_plus(&newtex->id);
1218         }
1219         else if (part->mtex[act]) {
1220                 MEM_freeN(part->mtex[act]);
1221                 part->mtex[act] = NULL;
1222         }
1223 }
1224
1225 /* ------------------------------------------------------------------------- */
1226
1227 EnvMap *BKE_add_envmap(void)
1228 {
1229         EnvMap *env;
1230         
1231         env = MEM_callocN(sizeof(EnvMap), "envmap");
1232         env->type = ENV_CUBE;
1233         env->stype = ENV_ANIM;
1234         env->clipsta = 0.1;
1235         env->clipend = 100.0;
1236         env->cuberes = 600;
1237         env->viewscale = 0.5;
1238         
1239         return env;
1240
1241
1242 /* ------------------------------------------------------------------------- */
1243
1244 EnvMap *BKE_copy_envmap(EnvMap *env)
1245 {
1246         EnvMap *envn;
1247         int a;
1248         
1249         envn = MEM_dupallocN(env);
1250         envn->ok = 0;
1251         for (a = 0; a < 6; a++) envn->cube[a] = NULL;
1252         if (envn->ima) id_us_plus((ID *)envn->ima);
1253         
1254         return envn;
1255 }
1256
1257 /* ------------------------------------------------------------------------- */
1258
1259 void BKE_free_envmapdata(EnvMap *env)
1260 {
1261         unsigned int part;
1262         
1263         for (part = 0; part < 6; part++) {
1264                 if (env->cube[part])
1265                         IMB_freeImBuf(env->cube[part]);
1266                 env->cube[part] = NULL;
1267         }
1268         env->ok = 0;
1269 }
1270
1271 /* ------------------------------------------------------------------------- */
1272
1273 void BKE_free_envmap(EnvMap *env)
1274 {
1275         
1276         BKE_free_envmapdata(env);
1277         MEM_freeN(env);
1278         
1279 }
1280
1281 /* ------------------------------------------------------------------------- */
1282
1283 PointDensity *BKE_add_pointdensity(void)
1284 {
1285         PointDensity *pd;
1286         
1287         pd = MEM_callocN(sizeof(PointDensity), "pointdensity");
1288         pd->flag = 0;
1289         pd->radius = 0.3f;
1290         pd->falloff_type = TEX_PD_FALLOFF_STD;
1291         pd->falloff_softness = 2.0;
1292         pd->source = TEX_PD_PSYS;
1293         pd->point_tree = NULL;
1294         pd->point_data = NULL;
1295         pd->noise_size = 0.5f;
1296         pd->noise_depth = 1;
1297         pd->noise_fac = 1.0f;
1298         pd->noise_influence = TEX_PD_NOISE_STATIC;
1299         pd->coba = add_colorband(true);
1300         pd->speed_scale = 1.0f;
1301         pd->totpoints = 0;
1302         pd->object = NULL;
1303         pd->psys = 0;
1304         pd->psys_cache_space = TEX_PD_WORLDSPACE;
1305         pd->falloff_curve = curvemapping_add(1, 0, 0, 1, 1);
1306
1307         pd->falloff_curve->preset = CURVE_PRESET_LINE;
1308         pd->falloff_curve->cm->flag &= ~CUMA_EXTEND_EXTRAPOLATE;
1309         curvemap_reset(pd->falloff_curve->cm, &pd->falloff_curve->clipr, pd->falloff_curve->preset, CURVEMAP_SLOPE_POSITIVE);
1310         curvemapping_changed(pd->falloff_curve, FALSE);
1311
1312         return pd;
1313
1314
1315 PointDensity *BKE_copy_pointdensity(PointDensity *pd)
1316 {
1317         PointDensity *pdn;
1318
1319         pdn = MEM_dupallocN(pd);
1320         pdn->point_tree = NULL;
1321         pdn->point_data = NULL;
1322         if (pdn->coba) pdn->coba = MEM_dupallocN(pdn->coba);
1323         pdn->falloff_curve = curvemapping_copy(pdn->falloff_curve); /* can be NULL */
1324         return pdn;
1325 }
1326
1327 void BKE_free_pointdensitydata(PointDensity *pd)
1328 {
1329         if (pd->point_tree) {
1330                 BLI_bvhtree_free(pd->point_tree);
1331                 pd->point_tree = NULL;
1332         }
1333         if (pd->point_data) {
1334                 MEM_freeN(pd->point_data);
1335                 pd->point_data = NULL;
1336         }
1337         if (pd->coba) {
1338                 MEM_freeN(pd->coba);
1339                 pd->coba = NULL;
1340         }
1341
1342         curvemapping_free(pd->falloff_curve); /* can be NULL */
1343 }
1344
1345 void BKE_free_pointdensity(PointDensity *pd)
1346 {
1347         BKE_free_pointdensitydata(pd);
1348         MEM_freeN(pd);
1349 }
1350
1351 /* ------------------------------------------------------------------------- */
1352
1353 void BKE_free_voxeldatadata(VoxelData *vd)
1354 {
1355         if (vd->dataset) {
1356                 MEM_freeN(vd->dataset);
1357                 vd->dataset = NULL;
1358         }
1359
1360 }
1361  
1362 void BKE_free_voxeldata(VoxelData *vd)
1363 {
1364         BKE_free_voxeldatadata(vd);
1365         MEM_freeN(vd);
1366 }
1367  
1368 VoxelData *BKE_add_voxeldata(void)
1369 {
1370         VoxelData *vd;
1371
1372         vd = MEM_callocN(sizeof(VoxelData), "voxeldata");
1373         vd->dataset = NULL;
1374         vd->resol[0] = vd->resol[1] = vd->resol[2] = 1;
1375         vd->interp_type = TEX_VD_LINEAR;
1376         vd->file_format = TEX_VD_SMOKE;
1377         vd->int_multiplier = 1.0;
1378         vd->extend = TEX_CLIP;
1379         vd->object = NULL;
1380         vd->cachedframe = -1;
1381         vd->ok = 0;
1382         
1383         return vd;
1384 }
1385  
1386 VoxelData *BKE_copy_voxeldata(VoxelData *vd)
1387 {
1388         VoxelData *vdn;
1389
1390         vdn = MEM_dupallocN(vd);
1391         vdn->dataset = NULL;
1392
1393         return vdn;
1394 }
1395
1396 /* ------------------------------------------------------------------------- */
1397
1398 OceanTex *BKE_add_oceantex(void)
1399 {
1400         OceanTex *ot;
1401         
1402         ot = MEM_callocN(sizeof(struct OceanTex), "ocean texture");
1403         ot->output = TEX_OCN_DISPLACEMENT;
1404         ot->object = NULL;
1405         
1406         return ot;
1407 }
1408
1409 OceanTex *BKE_copy_oceantex(struct OceanTex *ot)
1410 {
1411         OceanTex *otn = MEM_dupallocN(ot);
1412         
1413         return otn;
1414 }
1415
1416 void BKE_free_oceantex(struct OceanTex *ot)
1417 {
1418         MEM_freeN(ot);
1419 }
1420
1421
1422 /* ------------------------------------------------------------------------- */
1423 bool BKE_texture_dependsOnTime(const struct Tex *texture)
1424 {
1425         if (texture->ima &&
1426             ELEM(texture->ima->source, IMA_SRC_SEQUENCE, IMA_SRC_MOVIE))
1427         {
1428                 return 1;
1429         }
1430         else if (texture->adt) {
1431                 /* assume anything in adt means the texture is animated */
1432                 return 1;
1433         }
1434         else if (texture->type == TEX_NOISE) {
1435                 /* noise always varies with time */
1436                 return 1;
1437         }
1438         return 0;
1439 }
1440
1441 /* ------------------------------------------------------------------------- */
1442
1443 void BKE_texture_get_value(Scene *scene, Tex *texture, float *tex_co, TexResult *texres, bool use_color_management)
1444 {
1445         int result_type;
1446         bool do_color_manage = false;
1447
1448         if (scene && use_color_management) {
1449                 do_color_manage = BKE_scene_check_color_management_enabled(scene);
1450         }
1451
1452         /* no node textures for now */
1453         result_type = multitex_ext_safe(texture, tex_co, texres, NULL, do_color_manage);
1454
1455         /* if the texture gave an RGB value, we assume it didn't give a valid
1456          * intensity, since this is in the context of modifiers don't use perceptual color conversion.
1457          * if the texture didn't give an RGB value, copy the intensity across
1458          */
1459         if (result_type & TEX_RGB) {
1460                 texres->tin = (1.0f / 3.0f) * (texres->tr + texres->tg + texres->tb);
1461         }
1462         else {
1463                 copy_v3_fl(&texres->tr, texres->tin);
1464         }
1465 }