Blender V5.3
overlay_force_field.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_anim_path.h"
12
14
16
17#include "overlay_base.hh"
18
19namespace blender::draw::overlay {
20
26 using ForceFieldsInstanceBuf = ShapeInstanceBuf<ExtraInstanceData>;
27
28 private:
29 PassSimple ps_ = {"ForceFields"};
30
31 struct CallBuffers {
32 const SelectionType selection_type_;
33
34 ForceFieldsInstanceBuf field_force_buf = {selection_type_, "field_force_buf"};
35 ForceFieldsInstanceBuf field_wind_buf = {selection_type_, "field_wind_buf"};
36 ForceFieldsInstanceBuf field_vortex_buf = {selection_type_, "field_vortex_buf"};
37 ForceFieldsInstanceBuf field_curve_buf = {selection_type_, "field_curve_buf"};
38 ForceFieldsInstanceBuf field_sphere_limit_buf = {selection_type_, "field_sphere_limit_buf"};
39 ForceFieldsInstanceBuf field_tube_limit_buf = {selection_type_, "field_tube_limit_buf"};
40 ForceFieldsInstanceBuf field_cone_limit_buf = {selection_type_, "field_cone_limit_buf"};
41 } call_buffers_;
42
43 public:
44 ForceFields(const SelectionType selection_type) : call_buffers_{selection_type} {}
45
46 void begin_sync(Resources & /*res*/, const State & /*state*/) final
47 {
48 call_buffers_.field_force_buf.clear();
49 call_buffers_.field_wind_buf.clear();
50 call_buffers_.field_vortex_buf.clear();
51 call_buffers_.field_curve_buf.clear();
52 call_buffers_.field_sphere_limit_buf.clear();
53 call_buffers_.field_tube_limit_buf.clear();
54 call_buffers_.field_cone_limit_buf.clear();
55 }
56
57 void object_sync(Manager & /*manager*/,
58 const ObjectRef &ob_ref,
59 Resources &res,
60 const State &state) final
61 {
62 if (!ob_ref.object->pd || !ob_ref.object->pd->forcefield) {
63 return;
64 }
65 const select::ID select_id = res.select_id(ob_ref);
66 const Object *ob = ob_ref.object;
67 PartDeflect *pd = ob->pd;
69 nullptr;
70
72 ob->object_to_world(), res.object_background_blend_color(ob_ref, state), 1.0f);
73 float4x4 &matrix = data.object_to_world;
74 float &size_x = matrix[0][3];
75 float &size_y = matrix[1][3];
76 float &size_z = matrix[2][3];
77
78 size_x = size_y = size_z = ob->empty_drawsize;
79
80 switch (pd->forcefield) {
81 case PFIELD_FORCE:
82 call_buffers_.field_force_buf.append(data, select_id);
83 break;
84 case PFIELD_WIND:
85 size_z = pd->f_strength;
86 call_buffers_.field_wind_buf.append(data, select_id);
87 break;
88 case PFIELD_VORTEX:
89 size_y = (pd->f_strength < 0.0f) ? -size_y : size_y;
90 call_buffers_.field_vortex_buf.append(data, select_id);
91 break;
92 case PFIELD_GUIDE:
93 if (cu && (cu->flag & CU_PATH) && ob->runtime->curve_cache->anim_path_accum_length) {
94 size_x = size_y = size_z = pd->f_strength;
95 float4 pos;
96 BKE_where_on_path(ob, 0.0f, pos, nullptr, nullptr, nullptr, nullptr);
97 matrix.location() = ob->object_to_world().location();
98 matrix = math::translate(matrix, pos.xyz());
99 call_buffers_.field_curve_buf.append(data, select_id);
100
101 BKE_where_on_path(ob, 1.0f, pos, nullptr, nullptr, nullptr, nullptr);
102 matrix.location() = ob->object_to_world().location();
103 matrix = math::translate(matrix, pos.xyz());
104 call_buffers_.field_sphere_limit_buf.append(data, select_id);
105 /* Restore */
106 matrix.location() = ob->object_to_world().location();
107 }
108 break;
109 default:
110 break;
111 }
112
113 if (pd->falloff == PFIELD_FALL_TUBE) {
114 if (pd->flag & (PFIELD_USEMAX | PFIELD_USEMAXR)) {
115 size_z = (pd->flag & PFIELD_USEMAX) ? pd->maxdist : 0.0f;
116 size_x = (pd->flag & PFIELD_USEMAXR) ? pd->maxrad : 1.0f;
117 size_y = size_x;
118 call_buffers_.field_tube_limit_buf.append(data, select_id);
119 }
120 if (pd->flag & (PFIELD_USEMIN | PFIELD_USEMINR)) {
121 size_z = (pd->flag & PFIELD_USEMIN) ? pd->mindist : 0.0f;
122 size_x = (pd->flag & PFIELD_USEMINR) ? pd->minrad : 1.0f;
123 size_y = size_x;
124 call_buffers_.field_tube_limit_buf.append(data, select_id);
125 }
126 }
127 else if (pd->falloff == PFIELD_FALL_CONE) {
128 if (pd->flag & (PFIELD_USEMAX | PFIELD_USEMAXR)) {
129 float radius = DEG2RADF((pd->flag & PFIELD_USEMAXR) ? pd->maxrad : 1.0f);
130 float distance = (pd->flag & PFIELD_USEMAX) ? pd->maxdist : 0.0f;
131 size_x = distance * sinf(radius);
132 size_z = distance * cosf(radius);
133 size_y = size_x;
134 call_buffers_.field_cone_limit_buf.append(data, select_id);
135 }
136 if (pd->flag & (PFIELD_USEMIN | PFIELD_USEMINR)) {
137 float radius = DEG2RADF((pd->flag & PFIELD_USEMINR) ? pd->minrad : 1.0f);
138 float distance = (pd->flag & PFIELD_USEMIN) ? pd->mindist : 0.0f;
139 size_x = distance * sinf(radius);
140 size_z = distance * cosf(radius);
141 size_y = size_x;
142 call_buffers_.field_cone_limit_buf.append(data, select_id);
143 }
144 }
145 else if (pd->falloff == PFIELD_FALL_SPHERE) {
146 if (pd->flag & PFIELD_USEMAX) {
147 size_x = size_y = size_z = pd->maxdist;
148 call_buffers_.field_sphere_limit_buf.append(data, select_id);
149 }
150 if (pd->flag & PFIELD_USEMIN) {
151 size_x = size_y = size_z = pd->mindist;
152 call_buffers_.field_sphere_limit_buf.append(data, select_id);
153 }
154 }
155 }
156
157 void end_sync(Resources &res, const State &state) final
158 {
159 ps_.init();
160 res.select_bind(ps_);
162 state.clipping_plane_count);
163 ps_.shader_set(res.shaders->extra_shape.get());
164 ps_.bind_ubo(OVERLAY_GLOBALS_SLOT, &res.globals_buf);
165 ps_.bind_ubo(DRW_CLIPPING_UBO_SLOT, &res.clip_planes_buf);
166
167 call_buffers_.field_force_buf.end_sync(ps_, res.shapes.field_force.get());
168 call_buffers_.field_wind_buf.end_sync(ps_, res.shapes.field_wind.get());
169 call_buffers_.field_vortex_buf.end_sync(ps_, res.shapes.field_vortex.get());
170 call_buffers_.field_curve_buf.end_sync(ps_, res.shapes.field_curve.get());
171 call_buffers_.field_sphere_limit_buf.end_sync(ps_, res.shapes.field_sphere_limit.get());
172 call_buffers_.field_tube_limit_buf.end_sync(ps_, res.shapes.field_tube_limit.get());
173 call_buffers_.field_cone_limit_buf.end_sync(ps_, res.shapes.field_cone_limit.get());
174 }
175
176 void draw_line(Framebuffer &framebuffer, Manager &manager, View &view) final
177 {
178 GPU_framebuffer_bind(framebuffer);
179 manager.submit(ps_, view);
180 }
181};
182
183} // namespace blender::draw::overlay
#define DEG2RADF(_deg)
void object_sync(Manager &, const ObjectRef &ob_ref, Resources &res, const State &state) final
void end_sync(Resources &res, const State &state) final
void draw_line(Framebuffer &framebuffer, Manager &manager, View &view) final
void begin_sync(Resources &, const State &) final
ForceFields(const SelectionType selection_type)
#define DRW_CLIPPING_UBO_SLOT
#define OVERLAY_GLOBALS_SLOT
float distance(float, float) RET
const int state
select::SelectionType SelectionType
detail::Pass< command::DrawCommandBuf > PassSimple
MatBase< T, NumCol, NumRow > translate(const MatBase< T, NumCol, NumRow > &mat, const VectorT &translation)
void GPU_framebuffer_bind(gpu::FrameBuffer *fb)
bool BKE_where_on_path(const struct Object *ob, float ctime, float r_vec[4], float r_dir[3], float r_quat[4], float *r_radius, float *r_weight)
T & DRW_object_get_data_for_drawing(const Object &object)
BMesh const char void * data
@ DRW_STATE_WRITE_COLOR
Definition draw_state.hh:32
@ DRW_STATE_WRITE_DEPTH
Definition draw_state.hh:31
@ DRW_STATE_DEPTH_LESS_EQUAL
Definition draw_state.hh:40
#define sinf
#define cosf
const float * anim_path_accum_length
Definition BKE_curve.hh:53
struct PartDeflect * pd
bke::ObjectRuntime * runtime