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"
57 #include "BKE_animsys.h"
58 #include "BKE_displist.h"
59 #include "BKE_global.h"
60 #include "BKE_icons.h"
61 #include "BKE_image.h"
62 #include "BKE_library.h"
64 #include "BKE_material.h"
67 #include "BKE_curve.h"
69 #include "GPU_material.h"
71 /* used in UI and render */
74 /* called on startup, creator.c */
75 void init_def_material(void)
77 init_material(&defmaterial);
80 /* not material itself */
81 void BKE_material_free(Material *ma)
83 BKE_material_free_ex(ma, true);
86 /* not material itself */
87 void BKE_material_free_ex(Material *ma, bool do_id_user)
92 for (a = 0; a < MAX_MTEX; a++) {
94 if (do_id_user && mtex && mtex->tex) mtex->tex->id.us--;
95 if (mtex) MEM_freeN(mtex);
98 if (ma->ramp_col) MEM_freeN(ma->ramp_col);
99 if (ma->ramp_spec) MEM_freeN(ma->ramp_spec);
101 BKE_free_animdata((ID *)ma);
104 BKE_previewimg_free(&ma->preview);
105 BKE_icon_delete((struct ID *)ma);
108 /* is no lib link block, but material extension */
110 ntreeFreeTree_ex(ma->nodetree, do_id_user);
111 MEM_freeN(ma->nodetree);
114 if (ma->gpumaterial.first)
115 GPU_material_free(ma);
118 void init_material(Material *ma)
120 ma->r = ma->g = ma->b = ma->ref = 0.8;
121 ma->specr = ma->specg = ma->specb = 1.0;
122 ma->mirr = ma->mirg = ma->mirb = 1.0;
126 ma->spec = ma->hasize = 0.5;
128 ma->starc = ma->ringc = 4;
131 ma->flaresize = ma->subsize = 1.0;
144 ma->strand_sta = ma->strand_end = 1.0f;
148 ma->ray_depth_tra = 2;
149 ma->fresnel_mir = 0.0;
150 ma->fresnel_tra = 0.0;
151 ma->fresnel_tra_i = 1.25;
152 ma->fresnel_mir_i = 1.25;
154 ma->tx_falloff = 1.0;
155 ma->shad_alpha = 1.0f;
158 ma->gloss_mir = ma->gloss_tra = 1.0;
159 ma->samp_gloss_mir = ma->samp_gloss_tra = 18;
160 ma->adapt_thresh_mir = ma->adapt_thresh_tra = 0.005;
162 ma->fadeto_mir = MA_RAYMIR_FADETOSKY;
164 ma->rampfac_col = 1.0;
165 ma->rampfac_spec = 1.0;
166 ma->pr_lamp = 3; /* two lamps, is bits */
167 ma->pr_type = MA_SPHERE;
169 ma->sss_radius[0] = 1.0f;
170 ma->sss_radius[1] = 1.0f;
171 ma->sss_radius[2] = 1.0f;
172 ma->sss_col[0] = 1.0f;
173 ma->sss_col[1] = 1.0f;
174 ma->sss_col[2] = 1.0f;
175 ma->sss_error = 0.05f;
176 ma->sss_scale = 0.1f;
178 ma->sss_colfac = 1.0f;
179 ma->sss_texfac = 0.0f;
180 ma->sss_front = 1.0f;
183 ma->vol.density = 1.0f;
184 ma->vol.emission = 0.0f;
185 ma->vol.scattering = 1.0f;
186 ma->vol.reflection = 1.0f;
187 ma->vol.transmission_col[0] = ma->vol.transmission_col[1] = ma->vol.transmission_col[2] = 1.0f;
188 ma->vol.reflection_col[0] = ma->vol.reflection_col[1] = ma->vol.reflection_col[2] = 1.0f;
189 ma->vol.emission_col[0] = ma->vol.emission_col[1] = ma->vol.emission_col[2] = 1.0f;
190 ma->vol.density_scale = 1.0f;
191 ma->vol.depth_cutoff = 0.01f;
192 ma->vol.stepsize_type = MA_VOL_STEP_RANDOMIZED;
193 ma->vol.stepsize = 0.2f;
194 ma->vol.shade_type = MA_VOL_SHADE_SHADED;
195 ma->vol.shadeflag |= MA_VOL_PRECACHESHADING;
196 ma->vol.precache_resolution = 50;
197 ma->vol.ms_spread = 0.2f;
198 ma->vol.ms_diff = 1.f;
199 ma->vol.ms_intensity = 1.f;
201 ma->game.flag = GEMAT_BACKCULL;
202 ma->game.alpha_blend = 0;
203 ma->game.face_orientation = 0;
205 ma->mode = MA_TRACEBLE | MA_SHADBUF | MA_SHADOW | MA_RAYBIAS | MA_TANGENT_STR | MA_ZTRANSP;
206 ma->shade_flag = MA_APPROX_OCCLUSION;
210 Material *BKE_material_add(Main *bmain, const char *name)
214 ma = BKE_libblock_alloc(bmain, ID_MA, name);
221 /* XXX keep synced with next function */
222 Material *BKE_material_copy(Material *ma)
227 man = BKE_libblock_copy(&ma->id);
229 id_lib_extern((ID *)man->group);
231 for (a = 0; a < MAX_MTEX; a++) {
233 man->mtex[a] = MEM_mallocN(sizeof(MTex), "copymaterial");
234 memcpy(man->mtex[a], ma->mtex[a], sizeof(MTex));
235 id_us_plus((ID *)man->mtex[a]->tex);
239 if (ma->ramp_col) man->ramp_col = MEM_dupallocN(ma->ramp_col);
240 if (ma->ramp_spec) man->ramp_spec = MEM_dupallocN(ma->ramp_spec);
242 if (ma->preview) man->preview = BKE_previewimg_copy(ma->preview);
245 man->nodetree = ntreeCopyTree(ma->nodetree);
248 BLI_listbase_clear(&man->gpumaterial);
253 /* XXX (see above) material copy without adding to main dbase */
254 Material *localize_material(Material *ma)
259 man = BKE_libblock_copy_nolib(&ma->id);
261 /* no increment for texture ID users, in previewrender.c it prevents decrement */
262 for (a = 0; a < MAX_MTEX; a++) {
264 man->mtex[a] = MEM_mallocN(sizeof(MTex), "copymaterial");
265 memcpy(man->mtex[a], ma->mtex[a], sizeof(MTex));
269 if (ma->ramp_col) man->ramp_col = MEM_dupallocN(ma->ramp_col);
270 if (ma->ramp_spec) man->ramp_spec = MEM_dupallocN(ma->ramp_spec);
275 man->nodetree = ntreeLocalize(ma->nodetree);
277 BLI_listbase_clear(&man->gpumaterial);
282 static void extern_local_material(Material *ma)
285 for (i = 0; i < MAX_MTEX; i++) {
286 if (ma->mtex[i]) id_lib_extern((ID *)ma->mtex[i]->tex);
290 void BKE_material_make_local(Material *ma)
292 Main *bmain = G.main;
298 bool is_local = false, is_lib = false;
300 /* - only lib users: do nothing
301 * - only local users: set flag
305 if (ma->id.lib == NULL) return;
307 /* One local user; set flag and return. */
308 if (ma->id.us == 1) {
309 id_clear_lib_data(bmain, &ma->id);
310 extern_local_material(ma);
314 /* Check which other IDs reference this one to determine if it's used by
317 ob = bmain->object.first;
320 for (a = 0; a < ob->totcol; a++) {
321 if (ob->mat[a] == ma) {
322 if (ob->id.lib) is_lib = true;
323 else is_local = true;
330 me = bmain->mesh.first;
333 for (a = 0; a < me->totcol; a++) {
334 if (me->mat[a] == ma) {
335 if (me->id.lib) is_lib = true;
336 else is_local = true;
343 cu = bmain->curve.first;
346 for (a = 0; a < cu->totcol; a++) {
347 if (cu->mat[a] == ma) {
348 if (cu->id.lib) is_lib = true;
349 else is_local = true;
356 mb = bmain->mball.first;
359 for (a = 0; a < mb->totcol; a++) {
360 if (mb->mat[a] == ma) {
361 if (mb->id.lib) is_lib = true;
362 else is_local = true;
369 /* Only local users. */
370 if (is_local && is_lib == false) {
371 id_clear_lib_data(bmain, &ma->id);
372 extern_local_material(ma);
374 /* Both user and local, so copy. */
375 else if (is_local && is_lib) {
376 Material *ma_new = BKE_material_copy(ma);
380 /* Remap paths of new ID using old library as base. */
381 BKE_id_lib_local_paths(bmain, ma->id.lib, &ma_new->id);
384 ob = bmain->object.first;
387 for (a = 0; a < ob->totcol; a++) {
388 if (ob->mat[a] == ma) {
389 if (ob->id.lib == NULL) {
400 me = bmain->mesh.first;
403 for (a = 0; a < me->totcol; a++) {
404 if (me->mat[a] == ma) {
405 if (me->id.lib == NULL) {
416 cu = bmain->curve.first;
419 for (a = 0; a < cu->totcol; a++) {
420 if (cu->mat[a] == ma) {
421 if (cu->id.lib == NULL) {
432 mb = bmain->mball.first;
435 for (a = 0; a < mb->totcol; a++) {
436 if (mb->mat[a] == ma) {
437 if (mb->id.lib == NULL) {
450 /* for curve, mball, mesh types */
451 void extern_local_matarar(struct Material **matar, short totcol)
454 for (i = 0; i < totcol; i++) {
455 id_lib_extern((ID *)matar[i]);
459 Material ***give_matarar(Object *ob)
465 if (ob->type == OB_MESH) {
469 else if (ELEM3(ob->type, OB_CURVE, OB_FONT, OB_SURF)) {
473 else if (ob->type == OB_MBALL) {
480 short *give_totcolp(Object *ob)
486 if (ob->type == OB_MESH) {
488 return &(me->totcol);
490 else if (ELEM3(ob->type, OB_CURVE, OB_FONT, OB_SURF)) {
492 return &(cu->totcol);
494 else if (ob->type == OB_MBALL) {
496 return &(mb->totcol);
501 /* same as above but for ID's */
502 Material ***give_matarar_id(ID *id)
504 /* ensure we don't try get materials from non-obdata */
505 BLI_assert(OB_DATA_SUPPORT_ID(GS(id->name)));
507 switch (GS(id->name)) {
509 return &(((Mesh *)id)->mat);
511 return &(((Curve *)id)->mat);
513 return &(((MetaBall *)id)->mat);
518 short *give_totcolp_id(ID *id)
520 /* ensure we don't try get materials from non-obdata */
521 BLI_assert(OB_DATA_SUPPORT_ID(GS(id->name)));
523 switch (GS(id->name)) {
525 return &(((Mesh *)id)->totcol);
527 return &(((Curve *)id)->totcol);
529 return &(((MetaBall *)id)->totcol);
534 static void material_data_index_remove_id(ID *id, short index)
536 /* ensure we don't try get materials from non-obdata */
537 BLI_assert(OB_DATA_SUPPORT_ID(GS(id->name)));
539 switch (GS(id->name)) {
541 BKE_mesh_material_index_remove((Mesh *)id, index);
544 BKE_curve_material_index_remove((Curve *)id, index);
547 /* meta-elems don't have materials atm */
552 static void material_data_index_clear_id(ID *id)
554 /* ensure we don't try get materials from non-obdata */
555 BLI_assert(OB_DATA_SUPPORT_ID(GS(id->name)));
557 switch (GS(id->name)) {
559 BKE_mesh_material_index_clear((Mesh *)id);
562 BKE_curve_material_index_clear((Curve *)id);
565 /* meta-elems don't have materials atm */
570 void BKE_material_resize_id(struct ID *id, short totcol, bool do_id_user)
572 Material ***matar = give_matarar_id(id);
573 short *totcolp = give_totcolp_id(id);
579 if (do_id_user && totcol < (*totcolp)) {
581 for (i = totcol; i < (*totcolp); i++) {
582 id_us_min((ID *)(*matar)[i]);
593 *matar = MEM_recallocN(*matar, sizeof(void *) * totcol);
598 void BKE_material_append_id(ID *id, Material *ma)
601 if ((matar = give_matarar_id(id))) {
602 short *totcol = give_totcolp_id(id);
603 Material **mat = MEM_callocN(sizeof(void *) * ((*totcol) + 1), "newmatar");
604 if (*totcol) memcpy(mat, *matar, sizeof(void *) * (*totcol));
605 if (*matar) MEM_freeN(*matar);
608 (*matar)[(*totcol)++] = ma;
610 id_us_plus((ID *)ma);
611 test_object_materials(G.main, id);
615 Material *BKE_material_pop_id(ID *id, int index_i, bool update_data)
617 short index = (short)index_i;
618 Material *ret = NULL;
620 if ((matar = give_matarar_id(id))) {
621 short *totcol = give_totcolp_id(id);
622 if (index >= 0 && index < (*totcol)) {
623 ret = (*matar)[index];
624 id_us_min((ID *)ret);
632 if (index + 1 != (*totcol))
633 memmove((*matar) + index, (*matar) + (index + 1), sizeof(void *) * ((*totcol) - (index + 1)));
636 *matar = MEM_reallocN(*matar, sizeof(void *) * (*totcol));
637 test_object_materials(G.main, id);
641 /* decrease mat_nr index */
642 material_data_index_remove_id(id, index);
650 void BKE_material_clear_id(struct ID *id, bool update_data)
653 if ((matar = give_matarar_id(id))) {
654 short *totcol = give_totcolp_id(id);
662 /* decrease mat_nr index */
663 material_data_index_clear_id(id);
668 Material *give_current_material(Object *ob, short act)
670 Material ***matarar, *ma;
673 if (ob == NULL) return NULL;
675 /* if object cannot have material, (totcolp == NULL) */
676 totcolp = give_totcolp(ob);
677 if (totcolp == NULL || ob->totcol == 0) return NULL;
680 printf("Negative material index!\n");
683 /* return NULL for invalid 'act', can happen for mesh face indices */
684 if (act > ob->totcol)
689 if (ob->matbits && ob->matbits[act - 1]) { /* in object */
690 ma = ob->mat[act - 1];
694 /* check for inconsistency */
695 if (*totcolp < ob->totcol)
696 ob->totcol = *totcolp;
697 if (act > ob->totcol) act = ob->totcol;
699 matarar = give_matarar(ob);
701 if (matarar && *matarar) ma = (*matarar)[act - 1];
709 ID *material_from(Object *ob, short act)
712 if (ob == NULL) return NULL;
714 if (ob->totcol == 0) return ob->data;
715 if (act == 0) act = 1;
717 if (ob->matbits[act - 1]) return (ID *)ob;
718 else return ob->data;
721 Material *give_node_material(Material *ma)
723 if (ma && ma->use_nodes && ma->nodetree) {
724 bNode *node = nodeGetActiveID(ma->nodetree, ID_MA);
727 return (Material *)node->id;
733 void BKE_material_resize_object(Object *ob, const short totcol, bool do_id_user)
738 if (do_id_user && totcol < ob->totcol) {
740 for (i = totcol; i < ob->totcol; i++) {
741 id_us_min((ID *)ob->mat[i]);
748 MEM_freeN(ob->matbits);
753 else if (ob->totcol < totcol) {
754 newmatar = MEM_callocN(sizeof(void *) * totcol, "newmatar");
755 newmatbits = MEM_callocN(sizeof(char) * totcol, "newmatbits");
757 memcpy(newmatar, ob->mat, sizeof(void *) * ob->totcol);
758 memcpy(newmatbits, ob->matbits, sizeof(char) * ob->totcol);
760 MEM_freeN(ob->matbits);
763 ob->matbits = newmatbits;
765 /* XXX, why not realloc on shrink? - campbell */
768 if (ob->totcol && ob->actcol == 0) ob->actcol = 1;
769 if (ob->actcol > ob->totcol) ob->actcol = ob->totcol;
772 void test_object_materials(Main *bmain, ID *id)
774 /* make the ob mat-array same size as 'ob->data' mat-array */
778 if (id == NULL || (totcol = give_totcolp_id(id)) == NULL) {
782 for (ob = bmain->object.first; ob; ob = ob->id.next) {
783 if (ob->data == id) {
784 BKE_material_resize_object(ob, *totcol, false);
789 void assign_material_id(ID *id, Material *ma, short act)
791 Material *mao, **matar, ***matarar;
794 if (act > MAXMAT) return;
795 if (act < 1) act = 1;
797 /* prevent crashing when using accidentally */
798 BLI_assert(id->lib == NULL);
803 totcolp = give_totcolp_id(id);
804 matarar = give_matarar_id(id);
806 if (totcolp == NULL || matarar == NULL) return;
808 if (act > *totcolp) {
809 matar = MEM_callocN(sizeof(void *) * act, "matarray1");
812 memcpy(matar, *matarar, sizeof(void *) * (*totcolp));
821 mao = (*matarar)[act - 1];
822 if (mao) mao->id.us--;
823 (*matarar)[act - 1] = ma;
826 id_us_plus((ID *)ma);
828 test_object_materials(G.main, id);
831 void assign_material(Object *ob, Material *ma, short act, int assign_type)
833 Material *mao, **matar, ***matarar;
837 if (act > MAXMAT) return;
838 if (act < 1) act = 1;
840 /* prevent crashing when using accidentally */
841 BLI_assert(ob->id.lib == NULL);
842 if (ob->id.lib) return;
846 totcolp = give_totcolp(ob);
847 matarar = give_matarar(ob);
849 if (totcolp == NULL || matarar == NULL) return;
851 if (act > *totcolp) {
852 matar = MEM_callocN(sizeof(void *) * act, "matarray1");
855 memcpy(matar, *matarar, sizeof(void *) * (*totcolp));
863 /* Determine the object/mesh linking */
864 if (assign_type == BKE_MAT_ASSIGN_USERPREF && ob->totcol && ob->actcol) {
865 /* copy from previous material */
866 bit = ob->matbits[ob->actcol - 1];
869 switch (assign_type) {
870 case BKE_MAT_ASSIGN_OBDATA:
873 case BKE_MAT_ASSIGN_OBJECT:
876 case BKE_MAT_ASSIGN_USERPREF:
878 bit = (U.flag & USER_MAT_ON_OB) ? 1 : 0;
883 if (act > ob->totcol) {
884 /* Need more space in the material arrays */
885 ob->mat = MEM_recallocN_id(ob->mat, sizeof(void *) * act, "matarray2");
886 ob->matbits = MEM_recallocN_id(ob->matbits, sizeof(char) * act, "matbits1");
892 ob->matbits[act - 1] = bit;
893 if (bit == 1) { /* in object */
894 mao = ob->mat[act - 1];
895 if (mao) mao->id.us--;
896 ob->mat[act - 1] = ma;
899 mao = (*matarar)[act - 1];
900 if (mao) mao->id.us--;
901 (*matarar)[act - 1] = ma;
905 id_us_plus((ID *)ma);
906 test_object_materials(G.main, ob->data);
909 /* XXX - this calls many more update calls per object then are needed, could be optimized */
910 void assign_matarar(struct Object *ob, struct Material ***matar, short totcol)
912 int actcol_orig = ob->actcol;
915 while (object_remove_material_slot(ob)) {}
917 /* now we have the right number of slots */
918 for (i = 0; i < totcol; i++)
919 assign_material(ob, (*matar)[i], i + 1, BKE_MAT_ASSIGN_USERPREF);
921 if (actcol_orig > ob->totcol)
922 actcol_orig = ob->totcol;
924 ob->actcol = actcol_orig;
928 short find_material_index(Object *ob, Material *ma)
933 if (ma == NULL) return 0;
935 totcolp = give_totcolp(ob);
936 matarar = give_matarar(ob);
938 if (totcolp == NULL || matarar == NULL) return 0;
940 for (a = 0; a < *totcolp; a++)
941 if ((*matarar)[a] == ma)
948 bool object_add_material_slot(Object *ob)
950 if (ob == NULL) return false;
951 if (ob->totcol >= MAXMAT) return false;
953 assign_material(ob, NULL, ob->totcol + 1, BKE_MAT_ASSIGN_USERPREF);
954 ob->actcol = ob->totcol;
958 static void do_init_render_material(Material *ma, int r_mode, float *amb)
961 int a, needuv = 0, needtang = 0;
963 if (ma->flarec == 0) ma->flarec = 1;
965 /* add all texcoflags from mtex, texco and mapto were cleared in advance */
966 for (a = 0; a < MAX_MTEX; a++) {
968 /* separate tex switching */
969 if (ma->septex & (1 << a)) continue;
972 if (mtex && mtex->tex && (mtex->tex->type | (mtex->tex->use_nodes && mtex->tex->nodetree) )) {
974 ma->texco |= mtex->texco;
975 ma->mapto |= mtex->mapto;
977 /* always get derivatives for these textures */
978 if (ELEM(mtex->tex->type, TEX_IMAGE, TEX_ENVMAP)) ma->texco |= TEXCO_OSA;
979 else if (mtex->texflag & (MTEX_COMPAT_BUMP | MTEX_3TAP_BUMP | MTEX_5TAP_BUMP | MTEX_BICUBIC_BUMP)) ma->texco |= TEXCO_OSA;
981 if (ma->texco & (TEXCO_ORCO | TEXCO_REFL | TEXCO_NORM | TEXCO_STRAND | TEXCO_STRESS)) needuv = 1;
982 else if (ma->texco & (TEXCO_GLOB | TEXCO_UV | TEXCO_OBJECT | TEXCO_SPEED)) needuv = 1;
983 else if (ma->texco & (TEXCO_LAVECTOR | TEXCO_VIEW)) needuv = 1;
985 if ((ma->mapto & MAP_NORM) && (mtex->normapspace == MTEX_NSPACE_TANGENT))
990 if (needtang) ma->mode |= MA_NORMAP_TANG;
991 else ma->mode &= ~MA_NORMAP_TANG;
993 if (ma->mode & (MA_VERTEXCOL | MA_VERTEXCOLP | MA_FACETEXTURE)) {
995 if (r_mode & R_OSA) ma->texco |= TEXCO_OSA; /* for texfaces */
997 if (needuv) ma->texco |= NEED_UV;
999 /* since the raytracer doesnt recalc O structs for each ray, we have to preset them all */
1000 if (r_mode & R_RAYTRACE) {
1001 if ((ma->mode & (MA_RAYMIRROR | MA_SHADOW_TRA)) || ((ma->mode & MA_TRANSP) && (ma->mode & MA_RAYTRANSP))) {
1002 ma->texco |= NEED_UV | TEXCO_ORCO | TEXCO_REFL | TEXCO_NORM;
1003 if (r_mode & R_OSA) ma->texco |= TEXCO_OSA;
1008 ma->ambr = ma->amb * amb[0];
1009 ma->ambg = ma->amb * amb[1];
1010 ma->ambb = ma->amb * amb[2];
1012 /* will become or-ed result of all node modes */
1013 ma->mode_l = ma->mode;
1014 ma->mode_l &= ~MA_SHLESS;
1016 if (ma->strand_surfnor > 0.0f)
1017 ma->mode_l |= MA_STR_SURFDIFF;
1019 /* parses the geom+tex nodes */
1020 if (ma->nodetree && ma->use_nodes)
1021 ntreeShaderGetTexcoMode(ma->nodetree, r_mode, &ma->texco, &ma->mode_l);
1023 /* local group override */
1024 if ((ma->shade_flag & MA_GROUP_LOCAL) && ma->id.lib && ma->group && ma->group->id.lib) {
1027 for (group = G.main->group.first; group; group = group->id.next) {
1028 if (!group->id.lib && strcmp(group->id.name, ma->group->id.name) == 0) {
1035 static void init_render_nodetree(bNodeTree *ntree, Material *basemat, int r_mode, float *amb)
1039 for (node = ntree->nodes.first; node; node = node->next) {
1041 if (GS(node->id->name) == ID_MA) {
1042 Material *ma = (Material *)node->id;
1043 if (ma != basemat) {
1044 do_init_render_material(ma, r_mode, amb);
1045 basemat->texco |= ma->texco;
1046 basemat->mode_l |= ma->mode_l & ~(MA_TRANSP | MA_ZTRANSP | MA_RAYTRANSP);
1049 else if (node->type == NODE_GROUP)
1050 init_render_nodetree((bNodeTree *)node->id, basemat, r_mode, amb);
1055 void init_render_material(Material *mat, int r_mode, float *amb)
1058 do_init_render_material(mat, r_mode, amb);
1060 if (mat->nodetree && mat->use_nodes) {
1061 init_render_nodetree(mat->nodetree, mat, r_mode, amb);
1063 if (!mat->nodetree->execdata)
1064 mat->nodetree->execdata = ntreeShaderBeginExecTree(mat->nodetree);
1068 void init_render_materials(Main *bmain, int r_mode, float *amb)
1072 /* clear these flags before going over materials, to make sure they
1073 * are cleared only once, otherwise node materials contained in other
1074 * node materials can go wrong */
1075 for (ma = bmain->mat.first; ma; ma = ma->id.next) {
1082 /* two steps, first initialize, then or the flags for layers */
1083 for (ma = bmain->mat.first; ma; ma = ma->id.next) {
1084 /* is_used flag comes back in convertblender.c */
1085 ma->flag &= ~MA_IS_USED;
1087 init_render_material(ma, r_mode, amb);
1090 do_init_render_material(&defmaterial, r_mode, amb);
1093 /* only needed for nodes now */
1094 void end_render_material(Material *mat)
1096 if (mat && mat->nodetree && mat->use_nodes) {
1097 if (mat->nodetree->execdata)
1098 ntreeShaderEndExecTree(mat->nodetree->execdata);
1102 void end_render_materials(Main *bmain)
1105 for (ma = bmain->mat.first; ma; ma = ma->id.next)
1107 end_render_material(ma);
1110 static bool material_in_nodetree(bNodeTree *ntree, Material *mat)
1114 for (node = ntree->nodes.first; node; node = node->next) {
1116 if (GS(node->id->name) == ID_MA) {
1117 if (node->id == (ID *)mat) {
1121 else if (node->type == NODE_GROUP) {
1122 if (material_in_nodetree((bNodeTree *)node->id, mat)) {
1132 bool material_in_material(Material *parmat, Material *mat)
1136 else if (parmat->nodetree && parmat->use_nodes)
1137 return material_in_nodetree(parmat->nodetree, mat);
1143 /* ****************** */
1145 /* Update drivers for materials in a nodetree */
1146 static void material_node_drivers_update(Scene *scene, bNodeTree *ntree, float ctime)
1150 /* nodetree itself */
1151 if (ntree->adt && ntree->adt->drivers.first) {
1152 BKE_animsys_evaluate_animdata(scene, &ntree->id, ntree->adt, ctime, ADT_RECALC_DRIVERS);
1156 for (node = ntree->nodes.first; node; node = node->next) {
1158 if (GS(node->id->name) == ID_MA) {
1159 material_drivers_update(scene, (Material *)node->id, ctime);
1161 else if (node->type == NODE_GROUP) {
1162 material_node_drivers_update(scene, (bNodeTree *)node->id, ctime);
1168 /* Calculate all drivers for materials
1169 * FIXME: this is really a terrible method which may result in some things being calculated
1170 * multiple times. However, without proper despgraph support for these things, we are forced
1171 * into this sort of thing...
1173 void material_drivers_update(Scene *scene, Material *ma, float ctime)
1175 //if (G.f & G_DEBUG)
1176 // printf("material_drivers_update(%s, %s)\n", scene->id.name, ma->id.name);
1178 /* Prevent infinite recursion by checking (and tagging the material) as having been visited already
1179 * (see BKE_scene_update_tagged()). This assumes ma->id.flag & LIB_DOIT isn't set by anything else
1180 * in the meantime... [#32017]
1182 if (ma->id.flag & LIB_DOIT)
1185 ma->id.flag |= LIB_DOIT;
1187 /* material itself */
1188 if (ma->adt && ma->adt->drivers.first) {
1189 BKE_animsys_evaluate_animdata(scene, &ma->id, ma->adt, ctime, ADT_RECALC_DRIVERS);
1194 material_node_drivers_update(scene, ma->nodetree, ctime);
1197 ma->id.flag &= ~LIB_DOIT;
1200 bool object_remove_material_slot(Object *ob)
1202 Material *mao, ***matarar;
1207 if (ob == NULL || ob->totcol == 0) {
1211 /* this should never happen and used to crash */
1212 if (ob->actcol <= 0) {
1213 printf("%s: invalid material index %d, report a bug!\n", __func__, ob->actcol);
1218 /* take a mesh/curve/mball as starting point, remove 1 index,
1219 * AND with all objects that share the ob->data
1221 * after that check indices in mesh/curve/mball!!!
1224 totcolp = give_totcolp(ob);
1225 matarar = give_matarar(ob);
1227 if (ELEM(NULL, matarar, *matarar)) {
1231 /* can happen on face selection in editmode */
1232 if (ob->actcol > ob->totcol) {
1233 ob->actcol = ob->totcol;
1236 /* we delete the actcol */
1237 mao = (*matarar)[ob->actcol - 1];
1238 if (mao) mao->id.us--;
1240 for (a = ob->actcol; a < ob->totcol; a++)
1241 (*matarar)[a - 1] = (*matarar)[a];
1244 if (*totcolp == 0) {
1245 MEM_freeN(*matarar);
1249 actcol = ob->actcol;
1250 obt = G.main->object.first;
1253 if (obt->data == ob->data) {
1255 /* WATCH IT: do not use actcol from ob or from obt (can become zero) */
1256 mao = obt->mat[actcol - 1];
1257 if (mao) mao->id.us--;
1259 for (a = actcol; a < obt->totcol; a++) {
1260 obt->mat[a - 1] = obt->mat[a];
1261 obt->matbits[a - 1] = obt->matbits[a];
1264 if (obt->actcol > obt->totcol) obt->actcol = obt->totcol;
1266 if (obt->totcol == 0) {
1267 MEM_freeN(obt->mat);
1268 MEM_freeN(obt->matbits);
1270 obt->matbits = NULL;
1276 /* check indices from mesh */
1277 if (ELEM4(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT)) {
1278 material_data_index_remove_id((ID *)ob->data, actcol - 1);
1279 if (ob->curve_cache) {
1280 BKE_displist_free(&ob->curve_cache->disp);
1288 /* r_col = current value, col = new value, (fac == 0) is no change */
1289 void ramp_blend(int type, float r_col[3], const float fac, const float col[3])
1291 float tmp, facm = 1.0f - fac;
1295 r_col[0] = facm * (r_col[0]) + fac * col[0];
1296 r_col[1] = facm * (r_col[1]) + fac * col[1];
1297 r_col[2] = facm * (r_col[2]) + fac * col[2];
1300 r_col[0] += fac * col[0];
1301 r_col[1] += fac * col[1];
1302 r_col[2] += fac * col[2];
1305 r_col[0] *= (facm + fac * col[0]);
1306 r_col[1] *= (facm + fac * col[1]);
1307 r_col[2] *= (facm + fac * col[2]);
1309 case MA_RAMP_SCREEN:
1310 r_col[0] = 1.0f - (facm + fac * (1.0f - col[0])) * (1.0f - r_col[0]);
1311 r_col[1] = 1.0f - (facm + fac * (1.0f - col[1])) * (1.0f - r_col[1]);
1312 r_col[2] = 1.0f - (facm + fac * (1.0f - col[2])) * (1.0f - r_col[2]);
1314 case MA_RAMP_OVERLAY:
1315 if (r_col[0] < 0.5f)
1316 r_col[0] *= (facm + 2.0f * fac * col[0]);
1318 r_col[0] = 1.0f - (facm + 2.0f * fac * (1.0f - col[0])) * (1.0f - r_col[0]);
1319 if (r_col[1] < 0.5f)
1320 r_col[1] *= (facm + 2.0f * fac * col[1]);
1322 r_col[1] = 1.0f - (facm + 2.0f * fac * (1.0f - col[1])) * (1.0f - r_col[1]);
1323 if (r_col[2] < 0.5f)
1324 r_col[2] *= (facm + 2.0f * fac * col[2]);
1326 r_col[2] = 1.0f - (facm + 2.0f * fac * (1.0f - col[2])) * (1.0f - r_col[2]);
1329 r_col[0] -= fac * col[0];
1330 r_col[1] -= fac * col[1];
1331 r_col[2] -= fac * col[2];
1335 r_col[0] = facm * (r_col[0]) + fac * (r_col[0]) / col[0];
1337 r_col[1] = facm * (r_col[1]) + fac * (r_col[1]) / col[1];
1339 r_col[2] = facm * (r_col[2]) + fac * (r_col[2]) / col[2];
1342 r_col[0] = facm * (r_col[0]) + fac * fabsf(r_col[0] - col[0]);
1343 r_col[1] = facm * (r_col[1]) + fac * fabsf(r_col[1] - col[1]);
1344 r_col[2] = facm * (r_col[2]) + fac * fabsf(r_col[2] - col[2]);
1347 tmp = col[0] + ((1 - col[0]) * facm);
1348 if (tmp < r_col[0]) r_col[0] = tmp;
1349 tmp = col[1] + ((1 - col[1]) * facm);
1350 if (tmp < r_col[1]) r_col[1] = tmp;
1351 tmp = col[2] + ((1 - col[2]) * facm);
1352 if (tmp < r_col[2]) r_col[2] = tmp;
1356 if (tmp > r_col[0]) r_col[0] = tmp;
1358 if (tmp > r_col[1]) r_col[1] = tmp;
1360 if (tmp > r_col[2]) r_col[2] = tmp;
1363 if (r_col[0] != 0.0f) {
1364 tmp = 1.0f - fac * col[0];
1367 else if ((tmp = (r_col[0]) / tmp) > 1.0f)
1372 if (r_col[1] != 0.0f) {
1373 tmp = 1.0f - fac * col[1];
1376 else if ((tmp = (r_col[1]) / tmp) > 1.0f)
1381 if (r_col[2] != 0.0f) {
1382 tmp = 1.0f - fac * col[2];
1385 else if ((tmp = (r_col[2]) / tmp) > 1.0f)
1392 tmp = facm + fac * col[0];
1396 else if ((tmp = (1.0f - (1.0f - (r_col[0])) / tmp)) < 0.0f)
1398 else if (tmp > 1.0f)
1403 tmp = facm + fac * col[1];
1406 else if ((tmp = (1.0f - (1.0f - (r_col[1])) / tmp)) < 0.0f)
1408 else if (tmp > 1.0f)
1413 tmp = facm + fac * col[2];
1416 else if ((tmp = (1.0f - (1.0f - (r_col[2])) / tmp)) < 0.0f)
1418 else if (tmp > 1.0f)
1426 float colH, colS, colV;
1427 float tmpr, tmpg, tmpb;
1428 rgb_to_hsv(col[0], col[1], col[2], &colH, &colS, &colV);
1430 rgb_to_hsv(r_col[0], r_col[1], r_col[2], &rH, &rS, &rV);
1431 hsv_to_rgb(colH, rS, rV, &tmpr, &tmpg, &tmpb);
1432 r_col[0] = facm * (r_col[0]) + fac * tmpr;
1433 r_col[1] = facm * (r_col[1]) + fac * tmpg;
1434 r_col[2] = facm * (r_col[2]) + fac * tmpb;
1441 float colH, colS, colV;
1442 rgb_to_hsv(r_col[0], r_col[1], r_col[2], &rH, &rS, &rV);
1444 rgb_to_hsv(col[0], col[1], col[2], &colH, &colS, &colV);
1445 hsv_to_rgb(rH, (facm * rS + fac * colS), rV, r_col + 0, r_col + 1, r_col + 2);
1452 float colH, colS, colV;
1453 rgb_to_hsv(r_col[0], r_col[1], r_col[2], &rH, &rS, &rV);
1454 rgb_to_hsv(col[0], col[1], col[2], &colH, &colS, &colV);
1455 hsv_to_rgb(rH, rS, (facm * rV + fac * colV), r_col + 0, r_col + 1, r_col + 2);
1461 float colH, colS, colV;
1462 float tmpr, tmpg, tmpb;
1463 rgb_to_hsv(col[0], col[1], col[2], &colH, &colS, &colV);
1465 rgb_to_hsv(r_col[0], r_col[1], r_col[2], &rH, &rS, &rV);
1466 hsv_to_rgb(colH, colS, rV, &tmpr, &tmpg, &tmpb);
1467 r_col[0] = facm * (r_col[0]) + fac * tmpr;
1468 r_col[1] = facm * (r_col[1]) + fac * tmpg;
1469 r_col[2] = facm * (r_col[2]) + fac * tmpb;
1475 float scr, scg, scb;
1477 /* first calculate non-fac based Screen mix */
1478 scr = 1.0f - (1.0f - col[0]) * (1.0f - r_col[0]);
1479 scg = 1.0f - (1.0f - col[1]) * (1.0f - r_col[1]);
1480 scb = 1.0f - (1.0f - col[2]) * (1.0f - r_col[2]);
1482 r_col[0] = facm * (r_col[0]) + fac * (((1.0f - r_col[0]) * col[0] * (r_col[0])) + (r_col[0] * scr));
1483 r_col[1] = facm * (r_col[1]) + fac * (((1.0f - r_col[1]) * col[1] * (r_col[1])) + (r_col[1] * scg));
1484 r_col[2] = facm * (r_col[2]) + fac * (((1.0f - r_col[2]) * col[2] * (r_col[2])) + (r_col[2] * scb));
1487 case MA_RAMP_LINEAR:
1489 r_col[0] = r_col[0] + fac * (2.0f * (col[0] - 0.5f));
1491 r_col[0] = r_col[0] + fac * (2.0f * (col[0]) - 1.0f);
1493 r_col[1] = r_col[1] + fac * (2.0f * (col[1] - 0.5f));
1495 r_col[1] = r_col[1] + fac * (2.0f * (col[1]) - 1.0f);
1497 r_col[2] = r_col[2] + fac * (2.0f * (col[2] - 0.5f));
1499 r_col[2] = r_col[2] + fac * (2.0f * (col[2]) - 1.0f);
1505 * \brief copy/paste buffer, if we had a proper py api that would be better
1506 * \note matcopybuf.nodetree does _NOT_ use ID's
1507 * \todo matcopybuf.nodetree's node->id's are NOT validated, this will crash!
1509 static Material matcopybuf;
1510 static short matcopied = 0;
1512 void clear_matcopybuf(void)
1514 memset(&matcopybuf, 0, sizeof(Material));
1518 void free_matcopybuf(void)
1522 for (a = 0; a < MAX_MTEX; a++) {
1523 if (matcopybuf.mtex[a]) {
1524 MEM_freeN(matcopybuf.mtex[a]);
1525 matcopybuf.mtex[a] = NULL;
1529 if (matcopybuf.ramp_col) MEM_freeN(matcopybuf.ramp_col);
1530 if (matcopybuf.ramp_spec) MEM_freeN(matcopybuf.ramp_spec);
1532 matcopybuf.ramp_col = NULL;
1533 matcopybuf.ramp_spec = NULL;
1535 if (matcopybuf.nodetree) {
1536 ntreeFreeTree_ex(matcopybuf.nodetree, false);
1537 MEM_freeN(matcopybuf.nodetree);
1538 matcopybuf.nodetree = NULL;
1544 void copy_matcopybuf(Material *ma)
1552 memcpy(&matcopybuf, ma, sizeof(Material));
1553 if (matcopybuf.ramp_col) matcopybuf.ramp_col = MEM_dupallocN(matcopybuf.ramp_col);
1554 if (matcopybuf.ramp_spec) matcopybuf.ramp_spec = MEM_dupallocN(matcopybuf.ramp_spec);
1556 for (a = 0; a < MAX_MTEX; a++) {
1557 mtex = matcopybuf.mtex[a];
1559 matcopybuf.mtex[a] = MEM_dupallocN(mtex);
1562 matcopybuf.nodetree = ntreeCopyTree_ex(ma->nodetree, false);
1563 matcopybuf.preview = NULL;
1564 BLI_listbase_clear(&matcopybuf.gpumaterial);
1568 void paste_matcopybuf(Material *ma)
1576 /* free current mat */
1577 if (ma->ramp_col) MEM_freeN(ma->ramp_col);
1578 if (ma->ramp_spec) MEM_freeN(ma->ramp_spec);
1579 for (a = 0; a < MAX_MTEX; a++) {
1581 if (mtex && mtex->tex) mtex->tex->id.us--;
1582 if (mtex) MEM_freeN(mtex);
1586 ntreeFreeTree(ma->nodetree);
1587 MEM_freeN(ma->nodetree);
1590 GPU_material_free(ma);
1593 memcpy(ma, &matcopybuf, sizeof(Material));
1596 if (matcopybuf.ramp_col) ma->ramp_col = MEM_dupallocN(matcopybuf.ramp_col);
1597 if (matcopybuf.ramp_spec) ma->ramp_spec = MEM_dupallocN(matcopybuf.ramp_spec);
1599 for (a = 0; a < MAX_MTEX; a++) {
1602 ma->mtex[a] = MEM_dupallocN(mtex);
1604 /* first check this is in main (we may have loaded another file) [#35500] */
1605 if (BLI_findindex(&G.main->tex, mtex->tex) != -1) {
1606 id_us_plus((ID *)mtex->tex);
1609 ma->mtex[a]->tex = NULL;
1615 ma->nodetree = ntreeCopyTree_ex(matcopybuf.nodetree, false);
1619 /*********************** texface to material convert functions **********************/
1620 /* encode all the TF information into a single int */
1621 static int encode_tfaceflag(MTFace *tf, int convertall)
1623 /* calculate the flag */
1624 int flag = tf->mode;
1626 /* options that change the material offline render */
1631 /* clean flags that are not being converted */
1633 flag &= ~TF_SHAREDVERT;
1634 flag &= ~TF_SHAREDCOL;
1635 flag &= ~TF_CONVERTED;
1637 /* light tface flag is ignored in GLSL mode */
1640 /* 15 is how big the flag can be - hardcoded here and in decode_tfaceflag() */
1641 flag |= tf->transp << 15;
1643 /* increase 1 so flag 0 is different than no flag yet */
1647 /* set the material options based in the tface flag */
1648 static void decode_tfaceflag(Material *ma, int flag, int convertall)
1651 GameSettings *game = &ma->game;
1653 /* flag is shifted in 1 to make 0 != no flag yet (see encode_tfaceflag) */
1656 alphablend = flag >> 15; /* encoded in the encode_tfaceflag function */
1659 /* General Material Options */
1660 if ((flag & TF_DYNAMIC) == 0) (*game).flag |= GEMAT_NOPHYSICS;
1662 /* Material Offline Rendering Properties */
1664 if (flag & TF_OBCOL) ma->shade_flag |= MA_OBCOLOR;
1667 /* Special Face Properties */
1668 if ((flag & TF_TWOSIDE) == 0) (*game).flag |= GEMAT_BACKCULL;
1669 if (flag & TF_INVISIBLE) (*game).flag |= GEMAT_INVISIBLE;
1670 if (flag & TF_BMFONT) (*game).flag |= GEMAT_TEXT;
1672 /* Face Orientation */
1673 if (flag & TF_BILLBOARD) (*game).face_orientation |= GEMAT_HALO;
1674 else if (flag & TF_BILLBOARD2) (*game).face_orientation |= GEMAT_BILLBOARD;
1675 else if (flag & TF_SHADOW) (*game).face_orientation |= GEMAT_SHADOW;
1678 if (flag & TF_ALPHASORT && ELEM(alphablend, TF_ALPHA, TF_ADD)) (*game).alpha_blend = GEMAT_ALPHA_SORT;
1679 else if (alphablend & TF_ALPHA) (*game).alpha_blend = GEMAT_ALPHA;
1680 else if (alphablend & TF_ADD) (*game).alpha_blend = GEMAT_ADD;
1681 else if (alphablend & TF_CLIP) (*game).alpha_blend = GEMAT_CLIP;
1684 /* boolean check to see if the mesh needs a material */
1685 static int check_tfaceneedmaterial(int flag)
1687 /* check if the flags we have are not deprecated != than default material options
1688 * also if only flags are visible and collision see if all objects using this mesh have this option in physics */
1690 /* flag is shifted in 1 to make 0 != no flag yet (see encode_tfaceflag) */
1693 /* deprecated flags */
1695 flag &= ~TF_SHAREDVERT;
1696 flag &= ~TF_SHAREDCOL;
1698 /* light tface flag is ignored in GLSL mode */
1701 /* automatic detected if tex image has alpha */
1702 flag &= ~(TF_ALPHA << 15);
1703 /* automatic detected if using texture */
1706 /* settings for the default NoMaterial */
1707 if (flag == TF_DYNAMIC)
1714 /* return number of digits of an integer */
1715 /* XXX to be optmized or replaced by an equivalent blender internal function */
1716 static int integer_getdigits(int number)
1719 if (number == 0) return 1;
1721 while (number != 0) {
1722 number = (int)(number / 10);
1728 static void calculate_tface_materialname(char *matname, char *newname, int flag)
1730 /* if flag has only light and collision and material matches those values
1731 * you can do strcpy(name, mat_name);
1733 int digits = integer_getdigits(flag);
1734 /* clamp the old name, remove the MA prefix and add the .TF.flag suffix
1735 * e.g. matname = "MALoooooooooooooongName"; newname = "Loooooooooooooon.TF.2" */
1736 BLI_snprintf(newname, MAX_ID_NAME, "%.*s.TF.%0*d", MAX_ID_NAME - (digits + 5), matname, digits, flag);
1739 /* returns -1 if no match */
1740 static short mesh_getmaterialnumber(Mesh *me, Material *ma)
1744 for (a = 0; a < me->totcol; a++) {
1745 if (me->mat[a] == ma) {
1753 /* append material */
1754 static short mesh_addmaterial(Mesh *me, Material *ma)
1756 BKE_material_append_id(&me->id, NULL);
1757 me->mat[me->totcol - 1] = ma;
1759 id_us_plus(&ma->id);
1761 return me->totcol - 1;
1764 static void set_facetexture_flags(Material *ma, Image *image)
1767 ma->mode |= MA_FACETEXTURE;
1768 /* we could check if the texture has alpha, but then more meshes sharing the same
1769 * material may need it. Let's make it simple. */
1770 if (BKE_image_has_alpha(image))
1771 ma->mode |= MA_FACETEXTURE_ALPHA;
1775 /* returns material number */
1776 static short convert_tfacenomaterial(Main *main, Mesh *me, MTFace *tf, int flag)
1779 char idname[MAX_ID_NAME];
1782 /* new material, the name uses the flag*/
1783 BLI_snprintf(idname, sizeof(idname), "MAMaterial.TF.%0*d", integer_getdigits(flag), flag);
1785 if ((ma = BLI_findstring(&main->mat, idname + 2, offsetof(ID, name) + 2))) {
1786 mat_nr = mesh_getmaterialnumber(me, ma);
1787 /* assign the material to the mesh */
1788 if (mat_nr == -1) mat_nr = mesh_addmaterial(me, ma);
1790 /* if needed set "Face Textures [Alpha]" Material options */
1791 set_facetexture_flags(ma, tf->tpage);
1793 /* create a new material */
1795 ma = BKE_material_add(main, idname + 2);
1798 printf("TexFace Convert: Material \"%s\" created.\n", idname + 2);
1799 mat_nr = mesh_addmaterial(me, ma);
1801 /* if needed set "Face Textures [Alpha]" Material options */
1802 set_facetexture_flags(ma, tf->tpage);
1804 decode_tfaceflag(ma, flag, 1);
1805 /* the final decoding will happen after, outside the main loop
1806 * for now store the flag into the material and change light/tex/collision
1807 * store the flag as a negative number */
1808 ma->game.flag = -flag;
1809 id_us_min((ID *)ma);
1812 printf("Error: Unable to create Material \"%s\" for Mesh \"%s\".", idname + 2, me->id.name + 2);
1816 /* set as converted, no need to go bad to this face */
1817 tf->mode |= TF_CONVERTED;
1821 /* Function to fully convert materials */
1822 static void convert_tfacematerial(Main *main, Material *ma)
1831 CustomDataLayer *cdl;
1832 char idname[MAX_ID_NAME];
1834 for (me = main->mesh.first; me; me = me->id.next) {
1835 /* check if this mesh uses this material */
1836 for (a = 0; a < me->totcol; a++)
1837 if (me->mat[a] == ma) break;
1839 /* no material found */
1840 if (a == me->totcol) continue;
1842 /* get the active tface layer */
1843 index = CustomData_get_active_layer_index(&me->fdata, CD_MTFACE);
1844 cdl = (index == -1) ? NULL : &me->fdata.layers[index];
1847 /* loop over all the faces and stop at the ones that use the material*/
1848 for (a = 0, mf = me->mface; a < me->totface; a++, mf++) {
1849 if (me->mat[mf->mat_nr] != ma) continue;
1851 /* texface data for this face */
1852 tf = ((MTFace *)cdl->data) + a;
1853 flag = encode_tfaceflag(tf, 1);
1855 /* the name of the new material */
1856 calculate_tface_materialname(ma->id.name, (char *)&idname, flag);
1858 if ((mat_new = BLI_findstring(&main->mat, idname + 2, offsetof(ID, name) + 2))) {
1859 /* material already existent, see if the mesh has it */
1860 mat_nr = mesh_getmaterialnumber(me, mat_new);
1861 /* material is not in the mesh, add it */
1862 if (mat_nr == -1) mat_nr = mesh_addmaterial(me, mat_new);
1864 /* create a new material */
1866 mat_new = BKE_material_copy(ma);
1868 /* rename the material*/
1869 BLI_strncpy(mat_new->id.name, idname, sizeof(mat_new->id.name));
1870 id_us_min((ID *)mat_new);
1872 mat_nr = mesh_addmaterial(me, mat_new);
1873 decode_tfaceflag(mat_new, flag, 1);
1876 printf("Error: Unable to create Material \"%s\" for Mesh \"%s.", idname + 2, me->id.name + 2);
1877 mat_nr = mf->mat_nr;
1882 /* if the material has a texture but no texture channel
1883 * set "Face Textures [Alpha]" Material options
1884 * actually we need to run it always, because of old behavior
1885 * of using face texture if any texture channel was present (multitex) */
1886 //if ((!mat_new->mtex[0]) && (!mat_new->mtex[0]->tex))
1887 set_facetexture_flags(mat_new, tf->tpage);
1889 /* set the material number to the face*/
1890 mf->mat_nr = mat_nr;
1892 /* remove material from mesh */
1893 for (a = 0; a < me->totcol; ) {
1894 if (me->mat[a] == ma) {
1895 BKE_material_pop_id(&me->id, a, true);
1905 #define MAT_BGE_DISPUTED -99999
1907 int do_version_tface(Main *main)
1913 CustomDataLayer *cdl;
1918 /* Operator in help menu has been removed for 2.7x */
1921 /* sometimes mesh has no materials but will need a new one. In those
1922 * cases we need to ignore the mf->mat_nr and only look at the face
1923 * mode because it can be zero as uninitialized or the 1st created material
1925 int nomaterialslots;
1927 /* alert to user to check the console */
1930 /* mark all the materials to conversion with a flag
1931 * if there is tface create a complete flag for that storing in flag
1932 * if there is tface and flag > 0: creates a new flag based on this face
1933 * if flags are different set flag to -1
1936 /* 1st part: marking mesh materials to update */
1937 for (me = main->mesh.first; me; me = me->id.next) {
1938 if (me->id.lib) continue;
1940 /* get the active tface layer */
1941 index = CustomData_get_active_layer_index(&me->fdata, CD_MTFACE);
1942 cdl = (index == -1) ? NULL : &me->fdata.layers[index];
1945 nomaterialslots = (me->totcol == 0 ? 1 : 0);
1947 /* loop over all the faces*/
1948 for (a = 0, mf = me->mface; a < me->totface; a++, mf++) {
1949 /* texface data for this face */
1950 tf = ((MTFace *)cdl->data) + a;
1952 /* conversion should happen only once */
1954 tf->mode &= ~TF_CONVERTED;
1956 if ((tf->mode & TF_CONVERTED)) continue;
1957 else tf->mode |= TF_CONVERTED;
1960 /* no material slots */
1961 if (nomaterialslots) {
1962 flag = encode_tfaceflag(tf, 1);
1964 /* create/find a new material and assign to the face */
1965 if (check_tfaceneedmaterial(flag)) {
1966 mf->mat_nr = convert_tfacenomaterial(main, me, tf, flag);
1968 /* else mark them as no-material to be reverted to 0 later */
1973 else if (mf->mat_nr < me->totcol) {
1974 ma = me->mat[mf->mat_nr];
1976 /* no material create one if necessary */
1978 /* find a new material and assign to the face */
1979 flag = encode_tfaceflag(tf, 1);
1981 /* create/find a new material and assign to the face */
1982 if (check_tfaceneedmaterial(flag))
1983 mf->mat_nr = convert_tfacenomaterial(main, me, tf, flag);
1988 /* we can't read from this if it comes from a library,
1989 * at doversion time: direct_link might not have happened on it,
1990 * so ma->mtex is not pointing to valid memory yet.
1991 * later we could, but it's better not */
1992 else if (ma->id.lib)
1995 /* material already marked as disputed */
1996 else if (ma->game.flag == MAT_BGE_DISPUTED)
1999 /* found a material */
2001 flag = encode_tfaceflag(tf, ((fileload) ? 0 : 1));
2003 /* first time changing this material */
2004 if (ma->game.flag == 0)
2005 ma->game.flag = -flag;
2007 /* mark material as disputed */
2008 else if (ma->game.flag != -flag) {
2009 ma->game.flag = MAT_BGE_DISPUTED;
2013 /* material ok so far */
2015 ma->game.flag = -flag;
2017 /* some people uses multitexture with TexFace by creating a texture
2018 * channel which not necessarily the tf->tpage image. But the game engine
2019 * was enabling it. Now it's required to set "Face Texture [Alpha] in the
2020 * material settings. */
2022 set_facetexture_flags(ma, tf->tpage);
2031 /* if we didn't have material slot and now we do, we need to
2032 * make sure the materials are correct */
2033 if (nomaterialslots) {
2034 if (me->totcol > 0) {
2035 for (a = 0, mf = me->mface; a < me->totface; a++, mf++) {
2036 if (mf->mat_nr == -1) {
2037 /* texface data for this face */
2038 tf = ((MTFace *)cdl->data) + a;
2039 mf->mat_nr = convert_tfacenomaterial(main, me, tf, encode_tfaceflag(tf, 1));
2044 for (a = 0, mf = me->mface; a < me->totface; a++, mf++) {
2052 /* 2nd part - conversion */
2053 /* skip library files */
2055 /* we shouldn't loop through the materials created in the loop. make the loop stop at its original length) */
2056 for (ma = main->mat.first, a = 0; ma; ma = ma->id.next, a++) {
2057 if (ma->id.lib) continue;
2059 /* disputed material */
2060 if (ma->game.flag == MAT_BGE_DISPUTED) {
2063 printf("Warning: material \"%s\" skipped.\n", ma->id.name + 2);
2067 convert_tfacematerial(main, ma);
2072 /* no conflicts in this material - 90% of cases
2073 * convert from tface system to material */
2074 else if (ma->game.flag < 0) {
2075 decode_tfaceflag(ma, -(ma->game.flag), 1);
2077 /* material is good make sure all faces using
2078 * this material are set to converted */
2080 for (me = main->mesh.first; me; me = me->id.next) {
2081 /* check if this mesh uses this material */
2082 for (a = 0; a < me->totcol; a++)
2083 if (me->mat[a] == ma) break;
2085 /* no material found */
2086 if (a == me->totcol) continue;
2088 /* get the active tface layer */
2089 index = CustomData_get_active_layer_index(&me->fdata, CD_MTFACE);
2090 cdl = (index == -1) ? NULL : &me->fdata.layers[index];
2093 /* loop over all the faces and stop at the ones that use the material*/
2094 for (a = 0, mf = me->mface; a < me->totface; a++, mf++) {
2095 if (me->mat[mf->mat_nr] == ma) {
2096 /* texface data for this face */
2097 tf = ((MTFace *)cdl->data) + a;
2098 tf->mode |= TF_CONVERTED;
2104 /* material is not used by faces with texface
2105 * set the default flag - do it only once */
2108 ma->game.flag = GEMAT_BACKCULL;