Cleanup: Move some duplicate code to new function

This commit is contained in:
Antonioya
2018-08-08 13:37:56 +02:00
parent 08e49c034b
commit 78a6fa1a72
4 changed files with 58 additions and 69 deletions

View File

@@ -33,6 +33,7 @@
#include "MEM_guardedalloc.h"
#include "BLI_blenlib.h"
#include "BLI_ghash.h"
#include "BLI_utildefines.h"
#include "BLI_math_vector.h"
#include "BLI_math_color.h"
@@ -45,6 +46,7 @@
#include "DNA_gpencil_modifier_types.h"
#include "BKE_global.h"
#include "BKE_main.h"
#include "BKE_object.h"
#include "BKE_lattice.h"
#include "BKE_material.h"
@@ -52,6 +54,8 @@
#include "BKE_gpencil_modifier.h"
#include "BKE_colortools.h"
#include "DEG_depsgraph.h"
#include "MOD_gpencil_modifiertypes.h"
#include "MOD_gpencil_util.h"
@@ -140,3 +144,46 @@ float get_modifier_point_weight(MDeformVert *dvert, int inverse, int vindex)
return weight;
}
/* set material when apply modifiers (used in tint and color modifier) */
void gpencil_apply_modifier_material(
Main *bmain, Object *ob, Material *mat,
GHash *gh_color, bGPDstroke *gps, bool crt_material)
{
MaterialGPencilStyle *gp_style = mat->gp_style;
/* look for color */
if (crt_material) {
Material *newmat = BLI_ghash_lookup(gh_color, mat->id.name);
if (newmat == NULL) {
BKE_object_material_slot_add(bmain, ob);
newmat = BKE_material_copy(bmain, mat);
newmat->preview = NULL;
assign_material(bmain, ob, newmat, ob->totcol, BKE_MAT_ASSIGN_USERPREF);
copy_v4_v4(newmat->gp_style->stroke_rgba, gps->runtime.tmp_stroke_rgba);
copy_v4_v4(newmat->gp_style->fill_rgba, gps->runtime.tmp_fill_rgba);
BLI_ghash_insert(gh_color, mat->id.name, newmat);
DEG_id_tag_update(&newmat->id, DEG_TAG_COPY_ON_WRITE);
}
/* reasign color index */
int idx = BKE_object_material_slot_find_index(ob, newmat);
gps->mat_nr = idx - 1;
}
else {
/* reuse existing color (but update only first time) */
if (BLI_ghash_lookup(gh_color, mat->id.name) == NULL) {
copy_v4_v4(gp_style->stroke_rgba, gps->runtime.tmp_stroke_rgba);
copy_v4_v4(gp_style->fill_rgba, gps->runtime.tmp_fill_rgba);
BLI_ghash_insert(gh_color, mat->id.name, mat);
}
/* update previews (icon and thumbnail) */
if (mat->preview != NULL) {
mat->preview->flag[ICON_SIZE_ICON] |= PRV_CHANGED;
mat->preview->flag[ICON_SIZE_PREVIEW] |= PRV_CHANGED;
}
DEG_id_tag_update(&mat->id, DEG_TAG_COPY_ON_WRITE);
}
}

View File

