Blender V5.3
MOD_grease_pencil_texture.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_attribute.hh"
10#include "BLI_index_range.hh"
11#include "BLI_math_base.hh"
12#include "BLI_math_matrix.hh"
13#include "BLI_span.hh"
14
15#include "DNA_modifier_types.h"
16
17#include "BKE_curves.hh"
18#include "BKE_geometry_set.hh"
19#include "BKE_grease_pencil.hh"
20#include "BKE_instances.hh"
21#include "BKE_modifier.hh"
22#include "BKE_screen.hh"
23
24#include "BLO_read_write.hh"
25
27
29#include "UI_resources.hh"
30
31#include "BLT_translation.hh"
32
33#include "WM_api.hh"
34#include "WM_types.hh"
35
36#include "RNA_access.hh"
37#include "RNA_prototypes.hh"
38
40#include "MOD_ui_common.hh"
41
42namespace blender {
43
44static void init_data(ModifierData *md)
45{
46 auto *tmd = reinterpret_cast<GreasePencilTextureModifierData *>(md);
48 modifier::greasepencil::init_influence_data(&tmd->influence, false);
49}
50
51static void copy_data(const ModifierData *md, ModifierData *target, const int flag)
52{
53 const auto *tmd = reinterpret_cast<const GreasePencilTextureModifierData *>(md);
54 auto *tmmd = reinterpret_cast<GreasePencilTextureModifierData *>(target);
55
57
58 BKE_modifier_copydata_generic(md, target, flag);
59 modifier::greasepencil::copy_influence_data(&tmd->influence, &tmmd->influence, flag);
60}
61
62static void free_data(ModifierData *md)
63{
64 auto *tmd = reinterpret_cast<GreasePencilTextureModifierData *>(md);
66}
67
68static void foreach_ID_link(ModifierData *md, Object *ob, IDWalkFunc walk, void *user_data)
69{
70 auto *tmd = reinterpret_cast<GreasePencilTextureModifierData *>(md);
71 modifier::greasepencil::foreach_influence_ID_link(&tmd->influence, ob, walk, user_data);
72}
73
75 const IndexMask &curves_mask,
76 const float offset,
77 const float rotation,
78 const float scale,
79 const bool normalize_u)
80{
82 const OffsetIndices<int> points_by_curve = curves.points_by_curve();
83 const VArray<bool> cyclic = curves.cyclic();
84
85 bke::MutableAttributeAccessor attributes = curves.attributes_for_write();
86 bke::SpanAttributeWriter<float> u_translations = attributes.lookup_or_add_for_write_span<float>(
87 "u_translation", bke::AttrDomain::Curve);
89 "rotation", bke::AttrDomain::Point);
92 if (!u_translations || !rotations || !u_scales) {
93 return;
94 }
95
96 curves.ensure_evaluated_lengths();
97
98 curves_mask.foreach_index(
99 [&](int64_t curve_i) {
100 const IndexRange points = points_by_curve[curve_i];
101 const bool is_cyclic = cyclic[curve_i];
102 const Span<float> lengths = curves.evaluated_lengths_for_curve(curve_i, is_cyclic);
103 const float norm = normalize_u ? math::safe_rcp(lengths.last()) : 1.0f;
104
105 u_translations.span[curve_i] += offset;
106 u_scales.span[curve_i] *= scale * norm;
107 for (const int point_i : points) {
108 rotations.span[point_i] += rotation;
109 }
110 },
112
113 u_translations.finish();
114 u_scales.finish();
115 rotations.finish();
116}
117
118static float2 rotate_by_angle(const float2 &p, const float angle)
119{
120 const float cos_angle = math::cos(angle);
121 const float sin_angle = math::sin(angle);
122 return float2(p.x * cos_angle - p.y * sin_angle, p.x * sin_angle + p.y * cos_angle);
123}
124
125/*
126 * This gets the legacy stroke-space to layer-space matrix.
127 */
128static void get_legacy_stroke_matrix(const Span<float3> positions,
129 float3x4 &stroke_to_layer,
130 float4x3 &layer_to_stroke)
131{
132 using namespace blender::math;
133
134 if (positions.size() < 2) {
135 stroke_to_layer = float3x4::identity();
136 layer_to_stroke = float4x3::identity();
137 }
138
139 const float3 &pt0 = positions[0];
140 const float3 &pt1 = positions[1];
141 const float3 &pt3 = positions[int(positions.size() * 0.75f)];
142
143 /* Local X axis (p0 -> p1) */
144 const float3 local_x = normalize(pt1 - pt0);
145
146 /* Point vector at 3/4 */
147 const float3 local_3 = (positions.size() == 2) ? (pt3 * 0.001f) - pt0 : pt3 - pt0;
148
149 /* Vector orthogonal to polygon plane. */
150 const float3 normal = cross(local_x, local_3);
151
152 /* Local Y axis (cross to normal/x axis). */
153 const float3 local_y = normalize(cross(normal, local_x));
154
155 /* Get layer space using first point as origin. */
156 stroke_to_layer = float3x4(float4(local_x, 0), float4(local_y, 0), float4(pt0, 1));
157 layer_to_stroke = math::transpose(float3x4(float4(local_x, -dot(pt0, local_x)),
158 float4(local_y, -dot(pt0, local_y)),
159 float4(0, 0, 0, 1)));
160}
161
163 const IndexMask &curves_mask,
164 const float2 &offset,
165 const float rotation,
166 const float scale)
167{
168 /* Texture matrices are a combination of an unknown 3D transform into UV space, with a known 2D
169 * transform on top.
170 *
171 * However, the modifier offset is not applied directly to the UV transform, since it emulates
172 * legacy behavior of the GPv2 modifier, which applied translation first, before rotating about
173 * (0.5, 0.5) and scaling. To achieve the same result as the legacy modifier, the actual offset
174 * is calculated such that the result matches the GPv2 behavior.
175 *
176 * The canonical transform is
177 * uv = T + R / S * xy
178 *
179 * In terms of legacy variables TL, RL, SL the same transform is described as
180 * uv = (RL * (xy / 2 + TL) + 1/2) / SL
181 *
182 * where the 1/2 scaling factor and offset are the "bounds" transform and rotation center.
183 *
184 * Rearranging into canonical loc/rot/scale terms:
185 * uv = (RL * TL + 1/2) / SL + 1/2 * RL / SL * xy
186 * <=>
187 * T = (RL * TL + 1/2) / SL
188 * R = RL
189 * S = 2*SL
190 * <=>
191 * TL = 1/2 * R^T * (T * S - 1)
192 * RL = R
193 * SL = S/2
194 */
195
197 const Span<float3> positions = curves.positions();
198 Array<float4x2> texture_matrices(drawing.texture_matrices());
199
200 curves_mask.foreach_index(
201 [&](int64_t curve_i) {
202 const IndexRange points = curves.points_by_curve()[curve_i];
203 float4x2 &texture_matrix = texture_matrices[curve_i];
204 /* Factor out the stroke-to-layer transform part used by GPv2.
205 * This may not be the same as the transform used by GPv3 for concave shapes due to a
206 * simplistic normal calculation, but we want to achieve the same effect as GPv2 so have to
207 * use the same matrix. */
208 float3x4 stroke_to_layer;
209 float4x3 layer_to_stroke;
210 get_legacy_stroke_matrix(positions.slice(points), stroke_to_layer, layer_to_stroke);
211 const float3x2 uv_matrix = texture_matrix * stroke_to_layer;
212 const float2 uv_translation = uv_matrix[2];
213 float2 inv_uv_scale;
214 const float2 axis_u = math::normalize_and_get_length(uv_matrix[0], inv_uv_scale[0]);
215 const float2 axis_v = math::normalize_and_get_length(uv_matrix[1], inv_uv_scale[1]);
216 UNUSED_VARS(axis_v); /* `inv_uv_scale[1]` is used. */
217 const float uv_rotation = math::atan2(axis_u[1], axis_u[0]);
218 const float2 uv_scale = math::safe_rcp(inv_uv_scale);
219
220 const float2 legacy_uv_translation = rotate_by_angle(
221 0.5f * uv_scale * uv_translation - 0.5f, -uv_rotation);
222 const float legacy_uv_rotation = uv_rotation;
223 const float2 legacy_uv_scale = 0.5f * uv_scale;
224
225 const float2 legacy_uv_translation_new = legacy_uv_translation + offset;
226 const float legacy_uv_rotation_new = legacy_uv_rotation + rotation;
227 const float2 legacy_uv_scale_new = legacy_uv_scale * scale;
228
229 const float2 uv_translation_new =
230 (rotate_by_angle(legacy_uv_translation_new, legacy_uv_rotation_new) + 0.5f) *
231 math::safe_rcp(legacy_uv_scale_new);
232 const float uv_rotation_new = legacy_uv_rotation_new;
233 const float2 uv_scale_new = 2.0f * legacy_uv_scale_new;
234
235 const float cos_uv_rotation_new = math::cos(uv_rotation_new);
236 const float sin_uv_rotation_new = math::sin(uv_rotation_new);
237 const float2 inv_uv_scale_new = math::safe_rcp(uv_scale_new);
238 const float3x2 uv_matrix_new = float3x2(
239 inv_uv_scale_new[0] * float2(cos_uv_rotation_new, sin_uv_rotation_new),
240 inv_uv_scale_new[1] * float2(-sin_uv_rotation_new, cos_uv_rotation_new),
241 uv_translation_new);
242 texture_matrix = uv_matrix_new * layer_to_stroke;
243 },
245
246 drawing.set_texture_matrices(texture_matrices, curves_mask);
247}
248
250 const ModifierEvalContext &ctx,
252{
253 IndexMaskMemory mask_memory;
255 ctx.object, drawing.strokes(), tmd.influence, mask_memory);
256
257 const bool normalize_u = (tmd.fit_method == MOD_GREASE_PENCIL_TEXTURE_FIT_STROKE);
261 drawing, curves_mask, tmd.uv_offset, tmd.alignment_rotation, tmd.uv_scale, normalize_u);
262 break;
265 drawing, curves_mask, tmd.fill_offset, tmd.fill_rotation, tmd.fill_scale);
266 break;
269 drawing, curves_mask, tmd.uv_offset, tmd.alignment_rotation, tmd.uv_scale, normalize_u);
271 drawing, curves_mask, tmd.fill_offset, tmd.fill_rotation, tmd.fill_scale);
272 break;
273 }
274}
275
277 const ModifierEvalContext *ctx,
278 bke::GeometrySet *geometry_set)
279{
282
283 const auto &tmd = *reinterpret_cast<const GreasePencilTextureModifierData *>(md);
284
285 if (!geometry_set->has_grease_pencil()) {
286 return;
287 }
289
290 IndexMaskMemory mask_memory;
292 grease_pencil, tmd.influence, mask_memory);
293 const int frame = grease_pencil.runtime->eval_frame;
295 grease_pencil, layer_mask, frame);
297 [&](Drawing *drawing) { modify_curves(tmd, *ctx, *drawing); });
298}
299
300static void panel_draw(const bContext *C, Panel *panel)
301{
302 ui::Layout &layout = *panel->layout;
303
304 PointerRNA ob_ptr;
306 const auto &tmd = *static_cast<GreasePencilTextureModifierData *>(ptr->data);
307 const auto mode = GreasePencilTextureModifierMode(tmd.mode);
308
309 layout.use_property_split_set(true);
310
311 layout.prop(ptr, "mode", UI_ITEM_NONE, std::nullopt, ICON_NONE);
312
314 ui::Layout &col = layout.column(false);
315 col.prop(ptr, "fit_method", UI_ITEM_NONE, IFACE_("Stroke Fit Method"), ICON_NONE);
316 col.prop(ptr, "uv_offset", UI_ITEM_NONE, std::nullopt, ICON_NONE);
317 col.prop(ptr, "alignment_rotation", UI_ITEM_NONE, std::nullopt, ICON_NONE);
318 col.prop(ptr, "uv_scale", UI_ITEM_NONE, IFACE_("Scale"), ICON_NONE);
319 }
320
322 layout.separator();
323 }
324
326 ui::Layout &col = layout.column(false);
327 col.prop(ptr, "fill_rotation", UI_ITEM_NONE, std::nullopt, ICON_NONE);
328 col.prop(ptr, "fill_offset", UI_ITEM_NONE, IFACE_("Offset"), ICON_NONE);
329 col.prop(ptr, "fill_scale", UI_ITEM_NONE, IFACE_("Scale"), ICON_NONE);
330 }
331
332 if (ui::Layout *influence_panel = layout.panel_prop(
333 C, ptr, "open_influence_panel", IFACE_("Influence")))
334 {
337 }
338
339 modifier_error_message_draw(layout, ptr);
340}
341
346
347static void blend_write(BlendWriter *writer, const ID * /*id_owner*/, const ModifierData *md)
348{
349 const auto *tmd = reinterpret_cast<const GreasePencilTextureModifierData *>(md);
350
351 writer->write_struct(tmd);
352 modifier::greasepencil::write_influence_data(writer, &tmd->influence);
353}
354
355static void blend_read(BlendDataReader *reader, ModifierData *md)
356{
357 auto *tmd = reinterpret_cast<GreasePencilTextureModifierData *>(md);
358
359 modifier::greasepencil::read_influence_data(reader, &tmd->influence);
360}
361
363 /*idname*/ "GreasePencilTexture",
364 /*name*/ N_("TextureMapping"),
365 /*struct_name*/ "GreasePencilTextureModifierData",
366 /*struct_size*/ sizeof(GreasePencilTextureModifierData),
367 /*srna*/ &RNA_GreasePencilTextureModifier,
371 /*icon*/ ICON_MOD_UVPROJECT,
372
373 /*copy_data*/ copy_data,
374
375 /*deform_verts*/ nullptr,
376 /*deform_matrices*/ nullptr,
377 /*deform_verts_EM*/ nullptr,
378 /*deform_matrices_EM*/ nullptr,
379 /*modify_mesh*/ nullptr,
380 /*modify_geometry_set*/ modify_geometry_set,
381
382 /*init_data*/ init_data,
383 /*required_data_mask*/ nullptr,
384 /*free_data*/ free_data,
385 /*is_disabled*/ nullptr,
386 /*update_depsgraph*/ nullptr,
387 /*depends_on_time*/ nullptr,
388 /*depends_on_normals*/ nullptr,
389 /*foreach_ID_link*/ foreach_ID_link,
390 /*foreach_tex_link*/ nullptr,
391 /*free_runtime_data*/ nullptr,
392 /*panel_register*/ panel_register,
393 /*blend_write*/ blend_write,
394 /*blend_read*/ blend_read,
395};
396
397} // namespace blender
Low-level operations for curves.
Low-level operations for grease pencil.
#define IFACE_(msgid)
#define C
Definition RandGen.cpp:29
#define UI_ITEM_NONE
#define UNUSED_VARS(...)
#define INIT_DEFAULT_STRUCT_AFTER(struct_dst, member)
#define ELEM(...)
SIMD_FORCE_INLINE btScalar norm() const
Return the norm (length) of the vector.
Definition btVector3.h:263
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
constexpr Span slice(int64_t start, int64_t size) const
Definition BLI_span.hh:141
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr const T & last(const int64_t n=0) const
Definition BLI_span.hh:329
GSpanAttributeWriter lookup_or_add_for_write_span(StringRef name, AttrDomain domain, AttrType data_type, const AttributeInit &initializer=AttributeInitDefaultValue())
Span< float4x2 > texture_matrices() const
bke::CurvesGeometry & strokes_for_write()
const bke::CurvesGeometry & strokes() const
void set_texture_matrices(Span< float4x2 > matrices, const IndexMask &selection)
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
VecBase< float, D > normalize(VecOp< float, D >) RET
float dot(VecOp< float, D >, VecOp< float, D >) RET
VecBase< float, 3 > cross(VecOp< float, 3 >, VecOp< float, 3 >) RET
float3x3 scale(float3x3 mat, float2 scale)
constexpr ParallelGrainSize grain_size(int grain_size)
T cos(const AngleRadianBase< T > &a)
MatBase< T, NumCol, NumRow > transpose(const MatBase< T, NumRow, NumCol > &mat)
T safe_rcp(const T &a)
QuaternionBase< T > normalize_and_get_length(const QuaternionBase< T > &q, T &out_length)
T atan2(const T &y, const T &x)
T sin(const AngleRadianBase< T > &a)
void read_influence_data(BlendDataReader *reader, GreasePencilModifierInfluenceData *influence_data)
void draw_material_filter_settings(const bContext *, ui::Layout &layout, PointerRNA *ptr)
void init_influence_data(GreasePencilModifierInfluenceData *influence_data, const bool has_custom_curve)
void write_influence_data(BlendWriter *writer, const GreasePencilModifierInfluenceData *influence_data)
void draw_layer_filter_settings(const bContext *, ui::Layout &layout, PointerRNA *ptr)
IndexMask get_filtered_layer_mask(const GreasePencil &grease_pencil, const GreasePencilModifierInfluenceData &influence_data, IndexMaskMemory &memory)
Vector< bke::greasepencil::Drawing * > get_drawings_for_write(GreasePencil &grease_pencil, const IndexMask &layer_mask, const int frame)
IndexMask get_filtered_stroke_mask(const Object *ob, const bke::CurvesGeometry &curves, const GreasePencilModifierInfluenceData &influence_data, IndexMaskMemory &memory)
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 parallel_for_each(Range &&range, const Function &function)
Definition BLI_task.hh:31
BMesh Iterators.
static void copy_data(const ModifierData *md, ModifierData *target, const int flag)
__int64 int64_t
Definition BKE_text.h:90
static void blend_write(BlendWriter *writer, const ID *, const ModifierData *md)
Definition MOD_bevel.cc:454
PointerRNA * modifier_panel_get_property_pointers(Panel *panel, PointerRNA *r_ob_ptr)
static float2 rotate_by_angle(const float2 &p, const float angle)
static void modify_curves(ModifierData &md, const ModifierEvalContext &ctx, Drawing &drawing, bke::GreasePencilDrawingEditHints *edit_hints)
@ eModifierType_GreasePencilTexture
static void init_data(ModifierData *md)
void(*)(void *user_data, Object *ob, ID **idpoin, LibraryForeachIDCallbackFlag cb_flag) IDWalkFunc
static void foreach_ID_link(ModifierData *md, Object *ob, IDWalkFunc walk, void *user_data)
@ MOD_GREASE_PENCIL_TEXTURE_FILL
@ MOD_GREASE_PENCIL_TEXTURE_STROKE_AND_FILL
@ MOD_GREASE_PENCIL_TEXTURE_STROKE
static void write_fill_transforms(bke::greasepencil::Drawing &drawing, const IndexMask &curves_mask, const float2 &offset, const float rotation, const float scale)
ModifierTypeInfo modifierType_GreasePencilTexture
MatBase< float, 3, 4 > float3x4
VecBase< float, 4 > float4
static void modify_geometry_set(ModifierData *md, const ModifierEvalContext *ctx, bke::GeometrySet *geometry_set)
void modifier_error_message_draw(ui::Layout &layout, PointerRNA *ptr)
VecBase< float, 2 > float2
static void free_data(ModifierData *md)
MatBase< float, 4, 2 > float4x2
static void panel_register(ARegionType *region_type)
MatBase< float, 3, 2 > float3x2
static void write_stroke_transforms(bke::greasepencil::Drawing &drawing, const IndexMask &curves_mask, const float offset, const float rotation, const float scale, const bool normalize_u)
MatBase< float, 4, 3 > float4x3
@ MOD_GREASE_PENCIL_TEXTURE_FIT_STROKE
@ eModifierTypeFlag_SupportsMapping
@ eModifierTypeFlag_AcceptsGreasePencil
@ eModifierTypeFlag_EnableInEditmode
@ eModifierTypeFlag_SupportsEditmode
static void get_legacy_stroke_matrix(const Span< float3 > positions, float3x4 &stroke_to_layer, float4x3 &layer_to_stroke)
static void blend_read(BlendDataReader *, ModifierData *md)
PanelType * modifier_panel_register(ARegionType *region_type, ModifierType type, PanelDrawFn draw)
static void panel_draw(const bContext *, Panel *panel)
VecBase< float, 3 > float3
void BKE_modifier_copydata_generic(const ModifierData *md, ModifierData *md_dst, int flag)
void write_struct(const T *data, BlendStructWriterTypedFn< T > fn=nullptr)
GreasePencilTextureModifierFit fit_method
GreasePencilModifierInfluenceData influence
ui::Layout * layout
GreasePencil * get_grease_pencil_for_write()
void use_property_split_set(bool is_sep)
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)
Layout & column(bool align)
void separator(float factor=1.0f, LayoutSeparatorType type=LayoutSeparatorType::Auto)
PanelLayout panel_prop(const bContext *C, PointerRNA *open_prop_owner, StringRefNull open_prop_name)
#define N_(msgid)