b89c37924552dc58e5e4b0579af4d3eebb0094e2
[blender.git] / source / blender / gpencil_modifiers / intern / MOD_gpencilopacity.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software  Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2017, Blender Foundation
19  * This is a new part of Blender
20  *
21  * Contributor(s): Antonio Vazquez
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  *
25  */
26
27 /** \file blender/gpencil_modifiers/intern/MOD_gpencilopacity.c
28  *  \ingroup modifiers
29  */
30
31 #include <stdio.h>
32
33 #include "BLI_blenlib.h"
34 #include "BLI_ghash.h"
35 #include "BLI_math_vector.h"
36 #include "BLI_utildefines.h"
37
38 #include "DNA_meshdata_types.h"
39 #include "DNA_scene_types.h"
40 #include "DNA_object_types.h"
41 #include "DNA_gpencil_types.h"
42 #include "DNA_gpencil_modifier_types.h"
43
44 #include "BKE_context.h"
45 #include "BKE_deform.h"
46 #include "BKE_material.h"
47 #include "BKE_gpencil.h"
48 #include "BKE_gpencil_modifier.h"
49 #include "BKE_main.h"
50
51 #include "DEG_depsgraph.h"
52
53 #include "MOD_gpencil_util.h"
54 #include "MOD_gpencil_modifiertypes.h"
55
56 static void initData(GpencilModifierData *md)
57 {
58         OpacityGpencilModifierData *gpmd = (OpacityGpencilModifierData *)md;
59         gpmd->pass_index = 0;
60         gpmd->factor = 1.0f;
61         gpmd->layername[0] = '\0';
62         gpmd->vgname[0] = '\0';
63         gpmd->flag |= GP_OPACITY_CREATE_COLORS;
64         gpmd->modify_color = GP_MODIFY_COLOR_BOTH;
65 }
66
67 static void copyData(const GpencilModifierData *md, GpencilModifierData *target)
68 {
69         BKE_gpencil_modifier_copyData_generic(md, target);
70 }
71
72 /* opacity strokes */
73 static void deformStroke(
74         GpencilModifierData *md, Depsgraph *UNUSED(depsgraph),
75         Object *ob, bGPDlayer *gpl, bGPDstroke *gps)
76 {
77         OpacityGpencilModifierData *mmd = (OpacityGpencilModifierData *)md;
78         const int def_nr = defgroup_name_index(ob, mmd->vgname);
79
80         if (!is_stroke_affected_by_modifier(
81                     ob,
82                     mmd->layername, mmd->pass_index, mmd->layer_pass, 1, gpl, gps,
83                     mmd->flag & GP_OPACITY_INVERT_LAYER, mmd->flag & GP_OPACITY_INVERT_PASS,
84                     mmd->flag & GP_OPACITY_INVERT_LAYERPASS))
85         {
86                 return;
87         }
88
89         if (mmd->modify_color != GP_MODIFY_COLOR_FILL) {
90                 gps->runtime.tmp_stroke_rgba[3] *= mmd->factor;
91                 /* if factor is > 1, then force opacity */
92                 if (mmd->factor > 1.0f) {
93                         gps->runtime.tmp_stroke_rgba[3] += mmd->factor - 1.0f;
94                 }
95                 CLAMP(gps->runtime.tmp_stroke_rgba[3], 0.0f, 1.0f);
96         }
97
98         if (mmd->modify_color != GP_MODIFY_COLOR_STROKE) {
99                 gps->runtime.tmp_fill_rgba[3] *= mmd->factor;
100                 /* if factor is > 1, then force opacity */
101                 if (mmd->factor > 1.0f && gps->runtime.tmp_fill_rgba[3] > 1e-5) {
102                         gps->runtime.tmp_fill_rgba[3] += mmd->factor - 1.0f;
103                 }
104                 CLAMP(gps->runtime.tmp_fill_rgba[3], 0.0f, 1.0f);
105         }
106
107         /* if opacity > 1.0, affect the strength of the stroke */
108         if (mmd->factor > 1.0f) {
109                 for (int i = 0; i < gps->totpoints; i++) {
110                         bGPDspoint *pt = &gps->points[i];
111                         MDeformVert *dvert = gps->dvert != NULL ? &gps->dvert[i] : NULL;
112
113                         /* verify vertex group */
114                         const float weight = get_modifier_point_weight(dvert, (mmd->flag & GP_OPACITY_INVERT_VGROUP) != 0, def_nr);
115                         if (weight < 0.0f) {
116                                 pt->strength += mmd->factor - 1.0f;
117                         }
118                         else {
119                                 pt->strength += (mmd->factor - 1.0f) * weight;
120                         }
121                         CLAMP(pt->strength, 0.0f, 1.0f);
122                 }
123         }
124 }
125
126 static void bakeModifier(
127         Main *bmain, Depsgraph *depsgraph,
128         GpencilModifierData *md, Object *ob)
129 {
130         OpacityGpencilModifierData *mmd = (OpacityGpencilModifierData *)md;
131         bGPdata *gpd = ob->data;
132
133         GHash *gh_color = BLI_ghash_str_new("GP_Opacity modifier");
134         for (bGPDlayer *gpl = gpd->layers.first; gpl; gpl = gpl->next) {
135                 for (bGPDframe *gpf = gpl->frames.first; gpf; gpf = gpf->next) {
136                         for (bGPDstroke *gps = gpf->strokes.first; gps; gps = gps->next) {
137
138                                 Material *mat = give_current_material(ob, gps->mat_nr + 1);
139                                 if (mat == NULL)
140                                         continue;
141                                 MaterialGPencilStyle *gp_style = mat->gp_style;
142                                 /* skip stroke if it doesn't have color info */
143                                 if (ELEM(NULL, gp_style))
144                                         continue;
145
146                                 copy_v4_v4(gps->runtime.tmp_stroke_rgba, gp_style->stroke_rgba);
147                                 copy_v4_v4(gps->runtime.tmp_fill_rgba, gp_style->fill_rgba);
148
149                                 deformStroke(md, depsgraph, ob, gpl, gps);
150
151                                 gpencil_apply_modifier_material(
152                                         bmain, ob, mat, gh_color, gps,
153                                         (bool)(mmd->flag & GP_OPACITY_CREATE_COLORS));
154                         }
155                 }
156         }
157         /* free hash buffers */
158         if (gh_color) {
159                 BLI_ghash_free(gh_color, NULL, NULL);
160                 gh_color = NULL;
161         }
162 }
163
164 GpencilModifierTypeInfo modifierType_Gpencil_Opacity = {
165         /* name */              "Opacity",
166         /* structName */        "OpacityGpencilModifierData",
167         /* structSize */        sizeof(OpacityGpencilModifierData),
168         /* type */              eGpencilModifierTypeType_Gpencil,
169         /* flags */             eGpencilModifierTypeFlag_SupportsEditmode,
170
171         /* copyData */          copyData,
172
173         /* deformStroke */      deformStroke,
174         /* generateStrokes */   NULL,
175         /* bakeModifier */      bakeModifier,
176         /* remapTime */         NULL,
177
178         /* initData */          initData,
179         /* freeData */          NULL,
180         /* isDisabled */        NULL,
181         /* updateDepsgraph */   NULL,
182         /* dependsOnTime */     NULL,
183         /* foreachObjectLink */ NULL,
184         /* foreachIDLink */     NULL,
185         /* foreachTexLink */    NULL,
186 };