Debian Codegraph @

scene/resources/3d/mesh_library.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* mesh_library.cpp */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #include "mesh_library.h"
32
33 #ifndef PHYSICS_3D_DISABLED
34 #include "box_shape_3d.h"
35 #endif // PHYSICS_3D_DISABLED
36
37 bool MeshLibrary::_set(const StringName &p_name, const Variant &p_value) {
38 String prop_name = p_name;
39 if (prop_name.begins_with("item/")) {
40 int idx = prop_name.get_slicec('/', 1).to_int();
41 String what = prop_name.get_slicec('/', 2);
42 if (!item_map.has(idx)) {
43 create_item(idx);
44 }
45
46 if (what == "name") {
47 set_item_name(idx, p_value);
48 } else if (what == "mesh") {
49 set_item_mesh(idx, p_value);
50 } else if (what == "mesh_transform") {
51 set_item_mesh_transform(idx, p_value);
52 } else if (what == "mesh_cast_shadow") {
53 switch ((int)p_value) {
54 case 0: {
55 set_item_mesh_cast_shadow(idx, RS::ShadowCastingSetting::SHADOW_CASTING_SETTING_OFF);
56 } break;
57 case 1: {
58 set_item_mesh_cast_shadow(idx, RS::ShadowCastingSetting::SHADOW_CASTING_SETTING_ON);
59 } break;
60 case 2: {
61 set_item_mesh_cast_shadow(idx, RS::ShadowCastingSetting::SHADOW_CASTING_SETTING_DOUBLE_SIDED);
62 } break;
63 case 3: {
64 set_item_mesh_cast_shadow(idx, RS::ShadowCastingSetting::SHADOW_CASTING_SETTING_SHADOWS_ONLY);
65 } break;
66 default: {
67 set_item_mesh_cast_shadow(idx, RS::ShadowCastingSetting::SHADOW_CASTING_SETTING_ON);
68 } break;
69 }
70 #ifndef PHYSICS_3D_DISABLED
71 } else if (what == "shape") {
72 Vector<ShapeData> shapes;
73 ShapeData sd;
74 sd.shape = p_value;
75 shapes.push_back(sd);
76 set_item_shapes(idx, shapes);
77 } else if (what == "shapes") {
78 _set_item_shapes(idx, p_value);
79 #endif // PHYSICS_3D_DISABLED
80 } else if (what == "preview") {
81 set_item_preview(idx, p_value);
82 } else if (what == "navigation_mesh") {
83 set_item_navigation_mesh(idx, p_value);
84 } else if (what == "navigation_mesh_transform") {
85 set_item_navigation_mesh_transform(idx, p_value);
86 #ifndef DISABLE_DEPRECATED
87 } else if (what == "navmesh") { // Renamed in 4.0 beta 9.
88 set_item_navigation_mesh(idx, p_value);
89 } else if (what == "navmesh_transform") { // Renamed in 4.0 beta 9.
90 set_item_navigation_mesh_transform(idx, p_value);
91 #endif // DISABLE_DEPRECATED
92 } else if (what == "navigation_layers") {
93 set_item_navigation_layers(idx, p_value);
94 } else {
95 return false;
96 }
97
98 return true;
99 }
100
101 return false;
102 }
103
104 bool MeshLibrary::_get(const StringName &p_name, Variant &r_ret) const {
105 String prop_name = p_name;
106 int idx = prop_name.get_slicec('/', 1).to_int();
107 ERR_FAIL_COND_V(!item_map.has(idx), false);
108 String what = prop_name.get_slicec('/', 2);
109
110 if (what == "name") {
111 r_ret = get_item_name(idx);
112 } else if (what == "mesh") {
113 r_ret = get_item_mesh(idx);
114 } else if (what == "mesh_transform") {
115 r_ret = get_item_mesh_transform(idx);
116 } else if (what == "mesh_cast_shadow") {
117 r_ret = (int)get_item_mesh_cast_shadow(idx);
118 #ifndef PHYSICS_3D_DISABLED
119 } else if (what == "shapes") {
120 r_ret = _get_item_shapes(idx);
121 #endif // PHYSICS_3D_DISABLED
122 } else if (what == "navigation_mesh") {
123 r_ret = get_item_navigation_mesh(idx);
124 } else if (what == "navigation_mesh_transform") {
125 r_ret = get_item_navigation_mesh_transform(idx);
126 #ifndef DISABLE_DEPRECATED
127 } else if (what == "navmesh") { // Renamed in 4.0 beta 9.
128 r_ret = get_item_navigation_mesh(idx);
129 } else if (what == "navmesh_transform") { // Renamed in 4.0 beta 9.
130 r_ret = get_item_navigation_mesh_transform(idx);
131 #endif // DISABLE_DEPRECATED
132 } else if (what == "navigation_layers") {
133 r_ret = get_item_navigation_layers(idx);
134 } else if (what == "preview") {
135 r_ret = get_item_preview(idx);
136 } else {
137 return false;
138 }
139
140 return true;
141 }
142
143 void MeshLibrary::_get_property_list(List<PropertyInfo> *p_list) const {
144 for (const KeyValue<int, Item> &E : item_map) {
145 String prop_name = vformat("%s/%d/", PNAME("item"), E.key);
146 p_list->push_back(PropertyInfo(Variant::STRING, prop_name + PNAME("name")));
147 p_list->push_back(PropertyInfo(Variant::OBJECT, prop_name + PNAME("mesh"), PROPERTY_HINT_RESOURCE_TYPE, "Mesh"));
148 p_list->push_back(PropertyInfo(Variant::TRANSFORM3D, prop_name + PNAME("mesh_transform"), PROPERTY_HINT_NONE, "suffix:m"));
149 p_list->push_back(PropertyInfo(Variant::INT, prop_name + PNAME("mesh_cast_shadow"), PROPERTY_HINT_ENUM, "Off,On,Double-Sided,Shadows Only"));
150 p_list->push_back(PropertyInfo(Variant::ARRAY, prop_name + PNAME("shapes")));
151 p_list->push_back(PropertyInfo(Variant::OBJECT, prop_name + PNAME("navigation_mesh"), PROPERTY_HINT_RESOURCE_TYPE, "NavigationMesh"));
152 p_list->push_back(PropertyInfo(Variant::TRANSFORM3D, prop_name + PNAME("navigation_mesh_transform"), PROPERTY_HINT_NONE, "suffix:m"));
153 p_list->push_back(PropertyInfo(Variant::INT, prop_name + PNAME("navigation_layers"), PROPERTY_HINT_LAYERS_3D_NAVIGATION));
154 p_list->push_back(PropertyInfo(Variant::OBJECT, prop_name + PNAME("preview"), PROPERTY_HINT_RESOURCE_TYPE, "Texture2D", PROPERTY_USAGE_DEFAULT));
155 }
156 }
157
158 void MeshLibrary::create_item(int p_item) {
159 ERR_FAIL_COND(p_item < 0);
160 ERR_FAIL_COND(item_map.has(p_item));
161 item_map[p_item] = Item();
162 emit_changed();
163 notify_property_list_changed();
164 }
165
166 void MeshLibrary::set_item_name(int p_item, const String &p_name) {
167 ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
168 item_map[p_item].name = p_name;
169 emit_changed();
170 }
171
172 void MeshLibrary::set_item_mesh(int p_item, const Ref<Mesh> &p_mesh) {
173 ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
174 item_map[p_item].mesh = p_mesh;
175 emit_changed();
176 }
177
178 void MeshLibrary::set_item_mesh_transform(int p_item, const Transform3D &p_transform) {
179 ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
180 item_map[p_item].mesh_transform = p_transform;
181 emit_changed();
182 }
183
184 void MeshLibrary::set_item_mesh_cast_shadow(int p_item, RS::ShadowCastingSetting p_shadow_casting_setting) {
185 ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
186 item_map[p_item].mesh_cast_shadow = p_shadow_casting_setting;
187 emit_changed();
188 }
189
190 #ifndef PHYSICS_3D_DISABLED
191 void MeshLibrary::set_item_shapes(int p_item, const Vector<ShapeData> &p_shapes) {
192 ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
193 item_map[p_item].shapes = p_shapes;
194 emit_changed();
195 notify_property_list_changed();
196 }
197 #endif // PHYSICS_3D_DISABLED
198
199 void MeshLibrary::set_item_navigation_mesh(int p_item, const Ref<NavigationMesh> &p_navigation_mesh) {
200 ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
201 item_map[p_item].navigation_mesh = p_navigation_mesh;
202 emit_changed();
203 }
204
205 void MeshLibrary::set_item_navigation_mesh_transform(int p_item, const Transform3D &p_transform) {
206 ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
207 item_map[p_item].navigation_mesh_transform = p_transform;
208 emit_changed();
209 }
210
211 void MeshLibrary::set_item_navigation_layers(int p_item, uint32_t p_navigation_layers) {
212 ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
213 item_map[p_item].navigation_layers = p_navigation_layers;
214 emit_changed();
215 }
216
217 void MeshLibrary::set_item_preview(int p_item, const Ref<Texture2D> &p_preview) {
218 ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
219 item_map[p_item].preview = p_preview;
220 emit_changed();
221 }
222
223 String MeshLibrary::get_item_name(int p_item) const {
224 ERR_FAIL_COND_V_MSG(!item_map.has(p_item), "", "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
225 return item_map[p_item].name;
226 }
227
228 Ref<Mesh> MeshLibrary::get_item_mesh(int p_item) const {
229 ERR_FAIL_COND_V_MSG(!item_map.has(p_item), Ref<Mesh>(), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
230 return item_map[p_item].mesh;
231 }
232
233 Transform3D MeshLibrary::get_item_mesh_transform(int p_item) const {
234 ERR_FAIL_COND_V_MSG(!item_map.has(p_item), Transform3D(), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
235 return item_map[p_item].mesh_transform;
236 }
237
238 RS::ShadowCastingSetting MeshLibrary::get_item_mesh_cast_shadow(int p_item) const {
239 ERR_FAIL_COND_V_MSG(!item_map.has(p_item), RS::ShadowCastingSetting::SHADOW_CASTING_SETTING_ON, "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
240 return item_map[p_item].mesh_cast_shadow;
241 }
242
243 #ifndef PHYSICS_3D_DISABLED
244 Vector<MeshLibrary::ShapeData> MeshLibrary::get_item_shapes(int p_item) const {
245 ERR_FAIL_COND_V_MSG(!item_map.has(p_item), Vector<ShapeData>(), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
246 return item_map[p_item].shapes;
247 }
248 #endif // PHYSICS_3D_DISABLED
249
250 Ref<NavigationMesh> MeshLibrary::get_item_navigation_mesh(int p_item) const {
251 ERR_FAIL_COND_V_MSG(!item_map.has(p_item), Ref<NavigationMesh>(), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
252 return item_map[p_item].navigation_mesh;
253 }
254
255 Transform3D MeshLibrary::get_item_navigation_mesh_transform(int p_item) const {
256 ERR_FAIL_COND_V_MSG(!item_map.has(p_item), Transform3D(), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
257 return item_map[p_item].navigation_mesh_transform;
258 }
259
260 uint32_t MeshLibrary::get_item_navigation_layers(int p_item) const {
261 ERR_FAIL_COND_V_MSG(!item_map.has(p_item), 0, "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
262 return item_map[p_item].navigation_layers;
263 }
264
265 Ref<Texture2D> MeshLibrary::get_item_preview(int p_item) const {
266 ERR_FAIL_COND_V_MSG(!item_map.has(p_item), Ref<Texture2D>(), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
267 return item_map[p_item].preview;
268 }
269
270 bool MeshLibrary::has_item(int p_item) const {
271 return item_map.has(p_item);
272 }
273
274 void MeshLibrary::remove_item(int p_item) {
275 ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
276 item_map.erase(p_item);
277 notify_property_list_changed();
278 emit_changed();
279 }
280
281 void MeshLibrary::clear() {
282 item_map.clear();
283 notify_property_list_changed();
284 emit_changed();
285 }
286
287 Vector<int> MeshLibrary::get_item_list() const {
288 Vector<int> ret;
289 ret.resize(item_map.size());
290 int idx = 0;
291 for (const KeyValue<int, Item> &E : item_map) {
292 ret.write[idx++] = E.key;
293 }
294
295 return ret;
296 }
297
298 int MeshLibrary::find_item_by_name(const String &p_name) const {
299 for (const KeyValue<int, Item> &E : item_map) {
300 if (E.value.name == p_name) {
301 return E.key;
302 }
303 }
304 return -1;
305 }
306
307 int MeshLibrary::get_last_unused_item_id() const {
308 if (!item_map.size()) {
309 return 0;
310 } else {
311 return item_map.back()->key() + 1;
312 }
313 }
314
315 #ifndef PHYSICS_3D_DISABLED
316 void MeshLibrary::_set_item_shapes(int p_item, const Array &p_shapes) {
317 Array arr_shapes = p_shapes;
318 int size = p_shapes.size();
319 if (size & 1) {
320 ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
321 int prev_size = item_map[p_item].shapes.size() * 2;
322
323 if (prev_size < size) {
324 // Check if last element is a shape.
325 Ref<Shape3D> shape = arr_shapes[size - 1];
326 if (shape.is_null()) {
327 Ref<BoxShape3D> box_shape;
328 box_shape.instantiate();
329 arr_shapes[size - 1] = box_shape;
330 }
331
332 // Make sure the added element is a Transform3D.
333 arr_shapes.push_back(Transform3D());
334 size++;
335 } else {
336 size--;
337 arr_shapes.resize(size);
338 }
339 }
340
341 Vector<ShapeData> shapes;
342 for (int i = 0; i < size; i += 2) {
343 ShapeData sd;
344 sd.shape = arr_shapes[i + 0];
345 sd.local_transform = arr_shapes[i + 1];
346
347 if (sd.shape.is_valid()) {
348 shapes.push_back(sd);
349 }
350 }
351
352 set_item_shapes(p_item, shapes);
353 }
354
355 Array MeshLibrary::_get_item_shapes(int p_item) const {
356 Vector<ShapeData> shapes = get_item_shapes(p_item);
357 Array ret;
358 for (int i = 0; i < shapes.size(); i++) {
359 ret.push_back(shapes[i].shape);
360 ret.push_back(shapes[i].local_transform);
361 }
362
363 return ret;
364 }
365 #endif // PHYSICS_3D_DISABLED
366
367 void MeshLibrary::reset_state() {
368 clear();
369 }
370 void MeshLibrary::_bind_methods() {
371 ClassDB::bind_method(D_METHOD("create_item", "id"), &MeshLibrary::create_item);
372 ClassDB::bind_method(D_METHOD("set_item_name", "id", "name"), &MeshLibrary::set_item_name);
373 ClassDB::bind_method(D_METHOD("set_item_mesh", "id", "mesh"), &MeshLibrary::set_item_mesh);
374 ClassDB::bind_method(D_METHOD("set_item_mesh_transform", "id", "mesh_transform"), &MeshLibrary::set_item_mesh_transform);
375 ClassDB::bind_method(D_METHOD("set_item_mesh_cast_shadow", "id", "shadow_casting_setting"), &MeshLibrary::set_item_mesh_cast_shadow);
376 ClassDB::bind_method(D_METHOD("set_item_navigation_mesh", "id", "navigation_mesh"), &MeshLibrary::set_item_navigation_mesh);
377 ClassDB::bind_method(D_METHOD("set_item_navigation_mesh_transform", "id", "navigation_mesh"), &MeshLibrary::set_item_navigation_mesh_transform);
378 ClassDB::bind_method(D_METHOD("set_item_navigation_layers", "id", "navigation_layers"), &MeshLibrary::set_item_navigation_layers);
379 #ifndef PHYSICS_3D_DISABLED
380 ClassDB::bind_method(D_METHOD("set_item_shapes", "id", "shapes"), &MeshLibrary::_set_item_shapes);
381 #endif // PHYSICS_3D_DISABLED
382 ClassDB::bind_method(D_METHOD("set_item_preview", "id", "texture"), &MeshLibrary::set_item_preview);
383 ClassDB::bind_method(D_METHOD("get_item_name", "id"), &MeshLibrary::get_item_name);
384 ClassDB::bind_method(D_METHOD("get_item_mesh", "id"), &MeshLibrary::get_item_mesh);
385 ClassDB::bind_method(D_METHOD("get_item_mesh_transform", "id"), &MeshLibrary::get_item_mesh_transform);
386 ClassDB::bind_method(D_METHOD("get_item_mesh_cast_shadow", "id"), &MeshLibrary::get_item_mesh_cast_shadow);
387 ClassDB::bind_method(D_METHOD("get_item_navigation_mesh", "id"), &MeshLibrary::get_item_navigation_mesh);
388 ClassDB::bind_method(D_METHOD("get_item_navigation_mesh_transform", "id"), &MeshLibrary::get_item_navigation_mesh_transform);
389 ClassDB::bind_method(D_METHOD("get_item_navigation_layers", "id"), &MeshLibrary::get_item_navigation_layers);
390 #ifndef PHYSICS_3D_DISABLED
391 ClassDB::bind_method(D_METHOD("get_item_shapes", "id"), &MeshLibrary::_get_item_shapes);
392 #endif // PHYSICS_3D_DISABLED
393 ClassDB::bind_method(D_METHOD("get_item_preview", "id"), &MeshLibrary::get_item_preview);
394 ClassDB::bind_method(D_METHOD("remove_item", "id"), &MeshLibrary::remove_item);
395 ClassDB::bind_method(D_METHOD("find_item_by_name", "name"), &MeshLibrary::find_item_by_name);
396
397 ClassDB::bind_method(D_METHOD("clear"), &MeshLibrary::clear);
398 ClassDB::bind_method(D_METHOD("get_item_list"), &MeshLibrary::get_item_list);
399 ClassDB::bind_method(D_METHOD("get_last_unused_item_id"), &MeshLibrary::get_last_unused_item_id);
400 }
401
402 MeshLibrary::MeshLibrary() {
403 }
404
405 MeshLibrary::~MeshLibrary() {
406 }