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/material.c
37 #include "MEM_guardedalloc.h"
39 #include "DNA_anim_types.h"
40 #include "DNA_curve_types.h"
41 #include "DNA_group_types.h"
42 #include "DNA_material_types.h"
43 #include "DNA_mesh_types.h"
44 #include "DNA_meshdata_types.h"
45 #include "DNA_customdata_types.h"
47 #include "DNA_meta_types.h"
48 #include "DNA_node_types.h"
49 #include "DNA_object_types.h"
50 #include "DNA_scene_types.h"
53 #include "BLI_listbase.h"
54 #include "BLI_utildefines.h"
55 #include "BLI_string.h"
56 #include "BLI_array_utils.h"
58 #include "BKE_animsys.h"
59 #include "BKE_displist.h"
60 #include "BKE_global.h"
61 #include "BKE_icons.h"
62 #include "BKE_image.h"
63 #include "BKE_library.h"
65 #include "BKE_material.h"
67 #include "BKE_scene.h"
69 #include "BKE_curve.h"
70 #include "BKE_editmesh.h"
73 #include "GPU_material.h"
75 /* used in UI and render */
78 /* called on startup, creator.c */
79 void init_def_material(void)
81 init_material(&defmaterial);
84 /* not material itself */
85 void BKE_material_free(Material *ma)
87 BKE_material_free_ex(ma, true);
90 /* not material itself */
91 void BKE_material_free_ex(Material *ma, bool do_id_user)
96 for (a = 0; a < MAX_MTEX; a++) {
98 if (do_id_user && mtex && mtex->tex) mtex->tex->id.us--;
99 if (mtex) MEM_freeN(mtex);
102 if (ma->ramp_col) MEM_freeN(ma->ramp_col);
103 if (ma->ramp_spec) MEM_freeN(ma->ramp_spec);
105 BKE_animdata_free((ID *)ma);
108 BKE_previewimg_free(&ma->preview);
109 BKE_icon_delete((struct ID *)ma);
112 /* is no lib link block, but material extension */
114 ntreeFreeTree_ex(ma->nodetree, do_id_user);
115 MEM_freeN(ma->nodetree);
118 if (ma->texpaintslot)
119 MEM_freeN(ma->texpaintslot);
121 if (ma->gpumaterial.first)
122 GPU_material_free(&ma->gpumaterial);
125 void init_material(Material *ma)
127 ma->r = ma->g = ma->b = ma->ref = 0.8;
128 ma->specr = ma->specg = ma->specb = 1.0;
129 ma->mirr = ma->mirg = ma->mirb = 1.0;
133 ma->spec = ma->hasize = 0.5;
135 ma->starc = ma->ringc = 4;
138 ma->flaresize = ma->subsize = 1.0;
151 ma->strand_sta = ma->strand_end = 1.0f;
155 ma->ray_depth_tra = 2;
156 ma->fresnel_mir = 0.0;
157 ma->fresnel_tra = 0.0;
158 ma->fresnel_tra_i = 1.25;
159 ma->fresnel_mir_i = 1.25;
161 ma->tx_falloff = 1.0;
162 ma->shad_alpha = 1.0f;
165 ma->gloss_mir = ma->gloss_tra = 1.0;
166 ma->samp_gloss_mir = ma->samp_gloss_tra = 18;
167 ma->adapt_thresh_mir = ma->adapt_thresh_tra = 0.005;
169 ma->fadeto_mir = MA_RAYMIR_FADETOSKY;
171 ma->rampfac_col = 1.0;
172 ma->rampfac_spec = 1.0;
173 ma->pr_lamp = 3; /* two lamps, is bits */
174 ma->pr_type = MA_SPHERE;
176 ma->sss_radius[0] = 1.0f;
177 ma->sss_radius[1] = 1.0f;
178 ma->sss_radius[2] = 1.0f;
179 ma->sss_col[0] = 1.0f;
180 ma->sss_col[1] = 1.0f;
181 ma->sss_col[2] = 1.0f;
182 ma->sss_error = 0.05f;
183 ma->sss_scale = 0.1f;
185 ma->sss_colfac = 1.0f;
186 ma->sss_texfac = 0.0f;
187 ma->sss_front = 1.0f;
190 ma->vol.density = 1.0f;
191 ma->vol.emission = 0.0f;
192 ma->vol.scattering = 1.0f;
193 ma->vol.reflection = 1.0f;
194 ma->vol.transmission_col[0] = ma->vol.transmission_col[1] = ma->vol.transmission_col[2] = 1.0f;
195 ma->vol.reflection_col[0] = ma->vol.reflection_col[1] = ma->vol.reflection_col[2] = 1.0f;
196 ma->vol.emission_col[0] = ma->vol.emission_col[1] = ma->vol.emission_col[2] = 1.0f;
197 ma->vol.density_scale = 1.0f;
198 ma->vol.depth_cutoff = 0.01f;
199 ma->vol.stepsize_type = MA_VOL_STEP_RANDOMIZED;
200 ma->vol.stepsize = 0.2f;
201 ma->vol.shade_type = MA_VOL_SHADE_SHADED;
202 ma->vol.shadeflag |= MA_VOL_PRECACHESHADING;
203 ma->vol.precache_resolution = 50;
204 ma->vol.ms_spread = 0.2f;
205 ma->vol.ms_diff = 1.f;
206 ma->vol.ms_intensity = 1.f;
208 ma->game.flag = GEMAT_BACKCULL;
209 ma->game.alpha_blend = 0;
210 ma->game.face_orientation = 0;
212 ma->mode = MA_TRACEBLE | MA_SHADBUF | MA_SHADOW | MA_RAYBIAS | MA_TANGENT_STR | MA_ZTRANSP;
213 ma->mode2 = MA_CASTSHADOW;
214 ma->shade_flag = MA_APPROX_OCCLUSION;
218 Material *BKE_material_add(Main *bmain, const char *name)
222 ma = BKE_libblock_alloc(bmain, ID_MA, name);
229 /* XXX keep synced with next function */
230 Material *BKE_material_copy(Material *ma)
235 man = BKE_libblock_copy(&ma->id);
237 id_lib_extern((ID *)man->group);
239 for (a = 0; a < MAX_MTEX; a++) {
241 man->mtex[a] = MEM_mallocN(sizeof(MTex), "copymaterial");
242 memcpy(man->mtex[a], ma->mtex[a], sizeof(MTex));
243 id_us_plus((ID *)man->mtex[a]->tex);
247 if (ma->ramp_col) man->ramp_col = MEM_dupallocN(ma->ramp_col);
248 if (ma->ramp_spec) man->ramp_spec = MEM_dupallocN(ma->ramp_spec);
250 if (ma->preview) man->preview = BKE_previewimg_copy(ma->preview);
253 man->nodetree = ntreeCopyTree(ma->nodetree);
256 BLI_listbase_clear(&man->gpumaterial);
259 BKE_id_lib_local_paths(G.main, ma->id.lib, &man->id);
265 /* XXX (see above) material copy without adding to main dbase */
266 Material *localize_material(Material *ma)
271 man = BKE_libblock_copy_nolib(&ma->id, false);
273 /* no increment for texture ID users, in previewrender.c it prevents decrement */
274 for (a = 0; a < MAX_MTEX; a++) {
276 man->mtex[a] = MEM_mallocN(sizeof(MTex), "copymaterial");
277 memcpy(man->mtex[a], ma->mtex[a], sizeof(MTex));
281 if (ma->ramp_col) man->ramp_col = MEM_dupallocN(ma->ramp_col);
282 if (ma->ramp_spec) man->ramp_spec = MEM_dupallocN(ma->ramp_spec);
284 man->texpaintslot = NULL;
288 man->nodetree = ntreeLocalize(ma->nodetree);
290 BLI_listbase_clear(&man->gpumaterial);
295 static void extern_local_material(Material *ma)
298 for (i = 0; i < MAX_MTEX; i++) {
299 if (ma->mtex[i]) id_lib_extern((ID *)ma->mtex[i]->tex);
303 void BKE_material_make_local(Material *ma)
305 Main *bmain = G.main;
311 bool is_local = false, is_lib = false;
313 /* - only lib users: do nothing
314 * - only local users: set flag
318 if (ma->id.lib == NULL) return;
320 /* One local user; set flag and return. */
321 if (ma->id.us == 1) {
322 id_clear_lib_data(bmain, &ma->id);
323 extern_local_material(ma);
327 /* Check which other IDs reference this one to determine if it's used by
330 ob = bmain->object.first;
333 for (a = 0; a < ob->totcol; a++) {
334 if (ob->mat[a] == ma) {
335 if (ob->id.lib) is_lib = true;
336 else is_local = true;
343 me = bmain->mesh.first;
346 for (a = 0; a < me->totcol; a++) {
347 if (me->mat[a] == ma) {
348 if (me->id.lib) is_lib = true;
349 else is_local = true;
356 cu = bmain->curve.first;
359 for (a = 0; a < cu->totcol; a++) {
360 if (cu->mat[a] == ma) {
361 if (cu->id.lib) is_lib = true;
362 else is_local = true;
369 mb = bmain->mball.first;
372 for (a = 0; a < mb->totcol; a++) {
373 if (mb->mat[a] == ma) {
374 if (mb->id.lib) is_lib = true;
375 else is_local = true;
382 /* Only local users. */
383 if (is_local && is_lib == false) {
384 id_clear_lib_data(bmain, &ma->id);
385 extern_local_material(ma);
387 /* Both user and local, so copy. */
388 else if (is_local && is_lib) {
389 Material *ma_new = BKE_material_copy(ma);
393 /* Remap paths of new ID using old library as base. */
394 BKE_id_lib_local_paths(bmain, ma->id.lib, &ma_new->id);
397 ob = bmain->object.first;
400 for (a = 0; a < ob->totcol; a++) {
401 if (ob->mat[a] == ma) {
402 if (ob->id.lib == NULL) {
413 me = bmain->mesh.first;
416 for (a = 0; a < me->totcol; a++) {
417 if (me->mat[a] == ma) {
418 if (me->id.lib == NULL) {
429 cu = bmain->curve.first;
432 for (a = 0; a < cu->totcol; a++) {
433 if (cu->mat[a] == ma) {
434 if (cu->id.lib == NULL) {
445 mb = bmain->mball.first;
448 for (a = 0; a < mb->totcol; a++) {
449 if (mb->mat[a] == ma) {
450 if (mb->id.lib == NULL) {
463 /* for curve, mball, mesh types */
464 void extern_local_matarar(struct Material **matar, short totcol)
467 for (i = 0; i < totcol; i++) {
468 id_lib_extern((ID *)matar[i]);
472 Material ***give_matarar(Object *ob)
478 if (ob->type == OB_MESH) {
482 else if (ELEM(ob->type, OB_CURVE, OB_FONT, OB_SURF)) {
486 else if (ob->type == OB_MBALL) {
493 short *give_totcolp(Object *ob)
499 if (ob->type == OB_MESH) {
501 return &(me->totcol);
503 else if (ELEM(ob->type, OB_CURVE, OB_FONT, OB_SURF)) {
505 return &(cu->totcol);
507 else if (ob->type == OB_MBALL) {
509 return &(mb->totcol);
514 /* same as above but for ID's */
515 Material ***give_matarar_id(ID *id)
517 /* ensure we don't try get materials from non-obdata */
518 BLI_assert(OB_DATA_SUPPORT_ID(GS(id->name)));
520 switch (GS(id->name)) {
522 return &(((Mesh *)id)->mat);
524 return &(((Curve *)id)->mat);
526 return &(((MetaBall *)id)->mat);
531 short *give_totcolp_id(ID *id)
533 /* ensure we don't try get materials from non-obdata */
534 BLI_assert(OB_DATA_SUPPORT_ID(GS(id->name)));
536 switch (GS(id->name)) {
538 return &(((Mesh *)id)->totcol);
540 return &(((Curve *)id)->totcol);
542 return &(((MetaBall *)id)->totcol);
547 static void material_data_index_remove_id(ID *id, short index)
549 /* ensure we don't try get materials from non-obdata */
550 BLI_assert(OB_DATA_SUPPORT_ID(GS(id->name)));
552 switch (GS(id->name)) {
554 BKE_mesh_material_index_remove((Mesh *)id, index);
557 BKE_curve_material_index_remove((Curve *)id, index);
560 /* meta-elems don't have materials atm */
565 static void material_data_index_clear_id(ID *id)
567 /* ensure we don't try get materials from non-obdata */
568 BLI_assert(OB_DATA_SUPPORT_ID(GS(id->name)));
570 switch (GS(id->name)) {
572 BKE_mesh_material_index_clear((Mesh *)id);
575 BKE_curve_material_index_clear((Curve *)id);
578 /* meta-elems don't have materials atm */
583 void BKE_material_resize_id(struct ID *id, short totcol, bool do_id_user)
585 Material ***matar = give_matarar_id(id);
586 short *totcolp = give_totcolp_id(id);
592 if (do_id_user && totcol < (*totcolp)) {
594 for (i = totcol; i < (*totcolp); i++) {
595 id_us_min((ID *)(*matar)[i]);
606 *matar = MEM_recallocN(*matar, sizeof(void *) * totcol);
611 void BKE_material_append_id(ID *id, Material *ma)
614 if ((matar = give_matarar_id(id))) {
615 short *totcol = give_totcolp_id(id);
616 Material **mat = MEM_callocN(sizeof(void *) * ((*totcol) + 1), "newmatar");
617 if (*totcol) memcpy(mat, *matar, sizeof(void *) * (*totcol));
618 if (*matar) MEM_freeN(*matar);
621 (*matar)[(*totcol)++] = ma;
623 id_us_plus((ID *)ma);
624 test_object_materials(G.main, id);
628 Material *BKE_material_pop_id(ID *id, int index_i, bool update_data)
630 short index = (short)index_i;
631 Material *ret = NULL;
633 if ((matar = give_matarar_id(id))) {
634 short *totcol = give_totcolp_id(id);
635 if (index >= 0 && index < (*totcol)) {
636 ret = (*matar)[index];
637 id_us_min((ID *)ret);
645 if (index + 1 != (*totcol))
646 memmove((*matar) + index, (*matar) + (index + 1), sizeof(void *) * ((*totcol) - (index + 1)));
649 *matar = MEM_reallocN(*matar, sizeof(void *) * (*totcol));
650 test_object_materials(G.main, id);
654 /* decrease mat_nr index */
655 material_data_index_remove_id(id, index);
663 void BKE_material_clear_id(struct ID *id, bool update_data)
666 if ((matar = give_matarar_id(id))) {
667 short *totcol = give_totcolp_id(id);
675 /* decrease mat_nr index */
676 material_data_index_clear_id(id);
681 Material *give_current_material(Object *ob, short act)
683 Material ***matarar, *ma;
684 const short *totcolp;
686 if (ob == NULL) return NULL;
688 /* if object cannot have material, (totcolp == NULL) */
689 totcolp = give_totcolp(ob);
690 if (totcolp == NULL || ob->totcol == 0) return NULL;
692 /* return NULL for invalid 'act', can happen for mesh face indices */
693 if (act > ob->totcol)
697 printf("Negative material index!\n");
702 if (ob->matbits && ob->matbits[act - 1]) { /* in object */
703 ma = ob->mat[act - 1];
707 /* check for inconsistency */
708 if (*totcolp < ob->totcol)
709 ob->totcol = *totcolp;
710 if (act > ob->totcol) act = ob->totcol;
712 matarar = give_matarar(ob);
714 if (matarar && *matarar) ma = (*matarar)[act - 1];
722 ID *material_from(Object *ob, short act)
725 if (ob == NULL) return NULL;
727 if (ob->totcol == 0) return ob->data;
728 if (act == 0) act = 1;
730 if (ob->matbits[act - 1]) return (ID *)ob;
731 else return ob->data;
734 Material *give_node_material(Material *ma)
736 if (ma && ma->use_nodes && ma->nodetree) {
737 bNode *node = nodeGetActiveID(ma->nodetree, ID_MA);
740 return (Material *)node->id;
746 void BKE_material_resize_object(Object *ob, const short totcol, bool do_id_user)
751 if (do_id_user && totcol < ob->totcol) {
753 for (i = totcol; i < ob->totcol; i++) {
754 id_us_min((ID *)ob->mat[i]);
761 MEM_freeN(ob->matbits);
766 else if (ob->totcol < totcol) {
767 newmatar = MEM_callocN(sizeof(void *) * totcol, "newmatar");
768 newmatbits = MEM_callocN(sizeof(char) * totcol, "newmatbits");
770 memcpy(newmatar, ob->mat, sizeof(void *) * ob->totcol);
771 memcpy(newmatbits, ob->matbits, sizeof(char) * ob->totcol);
773 MEM_freeN(ob->matbits);
776 ob->matbits = newmatbits;
778 /* XXX, why not realloc on shrink? - campbell */
781 if (ob->totcol && ob->actcol == 0) ob->actcol = 1;
782 if (ob->actcol > ob->totcol) ob->actcol = ob->totcol;
785 void test_object_materials(Main *bmain, ID *id)
787 /* make the ob mat-array same size as 'ob->data' mat-array */
791 if (id == NULL || (totcol = give_totcolp_id(id)) == NULL) {
795 BKE_main_lock(bmain);
796 for (ob = bmain->object.first; ob; ob = ob->id.next) {
797 if (ob->data == id) {
798 BKE_material_resize_object(ob, *totcol, false);
801 BKE_main_unlock(bmain);
804 void assign_material_id(ID *id, Material *ma, short act)
806 Material *mao, **matar, ***matarar;
809 if (act > MAXMAT) return;
810 if (act < 1) act = 1;
812 /* this is needed for Python overrides,
813 * we just have to take care that the UI can't do this */
815 /* prevent crashing when using accidentally */
816 BLI_assert(id->lib == NULL);
822 totcolp = give_totcolp_id(id);
823 matarar = give_matarar_id(id);
825 if (totcolp == NULL || matarar == NULL) return;
827 if (act > *totcolp) {
828 matar = MEM_callocN(sizeof(void *) * act, "matarray1");
831 memcpy(matar, *matarar, sizeof(void *) * (*totcolp));
840 mao = (*matarar)[act - 1];
841 if (mao) mao->id.us--;
842 (*matarar)[act - 1] = ma;
845 id_us_plus((ID *)ma);
847 test_object_materials(G.main, id);
850 void assign_material(Object *ob, Material *ma, short act, int assign_type)
852 Material *mao, **matar, ***matarar;
856 if (act > MAXMAT) return;
857 if (act < 1) act = 1;
859 /* prevent crashing when using accidentally */
860 BLI_assert(ob->id.lib == NULL);
861 if (ob->id.lib) return;
865 totcolp = give_totcolp(ob);
866 matarar = give_matarar(ob);
868 if (totcolp == NULL || matarar == NULL) return;
870 if (act > *totcolp) {
871 matar = MEM_callocN(sizeof(void *) * act, "matarray1");
874 memcpy(matar, *matarar, sizeof(void *) * (*totcolp));
882 /* Determine the object/mesh linking */
883 if (assign_type == BKE_MAT_ASSIGN_USERPREF && ob->totcol && ob->actcol) {
884 /* copy from previous material */
885 bit = ob->matbits[ob->actcol - 1];
888 switch (assign_type) {
889 case BKE_MAT_ASSIGN_OBDATA:
892 case BKE_MAT_ASSIGN_OBJECT:
895 case BKE_MAT_ASSIGN_USERPREF:
897 bit = (U.flag & USER_MAT_ON_OB) ? 1 : 0;
902 if (act > ob->totcol) {
903 /* Need more space in the material arrays */
904 ob->mat = MEM_recallocN_id(ob->mat, sizeof(void *) * act, "matarray2");
905 ob->matbits = MEM_recallocN_id(ob->matbits, sizeof(char) * act, "matbits1");
911 ob->matbits[act - 1] = bit;
912 if (bit == 1) { /* in object */
913 mao = ob->mat[act - 1];
914 if (mao) mao->id.us--;
915 ob->mat[act - 1] = ma;
918 mao = (*matarar)[act - 1];
919 if (mao) mao->id.us--;
920 (*matarar)[act - 1] = ma;
924 id_us_plus((ID *)ma);
925 test_object_materials(G.main, ob->data);
929 void BKE_material_remap_object(Object *ob, const unsigned int *remap)
931 Material ***matar = give_matarar(ob);
932 const short *totcol_p = give_totcolp(ob);
934 BLI_array_permute(ob->mat, ob->totcol, remap);
937 BLI_array_permute(ob->matbits, ob->totcol, remap);
941 BLI_array_permute(*matar, *totcol_p, remap);
944 if (ob->type == OB_MESH) {
945 BKE_mesh_material_remap(ob->data, remap, ob->totcol);
947 else if (ELEM(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
948 BKE_curve_material_remap(ob->data, remap, ob->totcol);
951 /* add support for this object data! */
952 BLI_assert(matar == NULL);
957 /* XXX - this calls many more update calls per object then are needed, could be optimized */
958 void assign_matarar(struct Object *ob, struct Material ***matar, short totcol)
960 int actcol_orig = ob->actcol;
963 while (object_remove_material_slot(ob)) {}
965 /* now we have the right number of slots */
966 for (i = 0; i < totcol; i++)
967 assign_material(ob, (*matar)[i], i + 1, BKE_MAT_ASSIGN_USERPREF);
969 if (actcol_orig > ob->totcol)
970 actcol_orig = ob->totcol;
972 ob->actcol = actcol_orig;
976 short find_material_index(Object *ob, Material *ma)
981 if (ma == NULL) return 0;
983 totcolp = give_totcolp(ob);
984 matarar = give_matarar(ob);
986 if (totcolp == NULL || matarar == NULL) return 0;
988 for (a = 0; a < *totcolp; a++)
989 if ((*matarar)[a] == ma)
996 bool object_add_material_slot(Object *ob)
998 if (ob == NULL) return false;
999 if (ob->totcol >= MAXMAT) return false;
1001 assign_material(ob, NULL, ob->totcol + 1, BKE_MAT_ASSIGN_USERPREF);
1002 ob->actcol = ob->totcol;
1006 static void do_init_render_material(Material *ma, int r_mode, float *amb)
1009 int a, needuv = 0, needtang = 0;
1011 if (ma->flarec == 0) ma->flarec = 1;
1013 /* add all texcoflags from mtex, texco and mapto were cleared in advance */
1014 for (a = 0; a < MAX_MTEX; a++) {
1016 /* separate tex switching */
1017 if (ma->septex & (1 << a)) continue;
1020 if (mtex && mtex->tex && (mtex->tex->type | (mtex->tex->use_nodes && mtex->tex->nodetree) )) {
1022 ma->texco |= mtex->texco;
1023 ma->mapto |= mtex->mapto;
1025 /* always get derivatives for these textures */
1026 if (ELEM(mtex->tex->type, TEX_IMAGE, TEX_ENVMAP)) ma->texco |= TEXCO_OSA;
1027 else if (mtex->texflag & (MTEX_COMPAT_BUMP | MTEX_3TAP_BUMP | MTEX_5TAP_BUMP | MTEX_BICUBIC_BUMP)) ma->texco |= TEXCO_OSA;
1029 if (ma->texco & (TEXCO_ORCO | TEXCO_REFL | TEXCO_NORM | TEXCO_STRAND | TEXCO_STRESS)) needuv = 1;
1030 else if (ma->texco & (TEXCO_GLOB | TEXCO_UV | TEXCO_OBJECT | TEXCO_SPEED)) needuv = 1;
1031 else if (ma->texco & (TEXCO_LAVECTOR | TEXCO_VIEW)) needuv = 1;
1033 if ((ma->mapto & MAP_NORM) && (mtex->normapspace == MTEX_NSPACE_TANGENT))
1038 if (needtang) ma->mode |= MA_NORMAP_TANG;
1039 else ma->mode &= ~MA_NORMAP_TANG;
1041 if (ma->mode & (MA_VERTEXCOL | MA_VERTEXCOLP | MA_FACETEXTURE)) {
1043 if (r_mode & R_OSA) ma->texco |= TEXCO_OSA; /* for texfaces */
1045 if (needuv) ma->texco |= NEED_UV;
1047 /* since the raytracer doesnt recalc O structs for each ray, we have to preset them all */
1048 if (r_mode & R_RAYTRACE) {
1049 if ((ma->mode & (MA_RAYMIRROR | MA_SHADOW_TRA)) || ((ma->mode & MA_TRANSP) && (ma->mode & MA_RAYTRANSP))) {
1050 ma->texco |= NEED_UV | TEXCO_ORCO | TEXCO_REFL | TEXCO_NORM;
1051 if (r_mode & R_OSA) ma->texco |= TEXCO_OSA;
1056 ma->ambr = ma->amb * amb[0];
1057 ma->ambg = ma->amb * amb[1];
1058 ma->ambb = ma->amb * amb[2];
1061 /* local group override */
1062 if ((ma->shade_flag & MA_GROUP_LOCAL) && ma->id.lib && ma->group && ma->group->id.lib) {
1065 for (group = G.main->group.first; group; group = group->id.next) {
1066 if (!group->id.lib && STREQ(group->id.name, ma->group->id.name)) {
1073 static void init_render_nodetree(bNodeTree *ntree, Material *basemat, int r_mode, float *amb)
1077 for (node = ntree->nodes.first; node; node = node->next) {
1079 if (GS(node->id->name) == ID_MA) {
1080 Material *ma = (Material *)node->id;
1081 if (ma != basemat) {
1082 do_init_render_material(ma, r_mode, amb);
1083 basemat->texco |= ma->texco;
1086 basemat->mode_l |= ma->mode & ~(MA_MODE_PIPELINE | MA_SHLESS);
1087 basemat->mode2_l |= ma->mode2 & ~MA_MODE2_PIPELINE;
1088 /* basemat only considered shadeless if all node materials are too */
1089 if (!(ma->mode & MA_SHLESS))
1090 basemat->mode_l &= ~MA_SHLESS;
1092 if (ma->strand_surfnor > 0.0f)
1093 basemat->mode_l |= MA_STR_SURFDIFF;
1095 else if (node->type == NODE_GROUP)
1096 init_render_nodetree((bNodeTree *)node->id, basemat, r_mode, amb);
1101 void init_render_material(Material *mat, int r_mode, float *amb)
1104 do_init_render_material(mat, r_mode, amb);
1106 if (mat->nodetree && mat->use_nodes) {
1107 /* mode_l will take the pipeline options from the main material, and the or-ed
1108 * result of non-pipeline options from the nodes. shadeless is an exception,
1109 * mode_l will have it set when all node materials are shadeless. */
1110 mat->mode_l = (mat->mode & MA_MODE_PIPELINE) | MA_SHLESS;
1111 mat->mode2_l = mat->mode2 & MA_MODE2_PIPELINE;
1113 /* parses the geom+tex nodes */
1114 ntreeShaderGetTexcoMode(mat->nodetree, r_mode, &mat->texco, &mat->mode_l);
1116 init_render_nodetree(mat->nodetree, mat, r_mode, amb);
1118 if (!mat->nodetree->execdata)
1119 mat->nodetree->execdata = ntreeShaderBeginExecTree(mat->nodetree);
1122 mat->mode_l = mat->mode;
1123 mat->mode2_l = mat->mode2;
1125 if (mat->strand_surfnor > 0.0f)
1126 mat->mode_l |= MA_STR_SURFDIFF;
1130 void init_render_materials(Main *bmain, int r_mode, float *amb)
1134 /* clear these flags before going over materials, to make sure they
1135 * are cleared only once, otherwise node materials contained in other
1136 * node materials can go wrong */
1137 for (ma = bmain->mat.first; ma; ma = ma->id.next) {
1144 /* two steps, first initialize, then or the flags for layers */
1145 for (ma = bmain->mat.first; ma; ma = ma->id.next) {
1146 /* is_used flag comes back in convertblender.c */
1147 ma->flag &= ~MA_IS_USED;
1149 init_render_material(ma, r_mode, amb);
1152 init_render_material(&defmaterial, r_mode, amb);
1155 /* only needed for nodes now */
1156 void end_render_material(Material *mat)
1158 if (mat && mat->nodetree && mat->use_nodes) {
1159 if (mat->nodetree->execdata)
1160 ntreeShaderEndExecTree(mat->nodetree->execdata);
1164 void end_render_materials(Main *bmain)
1167 for (ma = bmain->mat.first; ma; ma = ma->id.next)
1169 end_render_material(ma);
1172 static bool material_in_nodetree(bNodeTree *ntree, Material *mat)
1176 for (node = ntree->nodes.first; node; node = node->next) {
1178 if (GS(node->id->name) == ID_MA) {
1179 if (node->id == (ID *)mat) {
1183 else if (node->type == NODE_GROUP) {
1184 if (material_in_nodetree((bNodeTree *)node->id, mat)) {
1194 bool material_in_material(Material *parmat, Material *mat)
1198 else if (parmat->nodetree && parmat->use_nodes)
1199 return material_in_nodetree(parmat->nodetree, mat);
1205 /* ****************** */
1207 /* Update drivers for materials in a nodetree */
1208 static void material_node_drivers_update(Scene *scene, bNodeTree *ntree, float ctime)
1212 /* nodetree itself */
1213 if (ntree->adt && ntree->adt->drivers.first) {
1214 BKE_animsys_evaluate_animdata(scene, &ntree->id, ntree->adt, ctime, ADT_RECALC_DRIVERS);
1218 for (node = ntree->nodes.first; node; node = node->next) {
1220 if (GS(node->id->name) == ID_MA) {
1221 material_drivers_update(scene, (Material *)node->id, ctime);
1223 else if (node->type == NODE_GROUP) {
1224 material_node_drivers_update(scene, (bNodeTree *)node->id, ctime);
1230 /* Calculate all drivers for materials
1231 * FIXME: this is really a terrible method which may result in some things being calculated
1232 * multiple times. However, without proper despgraph support for these things, we are forced
1233 * into this sort of thing...
1235 void material_drivers_update(Scene *scene, Material *ma, float ctime)
1237 //if (G.f & G_DEBUG)
1238 // printf("material_drivers_update(%s, %s)\n", scene->id.name, ma->id.name);
1240 /* Prevent infinite recursion by checking (and tagging the material) as having been visited already
1241 * (see BKE_scene_update_tagged()). This assumes ma->id.flag & LIB_DOIT isn't set by anything else
1242 * in the meantime... [#32017]
1244 if (ma->id.flag & LIB_DOIT)
1247 ma->id.flag |= LIB_DOIT;
1249 /* material itself */
1250 if (ma->adt && ma->adt->drivers.first) {
1251 BKE_animsys_evaluate_animdata(scene, &ma->id, ma->adt, ctime, ADT_RECALC_DRIVERS);
1256 material_node_drivers_update(scene, ma->nodetree, ctime);
1259 ma->id.flag &= ~LIB_DOIT;
1262 bool object_remove_material_slot(Object *ob)
1264 Material *mao, ***matarar;
1269 if (ob == NULL || ob->totcol == 0) {
1273 /* this should never happen and used to crash */
1274 if (ob->actcol <= 0) {
1275 printf("%s: invalid material index %d, report a bug!\n", __func__, ob->actcol);
1280 /* take a mesh/curve/mball as starting point, remove 1 index,
1281 * AND with all objects that share the ob->data
1283 * after that check indices in mesh/curve/mball!!!
1286 totcolp = give_totcolp(ob);
1287 matarar = give_matarar(ob);
1289 if (ELEM(NULL, matarar, *matarar)) {
1293 /* can happen on face selection in editmode */
1294 if (ob->actcol > ob->totcol) {
1295 ob->actcol = ob->totcol;
1298 /* we delete the actcol */
1299 mao = (*matarar)[ob->actcol - 1];
1300 if (mao) mao->id.us--;
1302 for (a = ob->actcol; a < ob->totcol; a++)
1303 (*matarar)[a - 1] = (*matarar)[a];
1306 if (*totcolp == 0) {
1307 MEM_freeN(*matarar);
1311 actcol = ob->actcol;
1312 obt = G.main->object.first;
1315 if (obt->data == ob->data) {
1317 /* WATCH IT: do not use actcol from ob or from obt (can become zero) */
1318 mao = obt->mat[actcol - 1];
1319 if (mao) mao->id.us--;
1321 for (a = actcol; a < obt->totcol; a++) {
1322 obt->mat[a - 1] = obt->mat[a];
1323 obt->matbits[a - 1] = obt->matbits[a];
1326 if (obt->actcol > obt->totcol) obt->actcol = obt->totcol;
1328 if (obt->totcol == 0) {
1329 MEM_freeN(obt->mat);
1330 MEM_freeN(obt->matbits);
1332 obt->matbits = NULL;
1338 /* check indices from mesh */
1339 if (ELEM(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT)) {
1340 material_data_index_remove_id((ID *)ob->data, actcol - 1);
1341 if (ob->curve_cache) {
1342 BKE_displist_free(&ob->curve_cache->disp);
1349 static bool get_mtex_slot_valid_texpaint(struct MTex *mtex)
1351 return (mtex && (mtex->texco == TEXCO_UV) &&
1352 mtex->tex && (mtex->tex->type == TEX_IMAGE) &&
1356 static bNode *nodetree_uv_node_recursive(bNode *node)
1361 for (sock = node->inputs.first; sock; sock = sock->next) {
1363 inode = sock->link->fromnode;
1364 if (inode->typeinfo->nclass == NODE_CLASS_INPUT && inode->typeinfo->type == SH_NODE_UVMAP) {
1368 return nodetree_uv_node_recursive(inode);
1376 void BKE_texpaint_slot_refresh_cache(Scene *scene, Material *ma)
1382 bool use_nodes = BKE_scene_use_new_shading_nodes(scene);
1383 bool is_bi = BKE_scene_uses_blender_internal(scene) || BKE_scene_uses_blender_game(scene);
1388 if (ma->texpaintslot) {
1389 MEM_freeN(ma->texpaintslot);
1391 ma->texpaintslot = NULL;
1394 if (scene->toolsettings->imapaint.mode == IMAGEPAINT_MODE_IMAGE) {
1395 ma->paint_active_slot = 0;
1396 ma->paint_clone_slot = 0;
1400 if (use_nodes || ma->use_nodes) {
1401 bNode *node, *active_node;
1403 if (!(ma->nodetree)) {
1404 ma->paint_active_slot = 0;
1405 ma->paint_clone_slot = 0;
1409 for (node = ma->nodetree->nodes.first; node; node = node->next) {
1410 if (node->typeinfo->nclass == NODE_CLASS_TEXTURE && node->typeinfo->type == SH_NODE_TEX_IMAGE && node->id)
1415 ma->paint_active_slot = 0;
1416 ma->paint_clone_slot = 0;
1419 ma->texpaintslot = MEM_callocN(sizeof(*ma->texpaintslot) * count, "texpaint_slots");
1421 active_node = nodeGetActiveTexture(ma->nodetree);
1423 for (node = ma->nodetree->nodes.first; node; node = node->next) {
1424 if (node->typeinfo->nclass == NODE_CLASS_TEXTURE && node->typeinfo->type == SH_NODE_TEX_IMAGE && node->id) {
1425 if (active_node == node)
1426 ma->paint_active_slot = index;
1427 ma->texpaintslot[index].ima = (Image *)node->id;
1429 /* for new renderer, we need to traverse the treeback in search of a UV node */
1431 bNode *uvnode = nodetree_uv_node_recursive(node);
1434 NodeShaderUVMap *storage = (NodeShaderUVMap *)uvnode->storage;
1435 ma->texpaintslot[index].uvname = storage->uv_map;
1436 /* set a value to index so UI knows that we have a valid pointer for the mesh */
1437 ma->texpaintslot[index].index = 0;
1440 /* just invalidate the index here so UV map does not get displayed on the UI */
1441 ma->texpaintslot[index].index = -1;
1445 ma->texpaintslot[index].index = -1;
1452 for (mtex = ma->mtex, i = 0; i < MAX_MTEX; i++, mtex++) {
1453 if (get_mtex_slot_valid_texpaint(*mtex)) {
1459 ma->paint_active_slot = 0;
1460 ma->paint_clone_slot = 0;
1464 ma->texpaintslot = MEM_callocN(sizeof(*ma->texpaintslot) * count, "texpaint_slots");
1466 for (mtex = ma->mtex, i = 0; i < MAX_MTEX; i++, mtex++) {
1467 if (get_mtex_slot_valid_texpaint(*mtex)) {
1468 ma->texpaintslot[index].ima = (*mtex)->tex->ima;
1469 ma->texpaintslot[index].uvname = (*mtex)->uvname;
1470 ma->texpaintslot[index].index = i;
1477 ma->paint_active_slot = 0;
1478 ma->paint_clone_slot = 0;
1483 ma->tot_slots = count;
1486 if (ma->paint_active_slot >= count) {
1487 ma->paint_active_slot = count - 1;
1490 if (ma->paint_clone_slot >= count) {
1491 ma->paint_clone_slot = count - 1;
1497 void BKE_texpaint_slots_refresh_object(Scene *scene, struct Object *ob)
1501 for (i = 1; i < ob->totcol + 1; i++) {
1502 Material *ma = give_current_material(ob, i);
1503 BKE_texpaint_slot_refresh_cache(scene, ma);
1508 /* r_col = current value, col = new value, (fac == 0) is no change */
1509 void ramp_blend(int type, float r_col[3], const float fac, const float col[3])
1511 float tmp, facm = 1.0f - fac;
1515 r_col[0] = facm * (r_col[0]) + fac * col[0];
1516 r_col[1] = facm * (r_col[1]) + fac * col[1];
1517 r_col[2] = facm * (r_col[2]) + fac * col[2];
1520 r_col[0] += fac * col[0];
1521 r_col[1] += fac * col[1];
1522 r_col[2] += fac * col[2];
1525 r_col[0] *= (facm + fac * col[0]);
1526 r_col[1] *= (facm + fac * col[1]);
1527 r_col[2] *= (facm + fac * col[2]);
1529 case MA_RAMP_SCREEN:
1530 r_col[0] = 1.0f - (facm + fac * (1.0f - col[0])) * (1.0f - r_col[0]);
1531 r_col[1] = 1.0f - (facm + fac * (1.0f - col[1])) * (1.0f - r_col[1]);
1532 r_col[2] = 1.0f - (facm + fac * (1.0f - col[2])) * (1.0f - r_col[2]);
1534 case MA_RAMP_OVERLAY:
1535 if (r_col[0] < 0.5f)
1536 r_col[0] *= (facm + 2.0f * fac * col[0]);
1538 r_col[0] = 1.0f - (facm + 2.0f * fac * (1.0f - col[0])) * (1.0f - r_col[0]);
1539 if (r_col[1] < 0.5f)
1540 r_col[1] *= (facm + 2.0f * fac * col[1]);
1542 r_col[1] = 1.0f - (facm + 2.0f * fac * (1.0f - col[1])) * (1.0f - r_col[1]);
1543 if (r_col[2] < 0.5f)
1544 r_col[2] *= (facm + 2.0f * fac * col[2]);
1546 r_col[2] = 1.0f - (facm + 2.0f * fac * (1.0f - col[2])) * (1.0f - r_col[2]);
1549 r_col[0] -= fac * col[0];
1550 r_col[1] -= fac * col[1];
1551 r_col[2] -= fac * col[2];
1555 r_col[0] = facm * (r_col[0]) + fac * (r_col[0]) / col[0];
1557 r_col[1] = facm * (r_col[1]) + fac * (r_col[1]) / col[1];
1559 r_col[2] = facm * (r_col[2]) + fac * (r_col[2]) / col[2];
1562 r_col[0] = facm * (r_col[0]) + fac * fabsf(r_col[0] - col[0]);
1563 r_col[1] = facm * (r_col[1]) + fac * fabsf(r_col[1] - col[1]);
1564 r_col[2] = facm * (r_col[2]) + fac * fabsf(r_col[2] - col[2]);
1567 r_col[0] = min_ff(r_col[0], col[0]) * fac + r_col[0] * facm;
1568 r_col[1] = min_ff(r_col[1], col[1]) * fac + r_col[1] * facm;
1569 r_col[2] = min_ff(r_col[2], col[2]) * fac + r_col[2] * facm;
1573 if (tmp > r_col[0]) r_col[0] = tmp;
1575 if (tmp > r_col[1]) r_col[1] = tmp;
1577 if (tmp > r_col[2]) r_col[2] = tmp;
1580 if (r_col[0] != 0.0f) {
1581 tmp = 1.0f - fac * col[0];
1584 else if ((tmp = (r_col[0]) / tmp) > 1.0f)
1589 if (r_col[1] != 0.0f) {
1590 tmp = 1.0f - fac * col[1];
1593 else if ((tmp = (r_col[1]) / tmp) > 1.0f)
1598 if (r_col[2] != 0.0f) {
1599 tmp = 1.0f - fac * col[2];
1602 else if ((tmp = (r_col[2]) / tmp) > 1.0f)
1609 tmp = facm + fac * col[0];
1613 else if ((tmp = (1.0f - (1.0f - (r_col[0])) / tmp)) < 0.0f)
1615 else if (tmp > 1.0f)
1620 tmp = facm + fac * col[1];
1623 else if ((tmp = (1.0f - (1.0f - (r_col[1])) / tmp)) < 0.0f)
1625 else if (tmp > 1.0f)
1630 tmp = facm + fac * col[2];
1633 else if ((tmp = (1.0f - (1.0f - (r_col[2])) / tmp)) < 0.0f)
1635 else if (tmp > 1.0f)
1643 float colH, colS, colV;
1644 float tmpr, tmpg, tmpb;
1645 rgb_to_hsv(col[0], col[1], col[2], &colH, &colS, &colV);
1647 rgb_to_hsv(r_col[0], r_col[1], r_col[2], &rH, &rS, &rV);
1648 hsv_to_rgb(colH, rS, rV, &tmpr, &tmpg, &tmpb);
1649 r_col[0] = facm * (r_col[0]) + fac * tmpr;
1650 r_col[1] = facm * (r_col[1]) + fac * tmpg;
1651 r_col[2] = facm * (r_col[2]) + fac * tmpb;
1658 float colH, colS, colV;
1659 rgb_to_hsv(r_col[0], r_col[1], r_col[2], &rH, &rS, &rV);
1661 rgb_to_hsv(col[0], col[1], col[2], &colH, &colS, &colV);
1662 hsv_to_rgb(rH, (facm * rS + fac * colS), rV, r_col + 0, r_col + 1, r_col + 2);
1669 float colH, colS, colV;
1670 rgb_to_hsv(r_col[0], r_col[1], r_col[2], &rH, &rS, &rV);
1671 rgb_to_hsv(col[0], col[1], col[2], &colH, &colS, &colV);
1672 hsv_to_rgb(rH, rS, (facm * rV + fac * colV), r_col + 0, r_col + 1, r_col + 2);
1678 float colH, colS, colV;
1679 float tmpr, tmpg, tmpb;
1680 rgb_to_hsv(col[0], col[1], col[2], &colH, &colS, &colV);
1682 rgb_to_hsv(r_col[0], r_col[1], r_col[2], &rH, &rS, &rV);
1683 hsv_to_rgb(colH, colS, rV, &tmpr, &tmpg, &tmpb);
1684 r_col[0] = facm * (r_col[0]) + fac * tmpr;
1685 r_col[1] = facm * (r_col[1]) + fac * tmpg;
1686 r_col[2] = facm * (r_col[2]) + fac * tmpb;
1692 float scr, scg, scb;
1694 /* first calculate non-fac based Screen mix */
1695 scr = 1.0f - (1.0f - col[0]) * (1.0f - r_col[0]);
1696 scg = 1.0f - (1.0f - col[1]) * (1.0f - r_col[1]);
1697 scb = 1.0f - (1.0f - col[2]) * (1.0f - r_col[2]);
1699 r_col[0] = facm * (r_col[0]) + fac * (((1.0f - r_col[0]) * col[0] * (r_col[0])) + (r_col[0] * scr));
1700 r_col[1] = facm * (r_col[1]) + fac * (((1.0f - r_col[1]) * col[1] * (r_col[1])) + (r_col[1] * scg));
1701 r_col[2] = facm * (r_col[2]) + fac * (((1.0f - r_col[2]) * col[2] * (r_col[2])) + (r_col[2] * scb));
1704 case MA_RAMP_LINEAR:
1706 r_col[0] = r_col[0] + fac * (2.0f * (col[0] - 0.5f));
1708 r_col[0] = r_col[0] + fac * (2.0f * (col[0]) - 1.0f);
1710 r_col[1] = r_col[1] + fac * (2.0f * (col[1] - 0.5f));
1712 r_col[1] = r_col[1] + fac * (2.0f * (col[1]) - 1.0f);
1714 r_col[2] = r_col[2] + fac * (2.0f * (col[2] - 0.5f));
1716 r_col[2] = r_col[2] + fac * (2.0f * (col[2]) - 1.0f);
1722 * \brief copy/paste buffer, if we had a proper py api that would be better
1723 * \note matcopybuf.nodetree does _NOT_ use ID's
1724 * \todo matcopybuf.nodetree's node->id's are NOT validated, this will crash!
1726 static Material matcopybuf;
1727 static short matcopied = 0;
1729 void clear_matcopybuf(void)
1731 memset(&matcopybuf, 0, sizeof(Material));
1735 void free_matcopybuf(void)
1739 for (a = 0; a < MAX_MTEX; a++) {
1740 if (matcopybuf.mtex[a]) {
1741 MEM_freeN(matcopybuf.mtex[a]);
1742 matcopybuf.mtex[a] = NULL;
1746 if (matcopybuf.ramp_col) MEM_freeN(matcopybuf.ramp_col);
1747 if (matcopybuf.ramp_spec) MEM_freeN(matcopybuf.ramp_spec);
1749 matcopybuf.ramp_col = NULL;
1750 matcopybuf.ramp_spec = NULL;
1752 if (matcopybuf.nodetree) {
1753 ntreeFreeTree_ex(matcopybuf.nodetree, false);
1754 MEM_freeN(matcopybuf.nodetree);
1755 matcopybuf.nodetree = NULL;
1761 void copy_matcopybuf(Material *ma)
1769 memcpy(&matcopybuf, ma, sizeof(Material));
1770 if (matcopybuf.ramp_col) matcopybuf.ramp_col = MEM_dupallocN(matcopybuf.ramp_col);
1771 if (matcopybuf.ramp_spec) matcopybuf.ramp_spec = MEM_dupallocN(matcopybuf.ramp_spec);
1773 for (a = 0; a < MAX_MTEX; a++) {
1774 mtex = matcopybuf.mtex[a];
1776 matcopybuf.mtex[a] = MEM_dupallocN(mtex);
1779 matcopybuf.nodetree = ntreeCopyTree_ex(ma->nodetree, G.main, false);
1780 matcopybuf.preview = NULL;
1781 BLI_listbase_clear(&matcopybuf.gpumaterial);
1785 void paste_matcopybuf(Material *ma)
1793 /* free current mat */
1794 if (ma->ramp_col) MEM_freeN(ma->ramp_col);
1795 if (ma->ramp_spec) MEM_freeN(ma->ramp_spec);
1796 for (a = 0; a < MAX_MTEX; a++) {
1798 if (mtex && mtex->tex) mtex->tex->id.us--;
1799 if (mtex) MEM_freeN(mtex);
1803 ntreeFreeTree(ma->nodetree);
1804 MEM_freeN(ma->nodetree);
1807 GPU_material_free(&ma->gpumaterial);
1810 memcpy(ma, &matcopybuf, sizeof(Material));
1813 if (matcopybuf.ramp_col) ma->ramp_col = MEM_dupallocN(matcopybuf.ramp_col);
1814 if (matcopybuf.ramp_spec) ma->ramp_spec = MEM_dupallocN(matcopybuf.ramp_spec);
1816 for (a = 0; a < MAX_MTEX; a++) {
1819 ma->mtex[a] = MEM_dupallocN(mtex);
1821 /* first check this is in main (we may have loaded another file) [#35500] */
1822 if (BLI_findindex(&G.main->tex, mtex->tex) != -1) {
1823 id_us_plus((ID *)mtex->tex);
1826 ma->mtex[a]->tex = NULL;
1832 ma->nodetree = ntreeCopyTree_ex(matcopybuf.nodetree, G.main, false);
1836 /*********************** texface to material convert functions **********************/
1837 /* encode all the TF information into a single int */
1838 static int encode_tfaceflag(MTFace *tf, int convertall)
1840 /* calculate the flag */
1841 int flag = tf->mode;
1843 /* options that change the material offline render */
1848 /* clean flags that are not being converted */
1850 flag &= ~TF_SHAREDVERT;
1851 flag &= ~TF_SHAREDCOL;
1852 flag &= ~TF_CONVERTED;
1854 /* light tface flag is ignored in GLSL mode */
1857 /* 15 is how big the flag can be - hardcoded here and in decode_tfaceflag() */
1858 flag |= tf->transp << 15;
1860 /* increase 1 so flag 0 is different than no flag yet */
1864 /* set the material options based in the tface flag */
1865 static void decode_tfaceflag(Material *ma, int flag, int convertall)
1868 GameSettings *game = &ma->game;
1870 /* flag is shifted in 1 to make 0 != no flag yet (see encode_tfaceflag) */
1873 alphablend = flag >> 15; /* encoded in the encode_tfaceflag function */
1876 /* General Material Options */
1877 if ((flag & TF_DYNAMIC) == 0) (*game).flag |= GEMAT_NOPHYSICS;
1879 /* Material Offline Rendering Properties */
1881 if (flag & TF_OBCOL) ma->shade_flag |= MA_OBCOLOR;
1884 /* Special Face Properties */
1885 if ((flag & TF_TWOSIDE) == 0) (*game).flag |= GEMAT_BACKCULL;
1886 if (flag & TF_INVISIBLE) (*game).flag |= GEMAT_INVISIBLE;
1887 if (flag & TF_BMFONT) (*game).flag |= GEMAT_TEXT;
1889 /* Face Orientation */
1890 if (flag & TF_BILLBOARD) (*game).face_orientation |= GEMAT_HALO;
1891 else if (flag & TF_BILLBOARD2) (*game).face_orientation |= GEMAT_BILLBOARD;
1892 else if (flag & TF_SHADOW) (*game).face_orientation |= GEMAT_SHADOW;
1895 if (flag & TF_ALPHASORT && ELEM(alphablend, TF_ALPHA, TF_ADD)) (*game).alpha_blend = GEMAT_ALPHA_SORT;
1896 else if (alphablend & TF_ALPHA) (*game).alpha_blend = GEMAT_ALPHA;
1897 else if (alphablend & TF_ADD) (*game).alpha_blend = GEMAT_ADD;
1898 else if (alphablend & TF_CLIP) (*game).alpha_blend = GEMAT_CLIP;
1901 /* boolean check to see if the mesh needs a material */
1902 static int check_tfaceneedmaterial(int flag)
1904 /* check if the flags we have are not deprecated != than default material options
1905 * also if only flags are visible and collision see if all objects using this mesh have this option in physics */
1907 /* flag is shifted in 1 to make 0 != no flag yet (see encode_tfaceflag) */
1910 /* deprecated flags */
1912 flag &= ~TF_SHAREDVERT;
1913 flag &= ~TF_SHAREDCOL;
1915 /* light tface flag is ignored in GLSL mode */
1918 /* automatic detected if tex image has alpha */
1919 flag &= ~(TF_ALPHA << 15);
1920 /* automatic detected if using texture */
1923 /* settings for the default NoMaterial */
1924 if (flag == TF_DYNAMIC)
1931 /* return number of digits of an integer */
1932 /* XXX to be optmized or replaced by an equivalent blender internal function */
1933 static int integer_getdigits(int number)
1936 if (number == 0) return 1;
1938 while (number != 0) {
1939 number = (int)(number / 10);
1945 static void calculate_tface_materialname(char *matname, char *newname, int flag)
1947 /* if flag has only light and collision and material matches those values
1948 * you can do strcpy(name, mat_name);
1950 int digits = integer_getdigits(flag);
1951 /* clamp the old name, remove the MA prefix and add the .TF.flag suffix
1952 * e.g. matname = "MALoooooooooooooongName"; newname = "Loooooooooooooon.TF.2" */
1953 BLI_snprintf(newname, MAX_ID_NAME, "%.*s.TF.%0*d", MAX_ID_NAME - (digits + 5), matname, digits, flag);
1956 /* returns -1 if no match */
1957 static short mesh_getmaterialnumber(Mesh *me, Material *ma)
1961 for (a = 0; a < me->totcol; a++) {
1962 if (me->mat[a] == ma) {
1970 /* append material */
1971 static short mesh_addmaterial(Mesh *me, Material *ma)
1973 BKE_material_append_id(&me->id, NULL);
1974 me->mat[me->totcol - 1] = ma;
1976 id_us_plus(&ma->id);
1978 return me->totcol - 1;
1981 static void set_facetexture_flags(Material *ma, Image *image)
1984 ma->mode |= MA_FACETEXTURE;
1985 /* we could check if the texture has alpha, but then more meshes sharing the same
1986 * material may need it. Let's make it simple. */
1987 if (BKE_image_has_alpha(image))
1988 ma->mode |= MA_FACETEXTURE_ALPHA;
1992 /* returns material number */
1993 static short convert_tfacenomaterial(Main *main, Mesh *me, MTFace *tf, int flag)
1996 char idname[MAX_ID_NAME];
1999 /* new material, the name uses the flag*/
2000 BLI_snprintf(idname, sizeof(idname), "MAMaterial.TF.%0*d", integer_getdigits(flag), flag);
2002 if ((ma = BLI_findstring(&main->mat, idname + 2, offsetof(ID, name) + 2))) {
2003 mat_nr = mesh_getmaterialnumber(me, ma);
2004 /* assign the material to the mesh */
2005 if (mat_nr == -1) mat_nr = mesh_addmaterial(me, ma);
2007 /* if needed set "Face Textures [Alpha]" Material options */
2008 set_facetexture_flags(ma, tf->tpage);
2010 /* create a new material */
2012 ma = BKE_material_add(main, idname + 2);
2015 printf("TexFace Convert: Material \"%s\" created.\n", idname + 2);
2016 mat_nr = mesh_addmaterial(me, ma);
2018 /* if needed set "Face Textures [Alpha]" Material options */
2019 set_facetexture_flags(ma, tf->tpage);
2021 decode_tfaceflag(ma, flag, 1);
2022 /* the final decoding will happen after, outside the main loop
2023 * for now store the flag into the material and change light/tex/collision
2024 * store the flag as a negative number */
2025 ma->game.flag = -flag;
2026 id_us_min((ID *)ma);
2029 printf("Error: Unable to create Material \"%s\" for Mesh \"%s\".", idname + 2, me->id.name + 2);
2033 /* set as converted, no need to go bad to this face */
2034 tf->mode |= TF_CONVERTED;
2038 /* Function to fully convert materials */
2039 static void convert_tfacematerial(Main *main, Material *ma)
2048 CustomDataLayer *cdl;
2049 char idname[MAX_ID_NAME];
2051 for (me = main->mesh.first; me; me = me->id.next) {
2052 /* check if this mesh uses this material */
2053 for (a = 0; a < me->totcol; a++)
2054 if (me->mat[a] == ma) break;
2056 /* no material found */
2057 if (a == me->totcol) continue;
2059 /* get the active tface layer */
2060 index = CustomData_get_active_layer_index(&me->fdata, CD_MTFACE);
2061 cdl = (index == -1) ? NULL : &me->fdata.layers[index];
2064 /* loop over all the faces and stop at the ones that use the material*/
2065 for (a = 0, mf = me->mface; a < me->totface; a++, mf++) {
2066 if (me->mat[mf->mat_nr] != ma) continue;
2068 /* texface data for this face */
2069 tf = ((MTFace *)cdl->data) + a;
2070 flag = encode_tfaceflag(tf, 1);
2072 /* the name of the new material */
2073 calculate_tface_materialname(ma->id.name, (char *)&idname, flag);
2075 if ((mat_new = BLI_findstring(&main->mat, idname + 2, offsetof(ID, name) + 2))) {
2076 /* material already existent, see if the mesh has it */
2077 mat_nr = mesh_getmaterialnumber(me, mat_new);
2078 /* material is not in the mesh, add it */
2079 if (mat_nr == -1) mat_nr = mesh_addmaterial(me, mat_new);
2081 /* create a new material */
2083 mat_new = BKE_material_copy(ma);
2085 /* rename the material*/
2086 BLI_strncpy(mat_new->id.name, idname, sizeof(mat_new->id.name));
2087 id_us_min((ID *)mat_new);
2089 mat_nr = mesh_addmaterial(me, mat_new);
2090 decode_tfaceflag(mat_new, flag, 1);
2093 printf("Error: Unable to create Material \"%s\" for Mesh \"%s.", idname + 2, me->id.name + 2);
2094 mat_nr = mf->mat_nr;
2099 /* if the material has a texture but no texture channel
2100 * set "Face Textures [Alpha]" Material options
2101 * actually we need to run it always, because of old behavior
2102 * of using face texture if any texture channel was present (multitex) */
2103 //if ((!mat_new->mtex[0]) && (!mat_new->mtex[0]->tex))
2104 set_facetexture_flags(mat_new, tf->tpage);
2106 /* set the material number to the face*/
2107 mf->mat_nr = mat_nr;
2109 /* remove material from mesh */
2110 for (a = 0; a < me->totcol; ) {
2111 if (me->mat[a] == ma) {
2112 BKE_material_pop_id(&me->id, a, true);
2122 #define MAT_BGE_DISPUTED -99999
2124 int do_version_tface(Main *main)
2130 CustomDataLayer *cdl;
2135 /* Operator in help menu has been removed for 2.7x */
2138 /* sometimes mesh has no materials but will need a new one. In those
2139 * cases we need to ignore the mf->mat_nr and only look at the face
2140 * mode because it can be zero as uninitialized or the 1st created material
2142 int nomaterialslots;
2144 /* alert to user to check the console */
2147 /* mark all the materials to conversion with a flag
2148 * if there is tface create a complete flag for that storing in flag
2149 * if there is tface and flag > 0: creates a new flag based on this face
2150 * if flags are different set flag to -1
2153 /* 1st part: marking mesh materials to update */
2154 for (me = main->mesh.first; me; me = me->id.next) {
2155 if (me->id.lib) continue;
2157 /* get the active tface layer */
2158 index = CustomData_get_active_layer_index(&me->fdata, CD_MTFACE);
2159 cdl = (index == -1) ? NULL : &me->fdata.layers[index];
2162 nomaterialslots = (me->totcol == 0 ? 1 : 0);
2164 /* loop over all the faces*/
2165 for (a = 0, mf = me->mface; a < me->totface; a++, mf++) {
2166 /* texface data for this face */
2167 tf = ((MTFace *)cdl->data) + a;
2169 /* conversion should happen only once */
2171 tf->mode &= ~TF_CONVERTED;
2173 if ((tf->mode & TF_CONVERTED)) continue;
2174 else tf->mode |= TF_CONVERTED;
2177 /* no material slots */
2178 if (nomaterialslots) {
2179 flag = encode_tfaceflag(tf, 1);
2181 /* create/find a new material and assign to the face */
2182 if (check_tfaceneedmaterial(flag)) {
2183 mf->mat_nr = convert_tfacenomaterial(main, me, tf, flag);
2185 /* else mark them as no-material to be reverted to 0 later */
2190 else if (mf->mat_nr < me->totcol) {
2191 ma = me->mat[mf->mat_nr];
2193 /* no material create one if necessary */
2195 /* find a new material and assign to the face */
2196 flag = encode_tfaceflag(tf, 1);
2198 /* create/find a new material and assign to the face */
2199 if (check_tfaceneedmaterial(flag))
2200 mf->mat_nr = convert_tfacenomaterial(main, me, tf, flag);
2205 /* we can't read from this if it comes from a library,
2206 * at doversion time: direct_link might not have happened on it,
2207 * so ma->mtex is not pointing to valid memory yet.
2208 * later we could, but it's better not */
2209 else if (ma->id.lib)
2212 /* material already marked as disputed */
2213 else if (ma->game.flag == MAT_BGE_DISPUTED)
2216 /* found a material */
2218 flag = encode_tfaceflag(tf, ((fileload) ? 0 : 1));
2220 /* first time changing this material */
2221 if (ma->game.flag == 0)
2222 ma->game.flag = -flag;
2224 /* mark material as disputed */
2225 else if (ma->game.flag != -flag) {
2226 ma->game.flag = MAT_BGE_DISPUTED;
2230 /* material ok so far */
2232 ma->game.flag = -flag;
2234 /* some people uses multitexture with TexFace by creating a texture
2235 * channel which not necessarily the tf->tpage image. But the game engine
2236 * was enabling it. Now it's required to set "Face Texture [Alpha] in the
2237 * material settings. */
2239 set_facetexture_flags(ma, tf->tpage);
2248 /* if we didn't have material slot and now we do, we need to
2249 * make sure the materials are correct */
2250 if (nomaterialslots) {
2251 if (me->totcol > 0) {
2252 for (a = 0, mf = me->mface; a < me->totface; a++, mf++) {
2253 if (mf->mat_nr == -1) {
2254 /* texface data for this face */
2255 tf = ((MTFace *)cdl->data) + a;
2256 mf->mat_nr = convert_tfacenomaterial(main, me, tf, encode_tfaceflag(tf, 1));
2261 for (a = 0, mf = me->mface; a < me->totface; a++, mf++) {
2269 /* 2nd part - conversion */
2270 /* skip library files */
2272 /* we shouldn't loop through the materials created in the loop. make the loop stop at its original length) */
2273 for (ma = main->mat.first, a = 0; ma; ma = ma->id.next, a++) {
2274 if (ma->id.lib) continue;
2276 /* disputed material */
2277 if (ma->game.flag == MAT_BGE_DISPUTED) {
2280 printf("Warning: material \"%s\" skipped.\n", ma->id.name + 2);
2284 convert_tfacematerial(main, ma);
2289 /* no conflicts in this material - 90% of cases
2290 * convert from tface system to material */
2291 else if (ma->game.flag < 0) {
2292 decode_tfaceflag(ma, -(ma->game.flag), 1);
2294 /* material is good make sure all faces using
2295 * this material are set to converted */
2297 for (me = main->mesh.first; me; me = me->id.next) {
2298 /* check if this mesh uses this material */
2299 for (a = 0; a < me->totcol; a++)
2300 if (me->mat[a] == ma) break;
2302 /* no material found */
2303 if (a == me->totcol) continue;
2305 /* get the active tface layer */
2306 index = CustomData_get_active_layer_index(&me->fdata, CD_MTFACE);
2307 cdl = (index == -1) ? NULL : &me->fdata.layers[index];
2310 /* loop over all the faces and stop at the ones that use the material*/
2311 for (a = 0, mf = me->mface; a < me->totface; a++, mf++) {
2312 if (me->mat[mf->mat_nr] == ma) {
2313 /* texface data for this face */
2314 tf = ((MTFace *)cdl->data) + a;
2315 tf->mode |= TF_CONVERTED;
2321 /* material is not used by faces with texface
2322 * set the default flag - do it only once */
2325 ma->game.flag = GEMAT_BACKCULL;