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->mode2 = MA_CASTSHADOW;
207 ma->shade_flag = MA_APPROX_OCCLUSION;
211 Material *BKE_material_add(Main *bmain, const char *name)
215 ma = BKE_libblock_alloc(bmain, ID_MA, name);
222 /* XXX keep synced with next function */
223 Material *BKE_material_copy(Material *ma)
228 man = BKE_libblock_copy(&ma->id);
230 id_lib_extern((ID *)man->group);
232 for (a = 0; a < MAX_MTEX; a++) {
234 man->mtex[a] = MEM_mallocN(sizeof(MTex), "copymaterial");
235 memcpy(man->mtex[a], ma->mtex[a], sizeof(MTex));
236 id_us_plus((ID *)man->mtex[a]->tex);
240 if (ma->ramp_col) man->ramp_col = MEM_dupallocN(ma->ramp_col);
241 if (ma->ramp_spec) man->ramp_spec = MEM_dupallocN(ma->ramp_spec);
243 if (ma->preview) man->preview = BKE_previewimg_copy(ma->preview);
246 man->nodetree = ntreeCopyTree(ma->nodetree);
249 BLI_listbase_clear(&man->gpumaterial);
254 /* XXX (see above) material copy without adding to main dbase */
255 Material *localize_material(Material *ma)
260 man = BKE_libblock_copy_nolib(&ma->id);
262 /* no increment for texture ID users, in previewrender.c it prevents decrement */
263 for (a = 0; a < MAX_MTEX; a++) {
265 man->mtex[a] = MEM_mallocN(sizeof(MTex), "copymaterial");
266 memcpy(man->mtex[a], ma->mtex[a], sizeof(MTex));
270 if (ma->ramp_col) man->ramp_col = MEM_dupallocN(ma->ramp_col);
271 if (ma->ramp_spec) man->ramp_spec = MEM_dupallocN(ma->ramp_spec);
276 man->nodetree = ntreeLocalize(ma->nodetree);
278 BLI_listbase_clear(&man->gpumaterial);
283 static void extern_local_material(Material *ma)
286 for (i = 0; i < MAX_MTEX; i++) {
287 if (ma->mtex[i]) id_lib_extern((ID *)ma->mtex[i]->tex);
291 void BKE_material_make_local(Material *ma)
293 Main *bmain = G.main;
299 bool is_local = false, is_lib = false;
301 /* - only lib users: do nothing
302 * - only local users: set flag
306 if (ma->id.lib == NULL) return;
308 /* One local user; set flag and return. */
309 if (ma->id.us == 1) {
310 id_clear_lib_data(bmain, &ma->id);
311 extern_local_material(ma);
315 /* Check which other IDs reference this one to determine if it's used by
318 ob = bmain->object.first;
321 for (a = 0; a < ob->totcol; a++) {
322 if (ob->mat[a] == ma) {
323 if (ob->id.lib) is_lib = true;
324 else is_local = true;
331 me = bmain->mesh.first;
334 for (a = 0; a < me->totcol; a++) {
335 if (me->mat[a] == ma) {
336 if (me->id.lib) is_lib = true;
337 else is_local = true;
344 cu = bmain->curve.first;
347 for (a = 0; a < cu->totcol; a++) {
348 if (cu->mat[a] == ma) {
349 if (cu->id.lib) is_lib = true;
350 else is_local = true;
357 mb = bmain->mball.first;
360 for (a = 0; a < mb->totcol; a++) {
361 if (mb->mat[a] == ma) {
362 if (mb->id.lib) is_lib = true;
363 else is_local = true;
370 /* Only local users. */
371 if (is_local && is_lib == false) {
372 id_clear_lib_data(bmain, &ma->id);
373 extern_local_material(ma);
375 /* Both user and local, so copy. */
376 else if (is_local && is_lib) {
377 Material *ma_new = BKE_material_copy(ma);
381 /* Remap paths of new ID using old library as base. */
382 BKE_id_lib_local_paths(bmain, ma->id.lib, &ma_new->id);
385 ob = bmain->object.first;
388 for (a = 0; a < ob->totcol; a++) {
389 if (ob->mat[a] == ma) {
390 if (ob->id.lib == NULL) {
401 me = bmain->mesh.first;
404 for (a = 0; a < me->totcol; a++) {
405 if (me->mat[a] == ma) {
406 if (me->id.lib == NULL) {
417 cu = bmain->curve.first;
420 for (a = 0; a < cu->totcol; a++) {
421 if (cu->mat[a] == ma) {
422 if (cu->id.lib == NULL) {
433 mb = bmain->mball.first;
436 for (a = 0; a < mb->totcol; a++) {
437 if (mb->mat[a] == ma) {
438 if (mb->id.lib == NULL) {
451 /* for curve, mball, mesh types */
452 void extern_local_matarar(struct Material **matar, short totcol)
455 for (i = 0; i < totcol; i++) {
456 id_lib_extern((ID *)matar[i]);
460 Material ***give_matarar(Object *ob)
466 if (ob->type == OB_MESH) {
470 else if (ELEM3(ob->type, OB_CURVE, OB_FONT, OB_SURF)) {
474 else if (ob->type == OB_MBALL) {
481 short *give_totcolp(Object *ob)
487 if (ob->type == OB_MESH) {
489 return &(me->totcol);
491 else if (ELEM3(ob->type, OB_CURVE, OB_FONT, OB_SURF)) {
493 return &(cu->totcol);
495 else if (ob->type == OB_MBALL) {
497 return &(mb->totcol);
502 /* same as above but for ID's */
503 Material ***give_matarar_id(ID *id)
505 /* ensure we don't try get materials from non-obdata */
506 BLI_assert(OB_DATA_SUPPORT_ID(GS(id->name)));
508 switch (GS(id->name)) {
510 return &(((Mesh *)id)->mat);
512 return &(((Curve *)id)->mat);
514 return &(((MetaBall *)id)->mat);
519 short *give_totcolp_id(ID *id)
521 /* ensure we don't try get materials from non-obdata */
522 BLI_assert(OB_DATA_SUPPORT_ID(GS(id->name)));
524 switch (GS(id->name)) {
526 return &(((Mesh *)id)->totcol);
528 return &(((Curve *)id)->totcol);
530 return &(((MetaBall *)id)->totcol);
535 static void material_data_index_remove_id(ID *id, short index)
537 /* ensure we don't try get materials from non-obdata */
538 BLI_assert(OB_DATA_SUPPORT_ID(GS(id->name)));
540 switch (GS(id->name)) {
542 BKE_mesh_material_index_remove((Mesh *)id, index);
545 BKE_curve_material_index_remove((Curve *)id, index);
548 /* meta-elems don't have materials atm */
553 static void material_data_index_clear_id(ID *id)
555 /* ensure we don't try get materials from non-obdata */
556 BLI_assert(OB_DATA_SUPPORT_ID(GS(id->name)));
558 switch (GS(id->name)) {
560 BKE_mesh_material_index_clear((Mesh *)id);
563 BKE_curve_material_index_clear((Curve *)id);
566 /* meta-elems don't have materials atm */
571 void BKE_material_resize_id(struct ID *id, short totcol, bool do_id_user)
573 Material ***matar = give_matarar_id(id);
574 short *totcolp = give_totcolp_id(id);
580 if (do_id_user && totcol < (*totcolp)) {
582 for (i = totcol; i < (*totcolp); i++) {
583 id_us_min((ID *)(*matar)[i]);
594 *matar = MEM_recallocN(*matar, sizeof(void *) * totcol);
599 void BKE_material_append_id(ID *id, Material *ma)
602 if ((matar = give_matarar_id(id))) {
603 short *totcol = give_totcolp_id(id);
604 Material **mat = MEM_callocN(sizeof(void *) * ((*totcol) + 1), "newmatar");
605 if (*totcol) memcpy(mat, *matar, sizeof(void *) * (*totcol));
606 if (*matar) MEM_freeN(*matar);
609 (*matar)[(*totcol)++] = ma;
611 id_us_plus((ID *)ma);
612 test_object_materials(G.main, id);
616 Material *BKE_material_pop_id(ID *id, int index_i, bool update_data)
618 short index = (short)index_i;
619 Material *ret = NULL;
621 if ((matar = give_matarar_id(id))) {
622 short *totcol = give_totcolp_id(id);
623 if (index >= 0 && index < (*totcol)) {
624 ret = (*matar)[index];
625 id_us_min((ID *)ret);
633 if (index + 1 != (*totcol))
634 memmove((*matar) + index, (*matar) + (index + 1), sizeof(void *) * ((*totcol) - (index + 1)));
637 *matar = MEM_reallocN(*matar, sizeof(void *) * (*totcol));
638 test_object_materials(G.main, id);
642 /* decrease mat_nr index */
643 material_data_index_remove_id(id, index);
651 void BKE_material_clear_id(struct ID *id, bool update_data)
654 if ((matar = give_matarar_id(id))) {
655 short *totcol = give_totcolp_id(id);
663 /* decrease mat_nr index */
664 material_data_index_clear_id(id);
669 Material *give_current_material(Object *ob, short act)
671 Material ***matarar, *ma;
674 if (ob == NULL) return NULL;
676 /* if object cannot have material, (totcolp == NULL) */
677 totcolp = give_totcolp(ob);
678 if (totcolp == NULL || ob->totcol == 0) return NULL;
681 printf("Negative material index!\n");
684 /* return NULL for invalid 'act', can happen for mesh face indices */
685 if (act > ob->totcol)
690 if (ob->matbits && ob->matbits[act - 1]) { /* in object */
691 ma = ob->mat[act - 1];
695 /* check for inconsistency */
696 if (*totcolp < ob->totcol)
697 ob->totcol = *totcolp;
698 if (act > ob->totcol) act = ob->totcol;
700 matarar = give_matarar(ob);
702 if (matarar && *matarar) ma = (*matarar)[act - 1];
710 ID *material_from(Object *ob, short act)
713 if (ob == NULL) return NULL;
715 if (ob->totcol == 0) return ob->data;
716 if (act == 0) act = 1;
718 if (ob->matbits[act - 1]) return (ID *)ob;
719 else return ob->data;
722 Material *give_node_material(Material *ma)
724 if (ma && ma->use_nodes && ma->nodetree) {
725 bNode *node = nodeGetActiveID(ma->nodetree, ID_MA);
728 return (Material *)node->id;
734 void BKE_material_resize_object(Object *ob, const short totcol, bool do_id_user)
739 if (do_id_user && totcol < ob->totcol) {
741 for (i = totcol; i < ob->totcol; i++) {
742 id_us_min((ID *)ob->mat[i]);
749 MEM_freeN(ob->matbits);
754 else if (ob->totcol < totcol) {
755 newmatar = MEM_callocN(sizeof(void *) * totcol, "newmatar");
756 newmatbits = MEM_callocN(sizeof(char) * totcol, "newmatbits");
758 memcpy(newmatar, ob->mat, sizeof(void *) * ob->totcol);
759 memcpy(newmatbits, ob->matbits, sizeof(char) * ob->totcol);
761 MEM_freeN(ob->matbits);
764 ob->matbits = newmatbits;
766 /* XXX, why not realloc on shrink? - campbell */
769 if (ob->totcol && ob->actcol == 0) ob->actcol = 1;
770 if (ob->actcol > ob->totcol) ob->actcol = ob->totcol;
773 void test_object_materials(Main *bmain, ID *id)
775 /* make the ob mat-array same size as 'ob->data' mat-array */
779 if (id == NULL || (totcol = give_totcolp_id(id)) == NULL) {
783 for (ob = bmain->object.first; ob; ob = ob->id.next) {
784 if (ob->data == id) {
785 BKE_material_resize_object(ob, *totcol, false);
790 void assign_material_id(ID *id, Material *ma, short act)
792 Material *mao, **matar, ***matarar;
795 if (act > MAXMAT) return;
796 if (act < 1) act = 1;
798 /* prevent crashing when using accidentally */
799 BLI_assert(id->lib == NULL);
804 totcolp = give_totcolp_id(id);
805 matarar = give_matarar_id(id);
807 if (totcolp == NULL || matarar == NULL) return;
809 if (act > *totcolp) {
810 matar = MEM_callocN(sizeof(void *) * act, "matarray1");
813 memcpy(matar, *matarar, sizeof(void *) * (*totcolp));
822 mao = (*matarar)[act - 1];
823 if (mao) mao->id.us--;
824 (*matarar)[act - 1] = ma;
827 id_us_plus((ID *)ma);
829 test_object_materials(G.main, id);
832 void assign_material(Object *ob, Material *ma, short act, int assign_type)
834 Material *mao, **matar, ***matarar;
838 if (act > MAXMAT) return;
839 if (act < 1) act = 1;
841 /* prevent crashing when using accidentally */
842 BLI_assert(ob->id.lib == NULL);
843 if (ob->id.lib) return;
847 totcolp = give_totcolp(ob);
848 matarar = give_matarar(ob);
850 if (totcolp == NULL || matarar == NULL) return;
852 if (act > *totcolp) {
853 matar = MEM_callocN(sizeof(void *) * act, "matarray1");
856 memcpy(matar, *matarar, sizeof(void *) * (*totcolp));
864 /* Determine the object/mesh linking */
865 if (assign_type == BKE_MAT_ASSIGN_USERPREF && ob->totcol && ob->actcol) {
866 /* copy from previous material */
867 bit = ob->matbits[ob->actcol - 1];
870 switch (assign_type) {
871 case BKE_MAT_ASSIGN_OBDATA:
874 case BKE_MAT_ASSIGN_OBJECT:
877 case BKE_MAT_ASSIGN_USERPREF:
879 bit = (U.flag & USER_MAT_ON_OB) ? 1 : 0;
884 if (act > ob->totcol) {
885 /* Need more space in the material arrays */
886 ob->mat = MEM_recallocN_id(ob->mat, sizeof(void *) * act, "matarray2");
887 ob->matbits = MEM_recallocN_id(ob->matbits, sizeof(char) * act, "matbits1");
893 ob->matbits[act - 1] = bit;
894 if (bit == 1) { /* in object */
895 mao = ob->mat[act - 1];
896 if (mao) mao->id.us--;
897 ob->mat[act - 1] = ma;
900 mao = (*matarar)[act - 1];
901 if (mao) mao->id.us--;
902 (*matarar)[act - 1] = ma;
906 id_us_plus((ID *)ma);
907 test_object_materials(G.main, ob->data);
910 /* XXX - this calls many more update calls per object then are needed, could be optimized */
911 void assign_matarar(struct Object *ob, struct Material ***matar, short totcol)
913 int actcol_orig = ob->actcol;
916 while (object_remove_material_slot(ob)) {}
918 /* now we have the right number of slots */
919 for (i = 0; i < totcol; i++)
920 assign_material(ob, (*matar)[i], i + 1, BKE_MAT_ASSIGN_USERPREF);
922 if (actcol_orig > ob->totcol)
923 actcol_orig = ob->totcol;
925 ob->actcol = actcol_orig;
929 short find_material_index(Object *ob, Material *ma)
934 if (ma == NULL) return 0;
936 totcolp = give_totcolp(ob);
937 matarar = give_matarar(ob);
939 if (totcolp == NULL || matarar == NULL) return 0;
941 for (a = 0; a < *totcolp; a++)
942 if ((*matarar)[a] == ma)
949 bool object_add_material_slot(Object *ob)
951 if (ob == NULL) return false;
952 if (ob->totcol >= MAXMAT) return false;
954 assign_material(ob, NULL, ob->totcol + 1, BKE_MAT_ASSIGN_USERPREF);
955 ob->actcol = ob->totcol;
959 static void do_init_render_material(Material *ma, int r_mode, float *amb)
962 int a, needuv = 0, needtang = 0;
964 if (ma->flarec == 0) ma->flarec = 1;
966 /* add all texcoflags from mtex, texco and mapto were cleared in advance */
967 for (a = 0; a < MAX_MTEX; a++) {
969 /* separate tex switching */
970 if (ma->septex & (1 << a)) continue;
973 if (mtex && mtex->tex && (mtex->tex->type | (mtex->tex->use_nodes && mtex->tex->nodetree) )) {
975 ma->texco |= mtex->texco;
976 ma->mapto |= mtex->mapto;
978 /* always get derivatives for these textures */
979 if (ELEM(mtex->tex->type, TEX_IMAGE, TEX_ENVMAP)) ma->texco |= TEXCO_OSA;
980 else if (mtex->texflag & (MTEX_COMPAT_BUMP | MTEX_3TAP_BUMP | MTEX_5TAP_BUMP | MTEX_BICUBIC_BUMP)) ma->texco |= TEXCO_OSA;
982 if (ma->texco & (TEXCO_ORCO | TEXCO_REFL | TEXCO_NORM | TEXCO_STRAND | TEXCO_STRESS)) needuv = 1;
983 else if (ma->texco & (TEXCO_GLOB | TEXCO_UV | TEXCO_OBJECT | TEXCO_SPEED)) needuv = 1;
984 else if (ma->texco & (TEXCO_LAVECTOR | TEXCO_VIEW)) needuv = 1;
986 if ((ma->mapto & MAP_NORM) && (mtex->normapspace == MTEX_NSPACE_TANGENT))
991 if (needtang) ma->mode |= MA_NORMAP_TANG;
992 else ma->mode &= ~MA_NORMAP_TANG;
994 if (ma->mode & (MA_VERTEXCOL | MA_VERTEXCOLP | MA_FACETEXTURE)) {
996 if (r_mode & R_OSA) ma->texco |= TEXCO_OSA; /* for texfaces */
998 if (needuv) ma->texco |= NEED_UV;
1000 /* since the raytracer doesnt recalc O structs for each ray, we have to preset them all */
1001 if (r_mode & R_RAYTRACE) {
1002 if ((ma->mode & (MA_RAYMIRROR | MA_SHADOW_TRA)) || ((ma->mode & MA_TRANSP) && (ma->mode & MA_RAYTRANSP))) {
1003 ma->texco |= NEED_UV | TEXCO_ORCO | TEXCO_REFL | TEXCO_NORM;
1004 if (r_mode & R_OSA) ma->texco |= TEXCO_OSA;
1009 ma->ambr = ma->amb * amb[0];
1010 ma->ambg = ma->amb * amb[1];
1011 ma->ambb = ma->amb * amb[2];
1014 /* local group override */
1015 if ((ma->shade_flag & MA_GROUP_LOCAL) && ma->id.lib && ma->group && ma->group->id.lib) {
1018 for (group = G.main->group.first; group; group = group->id.next) {
1019 if (!group->id.lib && strcmp(group->id.name, ma->group->id.name) == 0) {
1026 static void init_render_nodetree(bNodeTree *ntree, Material *basemat, int r_mode, float *amb)
1030 for (node = ntree->nodes.first; node; node = node->next) {
1032 if (GS(node->id->name) == ID_MA) {
1033 Material *ma = (Material *)node->id;
1034 if (ma != basemat) {
1035 do_init_render_material(ma, r_mode, amb);
1036 basemat->texco |= ma->texco;
1039 basemat->mode_l |= ma->mode & ~(MA_MODE_PIPELINE | MA_SHLESS);
1040 basemat->mode2_l |= ma->mode2 & ~MA_MODE2_PIPELINE;
1041 /* basemat only considered shadeless if all node materials are too */
1042 if(!(ma->mode & MA_SHLESS))
1043 basemat->mode_l &= ~MA_SHLESS;
1045 if (ma->strand_surfnor > 0.0f)
1046 basemat->mode_l |= MA_STR_SURFDIFF;
1048 else if (node->type == NODE_GROUP)
1049 init_render_nodetree((bNodeTree *)node->id, basemat, r_mode, amb);
1054 void init_render_material(Material *mat, int r_mode, float *amb)
1057 do_init_render_material(mat, r_mode, amb);
1059 if (mat->nodetree && mat->use_nodes) {
1060 /* mode_l will take the pipeline options from the main material, and the or-ed
1061 * result of non-pipeline options from the nodes. shadeless is an exception,
1062 * mode_l will have it set when all node materials are shadeless. */
1063 mat->mode_l = (mat->mode & MA_MODE_PIPELINE) | MA_SHLESS;
1064 mat->mode2_l = mat->mode2 & MA_MODE2_PIPELINE;
1066 /* parses the geom+tex nodes */
1067 ntreeShaderGetTexcoMode(mat->nodetree, r_mode, &mat->texco, &mat->mode_l);
1069 init_render_nodetree(mat->nodetree, mat, r_mode, amb);
1071 if (!mat->nodetree->execdata)
1072 mat->nodetree->execdata = ntreeShaderBeginExecTree(mat->nodetree);
1075 mat->mode_l = mat->mode;
1076 mat->mode2_l = mat->mode2;
1078 if (mat->strand_surfnor > 0.0f)
1079 mat->mode_l |= MA_STR_SURFDIFF;
1083 void init_render_materials(Main *bmain, int r_mode, float *amb)
1087 /* clear these flags before going over materials, to make sure they
1088 * are cleared only once, otherwise node materials contained in other
1089 * node materials can go wrong */
1090 for (ma = bmain->mat.first; ma; ma = ma->id.next) {
1097 /* two steps, first initialize, then or the flags for layers */
1098 for (ma = bmain->mat.first; ma; ma = ma->id.next) {
1099 /* is_used flag comes back in convertblender.c */
1100 ma->flag &= ~MA_IS_USED;
1102 init_render_material(ma, r_mode, amb);
1105 init_render_material(&defmaterial, r_mode, amb);
1108 /* only needed for nodes now */
1109 void end_render_material(Material *mat)
1111 if (mat && mat->nodetree && mat->use_nodes) {
1112 if (mat->nodetree->execdata)
1113 ntreeShaderEndExecTree(mat->nodetree->execdata);
1117 void end_render_materials(Main *bmain)
1120 for (ma = bmain->mat.first; ma; ma = ma->id.next)
1122 end_render_material(ma);
1125 static bool material_in_nodetree(bNodeTree *ntree, Material *mat)
1129 for (node = ntree->nodes.first; node; node = node->next) {
1131 if (GS(node->id->name) == ID_MA) {
1132 if (node->id == (ID *)mat) {
1136 else if (node->type == NODE_GROUP) {
1137 if (material_in_nodetree((bNodeTree *)node->id, mat)) {
1147 bool material_in_material(Material *parmat, Material *mat)
1151 else if (parmat->nodetree && parmat->use_nodes)
1152 return material_in_nodetree(parmat->nodetree, mat);
1158 /* ****************** */
1160 /* Update drivers for materials in a nodetree */
1161 static void material_node_drivers_update(Scene *scene, bNodeTree *ntree, float ctime)
1165 /* nodetree itself */
1166 if (ntree->adt && ntree->adt->drivers.first) {
1167 BKE_animsys_evaluate_animdata(scene, &ntree->id, ntree->adt, ctime, ADT_RECALC_DRIVERS);
1171 for (node = ntree->nodes.first; node; node = node->next) {
1173 if (GS(node->id->name) == ID_MA) {
1174 material_drivers_update(scene, (Material *)node->id, ctime);
1176 else if (node->type == NODE_GROUP) {
1177 material_node_drivers_update(scene, (bNodeTree *)node->id, ctime);
1183 /* Calculate all drivers for materials
1184 * FIXME: this is really a terrible method which may result in some things being calculated
1185 * multiple times. However, without proper despgraph support for these things, we are forced
1186 * into this sort of thing...
1188 void material_drivers_update(Scene *scene, Material *ma, float ctime)
1190 //if (G.f & G_DEBUG)
1191 // printf("material_drivers_update(%s, %s)\n", scene->id.name, ma->id.name);
1193 /* Prevent infinite recursion by checking (and tagging the material) as having been visited already
1194 * (see BKE_scene_update_tagged()). This assumes ma->id.flag & LIB_DOIT isn't set by anything else
1195 * in the meantime... [#32017]
1197 if (ma->id.flag & LIB_DOIT)
1200 ma->id.flag |= LIB_DOIT;
1202 /* material itself */
1203 if (ma->adt && ma->adt->drivers.first) {
1204 BKE_animsys_evaluate_animdata(scene, &ma->id, ma->adt, ctime, ADT_RECALC_DRIVERS);
1209 material_node_drivers_update(scene, ma->nodetree, ctime);
1212 ma->id.flag &= ~LIB_DOIT;
1215 bool object_remove_material_slot(Object *ob)
1217 Material *mao, ***matarar;
1222 if (ob == NULL || ob->totcol == 0) {
1226 /* this should never happen and used to crash */
1227 if (ob->actcol <= 0) {
1228 printf("%s: invalid material index %d, report a bug!\n", __func__, ob->actcol);
1233 /* take a mesh/curve/mball as starting point, remove 1 index,
1234 * AND with all objects that share the ob->data
1236 * after that check indices in mesh/curve/mball!!!
1239 totcolp = give_totcolp(ob);
1240 matarar = give_matarar(ob);
1242 if (ELEM(NULL, matarar, *matarar)) {
1246 /* can happen on face selection in editmode */
1247 if (ob->actcol > ob->totcol) {
1248 ob->actcol = ob->totcol;
1251 /* we delete the actcol */
1252 mao = (*matarar)[ob->actcol - 1];
1253 if (mao) mao->id.us--;
1255 for (a = ob->actcol; a < ob->totcol; a++)
1256 (*matarar)[a - 1] = (*matarar)[a];
1259 if (*totcolp == 0) {
1260 MEM_freeN(*matarar);
1264 actcol = ob->actcol;
1265 obt = G.main->object.first;
1268 if (obt->data == ob->data) {
1270 /* WATCH IT: do not use actcol from ob or from obt (can become zero) */
1271 mao = obt->mat[actcol - 1];
1272 if (mao) mao->id.us--;
1274 for (a = actcol; a < obt->totcol; a++) {
1275 obt->mat[a - 1] = obt->mat[a];
1276 obt->matbits[a - 1] = obt->matbits[a];
1279 if (obt->actcol > obt->totcol) obt->actcol = obt->totcol;
1281 if (obt->totcol == 0) {
1282 MEM_freeN(obt->mat);
1283 MEM_freeN(obt->matbits);
1285 obt->matbits = NULL;
1291 /* check indices from mesh */
1292 if (ELEM4(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT)) {
1293 material_data_index_remove_id((ID *)ob->data, actcol - 1);
1294 if (ob->curve_cache) {
1295 BKE_displist_free(&ob->curve_cache->disp);
1303 /* r_col = current value, col = new value, (fac == 0) is no change */
1304 void ramp_blend(int type, float r_col[3], const float fac, const float col[3])
1306 float tmp, facm = 1.0f - fac;
1310 r_col[0] = facm * (r_col[0]) + fac * col[0];
1311 r_col[1] = facm * (r_col[1]) + fac * col[1];
1312 r_col[2] = facm * (r_col[2]) + fac * col[2];
1315 r_col[0] += fac * col[0];
1316 r_col[1] += fac * col[1];
1317 r_col[2] += fac * col[2];
1320 r_col[0] *= (facm + fac * col[0]);
1321 r_col[1] *= (facm + fac * col[1]);
1322 r_col[2] *= (facm + fac * col[2]);
1324 case MA_RAMP_SCREEN:
1325 r_col[0] = 1.0f - (facm + fac * (1.0f - col[0])) * (1.0f - r_col[0]);
1326 r_col[1] = 1.0f - (facm + fac * (1.0f - col[1])) * (1.0f - r_col[1]);
1327 r_col[2] = 1.0f - (facm + fac * (1.0f - col[2])) * (1.0f - r_col[2]);
1329 case MA_RAMP_OVERLAY:
1330 if (r_col[0] < 0.5f)
1331 r_col[0] *= (facm + 2.0f * fac * col[0]);
1333 r_col[0] = 1.0f - (facm + 2.0f * fac * (1.0f - col[0])) * (1.0f - r_col[0]);
1334 if (r_col[1] < 0.5f)
1335 r_col[1] *= (facm + 2.0f * fac * col[1]);
1337 r_col[1] = 1.0f - (facm + 2.0f * fac * (1.0f - col[1])) * (1.0f - r_col[1]);
1338 if (r_col[2] < 0.5f)
1339 r_col[2] *= (facm + 2.0f * fac * col[2]);
1341 r_col[2] = 1.0f - (facm + 2.0f * fac * (1.0f - col[2])) * (1.0f - r_col[2]);
1344 r_col[0] -= fac * col[0];
1345 r_col[1] -= fac * col[1];
1346 r_col[2] -= fac * col[2];
1350 r_col[0] = facm * (r_col[0]) + fac * (r_col[0]) / col[0];
1352 r_col[1] = facm * (r_col[1]) + fac * (r_col[1]) / col[1];
1354 r_col[2] = facm * (r_col[2]) + fac * (r_col[2]) / col[2];
1357 r_col[0] = facm * (r_col[0]) + fac * fabsf(r_col[0] - col[0]);
1358 r_col[1] = facm * (r_col[1]) + fac * fabsf(r_col[1] - col[1]);
1359 r_col[2] = facm * (r_col[2]) + fac * fabsf(r_col[2] - col[2]);
1362 tmp = col[0] + ((1 - col[0]) * facm);
1363 if (tmp < r_col[0]) r_col[0] = tmp;
1364 tmp = col[1] + ((1 - col[1]) * facm);
1365 if (tmp < r_col[1]) r_col[1] = tmp;
1366 tmp = col[2] + ((1 - col[2]) * facm);
1367 if (tmp < r_col[2]) r_col[2] = tmp;
1371 if (tmp > r_col[0]) r_col[0] = tmp;
1373 if (tmp > r_col[1]) r_col[1] = tmp;
1375 if (tmp > r_col[2]) r_col[2] = tmp;
1378 if (r_col[0] != 0.0f) {
1379 tmp = 1.0f - fac * col[0];
1382 else if ((tmp = (r_col[0]) / tmp) > 1.0f)
1387 if (r_col[1] != 0.0f) {
1388 tmp = 1.0f - fac * col[1];
1391 else if ((tmp = (r_col[1]) / tmp) > 1.0f)
1396 if (r_col[2] != 0.0f) {
1397 tmp = 1.0f - fac * col[2];
1400 else if ((tmp = (r_col[2]) / tmp) > 1.0f)
1407 tmp = facm + fac * col[0];
1411 else if ((tmp = (1.0f - (1.0f - (r_col[0])) / tmp)) < 0.0f)
1413 else if (tmp > 1.0f)
1418 tmp = facm + fac * col[1];
1421 else if ((tmp = (1.0f - (1.0f - (r_col[1])) / tmp)) < 0.0f)
1423 else if (tmp > 1.0f)
1428 tmp = facm + fac * col[2];
1431 else if ((tmp = (1.0f - (1.0f - (r_col[2])) / tmp)) < 0.0f)
1433 else if (tmp > 1.0f)
1441 float colH, colS, colV;
1442 float tmpr, tmpg, tmpb;
1443 rgb_to_hsv(col[0], col[1], col[2], &colH, &colS, &colV);
1445 rgb_to_hsv(r_col[0], r_col[1], r_col[2], &rH, &rS, &rV);
1446 hsv_to_rgb(colH, rS, rV, &tmpr, &tmpg, &tmpb);
1447 r_col[0] = facm * (r_col[0]) + fac * tmpr;
1448 r_col[1] = facm * (r_col[1]) + fac * tmpg;
1449 r_col[2] = facm * (r_col[2]) + fac * tmpb;
1456 float colH, colS, colV;
1457 rgb_to_hsv(r_col[0], r_col[1], r_col[2], &rH, &rS, &rV);
1459 rgb_to_hsv(col[0], col[1], col[2], &colH, &colS, &colV);
1460 hsv_to_rgb(rH, (facm * rS + fac * colS), rV, r_col + 0, r_col + 1, r_col + 2);
1467 float colH, colS, colV;
1468 rgb_to_hsv(r_col[0], r_col[1], r_col[2], &rH, &rS, &rV);
1469 rgb_to_hsv(col[0], col[1], col[2], &colH, &colS, &colV);
1470 hsv_to_rgb(rH, rS, (facm * rV + fac * colV), r_col + 0, r_col + 1, r_col + 2);
1476 float colH, colS, colV;
1477 float tmpr, tmpg, tmpb;
1478 rgb_to_hsv(col[0], col[1], col[2], &colH, &colS, &colV);
1480 rgb_to_hsv(r_col[0], r_col[1], r_col[2], &rH, &rS, &rV);
1481 hsv_to_rgb(colH, colS, rV, &tmpr, &tmpg, &tmpb);
1482 r_col[0] = facm * (r_col[0]) + fac * tmpr;
1483 r_col[1] = facm * (r_col[1]) + fac * tmpg;
1484 r_col[2] = facm * (r_col[2]) + fac * tmpb;
1490 float scr, scg, scb;
1492 /* first calculate non-fac based Screen mix */
1493 scr = 1.0f - (1.0f - col[0]) * (1.0f - r_col[0]);
1494 scg = 1.0f - (1.0f - col[1]) * (1.0f - r_col[1]);
1495 scb = 1.0f - (1.0f - col[2]) * (1.0f - r_col[2]);
1497 r_col[0] = facm * (r_col[0]) + fac * (((1.0f - r_col[0]) * col[0] * (r_col[0])) + (r_col[0] * scr));
1498 r_col[1] = facm * (r_col[1]) + fac * (((1.0f - r_col[1]) * col[1] * (r_col[1])) + (r_col[1] * scg));
1499 r_col[2] = facm * (r_col[2]) + fac * (((1.0f - r_col[2]) * col[2] * (r_col[2])) + (r_col[2] * scb));
1502 case MA_RAMP_LINEAR:
1504 r_col[0] = r_col[0] + fac * (2.0f * (col[0] - 0.5f));
1506 r_col[0] = r_col[0] + fac * (2.0f * (col[0]) - 1.0f);
1508 r_col[1] = r_col[1] + fac * (2.0f * (col[1] - 0.5f));
1510 r_col[1] = r_col[1] + fac * (2.0f * (col[1]) - 1.0f);
1512 r_col[2] = r_col[2] + fac * (2.0f * (col[2] - 0.5f));
1514 r_col[2] = r_col[2] + fac * (2.0f * (col[2]) - 1.0f);
1520 * \brief copy/paste buffer, if we had a proper py api that would be better
1521 * \note matcopybuf.nodetree does _NOT_ use ID's
1522 * \todo matcopybuf.nodetree's node->id's are NOT validated, this will crash!
1524 static Material matcopybuf;
1525 static short matcopied = 0;
1527 void clear_matcopybuf(void)
1529 memset(&matcopybuf, 0, sizeof(Material));
1533 void free_matcopybuf(void)
1537 for (a = 0; a < MAX_MTEX; a++) {
1538 if (matcopybuf.mtex[a]) {
1539 MEM_freeN(matcopybuf.mtex[a]);
1540 matcopybuf.mtex[a] = NULL;
1544 if (matcopybuf.ramp_col) MEM_freeN(matcopybuf.ramp_col);
1545 if (matcopybuf.ramp_spec) MEM_freeN(matcopybuf.ramp_spec);
1547 matcopybuf.ramp_col = NULL;
1548 matcopybuf.ramp_spec = NULL;
1550 if (matcopybuf.nodetree) {
1551 ntreeFreeTree_ex(matcopybuf.nodetree, false);
1552 MEM_freeN(matcopybuf.nodetree);
1553 matcopybuf.nodetree = NULL;
1559 void copy_matcopybuf(Material *ma)
1567 memcpy(&matcopybuf, ma, sizeof(Material));
1568 if (matcopybuf.ramp_col) matcopybuf.ramp_col = MEM_dupallocN(matcopybuf.ramp_col);
1569 if (matcopybuf.ramp_spec) matcopybuf.ramp_spec = MEM_dupallocN(matcopybuf.ramp_spec);
1571 for (a = 0; a < MAX_MTEX; a++) {
1572 mtex = matcopybuf.mtex[a];
1574 matcopybuf.mtex[a] = MEM_dupallocN(mtex);
1577 matcopybuf.nodetree = ntreeCopyTree_ex(ma->nodetree, false);
1578 matcopybuf.preview = NULL;
1579 BLI_listbase_clear(&matcopybuf.gpumaterial);
1583 void paste_matcopybuf(Material *ma)
1591 /* free current mat */
1592 if (ma->ramp_col) MEM_freeN(ma->ramp_col);
1593 if (ma->ramp_spec) MEM_freeN(ma->ramp_spec);
1594 for (a = 0; a < MAX_MTEX; a++) {
1596 if (mtex && mtex->tex) mtex->tex->id.us--;
1597 if (mtex) MEM_freeN(mtex);
1601 ntreeFreeTree(ma->nodetree);
1602 MEM_freeN(ma->nodetree);
1605 GPU_material_free(ma);
1608 memcpy(ma, &matcopybuf, sizeof(Material));
1611 if (matcopybuf.ramp_col) ma->ramp_col = MEM_dupallocN(matcopybuf.ramp_col);
1612 if (matcopybuf.ramp_spec) ma->ramp_spec = MEM_dupallocN(matcopybuf.ramp_spec);
1614 for (a = 0; a < MAX_MTEX; a++) {
1617 ma->mtex[a] = MEM_dupallocN(mtex);
1619 /* first check this is in main (we may have loaded another file) [#35500] */
1620 if (BLI_findindex(&G.main->tex, mtex->tex) != -1) {
1621 id_us_plus((ID *)mtex->tex);
1624 ma->mtex[a]->tex = NULL;
1630 ma->nodetree = ntreeCopyTree_ex(matcopybuf.nodetree, false);
1634 /*********************** texface to material convert functions **********************/
1635 /* encode all the TF information into a single int */
1636 static int encode_tfaceflag(MTFace *tf, int convertall)
1638 /* calculate the flag */
1639 int flag = tf->mode;
1641 /* options that change the material offline render */
1646 /* clean flags that are not being converted */
1648 flag &= ~TF_SHAREDVERT;
1649 flag &= ~TF_SHAREDCOL;
1650 flag &= ~TF_CONVERTED;
1652 /* light tface flag is ignored in GLSL mode */
1655 /* 15 is how big the flag can be - hardcoded here and in decode_tfaceflag() */
1656 flag |= tf->transp << 15;
1658 /* increase 1 so flag 0 is different than no flag yet */
1662 /* set the material options based in the tface flag */
1663 static void decode_tfaceflag(Material *ma, int flag, int convertall)
1666 GameSettings *game = &ma->game;
1668 /* flag is shifted in 1 to make 0 != no flag yet (see encode_tfaceflag) */
1671 alphablend = flag >> 15; /* encoded in the encode_tfaceflag function */
1674 /* General Material Options */
1675 if ((flag & TF_DYNAMIC) == 0) (*game).flag |= GEMAT_NOPHYSICS;
1677 /* Material Offline Rendering Properties */
1679 if (flag & TF_OBCOL) ma->shade_flag |= MA_OBCOLOR;
1682 /* Special Face Properties */
1683 if ((flag & TF_TWOSIDE) == 0) (*game).flag |= GEMAT_BACKCULL;
1684 if (flag & TF_INVISIBLE) (*game).flag |= GEMAT_INVISIBLE;
1685 if (flag & TF_BMFONT) (*game).flag |= GEMAT_TEXT;
1687 /* Face Orientation */
1688 if (flag & TF_BILLBOARD) (*game).face_orientation |= GEMAT_HALO;
1689 else if (flag & TF_BILLBOARD2) (*game).face_orientation |= GEMAT_BILLBOARD;
1690 else if (flag & TF_SHADOW) (*game).face_orientation |= GEMAT_SHADOW;
1693 if (flag & TF_ALPHASORT && ELEM(alphablend, TF_ALPHA, TF_ADD)) (*game).alpha_blend = GEMAT_ALPHA_SORT;
1694 else if (alphablend & TF_ALPHA) (*game).alpha_blend = GEMAT_ALPHA;
1695 else if (alphablend & TF_ADD) (*game).alpha_blend = GEMAT_ADD;
1696 else if (alphablend & TF_CLIP) (*game).alpha_blend = GEMAT_CLIP;
1699 /* boolean check to see if the mesh needs a material */
1700 static int check_tfaceneedmaterial(int flag)
1702 /* check if the flags we have are not deprecated != than default material options
1703 * also if only flags are visible and collision see if all objects using this mesh have this option in physics */
1705 /* flag is shifted in 1 to make 0 != no flag yet (see encode_tfaceflag) */
1708 /* deprecated flags */
1710 flag &= ~TF_SHAREDVERT;
1711 flag &= ~TF_SHAREDCOL;
1713 /* light tface flag is ignored in GLSL mode */
1716 /* automatic detected if tex image has alpha */
1717 flag &= ~(TF_ALPHA << 15);
1718 /* automatic detected if using texture */
1721 /* settings for the default NoMaterial */
1722 if (flag == TF_DYNAMIC)
1729 /* return number of digits of an integer */
1730 /* XXX to be optmized or replaced by an equivalent blender internal function */
1731 static int integer_getdigits(int number)
1734 if (number == 0) return 1;
1736 while (number != 0) {
1737 number = (int)(number / 10);
1743 static void calculate_tface_materialname(char *matname, char *newname, int flag)
1745 /* if flag has only light and collision and material matches those values
1746 * you can do strcpy(name, mat_name);
1748 int digits = integer_getdigits(flag);
1749 /* clamp the old name, remove the MA prefix and add the .TF.flag suffix
1750 * e.g. matname = "MALoooooooooooooongName"; newname = "Loooooooooooooon.TF.2" */
1751 BLI_snprintf(newname, MAX_ID_NAME, "%.*s.TF.%0*d", MAX_ID_NAME - (digits + 5), matname, digits, flag);
1754 /* returns -1 if no match */
1755 static short mesh_getmaterialnumber(Mesh *me, Material *ma)
1759 for (a = 0; a < me->totcol; a++) {
1760 if (me->mat[a] == ma) {
1768 /* append material */
1769 static short mesh_addmaterial(Mesh *me, Material *ma)
1771 BKE_material_append_id(&me->id, NULL);
1772 me->mat[me->totcol - 1] = ma;
1774 id_us_plus(&ma->id);
1776 return me->totcol - 1;
1779 static void set_facetexture_flags(Material *ma, Image *image)
1782 ma->mode |= MA_FACETEXTURE;
1783 /* we could check if the texture has alpha, but then more meshes sharing the same
1784 * material may need it. Let's make it simple. */
1785 if (BKE_image_has_alpha(image))
1786 ma->mode |= MA_FACETEXTURE_ALPHA;
1790 /* returns material number */
1791 static short convert_tfacenomaterial(Main *main, Mesh *me, MTFace *tf, int flag)
1794 char idname[MAX_ID_NAME];
1797 /* new material, the name uses the flag*/
1798 BLI_snprintf(idname, sizeof(idname), "MAMaterial.TF.%0*d", integer_getdigits(flag), flag);
1800 if ((ma = BLI_findstring(&main->mat, idname + 2, offsetof(ID, name) + 2))) {
1801 mat_nr = mesh_getmaterialnumber(me, ma);
1802 /* assign the material to the mesh */
1803 if (mat_nr == -1) mat_nr = mesh_addmaterial(me, ma);
1805 /* if needed set "Face Textures [Alpha]" Material options */
1806 set_facetexture_flags(ma, tf->tpage);
1808 /* create a new material */
1810 ma = BKE_material_add(main, idname + 2);
1813 printf("TexFace Convert: Material \"%s\" created.\n", idname + 2);
1814 mat_nr = mesh_addmaterial(me, ma);
1816 /* if needed set "Face Textures [Alpha]" Material options */
1817 set_facetexture_flags(ma, tf->tpage);
1819 decode_tfaceflag(ma, flag, 1);
1820 /* the final decoding will happen after, outside the main loop
1821 * for now store the flag into the material and change light/tex/collision
1822 * store the flag as a negative number */
1823 ma->game.flag = -flag;
1824 id_us_min((ID *)ma);
1827 printf("Error: Unable to create Material \"%s\" for Mesh \"%s\".", idname + 2, me->id.name + 2);
1831 /* set as converted, no need to go bad to this face */
1832 tf->mode |= TF_CONVERTED;
1836 /* Function to fully convert materials */
1837 static void convert_tfacematerial(Main *main, Material *ma)
1846 CustomDataLayer *cdl;
1847 char idname[MAX_ID_NAME];
1849 for (me = main->mesh.first; me; me = me->id.next) {
1850 /* check if this mesh uses this material */
1851 for (a = 0; a < me->totcol; a++)
1852 if (me->mat[a] == ma) break;
1854 /* no material found */
1855 if (a == me->totcol) continue;
1857 /* get the active tface layer */
1858 index = CustomData_get_active_layer_index(&me->fdata, CD_MTFACE);
1859 cdl = (index == -1) ? NULL : &me->fdata.layers[index];
1862 /* loop over all the faces and stop at the ones that use the material*/
1863 for (a = 0, mf = me->mface; a < me->totface; a++, mf++) {
1864 if (me->mat[mf->mat_nr] != ma) continue;
1866 /* texface data for this face */
1867 tf = ((MTFace *)cdl->data) + a;
1868 flag = encode_tfaceflag(tf, 1);
1870 /* the name of the new material */
1871 calculate_tface_materialname(ma->id.name, (char *)&idname, flag);
1873 if ((mat_new = BLI_findstring(&main->mat, idname + 2, offsetof(ID, name) + 2))) {
1874 /* material already existent, see if the mesh has it */
1875 mat_nr = mesh_getmaterialnumber(me, mat_new);
1876 /* material is not in the mesh, add it */
1877 if (mat_nr == -1) mat_nr = mesh_addmaterial(me, mat_new);
1879 /* create a new material */
1881 mat_new = BKE_material_copy(ma);
1883 /* rename the material*/
1884 BLI_strncpy(mat_new->id.name, idname, sizeof(mat_new->id.name));
1885 id_us_min((ID *)mat_new);
1887 mat_nr = mesh_addmaterial(me, mat_new);
1888 decode_tfaceflag(mat_new, flag, 1);
1891 printf("Error: Unable to create Material \"%s\" for Mesh \"%s.", idname + 2, me->id.name + 2);
1892 mat_nr = mf->mat_nr;
1897 /* if the material has a texture but no texture channel
1898 * set "Face Textures [Alpha]" Material options
1899 * actually we need to run it always, because of old behavior
1900 * of using face texture if any texture channel was present (multitex) */
1901 //if ((!mat_new->mtex[0]) && (!mat_new->mtex[0]->tex))
1902 set_facetexture_flags(mat_new, tf->tpage);
1904 /* set the material number to the face*/
1905 mf->mat_nr = mat_nr;
1907 /* remove material from mesh */
1908 for (a = 0; a < me->totcol; ) {
1909 if (me->mat[a] == ma) {
1910 BKE_material_pop_id(&me->id, a, true);
1920 #define MAT_BGE_DISPUTED -99999
1922 int do_version_tface(Main *main)
1928 CustomDataLayer *cdl;
1933 /* Operator in help menu has been removed for 2.7x */
1936 /* sometimes mesh has no materials but will need a new one. In those
1937 * cases we need to ignore the mf->mat_nr and only look at the face
1938 * mode because it can be zero as uninitialized or the 1st created material
1940 int nomaterialslots;
1942 /* alert to user to check the console */
1945 /* mark all the materials to conversion with a flag
1946 * if there is tface create a complete flag for that storing in flag
1947 * if there is tface and flag > 0: creates a new flag based on this face
1948 * if flags are different set flag to -1
1951 /* 1st part: marking mesh materials to update */
1952 for (me = main->mesh.first; me; me = me->id.next) {
1953 if (me->id.lib) continue;
1955 /* get the active tface layer */
1956 index = CustomData_get_active_layer_index(&me->fdata, CD_MTFACE);
1957 cdl = (index == -1) ? NULL : &me->fdata.layers[index];
1960 nomaterialslots = (me->totcol == 0 ? 1 : 0);
1962 /* loop over all the faces*/
1963 for (a = 0, mf = me->mface; a < me->totface; a++, mf++) {
1964 /* texface data for this face */
1965 tf = ((MTFace *)cdl->data) + a;
1967 /* conversion should happen only once */
1969 tf->mode &= ~TF_CONVERTED;
1971 if ((tf->mode & TF_CONVERTED)) continue;
1972 else tf->mode |= TF_CONVERTED;
1975 /* no material slots */
1976 if (nomaterialslots) {
1977 flag = encode_tfaceflag(tf, 1);
1979 /* create/find a new material and assign to the face */
1980 if (check_tfaceneedmaterial(flag)) {
1981 mf->mat_nr = convert_tfacenomaterial(main, me, tf, flag);
1983 /* else mark them as no-material to be reverted to 0 later */
1988 else if (mf->mat_nr < me->totcol) {
1989 ma = me->mat[mf->mat_nr];
1991 /* no material create one if necessary */
1993 /* find a new material and assign to the face */
1994 flag = encode_tfaceflag(tf, 1);
1996 /* create/find a new material and assign to the face */
1997 if (check_tfaceneedmaterial(flag))
1998 mf->mat_nr = convert_tfacenomaterial(main, me, tf, flag);
2003 /* we can't read from this if it comes from a library,
2004 * at doversion time: direct_link might not have happened on it,
2005 * so ma->mtex is not pointing to valid memory yet.
2006 * later we could, but it's better not */
2007 else if (ma->id.lib)
2010 /* material already marked as disputed */
2011 else if (ma->game.flag == MAT_BGE_DISPUTED)
2014 /* found a material */
2016 flag = encode_tfaceflag(tf, ((fileload) ? 0 : 1));
2018 /* first time changing this material */
2019 if (ma->game.flag == 0)
2020 ma->game.flag = -flag;
2022 /* mark material as disputed */
2023 else if (ma->game.flag != -flag) {
2024 ma->game.flag = MAT_BGE_DISPUTED;
2028 /* material ok so far */
2030 ma->game.flag = -flag;
2032 /* some people uses multitexture with TexFace by creating a texture
2033 * channel which not necessarily the tf->tpage image. But the game engine
2034 * was enabling it. Now it's required to set "Face Texture [Alpha] in the
2035 * material settings. */
2037 set_facetexture_flags(ma, tf->tpage);
2046 /* if we didn't have material slot and now we do, we need to
2047 * make sure the materials are correct */
2048 if (nomaterialslots) {
2049 if (me->totcol > 0) {
2050 for (a = 0, mf = me->mface; a < me->totface; a++, mf++) {
2051 if (mf->mat_nr == -1) {
2052 /* texface data for this face */
2053 tf = ((MTFace *)cdl->data) + a;
2054 mf->mat_nr = convert_tfacenomaterial(main, me, tf, encode_tfaceflag(tf, 1));
2059 for (a = 0, mf = me->mface; a < me->totface; a++, mf++) {
2067 /* 2nd part - conversion */
2068 /* skip library files */
2070 /* we shouldn't loop through the materials created in the loop. make the loop stop at its original length) */
2071 for (ma = main->mat.first, a = 0; ma; ma = ma->id.next, a++) {
2072 if (ma->id.lib) continue;
2074 /* disputed material */
2075 if (ma->game.flag == MAT_BGE_DISPUTED) {
2078 printf("Warning: material \"%s\" skipped.\n", ma->id.name + 2);
2082 convert_tfacematerial(main, ma);
2087 /* no conflicts in this material - 90% of cases
2088 * convert from tface system to material */
2089 else if (ma->game.flag < 0) {
2090 decode_tfaceflag(ma, -(ma->game.flag), 1);
2092 /* material is good make sure all faces using
2093 * this material are set to converted */
2095 for (me = main->mesh.first; me; me = me->id.next) {
2096 /* check if this mesh uses this material */
2097 for (a = 0; a < me->totcol; a++)
2098 if (me->mat[a] == ma) break;
2100 /* no material found */
2101 if (a == me->totcol) continue;
2103 /* get the active tface layer */
2104 index = CustomData_get_active_layer_index(&me->fdata, CD_MTFACE);
2105 cdl = (index == -1) ? NULL : &me->fdata.layers[index];
2108 /* loop over all the faces and stop at the ones that use the material*/
2109 for (a = 0, mf = me->mface; a < me->totface; a++, mf++) {
2110 if (me->mat[mf->mat_nr] == ma) {
2111 /* texface data for this face */
2112 tf = ((MTFace *)cdl->data) + a;
2113 tf->mode |= TF_CONVERTED;
2119 /* material is not used by faces with texface
2120 * set the default flag - do it only once */
2123 ma->game.flag = GEMAT_BACKCULL;