Blender V4.3
overlay_next_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
9#pragma once
10
13
14#include "ED_mball.hh"
15
16namespace blender::draw::overlay {
17
18class Metaballs {
20
21 private:
22 const SelectionType selection_type_;
23
24 PassSimple ps_ = {"MetaBalls"};
25
26 SphereOutlineInstanceBuf circle_buf_ = {selection_type_, "metaball_data_buf"};
27
28 public:
29 Metaballs(const SelectionType selection_type) : selection_type_(selection_type){};
30
32 {
33 circle_buf_.clear();
34 }
35
36 void edit_object_sync(const ObjectRef &ob_ref, Resources &res)
37 {
38 const Object *ob = ob_ref.object;
39 const MetaBall *mb = static_cast<MetaBall *>(ob->data);
40
41 const float *color;
42 const float *col_radius = res.theme_settings.color_mball_radius;
43 const float *col_radius_select = res.theme_settings.color_mball_radius_select;
44 const float *col_stiffness = res.theme_settings.color_mball_stiffness;
45 const float *col_stiffness_select = res.theme_settings.color_mball_stiffness_select;
46
47 int elem_num = 0;
49 const bool is_selected = (ml->flag & SELECT) != 0;
50 const bool is_scale_radius = (ml->flag & MB_SCALE_RAD) != 0;
51 const float stiffness_radius = ml->rad * atanf(ml->s) * 2.0f / math::numbers::pi;
52 const float3 position = float3(&ml->x);
53
54 const select::ID radius_id = res.select_id(ob_ref, MBALLSEL_RADIUS | elem_num);
55 color = (is_selected && is_scale_radius) ? col_radius_select : col_radius;
56 circle_buf_.append({ob->object_to_world(), position, ml->rad, color}, radius_id);
57
58 const select::ID stiff_id = res.select_id(ob_ref, MBALLSEL_STIFF | elem_num);
59 color = (is_selected && !is_scale_radius) ? col_stiffness_select : col_stiffness;
60 circle_buf_.append({ob->object_to_world(), position, stiffness_radius, color}, stiff_id);
61 elem_num += 1 << 16;
62 }
63 }
64
65 void object_sync(const ObjectRef &ob_ref, Resources &res, const State &state)
66 {
67 const Object *ob = ob_ref.object;
68 const MetaBall *mb = static_cast<MetaBall *>(ob->data);
69
70 const float4 &color = res.object_wire_color(ob_ref, state);
71 const select::ID select_id = res.select_id(ob_ref);
72
73 LISTBASE_FOREACH (MetaElem *, ml, &mb->elems) {
74 const float3 position = float3(&ml->x);
75 /* Draw radius only. */
76 circle_buf_.append({ob->object_to_world(), position, ml->rad, color}, select_id);
77 }
78 }
79
80 void end_sync(Resources &res, ShapeCache &shapes, const State &state)
81 {
82 ps_.init();
84 state.clipping_plane_count);
85 /* NOTE: Use armature sphere outline shader to have perspective correct outline instead of
86 * just a circle facing the camera. */
88 ps_.bind_ubo("globalsBlock", &res.globals_buf);
89 res.select_bind(ps_);
90
91 circle_buf_.end_sync(ps_, shapes.metaball_wire_circle.get());
92 }
93
94 void draw(Framebuffer &framebuffer, Manager &manager, View &view)
95 {
96 GPU_framebuffer_bind(framebuffer);
97 manager.submit(ps_, view);
98 }
99};
100
101} // namespace blender::draw::overlay
#define LISTBASE_FOREACH(type, var, list)
@ MB_SCALE_RAD
#define MBALLSEL_STIFF
Definition ED_mball.hh:62
#define MBALLSEL_RADIUS
Definition ED_mball.hh:63
void GPU_framebuffer_bind(GPUFrameBuffer *framebuffer)
void submit(PassSimple &pass, View &view)
void state_set(DRWState state, int clip_plane_count=0)
Definition draw_pass.hh:954
void bind_ubo(const char *name, GPUUniformBuf *buffer)
void shader_set(GPUShader *shader)
Definition draw_pass.hh:971
void edit_object_sync(const ObjectRef &ob_ref, Resources &res)
Metaballs(const SelectionType selection_type)
void object_sync(const ObjectRef &ob_ref, Resources &res, const State &state)
void draw(Framebuffer &framebuffer, Manager &manager, View &view)
void end_sync(Resources &res, ShapeCache &shapes, const State &state)
#define SELECT
#define atanf(x)
@ 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]
VecBase< float, 3 > float3
ListBase elems
ListBase * editelems
const float4 & object_wire_color(const ObjectRef &ob_ref, ThemeColorID theme_id) const
void end_sync(PassSimple::Sub &pass, gpu::Batch *shape)
void append(const InstanceDataT &data, select::ID select_id)
void select_bind(PassSimple &pass)
const ID select_id(const ObjectRef &ob_ref, uint sub_object_id=0)