Blender V4.3
MOD_grease_pencil_outline.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
9#include "BKE_attribute.hh"
10#include "BKE_material.h"
11#include "BLI_array_utils.hh"
13#include "BLI_index_range.hh"
14#include "BLI_math_matrix.hh"
15#include "BLI_math_vector.hh"
16#include "BLI_offset_indices.hh"
17#include "BLI_span.hh"
18#include "BLI_virtual_array.hh"
19
20#include "DNA_defaults.h"
21#include "DNA_modifier_types.h"
22#include "DNA_scene_types.h"
23
24#include "BKE_curves.hh"
25#include "BKE_geometry_set.hh"
26#include "BKE_grease_pencil.hh"
27#include "BKE_instances.hh"
28#include "BKE_lib_query.hh"
29#include "BKE_modifier.hh"
30#include "BKE_screen.hh"
31
32#include "BLO_read_write.hh"
33
35
36#include "ED_grease_pencil.hh"
37
39
40#include "UI_interface.hh"
41#include "UI_resources.hh"
42
43#include "BLT_translation.hh"
44
45#include "WM_api.hh"
46#include "WM_types.hh"
47
48#include "RNA_access.hh"
49#include "RNA_prototypes.hh"
50
52#include "MOD_ui_common.hh"
53
54namespace blender {
55
56static void init_data(ModifierData *md)
57{
58 auto *omd = reinterpret_cast<GreasePencilOutlineModifierData *>(md);
59
61
63 modifier::greasepencil::init_influence_data(&omd->influence, false);
64}
65
66static void copy_data(const ModifierData *md, ModifierData *target, const int flag)
67{
68 const auto *omd = reinterpret_cast<const GreasePencilOutlineModifierData *>(md);
69 auto *tmmd = reinterpret_cast<GreasePencilOutlineModifierData *>(target);
70
72
74 modifier::greasepencil::copy_influence_data(&omd->influence, &tmmd->influence, flag);
75}
76
77static void free_data(ModifierData *md)
78{
79 auto *omd = reinterpret_cast<GreasePencilOutlineModifierData *>(md);
81}
82
83static void foreach_ID_link(ModifierData *md, Object *ob, IDWalkFunc walk, void *user_data)
84{
85 auto *omd = reinterpret_cast<GreasePencilOutlineModifierData *>(md);
86 modifier::greasepencil::foreach_influence_ID_link(&omd->influence, ob, walk, user_data);
87 walk(user_data, ob, (ID **)&omd->outline_material, IDWALK_CB_USER);
88 walk(user_data, ob, (ID **)&omd->object, IDWALK_CB_NOP);
89}
90
92{
93 auto *omd = reinterpret_cast<GreasePencilOutlineModifierData *>(md);
94 if (ctx->scene->camera) {
96 ctx->node, ctx->scene->camera, DEG_OB_COMP_TRANSFORM, "Grease Pencil Outline Modifier");
98 ctx->node, ctx->scene->camera, DEG_OB_COMP_PARAMETERS, "Grease Pencil Outline Modifier");
99 }
100 if (omd->object != nullptr) {
102 ctx->node, omd->object, DEG_OB_COMP_TRANSFORM, "Grease Pencil Outline Modifier");
103 }
105 ctx->node, ctx->object, DEG_OB_COMP_TRANSFORM, "Grease Pencil Outline Modifier");
106}
107
114 const IndexMask &curve_selection,
115 const Span<int> curve_offsets)
116{
117 BLI_assert(curve_offsets.size() == src_curves.curves_num());
118
119 OffsetIndices<int> src_offsets = src_curves.points_by_curve();
120 bke::AttributeAccessor src_attributes = src_curves.attributes();
121
122 Array<int> indices(src_curves.points_num());
123 curve_selection.foreach_index(GrainSize(512), [&](const int64_t curve_i) {
124 const IndexRange points = src_offsets[curve_i];
125 const int point_num = points.size();
126 const int point_start = points.start();
127 MutableSpan<int> point_indices = indices.as_mutable_span().slice(points);
128 if (points.size() < 2) {
129 array_utils::fill_index_range(point_indices, point_start);
130 return;
131 }
132 /* Offset can be negative or larger than the buffer. Use modulo to get an
133 * equivalent offset within buffer size to simplify copying. */
134 const int offset_raw = curve_offsets[curve_i];
135 const int offset = offset_raw >= 0 ? offset_raw % points.size() :
136 points.size() - ((-offset_raw) % points.size());
137 BLI_assert(0 <= offset && offset < points.size());
138 if (offset == 0) {
139 array_utils::fill_index_range(point_indices, point_start);
140 return;
141 }
142
143 const int point_middle = point_start + offset;
144 array_utils::fill_index_range(point_indices.take_front(point_num - offset), point_middle);
145 array_utils::fill_index_range(point_indices.take_back(offset), point_start);
146 });
147
148 /* Have to make a copy of the input geometry, gather_attributes does not work in-place when the
149 * source indices are not ordered. */
150 bke::CurvesGeometry dst_curves(src_curves);
151 bke::MutableAttributeAccessor dst_attributes = dst_curves.attributes_for_write();
153 src_attributes, bke::AttrDomain::Point, bke::AttrDomain::Point, {}, indices, dst_attributes);
154
155 return dst_curves;
156}
157
158static int find_closest_point(const Span<float3> positions, const float3 &target)
159{
160 if (positions.is_empty()) {
161 return 0;
162 }
163
164 int closest_i = 0;
165 float min_dist_squared = math::distance_squared(positions.first(), target);
166 for (const int i : positions.index_range().drop_front(1)) {
167 const float dist_squared = math::distance_squared(positions[i], target);
168 if (dist_squared < min_dist_squared) {
169 closest_i = i;
170 min_dist_squared = dist_squared;
171 }
172 }
173 return closest_i;
174}
175
177 const ModifierEvalContext &ctx,
179 const float4x4 &viewmat)
180{
182
183 if (drawing.strokes().curve_num == 0) {
184 return;
185 }
186
187 /* Selected source curves. */
188 IndexMaskMemory curve_mask_memory;
190 ctx.object, drawing.strokes(), omd.influence, curve_mask_memory);
191
192 /* Unit object scale is applied to the stroke radius. */
193 const float object_scale = math::length(
194 math::transform_direction(ctx.object->object_to_world(), float3(M_SQRT1_3)));
195 /* Legacy thickness setting is diameter in pixels, divide by 2000 to get radius. */
196 const float radius = math::max(omd.thickness * object_scale, 1.0f) *
198 /* Offset the strokes by the radius so the outside aligns with the input stroke. */
199 const float outline_offset = (omd.flag & MOD_GREASE_PENCIL_OUTLINE_KEEP_SHAPE) != 0 ? -radius :
200 0.0f;
201 const int mat_nr = (omd.outline_material ?
203 -1);
204
206 drawing, curves_mask, viewmat, omd.subdiv, radius, outline_offset, mat_nr);
207
208 /* Cyclic curve reordering feature. */
209 if (omd.object) {
210 const OffsetIndices points_by_curve = curves.points_by_curve();
211
212 /* Computes the offset of the closest point to the object from the curve start. */
213 Array<int> offset_by_curve(curves.curves_num());
214 for (const int i : curves.curves_range()) {
215 const IndexRange points = points_by_curve[i];
216 /* Closest point index is already relative to the point range and can be used as offset. */
217 offset_by_curve[i] = find_closest_point(curves.positions().slice(points), omd.object->loc);
218 }
219
220 curves = reorder_cyclic_curve_points(curves, curves.curves_range(), offset_by_curve);
221 }
222
223 /* Resampling feature. */
224 if (omd.sample_length > 0.0f) {
226 curves.curves_num());
227 curves = geometry::resample_to_length(curves, curves.curves_range(), sample_lengths);
228 }
229
230 drawing.strokes_for_write() = std::move(curves);
231 drawing.tag_topology_changed();
232}
233
235 const ModifierEvalContext *ctx,
236 bke::GeometrySet *geometry_set)
237{
241
242 const auto &omd = *reinterpret_cast<const GreasePencilOutlineModifierData *>(md);
243
244 const Scene *scene = DEG_get_evaluated_scene(ctx->depsgraph);
245 if (!scene->camera) {
246 return;
247 }
248 const float4x4 viewinv = scene->camera->world_to_object();
249
250 if (!geometry_set->has_grease_pencil()) {
251 return;
252 }
253 GreasePencil &grease_pencil = *geometry_set->get_grease_pencil_for_write();
254 const int frame = grease_pencil.runtime->eval_frame;
255
256 IndexMaskMemory mask_memory;
258 grease_pencil, omd.influence, mask_memory);
259
261 grease_pencil, layer_mask, frame);
262 threading::parallel_for_each(drawings, [&](const LayerDrawingInfo &info) {
263 const Layer &layer = grease_pencil.layer(info.layer_index);
264 const float4x4 viewmat = viewinv * layer.to_world_space(*ctx->object);
265 modify_drawing(omd, *ctx, *info.drawing, viewmat);
266 });
267}
268
269static void panel_draw(const bContext *C, Panel *panel)
270{
271 uiLayout *layout = panel->layout;
272
273 PointerRNA ob_ptr;
275
276 uiLayoutSetPropSep(layout, true);
277
278 uiItemR(layout, ptr, "thickness", UI_ITEM_NONE, nullptr, ICON_NONE);
279 uiItemR(layout, ptr, "use_keep_shape", UI_ITEM_NONE, nullptr, ICON_NONE);
280 uiItemR(layout, ptr, "subdivision", UI_ITEM_NONE, nullptr, ICON_NONE);
281 uiItemR(layout, ptr, "sample_length", UI_ITEM_NONE, nullptr, ICON_NONE);
282 uiItemR(layout, ptr, "outline_material", UI_ITEM_NONE, nullptr, ICON_NONE);
283 uiItemR(layout, ptr, "object", UI_ITEM_NONE, nullptr, ICON_NONE);
284
285 Scene *scene = CTX_data_scene(C);
286 if (scene->camera == nullptr) {
287 uiItemL(layout, RPT_("Outline requires an active camera"), ICON_ERROR);
288 }
289
290 if (uiLayout *influence_panel = uiLayoutPanelProp(
291 C, layout, ptr, "open_influence_panel", IFACE_("Influence")))
292 {
295 }
296
297 modifier_panel_end(layout, ptr);
298}
299
304
305static void blend_write(BlendWriter *writer, const ID * /*id_owner*/, const ModifierData *md)
306{
307 const auto *omd = reinterpret_cast<const GreasePencilOutlineModifierData *>(md);
308
310 modifier::greasepencil::write_influence_data(writer, &omd->influence);
311}
312
313static void blend_read(BlendDataReader *reader, ModifierData *md)
314{
315 auto *omd = reinterpret_cast<GreasePencilOutlineModifierData *>(md);
316
317 modifier::greasepencil::read_influence_data(reader, &omd->influence);
318}
319
320} // namespace blender
321
323 /*idname*/ "GreasePencilOutline",
324 /*name*/ N_("Outline"),
325 /*struct_name*/ "GreasePencilOutlineModifierData",
326 /*struct_size*/ sizeof(GreasePencilOutlineModifierData),
327 /*srna*/ &RNA_GreasePencilOutlineModifier,
331 /*icon*/ ICON_MOD_OUTLINE,
332
333 /*copy_data*/ blender::copy_data,
334
335 /*deform_verts*/ nullptr,
336 /*deform_matrices*/ nullptr,
337 /*deform_verts_EM*/ nullptr,
338 /*deform_matrices_EM*/ nullptr,
339 /*modify_mesh*/ nullptr,
340 /*modify_geometry_set*/ blender::modify_geometry_set,
341
342 /*init_data*/ blender::init_data,
343 /*required_data_mask*/ nullptr,
344 /*free_data*/ blender::free_data,
345 /*is_disabled*/ nullptr,
346 /*update_depsgraph*/ blender::update_depsgraph,
347 /*depends_on_time*/ nullptr,
348 /*depends_on_normals*/ nullptr,
349 /*foreach_ID_link*/ blender::foreach_ID_link,
350 /*foreach_tex_link*/ nullptr,
351 /*free_runtime_data*/ nullptr,
352 /*panel_register*/ blender::panel_register,
353 /*blend_write*/ blender::blend_write,
354 /*blend_read*/ blender::blend_read,
355};
Scene * CTX_data_scene(const bContext *C)
Low-level operations for curves.
Low-level operations for grease pencil.
@ IDWALK_CB_USER
@ IDWALK_CB_NOP
General operations, lookup, etc. for materials.
int BKE_object_material_index_get(Object *ob, const Material *ma)
void BKE_modifier_copydata_generic(const ModifierData *md, ModifierData *md_dst, int flag)
@ eModifierTypeFlag_SupportsMapping
@ eModifierTypeFlag_AcceptsGreasePencil
@ eModifierTypeFlag_EnableInEditmode
@ eModifierTypeFlag_SupportsEditmode
void(*)(void *user_data, Object *ob, ID **idpoin, int cb_flag) IDWalkFunc
#define BLI_assert(a)
Definition BLI_assert.h:50
#define M_SQRT1_3
#define MEMCMP_STRUCT_AFTER_IS_ZERO(struct_var, member)
#define MEMCPY_STRUCT_AFTER(struct_dst, struct_src, member)
#define BLO_write_struct(writer, struct_name, data_ptr)
#define RPT_(msgid)
#define IFACE_(msgid)
void DEG_add_object_relation(DepsNodeHandle *node_handle, Object *object, eDepsObjectComponentType component, const char *description)
@ DEG_OB_COMP_TRANSFORM
@ DEG_OB_COMP_PARAMETERS
Scene * DEG_get_evaluated_scene(const Depsgraph *graph)
#define DNA_struct_default_get(struct_name)
struct GreasePencilOutlineModifierData GreasePencilOutlineModifierData
@ MOD_GREASE_PENCIL_OUTLINE_KEEP_SHAPE
@ eModifierType_GreasePencilOutline
ModifierTypeInfo modifierType_GreasePencilOutline
void modifier_panel_end(uiLayout *layout, PointerRNA *ptr)
PanelType * modifier_panel_register(ARegionType *region_type, ModifierType type, PanelDrawFn draw)
PointerRNA * modifier_panel_get_property_pointers(Panel *panel, PointerRNA *r_ob_ptr)
void uiItemL(uiLayout *layout, const char *name, int icon)
void uiLayoutSetPropSep(uiLayout *layout, bool is_sep)
#define UI_ITEM_NONE
PanelLayout uiLayoutPanelProp(const bContext *C, uiLayout *layout, PointerRNA *open_prop_owner, const char *open_prop_name)
void uiItemR(uiLayout *layout, PointerRNA *ptr, const char *propname, eUI_Item_Flag flag, const char *name, int icon)
constexpr MutableSpan take_back(const int64_t n) const
Definition BLI_span.hh:641
constexpr MutableSpan take_front(const int64_t n) const
Definition BLI_span.hh:630
constexpr int64_t size() const
Definition BLI_span.hh:253
static VArray ForSingle(T value, const int64_t size)
OffsetIndices< int > points_by_curve() const
MutableAttributeAccessor attributes_for_write()
bke::CurvesGeometry & strokes_for_write()
const bke::CurvesGeometry & strokes() const
void foreach_index(Fn &&fn) const
static ushort indices[]
void fill_index_range(MutableSpan< T > span, const T start=0)
constexpr float LEGACY_RADIUS_CONVERSION_FACTOR
void gather_attributes(AttributeAccessor src_attributes, AttrDomain src_domain, AttrDomain dst_domain, const AttributeFilter &attribute_filter, const IndexMask &selection, MutableAttributeAccessor dst_attributes)
bke::CurvesGeometry create_curves_outline(const bke::greasepencil::Drawing &drawing, const IndexMask &strokes, const float4x4 &transform, const int corner_subdivisions, const float outline_radius, const float outline_offset, const int material_index)
CurvesGeometry resample_to_length(const CurvesGeometry &src_curves, const IndexMask &selection, const VArray< float > &sample_lengths, const ResampleCurvesOutputAttributeIDs &output_ids={})
T length(const VecBase< T, Size > &a)
T distance_squared(const VecBase< T, Size > &a, const VecBase< T, Size > &b)
T max(const T &a, const T &b)
VecBase< T, 3 > transform_direction(const MatBase< T, 3, 3 > &mat, const VecBase< T, 3 > &direction)
void read_influence_data(BlendDataReader *reader, GreasePencilModifierInfluenceData *influence_data)
void init_influence_data(GreasePencilModifierInfluenceData *influence_data, const bool has_custom_curve)
Vector< LayerDrawingInfo > get_drawing_infos_by_layer(GreasePencil &grease_pencil, const IndexMask &layer_mask, const int frame)
static IndexMask get_filtered_layer_mask(const GreasePencil &grease_pencil, const std::optional< StringRef > layer_name_filter, const std::optional< int > layer_pass_filter, const bool layer_filter_invert, const bool layer_pass_filter_invert, IndexMaskMemory &memory)
static IndexMask get_filtered_stroke_mask(const Object *ob, const bke::CurvesGeometry &curves, const Material *material_filter, const std::optional< int > material_pass_filter, const bool material_filter_invert, const bool material_pass_filter_invert, IndexMaskMemory &memory)
void write_influence_data(BlendWriter *writer, const GreasePencilModifierInfluenceData *influence_data)
void draw_material_filter_settings(const bContext *, uiLayout *layout, PointerRNA *ptr)
void draw_layer_filter_settings(const bContext *, uiLayout *layout, PointerRNA *ptr)
void free_influence_data(GreasePencilModifierInfluenceData *influence_data)
void foreach_influence_ID_link(GreasePencilModifierInfluenceData *influence_data, Object *ob, IDWalkFunc walk, void *user_data)
void copy_influence_data(const GreasePencilModifierInfluenceData *influence_data_src, GreasePencilModifierInfluenceData *influence_data_dst, const int)
void ensure_no_bezier_curves(Drawing &drawing)
void parallel_for_each(Range &&range, const Function &function)
Definition BLI_task.hh:58
static void copy_data(const ModifierData *md, ModifierData *target, const int flag)
static void blend_write(BlendWriter *writer, const ID *, const ModifierData *md)
static int find_closest_point(const Span< float3 > positions, const float3 &target)
static void init_data(ModifierData *md)
static void foreach_ID_link(ModifierData *md, Object *ob, IDWalkFunc walk, void *user_data)
static void panel_draw(const bContext *C, Panel *panel)
static bke::CurvesGeometry reorder_cyclic_curve_points(const bke::CurvesGeometry &src_curves, const IndexMask &curve_selection, const Span< int > curve_offsets)
static void modify_geometry_set(ModifierData *md, const ModifierEvalContext *ctx, bke::GeometrySet *geometry_set)
static void free_data(ModifierData *md)
static void panel_register(ARegionType *region_type)
static void modify_drawing(const GreasePencilArrayModifierData &mmd, const ModifierEvalContext &ctx, bke::greasepencil::Drawing &drawing)
static void update_depsgraph(ModifierData *md, const ModifierUpdateDepsgraphContext *ctx)
static void blend_read(BlendDataReader *reader, ModifierData *md)
__int64 int64_t
Definition stdint.h:89
GreasePencilModifierInfluenceData influence
GreasePencilRuntimeHandle * runtime
Definition DNA_ID.h:413
float loc[3]
struct uiLayout * layout
struct Object * camera
GreasePencil * get_grease_pencil_for_write()
#define N_(msgid)
PointerRNA * ptr
Definition wm_files.cc:4126
uint8_t flag
Definition wm_window.cc:138