Blender V5.3
overlay_particle.hh
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#pragma once
10
11#include "BKE_material.hh"
12#include "BKE_pointcache.h"
14#include "DNA_material_types.h"
15#include "DNA_particle_types.h"
16#include "ED_particle.hh"
17
18#include "draw_cache.hh"
19#include "draw_cache_impl.hh"
20#include "overlay_base.hh"
21
22namespace blender::draw::overlay {
23
29 private:
30 PassMain particle_ps_ = {"particle_ps_"};
31 PassMain::Sub *dot_ps_ = nullptr;
32 PassMain::Sub *shape_ps_ = nullptr;
33 PassMain::Sub *hair_ps_ = nullptr;
34
35 PassSimple edit_particle_ps_ = {"edit_particle_ps_"};
36 PassSimple::Sub *edit_vert_ps_ = nullptr;
37 PassSimple::Sub *edit_edge_ps_ = nullptr;
38
39 bool show_weight_ = false;
40 bool show_point_inner_ = false;
41 bool show_point_tip_ = false;
42
43 public:
44 void begin_sync(Resources &res, const State &state) final
45 {
46 enabled_ = state.is_space_v3d() && !state.skip_particles;
47
48 if (!enabled_) {
49 return;
50 }
51
52 const bool is_transform = (G.moving & G_TRANSFORM_OBJ) != 0;
53
54 const ParticleEditSettings *edit_settings = PE_settings(const_cast<Scene *>(state.scene));
55 if (edit_settings) {
56 show_weight_ = (edit_settings->brushtype == PE_BRUSH_WEIGHT);
57 show_point_inner_ = edit_settings->selectmode == SCE_SELECT_POINT;
58 show_point_tip_ = ELEM(edit_settings->selectmode, SCE_SELECT_POINT, SCE_SELECT_END);
59 }
60
61 {
62 auto &pass = particle_ps_;
63 pass.init();
64 pass.bind_ubo(OVERLAY_GLOBALS_SLOT, &res.globals_buf);
65 pass.bind_ubo(DRW_CLIPPING_UBO_SLOT, &res.clip_planes_buf);
67 state.clipping_plane_count);
68 res.select_bind(pass);
69 {
70 auto &sub = pass.sub("Dots");
71 sub.shader_set(res.shaders->particle_dot.get());
72 sub.bind_texture("weight_tx", res.weight_ramp_tx);
73 dot_ps_ = &sub;
74 }
75 {
76 auto &sub = pass.sub("Shapes");
77 sub.shader_set(res.shaders->particle_shape.get());
78 sub.bind_texture("weight_tx", res.weight_ramp_tx);
79 shape_ps_ = &sub;
80 }
81 {
82 auto &sub = pass.sub("Hair");
83 sub.shader_set(res.shaders->particle_hair.get());
84 sub.push_constant("color_type", state.v3d->shading.wire_color_type);
85 sub.push_constant("is_transform", is_transform);
86 hair_ps_ = &sub;
87 }
88 }
89
90 {
91 auto &pass = edit_particle_ps_;
92 pass.init();
93 pass.bind_ubo(OVERLAY_GLOBALS_SLOT, &res.globals_buf);
94 pass.bind_ubo(DRW_CLIPPING_UBO_SLOT, &res.clip_planes_buf);
96 state.clipping_plane_count);
97 res.select_bind(pass);
98 {
99 auto &sub = pass.sub("Dots");
100 sub.shader_set(res.shaders->particle_edit_vert.get());
101 sub.bind_texture("weight_tx", res.weight_ramp_tx);
102 sub.push_constant("use_weight", false);
103 sub.push_constant("use_grease_pencil", false);
104 edit_vert_ps_ = &sub;
105 }
106 {
107 auto &sub = pass.sub("Edges");
108 sub.shader_set(res.shaders->particle_edit_edge.get());
109 sub.bind_texture("weight_tx", res.weight_ramp_tx);
110 sub.push_constant("use_weight", show_weight_);
111 sub.push_constant("use_grease_pencil", false);
112 sub.push_constant("skip_unselected", false);
113 edit_edge_ps_ = &sub;
114 }
115 }
116 }
117
119 const ObjectRef &ob_ref,
120 Resources & /*res*/,
121 const State &state) final
122 {
123 if (!enabled_) {
124 return;
125 }
126
127 /* Usually the edit structure is created by Particle Edit Mode Toggle
128 * operator, but sometimes it's invoked after tagging hair as outdated
129 * (for example, when toggling edit mode). That makes it impossible to
130 * create edit structure for until after next dependency graph evaluation.
131 *
132 * Ideally, the edit structure will be created here already via some
133 * dependency graph callback or so, but currently trying to make it nicer
134 * only causes bad level calls and breaks design from the past.
135 */
136 Object *object_eval = ob_ref.object;
137 Object *object_orig = DEG_get_original(object_eval);
138 Scene *scene_orig = const_cast<Scene *>(DEG_get_original(state.scene));
139 PTCacheEdit *edit = PE_create_current(state.depsgraph, scene_orig, object_orig);
140 if (edit == nullptr) {
141 /* Happens when trying to edit particles in EMITTER mode without having them cached. */
142 return;
143 }
144
145 auto find_active_evaluated_psys =
146 [&](ListBaseWrapper<ParticleSystem> particle_systems_orig,
147 ListBaseWrapper<ParticleSystem> particle_systems_eval) -> ParticleSystem * {
148 int psys_index = 0;
149 for (ParticleSystem *psys_orig : particle_systems_orig) {
150 if (PE_get_current_from_psys(psys_orig) == edit) {
151 return particle_systems_eval.get(psys_index);
152 }
153 psys_index++;
154 }
155 return nullptr;
156 };
157
158 ParticleSystem *psys = find_active_evaluated_psys(&object_orig->particlesystem,
159 &object_eval->particlesystem);
160 if (psys == nullptr) {
161 printf("Error getting evaluated particle system for edit.\n");
162 return;
163 }
164
165 Object *ob = ob_ref.object;
166
167 ResourceHandleRange handle = manager.resource_handle_for_psys(ob_ref,
168 ob_ref.particles_matrix());
169
170 {
171 gpu::Batch *geom = DRW_cache_particles_get_edit_strands(ob, psys, edit, show_weight_);
172 edit_edge_ps_->draw(geom, handle);
173 }
174 if (show_point_inner_) {
175 gpu::Batch *geom = DRW_cache_particles_get_edit_inner_points(ob, psys, edit);
176 edit_vert_ps_->draw(geom, handle);
177 }
178 if (show_point_tip_) {
179 gpu::Batch *geom = DRW_cache_particles_get_edit_tip_points(ob, psys, edit);
180 edit_vert_ps_->draw(geom, handle);
181 }
182 }
183
184 void object_sync(Manager &manager,
185 const ObjectRef &ob_ref,
186 Resources &res,
187 const State &state) final
188 {
189 if (!enabled_) {
190 return;
191 }
192
193 Object *ob = ob_ref.object;
194
195 ResourceHandleRange handle = {};
196
199 continue;
200 }
201
202 const ParticleSettings *part = psys->part;
203 const int draw_as = (part->draw_as == PART_DRAW_REND) ? part->ren_as : part->draw_as;
204
206 /* Skip early, before we try to create a handle, since we can't create these for ObjectRefs
207 * with instances. Objects and groups are realized by the Depsgraph as regular instances
208 * and therefore support instancing optimizations, but don't need to be handled here. */
209 continue;
210 }
211
212 if (!handle.is_valid()) {
213 handle = manager.resource_handle_for_psys(ob_ref, ob_ref.particles_matrix());
214 }
215
216 auto set_color = [&](PassMain::Sub &sub) {
217 /* NOTE(fclem): Is color even useful in our modern context? */
219 sub.push_constant("ucolor", float4(ma ? float3(&ma->r) : float3(0.6f), part->draw_size));
220 };
221
222 gpu::Batch *geom = nullptr;
223 switch (draw_as) {
224 case PART_DRAW_PATH:
225 if ((state.is_wireframe_mode == false) && (part->draw_as == PART_DRAW_REND)) {
226 /* Render engine should have rendered it already. */
227 break;
228 }
229 geom = DRW_cache_particles_get_hair(ob, psys, nullptr);
230 hair_ps_->push_constant("use_coloring", true); /* TODO */
231 hair_ps_->draw(geom, handle, res.select_id(ob_ref).get());
232 break;
233 case PART_DRAW_NOT:
234 /* Nothing to draw. */
235 break;
236 case PART_DRAW_OB:
237 case PART_DRAW_GR:
238 /* Instances are realized by Depsgraph and rendered as a regular object instance. */
239 break;
240 default:
241 /* Eventually, would be good to assert. But there are many other draw type that could be
242 * set and they need to revert to PART_DRAW_DOT. */
243 // BLI_assert_unreachable();
244 case PART_DRAW_DOT:
245 geom = DRW_cache_particles_get_dots(ob, psys);
246 set_color(*dot_ps_);
247 dot_ps_->draw(geom, handle, res.select_id(ob_ref).get());
248 break;
249 case PART_DRAW_AXIS:
250 geom = DRW_cache_particles_get_dots(ob, psys);
251 set_color(*shape_ps_);
252 shape_ps_->push_constant("shape_type", int(PART_SHAPE_AXIS));
253 shape_ps_->draw_expand(geom, GPU_PRIM_LINES, 3, 1, handle, res.select_id(ob_ref).get());
254 break;
255 case PART_DRAW_CIRC:
256 geom = DRW_cache_particles_get_dots(ob, psys);
257 set_color(*shape_ps_);
258 shape_ps_->push_constant("shape_type", int(PART_SHAPE_CIRCLE));
259 shape_ps_->draw_expand(geom,
262 1,
263 handle,
264 res.select_id(ob_ref).get());
265 break;
266 case PART_DRAW_CROSS:
267 geom = DRW_cache_particles_get_dots(ob, psys);
268 set_color(*shape_ps_);
269 shape_ps_->push_constant("shape_type", int(PART_SHAPE_CROSS));
270 shape_ps_->draw_expand(geom, GPU_PRIM_LINES, 3, 1, handle, res.select_id(ob_ref).get());
271 break;
272 }
273 }
274 }
275
276 void pre_draw(Manager &manager, View &view) final
277 {
278 if (!enabled_) {
279 return;
280 }
281
282 manager.generate_commands(particle_ps_, view);
283 }
284
285 void draw_line(Framebuffer &framebuffer, Manager &manager, View &view) final
286 {
287 if (!enabled_) {
288 return;
289 }
290
291 GPU_framebuffer_bind(framebuffer);
292 manager.submit_only(particle_ps_, view);
293 }
294
295 void draw(Framebuffer &framebuffer, Manager &manager, View &view) final
296 {
297 if (!enabled_) {
298 return;
299 }
300
301 GPU_framebuffer_bind(framebuffer);
302 manager.submit(edit_particle_ps_, view);
303 }
304};
305} // namespace blender::draw::overlay
General operations, lookup, etc. for materials.
#define ELEM(...)
detail::PassBase< command::DrawMultiBuf > Sub
Definition draw_pass.hh:509
void object_sync(Manager &manager, const ObjectRef &ob_ref, Resources &res, const State &state) final
void edit_object_sync(Manager &manager, const ObjectRef &ob_ref, Resources &, const State &state) final
void draw(Framebuffer &framebuffer, Manager &manager, View &view) final
void begin_sync(Resources &res, const State &state) final
void pre_draw(Manager &manager, View &view) final
void draw_line(Framebuffer &framebuffer, Manager &manager, View &view) final
#define DRW_CLIPPING_UBO_SLOT
#define OVERLAY_GLOBALS_SLOT
const int state
gpu::Batch * DRW_cache_particles_get_edit_inner_points(Object *object, ParticleSystem *psys, PTCacheEdit *edit)
detail::Pass< command::DrawCommandBuf > PassSimple
detail::Pass< command::DrawMultiBuf > PassMain
gpu::Batch * DRW_cache_particles_get_dots(Object *object, ParticleSystem *psys)
gpu::Batch * DRW_cache_particles_get_edit_tip_points(Object *object, ParticleSystem *psys, PTCacheEdit *edit)
gpu::Batch * DRW_cache_particles_get_hair(Object *object, ParticleSystem *psys, ModifierData *md)
gpu::Batch * DRW_cache_particles_get_edit_strands(Object *object, ParticleSystem *psys, PTCacheEdit *edit, bool use_weight)
void GPU_framebuffer_bind(gpu::FrameBuffer *fb)
PTCacheEdit * PE_get_current_from_psys(ParticleSystem *psys)
Global G
Definition blender.cc:57
ListBaseWrapperTemplate< ListBase, T > ListBaseWrapper
bool DRW_object_is_visible_psys_in_active_context(const Object *object, const ParticleSystem *psys)
@ DRW_STATE_WRITE_COLOR
Definition draw_state.hh:32
@ DRW_STATE_WRITE_DEPTH
Definition draw_state.hh:31
@ DRW_STATE_DEPTH_LESS_EQUAL
Definition draw_state.hh:40
T * DEG_get_original(T *id)
Material * BKE_object_material_get_eval(Object *ob, short act)
@ G_TRANSFORM_OBJ
PTCacheEdit * PE_create_current(Depsgraph *depsgraph, Scene *scene, Object *ob)
ParticleEditSettings * PE_settings(Scene *scene)
@ PART_SHAPE_CIRCLE
#define PARTICLE_SHAPE_CIRCLE_RESOLUTION
ListBaseT< struct ParticleSystem > particlesystem