Blender V5.0
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 {
50 PassMain::Sub &sub = ps_.sub("edit_mesh.fade");
51 float4 color = res.background_color_get(state);
52 color[3] = state.overlay.fade_alpha;
53 if (state.v3d->shading.background_type == V3D_SHADING_BACKGROUND_THEME) {
55 }
56 sub.push_constant("ucolor", color);
57 mesh_fade_geometry_ps_ = ⊂
58 }
59
60 /* Fade Geometry. */
61 if (state.do_pose_fade_geom) {
62 const float alpha = state.overlay.xray_alpha_bone;
63 float4 color = {0.0f, 0.0f, 0.0f, alpha};
64 {
65 auto &sub = ps_.sub("fade_geometry.active");
66 sub.push_constant("ucolor", color);
67 armature_fade_geometry_active_ps_ = ⊂
68 }
69 {
70 color[3] = powf(alpha, 4);
71 auto &sub = ps_.sub("fade_geometry");
72 sub.push_constant("ucolor", color);
73 armature_fade_geometry_other_ps_ = ⊂
74 }
75 }
76 }
77
78 void object_sync(Manager &manager,
79 const ObjectRef &ob_ref,
80 Resources & /*res*/,
81 const State &state) final
82 {
83 if (!enabled_) {
84 return;
85 }
86 const Object *ob = ob_ref.object;
87 const bool renderable = DRW_object_is_renderable(ob);
88 const bool draw_surface = (ob->dt >= OB_WIRE) && (renderable || (ob->dt == OB_WIRE));
89 const bool draw_fade = draw_surface && overlay_should_fade_object(ob, state.object_active);
90
91 const bool draw_bone_selection = (ob_ref.object->type == OB_MESH) && state.do_pose_fade_geom;
92
93 auto fade_sync =
94 [](Manager &manager, const ObjectRef &ob_ref, const State &state, PassMain::Sub &sub) {
95 const bool use_sculpt_pbvh = BKE_sculptsession_use_pbvh_draw(ob_ref.object,
96 state.rv3d) &&
97 !state.is_image_render;
98
99 if (use_sculpt_pbvh) {
100 ResourceHandleRange handle = manager.unique_handle_for_sculpt(ob_ref);
101
103 sub.draw(batch.batch, handle);
104 }
105 }
106 else {
107 blender::gpu::Batch *geom = DRW_cache_object_surface_get((Object *)ob_ref.object);
108 if (geom) {
109 sub.draw(geom, manager.unique_handle(ob_ref));
110 }
111 }
112 };
113
114 if (draw_bone_selection) {
115 fade_sync(manager,
116 ob_ref,
117 state,
118 is_driven_by_active_armature(ob_ref.object, state) ?
119 *armature_fade_geometry_active_ps_ :
120 *armature_fade_geometry_other_ps_);
121 }
122 else if (draw_fade) {
123 fade_sync(manager, ob_ref, state, *mesh_fade_geometry_ps_);
124 }
125 }
126
127 void pre_draw(Manager &manager, View &view) final
128 {
129 if (!enabled_) {
130 return;
131 }
132
133 manager.generate_commands(ps_, view);
134 }
135
136 void draw(Framebuffer &framebuffer, Manager &manager, View &view) final
137 {
138 if (!enabled_) {
139 return;
140 }
141
142 GPU_framebuffer_bind(framebuffer);
143 manager.submit_only(ps_, view);
144 }
145
146 private:
151 static int effective_mode(const Object *ob, const Object *active_object)
152 {
153 /* Any modes that support multi-object editing should be included here. */
154 if (ELEM(active_object->mode, OB_MODE_EDIT)) {
155 return active_object->mode & ob->mode;
156 }
157
158 return active_object == ob ? ob->mode : OB_MODE_OBJECT;
159 }
160
161 static bool overlay_should_fade_object(const Object *ob, const Object *active_object)
162 {
163 if (!active_object || !ob) {
164 return false;
165 }
166
167 if (ELEM(active_object->mode, OB_MODE_OBJECT, OB_MODE_POSE)) {
168 return false;
169 }
170
171 if (active_object->mode == effective_mode(ob, active_object)) {
172 return false;
173 }
174
175 return true;
176 }
177
178 static bool is_driven_by_active_armature(Object *ob, const State &state)
179 {
181 if (ob_arm) {
182 return Armatures::is_pose_mode(ob_arm, state);
183 }
184
186 if (ob_mesh_deform) {
187 /* Recursive. */
188 return is_driven_by_active_armature(ob_mesh_deform, state);
189 }
190
191 return false;
192 }
193};
194} // namespace blender::draw::overlay
Object * BKE_modifiers_is_deformed_by_meshdeform(Object *ob)
Object * BKE_modifiers_is_deformed_by_armature(Object *ob)
bool BKE_sculptsession_use_pbvh_draw(const Object *ob, const RegionView3D *rv3d)
Definition paint.cc:3068
MINLINE void srgb_to_linearrgb_v4(float linear[4], const float srgb[4])
#define ELEM(...)
@ OB_WIRE
@ OB_MODE_EDIT
@ OB_MODE_POSE
@ OB_MODE_OBJECT
@ OB_MESH
struct Object Object
@ V3D_SHADING_BACKGROUND_THEME
static AppView * view
void GPU_framebuffer_bind(blender::gpu::FrameBuffer *fb)
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:499
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)
bool DRW_object_is_renderable(const Object *ob)
@ DRW_STATE_BLEND_ALPHA
Definition draw_state.hh:55
@ DRW_STATE_WRITE_COLOR
Definition draw_state.hh:30
@ DRW_STATE_DEPTH_LESS_EQUAL
Definition draw_state.hh:38
struct @021025263243242147216143265077100330027142264337::@225245033123204053237120173316075113304004012000 batch
static ulong state[N]
detail::Pass< command::DrawMultiBuf > PassMain
Vector< SculptBatch > sculpt_batches_get(const Object *ob, SculptBatchFeature features)
gpu::Batch * DRW_cache_object_surface_get(Object *ob)
VecBase< float, 4 > float4