Blender V5.0
grease_pencil_randomize.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BKE_brush.hh"
10#include "BKE_paint.hh"
11#include "BLI_noise.hh"
12#include "BLI_rand.hh"
13
14#include "BKE_colortools.hh"
15
16#include "ED_grease_pencil.hh"
17
18#include "DNA_brush_types.h"
19
21
23 const float stroke_factor,
24 const float distance,
25 const float radius,
26 const float pressure)
27{
28 const bool use_random = (settings.flag & GP_BRUSH_GROUP_RANDOM) != 0;
29 if (!use_random || !(settings.draw_random_press > 0.0f)) {
30 return radius;
31 }
32 float random_factor = 0.0f;
33 if ((settings.flag2 & GP_BRUSH_USE_PRESS_AT_STROKE) == 0) {
34 /* TODO: This should be exposed as a setting to scale the noise along the stroke. */
35 constexpr float noise_scale = 1 / 20.0f;
36 random_factor = noise::perlin_signed(float2(distance * noise_scale, stroke_factor));
37 }
38 else {
39 random_factor = stroke_factor;
40 }
41
42 if ((settings.flag2 & GP_BRUSH_USE_PRESSURE_RAND_PRESS) != 0) {
43 random_factor *= BKE_curvemapping_evaluateF(settings.curve_rand_pressure, 0, pressure);
44 }
45
46 const float randomized_radius = math::interpolate(
47 radius, radius * (1.0f + random_factor), settings.draw_random_press);
48 return math::max(randomized_radius, 0.0f);
49}
50
52 const float stroke_factor,
53 const float distance,
54 const float opacity,
55 const float pressure)
56{
57 const bool use_random = (settings.flag & GP_BRUSH_GROUP_RANDOM) != 0;
58 if (!use_random || !(settings.draw_random_strength > 0.0f)) {
59 return opacity;
60 }
61 float random_factor = 0.0f;
62 if ((settings.flag2 & GP_BRUSH_USE_STRENGTH_AT_STROKE) == 0) {
63 /* TODO: This should be exposed as a setting to scale the noise along the stroke. */
64 constexpr float noise_scale = 1 / 20.0f;
65 random_factor = noise::perlin_signed(float2(distance * noise_scale, stroke_factor));
66 }
67 else {
68 random_factor = stroke_factor;
69 }
70
71 if ((settings.flag2 & GP_BRUSH_USE_STRENGTH_RAND_PRESS) != 0) {
72 random_factor *= BKE_curvemapping_evaluateF(settings.curve_rand_strength, 0, pressure);
73 }
74
75 const float randomized_opacity = math::interpolate(
76 opacity, opacity + random_factor, settings.draw_random_strength);
77 return math::clamp(randomized_opacity, 0.0f, 1.0f);
78}
79
81 const float stroke_factor,
82 const float distance,
83 const float pressure)
84{
85 const bool use_random = (settings.flag & GP_BRUSH_GROUP_RANDOM) != 0;
86 if (!use_random || !(settings.uv_random > 0.0f)) {
87 return 0.0f;
88 }
89 float random_factor = 0.0f;
90 if ((settings.flag2 & GP_BRUSH_USE_UV_AT_STROKE) == 0) {
91 /* TODO: This should be exposed as a setting to scale the noise along the stroke. */
92 constexpr float noise_scale = 1 / 20.0f;
93 random_factor = noise::perlin_signed(float2(distance * noise_scale, stroke_factor));
94 }
95 else {
96 random_factor = stroke_factor;
97 }
98
99 if ((settings.flag2 & GP_BRUSH_USE_UV_RAND_PRESS) != 0) {
100 random_factor *= BKE_curvemapping_evaluateF(settings.curve_rand_uv, 0, pressure);
101 }
102
103 const float random_rotation = random_factor * math::numbers::pi;
104 return math::interpolate(0.0f, random_rotation, settings.uv_random);
105}
106
109 const float stroke_factor,
110 const float pressure)
111{
112 const bool use_random = (settings.flag & GP_BRUSH_GROUP_RANDOM) != 0;
113 if (!use_random || !(settings.uv_random > 0.0f)) {
114 return 0.0f;
115 }
116 float random_factor = 0.0f;
117 if ((settings.flag2 & GP_BRUSH_USE_UV_AT_STROKE) == 0) {
118 random_factor = rng.get_float() * 2.0f - 1.0f;
119 }
120 else {
121 random_factor = stroke_factor;
122 }
123
124 if ((settings.flag2 & GP_BRUSH_USE_UV_RAND_PRESS) != 0) {
125 random_factor *= BKE_curvemapping_evaluateF(settings.curve_rand_uv, 0, pressure);
126 }
127
128 const float random_rotation = random_factor * math::numbers::pi;
129 return math::interpolate(0.0f, random_rotation, settings.uv_random);
130}
131
133 const std::optional<BrushColorJitterSettings> &jitter,
134 const float stroke_hue_factor,
135 const float stroke_saturation_factor,
136 const float stroke_value_factor,
137 const float distance,
139 const float pressure)
140{
141 const bool use_random = (settings.flag & GP_BRUSH_GROUP_RANDOM) != 0;
142 if (!use_random || !jitter) {
143 return color;
144 }
145 blender::float3 initial_hsv_jitter = {
146 stroke_hue_factor, stroke_saturation_factor, stroke_value_factor};
147
149 *jitter, initial_hsv_jitter, distance, pressure, {color.r, color.g, color.b});
150
151 return {jittered[0], jittered[1], jittered[2], color.a};
152}
153
154} // namespace blender::ed::greasepencil
float BKE_curvemapping_evaluateF(const CurveMapping *cumap, int cur, float value)
blender::float3 BKE_paint_randomize_color(const BrushColorJitterSettings &color_jitter, const blender::float3 &initial_hsv_jitter, const float distance, const float pressure, const blender::float3 &color)
Definition paint.cc:1964
@ GP_BRUSH_USE_STRENGTH_RAND_PRESS
@ GP_BRUSH_USE_STRENGTH_AT_STROKE
@ GP_BRUSH_USE_UV_RAND_PRESS
@ GP_BRUSH_USE_PRESS_AT_STROKE
@ GP_BRUSH_USE_UV_AT_STROKE
@ GP_BRUSH_USE_PRESSURE_RAND_PRESS
@ GP_BRUSH_GROUP_RANDOM
float distance(VecOp< float, D >, VecOp< float, D >) RET
float randomize_rotation(const BrushGpencilSettings &settings, const float stroke_factor, const float distance, const float pressure)
float randomize_opacity(const BrushGpencilSettings &settings, const float stroke_factor, const float distance, const float opacity, const float pressure)
ColorGeometry4f randomize_color(const BrushGpencilSettings &settings, const std::optional< BrushColorJitterSettings > &jitter, const float stroke_hue_factor, const float stroke_saturation_factor, const float stroke_value_factor, const float distance, const ColorGeometry4f color, const float pressure)
float randomize_radius(const BrushGpencilSettings &settings, const float stroke_factor, const float distance, const float radius, const float pressure)
T clamp(const T &a, const T &min, const T &max)
T interpolate(const T &a, const T &b, const FactorT &t)
T max(const T &a, const T &b)
float perlin_signed(float position)
Definition noise.cc:614
VecBase< float, 2 > float2
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
VecBase< float, 3 > float3
struct CurveMapping * curve_rand_pressure
struct CurveMapping * curve_rand_strength
struct CurveMapping * curve_rand_uv