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