1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247 | /**************************************************************************/
/* occluder_shape.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "occluder_shape.h"
#include "core/engine.h"
#include "core/math/transform.h"
#include "servers/visual_server.h"
#ifdef TOOLS_ENABLED
#include "editor/editor_node.h"
#endif
void OccluderShape::_bind_methods() {
}
OccluderShape::OccluderShape() {
_shape = RID_PRIME(VisualServer::get_singleton()->occluder_resource_create());<--- Variable '_shape' is assigned in constructor body. Consider performing initialization in initialization list. [+]When an object of a class is created, the constructors of all member variables are called consecutively in the order the variables are declared, even if you don't explicitly write them to the initialization list. You could avoid assigning '_shape' a value by passing the value to the constructor in the initialization list.
}
OccluderShape::~OccluderShape() {
if (_shape.is_valid()) {
VisualServer::get_singleton()->free(_shape);
}
}
#ifdef TOOLS_ENABLED
AABB OccluderShape::get_fallback_gizmo_aabb() const {
return AABB(Vector3(-0.5, -0.5, -0.5), Vector3(1, 1, 1));
}
#endif
//////////////////////////////////////////////
void OccluderShapeSphere::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_spheres", "spheres"), &OccluderShapeSphere::set_spheres);
ClassDB::bind_method(D_METHOD("get_spheres"), &OccluderShapeSphere::get_spheres);
ClassDB::bind_method(D_METHOD("set_sphere_position", "index", "position"), &OccluderShapeSphere::set_sphere_position);
ClassDB::bind_method(D_METHOD("set_sphere_radius", "index", "radius"), &OccluderShapeSphere::set_sphere_radius);
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "spheres", PROPERTY_HINT_NONE, itos(Variant::PLANE) + ":"), "set_spheres", "get_spheres");
}
#ifdef TOOLS_ENABLED
void OccluderShapeSphere::_update_aabb() {
_aabb_local = AABB();
if (!_spheres.size()) {
return;
}
_aabb_local.position = _spheres[0].normal;
for (int n = 0; n < _spheres.size(); n++) {
AABB bb(_spheres[n].normal, Vector3(0, 0, 0));
bb.grow_by(_spheres[n].d);
_aabb_local.merge_with(bb);
}
}
AABB OccluderShapeSphere::get_fallback_gizmo_aabb() const {
return _aabb_local;
}
#endif
void OccluderShapeSphere::update_shape_to_visual_server() {
VisualServer::get_singleton()->occluder_resource_spheres_update(get_shape(), _spheres);
}
Transform OccluderShapeSphere::center_node(const Transform &p_global_xform, const Transform &p_parent_xform, real_t p_snap) {
if (!_spheres.size()) {
return Transform();
}
// make sure world spheres correct
Vector<Plane> spheres_world_space;
if (spheres_world_space.size() != _spheres.size()) {
spheres_world_space.resize(_spheres.size());
}
Vector3 scale3 = p_global_xform.basis.get_scale_abs();
real_t scale = (scale3.x + scale3.y + scale3.z) / 3.0;
for (int n = 0; n < _spheres.size(); n++) {
Plane p;
p.normal = p_global_xform.xform(_spheres[n].normal);
p.d = _spheres[n].d * scale;
spheres_world_space.set(n, p);
}
// first find the center
AABB bb;
bb.set_position(spheres_world_space[0].normal);
// new positions
for (int n = 0; n < spheres_world_space.size(); n++) {
const Plane &sphere = spheres_world_space[n];
// update aabb
AABB sphere_bb(sphere.normal, Vector3());
sphere_bb.grow_by(sphere.d);
bb.merge_with(sphere_bb);
}
Vector3 center = bb.get_center();
// snapping
if (p_snap > 0.0001) {
center.snap(Vector3(p_snap, p_snap, p_snap));
}
// new transform with no rotate or scale, centered
Transform new_local_xform = Transform();
new_local_xform.translate(center.x, center.y, center.z);
Transform inv_xform = new_local_xform.affine_inverse();
// back calculate the new spheres
for (int n = 0; n < spheres_world_space.size(); n++) {
Plane p = spheres_world_space[n];
p.normal = inv_xform.xform(p.normal);
// assuming uniform scale, otherwise this will go wrong
Vector3 inv_scale = inv_xform.basis.get_scale_abs();
p.d *= inv_scale.x;
spheres_world_space.set(n, p);
}
#ifdef TOOLS_ENABLED
if (Engine::get_singleton()->is_editor_hint()) {
UndoRedo *undo_redo = EditorNode::get_undo_redo();
undo_redo->create_action(TTR("OccluderShapeSphere Set Spheres"));
undo_redo->add_do_method(this, "set_spheres", spheres_world_space);
undo_redo->add_undo_method(this, "set_spheres", _spheres);
undo_redo->commit_action();
} else {
set_spheres(spheres_world_space);
}
#else
set_spheres(spheres_world_space);
#endif
notify_change_to_owners();
return new_local_xform;
}
void OccluderShapeSphere::set_spheres(const Vector<Plane> &p_spheres) {
#ifdef TOOLS_ENABLED
// try and detect special circumstance of adding a new sphere in the editor
bool adding_in_editor = false;
if ((p_spheres.size() == _spheres.size() + 1) && (p_spheres[p_spheres.size() - 1] == Plane())) {
adding_in_editor = true;
}
#endif
_spheres = p_spheres;
// sanitize radii
for (int n = 0; n < _spheres.size(); n++) {
if (_spheres[n].d < _min_radius) {
Plane p = _spheres[n];
p.d = _min_radius;
_spheres.set(n, p);
}
}
#ifdef TOOLS_ENABLED
if (adding_in_editor) {
_spheres.set(_spheres.size() - 1, Plane(Vector3(), 1.0));
}
_update_aabb();
#endif
update_shape_to_visual_server();
notify_change_to_owners();
}
void OccluderShapeSphere::set_sphere_position(int p_idx, const Vector3 &p_position) {
ERR_FAIL_INDEX(p_idx, _spheres.size());
Plane p = _spheres[p_idx];
p.normal = p_position;
_spheres.set(p_idx, p);
#ifdef TOOLS_ENABLED
_update_aabb();
#endif
update_shape_to_visual_server();
notify_change_to_owners();
}
void OccluderShapeSphere::set_sphere_radius(int p_idx, real_t p_radius) {
ERR_FAIL_INDEX(p_idx, _spheres.size());
Plane p = _spheres[p_idx];
p.d = MAX(p_radius, _min_radius);
_spheres.set(p_idx, p);
#ifdef TOOLS_ENABLED
_update_aabb();
#endif
update_shape_to_visual_server();
notify_change_to_owners();
}
OccluderShapeSphere::OccluderShapeSphere() {
if (get_shape().is_valid()) {
VisualServer::get_singleton()->occluder_resource_prepare(get_shape(), VisualServer::OCCLUDER_TYPE_SPHERE);
}
// Create a default sphere
Vector<Plane> planes;
planes.push_back(Plane(Vector3(0, 0, 0), 1));
set_spheres(planes);
}
|