Blender V5.3
grease_pencil/paint_cursor.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
6
8
9#include "BLI_math_matrix.hh"
10#include "BLI_math_vector.hh"
11#include "BLI_rect.hh"
12
13#include "BKE_brush.hh"
14#include "BKE_grease_pencil.hh"
15#include "BKE_paint_types.hh"
16
18
19#include "GPU_immediate.hh"
20#include "GPU_immediate_util.hh"
21#include "GPU_state.hh"
22
23#include "ED_grease_pencil.hh"
24#include "ED_view3d.hh"
25
27
30 const float radius,
31 const float3 world_location,
32 const float4x4 &to_world)
33{
34 const float2 xy_delta = float2(1.0f, 0.0f);
35
36 bool z_flip;
37 const float zfac = ED_view3d_calc_zfac_ex(vc->rv3d, world_location, &z_flip);
38 if (z_flip) {
39 /* Location is behind camera. Return 0 to make the cursor disappear. */
40 return 0;
41 }
42 float3 delta;
43 ED_view3d_win_to_delta(vc->region, xy_delta, zfac, delta);
44
45 const float scale = math::length(
46 math::transform_direction(to_world, float3(std::numbers::inv_sqrt3)));
47 return math::safe_divide(scale * radius, math::length(delta));
48}
49
51{
52 float radius = float(pcontext.pixel_radius);
53
54 /* Red-ish color with alpha. */
55 immUniformColor4ub(255, 100, 100, 20);
56 imm_draw_circle_fill_2d(pcontext.pos, pcontext.mval.x, pcontext.mval.y, radius, 40);
57
59
61
62 float viewport_size[4];
63 GPU_viewport_size_get_f(viewport_size);
64 immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);
65
66 immUniformColor4f(1.0f, 0.39f, 0.39f, 0.78f);
67 immUniform1i("colors_len", 0); /* "simple" mode */
68 immUniform1f("dash_width", 12.0f);
69 immUniform1f("udash_factor", 0.5f);
70
71 /* XXX Dashed shader gives bad results with sets of small segments
72 * currently, temp hack around the issue. :( */
73 const int nsegments = max_ii(8, radius / 2);
74 imm_draw_circle_wire_2d(pcontext.pos, pcontext.mval.x, pcontext.mval.y, radius, nsegments);
75}
76
78{
79 if (pcontext.region &&
80 !BLI_rcti_isect_pt(&pcontext.region->winrct, pcontext.mval.x, pcontext.mval.y))
81 {
82 return;
83 }
84
85 Object *object = pcontext.object;
86 if (object->type != OB_GREASE_PENCIL) {
87 return;
88 }
89
90 GreasePencil *grease_pencil = id_cast<GreasePencil *>(object->data);
91 Paint *paint = pcontext.paint;
92 Brush *brush = pcontext.brush;
93 if ((brush == nullptr) || (brush->gpencil_settings == nullptr)) {
94 return;
95 }
96
97 float3 color(1.0f);
98 const int2 mval = pcontext.mval;
99
100 if (pcontext.mode == PaintMode::GPencil) {
101 /* Hide the cursor while drawing. */
102 if (grease_pencil->runtime->is_drawing_stroke) {
103 return;
104 }
105
106 /* Eraser has a special shape and uses a different shader program. */
107 if (brush->gpencil_brush_type == GPAINT_BRUSH_TYPE_ERASE ||
108 grease_pencil->runtime->temp_use_eraser)
109 {
110 /* If we use the eraser from the draw tool with a "scene" radius unit, we need to draw the
111 * cursor with the appropriate size. */
112 if (grease_pencil->runtime->temp_use_eraser) {
113 pcontext.pixel_radius = std::max(int(grease_pencil->runtime->temp_eraser_radius), 1);
114 }
115 else {
116 pcontext.pixel_radius = std::max(int(BKE_brush_radius_get(paint, brush)), 1);
117 }
119 return;
120 }
121
122 if (brush->gpencil_brush_type == GPAINT_BRUSH_TYPE_FILL) {
123 /* The fill tool doesn't use a brush size currently, but not showing any brush means that it
124 * can be hard to see where the cursor is. Use a fixed size that's not too big (10px). By
125 * disabling the "Display Cursor" option, this can still be turned off. */
126 pcontext.pixel_radius = 10;
127 }
128
129 if (brush->gpencil_brush_type == GPAINT_BRUSH_TYPE_TINT) {
130 pcontext.pixel_radius = std::max(int(BKE_brush_radius_get(paint, brush)), 1);
131 }
132
133 if (brush->gpencil_brush_type == GPAINT_BRUSH_TYPE_DRAW) {
134 if ((brush->flag & BRUSH_LOCK_SIZE) != 0) {
135 const bke::greasepencil::Layer *layer = grease_pencil->get_active_layer();
137 *pcontext.scene, *pcontext.region, *pcontext.vc.v3d, *object, layer);
138 const float2 coordinate = float2(pcontext.mval.x - pcontext.region->winrct.xmin,
139 pcontext.mval.y - pcontext.region->winrct.ymin);
140 bool clipped = false;
141 const float3 pos = placement.project(coordinate, clipped);
142 if (!clipped) {
143 const float3 world_location = math::transform_point(placement.to_world_space(), pos);
145 &pcontext.vc,
147 world_location,
148 placement.to_world_space());
149 }
150 else {
151 pcontext.pixel_radius = 0;
152 }
153 BKE_brush_size_set(paint, brush, std::max(pcontext.pixel_radius * 2, 1));
154 }
155 else {
157 }
158 }
159
160 /* Get current drawing material. */
162 MaterialGPencilStyle *gp_style = ma->gp_style;
163
164 /* Follow user settings for the size of the draw cursor:
165 * - Fixed size, or
166 * - Brush size (i.e. stroke thickness)
167 */
168 if ((gp_style) && ((brush->flag & BRUSH_SMOOTH_STROKE) == 0) &&
169 (brush->gpencil_brush_type == GPAINT_BRUSH_TYPE_DRAW))
170 {
171
173 pcontext.scene->toolsettings->gp_paint, brush);
174 const bool use_vertex_color_stroke = use_vertex_color;
175 if (use_vertex_color_stroke) {
178 }
179 else {
180 color = float4(gp_style->stroke_rgba).xyz();
181 }
182 }
183 }
184
185 if ((brush->flag & BRUSH_SMOOTH_STROKE) != 0) {
186 color = float3(1.0f, 0.4f, 0.4f);
187 }
188 }
189 else if (pcontext.mode == PaintMode::VertexGPencil) {
193 }
194
195 GPU_line_width(1.0f);
196 /* Inner Ring: Color from UI panel */
197 immUniformColor4f(color.x, color.y, color.z, 0.8f);
198 imm_draw_circle_wire_2d(pcontext.pos, mval.x, mval.y, pcontext.pixel_radius, 32);
199
200 /* Outer Ring: Dark color for contrast on light backgrounds (e.g. gray on white) */
201 const float3 darkcolor = color * 0.40f;
202 immUniformColor4f(darkcolor[0], darkcolor[1], darkcolor[2], 0.8f);
203 imm_draw_circle_wire_2d(pcontext.pos, mval.x, mval.y, pcontext.pixel_radius + 1, 32);
204}
205} // namespace blender::ed::sculpt_paint
Low-level operations for grease pencil.
float3 project(float2 co, bool &clipped) const
#define layer
float3x3 scale(float3x3 mat, float2 scale)
bool brush_using_vertex_color(const GpPaint *gp_paint, const Brush *brush)
static void grease_pencil_eraser_draw(PaintCursorContext &pcontext)
void grease_pencil_cursor_draw(PaintCursorContext &pcontext)
static int project_brush_radius_grease_pencil(ViewContext *vc, const float radius, const float3 world_location, const float4x4 &to_world)
T safe_divide(const T &a, const T &b)
T length(const VecBase< T, Size > &a)
VecBase< T, 3 > transform_direction(const MatBase< T, 3, 3 > &mat, const VecBase< T, 3 > &direction)
VecBase< T, 3 > transform_point(const CartesianBasis &basis, const VecBase< T, 3 > &v)
void immUniformColor4ub(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
void immUniform2f(const char *name, float x, float y)
float BKE_brush_radius_get(const Paint *paint, const Brush *brush)
Definition brush.cc:1366
MINLINE int max_ii(int a, int b)
MatBase< float, 4, 4 > float4x4
void immUnbindProgram()
void GPU_line_width(float width)
Definition gpu_state.cc:151
float BKE_brush_unprojected_radius_get(const Paint *paint, const Brush *brush)
Definition brush.cc:1412
VecBase< float, 4 > float4
void immBindBuiltinProgram(GPUBuiltinShader shader_id)
void imm_draw_circle_wire_2d(uint shdr_pos, float x, float y, float radius, int nsegments)
@ GPAINT_BRUSH_TYPE_ERASE
@ GPAINT_BRUSH_TYPE_FILL
@ GPAINT_BRUSH_TYPE_TINT
@ GPAINT_BRUSH_TYPE_DRAW
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
void immUniform1f(const char *name, float x)
void GPU_viewport_size_get_f(float coords[4])
Definition gpu_state.cc:258
@ GPU_SHADER_3D_LINE_DASHED_UNIFORM_COLOR
void immUniformColor4f(float r, float g, float b, float a)
void BKE_brush_size_set(Paint *paint, Brush *brush, int size)
Definition brush.cc:1340
float ED_view3d_calc_zfac_ex(const RegionView3D *rv3d, const float co[3], bool *r_flip)
Material * BKE_grease_pencil_object_material_from_brush_get(Object *ob, const Brush *brush)
float3 BKE_brush_color_get(const Paint *paint, const Brush *brush)
Definition brush.cc:1253
bool BLI_rcti_isect_pt(const struct rcti *rect, int x, int y)
void immUniform1i(const char *name, int x)
VecBase< float, 3 > float3
BLI_INLINE void IMB_colormanagement_scene_linear_to_srgb_v3(float srgb[3], const float scene_linear[3])
void imm_draw_circle_fill_2d(uint shdr_pos, float x, float y, float radius, int nsegments)
void ED_view3d_win_to_delta(const ARegion *region, const float xy_delta[2], float zfac, float r_out[3], bool precise=false)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
struct ToolSettings * toolsettings
RegionView3D * rv3d
Definition ED_view3d.hh:86