Blender V5.0
overlay_metaball.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#pragma once
10
11#include "BKE_mball.hh"
12
13#include "ED_mball.hh"
14
15#include "overlay_base.hh"
16
17namespace blender::draw::overlay {
18
23 using SphereOutlineInstanceBuf = ShapeInstanceBuf<BoneInstanceData>;
24
25 private:
26 const SelectionType selection_type_;
27
28 PassSimple ps_ = {"MetaBalls"};
29
30 SphereOutlineInstanceBuf circle_buf_ = {selection_type_, "metaball_data_buf"};
31
32 public:
33 Metaballs(const SelectionType selection_type) : selection_type_(selection_type) {};
34
35 void begin_sync(Resources & /*res*/, const State & /*state*/) final
36 {
37 circle_buf_.clear();
38 }
39
40 void edit_object_sync(Manager & /*manager*/,
41 const ObjectRef &ob_ref,
42 Resources &res,
43 const State & /*state*/) final
44 {
45 const Object *ob = ob_ref.object;
47
48 const float *color;
49 const float *col_radius = res.theme.colors.mball_radius;
50 const float *col_radius_select = res.theme.colors.mball_radius_select;
51 const float *col_stiffness = res.theme.colors.mball_stiffness;
52 const float *col_stiffness_select = res.theme.colors.mball_stiffness_select;
53
54 int elem_num = 0;
56 const bool is_selected = (ml->flag & SELECT) != 0;
57 const bool is_scale_radius = (ml->flag & MB_SCALE_RAD) != 0;
59 const float3 position = float3(&ml->x);
60
61 const select::ID radius_id = res.select_id(ob_ref, MBALLSEL_RADIUS | elem_num);
62 color = (is_selected && is_scale_radius) ? col_radius_select : col_radius;
63 circle_buf_.append({ob->object_to_world(), position, radius_stiffness[0], color}, radius_id);
64
65 const select::ID stiff_id = res.select_id(ob_ref, MBALLSEL_STIFF | elem_num);
66 color = (is_selected && !is_scale_radius) ? col_stiffness_select : col_stiffness;
67 circle_buf_.append({ob->object_to_world(), position, radius_stiffness[1], color}, stiff_id);
68 elem_num += 1 << 16;
69 }
70 }
71
72 void object_sync(Manager & /*manager*/,
73 const ObjectRef &ob_ref,
74 Resources &res,
75 const State &state) final
76 {
77 const Object *ob = ob_ref.object;
79
80 const float4 &color = res.object_wire_color(ob_ref, state);
81 const select::ID select_id = res.select_id(ob_ref);
82
83 LISTBASE_FOREACH (MetaElem *, ml, &mb->elems) {
84 const float3 position = float3(&ml->x);
85 /* Draw radius only. */
86 const float radius = BKE_mball_element_display_radius_calc(ml);
87 circle_buf_.append({ob->object_to_world(), position, radius, color}, select_id);
88 }
89 }
90
91 void end_sync(Resources &res, const State &state) final
92 {
93 ps_.init();
95 state.clipping_plane_count);
96 /* NOTE: Use armature sphere outline shader to have perspective correct outline instead of
97 * just a circle facing the camera. */
98 ps_.shader_set(res.shaders->armature_sphere_outline.get());
99 ps_.bind_ubo(OVERLAY_GLOBALS_SLOT, &res.globals_buf);
100 ps_.bind_ubo(DRW_CLIPPING_UBO_SLOT, &res.clip_planes_buf);
101 ps_.push_constant("alpha", 1.0f);
102 res.select_bind(ps_);
103
104 circle_buf_.end_sync(ps_, res.shapes.metaball_wire_circle.get());
105 }
106
107 void draw_line(Framebuffer &framebuffer, Manager &manager, View &view) final
108 {
109 GPU_framebuffer_bind(framebuffer);
110 manager.submit(ps_, view);
111 }
112};
113
114} // namespace blender::draw::overlay
blender::float2 BKE_mball_element_display_radius_calc_with_stiffness(const MetaElem *ml)
Definition mball.cc:226
float BKE_mball_element_display_radius_calc(const MetaElem *ml)
Definition mball.cc:245
#define LISTBASE_FOREACH(type, var, list)
@ MB_SCALE_RAD
T & DRW_object_get_data_for_drawing(const Object &object)
#define MBALLSEL_STIFF
Definition ED_mball.hh:62
#define MBALLSEL_RADIUS
Definition ED_mball.hh:63
static AppView * view
void GPU_framebuffer_bind(blender::gpu::FrameBuffer *fb)
Metaballs(const SelectionType selection_type)
void end_sync(Resources &res, const State &state) final
void begin_sync(Resources &, const State &) final
void object_sync(Manager &, const ObjectRef &ob_ref, Resources &res, const State &state) final
void edit_object_sync(Manager &, const ObjectRef &ob_ref, Resources &res, const State &) final
void draw_line(Framebuffer &framebuffer, Manager &manager, View &view) final
#define SELECT
#define DRW_CLIPPING_UBO_SLOT
#define OVERLAY_GLOBALS_SLOT
@ DRW_STATE_WRITE_DEPTH
Definition draw_state.hh:29
@ DRW_STATE_WRITE_COLOR
Definition draw_state.hh:30
@ DRW_STATE_DEPTH_LESS_EQUAL
Definition draw_state.hh:38
static ulong state[N]
select::SelectionType SelectionType
detail::Pass< command::DrawCommandBuf > PassSimple
VecBase< float, 4 > float4
VecBase< float, 2 > float2
VecBase< float, 3 > float3
ListBase elems
ListBase * editelems