Cleanup: no need for plural for term 'collision'
[blender.git] / source / blender / blenkernel / intern / brush.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16
17 /** \file
18  * \ingroup bke
19  */
20
21 #include "MEM_guardedalloc.h"
22
23 #include "DNA_brush_types.h"
24 #include "DNA_defaults.h"
25 #include "DNA_gpencil_types.h"
26 #include "DNA_object_types.h"
27 #include "DNA_scene_types.h"
28
29 #include "BLI_listbase.h"
30 #include "BLI_math.h"
31 #include "BLI_rand.h"
32
33 #include "BLT_translation.h"
34
35 #include "BKE_brush.h"
36 #include "BKE_colortools.h"
37 #include "BKE_context.h"
38 #include "BKE_icons.h"
39 #include "BKE_idtype.h"
40 #include "BKE_lib_id.h"
41 #include "BKE_lib_query.h"
42 #include "BKE_lib_remap.h"
43 #include "BKE_main.h"
44 #include "BKE_material.h"
45 #include "BKE_paint.h"
46 #include "BKE_texture.h"
47
48 #include "IMB_colormanagement.h"
49 #include "IMB_imbuf.h"
50 #include "IMB_imbuf_types.h"
51
52 #include "RE_render_ext.h" /* RE_texture_evaluate */
53
54 static void brush_init_data(ID *id)
55 {
56   Brush *brush = (Brush *)id;
57   BLI_assert(MEMCMP_STRUCT_AFTER_IS_ZERO(brush, id));
58
59   MEMCPY_STRUCT_AFTER(brush, DNA_struct_default_get(Brush), id);
60
61   /* enable fake user by default */
62   id_fake_user_set(&brush->id);
63
64   /* the default alpha falloff curve */
65   BKE_brush_curve_preset(brush, CURVE_PRESET_SMOOTH);
66 }
67
68 static void brush_copy_data(Main *UNUSED(bmain), ID *id_dst, const ID *id_src, const int flag)
69 {
70   Brush *brush_dst = (Brush *)id_dst;
71   const Brush *brush_src = (const Brush *)id_src;
72   if (brush_src->icon_imbuf) {
73     brush_dst->icon_imbuf = IMB_dupImBuf(brush_src->icon_imbuf);
74   }
75
76   if ((flag & LIB_ID_COPY_NO_PREVIEW) == 0) {
77     BKE_previewimg_id_copy(&brush_dst->id, &brush_src->id);
78   }
79   else {
80     brush_dst->preview = NULL;
81   }
82
83   brush_dst->curve = BKE_curvemapping_copy(brush_src->curve);
84   if (brush_src->gpencil_settings != NULL) {
85     brush_dst->gpencil_settings = MEM_dupallocN(brush_src->gpencil_settings);
86     brush_dst->gpencil_settings->curve_sensitivity = BKE_curvemapping_copy(
87         brush_src->gpencil_settings->curve_sensitivity);
88     brush_dst->gpencil_settings->curve_strength = BKE_curvemapping_copy(
89         brush_src->gpencil_settings->curve_strength);
90     brush_dst->gpencil_settings->curve_jitter = BKE_curvemapping_copy(
91         brush_src->gpencil_settings->curve_jitter);
92
93     brush_dst->gpencil_settings->curve_rand_pressure = BKE_curvemapping_copy(
94         brush_src->gpencil_settings->curve_rand_pressure);
95     brush_dst->gpencil_settings->curve_rand_strength = BKE_curvemapping_copy(
96         brush_src->gpencil_settings->curve_rand_strength);
97     brush_dst->gpencil_settings->curve_rand_uv = BKE_curvemapping_copy(
98         brush_src->gpencil_settings->curve_rand_uv);
99     brush_dst->gpencil_settings->curve_rand_hue = BKE_curvemapping_copy(
100         brush_src->gpencil_settings->curve_rand_hue);
101     brush_dst->gpencil_settings->curve_rand_saturation = BKE_curvemapping_copy(
102         brush_src->gpencil_settings->curve_rand_saturation);
103     brush_dst->gpencil_settings->curve_rand_value = BKE_curvemapping_copy(
104         brush_src->gpencil_settings->curve_rand_value);
105   }
106
107   /* enable fake user by default */
108   id_fake_user_set(&brush_dst->id);
109 }
110
111 static void brush_free_data(ID *id)
112 {
113   Brush *brush = (Brush *)id;
114   if (brush->icon_imbuf) {
115     IMB_freeImBuf(brush->icon_imbuf);
116   }
117   BKE_curvemapping_free(brush->curve);
118
119   if (brush->gpencil_settings != NULL) {
120     BKE_curvemapping_free(brush->gpencil_settings->curve_sensitivity);
121     BKE_curvemapping_free(brush->gpencil_settings->curve_strength);
122     BKE_curvemapping_free(brush->gpencil_settings->curve_jitter);
123
124     BKE_curvemapping_free(brush->gpencil_settings->curve_rand_pressure);
125     BKE_curvemapping_free(brush->gpencil_settings->curve_rand_strength);
126     BKE_curvemapping_free(brush->gpencil_settings->curve_rand_uv);
127     BKE_curvemapping_free(brush->gpencil_settings->curve_rand_hue);
128     BKE_curvemapping_free(brush->gpencil_settings->curve_rand_saturation);
129     BKE_curvemapping_free(brush->gpencil_settings->curve_rand_value);
130
131     MEM_SAFE_FREE(brush->gpencil_settings);
132   }
133
134   MEM_SAFE_FREE(brush->gradient);
135
136   BKE_previewimg_free(&(brush->preview));
137 }
138
139 static void brush_make_local(Main *bmain, ID *id, const int flags)
140 {
141   Brush *brush = (Brush *)id;
142   const bool lib_local = (flags & LIB_ID_MAKELOCAL_FULL_LIBRARY) != 0;
143   bool is_local = false, is_lib = false;
144
145   /* - only lib users: do nothing (unless force_local is set)
146    * - only local users: set flag
147    * - mixed: make copy
148    */
149
150   if (!ID_IS_LINKED(brush)) {
151     return;
152   }
153
154   if (brush->clone.image) {
155     /* Special case: ima always local immediately. Clone image should only have one user anyway. */
156     BKE_lib_id_make_local(bmain, &brush->clone.image->id, false, 0);
157   }
158
159   BKE_library_ID_test_usages(bmain, brush, &is_local, &is_lib);
160
161   if (lib_local || is_local) {
162     if (!is_lib) {
163       BKE_lib_id_clear_library_data(bmain, &brush->id);
164       BKE_lib_id_expand_local(bmain, &brush->id);
165
166       /* enable fake user by default */
167       id_fake_user_set(&brush->id);
168     }
169     else {
170       Brush *brush_new = BKE_brush_copy(bmain, brush); /* Ensures FAKE_USER is set */
171
172       brush_new->id.us = 0;
173
174       /* setting newid is mandatory for complex make_lib_local logic... */
175       ID_NEW_SET(brush, brush_new);
176
177       if (!lib_local) {
178         BKE_libblock_remap(bmain, brush, brush_new, ID_REMAP_SKIP_INDIRECT_USAGE);
179       }
180     }
181   }
182 }
183
184 static void brush_foreach_id(ID *id, LibraryForeachIDData *data)
185 {
186   Brush *brush = (Brush *)id;
187
188   BKE_LIB_FOREACHID_PROCESS(data, brush->toggle_brush, IDWALK_CB_NOP);
189   BKE_LIB_FOREACHID_PROCESS(data, brush->clone.image, IDWALK_CB_NOP);
190   BKE_LIB_FOREACHID_PROCESS(data, brush->paint_curve, IDWALK_CB_USER);
191   if (brush->gpencil_settings) {
192     BKE_LIB_FOREACHID_PROCESS(data, brush->gpencil_settings->material, IDWALK_CB_USER);
193   }
194   BKE_texture_mtex_foreach_id(data, &brush->mtex);
195   BKE_texture_mtex_foreach_id(data, &brush->mask_mtex);
196 }
197
198 IDTypeInfo IDType_ID_BR = {
199     .id_code = ID_BR,
200     .id_filter = FILTER_ID_BR,
201     .main_listbase_index = INDEX_ID_BR,
202     .struct_size = sizeof(Brush),
203     .name = "Brush",
204     .name_plural = "brushes",
205     .translation_context = BLT_I18NCONTEXT_ID_BRUSH,
206     .flags = 0,
207
208     .init_data = brush_init_data,
209     .copy_data = brush_copy_data,
210     .free_data = brush_free_data,
211     .make_local = brush_make_local,
212     .foreach_id = brush_foreach_id,
213 };
214
215 static RNG *brush_rng;
216
217 void BKE_brush_system_init(void)
218 {
219   brush_rng = BLI_rng_new(0);
220   BLI_rng_srandom(brush_rng, 31415682);
221 }
222
223 void BKE_brush_system_exit(void)
224 {
225   if (brush_rng == NULL) {
226     return;
227   }
228   BLI_rng_free(brush_rng);
229   brush_rng = NULL;
230 }
231
232 static void brush_defaults(Brush *brush)
233 {
234
235   const Brush *brush_def = DNA_struct_default_get(Brush);
236
237 #define FROM_DEFAULT(member) memcpy(&brush->member, &brush_def->member, sizeof(brush->member))
238 #define FROM_DEFAULT_PTR(member) memcpy(brush->member, brush_def->member, sizeof(brush->member))
239
240   FROM_DEFAULT(blend);
241   FROM_DEFAULT(flag);
242   FROM_DEFAULT(weight);
243   FROM_DEFAULT(size);
244   FROM_DEFAULT(alpha);
245   FROM_DEFAULT(hardness);
246   FROM_DEFAULT(autosmooth_factor);
247   FROM_DEFAULT(topology_rake_factor);
248   FROM_DEFAULT(crease_pinch_factor);
249   FROM_DEFAULT(normal_radius_factor);
250   FROM_DEFAULT(area_radius_factor);
251   FROM_DEFAULT(disconnected_distance_max);
252   FROM_DEFAULT(sculpt_plane);
253   FROM_DEFAULT(plane_offset);
254   FROM_DEFAULT(clone.alpha);
255   FROM_DEFAULT(normal_weight);
256   FROM_DEFAULT(fill_threshold);
257   FROM_DEFAULT(flag);
258   FROM_DEFAULT(sampling_flag);
259   FROM_DEFAULT_PTR(rgb);
260   FROM_DEFAULT_PTR(secondary_rgb);
261   FROM_DEFAULT(spacing);
262   FROM_DEFAULT(smooth_stroke_radius);
263   FROM_DEFAULT(smooth_stroke_factor);
264   FROM_DEFAULT(rate);
265   FROM_DEFAULT(jitter);
266   FROM_DEFAULT(texture_sample_bias);
267   FROM_DEFAULT(texture_overlay_alpha);
268   FROM_DEFAULT(mask_overlay_alpha);
269   FROM_DEFAULT(cursor_overlay_alpha);
270   FROM_DEFAULT(overlay_flags);
271   FROM_DEFAULT_PTR(add_col);
272   FROM_DEFAULT_PTR(sub_col);
273   FROM_DEFAULT(stencil_pos);
274   FROM_DEFAULT(stencil_dimension);
275   FROM_DEFAULT(mtex);
276   FROM_DEFAULT(mask_mtex);
277
278 #undef FROM_DEFAULT
279 #undef FROM_DEFAULT_PTR
280 }
281
282 /* Datablock add/copy/free/make_local */
283
284 /**
285  * \note Resulting brush will have two users: one as a fake user,
286  * another is assumed to be used by the caller.
287  */
288 Brush *BKE_brush_add(Main *bmain, const char *name, const eObjectMode ob_mode)
289 {
290   Brush *brush;
291
292   brush = BKE_libblock_alloc(bmain, ID_BR, name, 0);
293
294   brush_init_data(&brush->id);
295
296   brush->ob_mode = ob_mode;
297
298   return brush;
299 }
300
301 /* add grease pencil settings */
302 void BKE_brush_init_gpencil_settings(Brush *brush)
303 {
304   if (brush->gpencil_settings == NULL) {
305     brush->gpencil_settings = MEM_callocN(sizeof(BrushGpencilSettings), "BrushGpencilSettings");
306   }
307
308   brush->gpencil_settings->draw_smoothlvl = 1;
309   brush->gpencil_settings->flag = 0;
310   brush->gpencil_settings->flag |= GP_BRUSH_USE_PRESSURE;
311   brush->gpencil_settings->draw_strength = 1.0f;
312   brush->gpencil_settings->draw_jitter = 0.0f;
313   brush->gpencil_settings->flag |= GP_BRUSH_USE_JITTER_PRESSURE;
314   brush->gpencil_settings->icon_id = GP_BRUSH_ICON_PEN;
315
316   /* curves */
317   brush->gpencil_settings->curve_sensitivity = BKE_curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
318   brush->gpencil_settings->curve_strength = BKE_curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
319   brush->gpencil_settings->curve_jitter = BKE_curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
320
321   brush->gpencil_settings->curve_rand_pressure = BKE_curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
322   brush->gpencil_settings->curve_rand_strength = BKE_curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
323   brush->gpencil_settings->curve_rand_uv = BKE_curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
324   brush->gpencil_settings->curve_rand_hue = BKE_curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
325   brush->gpencil_settings->curve_rand_saturation = BKE_curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
326   brush->gpencil_settings->curve_rand_value = BKE_curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
327 }
328
329 /* add a new gp-brush */
330 Brush *BKE_brush_add_gpencil(Main *bmain, ToolSettings *ts, const char *name, eObjectMode mode)
331 {
332   Paint *paint = NULL;
333   Brush *brush;
334   switch (mode) {
335     case OB_MODE_PAINT_GPENCIL: {
336       paint = &ts->gp_paint->paint;
337       break;
338     }
339     case OB_MODE_SCULPT_GPENCIL: {
340       paint = &ts->gp_sculptpaint->paint;
341       break;
342     }
343     case OB_MODE_WEIGHT_GPENCIL: {
344       paint = &ts->gp_weightpaint->paint;
345       break;
346     }
347     case OB_MODE_VERTEX_GPENCIL: {
348       paint = &ts->gp_vertexpaint->paint;
349       break;
350     }
351     default:
352       paint = &ts->gp_paint->paint;
353   }
354
355   brush = BKE_brush_add(bmain, name, mode);
356
357   BKE_paint_brush_set(paint, brush);
358   id_us_min(&brush->id);
359
360   brush->size = 3;
361
362   /* grease pencil basic settings */
363   BKE_brush_init_gpencil_settings(brush);
364
365   /* return brush */
366   return brush;
367 }
368
369 /* Delete a Brush. */
370 bool BKE_brush_delete(Main *bmain, Brush *brush)
371 {
372   if (brush->id.tag & LIB_TAG_INDIRECT) {
373     return false;
374   }
375   if (BKE_library_ID_is_indirectly_used(bmain, brush) && ID_REAL_USERS(brush) <= 1 &&
376       ID_EXTRA_USERS(brush) == 0) {
377     return false;
378   }
379
380   BKE_id_delete(bmain, brush);
381
382   return true;
383 }
384
385 /* grease pencil cumapping->preset */
386 typedef enum eGPCurveMappingPreset {
387   GPCURVE_PRESET_PENCIL = 0,
388   GPCURVE_PRESET_INK = 1,
389   GPCURVE_PRESET_INKNOISE = 2,
390   GPCURVE_PRESET_MARKER = 3,
391   GPCURVE_PRESET_CHISEL_SENSIVITY = 4,
392   GPCURVE_PRESET_CHISEL_STRENGTH = 5,
393 } eGPCurveMappingPreset;
394
395 static void brush_gpencil_curvemap_reset(CurveMap *cuma, int tot, int preset)
396 {
397   if (cuma->curve) {
398     MEM_freeN(cuma->curve);
399   }
400
401   cuma->totpoint = tot;
402   cuma->curve = MEM_callocN(cuma->totpoint * sizeof(CurveMapPoint), __func__);
403
404   switch (preset) {
405     case GPCURVE_PRESET_PENCIL:
406       cuma->curve[0].x = 0.0f;
407       cuma->curve[0].y = 0.0f;
408       cuma->curve[1].x = 0.75115f;
409       cuma->curve[1].y = 0.25f;
410       cuma->curve[2].x = 1.0f;
411       cuma->curve[2].y = 1.0f;
412       break;
413     case GPCURVE_PRESET_INK:
414       cuma->curve[0].x = 0.0f;
415       cuma->curve[0].y = 0.0f;
416       cuma->curve[1].x = 0.63448f;
417       cuma->curve[1].y = 0.375f;
418       cuma->curve[2].x = 1.0f;
419       cuma->curve[2].y = 1.0f;
420       break;
421     case GPCURVE_PRESET_INKNOISE:
422       cuma->curve[0].x = 0.0f;
423       cuma->curve[0].y = 0.0f;
424       cuma->curve[1].x = 0.55f;
425       cuma->curve[1].y = 0.45f;
426       cuma->curve[2].x = 0.85f;
427       cuma->curve[2].y = 1.0f;
428       break;
429     case GPCURVE_PRESET_MARKER:
430       cuma->curve[0].x = 0.0f;
431       cuma->curve[0].y = 0.0f;
432       cuma->curve[1].x = 0.38f;
433       cuma->curve[1].y = 0.22f;
434       cuma->curve[2].x = 0.65f;
435       cuma->curve[2].y = 0.68f;
436       cuma->curve[3].x = 1.0f;
437       cuma->curve[3].y = 1.0f;
438       break;
439     case GPCURVE_PRESET_CHISEL_SENSIVITY:
440       cuma->curve[0].x = 0.0f;
441       cuma->curve[0].y = 0.0f;
442       cuma->curve[1].x = 0.25f;
443       cuma->curve[1].y = 0.40f;
444       cuma->curve[2].x = 1.0f;
445       cuma->curve[2].y = 1.0f;
446       break;
447     case GPCURVE_PRESET_CHISEL_STRENGTH:
448       cuma->curve[0].x = 0.0f;
449       cuma->curve[0].y = 0.0f;
450       cuma->curve[1].x = 0.31f;
451       cuma->curve[1].y = 0.22f;
452       cuma->curve[2].x = 0.61f;
453       cuma->curve[2].y = 0.88f;
454       cuma->curve[3].x = 1.0f;
455       cuma->curve[3].y = 1.0f;
456       break;
457     default:
458       break;
459   }
460
461   if (cuma->table) {
462     MEM_freeN(cuma->table);
463     cuma->table = NULL;
464   }
465 }
466
467 void BKE_gpencil_brush_preset_set(Main *bmain, Brush *brush, const short type)
468 {
469 #define SMOOTH_STROKE_RADIUS 40
470 #define SMOOTH_STROKE_FACTOR 0.9f
471 #define ACTIVE_SMOOTH 0.35f
472
473   CurveMapping *custom_curve = NULL;
474
475   /* Set general defaults at brush level. */
476   brush->smooth_stroke_radius = SMOOTH_STROKE_RADIUS;
477   brush->smooth_stroke_factor = SMOOTH_STROKE_FACTOR;
478
479   brush->rgb[0] = 0.498f;
480   brush->rgb[1] = 1.0f;
481   brush->rgb[2] = 0.498f;
482
483   brush->secondary_rgb[0] = 1.0f;
484   brush->secondary_rgb[1] = 1.0f;
485   brush->secondary_rgb[2] = 1.0f;
486
487   brush->curve_preset = BRUSH_CURVE_SMOOTH;
488
489   if (brush->gpencil_settings == NULL) {
490     return;
491   }
492
493   /* Set preset type. */
494   brush->gpencil_settings->preset_type = type;
495
496   /* Set vertex mix factor. */
497   brush->gpencil_settings->vertex_mode = GPPAINT_MODE_BOTH;
498   brush->gpencil_settings->vertex_factor = 1.0f;
499
500   switch (type) {
501     case GP_BRUSH_PRESET_AIRBRUSH: {
502       brush->size = 300.0f;
503       brush->gpencil_settings->flag |= GP_BRUSH_USE_PRESSURE;
504
505       brush->gpencil_settings->draw_strength = 0.4f;
506       brush->gpencil_settings->flag |= GP_BRUSH_USE_STRENGTH_PRESSURE;
507
508       brush->gpencil_settings->input_samples = 10;
509       brush->gpencil_settings->active_smooth = ACTIVE_SMOOTH;
510       brush->gpencil_settings->draw_angle = 0.0f;
511       brush->gpencil_settings->draw_angle_factor = 0.0f;
512       brush->gpencil_settings->hardeness = 0.9f;
513       copy_v2_fl(brush->gpencil_settings->aspect_ratio, 1.0f);
514
515       brush->gpencil_tool = GPAINT_TOOL_DRAW;
516       brush->gpencil_settings->icon_id = GP_BRUSH_ICON_AIRBRUSH;
517
518       /* Create and link Black Dots material to brush.
519        * This material is required because the brush uses the material to define how the stroke is
520        * drawn. */
521       Material *ma = BLI_findstring(&bmain->materials, "Dots Stroke", offsetof(ID, name) + 2);
522       if (ma == NULL) {
523         ma = BKE_gpencil_material_add(bmain, "Dots Stroke");
524         ma->gp_style->mode = GP_MATERIAL_MODE_DOT;
525       }
526       brush->gpencil_settings->material = ma;
527       /* Pin the matterial to the brush. */
528       brush->gpencil_settings->flag |= GP_BRUSH_MATERIAL_PINNED;
529
530       zero_v3(brush->secondary_rgb);
531       break;
532     }
533     case GP_BRUSH_PRESET_INK_PEN: {
534
535       brush->size = 60.0f;
536       brush->gpencil_settings->flag |= GP_BRUSH_USE_PRESSURE;
537
538       brush->gpencil_settings->draw_strength = 1.0f;
539
540       brush->gpencil_settings->input_samples = 10;
541       brush->gpencil_settings->active_smooth = ACTIVE_SMOOTH;
542       brush->gpencil_settings->draw_angle = 0.0f;
543       brush->gpencil_settings->draw_angle_factor = 0.0f;
544       brush->gpencil_settings->hardeness = 1.0f;
545       copy_v2_fl(brush->gpencil_settings->aspect_ratio, 1.0f);
546
547       brush->gpencil_settings->flag |= GP_BRUSH_GROUP_SETTINGS;
548       brush->gpencil_settings->draw_smoothfac = 0.1f;
549       brush->gpencil_settings->draw_smoothlvl = 1;
550       brush->gpencil_settings->draw_subdivide = 0;
551       brush->gpencil_settings->simplify_f = 0.002f;
552
553       brush->gpencil_settings->draw_random_press = 0.0f;
554       brush->gpencil_settings->draw_jitter = 0.0f;
555       brush->gpencil_settings->flag |= GP_BRUSH_USE_JITTER_PRESSURE;
556
557       /* Curve. */
558       custom_curve = brush->gpencil_settings->curve_sensitivity;
559       BKE_curvemapping_set_defaults(custom_curve, 0, 0.0f, 0.0f, 1.0f, 1.0f);
560       BKE_curvemapping_init(custom_curve);
561       brush_gpencil_curvemap_reset(custom_curve->cm, 3, GPCURVE_PRESET_INK);
562
563       brush->gpencil_settings->icon_id = GP_BRUSH_ICON_INK;
564       brush->gpencil_tool = GPAINT_TOOL_DRAW;
565
566       zero_v3(brush->secondary_rgb);
567       break;
568     }
569     case GP_BRUSH_PRESET_INK_PEN_ROUGH: {
570       brush->size = 60.0f;
571       brush->gpencil_settings->flag |= GP_BRUSH_USE_PRESSURE;
572
573       brush->gpencil_settings->draw_strength = 1.0f;
574
575       brush->gpencil_settings->input_samples = 10;
576       brush->gpencil_settings->active_smooth = ACTIVE_SMOOTH;
577       brush->gpencil_settings->draw_angle = 0.0f;
578       brush->gpencil_settings->draw_angle_factor = 0.0f;
579       brush->gpencil_settings->hardeness = 1.0f;
580       copy_v2_fl(brush->gpencil_settings->aspect_ratio, 1.0f);
581
582       brush->gpencil_settings->flag &= ~GP_BRUSH_GROUP_SETTINGS;
583       brush->gpencil_settings->draw_smoothfac = 0.0f;
584       brush->gpencil_settings->draw_smoothlvl = 2;
585       brush->gpencil_settings->draw_subdivide = 0;
586       brush->gpencil_settings->simplify_f = 0.000f;
587
588       brush->gpencil_settings->flag |= GP_BRUSH_GROUP_RANDOM;
589       brush->gpencil_settings->draw_random_press = 0.6f;
590       brush->gpencil_settings->draw_random_strength = 0.0f;
591       brush->gpencil_settings->draw_jitter = 0.0f;
592       brush->gpencil_settings->flag |= GP_BRUSH_USE_JITTER_PRESSURE;
593
594       /* Curve. */
595       custom_curve = brush->gpencil_settings->curve_sensitivity;
596       BKE_curvemapping_set_defaults(custom_curve, 0, 0.0f, 0.0f, 1.0f, 1.0f);
597       BKE_curvemapping_init(custom_curve);
598       brush_gpencil_curvemap_reset(custom_curve->cm, 3, GPCURVE_PRESET_INKNOISE);
599
600       brush->gpencil_settings->icon_id = GP_BRUSH_ICON_INKNOISE;
601       brush->gpencil_tool = GPAINT_TOOL_DRAW;
602
603       zero_v3(brush->secondary_rgb);
604       break;
605     }
606     case GP_BRUSH_PRESET_MARKER_BOLD: {
607       brush->size = 150.0f;
608       brush->gpencil_settings->flag &= ~GP_BRUSH_USE_PRESSURE;
609
610       brush->gpencil_settings->draw_strength = 0.3f;
611
612       brush->gpencil_settings->input_samples = 10;
613       brush->gpencil_settings->active_smooth = ACTIVE_SMOOTH;
614       brush->gpencil_settings->draw_angle = 0.0f;
615       brush->gpencil_settings->draw_angle_factor = 0.0f;
616       brush->gpencil_settings->hardeness = 1.0f;
617       copy_v2_fl(brush->gpencil_settings->aspect_ratio, 1.0f);
618
619       brush->gpencil_settings->flag |= GP_BRUSH_GROUP_SETTINGS;
620       brush->gpencil_settings->draw_smoothfac = 0.1f;
621       brush->gpencil_settings->draw_smoothlvl = 1;
622       brush->gpencil_settings->draw_subdivide = 0;
623       brush->gpencil_settings->simplify_f = 0.002f;
624
625       brush->gpencil_settings->flag &= ~GP_BRUSH_GROUP_RANDOM;
626       brush->gpencil_settings->draw_random_press = 0.0f;
627       brush->gpencil_settings->draw_random_strength = 0.0f;
628       brush->gpencil_settings->draw_jitter = 0.0f;
629       brush->gpencil_settings->flag |= GP_BRUSH_USE_JITTER_PRESSURE;
630
631       /* Curve. */
632       custom_curve = brush->gpencil_settings->curve_sensitivity;
633       BKE_curvemapping_set_defaults(custom_curve, 0, 0.0f, 0.0f, 1.0f, 1.0f);
634       BKE_curvemapping_init(custom_curve);
635       brush_gpencil_curvemap_reset(custom_curve->cm, 4, GPCURVE_PRESET_MARKER);
636
637       brush->gpencil_settings->icon_id = GP_BRUSH_ICON_MARKER;
638       brush->gpencil_tool = GPAINT_TOOL_DRAW;
639
640       zero_v3(brush->secondary_rgb);
641       break;
642     }
643     case GP_BRUSH_PRESET_MARKER_CHISEL: {
644       brush->size = 150.0f;
645       brush->gpencil_settings->flag |= GP_BRUSH_USE_PRESSURE;
646
647       brush->gpencil_settings->draw_strength = 1.0f;
648
649       brush->gpencil_settings->input_samples = 10;
650       brush->gpencil_settings->active_smooth = 0.3f;
651       brush->gpencil_settings->draw_angle = DEG2RAD(35.0f);
652       brush->gpencil_settings->draw_angle_factor = 0.5f;
653       brush->gpencil_settings->hardeness = 1.0f;
654       copy_v2_fl(brush->gpencil_settings->aspect_ratio, 1.0f);
655
656       brush->gpencil_settings->flag |= GP_BRUSH_GROUP_SETTINGS;
657       brush->gpencil_settings->draw_smoothfac = 0.0f;
658       brush->gpencil_settings->draw_smoothlvl = 1;
659       brush->gpencil_settings->draw_subdivide = 0;
660       brush->gpencil_settings->simplify_f = 0.002f;
661
662       brush->gpencil_settings->flag &= ~GP_BRUSH_GROUP_RANDOM;
663       brush->gpencil_settings->draw_random_press = 0.0f;
664       brush->gpencil_settings->draw_jitter = 0.0f;
665       brush->gpencil_settings->flag |= GP_BRUSH_USE_JITTER_PRESSURE;
666
667       /* Curve. */
668       custom_curve = brush->gpencil_settings->curve_sensitivity;
669       BKE_curvemapping_set_defaults(custom_curve, 0, 0.0f, 0.0f, 1.0f, 1.0f);
670       BKE_curvemapping_init(custom_curve);
671       brush_gpencil_curvemap_reset(custom_curve->cm, 3, GPCURVE_PRESET_CHISEL_SENSIVITY);
672
673       custom_curve = brush->gpencil_settings->curve_strength;
674       BKE_curvemapping_set_defaults(custom_curve, 0, 0.0f, 0.0f, 1.0f, 1.0f);
675       BKE_curvemapping_init(custom_curve);
676       brush_gpencil_curvemap_reset(custom_curve->cm, 4, GPCURVE_PRESET_CHISEL_STRENGTH);
677
678       brush->gpencil_settings->icon_id = GP_BRUSH_ICON_CHISEL;
679       brush->gpencil_tool = GPAINT_TOOL_DRAW;
680
681       zero_v3(brush->secondary_rgb);
682       break;
683     }
684     case GP_BRUSH_PRESET_PEN: {
685       brush->size = 25.0f;
686       brush->gpencil_settings->flag &= ~GP_BRUSH_USE_PRESSURE;
687
688       brush->gpencil_settings->draw_strength = 1.0f;
689       brush->gpencil_settings->flag &= ~GP_BRUSH_USE_STRENGTH_PRESSURE;
690
691       brush->gpencil_settings->input_samples = 10;
692       brush->gpencil_settings->active_smooth = ACTIVE_SMOOTH;
693       brush->gpencil_settings->draw_angle = 0.0f;
694       brush->gpencil_settings->draw_angle_factor = 0.0f;
695       brush->gpencil_settings->hardeness = 1.0f;
696       copy_v2_fl(brush->gpencil_settings->aspect_ratio, 1.0f);
697
698       brush->gpencil_settings->flag |= GP_BRUSH_GROUP_SETTINGS;
699       brush->gpencil_settings->draw_smoothfac = 0.0f;
700       brush->gpencil_settings->draw_smoothlvl = 1;
701       brush->gpencil_settings->draw_subdivide = 1;
702       brush->gpencil_settings->simplify_f = 0.002f;
703
704       brush->gpencil_settings->draw_random_press = 0.0f;
705       brush->gpencil_settings->draw_random_strength = 0.0f;
706       brush->gpencil_settings->draw_jitter = 0.0f;
707       brush->gpencil_settings->flag |= GP_BRUSH_USE_JITTER_PRESSURE;
708
709       brush->gpencil_settings->icon_id = GP_BRUSH_ICON_PEN;
710       brush->gpencil_tool = GPAINT_TOOL_DRAW;
711
712       zero_v3(brush->secondary_rgb);
713       break;
714     }
715     case GP_BRUSH_PRESET_PENCIL_SOFT: {
716       brush->size = 80.0f;
717       brush->gpencil_settings->flag |= GP_BRUSH_USE_PRESSURE;
718
719       brush->gpencil_settings->draw_strength = 0.4f;
720       brush->gpencil_settings->flag |= GP_BRUSH_USE_STRENGTH_PRESSURE;
721
722       brush->gpencil_settings->input_samples = 10;
723       brush->gpencil_settings->active_smooth = ACTIVE_SMOOTH;
724       brush->gpencil_settings->draw_angle = 0.0f;
725       brush->gpencil_settings->draw_angle_factor = 0.0f;
726       brush->gpencil_settings->hardeness = 0.8f;
727       copy_v2_fl(brush->gpencil_settings->aspect_ratio, 1.0f);
728
729       brush->gpencil_settings->flag |= GP_BRUSH_GROUP_SETTINGS;
730       brush->gpencil_settings->draw_smoothfac = 0.0f;
731       brush->gpencil_settings->draw_smoothlvl = 1;
732       brush->gpencil_settings->draw_subdivide = 0;
733       brush->gpencil_settings->simplify_f = 0.000f;
734
735       brush->gpencil_settings->draw_random_press = 0.0f;
736       brush->gpencil_settings->draw_random_strength = 0.0f;
737       brush->gpencil_settings->draw_jitter = 0.0f;
738       brush->gpencil_settings->flag |= GP_BRUSH_USE_JITTER_PRESSURE;
739
740       brush->gpencil_settings->icon_id = GP_BRUSH_ICON_PENCIL;
741       brush->gpencil_tool = GPAINT_TOOL_DRAW;
742
743       /* Create and link Black Dots material to brush.
744        * This material is required because the brush uses the material to define how the stroke is
745        * drawn. */
746       Material *ma = BLI_findstring(&bmain->materials, "Dots Stroke", offsetof(ID, name) + 2);
747       if (ma == NULL) {
748         ma = BKE_gpencil_material_add(bmain, "Dots Stroke");
749         ma->gp_style->mode = GP_MATERIAL_MODE_DOT;
750       }
751       brush->gpencil_settings->material = ma;
752       /* Pin the matterial to the brush. */
753       brush->gpencil_settings->flag |= GP_BRUSH_MATERIAL_PINNED;
754
755       zero_v3(brush->secondary_rgb);
756       break;
757     }
758     case GP_BRUSH_PRESET_PENCIL: {
759       brush->size = 20.0f;
760       brush->gpencil_settings->flag |= GP_BRUSH_USE_PRESSURE;
761
762       brush->gpencil_settings->draw_strength = 0.6f;
763       brush->gpencil_settings->flag |= GP_BRUSH_USE_STRENGTH_PRESSURE;
764
765       brush->gpencil_settings->input_samples = 10;
766       brush->gpencil_settings->active_smooth = ACTIVE_SMOOTH;
767       brush->gpencil_settings->draw_angle = 0.0f;
768       brush->gpencil_settings->draw_angle_factor = 0.0f;
769       brush->gpencil_settings->hardeness = 1.0f;
770       copy_v2_fl(brush->gpencil_settings->aspect_ratio, 1.0f);
771
772       brush->gpencil_settings->flag |= GP_BRUSH_GROUP_SETTINGS;
773       brush->gpencil_settings->draw_smoothfac = 0.0f;
774       brush->gpencil_settings->draw_smoothlvl = 1;
775       brush->gpencil_settings->draw_subdivide = 0;
776       brush->gpencil_settings->simplify_f = 0.002f;
777
778       brush->gpencil_settings->draw_random_press = 0.0f;
779       brush->gpencil_settings->draw_jitter = 0.0f;
780       brush->gpencil_settings->flag |= GP_BRUSH_USE_JITTER_PRESSURE;
781
782       brush->gpencil_settings->icon_id = GP_BRUSH_ICON_PENCIL;
783       brush->gpencil_tool = GPAINT_TOOL_DRAW;
784
785       zero_v3(brush->secondary_rgb);
786       break;
787     }
788     case GP_BRUSH_PRESET_FILL_AREA: {
789       brush->size = 20.0f;
790
791       brush->gpencil_settings->fill_leak = 3;
792       brush->gpencil_settings->fill_threshold = 0.1f;
793       brush->gpencil_settings->fill_simplylvl = 1;
794       brush->gpencil_settings->fill_factor = 1;
795
796       brush->gpencil_settings->draw_strength = 1.0f;
797       brush->gpencil_settings->hardeness = 1.0f;
798       copy_v2_fl(brush->gpencil_settings->aspect_ratio, 1.0f);
799       brush->gpencil_settings->draw_smoothfac = 0.1f;
800       brush->gpencil_settings->draw_smoothlvl = 1;
801       brush->gpencil_settings->draw_subdivide = 1;
802
803       brush->gpencil_settings->icon_id = GP_BRUSH_ICON_FILL;
804       brush->gpencil_tool = GPAINT_TOOL_FILL;
805       brush->gpencil_settings->vertex_mode = GPPAINT_MODE_FILL;
806
807       zero_v3(brush->secondary_rgb);
808       break;
809     }
810     case GP_BRUSH_PRESET_ERASER_SOFT: {
811       brush->size = 30.0f;
812       brush->gpencil_settings->draw_strength = 0.5f;
813       brush->gpencil_settings->flag |= GP_BRUSH_DEFAULT_ERASER;
814       brush->gpencil_settings->flag |= GP_BRUSH_USE_PRESSURE;
815       brush->gpencil_settings->flag |= GP_BRUSH_USE_STRENGTH_PRESSURE;
816       brush->gpencil_settings->icon_id = GP_BRUSH_ICON_ERASE_SOFT;
817       brush->gpencil_tool = GPAINT_TOOL_ERASE;
818       brush->gpencil_settings->eraser_mode = GP_BRUSH_ERASER_SOFT;
819       brush->gpencil_settings->era_strength_f = 100.0f;
820       brush->gpencil_settings->era_thickness_f = 10.0f;
821
822       break;
823     }
824     case GP_BRUSH_PRESET_ERASER_HARD: {
825       brush->size = 30.0f;
826       brush->gpencil_settings->draw_strength = 1.0f;
827       brush->gpencil_settings->eraser_mode = GP_BRUSH_ERASER_SOFT;
828       brush->gpencil_settings->era_strength_f = 100.0f;
829       brush->gpencil_settings->era_thickness_f = 50.0f;
830
831       brush->gpencil_settings->icon_id = GP_BRUSH_ICON_ERASE_HARD;
832       brush->gpencil_tool = GPAINT_TOOL_ERASE;
833
834       break;
835     }
836     case GP_BRUSH_PRESET_ERASER_POINT: {
837       brush->size = 30.0f;
838       brush->gpencil_settings->eraser_mode = GP_BRUSH_ERASER_HARD;
839
840       brush->gpencil_settings->icon_id = GP_BRUSH_ICON_ERASE_HARD;
841       brush->gpencil_tool = GPAINT_TOOL_ERASE;
842
843       break;
844     }
845     case GP_BRUSH_PRESET_ERASER_STROKE: {
846       brush->size = 30.0f;
847       brush->gpencil_settings->eraser_mode = GP_BRUSH_ERASER_STROKE;
848
849       brush->gpencil_settings->icon_id = GP_BRUSH_ICON_ERASE_STROKE;
850       brush->gpencil_tool = GPAINT_TOOL_ERASE;
851
852       break;
853     }
854     case GP_BRUSH_PRESET_TINT: {
855       brush->gpencil_settings->icon_id = GP_BRUSH_ICON_TINT;
856       brush->gpencil_tool = GPAINT_TOOL_TINT;
857
858       brush->size = 25.0f;
859       brush->gpencil_settings->flag |= GP_BRUSH_USE_PRESSURE;
860
861       brush->gpencil_settings->draw_strength = 0.8f;
862       brush->gpencil_settings->flag |= GP_BRUSH_USE_STRENGTH_PRESSURE;
863
864       zero_v3(brush->secondary_rgb);
865       break;
866     }
867     case GP_BRUSH_PRESET_VERTEX_DRAW: {
868       brush->gpencil_settings->icon_id = GP_BRUSH_ICON_VERTEX_DRAW;
869       brush->gpencil_vertex_tool = GPVERTEX_TOOL_DRAW;
870
871       brush->size = 25.0f;
872       brush->gpencil_settings->flag |= GP_BRUSH_USE_PRESSURE;
873
874       brush->gpencil_settings->draw_strength = 0.8f;
875       brush->gpencil_settings->flag |= GP_BRUSH_USE_STRENGTH_PRESSURE;
876
877       zero_v3(brush->secondary_rgb);
878       break;
879     }
880     case GP_BRUSH_PRESET_VERTEX_BLUR: {
881       brush->gpencil_settings->icon_id = GP_BRUSH_ICON_VERTEX_BLUR;
882       brush->gpencil_vertex_tool = GPVERTEX_TOOL_BLUR;
883
884       brush->size = 25.0f;
885       brush->gpencil_settings->flag |= GP_BRUSH_USE_PRESSURE;
886
887       brush->gpencil_settings->draw_strength = 0.8f;
888       brush->gpencil_settings->flag |= GP_BRUSH_USE_STRENGTH_PRESSURE;
889
890       zero_v3(brush->secondary_rgb);
891       break;
892     }
893     case GP_BRUSH_PRESET_VERTEX_AVERAGE: {
894       brush->gpencil_settings->icon_id = GP_BRUSH_ICON_VERTEX_AVERAGE;
895       brush->gpencil_vertex_tool = GPVERTEX_TOOL_AVERAGE;
896
897       brush->size = 25.0f;
898       brush->gpencil_settings->flag |= GP_BRUSH_USE_PRESSURE;
899
900       brush->gpencil_settings->draw_strength = 0.8f;
901       brush->gpencil_settings->flag |= GP_BRUSH_USE_STRENGTH_PRESSURE;
902
903       zero_v3(brush->secondary_rgb);
904       break;
905     }
906     case GP_BRUSH_PRESET_VERTEX_SMEAR: {
907       brush->gpencil_settings->icon_id = GP_BRUSH_ICON_VERTEX_SMEAR;
908       brush->gpencil_vertex_tool = GPVERTEX_TOOL_SMEAR;
909
910       brush->size = 25.0f;
911       brush->gpencil_settings->flag |= GP_BRUSH_USE_PRESSURE;
912
913       brush->gpencil_settings->draw_strength = 0.8f;
914       brush->gpencil_settings->flag |= GP_BRUSH_USE_STRENGTH_PRESSURE;
915
916       zero_v3(brush->secondary_rgb);
917       break;
918     }
919     case GP_BRUSH_PRESET_VERTEX_REPLACE: {
920       brush->gpencil_settings->icon_id = GP_BRUSH_ICON_VERTEX_REPLACE;
921       brush->gpencil_vertex_tool = GPVERTEX_TOOL_REPLACE;
922
923       brush->size = 25.0f;
924       brush->gpencil_settings->flag |= GP_BRUSH_USE_PRESSURE;
925
926       brush->gpencil_settings->draw_strength = 0.8f;
927       brush->gpencil_settings->flag |= GP_BRUSH_USE_STRENGTH_PRESSURE;
928
929       zero_v3(brush->secondary_rgb);
930       break;
931     }
932     case GP_BRUSH_PRESET_SMOOTH_STROKE: {
933       brush->gpencil_settings->icon_id = GP_BRUSH_ICON_GPBRUSH_SMOOTH;
934       brush->gpencil_sculpt_tool = GPSCULPT_TOOL_SMOOTH;
935
936       brush->size = 25.0f;
937       brush->gpencil_settings->flag |= GP_BRUSH_USE_PRESSURE;
938
939       brush->gpencil_settings->draw_strength = 0.3f;
940       brush->gpencil_settings->flag |= GP_BRUSH_USE_STRENGTH_PRESSURE;
941       brush->gpencil_settings->sculpt_flag = GP_SCULPT_FLAG_SMOOTH_PRESSURE;
942       brush->gpencil_settings->sculpt_mode_flag |= GP_SCULPT_FLAGMODE_APPLY_POSITION;
943
944       break;
945     }
946     case GP_BRUSH_PRESET_STRENGTH_STROKE: {
947       brush->gpencil_settings->icon_id = GP_BRUSH_ICON_GPBRUSH_STRENGTH;
948       brush->gpencil_sculpt_tool = GPSCULPT_TOOL_STRENGTH;
949
950       brush->size = 25.0f;
951       brush->gpencil_settings->flag |= GP_BRUSH_USE_PRESSURE;
952
953       brush->gpencil_settings->draw_strength = 0.3f;
954       brush->gpencil_settings->flag |= GP_BRUSH_USE_STRENGTH_PRESSURE;
955       brush->gpencil_settings->sculpt_flag = GP_SCULPT_FLAG_SMOOTH_PRESSURE;
956       brush->gpencil_settings->sculpt_mode_flag |= GP_SCULPT_FLAGMODE_APPLY_POSITION;
957
958       break;
959     }
960     case GP_BRUSH_PRESET_THICKNESS_STROKE: {
961       brush->gpencil_settings->icon_id = GP_BRUSH_ICON_GPBRUSH_THICKNESS;
962       brush->gpencil_sculpt_tool = GPSCULPT_TOOL_THICKNESS;
963
964       brush->size = 25.0f;
965       brush->gpencil_settings->flag |= GP_BRUSH_USE_PRESSURE;
966
967       brush->gpencil_settings->draw_strength = 0.5f;
968       brush->gpencil_settings->flag |= GP_BRUSH_USE_STRENGTH_PRESSURE;
969       brush->gpencil_settings->sculpt_mode_flag |= GP_SCULPT_FLAGMODE_APPLY_POSITION;
970
971       break;
972     }
973     case GP_BRUSH_PRESET_GRAB_STROKE: {
974       brush->gpencil_settings->icon_id = GP_BRUSH_ICON_GPBRUSH_GRAB;
975       brush->gpencil_sculpt_tool = GPSCULPT_TOOL_GRAB;
976       brush->gpencil_settings->flag &= ~GP_BRUSH_USE_PRESSURE;
977
978       brush->size = 25.0f;
979
980       brush->gpencil_settings->draw_strength = 0.3f;
981       brush->gpencil_settings->flag |= GP_BRUSH_USE_STRENGTH_PRESSURE;
982       brush->gpencil_settings->sculpt_mode_flag |= GP_SCULPT_FLAGMODE_APPLY_POSITION;
983
984       break;
985     }
986     case GP_BRUSH_PRESET_PUSH_STROKE: {
987       brush->gpencil_settings->icon_id = GP_BRUSH_ICON_GPBRUSH_PUSH;
988       brush->gpencil_sculpt_tool = GPSCULPT_TOOL_PUSH;
989
990       brush->size = 25.0f;
991       brush->gpencil_settings->flag |= GP_BRUSH_USE_PRESSURE;
992
993       brush->gpencil_settings->draw_strength = 0.3f;
994       brush->gpencil_settings->flag |= GP_BRUSH_USE_STRENGTH_PRESSURE;
995       brush->gpencil_settings->sculpt_mode_flag |= GP_SCULPT_FLAGMODE_APPLY_POSITION;
996
997       break;
998     }
999     case GP_BRUSH_PRESET_TWIST_STROKE: {
1000       brush->gpencil_settings->icon_id = GP_BRUSH_ICON_GPBRUSH_TWIST;
1001       brush->gpencil_sculpt_tool = GPSCULPT_TOOL_TWIST;
1002
1003       brush->size = 50.0f;
1004       brush->gpencil_settings->flag |= GP_BRUSH_USE_PRESSURE;
1005
1006       brush->gpencil_settings->draw_strength = 0.3f;
1007       brush->gpencil_settings->flag |= GP_BRUSH_USE_STRENGTH_PRESSURE;
1008       brush->gpencil_settings->sculpt_mode_flag |= GP_SCULPT_FLAGMODE_APPLY_POSITION;
1009
1010       break;
1011     }
1012     case GP_BRUSH_PRESET_PINCH_STROKE: {
1013       brush->gpencil_settings->icon_id = GP_BRUSH_ICON_GPBRUSH_PINCH;
1014       brush->gpencil_sculpt_tool = GPSCULPT_TOOL_PINCH;
1015
1016       brush->size = 50.0f;
1017       brush->gpencil_settings->flag |= GP_BRUSH_USE_PRESSURE;
1018
1019       brush->gpencil_settings->draw_strength = 0.5f;
1020       brush->gpencil_settings->flag |= GP_BRUSH_USE_STRENGTH_PRESSURE;
1021       brush->gpencil_settings->sculpt_mode_flag |= GP_SCULPT_FLAGMODE_APPLY_POSITION;
1022
1023       break;
1024     }
1025     case GP_BRUSH_PRESET_RANDOMIZE_STROKE: {
1026       brush->gpencil_settings->icon_id = GP_BRUSH_ICON_GPBRUSH_RANDOMIZE;
1027       brush->gpencil_sculpt_tool = GPSCULPT_TOOL_RANDOMIZE;
1028
1029       brush->size = 25.0f;
1030       brush->gpencil_settings->flag |= GP_BRUSH_USE_PRESSURE;
1031
1032       brush->gpencil_settings->draw_strength = 0.5f;
1033       brush->gpencil_settings->flag |= GP_BRUSH_USE_STRENGTH_PRESSURE;
1034       brush->gpencil_settings->sculpt_mode_flag |= GP_SCULPT_FLAGMODE_APPLY_POSITION;
1035
1036       break;
1037     }
1038     case GP_BRUSH_PRESET_CLONE_STROKE: {
1039       brush->gpencil_settings->icon_id = GP_BRUSH_ICON_GPBRUSH_CLONE;
1040       brush->gpencil_sculpt_tool = GPSCULPT_TOOL_CLONE;
1041       brush->gpencil_settings->flag &= ~GP_BRUSH_USE_PRESSURE;
1042
1043       brush->size = 25.0f;
1044
1045       brush->gpencil_settings->draw_strength = 1.0f;
1046       brush->gpencil_settings->flag |= GP_BRUSH_USE_STRENGTH_PRESSURE;
1047       brush->gpencil_settings->sculpt_mode_flag |= GP_SCULPT_FLAGMODE_APPLY_POSITION;
1048
1049       break;
1050     }
1051     case GP_BRUSH_PRESET_DRAW_WEIGHT: {
1052       brush->gpencil_settings->icon_id = GP_BRUSH_ICON_GPBRUSH_WEIGHT;
1053       brush->gpencil_weight_tool = GPWEIGHT_TOOL_DRAW;
1054
1055       brush->size = 25.0f;
1056       brush->gpencil_settings->flag |= GP_BRUSH_USE_PRESSURE;
1057
1058       brush->gpencil_settings->draw_strength = 0.8f;
1059       brush->gpencil_settings->flag |= GP_BRUSH_USE_STRENGTH_PRESSURE;
1060       brush->gpencil_settings->sculpt_mode_flag |= GP_SCULPT_FLAGMODE_APPLY_POSITION;
1061
1062       break;
1063     }
1064     default:
1065       break;
1066   }
1067 }
1068
1069 static Brush *gpencil_brush_ensure(
1070     Main *bmain, ToolSettings *ts, const char *brush_name, eObjectMode mode, bool *r_new)
1071 {
1072   *r_new = false;
1073   Brush *brush = BLI_findstring(&bmain->brushes, brush_name, offsetof(ID, name) + 2);
1074
1075   /* If the brush exist, but the type is not GPencil or the mode is wrong, create a new one. */
1076   if ((brush != NULL) && ((brush->gpencil_settings == NULL) || (brush->ob_mode != mode))) {
1077     brush = NULL;
1078   }
1079
1080   if (brush == NULL) {
1081     brush = BKE_brush_add_gpencil(bmain, ts, brush_name, mode);
1082     *r_new = true;
1083   }
1084
1085   if (brush->gpencil_settings == NULL) {
1086     BKE_brush_init_gpencil_settings(brush);
1087   }
1088
1089   return brush;
1090 }
1091
1092 /* Create a set of grease pencil Drawing presets. */
1093 void BKE_brush_gpencil_paint_presets(Main *bmain, ToolSettings *ts, const bool reset)
1094 {
1095   bool r_new = false;
1096
1097   Paint *paint = &ts->gp_paint->paint;
1098   Brush *brush_prev = paint->brush;
1099   Brush *brush, *deft_draw;
1100   /* Airbrush brush. */
1101   brush = gpencil_brush_ensure(bmain, ts, "Airbrush", OB_MODE_PAINT_GPENCIL, &r_new);
1102   if ((reset) || (r_new)) {
1103     BKE_gpencil_brush_preset_set(bmain, brush, GP_BRUSH_PRESET_AIRBRUSH);
1104   }
1105
1106   /* Ink Pen brush. */
1107   brush = gpencil_brush_ensure(bmain, ts, "Ink Pen", OB_MODE_PAINT_GPENCIL, &r_new);
1108   if ((reset) || (r_new)) {
1109     BKE_gpencil_brush_preset_set(bmain, brush, GP_BRUSH_PRESET_INK_PEN);
1110   }
1111
1112   /* Ink Pen Rough brush. */
1113   brush = gpencil_brush_ensure(bmain, ts, "Ink Pen Rough", OB_MODE_PAINT_GPENCIL, &r_new);
1114   if ((reset) || (r_new)) {
1115     BKE_gpencil_brush_preset_set(bmain, brush, GP_BRUSH_PRESET_INK_PEN_ROUGH);
1116   }
1117
1118   /* Marker Bold brush. */
1119   brush = gpencil_brush_ensure(bmain, ts, "Marker Bold", OB_MODE_PAINT_GPENCIL, &r_new);
1120   if ((reset) || (r_new)) {
1121     BKE_gpencil_brush_preset_set(bmain, brush, GP_BRUSH_PRESET_MARKER_BOLD);
1122   }
1123
1124   /* Marker Chisel brush. */
1125   brush = gpencil_brush_ensure(bmain, ts, "Marker Chisel", OB_MODE_PAINT_GPENCIL, &r_new);
1126   if ((reset) || (r_new)) {
1127     BKE_gpencil_brush_preset_set(bmain, brush, GP_BRUSH_PRESET_MARKER_CHISEL);
1128   }
1129
1130   /* Pen brush. */
1131   brush = gpencil_brush_ensure(bmain, ts, "Pen", OB_MODE_PAINT_GPENCIL, &r_new);
1132   if ((reset) || (r_new)) {
1133     BKE_gpencil_brush_preset_set(bmain, brush, GP_BRUSH_PRESET_PEN);
1134   }
1135
1136   /* Pencil Soft brush. */
1137   brush = gpencil_brush_ensure(bmain, ts, "Pencil Soft", OB_MODE_PAINT_GPENCIL, &r_new);
1138   if ((reset) || (r_new)) {
1139     BKE_gpencil_brush_preset_set(bmain, brush, GP_BRUSH_PRESET_PENCIL_SOFT);
1140   }
1141
1142   /* Pencil brush. */
1143   brush = gpencil_brush_ensure(bmain, ts, "Pencil", OB_MODE_PAINT_GPENCIL, &r_new);
1144   if ((reset) || (r_new)) {
1145     BKE_gpencil_brush_preset_set(bmain, brush, GP_BRUSH_PRESET_PENCIL);
1146   }
1147   deft_draw = brush; /* save default brush. */
1148
1149   /* Fill brush. */
1150   brush = gpencil_brush_ensure(bmain, ts, "Fill Area", OB_MODE_PAINT_GPENCIL, &r_new);
1151   if ((reset) || (r_new)) {
1152     BKE_gpencil_brush_preset_set(bmain, brush, GP_BRUSH_PRESET_FILL_AREA);
1153   }
1154
1155   /* Soft Eraser brush. */
1156   brush = gpencil_brush_ensure(bmain, ts, "Eraser Soft", OB_MODE_PAINT_GPENCIL, &r_new);
1157   if ((reset) || (r_new)) {
1158     BKE_gpencil_brush_preset_set(bmain, brush, GP_BRUSH_PRESET_ERASER_SOFT);
1159   }
1160
1161   /* Hard Eraser brush. */
1162   brush = gpencil_brush_ensure(bmain, ts, "Eraser Hard", OB_MODE_PAINT_GPENCIL, &r_new);
1163   if ((reset) || (r_new)) {
1164     BKE_gpencil_brush_preset_set(bmain, brush, GP_BRUSH_PRESET_ERASER_HARD);
1165   }
1166
1167   /* Point Eraser brush. */
1168   brush = gpencil_brush_ensure(bmain, ts, "Eraser Point", OB_MODE_PAINT_GPENCIL, &r_new);
1169   if ((reset) || (r_new)) {
1170     BKE_gpencil_brush_preset_set(bmain, brush, GP_BRUSH_PRESET_ERASER_POINT);
1171   }
1172
1173   /* Stroke Eraser brush. */
1174   brush = gpencil_brush_ensure(bmain, ts, "Eraser Stroke", OB_MODE_PAINT_GPENCIL, &r_new);
1175   if ((reset) || (r_new)) {
1176     BKE_gpencil_brush_preset_set(bmain, brush, GP_BRUSH_PRESET_ERASER_STROKE);
1177   }
1178
1179   /* Tint brush. */
1180   brush = gpencil_brush_ensure(bmain, ts, "Tint", OB_MODE_PAINT_GPENCIL, &r_new);
1181   if ((reset) || (r_new)) {
1182     BKE_gpencil_brush_preset_set(bmain, brush, GP_BRUSH_PRESET_TINT);
1183   }
1184
1185   /* Set default Draw brush. */
1186   if (reset || brush_prev == NULL) {
1187     BKE_paint_brush_set(paint, deft_draw);
1188   }
1189   else {
1190     if (brush_prev != NULL) {
1191       BKE_paint_brush_set(paint, brush_prev);
1192     }
1193   }
1194 }
1195
1196 /* Create a set of grease pencil Vertex Paint presets. */
1197 void BKE_brush_gpencil_vertex_presets(Main *bmain, ToolSettings *ts, const bool reset)
1198 {
1199   bool r_new = false;
1200
1201   Paint *vertexpaint = &ts->gp_vertexpaint->paint;
1202   Brush *brush_prev = vertexpaint->brush;
1203   Brush *brush, *deft_vertex;
1204   /* Vertex Draw brush. */
1205   brush = gpencil_brush_ensure(bmain, ts, "Vertex Draw", OB_MODE_VERTEX_GPENCIL, &r_new);
1206   if ((reset) || (r_new)) {
1207     BKE_gpencil_brush_preset_set(bmain, brush, GP_BRUSH_PRESET_VERTEX_DRAW);
1208   }
1209   deft_vertex = brush; /* save default brush. */
1210
1211   /* Vertex Blur brush. */
1212   brush = gpencil_brush_ensure(bmain, ts, "Vertex Blur", OB_MODE_VERTEX_GPENCIL, &r_new);
1213   if ((reset) || (r_new)) {
1214     BKE_gpencil_brush_preset_set(bmain, brush, GP_BRUSH_PRESET_VERTEX_BLUR);
1215   }
1216   /* Vertex Average brush. */
1217   brush = gpencil_brush_ensure(bmain, ts, "Vertex Average", OB_MODE_VERTEX_GPENCIL, &r_new);
1218   if ((reset) || (r_new)) {
1219     BKE_gpencil_brush_preset_set(bmain, brush, GP_BRUSH_PRESET_VERTEX_AVERAGE);
1220   }
1221   /* Vertex Smear brush. */
1222   brush = gpencil_brush_ensure(bmain, ts, "Vertex Smear", OB_MODE_VERTEX_GPENCIL, &r_new);
1223   if ((reset) || (r_new)) {
1224     BKE_gpencil_brush_preset_set(bmain, brush, GP_BRUSH_PRESET_VERTEX_SMEAR);
1225   }
1226   /* Vertex Replace brush. */
1227   brush = gpencil_brush_ensure(bmain, ts, "Vertex Replace", OB_MODE_VERTEX_GPENCIL, &r_new);
1228   if ((reset) || (r_new)) {
1229     BKE_gpencil_brush_preset_set(bmain, brush, GP_BRUSH_PRESET_VERTEX_REPLACE);
1230   }
1231
1232   /* Set default Vertex brush. */
1233   if (reset || brush_prev == NULL) {
1234     BKE_paint_brush_set(vertexpaint, deft_vertex);
1235   }
1236   else {
1237     if (brush_prev != NULL) {
1238       BKE_paint_brush_set(vertexpaint, brush_prev);
1239     }
1240   }
1241 }
1242
1243 /* Create a set of grease pencil Sculpt Paint presets. */
1244 void BKE_brush_gpencil_sculpt_presets(Main *bmain, ToolSettings *ts, const bool reset)
1245 {
1246   bool r_new = false;
1247
1248   Paint *sculptpaint = &ts->gp_sculptpaint->paint;
1249   Brush *brush_prev = sculptpaint->brush;
1250   Brush *brush, *deft_sculpt;
1251
1252   /* Smooth brush. */
1253   brush = gpencil_brush_ensure(bmain, ts, "Smooth Stroke", OB_MODE_SCULPT_GPENCIL, &r_new);
1254   if ((reset) || (r_new)) {
1255     BKE_gpencil_brush_preset_set(bmain, brush, GP_BRUSH_PRESET_SMOOTH_STROKE);
1256   }
1257   deft_sculpt = brush;
1258
1259   /* Strength brush. */
1260   brush = gpencil_brush_ensure(bmain, ts, "Strength Stroke", OB_MODE_SCULPT_GPENCIL, &r_new);
1261   if ((reset) || (r_new)) {
1262     BKE_gpencil_brush_preset_set(bmain, brush, GP_BRUSH_PRESET_STRENGTH_STROKE);
1263   }
1264
1265   /* Thickness brush. */
1266   brush = gpencil_brush_ensure(bmain, ts, "Thickness Stroke", OB_MODE_SCULPT_GPENCIL, &r_new);
1267   if ((reset) || (r_new)) {
1268     BKE_gpencil_brush_preset_set(bmain, brush, GP_BRUSH_PRESET_THICKNESS_STROKE);
1269   }
1270
1271   /* Grab brush. */
1272   brush = gpencil_brush_ensure(bmain, ts, "Grab Stroke", OB_MODE_SCULPT_GPENCIL, &r_new);
1273   if ((reset) || (r_new)) {
1274     BKE_gpencil_brush_preset_set(bmain, brush, GP_BRUSH_PRESET_GRAB_STROKE);
1275   }
1276
1277   /* Push brush. */
1278   brush = gpencil_brush_ensure(bmain, ts, "Push Stroke", OB_MODE_SCULPT_GPENCIL, &r_new);
1279   if ((reset) || (r_new)) {
1280     BKE_gpencil_brush_preset_set(bmain, brush, GP_BRUSH_PRESET_PUSH_STROKE);
1281   }
1282
1283   /* Twist brush. */
1284   brush = gpencil_brush_ensure(bmain, ts, "Twist Stroke", OB_MODE_SCULPT_GPENCIL, &r_new);
1285   if ((reset) || (r_new)) {
1286     BKE_gpencil_brush_preset_set(bmain, brush, GP_BRUSH_PRESET_TWIST_STROKE);
1287   }
1288
1289   /* Pinch brush. */
1290   brush = gpencil_brush_ensure(bmain, ts, "Pinch Stroke", OB_MODE_SCULPT_GPENCIL, &r_new);
1291   if ((reset) || (r_new)) {
1292     BKE_gpencil_brush_preset_set(bmain, brush, GP_BRUSH_PRESET_PINCH_STROKE);
1293   }
1294
1295   /* Randomize brush. */
1296   brush = gpencil_brush_ensure(bmain, ts, "Randomize Stroke", OB_MODE_SCULPT_GPENCIL, &r_new);
1297   if ((reset) || (r_new)) {
1298     BKE_gpencil_brush_preset_set(bmain, brush, GP_BRUSH_PRESET_RANDOMIZE_STROKE);
1299   }
1300
1301   /* Clone brush. */
1302   brush = gpencil_brush_ensure(bmain, ts, "Clone Stroke", OB_MODE_SCULPT_GPENCIL, &r_new);
1303   if ((reset) || (r_new)) {
1304     BKE_gpencil_brush_preset_set(bmain, brush, GP_BRUSH_PRESET_CLONE_STROKE);
1305   }
1306
1307   /* Set default brush. */
1308   if (reset || brush_prev == NULL) {
1309     BKE_paint_brush_set(sculptpaint, deft_sculpt);
1310   }
1311   else {
1312     if (brush_prev != NULL) {
1313       BKE_paint_brush_set(sculptpaint, brush_prev);
1314     }
1315   }
1316 }
1317
1318 /* Create a set of grease pencil Weight Paint presets. */
1319 void BKE_brush_gpencil_weight_presets(Main *bmain, ToolSettings *ts, const bool reset)
1320 {
1321   bool r_new = false;
1322
1323   Paint *weightpaint = &ts->gp_weightpaint->paint;
1324   Brush *brush_prev = weightpaint->brush;
1325   Brush *brush, *deft_weight;
1326   /* Vertex Draw brush. */
1327   brush = gpencil_brush_ensure(bmain, ts, "Draw Weight", OB_MODE_WEIGHT_GPENCIL, &r_new);
1328   if ((reset) || (r_new)) {
1329     BKE_gpencil_brush_preset_set(bmain, brush, GP_BRUSH_PRESET_DRAW_WEIGHT);
1330   }
1331   deft_weight = brush; /* save default brush. */
1332
1333   /* Set default brush. */
1334   if (reset || brush_prev == NULL) {
1335     BKE_paint_brush_set(weightpaint, deft_weight);
1336   }
1337   else {
1338     if (brush_prev != NULL) {
1339       BKE_paint_brush_set(weightpaint, brush_prev);
1340     }
1341   }
1342 }
1343
1344 struct Brush *BKE_brush_first_search(struct Main *bmain, const eObjectMode ob_mode)
1345 {
1346   Brush *brush;
1347
1348   for (brush = bmain->brushes.first; brush; brush = brush->id.next) {
1349     if (brush->ob_mode & ob_mode) {
1350       return brush;
1351     }
1352   }
1353   return NULL;
1354 }
1355
1356 Brush *BKE_brush_copy(Main *bmain, const Brush *brush)
1357 {
1358   Brush *brush_copy;
1359   BKE_id_copy(bmain, &brush->id, (ID **)&brush_copy);
1360   return brush_copy;
1361 }
1362
1363 void BKE_brush_debug_print_state(Brush *br)
1364 {
1365   /* create a fake brush and set it to the defaults */
1366   Brush def = {{NULL}};
1367   brush_defaults(&def);
1368
1369 #define BR_TEST(field, t) \
1370   if (br->field != def.field) \
1371   printf("br->" #field " = %" #t ";\n", br->field)
1372
1373 #define BR_TEST_FLAG(_f) \
1374   if ((br->flag & _f) && !(def.flag & _f)) \
1375     printf("br->flag |= " #_f ";\n"); \
1376   else if (!(br->flag & _f) && (def.flag & _f)) \
1377   printf("br->flag &= ~" #_f ";\n")
1378
1379 #define BR_TEST_FLAG_OVERLAY(_f) \
1380   if ((br->overlay_flags & _f) && !(def.overlay_flags & _f)) \
1381     printf("br->overlay_flags |= " #_f ";\n"); \
1382   else if (!(br->overlay_flags & _f) && (def.overlay_flags & _f)) \
1383   printf("br->overlay_flags &= ~" #_f ";\n")
1384
1385   /* print out any non-default brush state */
1386   BR_TEST(normal_weight, f);
1387
1388   BR_TEST(blend, d);
1389   BR_TEST(size, d);
1390
1391   /* br->flag */
1392   BR_TEST_FLAG(BRUSH_AIRBRUSH);
1393   BR_TEST_FLAG(BRUSH_ALPHA_PRESSURE);
1394   BR_TEST_FLAG(BRUSH_SIZE_PRESSURE);
1395   BR_TEST_FLAG(BRUSH_JITTER_PRESSURE);
1396   BR_TEST_FLAG(BRUSH_SPACING_PRESSURE);
1397   BR_TEST_FLAG(BRUSH_ANCHORED);
1398   BR_TEST_FLAG(BRUSH_DIR_IN);
1399   BR_TEST_FLAG(BRUSH_SPACE);
1400   BR_TEST_FLAG(BRUSH_SMOOTH_STROKE);
1401   BR_TEST_FLAG(BRUSH_PERSISTENT);
1402   BR_TEST_FLAG(BRUSH_ACCUMULATE);
1403   BR_TEST_FLAG(BRUSH_LOCK_ALPHA);
1404   BR_TEST_FLAG(BRUSH_ORIGINAL_NORMAL);
1405   BR_TEST_FLAG(BRUSH_OFFSET_PRESSURE);
1406   BR_TEST_FLAG(BRUSH_SPACE_ATTEN);
1407   BR_TEST_FLAG(BRUSH_ADAPTIVE_SPACE);
1408   BR_TEST_FLAG(BRUSH_LOCK_SIZE);
1409   BR_TEST_FLAG(BRUSH_EDGE_TO_EDGE);
1410   BR_TEST_FLAG(BRUSH_DRAG_DOT);
1411   BR_TEST_FLAG(BRUSH_INVERSE_SMOOTH_PRESSURE);
1412   BR_TEST_FLAG(BRUSH_PLANE_TRIM);
1413   BR_TEST_FLAG(BRUSH_FRONTFACE);
1414   BR_TEST_FLAG(BRUSH_CUSTOM_ICON);
1415
1416   BR_TEST_FLAG_OVERLAY(BRUSH_OVERLAY_CURSOR);
1417   BR_TEST_FLAG_OVERLAY(BRUSH_OVERLAY_PRIMARY);
1418   BR_TEST_FLAG_OVERLAY(BRUSH_OVERLAY_SECONDARY);
1419   BR_TEST_FLAG_OVERLAY(BRUSH_OVERLAY_CURSOR_OVERRIDE_ON_STROKE);
1420   BR_TEST_FLAG_OVERLAY(BRUSH_OVERLAY_PRIMARY_OVERRIDE_ON_STROKE);
1421   BR_TEST_FLAG_OVERLAY(BRUSH_OVERLAY_SECONDARY_OVERRIDE_ON_STROKE);
1422
1423   BR_TEST(jitter, f);
1424   BR_TEST(spacing, d);
1425   BR_TEST(smooth_stroke_radius, d);
1426   BR_TEST(smooth_stroke_factor, f);
1427   BR_TEST(rate, f);
1428
1429   BR_TEST(alpha, f);
1430
1431   BR_TEST(sculpt_plane, d);
1432
1433   BR_TEST(plane_offset, f);
1434
1435   BR_TEST(autosmooth_factor, f);
1436
1437   BR_TEST(topology_rake_factor, f);
1438
1439   BR_TEST(crease_pinch_factor, f);
1440
1441   BR_TEST(plane_trim, f);
1442
1443   BR_TEST(texture_sample_bias, f);
1444   BR_TEST(texture_overlay_alpha, d);
1445
1446   BR_TEST(add_col[0], f);
1447   BR_TEST(add_col[1], f);
1448   BR_TEST(add_col[2], f);
1449   BR_TEST(add_col[3], f);
1450   BR_TEST(sub_col[0], f);
1451   BR_TEST(sub_col[1], f);
1452   BR_TEST(sub_col[2], f);
1453   BR_TEST(sub_col[3], f);
1454
1455   printf("\n");
1456
1457 #undef BR_TEST
1458 #undef BR_TEST_FLAG
1459 }
1460
1461 void BKE_brush_sculpt_reset(Brush *br)
1462 {
1463   /* enable this to see any non-default
1464    * settings used by a brush: */
1465   // BKE_brush_debug_print_state(br);
1466
1467   brush_defaults(br);
1468   BKE_brush_curve_preset(br, CURVE_PRESET_SMOOTH);
1469
1470   /* Use the curve presets by default */
1471   br->curve_preset = BRUSH_CURVE_SMOOTH;
1472
1473   /* Note that sculpt defaults where set when 0.5 was the default (now it's 1.0)
1474    * assign this so logic below can remain the same. */
1475   br->alpha = 0.5f;
1476
1477   /* Brush settings */
1478   switch (br->sculpt_tool) {
1479     case SCULPT_TOOL_DRAW_SHARP:
1480       br->flag |= BRUSH_DIR_IN;
1481       br->curve_preset = BRUSH_CURVE_POW4;
1482       br->spacing = 5;
1483       break;
1484     case SCULPT_TOOL_SLIDE_RELAX:
1485       br->spacing = 10;
1486       br->alpha = 1.0f;
1487       br->slide_deform_type = BRUSH_SLIDE_DEFORM_DRAG;
1488       break;
1489     case SCULPT_TOOL_CLAY:
1490       br->flag |= BRUSH_SIZE_PRESSURE;
1491       br->spacing = 3;
1492       br->autosmooth_factor = 0.25f;
1493       br->normal_radius_factor = 0.75f;
1494       br->hardness = 0.65f;
1495       break;
1496     case SCULPT_TOOL_CLAY_THUMB:
1497       br->alpha = 0.5f;
1498       br->normal_radius_factor = 1.0f;
1499       br->spacing = 6;
1500       br->hardness = 0.5f;
1501       br->flag |= BRUSH_SIZE_PRESSURE;
1502       br->flag &= ~BRUSH_SPACE_ATTEN;
1503       break;
1504     case SCULPT_TOOL_CLAY_STRIPS:
1505       br->flag |= BRUSH_ACCUMULATE | BRUSH_SIZE_PRESSURE;
1506       br->flag &= ~BRUSH_SPACE_ATTEN;
1507       br->alpha = 0.6f;
1508       br->spacing = 5;
1509       br->normal_radius_factor = 1.55f;
1510       br->tip_roundness = 0.18f;
1511       br->curve_preset = BRUSH_CURVE_SMOOTHER;
1512       break;
1513     case SCULPT_TOOL_MULTIPLANE_SCRAPE:
1514       br->flag2 |= BRUSH_MULTIPLANE_SCRAPE_DYNAMIC | BRUSH_MULTIPLANE_SCRAPE_PLANES_PREVIEW;
1515       br->alpha = 0.7f;
1516       br->normal_radius_factor = 0.70f;
1517       br->multiplane_scrape_angle = 60;
1518       br->curve_preset = BRUSH_CURVE_SMOOTH;
1519       br->spacing = 5;
1520       break;
1521     case SCULPT_TOOL_CREASE:
1522       br->flag |= BRUSH_DIR_IN;
1523       br->alpha = 0.25;
1524       break;
1525     case SCULPT_TOOL_SCRAPE:
1526     case SCULPT_TOOL_FILL:
1527       br->alpha = 1.0f;
1528       br->spacing = 7;
1529       br->flag |= BRUSH_ACCUMULATE;
1530       br->flag |= BRUSH_INVERT_TO_SCRAPE_FILL;
1531       break;
1532     case SCULPT_TOOL_ROTATE:
1533       br->alpha = 1.0;
1534       break;
1535     case SCULPT_TOOL_SMOOTH:
1536       br->flag &= ~BRUSH_SPACE_ATTEN;
1537       br->automasking_flags |= BRUSH_AUTOMASKING_BOUNDARY_EDGES;
1538       br->spacing = 5;
1539       br->alpha = 0.7f;
1540       br->surface_smooth_shape_preservation = 0.5f;
1541       br->surface_smooth_current_vertex = 0.5f;
1542       br->surface_smooth_iterations = 4;
1543       break;
1544     case SCULPT_TOOL_SNAKE_HOOK:
1545       br->alpha = 1.0f;
1546       br->rake_factor = 1.0f;
1547       break;
1548     case SCULPT_TOOL_THUMB:
1549       br->size = 75;
1550       br->flag &= ~BRUSH_ALPHA_PRESSURE;
1551       br->flag &= ~BRUSH_SPACE;
1552       br->flag &= ~BRUSH_SPACE_ATTEN;
1553       break;
1554     case SCULPT_TOOL_ELASTIC_DEFORM:
1555       br->elastic_deform_volume_preservation = 0.4f;
1556       br->elastic_deform_type = BRUSH_ELASTIC_DEFORM_GRAB_TRISCALE;
1557       br->flag &= ~BRUSH_ALPHA_PRESSURE;
1558       br->flag &= ~BRUSH_SPACE;
1559       br->flag &= ~BRUSH_SPACE_ATTEN;
1560       break;
1561     case SCULPT_TOOL_POSE:
1562       br->pose_smooth_iterations = 4;
1563       br->pose_ik_segments = 1;
1564       br->flag2 |= BRUSH_POSE_IK_ANCHORED | BRUSH_USE_CONNECTED_ONLY;
1565       br->flag &= ~BRUSH_ALPHA_PRESSURE;
1566       br->flag &= ~BRUSH_SPACE;
1567       br->flag &= ~BRUSH_SPACE_ATTEN;
1568       break;
1569     case SCULPT_TOOL_DRAW_FACE_SETS:
1570       br->alpha = 0.5f;
1571       br->flag &= ~BRUSH_ALPHA_PRESSURE;
1572       br->flag &= ~BRUSH_SPACE;
1573       br->flag &= ~BRUSH_SPACE_ATTEN;
1574       break;
1575     case SCULPT_TOOL_GRAB:
1576       br->alpha = 0.4f;
1577       br->size = 75;
1578       br->flag &= ~BRUSH_ALPHA_PRESSURE;
1579       br->flag &= ~BRUSH_SPACE;
1580       br->flag &= ~BRUSH_SPACE_ATTEN;
1581       break;
1582     case SCULPT_TOOL_CLOTH:
1583       br->cloth_mass = 1.0f;
1584       br->cloth_damping = 0.01f;
1585       br->cloth_sim_limit = 2.5f;
1586       br->cloth_sim_falloff = 0.75f;
1587       br->cloth_deform_type = BRUSH_CLOTH_DEFORM_DRAG;
1588       br->flag &= ~(BRUSH_ALPHA_PRESSURE | BRUSH_SIZE_PRESSURE);
1589       break;
1590     case SCULPT_TOOL_LAYER:
1591       br->flag &= ~BRUSH_SPACE_ATTEN;
1592       br->hardness = 0.35f;
1593       br->alpha = 1.0f;
1594       br->height = 0.05f;
1595       break;
1596     case SCULPT_TOOL_PAINT:
1597       br->hardness = 0.4f;
1598       br->spacing = 10;
1599       br->alpha = 0.6f;
1600       br->flow = 1.0f;
1601       br->tip_scale_x = 1.0f;
1602       br->tip_roundness = 1.0f;
1603       br->density = 1.0f;
1604       br->flag &= ~BRUSH_SPACE_ATTEN;
1605       zero_v3(br->rgb);
1606       break;
1607     case SCULPT_TOOL_SMEAR:
1608       br->alpha = 1.0f;
1609       br->spacing = 5;
1610       br->flag &= ~BRUSH_ALPHA_PRESSURE;
1611       br->flag &= ~BRUSH_SPACE_ATTEN;
1612       br->curve_preset = BRUSH_CURVE_SPHERE;
1613       break;
1614     default:
1615       break;
1616   }
1617
1618   /* Cursor colors */
1619
1620   /* Default Alpha */
1621   br->add_col[3] = 0.90f;
1622   br->sub_col[3] = 0.90f;
1623
1624   switch (br->sculpt_tool) {
1625     case SCULPT_TOOL_DRAW:
1626     case SCULPT_TOOL_DRAW_SHARP:
1627     case SCULPT_TOOL_CLAY:
1628     case SCULPT_TOOL_CLAY_STRIPS:
1629     case SCULPT_TOOL_CLAY_THUMB:
1630     case SCULPT_TOOL_LAYER:
1631     case SCULPT_TOOL_INFLATE:
1632     case SCULPT_TOOL_BLOB:
1633     case SCULPT_TOOL_CREASE:
1634       br->add_col[0] = 0.0f;
1635       br->add_col[1] = 0.5f;
1636       br->add_col[2] = 1.0f;
1637       br->sub_col[0] = 0.0f;
1638       br->sub_col[1] = 0.5f;
1639       br->sub_col[2] = 1.0f;
1640       break;
1641
1642     case SCULPT_TOOL_SMOOTH:
1643     case SCULPT_TOOL_FLATTEN:
1644     case SCULPT_TOOL_FILL:
1645     case SCULPT_TOOL_SCRAPE:
1646     case SCULPT_TOOL_MULTIPLANE_SCRAPE:
1647       br->add_col[0] = 0.877f;
1648       br->add_col[1] = 0.142f;
1649       br->add_col[2] = 0.117f;
1650       br->sub_col[0] = 0.877f;
1651       br->sub_col[1] = 0.142f;
1652       br->sub_col[2] = 0.117f;
1653       break;
1654
1655     case SCULPT_TOOL_PINCH:
1656     case SCULPT_TOOL_GRAB:
1657     case SCULPT_TOOL_SNAKE_HOOK:
1658     case SCULPT_TOOL_THUMB:
1659     case SCULPT_TOOL_NUDGE:
1660     case SCULPT_TOOL_ROTATE:
1661     case SCULPT_TOOL_ELASTIC_DEFORM:
1662     case SCULPT_TOOL_POSE:
1663     case SCULPT_TOOL_SLIDE_RELAX:
1664       br->add_col[0] = 1.0f;
1665       br->add_col[1] = 0.95f;
1666       br->add_col[2] = 0.005f;
1667       br->sub_col[0] = 1.0f;
1668       br->sub_col[1] = 0.95f;
1669       br->sub_col[2] = 0.005f;
1670       break;
1671
1672     case SCULPT_TOOL_SIMPLIFY:
1673     case SCULPT_TOOL_PAINT:
1674     case SCULPT_TOOL_MASK:
1675     case SCULPT_TOOL_DRAW_FACE_SETS:
1676       br->add_col[0] = 0.75f;
1677       br->add_col[1] = 0.75f;
1678       br->add_col[2] = 0.75f;
1679       br->sub_col[0] = 0.75f;
1680       br->sub_col[1] = 0.75f;
1681       br->sub_col[2] = 0.75f;
1682       break;
1683
1684     case SCULPT_TOOL_CLOTH:
1685       br->add_col[0] = 1.0f;
1686       br->add_col[1] = 0.5f;
1687       br->add_col[2] = 0.1f;
1688       br->sub_col[0] = 1.0f;
1689       br->sub_col[1] = 0.5f;
1690       br->sub_col[2] = 0.1f;
1691       break;
1692     default:
1693       break;
1694   }
1695 }
1696
1697 /**
1698  * Library Operations
1699  */
1700 void BKE_brush_curve_preset(Brush *b, eCurveMappingPreset preset)
1701 {
1702   CurveMapping *cumap = NULL;
1703   CurveMap *cuma = NULL;
1704
1705   if (!b->curve) {
1706     b->curve = BKE_curvemapping_add(1, 0, 0, 1, 1);
1707   }
1708   cumap = b->curve;
1709   cumap->flag &= ~CUMA_EXTEND_EXTRAPOLATE;
1710   cumap->preset = preset;
1711
1712   cuma = b->curve->cm;
1713   BKE_curvemap_reset(cuma, &cumap->clipr, cumap->preset, CURVEMAP_SLOPE_NEGATIVE);
1714   BKE_curvemapping_changed(cumap, false);
1715 }
1716
1717 /* Generic texture sampler for 3D painting systems. point has to be either in
1718  * region space mouse coordinates, or 3d world coordinates for 3D mapping.
1719  *
1720  * rgba outputs straight alpha. */
1721 float BKE_brush_sample_tex_3d(const Scene *scene,
1722                               const Brush *br,
1723                               const float point[3],
1724                               float rgba[4],
1725                               const int thread,
1726                               struct ImagePool *pool)
1727 {
1728   UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
1729   const MTex *mtex = &br->mtex;
1730   float intensity = 1.0;
1731   bool hasrgb = false;
1732
1733   if (!mtex->tex) {
1734     intensity = 1;
1735   }
1736   else if (mtex->brush_map_mode == MTEX_MAP_MODE_3D) {
1737     /* Get strength by feeding the vertex
1738      * location directly into a texture */
1739     hasrgb = RE_texture_evaluate(mtex, point, thread, pool, false, false, &intensity, rgba);
1740   }
1741   else if (mtex->brush_map_mode == MTEX_MAP_MODE_STENCIL) {
1742     float rotation = -mtex->rot;
1743     float point_2d[2] = {point[0], point[1]};
1744     float x, y;
1745     float co[3];
1746
1747     x = point_2d[0] - br->stencil_pos[0];
1748     y = point_2d[1] - br->stencil_pos[1];
1749
1750     if (rotation > 0.001f || rotation < -0.001f) {
1751       const float angle = atan2f(y, x) + rotation;
1752       const float flen = sqrtf(x * x + y * y);
1753
1754       x = flen * cosf(angle);
1755       y = flen * sinf(angle);
1756     }
1757
1758     if (fabsf(x) > br->stencil_dimension[0] || fabsf(y) > br->stencil_dimension[1]) {
1759       zero_v4(rgba);
1760       return 0.0f;
1761     }
1762     x /= (br->stencil_dimension[0]);
1763     y /= (br->stencil_dimension[1]);
1764
1765     co[0] = x;
1766     co[1] = y;
1767     co[2] = 0.0f;
1768
1769     hasrgb = RE_texture_evaluate(mtex, co, thread, pool, false, false, &intensity, rgba);
1770   }
1771   else {
1772     float rotation = -mtex->rot;
1773     float point_2d[2] = {point[0], point[1]};
1774     float x = 0.0f, y = 0.0f; /* Quite warnings */
1775     float invradius = 1.0f;   /* Quite warnings */
1776     float co[3];
1777
1778     if (mtex->brush_map_mode == MTEX_MAP_MODE_VIEW) {
1779       /* keep coordinates relative to mouse */
1780
1781       rotation += ups->brush_rotation;
1782
1783       x = point_2d[0] - ups->tex_mouse[0];
1784       y = point_2d[1] - ups->tex_mouse[1];
1785
1786       /* use pressure adjusted size for fixed mode */
1787       invradius = 1.0f / ups->pixel_radius;
1788     }
1789     else if (mtex->brush_map_mode == MTEX_MAP_MODE_TILED) {
1790       /* leave the coordinates relative to the screen */
1791
1792       /* use unadjusted size for tiled mode */
1793       invradius = 1.0f / BKE_brush_size_get(scene, br);
1794
1795       x = point_2d[0];
1796       y = point_2d[1];
1797     }
1798     else if (mtex->brush_map_mode == MTEX_MAP_MODE_RANDOM) {
1799       rotation += ups->brush_rotation;
1800       /* these contain a random coordinate */
1801       x = point_2d[0] - ups->tex_mouse[0];
1802       y = point_2d[1] - ups->tex_mouse[1];
1803
1804       invradius = 1.0f / ups->pixel_radius;
1805     }
1806
1807     x *= invradius;
1808     y *= invradius;
1809
1810     /* it is probably worth optimizing for those cases where
1811      * the texture is not rotated by skipping the calls to
1812      * atan2, sqrtf, sin, and cos. */
1813     if (rotation > 0.001f || rotation < -0.001f) {
1814       const float angle = atan2f(y, x) + rotation;
1815       const float flen = sqrtf(x * x + y * y);
1816
1817       x = flen * cosf(angle);
1818       y = flen * sinf(angle);
1819     }
1820
1821     co[0] = x;
1822     co[1] = y;
1823     co[2] = 0.0f;
1824
1825     hasrgb = RE_texture_evaluate(mtex, co, thread, pool, false, false, &intensity, rgba);
1826   }
1827
1828   intensity += br->texture_sample_bias;
1829
1830   if (!hasrgb) {
1831     rgba[0] = intensity;
1832     rgba[1] = intensity;
1833     rgba[2] = intensity;
1834     rgba[3] = 1.0f;
1835   }
1836   /* For consistency, sampling always returns color in linear space */
1837   else if (ups->do_linear_conversion) {
1838     IMB_colormanagement_colorspace_to_scene_linear_v3(rgba, ups->colorspace);
1839   }
1840
1841   return intensity;
1842 }
1843
1844 float BKE_brush_sample_masktex(
1845     const Scene *scene, Brush *br, const float point[2], const int thread, struct ImagePool *pool)
1846 {
1847   UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
1848   MTex *mtex = &br->mask_mtex;
1849   float rgba[4], intensity;
1850
1851   if (!mtex->tex) {
1852     return 1.0f;
1853   }
1854   if (mtex->brush_map_mode == MTEX_MAP_MODE_STENCIL) {
1855     float rotation = -mtex->rot;
1856     float point_2d[2] = {point[0], point[1]};
1857     float x, y;
1858     float co[3];
1859
1860     x = point_2d[0] - br->mask_stencil_pos[0];
1861     y = point_2d[1] - br->mask_stencil_pos[1];
1862
1863     if (rotation > 0.001f || rotation < -0.001f) {
1864       const float angle = atan2f(y, x) + rotation;
1865       const float flen = sqrtf(x * x + y * y);
1866
1867       x = flen * cosf(angle);
1868       y = flen * sinf(angle);
1869     }
1870
1871     if (fabsf(x) > br->mask_stencil_dimension[0] || fabsf(y) > br->mask_stencil_dimension[1]) {
1872       zero_v4(rgba);
1873       return 0.0f;
1874     }
1875     x /= (br->mask_stencil_dimension[0]);
1876     y /= (br->mask_stencil_dimension[1]);
1877
1878     co[0] = x;
1879     co[1] = y;
1880     co[2] = 0.0f;
1881
1882     RE_texture_evaluate(mtex, co, thread, pool, false, false, &intensity, rgba);
1883   }
1884   else {
1885     float rotation = -mtex->rot;
1886     float point_2d[2] = {point[0], point[1]};
1887     float x = 0.0f, y = 0.0f; /* Quite warnings */
1888     float invradius = 1.0f;   /* Quite warnings */
1889     float co[3];
1890
1891     if (mtex->brush_map_mode == MTEX_MAP_MODE_VIEW) {
1892       /* keep coordinates relative to mouse */
1893
1894       rotation += ups->brush_rotation_sec;
1895
1896       x = point_2d[0] - ups->mask_tex_mouse[0];
1897       y = point_2d[1] - ups->mask_tex_mouse[1];
1898
1899       /* use pressure adjusted size for fixed mode */
1900       invradius = 1.0f / ups->pixel_radius;
1901     }
1902     else if (mtex->brush_map_mode == MTEX_MAP_MODE_TILED) {
1903       /* leave the coordinates relative to the screen */
1904
1905       /* use unadjusted size for tiled mode */
1906       invradius = 1.0f / BKE_brush_size_get(scene, br);
1907
1908       x = point_2d[0];
1909       y = point_2d[1];
1910     }
1911     else if (mtex->brush_map_mode == MTEX_MAP_MODE_RANDOM) {
1912       rotation += ups->brush_rotation_sec;
1913       /* these contain a random coordinate */
1914       x = point_2d[0] - ups->mask_tex_mouse[0];
1915       y = point_2d[1] - ups->mask_tex_mouse[1];
1916
1917       invradius = 1.0f / ups->pixel_radius;
1918     }
1919
1920     x *= invradius;
1921     y *= invradius;
1922
1923     /* it is probably worth optimizing for those cases where
1924      * the texture is not rotated by skipping the calls to
1925      * atan2, sqrtf, sin, and cos. */
1926     if (rotation > 0.001f || rotation < -0.001f) {
1927       const float angle = atan2f(y, x) + rotation;
1928       const float flen = sqrtf(x * x + y * y);
1929
1930       x = flen * cosf(angle);
1931       y = flen * sinf(angle);
1932     }
1933
1934     co[0] = x;
1935     co[1] = y;
1936     co[2] = 0.0f;
1937
1938     RE_texture_evaluate(mtex, co, thread, pool, false, false, &intensity, rgba);
1939   }
1940
1941   CLAMP(intensity, 0.0f, 1.0f);
1942
1943   switch (br->mask_pressure) {
1944     case BRUSH_MASK_PRESSURE_CUTOFF:
1945       intensity = ((1.0f - intensity) < ups->size_pressure_value) ? 1.0f : 0.0f;
1946       break;
1947     case BRUSH_MASK_PRESSURE_RAMP:
1948       intensity = ups->size_pressure_value + intensity * (1.0f - ups->size_pressure_value);
1949       break;
1950     default:
1951       break;
1952   }
1953
1954   return intensity;
1955 }
1956
1957 /* Unified Size / Strength / Color */
1958
1959 /* XXX: be careful about setting size and unprojected radius
1960  * because they depend on one another
1961  * these functions do not set the other corresponding value
1962  * this can lead to odd behavior if size and unprojected
1963  * radius become inconsistent.
1964  * the biggest problem is that it isn't possible to change
1965  * unprojected radius because a view context is not
1966  * available.  my usual solution to this is to use the
1967  * ratio of change of the size to change the unprojected
1968  * radius.  Not completely convinced that is correct.
1969  * In any case, a better solution is needed to prevent
1970  * inconsistency. */
1971
1972 const float *BKE_brush_color_get(const struct Scene *scene, const struct Brush *brush)
1973 {
1974   UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
1975   return (ups->flag & UNIFIED_PAINT_COLOR) ? ups->rgb : brush->rgb;
1976 }
1977
1978 const float *BKE_brush_secondary_color_get(const struct Scene *scene, const struct Brush *brush)
1979 {
1980   UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
1981   return (ups->flag & UNIFIED_PAINT_COLOR) ? ups->secondary_rgb : brush->secondary_rgb;
1982 }
1983
1984 void BKE_brush_color_set(struct Scene *scene, struct Brush *brush, const float color[3])
1985 {
1986   UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
1987
1988   if (ups->flag & UNIFIED_PAINT_COLOR) {
1989     copy_v3_v3(ups->rgb, color);
1990   }
1991   else {
1992     copy_v3_v3(brush->rgb, color);
1993   }
1994 }
1995
1996 void BKE_brush_size_set(Scene *scene, Brush *brush, int size)
1997 {
1998   UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
1999
2000   /* make sure range is sane */
2001   CLAMP(size, 1, MAX_BRUSH_PIXEL_RADIUS);
2002
2003   if (ups->flag & UNIFIED_PAINT_SIZE) {
2004     ups->size = size;
2005   }
2006   else {
2007     brush->size = size;
2008   }
2009 }
2010
2011 int BKE_brush_size_get(const Scene *scene, const Brush *brush)
2012 {
2013   UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
2014   int size = (ups->flag & UNIFIED_PAINT_SIZE) ? ups->size : brush->size;
2015
2016   return size;
2017 }
2018
2019 bool BKE_brush_use_locked_size(const Scene *scene, const Brush *brush)
2020 {
2021   const short us_flag = scene->toolsettings->unified_paint_settings.flag;
2022
2023   return (us_flag & UNIFIED_PAINT_SIZE) ? (us_flag & UNIFIED_PAINT_BRUSH_LOCK_SIZE) :
2024                                           (brush->flag & BRUSH_LOCK_SIZE);
2025 }
2026
2027 bool BKE_brush_use_size_pressure(const Brush *brush)
2028 {
2029   return brush->flag & BRUSH_SIZE_PRESSURE;
2030 }
2031
2032 bool BKE_brush_use_alpha_pressure(const Brush *brush)
2033 {
2034   return brush->flag & BRUSH_ALPHA_PRESSURE;
2035 }
2036
2037 bool BKE_brush_sculpt_has_secondary_color(const Brush *brush)
2038 {
2039   return ELEM(brush->sculpt_tool,
2040               SCULPT_TOOL_BLOB,
2041               SCULPT_TOOL_DRAW,
2042               SCULPT_TOOL_DRAW_SHARP,
2043               SCULPT_TOOL_INFLATE,
2044               SCULPT_TOOL_CLAY,
2045               SCULPT_TOOL_CLAY_STRIPS,
2046               SCULPT_TOOL_CLAY_THUMB,
2047               SCULPT_TOOL_PINCH,
2048               SCULPT_TOOL_CREASE,
2049               SCULPT_TOOL_LAYER,
2050               SCULPT_TOOL_FLATTEN,
2051               SCULPT_TOOL_FILL,
2052               SCULPT_TOOL_SCRAPE,
2053               SCULPT_TOOL_MASK);
2054 }
2055
2056 void BKE_brush_unprojected_radius_set(Scene *scene, Brush *brush, float unprojected_radius)
2057 {
2058   UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
2059
2060   if (ups->flag & UNIFIED_PAINT_SIZE) {
2061     ups->unprojected_radius = unprojected_radius;
2062   }
2063   else {
2064     brush->unprojected_radius = unprojected_radius;
2065   }
2066 }
2067
2068 float BKE_brush_unprojected_radius_get(const Scene *scene, const Brush *brush)
2069 {
2070   UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
2071
2072   return (ups->flag & UNIFIED_PAINT_SIZE) ? ups->unprojected_radius : brush->unprojected_radius;
2073 }
2074
2075 void BKE_brush_alpha_set(Scene *scene, Brush *brush, float alpha)
2076 {
2077   UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
2078
2079   if (ups->flag & UNIFIED_PAINT_ALPHA) {
2080     ups->alpha = alpha;
2081   }
2082   else {
2083     brush->alpha = alpha;
2084   }
2085 }
2086
2087 float BKE_brush_alpha_get(const Scene *scene, const Brush *brush)
2088 {
2089   UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
2090
2091   return (ups->flag & UNIFIED_PAINT_ALPHA) ? ups->alpha : brush->alpha;
2092 }
2093
2094 float BKE_brush_weight_get(const Scene *scene, const Brush *brush)
2095 {
2096   UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
2097
2098   return (ups->flag & UNIFIED_PAINT_WEIGHT) ? ups->weight : brush->weight;
2099 }
2100
2101 void BKE_brush_weight_set(const Scene *scene, Brush *brush, float value)
2102 {
2103   UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
2104
2105   if (ups->flag & UNIFIED_PAINT_WEIGHT) {
2106     ups->weight = value;
2107   }
2108   else {
2109     brush->weight = value;
2110   }
2111 }
2112
2113 /* scale unprojected radius to reflect a change in the brush's 2D size */
2114 void BKE_brush_scale_unprojected_radius(float *unprojected_radius,
2115                                         int new_brush_size,
2116                                         int old_brush_size)
2117 {
2118   float scale = new_brush_size;
2119   /* avoid division by zero */
2120   if (old_brush_size != 0) {
2121     scale /= (float)old_brush_size;
2122   }
2123   (*unprojected_radius) *= scale;
2124 }
2125
2126 /* scale brush size to reflect a change in the brush's unprojected radius */
2127 void BKE_brush_scale_size(int *r_brush_size,
2128                           float new_unprojected_radius,
2129                           float old_unprojected_radius)
2130 {
2131   float scale = new_unprojected_radius;
2132   /* avoid division by zero */
2133   if (old_unprojected_radius != 0) {
2134     scale /= new_unprojected_radius;
2135   }
2136   (*r_brush_size) = (int)((float)(*r_brush_size) * scale);
2137 }
2138
2139 void BKE_brush_jitter_pos(const Scene *scene, Brush *brush, const float pos[2], float jitterpos[2])
2140 {
2141   float rand_pos[2];
2142   float spread;
2143   int diameter;
2144
2145   do {
2146     rand_pos[0] = BLI_rng_get_float(brush_rng) - 0.5f;
2147     rand_pos[1] = BLI_rng_get_float(brush_rng) - 0.5f;
2148   } while (len_squared_v2(rand_pos) > square_f(0.5f));
2149
2150   if (brush->flag & BRUSH_ABSOLUTE_JITTER) {
2151     diameter = 2 * brush->jitter_absolute;
2152     spread = 1.0;
2153   }
2154   else {
2155     diameter = 2 * BKE_brush_size_get(scene, brush);
2156     spread = brush->jitter;
2157   }
2158   /* find random position within a circle of diameter 1 */
2159   jitterpos[0] = pos[0] + 2 * rand_pos[0] * diameter * spread;
2160   jitterpos[1] = pos[1] + 2 * rand_pos[1] * diameter * spread;
2161 }
2162
2163 void BKE_brush_randomize_texture_coords(UnifiedPaintSettings *ups, bool mask)
2164 {
2165   /* we multiply with brush radius as an optimization for the brush
2166    * texture sampling functions */
2167   if (mask) {
2168     ups->mask_tex_mouse[0] = BLI_rng_get_float(brush_rng) * ups->pixel_radius;
2169     ups->mask_tex_mouse[1] = BLI_rng_get_float(brush_rng) * ups->pixel_radius;
2170   }
2171   else {
2172     ups->tex_mouse[0] = BLI_rng_get_float(brush_rng) * ups->pixel_radius;
2173     ups->tex_mouse[1] = BLI_rng_get_float(brush_rng) * ups->pixel_radius;
2174   }
2175 }
2176
2177 /* Uses the brush curve control to find a strength value */
2178 float BKE_brush_curve_strength(const Brush *br, float p, const float len)
2179 {
2180   float strength = 1.0f;
2181
2182   if (p >= len) {
2183     return 0;
2184   }
2185
2186   p = p / len;
2187   p = 1.0f - p;
2188
2189   switch (br->curve_preset) {
2190     case BRUSH_CURVE_CUSTOM:
2191       strength = BKE_curvemapping_evaluateF(br->curve, 0, 1.0f - p);
2192       break;
2193     case BRUSH_CURVE_SHARP:
2194       strength = p * p;
2195       break;
2196     case BRUSH_CURVE_SMOOTH:
2197       strength = 3.0f * p * p - 2.0f * p * p * p;
2198       break;
2199     case BRUSH_CURVE_SMOOTHER:
2200       strength = pow3f(p) * (p * (p * 6.0f - 15.0f) + 10.0f);
2201       break;
2202     case BRUSH_CURVE_ROOT:
2203       strength = sqrtf(p);
2204       break;
2205     case BRUSH_CURVE_LIN:
2206       strength = p;
2207       break;
2208     case BRUSH_CURVE_CONSTANT:
2209       strength = 1.0f;
2210       break;
2211     case BRUSH_CURVE_SPHERE:
2212       strength = sqrtf(2 * p - p * p);
2213       break;
2214     case BRUSH_CURVE_POW4:
2215       strength = p * p * p * p;
2216       break;
2217     case BRUSH_CURVE_INVSQUARE:
2218       strength = p * (2.0f - p);
2219       break;
2220   }
2221
2222   return strength;
2223 }
2224
2225 /* Uses the brush curve control to find a strength value between 0 and 1 */
2226 float BKE_brush_curve_strength_clamped(Brush *br, float p, const float len)
2227 {
2228   float strength = BKE_brush_curve_strength(br, p, len);
2229
2230   CLAMP(strength, 0.0f, 1.0f);
2231
2232   return strength;
2233 }
2234
2235 /* TODO: should probably be unified with BrushPainter stuff? */
2236 unsigned int *BKE_brush_gen_texture_cache(Brush *br, int half_side, bool use_secondary)
2237 {
2238   unsigned int *texcache = NULL;
2239   MTex *mtex = (use_secondary) ? &br->mask_mtex : &br->mtex;
2240   float intensity;
2241   float rgba_dummy[4];
2242   int ix, iy;
2243   int side = half_side * 2;
2244
2245   if (mtex->tex) {
2246     float x, y, step = 2.0 / side, co[3];
2247
2248     texcache = MEM_callocN(sizeof(int) * side * side, "Brush texture cache");
2249
2250     /* do normalized canonical view coords for texture */
2251     for (y = -1.0, iy = 0; iy < side; iy++, y += step) {
2252       for (x = -1.0, ix = 0; ix < side; ix++, x += step) {
2253         co[0] = x;
2254         co[1] = y;
2255         co[2] = 0.0f;
2256
2257         /* This is copied from displace modifier code */
2258         /* TODO(sergey): brush are always caching with CM enabled for now. */
2259         RE_texture_evaluate(mtex, co, 0, NULL, false, false, &intensity, rgba_dummy);
2260         copy_v4_uchar((uchar *)&texcache[iy * side + ix], (char)(intensity * 255.0f));
2261       }
2262     }
2263   }
2264
2265   return texcache;
2266 }
2267
2268 /**** Radial Control ****/
2269 struct ImBuf *BKE_brush_gen_radial_control_imbuf(Brush *br, bool secondary, bool display_gradient)
2270 {
2271   ImBuf *im = MEM_callocN(sizeof(ImBuf), "radial control texture");
2272   unsigned int *texcache;
2273   int side = 512;
2274   int half = side / 2;
2275   int i, j;
2276
2277   BKE_curvemapping_init(br->curve);
2278   texcache = BKE_brush_gen_texture_cache(br, half, secondary);
2279   im->rect_float = MEM_callocN(sizeof(float) * side * side, "radial control rect");
2280   im->x = im->y = side;
2281
2282   if (display_gradient || texcache) {
2283     for (i = 0; i < side; i++) {
2284       for (j = 0; j < side; j++) {
2285         float magn = sqrtf(pow2f(i - half) + pow2f(j - half));
2286         im->rect_float[i * side + j] = BKE_brush_curve_strength_clamped(br, magn, half);
2287       }
2288     }
2289   }
2290
2291   if (texcache) {
2292     /* Modulate curve with texture */
2293     for (i = 0; i < side; i++) {
2294       for (j = 0; j < side; j++) {
2295         const int col = texcache[i * side + j];
2296         im->rect_float[i * side + j] *= (((char *)&col)[0] + ((char *)&col)[1] +
2297                                          ((char *)&col)[2]) /
2298                                         3.0f / 255.0f;
2299       }
2300     }
2301     MEM_freeN(texcache);
2302   }
2303
2304   return im;
2305 }