Blender V4.3
overlay_next_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
9#pragma once
10
11#include "BKE_modifier.hh"
12#include "BKE_paint.hh"
13
16
17namespace blender::draw::overlay {
18class Fade {
19 private:
20 const SelectionType selection_type_;
21
22 PassMain ps_ = {"FadeGeometry"};
23
24 PassMain::Sub *mesh_fade_geometry_ps_;
25 /* Passes for Pose Fade Geometry. */
26 PassMain::Sub *armature_fade_geometry_active_ps_;
27 PassMain::Sub *armature_fade_geometry_other_ps_;
28
29 bool enabled_ = false;
30
31 public:
32 Fade(const SelectionType selection_type_) : selection_type_(selection_type_) {}
33
34 void begin_sync(Resources &res, const State &state)
35 {
36 const bool do_edit_mesh_fade_geom = !state.xray_enabled &&
37 (state.overlay.flag & V3D_OVERLAY_FADE_INACTIVE);
38 enabled_ = state.v3d && (do_edit_mesh_fade_geom || state.do_pose_fade_geom) &&
39 (selection_type_ == SelectionType::DISABLED);
40
41 if (!enabled_) {
42 /* Not used. But release the data. */
43 ps_.init();
44 return;
45 }
46
47 ps_.init();
49 state.clipping_plane_count);
50 ps_.shader_set(res.shaders.uniform_color.get());
51 {
52 PassMain::Sub &sub = ps_.sub("edit_mesh.fade");
54 color[3] = state.overlay.fade_alpha;
55 if (state.v3d->shading.background_type == V3D_SHADING_BACKGROUND_THEME) {
56 srgb_to_linearrgb_v4(color, color);
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, const ObjectRef &ob_ref, const State &state)
81 {
82 if (!enabled_) {
83 return;
84 }
85 const Object *ob = ob_ref.object;
86 const bool renderable = DRW_object_is_renderable(ob);
87 const bool draw_surface = (ob->dt >= OB_WIRE) && (renderable || (ob->dt == OB_WIRE));
88 const bool draw_fade = draw_surface && overlay_should_fade_object(ob, state.object_active);
89
90 const bool draw_bone_selection = (ob_ref.object->type == OB_MESH) && state.do_pose_fade_geom;
91
92 auto fade_sync =
93 [](Manager &manager, const ObjectRef &ob_ref, const State &state, PassMain::Sub &sub) {
94 const bool use_sculpt_pbvh = BKE_sculptsession_use_pbvh_draw(ob_ref.object,
95 state.rv3d) &&
97
98 if (use_sculpt_pbvh) {
99 ResourceHandle handle = manager.resource_handle_for_sculpt(ob_ref);
100
102 sub.draw(batch.batch, handle);
103 }
104 }
105 else {
106 blender::gpu::Batch *geom = DRW_cache_object_surface_get((Object *)ob_ref.object);
107 if (geom) {
108 sub.draw(geom, manager.unique_handle(ob_ref));
109 }
110 }
111 };
112
113 if (draw_bone_selection) {
114 fade_sync(manager,
115 ob_ref,
116 state,
117 is_driven_by_active_armature(ob_ref.object, state) ?
118 *armature_fade_geometry_active_ps_ :
119 *armature_fade_geometry_other_ps_);
120 }
121 else if (draw_fade) {
122 fade_sync(manager, ob_ref, state, *mesh_fade_geometry_ps_);
123 }
124 }
125
126 void draw(Framebuffer &framebuffer, Manager &manager, View &view)
127 {
128 GPU_framebuffer_bind(framebuffer);
129 manager.submit(ps_, view);
130 }
131
132 private:
133 static bool overlay_should_fade_object(const Object *ob, const Object *active_object)
134 {
135 if (!active_object || !ob) {
136 return false;
137 }
138
139 if (ELEM(active_object->mode, OB_MODE_OBJECT, OB_MODE_POSE)) {
140 return false;
141 }
142
143 if ((active_object->mode & ob->mode) != 0) {
144 return false;
145 }
146
147 return true;
148 }
149
150 static bool is_driven_by_active_armature(Object *ob, const State &state)
151 {
153 if (ob_arm) {
154 return Armatures::is_pose_mode(ob_arm, state);
155 }
156
158 if (ob_mesh_deform) {
159 /* Recursive. */
160 return is_driven_by_active_armature(ob_mesh_deform, state);
161 }
162
163 return false;
164 }
165};
166} // 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:2862
MINLINE void srgb_to_linearrgb_v4(float linear[4], const float srgb[4])
#define ELEM(...)
@ OB_WIRE
@ OB_MODE_POSE
@ OB_MODE_OBJECT
@ OB_MESH
@ V3D_SHADING_BACKGROUND_THEME
@ V3D_OVERLAY_FADE_INACTIVE
void GPU_framebuffer_bind(GPUFrameBuffer *framebuffer)
ResourceHandle unique_handle(const ObjectRef &ref)
ResourceHandle resource_handle_for_sculpt(const ObjectRef &ref)
void submit(PassSimple &pass, View &view)
PassBase< DrawCommandBufType > & sub(const char *name)
Definition draw_pass.hh:616
void state_set(DRWState state, int clip_plane_count=0)
Definition draw_pass.hh:954
void push_constant(const char *name, const float &data)
void shader_set(GPUShader *shader)
Definition draw_pass.hh:971
static bool is_pose_mode(const Object *armature_ob, const State &state)
void object_sync(Manager &manager, const ObjectRef &ob_ref, const State &state)
void begin_sync(Resources &res, const State &state)
Fade(const SelectionType selection_type_)
void draw(Framebuffer &framebuffer, Manager &manager, View &view)
#define powf(x, y)
blender::gpu::Batch * DRW_cache_object_surface_get(Object *ob)
bool DRW_object_is_renderable(const Object *ob)
bool DRW_state_is_image_render()
@ 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 @620::@622 batch
static ulong state[N]
Vector< SculptBatch > sculpt_batches_get(const Object *ob, SculptBatchFeature features)
float4 background_color_get(const State &state)