@@ -33,10 +33,13 @@
#ifndef __MOD_GPENCIL_UTIL_H__
#define __MOD_GPENCIL_UTIL_H__
struct Main;
struct Object;
struct bGPDlayer;
struct bGPDstroke;
struct MDeformVert;
struct Material;
struct GHash;
bool is_stroke_affected_by_modifier(
struct Object *ob, char *mlayername, int mpassindex, int minpoints,
@@ -44,4 +47,8 @@ bool is_stroke_affected_by_modifier(
float get_modifier_point_weight(struct MDeformVert *dvert, int inverse, int vindex);
void gpencil_apply_modifier_material(
struct Main *bmain, struct Object *ob, struct Material *mat,
struct GHash *gh_color, struct bGPDstroke *gps, bool crt_material);
#endif /* __MOD_GPENCIL_UTIL_H__ */

View File

@@ -122,41 +122,8 @@ static void bakeModifier(
deformStroke(md, depsgraph, ob, gpl, gps);
/* look for color */
if (mmd->flag & GP_COLOR_CREATE_COLORS) {
Material *newmat = BLI_ghash_lookup(gh_color, mat->id.name);
if (newmat == NULL) {
BKE_object_material_slot_add(bmain, ob);
newmat = BKE_material_copy(bmain, mat);
newmat->preview = NULL;
assign_material(bmain, ob, newmat, ob->totcol, BKE_MAT_ASSIGN_USERPREF);
copy_v4_v4(newmat->gp_style->stroke_rgba, gps->runtime.tmp_stroke_rgba);
copy_v4_v4(newmat->gp_style->fill_rgba, gps->runtime.tmp_fill_rgba);
BLI_ghash_insert(gh_color, mat->id.name, newmat);
DEG_id_tag_update(&newmat->id, DEG_TAG_COPY_ON_WRITE);
}
/* reasign color index */
int idx = BKE_object_material_slot_find_index(ob, newmat);
gps->mat_nr = idx - 1;
}
else {
/* reuse existing color (but update only first time) */
if (BLI_ghash_lookup(gh_color, mat->id.name) == NULL) {
copy_v4_v4(gp_style->stroke_rgba, gps->runtime.tmp_stroke_rgba);
copy_v4_v4(gp_style->fill_rgba, gps->runtime.tmp_fill_rgba);
BLI_ghash_insert(gh_color, mat->id.name, mat);
}
/* update previews (icon and thumbnail) */
if (mat->preview != NULL) {
mat->preview->flag[ICON_SIZE_ICON] |= PRV_CHANGED;
mat->preview->flag[ICON_SIZE_PREVIEW] |= PRV_CHANGED;
}
DEG_id_tag_update(&mat->id, DEG_TAG_COPY_ON_WRITE);
}
gpencil_apply_modifier_material(bmain, ob, mat, gh_color, gps,
(bool)(mmd->flag & GP_COLOR_CREATE_COLORS));
}
}
}

View File

@@ -130,40 +130,8 @@ static void bakeModifier(
deformStroke(md, depsgraph, ob, gpl, gps);
/* look for color */
if (mmd->flag & GP_TINT_CREATE_COLORS) {
Material *newmat = (Material *)BLI_ghash_lookup(gh_color, mat->id.name);
if (newmat == NULL) {
BKE_object_material_slot_add(bmain, ob);
newmat = BKE_material_copy(bmain, mat);
newmat->preview = NULL;
assign_material(bmain, ob, newmat, ob->totcol, BKE_MAT_ASSIGN_USERPREF);
copy_v4_v4(newmat->gp_style->stroke_rgba, gps->runtime.tmp_stroke_rgba);
copy_v4_v4(newmat->gp_style->fill_rgba, gps->runtime.tmp_fill_rgba);
BLI_ghash_insert(gh_color, mat->id.name, newmat);
DEG_id_tag_update(&newmat->id, DEG_TAG_COPY_ON_WRITE);
}
/* reasign color index */
int idx = BKE_object_material_slot_find_index(ob, newmat);
gps->mat_nr = idx - 1;
}
else {
/* reuse existing color (but update only first time) */
if (BLI_ghash_lookup(gh_color, mat->id.name) == NULL) {
copy_v4_v4(gp_style->stroke_rgba, gps->runtime.tmp_stroke_rgba);
copy_v4_v4(gp_style->fill_rgba, gps->runtime.tmp_fill_rgba);
BLI_ghash_insert(gh_color, mat->id.name, mat);
}
/* update previews (icon and thumbnail) */
if (mat->preview != NULL) {
mat->preview->flag[ICON_SIZE_ICON] |= PRV_CHANGED;
mat->preview->flag[ICON_SIZE_PREVIEW] |= PRV_CHANGED;
}
DEG_id_tag_update(&mat->id, DEG_TAG_COPY_ON_WRITE);
}
gpencil_apply_modifier_material(bmain, ob, mat, gh_color, gps,
(bool)(mmd->flag & GP_TINT_CREATE_COLORS));
}
}
}