Blender V5.3
overlay_fade.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_modifier.hh"
12#include "BKE_paint.hh"
13
14#include "overlay_armature.hh"
15#include "overlay_base.hh"
16
17namespace blender::draw::overlay {
18
23class Fade : Overlay {
24 private:
25 PassMain ps_ = {"FadeGeometry"};
26
27 PassMain::Sub *mesh_fade_geometry_ps_;
28 /* Passes for Pose Fade Geometry. */
29 PassMain::Sub *armature_fade_geometry_active_ps_;
30 PassMain::Sub *armature_fade_geometry_other_ps_;
31
32 public:
33 void begin_sync(Resources &res, const State &state) final
34 {
35 const bool do_edit_mesh_fade_geom = !state.xray_enabled && state.show_fade_inactive();
36 enabled_ = state.is_space_v3d() && (do_edit_mesh_fade_geom || state.do_pose_fade_geom) &&
37 !res.is_selection();
38
39 if (!enabled_) {
40 /* Not used. But release the data. */
41 ps_.init();
42 return;
43 }
44
45 ps_.init();
47 state.clipping_plane_count);
48 ps_.shader_set(res.shaders->uniform_color.get());
49 ps_.bind_ubo(OVERLAY_GLOBALS_SLOT, &res.globals_buf);
50 ps_.bind_ubo(DRW_CLIPPING_UBO_SLOT, &res.clip_planes_buf);
51 {
52 PassMain::Sub &sub = ps_.sub("edit_mesh.fade");
53 float4 color = res.background_color_get(state);
54 color[3] = state.overlay.fade_alpha;
55 if (state.v3d->shading.background_type == V3D_SHADING_BACKGROUND_THEME) {
57 }
58 sub.push_constant("ucolor", color);
59 mesh_fade_geometry_ps_ = ⊂
60 }
61
62 /* Fade Geometry. */
63 if (state.do_pose_fade_geom) {
64 const float alpha = state.overlay.xray_alpha_bone;
65 float4 color = {0.0f, 0.0f, 0.0f, alpha};
66 {
67 auto &sub = ps_.sub("fade_geometry.active");
68 sub.push_constant("ucolor", color);
69 armature_fade_geometry_active_ps_ = ⊂
70 }
71 {
72 color[3] = powf(alpha, 4);
73 auto &sub = ps_.sub("fade_geometry");
74 sub.push_constant("ucolor", color);
75 armature_fade_geometry_other_ps_ = ⊂
76 }
77 }
78 }
79
80 void object_sync(Manager &manager,
81 const ObjectRef &ob_ref,
82 Resources & /*res*/,
83 const State &state) final
84 {
85 if (!enabled_) {
86 return;
87 }
88 const Object *ob = ob_ref.object;
89 const bool renderable = DRW_object_is_renderable(ob);
90 const bool draw_surface = (ob->dt >= OB_WIRE) && (renderable || (ob->dt == OB_WIRE));
91 const bool draw_fade = draw_surface && overlay_should_fade_object(ob, state.object_active);
92
93 const bool draw_bone_selection = (ob_ref.object->type == OB_MESH) && state.do_pose_fade_geom;
94
95 auto fade_sync =
96 [](Manager &manager, const ObjectRef &ob_ref, const State &state, PassMain::Sub &sub) {
97 const bool use_sculpt_pbvh = BKE_sculptsession_use_pbvh_draw(ob_ref.object,
98 state.rv3d) &&
99 !state.is_image_render;
100
101 if (use_sculpt_pbvh) {
102 ResourceHandleRange handle = manager.unique_handle_for_sculpt(ob_ref);
103
104 for (SculptBatch &batch : sculpt_batches_get(ob_ref.object, SCULPT_BATCH_DEFAULT)) {
105 sub.draw(batch.batch, handle);
106 }
107 }
108 else {
109 gpu::Batch *geom = DRW_cache_object_surface_get(const_cast<Object *>(ob_ref.object));
110 if (geom) {
111 sub.draw(geom, manager.unique_handle(ob_ref));
112 }
113 }
114 };
115
116 if (draw_bone_selection) {
117 fade_sync(manager,
118 ob_ref,
119 state,
120 is_driven_by_active_armature(ob_ref.object, state) ?
121 *armature_fade_geometry_active_ps_ :
122 *armature_fade_geometry_other_ps_);
123 }
124 else if (draw_fade) {
125 fade_sync(manager, ob_ref, state, *mesh_fade_geometry_ps_);
126 }
127 }
128
129 void pre_draw(Manager &manager, View &view) final
130 {
131 if (!enabled_) {
132 return;
133 }
134
135 manager.generate_commands(ps_, view);
136 }
137
138 void draw(Framebuffer &framebuffer, Manager &manager, View &view) final
139 {
140 if (!enabled_) {
141 return;
142 }
143
144 GPU_framebuffer_bind(framebuffer);
145 manager.submit_only(ps_, view);
146 }
147
148 private:
153 static int effective_mode(const Object *ob, const Object *active_object)
154 {
155 /* Any modes that support multi-object editing should be included here. */
156 if (ELEM(active_object->mode, OB_MODE_EDIT)) {
157 return active_object->mode & ob->mode;
158 }
159
160 return active_object == ob ? ob->mode : OB_MODE_OBJECT;
161 }
162
163 static bool overlay_should_fade_object(const Object *ob, const Object *active_object)
164 {
165 if (!active_object || !ob) {
166 return false;
167 }
168
169 if (ELEM(active_object->mode, OB_MODE_OBJECT, OB_MODE_POSE)) {
170 return false;
171 }
172
173 if (active_object->mode == effective_mode(ob, active_object)) {
174 return false;
175 }
176
177 return true;
178 }
179
180 static bool is_driven_by_active_armature(Object *ob, const State &state)
181 {
183 if (ob_arm) {
184 return Armatures::is_pose_mode(ob_arm, state);
185 }
186
188 if (ob_mesh_deform) {
189 /* Recursive. */
190 return is_driven_by_active_armature(ob_mesh_deform, state);
191 }
192
193 return false;
194 }
195};
196} // namespace blender::draw::overlay
#define ELEM(...)
ResourceHandleRange unique_handle_for_sculpt(const ObjectRef &ref)
ResourceHandleRange unique_handle(const ObjectRef &ref)
void push_constant(const char *name, const float &data)
detail::PassBase< command::DrawMultiBuf > Sub
Definition draw_pass.hh:509
static bool is_pose_mode(const Object *armature_ob, const State &state)
void pre_draw(Manager &manager, View &view) final
void object_sync(Manager &manager, const ObjectRef &ob_ref, Resources &, const State &state) final
void begin_sync(Resources &res, const State &state) final
void draw(Framebuffer &framebuffer, Manager &manager, View &view) final
#define powf(x, y)
#define DRW_CLIPPING_UBO_SLOT
#define OVERLAY_GLOBALS_SLOT
const int state
detail::Pass< command::DrawMultiBuf > PassMain
Vector< SculptBatch > sculpt_batches_get(const Object *ob, SculptBatchFeature features)
gpu::Batch * DRW_cache_object_surface_get(Object *ob)
void GPU_framebuffer_bind(gpu::FrameBuffer *fb)
bool DRW_object_is_renderable(const Object *ob)
Object * BKE_modifiers_is_deformed_by_armature(Object *ob)
MINLINE void srgb_to_linearrgb_v4(float linear[4], const float srgb[4])
@ DRW_STATE_WRITE_COLOR
Definition draw_state.hh:32
@ DRW_STATE_BLEND_ALPHA
Definition draw_state.hh:57
@ DRW_STATE_DEPTH_LESS_EQUAL
Definition draw_state.hh:40
@ V3D_SHADING_BACKGROUND_THEME
bool BKE_sculptsession_use_pbvh_draw(const Object *ob, const RegionView3D *rv3d)
Object * BKE_modifiers_is_deformed_by_meshdeform(Object *ob)