brush.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  1. /*
  2. * ***** BEGIN GPL LICENSE BLOCK *****
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software Foundation,
  16. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. *
  18. * Contributor(s): none yet.
  19. *
  20. * ***** END GPL LICENSE BLOCK *****
  21. */
  22. /** \file blender/blenkernel/intern/brush.c
  23. * \ingroup bke
  24. */
  25. #include "MEM_guardedalloc.h"
  26. #include "DNA_brush_types.h"
  27. #include "DNA_scene_types.h"
  28. #include "DNA_object_types.h"
  29. #include "BLI_math.h"
  30. #include "BLI_blenlib.h"
  31. #include "BLI_rand.h"
  32. #include "BKE_brush.h"
  33. #include "BKE_colortools.h"
  34. #include "BKE_global.h"
  35. #include "BKE_library.h"
  36. #include "BKE_library_query.h"
  37. #include "BKE_library_remap.h"
  38. #include "BKE_main.h"
  39. #include "BKE_paint.h"
  40. #include "BKE_texture.h"
  41. #include "BKE_icons.h"
  42. #include "IMB_colormanagement.h"
  43. #include "IMB_imbuf.h"
  44. #include "IMB_imbuf_types.h"
  45. #include "RE_render_ext.h" /* externtex */
  46. static RNG *brush_rng;
  47. void BKE_brush_system_init(void)
  48. {
  49. brush_rng = BLI_rng_new(0);
  50. BLI_rng_srandom(brush_rng, 31415682);
  51. }
  52. void BKE_brush_system_exit(void)
  53. {
  54. BLI_rng_free(brush_rng);
  55. }
  56. static void brush_defaults(Brush *brush)
  57. {
  58. brush->blend = 0;
  59. brush->flag = 0;
  60. brush->ob_mode = OB_MODE_ALL_PAINT;
  61. /* BRUSH SCULPT TOOL SETTINGS */
  62. brush->weight = 1.0f; /* weight of brush 0 - 1.0 */
  63. brush->size = 35; /* radius of the brush in pixels */
  64. brush->alpha = 0.5f; /* brush strength/intensity probably variable should be renamed? */
  65. brush->autosmooth_factor = 0.0f;
  66. brush->crease_pinch_factor = 0.5f;
  67. brush->sculpt_plane = SCULPT_DISP_DIR_AREA;
  68. brush->plane_offset = 0.0f; /* how far above or below the plane that is found by averaging the faces */
  69. brush->plane_trim = 0.5f;
  70. brush->clone.alpha = 0.5f;
  71. brush->normal_weight = 0.0f;
  72. brush->fill_threshold = 0.2f;
  73. brush->flag |= BRUSH_ALPHA_PRESSURE;
  74. /* BRUSH PAINT TOOL SETTINGS */
  75. brush->rgb[0] = 1.0f; /* default rgb color of the brush when painting - white */
  76. brush->rgb[1] = 1.0f;
  77. brush->rgb[2] = 1.0f;
  78. zero_v3(brush->secondary_rgb);
  79. /* BRUSH STROKE SETTINGS */
  80. brush->flag |= (BRUSH_SPACE | BRUSH_SPACE_ATTEN);
  81. brush->spacing = 10; /* how far each brush dot should be spaced as a percentage of brush diameter */
  82. brush->smooth_stroke_radius = 75;
  83. brush->smooth_stroke_factor = 0.9f;
  84. brush->rate = 0.1f; /* time delay between dots of paint or sculpting when doing airbrush mode */
  85. brush->jitter = 0.0f;
  86. /* BRUSH TEXTURE SETTINGS */
  87. BKE_texture_mtex_default(&brush->mtex);
  88. BKE_texture_mtex_default(&brush->mask_mtex);
  89. brush->texture_sample_bias = 0; /* value to added to texture samples */
  90. brush->texture_overlay_alpha = 33;
  91. brush->mask_overlay_alpha = 33;
  92. brush->cursor_overlay_alpha = 33;
  93. brush->overlay_flags = 0;
  94. /* brush appearance */
  95. brush->add_col[0] = 1.00; /* add mode color is light red */
  96. brush->add_col[1] = 0.39;
  97. brush->add_col[2] = 0.39;
  98. brush->sub_col[0] = 0.39; /* subtract mode color is light blue */
  99. brush->sub_col[1] = 0.39;
  100. brush->sub_col[2] = 1.00;
  101. brush->stencil_pos[0] = 256;
  102. brush->stencil_pos[1] = 256;
  103. brush->stencil_dimension[0] = 256;
  104. brush->stencil_dimension[1] = 256;
  105. }
  106. /* Datablock add/copy/free/make_local */
  107. void BKE_brush_init(Brush *brush)
  108. {
  109. BLI_assert(MEMCMP_STRUCT_OFS_IS_ZERO(brush, id));
  110. /* enable fake user by default */
  111. id_fake_user_set(&brush->id);
  112. brush_defaults(brush);
  113. brush->sculpt_tool = SCULPT_TOOL_DRAW; /* sculpting defaults to the draw tool for new brushes */
  114. /* the default alpha falloff curve */
  115. BKE_brush_curve_preset(brush, CURVE_PRESET_SMOOTH);
  116. }
  117. Brush *BKE_brush_add(Main *bmain, const char *name, short ob_mode)
  118. {
  119. Brush *brush;
  120. brush = BKE_libblock_alloc(bmain, ID_BR, name);
  121. BKE_brush_init(brush);
  122. brush->ob_mode = ob_mode;
  123. return brush;
  124. }
  125. struct Brush *BKE_brush_first_search(struct Main *bmain, short ob_mode)
  126. {
  127. Brush *brush;
  128. for (brush = bmain->brush.first; brush; brush = brush->id.next) {
  129. if (brush->ob_mode & ob_mode)
  130. return brush;
  131. }
  132. return NULL;
  133. }
  134. Brush *BKE_brush_copy(Main *bmain, const Brush *brush)
  135. {
  136. Brush *brushn;
  137. brushn = BKE_libblock_copy(bmain, &brush->id);
  138. if (brush->mtex.tex)
  139. id_us_plus((ID *)brush->mtex.tex);
  140. if (brush->mask_mtex.tex)
  141. id_us_plus((ID *)brush->mask_mtex.tex);
  142. if (brush->paint_curve)
  143. id_us_plus((ID *)brush->paint_curve);
  144. if (brush->icon_imbuf)
  145. brushn->icon_imbuf = IMB_dupImBuf(brush->icon_imbuf);
  146. brushn->preview = NULL;
  147. brushn->curve = curvemapping_copy(brush->curve);
  148. /* enable fake user by default */
  149. id_fake_user_set(&brushn->id);
  150. BKE_id_copy_ensure_local(bmain, &brush->id, &brushn->id);
  151. return brushn;
  152. }
  153. /** Free (or release) any data used by this brush (does not free the brush itself). */
  154. void BKE_brush_free(Brush *brush)
  155. {
  156. if (brush->icon_imbuf) {
  157. IMB_freeImBuf(brush->icon_imbuf);
  158. }
  159. curvemapping_free(brush->curve);
  160. MEM_SAFE_FREE(brush->gradient);
  161. BKE_previewimg_free(&(brush->preview));
  162. }
  163. void BKE_brush_make_local(Main *bmain, Brush *brush, const bool lib_local)
  164. {
  165. bool is_local = false, is_lib = false;
  166. /* - only lib users: do nothing (unless force_local is set)
  167. * - only local users: set flag
  168. * - mixed: make copy
  169. */
  170. if (!ID_IS_LINKED_DATABLOCK(brush)) {
  171. return;
  172. }
  173. if (brush->clone.image) {
  174. /* Special case: ima always local immediately. Clone image should only have one user anyway. */
  175. id_make_local(bmain, &brush->clone.image->id, false, false);
  176. }
  177. BKE_library_ID_test_usages(bmain, brush, &is_local, &is_lib);
  178. if (lib_local || is_local) {
  179. if (!is_lib) {
  180. id_clear_lib_data(bmain, &brush->id);
  181. BKE_id_expand_local(bmain, &brush->id);
  182. /* enable fake user by default */
  183. id_fake_user_set(&brush->id);
  184. }
  185. else {
  186. Brush *brush_new = BKE_brush_copy(bmain, brush); /* Ensures FAKE_USER is set */
  187. brush_new->id.us = 0;
  188. /* setting newid is mandatory for complex make_lib_local logic... */
  189. ID_NEW_SET(brush, brush_new);
  190. if (!lib_local) {
  191. BKE_libblock_remap(bmain, brush, brush_new, ID_REMAP_SKIP_INDIRECT_USAGE);
  192. }
  193. }
  194. }
  195. }
  196. void BKE_brush_debug_print_state(Brush *br)
  197. {
  198. /* create a fake brush and set it to the defaults */
  199. Brush def = {{NULL}};
  200. brush_defaults(&def);
  201. #define BR_TEST(field, t) \
  202. if (br->field != def.field) \
  203. printf("br->" #field " = %" #t ";\n", br->field)
  204. #define BR_TEST_FLAG(_f) \
  205. if ((br->flag & _f) && !(def.flag & _f)) \
  206. printf("br->flag |= " #_f ";\n"); \
  207. else if (!(br->flag & _f) && (def.flag & _f)) \
  208. printf("br->flag &= ~" #_f ";\n")
  209. #define BR_TEST_FLAG_OVERLAY(_f) \
  210. if ((br->overlay_flags & _f) && !(def.overlay_flags & _f)) \
  211. printf("br->overlay_flags |= " #_f ";\n"); \
  212. else if (!(br->overlay_flags & _f) && (def.overlay_flags & _f)) \
  213. printf("br->overlay_flags &= ~" #_f ";\n")
  214. /* print out any non-default brush state */
  215. BR_TEST(normal_weight, f);
  216. BR_TEST(blend, d);
  217. BR_TEST(size, d);
  218. /* br->flag */
  219. BR_TEST_FLAG(BRUSH_AIRBRUSH);
  220. BR_TEST_FLAG(BRUSH_ALPHA_PRESSURE);
  221. BR_TEST_FLAG(BRUSH_SIZE_PRESSURE);
  222. BR_TEST_FLAG(BRUSH_JITTER_PRESSURE);
  223. BR_TEST_FLAG(BRUSH_SPACING_PRESSURE);
  224. BR_TEST_FLAG(BRUSH_ANCHORED);
  225. BR_TEST_FLAG(BRUSH_DIR_IN);
  226. BR_TEST_FLAG(BRUSH_SPACE);
  227. BR_TEST_FLAG(BRUSH_SMOOTH_STROKE);
  228. BR_TEST_FLAG(BRUSH_PERSISTENT);
  229. BR_TEST_FLAG(BRUSH_ACCUMULATE);
  230. BR_TEST_FLAG(BRUSH_LOCK_ALPHA);
  231. BR_TEST_FLAG(BRUSH_ORIGINAL_NORMAL);
  232. BR_TEST_FLAG(BRUSH_OFFSET_PRESSURE);
  233. BR_TEST_FLAG(BRUSH_SPACE_ATTEN);
  234. BR_TEST_FLAG(BRUSH_ADAPTIVE_SPACE);
  235. BR_TEST_FLAG(BRUSH_LOCK_SIZE);
  236. BR_TEST_FLAG(BRUSH_EDGE_TO_EDGE);
  237. BR_TEST_FLAG(BRUSH_DRAG_DOT);
  238. BR_TEST_FLAG(BRUSH_INVERSE_SMOOTH_PRESSURE);
  239. BR_TEST_FLAG(BRUSH_PLANE_TRIM);
  240. BR_TEST_FLAG(BRUSH_FRONTFACE);
  241. BR_TEST_FLAG(BRUSH_CUSTOM_ICON);
  242. BR_TEST_FLAG_OVERLAY(BRUSH_OVERLAY_CURSOR);
  243. BR_TEST_FLAG_OVERLAY(BRUSH_OVERLAY_PRIMARY);
  244. BR_TEST_FLAG_OVERLAY(BRUSH_OVERLAY_SECONDARY);
  245. BR_TEST_FLAG_OVERLAY(BRUSH_OVERLAY_CURSOR_OVERRIDE_ON_STROKE);
  246. BR_TEST_FLAG_OVERLAY(BRUSH_OVERLAY_PRIMARY_OVERRIDE_ON_STROKE);
  247. BR_TEST_FLAG_OVERLAY(BRUSH_OVERLAY_SECONDARY_OVERRIDE_ON_STROKE);
  248. BR_TEST(jitter, f);
  249. BR_TEST(spacing, d);
  250. BR_TEST(smooth_stroke_radius, d);
  251. BR_TEST(smooth_stroke_factor, f);
  252. BR_TEST(rate, f);
  253. BR_TEST(alpha, f);
  254. BR_TEST(sculpt_plane, d);
  255. BR_TEST(plane_offset, f);
  256. BR_TEST(autosmooth_factor, f);
  257. BR_TEST(crease_pinch_factor, f);
  258. BR_TEST(plane_trim, f);
  259. BR_TEST(texture_sample_bias, f);
  260. BR_TEST(texture_overlay_alpha, d);
  261. BR_TEST(add_col[0], f);
  262. BR_TEST(add_col[1], f);
  263. BR_TEST(add_col[2], f);
  264. BR_TEST(sub_col[0], f);
  265. BR_TEST(sub_col[1], f);
  266. BR_TEST(sub_col[2], f);
  267. printf("\n");
  268. #undef BR_TEST
  269. #undef BR_TEST_FLAG
  270. }
  271. void BKE_brush_sculpt_reset(Brush *br)
  272. {
  273. /* enable this to see any non-default
  274. * settings used by a brush: */
  275. // BKE_brush_debug_print_state(br);
  276. brush_defaults(br);
  277. BKE_brush_curve_preset(br, CURVE_PRESET_SMOOTH);
  278. switch (br->sculpt_tool) {
  279. case SCULPT_TOOL_CLAY:
  280. br->flag |= BRUSH_FRONTFACE;
  281. break;
  282. case SCULPT_TOOL_CREASE:
  283. br->flag |= BRUSH_DIR_IN;
  284. br->alpha = 0.25;
  285. break;
  286. case SCULPT_TOOL_FILL:
  287. br->add_col[1] = 1;
  288. br->sub_col[0] = 0.25;
  289. br->sub_col[1] = 1;
  290. break;
  291. case SCULPT_TOOL_FLATTEN:
  292. br->add_col[1] = 1;
  293. br->sub_col[0] = 0.25;
  294. br->sub_col[1] = 1;
  295. break;
  296. case SCULPT_TOOL_INFLATE:
  297. br->add_col[0] = 0.750000;
  298. br->add_col[1] = 0.750000;
  299. br->add_col[2] = 0.750000;
  300. br->sub_col[0] = 0.250000;
  301. br->sub_col[1] = 0.250000;
  302. br->sub_col[2] = 0.250000;
  303. break;
  304. case SCULPT_TOOL_NUDGE:
  305. br->add_col[0] = 0.250000;
  306. br->add_col[1] = 1.000000;
  307. br->add_col[2] = 0.250000;
  308. break;
  309. case SCULPT_TOOL_PINCH:
  310. br->add_col[0] = 0.750000;
  311. br->add_col[1] = 0.750000;
  312. br->add_col[2] = 0.750000;
  313. br->sub_col[0] = 0.250000;
  314. br->sub_col[1] = 0.250000;
  315. br->sub_col[2] = 0.250000;
  316. break;
  317. case SCULPT_TOOL_SCRAPE:
  318. br->add_col[1] = 1.000000;
  319. br->sub_col[0] = 0.250000;
  320. br->sub_col[1] = 1.000000;
  321. break;
  322. case SCULPT_TOOL_ROTATE:
  323. br->alpha = 1.0;
  324. break;
  325. case SCULPT_TOOL_SMOOTH:
  326. br->flag &= ~BRUSH_SPACE_ATTEN;
  327. br->spacing = 5;
  328. br->add_col[0] = 0.750000;
  329. br->add_col[1] = 0.750000;
  330. br->add_col[2] = 0.750000;
  331. break;
  332. case SCULPT_TOOL_GRAB:
  333. case SCULPT_TOOL_SNAKE_HOOK:
  334. case SCULPT_TOOL_THUMB:
  335. br->size = 75;
  336. br->flag &= ~BRUSH_ALPHA_PRESSURE;
  337. br->flag &= ~BRUSH_SPACE;
  338. br->flag &= ~BRUSH_SPACE_ATTEN;
  339. br->add_col[0] = 0.250000;
  340. br->add_col[1] = 1.000000;
  341. br->add_col[2] = 0.250000;
  342. break;
  343. default:
  344. break;
  345. }
  346. }
  347. /**
  348. * Library Operations
  349. * \param preset CurveMappingPreset
  350. */
  351. void BKE_brush_curve_preset(Brush *b, int preset)
  352. {
  353. CurveMap *cm = NULL;
  354. if (!b->curve)
  355. b->curve = curvemapping_add(1, 0, 0, 1, 1);
  356. cm = b->curve->cm;
  357. cm->flag &= ~CUMA_EXTEND_EXTRAPOLATE;
  358. b->curve->preset = preset;
  359. curvemap_reset(cm, &b->curve->clipr, b->curve->preset, CURVEMAP_SLOPE_NEGATIVE);
  360. curvemapping_changed(b->curve, false);
  361. }
  362. /* XXX Unused function. */
  363. int BKE_brush_texture_set_nr(Brush *brush, int nr)
  364. {
  365. ID *idtest, *id = NULL;
  366. id = (ID *)brush->mtex.tex;
  367. idtest = (ID *)BLI_findlink(&G.main->tex, nr - 1);
  368. if (idtest == NULL) { /* new tex */
  369. if (id) idtest = (ID *)BKE_texture_copy(G.main, (Tex *)id);
  370. else idtest = (ID *)BKE_texture_add(G.main, "Tex");
  371. id_us_min(idtest);
  372. }
  373. if (idtest != id) {
  374. BKE_brush_texture_delete(brush);
  375. brush->mtex.tex = (Tex *)idtest;
  376. id_us_plus(idtest);
  377. return 1;
  378. }
  379. return 0;
  380. }
  381. int BKE_brush_texture_delete(Brush *brush)
  382. {
  383. if (brush->mtex.tex)
  384. id_us_min(&brush->mtex.tex->id);
  385. return 1;
  386. }
  387. int BKE_brush_clone_image_set_nr(Brush *brush, int nr)
  388. {
  389. if (brush && nr > 0) {
  390. Image *ima = (Image *)BLI_findlink(&G.main->image, nr - 1);
  391. if (ima) {
  392. BKE_brush_clone_image_delete(brush);
  393. brush->clone.image = ima;
  394. id_us_plus(&ima->id);
  395. brush->clone.offset[0] = brush->clone.offset[1] = 0.0f;
  396. return 1;
  397. }
  398. }
  399. return 0;
  400. }
  401. int BKE_brush_clone_image_delete(Brush *brush)
  402. {
  403. if (brush && brush->clone.image) {
  404. id_us_min(&brush->clone.image->id);
  405. brush->clone.image = NULL;
  406. return 1;
  407. }
  408. return 0;
  409. }
  410. /* Generic texture sampler for 3D painting systems. point has to be either in
  411. * region space mouse coordinates, or 3d world coordinates for 3D mapping.
  412. *
  413. * rgba outputs straight alpha. */
  414. float BKE_brush_sample_tex_3D(const Scene *scene, Brush *br,
  415. const float point[3],
  416. float rgba[4], const int thread,
  417. struct ImagePool *pool)
  418. {
  419. UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
  420. MTex *mtex = &br->mtex;
  421. float intensity = 1.0;
  422. bool hasrgb = false;
  423. if (!mtex->tex) {
  424. intensity = 1;
  425. }
  426. else if (mtex->brush_map_mode == MTEX_MAP_MODE_3D) {
  427. /* Get strength by feeding the vertex
  428. * location directly into a texture */
  429. hasrgb = externtex(mtex, point, &intensity,
  430. rgba, rgba + 1, rgba + 2, rgba + 3, thread, pool, false, false);
  431. }
  432. else if (mtex->brush_map_mode == MTEX_MAP_MODE_STENCIL) {
  433. float rotation = -mtex->rot;
  434. float point_2d[2] = {point[0], point[1]};
  435. float x, y;
  436. float co[3];
  437. x = point_2d[0] - br->stencil_pos[0];
  438. y = point_2d[1] - br->stencil_pos[1];
  439. if (rotation > 0.001f || rotation < -0.001f) {
  440. const float angle = atan2f(y, x) + rotation;
  441. const float flen = sqrtf(x * x + y * y);
  442. x = flen * cosf(angle);
  443. y = flen * sinf(angle);
  444. }
  445. if (fabsf(x) > br->stencil_dimension[0] || fabsf(y) > br->stencil_dimension[1]) {
  446. zero_v4(rgba);
  447. return 0.0f;
  448. }
  449. x /= (br->stencil_dimension[0]);
  450. y /= (br->stencil_dimension[1]);
  451. co[0] = x;
  452. co[1] = y;
  453. co[2] = 0.0f;
  454. hasrgb = externtex(mtex, co, &intensity,
  455. rgba, rgba + 1, rgba + 2, rgba + 3, thread, pool, false, false);
  456. }
  457. else {
  458. float rotation = -mtex->rot;
  459. float point_2d[2] = {point[0], point[1]};
  460. float x = 0.0f, y = 0.0f; /* Quite warnings */
  461. float invradius = 1.0f; /* Quite warnings */
  462. float co[3];
  463. if (mtex->brush_map_mode == MTEX_MAP_MODE_VIEW) {
  464. /* keep coordinates relative to mouse */
  465. rotation += ups->brush_rotation;
  466. x = point_2d[0] - ups->tex_mouse[0];
  467. y = point_2d[1] - ups->tex_mouse[1];
  468. /* use pressure adjusted size for fixed mode */
  469. invradius = 1.0f / ups->pixel_radius;
  470. }
  471. else if (mtex->brush_map_mode == MTEX_MAP_MODE_TILED) {
  472. /* leave the coordinates relative to the screen */
  473. /* use unadjusted size for tiled mode */
  474. invradius = 1.0f / BKE_brush_size_get(scene, br);
  475. x = point_2d[0];
  476. y = point_2d[1];
  477. }
  478. else if (mtex->brush_map_mode == MTEX_MAP_MODE_RANDOM) {
  479. rotation += ups->brush_rotation;
  480. /* these contain a random coordinate */
  481. x = point_2d[0] - ups->tex_mouse[0];
  482. y = point_2d[1] - ups->tex_mouse[1];
  483. invradius = 1.0f / ups->pixel_radius;
  484. }
  485. x *= invradius;
  486. y *= invradius;
  487. /* it is probably worth optimizing for those cases where
  488. * the texture is not rotated by skipping the calls to
  489. * atan2, sqrtf, sin, and cos. */
  490. if (rotation > 0.001f || rotation < -0.001f) {
  491. const float angle = atan2f(y, x) + rotation;
  492. const float flen = sqrtf(x * x + y * y);
  493. x = flen * cosf(angle);
  494. y = flen * sinf(angle);
  495. }
  496. co[0] = x;
  497. co[1] = y;
  498. co[2] = 0.0f;
  499. hasrgb = externtex(mtex, co, &intensity,
  500. rgba, rgba + 1, rgba + 2, rgba + 3, thread, pool, false, false);
  501. }
  502. intensity += br->texture_sample_bias;
  503. if (!hasrgb) {
  504. rgba[0] = intensity;
  505. rgba[1] = intensity;
  506. rgba[2] = intensity;
  507. rgba[3] = 1.0f;
  508. }
  509. /* For consistency, sampling always returns color in linear space */
  510. else if (ups->do_linear_conversion) {
  511. IMB_colormanagement_colorspace_to_scene_linear_v3(rgba, ups->colorspace);
  512. }
  513. return intensity;
  514. }
  515. float BKE_brush_sample_masktex(const Scene *scene, Brush *br,
  516. const float point[2],
  517. const int thread,
  518. struct ImagePool *pool)
  519. {
  520. UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
  521. MTex *mtex = &br->mask_mtex;
  522. float rgba[4], intensity;
  523. if (!mtex->tex) {
  524. return 1.0f;
  525. }
  526. if (mtex->brush_map_mode == MTEX_MAP_MODE_STENCIL) {
  527. float rotation = -mtex->rot;
  528. float point_2d[2] = {point[0], point[1]};
  529. float x, y;
  530. float co[3];
  531. x = point_2d[0] - br->mask_stencil_pos[0];
  532. y = point_2d[1] - br->mask_stencil_pos[1];
  533. if (rotation > 0.001f || rotation < -0.001f) {
  534. const float angle = atan2f(y, x) + rotation;
  535. const float flen = sqrtf(x * x + y * y);
  536. x = flen * cosf(angle);
  537. y = flen * sinf(angle);
  538. }
  539. if (fabsf(x) > br->mask_stencil_dimension[0] || fabsf(y) > br->mask_stencil_dimension[1]) {
  540. zero_v4(rgba);
  541. return 0.0f;
  542. }
  543. x /= (br->mask_stencil_dimension[0]);
  544. y /= (br->mask_stencil_dimension[1]);
  545. co[0] = x;
  546. co[1] = y;
  547. co[2] = 0.0f;
  548. externtex(mtex, co, &intensity,
  549. rgba, rgba + 1, rgba + 2, rgba + 3, thread, pool, false, false);
  550. }
  551. else {
  552. float rotation = -mtex->rot;
  553. float point_2d[2] = {point[0], point[1]};
  554. float x = 0.0f, y = 0.0f; /* Quite warnings */
  555. float invradius = 1.0f; /* Quite warnings */
  556. float co[3];
  557. if (mtex->brush_map_mode == MTEX_MAP_MODE_VIEW) {
  558. /* keep coordinates relative to mouse */
  559. rotation += ups->brush_rotation_sec;
  560. x = point_2d[0] - ups->mask_tex_mouse[0];
  561. y = point_2d[1] - ups->mask_tex_mouse[1];
  562. /* use pressure adjusted size for fixed mode */
  563. invradius = 1.0f / ups->pixel_radius;
  564. }
  565. else if (mtex->brush_map_mode == MTEX_MAP_MODE_TILED) {
  566. /* leave the coordinates relative to the screen */
  567. /* use unadjusted size for tiled mode */
  568. invradius = 1.0f / BKE_brush_size_get(scene, br);
  569. x = point_2d[0];
  570. y = point_2d[1];
  571. }
  572. else if (mtex->brush_map_mode == MTEX_MAP_MODE_RANDOM) {
  573. rotation += ups->brush_rotation_sec;
  574. /* these contain a random coordinate */
  575. x = point_2d[0] - ups->mask_tex_mouse[0];
  576. y = point_2d[1] - ups->mask_tex_mouse[1];
  577. invradius = 1.0f / ups->pixel_radius;
  578. }
  579. x *= invradius;
  580. y *= invradius;
  581. /* it is probably worth optimizing for those cases where
  582. * the texture is not rotated by skipping the calls to
  583. * atan2, sqrtf, sin, and cos. */
  584. if (rotation > 0.001f || rotation < -0.001f) {
  585. const float angle = atan2f(y, x) + rotation;
  586. const float flen = sqrtf(x * x + y * y);
  587. x = flen * cosf(angle);
  588. y = flen * sinf(angle);
  589. }
  590. co[0] = x;
  591. co[1] = y;
  592. co[2] = 0.0f;
  593. externtex(mtex, co, &intensity,
  594. rgba, rgba + 1, rgba + 2, rgba + 3, thread, pool, false, false);
  595. }
  596. CLAMP(intensity, 0.0f, 1.0f);
  597. switch (br->mask_pressure) {
  598. case BRUSH_MASK_PRESSURE_CUTOFF:
  599. intensity = ((1.0f - intensity) < ups->size_pressure_value) ? 1.0f : 0.0f;
  600. break;
  601. case BRUSH_MASK_PRESSURE_RAMP:
  602. intensity = ups->size_pressure_value + intensity * (1.0f - ups->size_pressure_value);
  603. break;
  604. default:
  605. break;
  606. }
  607. return intensity;
  608. }
  609. /* Unified Size / Strength / Color */
  610. /* XXX: be careful about setting size and unprojected radius
  611. * because they depend on one another
  612. * these functions do not set the other corresponding value
  613. * this can lead to odd behavior if size and unprojected
  614. * radius become inconsistent.
  615. * the biggest problem is that it isn't possible to change
  616. * unprojected radius because a view context is not
  617. * available. my usual solution to this is to use the
  618. * ratio of change of the size to change the unprojected
  619. * radius. Not completely convinced that is correct.
  620. * In any case, a better solution is needed to prevent
  621. * inconsistency. */
  622. const float *BKE_brush_color_get(const struct Scene *scene, const struct Brush *brush)
  623. {
  624. UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
  625. return (ups->flag & UNIFIED_PAINT_COLOR) ? ups->rgb : brush->rgb;
  626. }
  627. const float *BKE_brush_secondary_color_get(const struct Scene *scene, const struct Brush *brush)
  628. {
  629. UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
  630. return (ups->flag & UNIFIED_PAINT_COLOR) ? ups->secondary_rgb : brush->secondary_rgb;
  631. }
  632. void BKE_brush_color_set(struct Scene *scene, struct Brush *brush, const float color[3])
  633. {
  634. UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
  635. if (ups->flag & UNIFIED_PAINT_COLOR)
  636. copy_v3_v3(ups->rgb, color);
  637. else
  638. copy_v3_v3(brush->rgb, color);
  639. }
  640. void BKE_brush_size_set(Scene *scene, Brush *brush, int size)
  641. {
  642. UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
  643. /* make sure range is sane */
  644. CLAMP(size, 1, MAX_BRUSH_PIXEL_RADIUS);
  645. if (ups->flag & UNIFIED_PAINT_SIZE)
  646. ups->size = size;
  647. else
  648. brush->size = size;
  649. }
  650. int BKE_brush_size_get(const Scene *scene, const Brush *brush)
  651. {
  652. UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
  653. int size = (ups->flag & UNIFIED_PAINT_SIZE) ? ups->size : brush->size;
  654. return size;
  655. }
  656. int BKE_brush_use_locked_size(const Scene *scene, const Brush *brush)
  657. {
  658. const short us_flag = scene->toolsettings->unified_paint_settings.flag;
  659. return (us_flag & UNIFIED_PAINT_SIZE) ?
  660. (us_flag & UNIFIED_PAINT_BRUSH_LOCK_SIZE) :
  661. (brush->flag & BRUSH_LOCK_SIZE);
  662. }
  663. int BKE_brush_use_size_pressure(const Scene *scene, const Brush *brush)
  664. {
  665. const short us_flag = scene->toolsettings->unified_paint_settings.flag;
  666. return (us_flag & UNIFIED_PAINT_SIZE) ?
  667. (us_flag & UNIFIED_PAINT_BRUSH_SIZE_PRESSURE) :
  668. (brush->flag & BRUSH_SIZE_PRESSURE);
  669. }
  670. int BKE_brush_use_alpha_pressure(const Scene *scene, const Brush *brush)
  671. {
  672. const short us_flag = scene->toolsettings->unified_paint_settings.flag;
  673. return (us_flag & UNIFIED_PAINT_ALPHA) ?
  674. (us_flag & UNIFIED_PAINT_BRUSH_ALPHA_PRESSURE) :
  675. (brush->flag & BRUSH_ALPHA_PRESSURE);
  676. }
  677. bool BKE_brush_sculpt_has_secondary_color(const Brush *brush)
  678. {
  679. return ELEM(
  680. brush->sculpt_tool, SCULPT_TOOL_BLOB, SCULPT_TOOL_DRAW,
  681. SCULPT_TOOL_INFLATE, SCULPT_TOOL_CLAY, SCULPT_TOOL_CLAY_STRIPS,
  682. SCULPT_TOOL_PINCH, SCULPT_TOOL_CREASE, SCULPT_TOOL_LAYER,
  683. SCULPT_TOOL_FLATTEN, SCULPT_TOOL_FILL, SCULPT_TOOL_SCRAPE,
  684. SCULPT_TOOL_MASK);
  685. }
  686. void BKE_brush_unprojected_radius_set(Scene *scene, Brush *brush, float unprojected_radius)
  687. {
  688. UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
  689. if (ups->flag & UNIFIED_PAINT_SIZE)
  690. ups->unprojected_radius = unprojected_radius;
  691. else
  692. brush->unprojected_radius = unprojected_radius;
  693. }
  694. float BKE_brush_unprojected_radius_get(const Scene *scene, const Brush *brush)
  695. {
  696. UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
  697. return (ups->flag & UNIFIED_PAINT_SIZE) ?
  698. ups->unprojected_radius :
  699. brush->unprojected_radius;
  700. }
  701. void BKE_brush_alpha_set(Scene *scene, Brush *brush, float alpha)
  702. {
  703. UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
  704. if (ups->flag & UNIFIED_PAINT_ALPHA)
  705. ups->alpha = alpha;
  706. else
  707. brush->alpha = alpha;
  708. }
  709. float BKE_brush_alpha_get(const Scene *scene, const Brush *brush)
  710. {
  711. UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
  712. return (ups->flag & UNIFIED_PAINT_ALPHA) ? ups->alpha : brush->alpha;
  713. }
  714. float BKE_brush_weight_get(const Scene *scene, const Brush *brush)
  715. {
  716. UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
  717. return (ups->flag & UNIFIED_PAINT_WEIGHT) ? ups->weight : brush->weight;
  718. }
  719. void BKE_brush_weight_set(const Scene *scene, Brush *brush, float value)
  720. {
  721. UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
  722. if (ups->flag & UNIFIED_PAINT_WEIGHT)
  723. ups->weight = value;
  724. else
  725. brush->weight = value;
  726. }
  727. /* scale unprojected radius to reflect a change in the brush's 2D size */
  728. void BKE_brush_scale_unprojected_radius(float *unprojected_radius,
  729. int new_brush_size,
  730. int old_brush_size)
  731. {
  732. float scale = new_brush_size;
  733. /* avoid division by zero */
  734. if (old_brush_size != 0)
  735. scale /= (float)old_brush_size;
  736. (*unprojected_radius) *= scale;
  737. }
  738. /* scale brush size to reflect a change in the brush's unprojected radius */
  739. void BKE_brush_scale_size(
  740. int *r_brush_size,
  741. float new_unprojected_radius,
  742. float old_unprojected_radius)
  743. {
  744. float scale = new_unprojected_radius;
  745. /* avoid division by zero */
  746. if (old_unprojected_radius != 0)
  747. scale /= new_unprojected_radius;
  748. (*r_brush_size) = (int)((float)(*r_brush_size) * scale);
  749. }
  750. void BKE_brush_jitter_pos(const Scene *scene, Brush *brush, const float pos[2], float jitterpos[2])
  751. {
  752. float rand_pos[2];
  753. float spread;
  754. int diameter;
  755. do {
  756. rand_pos[0] = BLI_rng_get_float(brush_rng) - 0.5f;
  757. rand_pos[1] = BLI_rng_get_float(brush_rng) - 0.5f;
  758. } while (len_squared_v2(rand_pos) > SQUARE(0.5f));
  759. if (brush->flag & BRUSH_ABSOLUTE_JITTER) {
  760. diameter = 2 * brush->jitter_absolute;
  761. spread = 1.0;
  762. }
  763. else {
  764. diameter = 2 * BKE_brush_size_get(scene, brush);
  765. spread = brush->jitter;
  766. }
  767. /* find random position within a circle of diameter 1 */
  768. jitterpos[0] = pos[0] + 2 * rand_pos[0] * diameter * spread;
  769. jitterpos[1] = pos[1] + 2 * rand_pos[1] * diameter * spread;
  770. }
  771. void BKE_brush_randomize_texture_coords(UnifiedPaintSettings *ups, bool mask)
  772. {
  773. /* we multiply with brush radius as an optimization for the brush
  774. * texture sampling functions */
  775. if (mask) {
  776. ups->mask_tex_mouse[0] = BLI_rng_get_float(brush_rng) * ups->pixel_radius;
  777. ups->mask_tex_mouse[1] = BLI_rng_get_float(brush_rng) * ups->pixel_radius;
  778. }
  779. else {
  780. ups->tex_mouse[0] = BLI_rng_get_float(brush_rng) * ups->pixel_radius;
  781. ups->tex_mouse[1] = BLI_rng_get_float(brush_rng) * ups->pixel_radius;
  782. }
  783. }
  784. /* Uses the brush curve control to find a strength value */
  785. float BKE_brush_curve_strength(Brush *br, float p, const float len)
  786. {
  787. float strength;
  788. if (p >= len) return 0;
  789. else p = p / len;
  790. strength = curvemapping_evaluateF(br->curve, 0, p);
  791. return strength;
  792. }
  793. /* Uses the brush curve control to find a strength value between 0 and 1 */
  794. float BKE_brush_curve_strength_clamped(Brush *br, float p, const float len)
  795. {
  796. float strength = BKE_brush_curve_strength(br, p, len);
  797. CLAMP(strength, 0.0f, 1.0f);
  798. return strength;
  799. }
  800. /* TODO: should probably be unified with BrushPainter stuff? */
  801. unsigned int *BKE_brush_gen_texture_cache(Brush *br, int half_side, bool use_secondary)
  802. {
  803. unsigned int *texcache = NULL;
  804. MTex *mtex = (use_secondary) ? &br->mask_mtex : &br->mtex;
  805. float intensity;
  806. float rgba[4];
  807. int ix, iy;
  808. int side = half_side * 2;
  809. if (mtex->tex) {
  810. float x, y, step = 2.0 / side, co[3];
  811. texcache = MEM_callocN(sizeof(int) * side * side, "Brush texture cache");
  812. /* do normalized cannonical view coords for texture */
  813. for (y = -1.0, iy = 0; iy < side; iy++, y += step) {
  814. for (x = -1.0, ix = 0; ix < side; ix++, x += step) {
  815. co[0] = x;
  816. co[1] = y;
  817. co[2] = 0.0f;
  818. /* This is copied from displace modifier code */
  819. /* TODO(sergey): brush are always cacheing with CM enabled for now. */
  820. externtex(mtex, co, &intensity,
  821. rgba, rgba + 1, rgba + 2, rgba + 3, 0, NULL, false, false);
  822. ((char *)texcache)[(iy * side + ix) * 4] =
  823. ((char *)texcache)[(iy * side + ix) * 4 + 1] =
  824. ((char *)texcache)[(iy * side + ix) * 4 + 2] =
  825. ((char *)texcache)[(iy * side + ix) * 4 + 3] = (char)(intensity * 255.0f);
  826. }
  827. }
  828. }
  829. return texcache;
  830. }
  831. /**** Radial Control ****/
  832. struct ImBuf *BKE_brush_gen_radial_control_imbuf(Brush *br, bool secondary)
  833. {
  834. ImBuf *im = MEM_callocN(sizeof(ImBuf), "radial control texture");
  835. unsigned int *texcache;
  836. int side = 128;
  837. int half = side / 2;
  838. int i, j;
  839. curvemapping_initialize(br->curve);
  840. texcache = BKE_brush_gen_texture_cache(br, half, secondary);
  841. im->rect_float = MEM_callocN(sizeof(float) * side * side, "radial control rect");
  842. im->x = im->y = side;
  843. for (i = 0; i < side; ++i) {
  844. for (j = 0; j < side; ++j) {
  845. float magn = sqrtf(pow2f(i - half) + pow2f(j - half));
  846. im->rect_float[i * side + j] = BKE_brush_curve_strength_clamped(br, magn, half);
  847. }
  848. }
  849. /* Modulate curve with texture */
  850. if (texcache) {
  851. for (i = 0; i < side; ++i) {
  852. for (j = 0; j < side; ++j) {
  853. const int col = texcache[i * side + j];
  854. im->rect_float[i * side + j] *= (((char *)&col)[0] + ((char *)&col)[1] + ((char *)&col)[2]) / 3.0f / 255.0f;
  855. }
  856. }
  857. MEM_freeN(texcache);
  858. }
  859. return im;
  860. }