Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/resources/3d/mesh_library.cpp
21147 views
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
#include "scene/resources/texture.h"
34
35
#ifndef PHYSICS_3D_DISABLED
36
#include "box_shape_3d.h"
37
#endif // PHYSICS_3D_DISABLED
38
39
bool MeshLibrary::_set(const StringName &p_name, const Variant &p_value) {
40
String prop_name = p_name;
41
if (prop_name.begins_with("item/")) {
42
int idx = prop_name.get_slicec('/', 1).to_int();
43
String what = prop_name.get_slicec('/', 2);
44
if (!item_map.has(idx)) {
45
create_item(idx);
46
}
47
48
if (what == "name") {
49
set_item_name(idx, p_value);
50
} else if (what == "mesh") {
51
set_item_mesh(idx, p_value);
52
} else if (what == "mesh_transform") {
53
set_item_mesh_transform(idx, p_value);
54
} else if (what == "mesh_cast_shadow") {
55
switch ((int)p_value) {
56
case 0: {
57
set_item_mesh_cast_shadow(idx, RS::ShadowCastingSetting::SHADOW_CASTING_SETTING_OFF);
58
} break;
59
case 1: {
60
set_item_mesh_cast_shadow(idx, RS::ShadowCastingSetting::SHADOW_CASTING_SETTING_ON);
61
} break;
62
case 2: {
63
set_item_mesh_cast_shadow(idx, RS::ShadowCastingSetting::SHADOW_CASTING_SETTING_DOUBLE_SIDED);
64
} break;
65
case 3: {
66
set_item_mesh_cast_shadow(idx, RS::ShadowCastingSetting::SHADOW_CASTING_SETTING_SHADOWS_ONLY);
67
} break;
68
default: {
69
set_item_mesh_cast_shadow(idx, RS::ShadowCastingSetting::SHADOW_CASTING_SETTING_ON);
70
} break;
71
}
72
#ifndef PHYSICS_3D_DISABLED
73
} else if (what == "shape") {
74
Vector<ShapeData> shapes;
75
ShapeData sd;
76
sd.shape = p_value;
77
shapes.push_back(sd);
78
set_item_shapes(idx, shapes);
79
} else if (what == "shapes") {
80
_set_item_shapes(idx, p_value);
81
#endif // PHYSICS_3D_DISABLED
82
} else if (what == "preview") {
83
set_item_preview(idx, p_value);
84
} else if (what == "navigation_mesh") {
85
set_item_navigation_mesh(idx, p_value);
86
} else if (what == "navigation_mesh_transform") {
87
set_item_navigation_mesh_transform(idx, p_value);
88
#ifndef DISABLE_DEPRECATED
89
} else if (what == "navmesh") { // Renamed in 4.0 beta 9.
90
set_item_navigation_mesh(idx, p_value);
91
} else if (what == "navmesh_transform") { // Renamed in 4.0 beta 9.
92
set_item_navigation_mesh_transform(idx, p_value);
93
#endif // DISABLE_DEPRECATED
94
} else if (what == "navigation_layers") {
95
set_item_navigation_layers(idx, p_value);
96
} else {
97
return false;
98
}
99
100
return true;
101
}
102
103
return false;
104
}
105
106
bool MeshLibrary::_get(const StringName &p_name, Variant &r_ret) const {
107
String prop_name = p_name;
108
int idx = prop_name.get_slicec('/', 1).to_int();
109
ERR_FAIL_COND_V(!item_map.has(idx), false);
110
String what = prop_name.get_slicec('/', 2);
111
112
if (what == "name") {
113
r_ret = get_item_name(idx);
114
} else if (what == "mesh") {
115
r_ret = get_item_mesh(idx);
116
} else if (what == "mesh_transform") {
117
r_ret = get_item_mesh_transform(idx);
118
} else if (what == "mesh_cast_shadow") {
119
r_ret = (int)get_item_mesh_cast_shadow(idx);
120
#ifndef PHYSICS_3D_DISABLED
121
} else if (what == "shapes") {
122
r_ret = _get_item_shapes(idx);
123
#endif // PHYSICS_3D_DISABLED
124
} else if (what == "navigation_mesh") {
125
r_ret = get_item_navigation_mesh(idx);
126
} else if (what == "navigation_mesh_transform") {
127
r_ret = get_item_navigation_mesh_transform(idx);
128
#ifndef DISABLE_DEPRECATED
129
} else if (what == "navmesh") { // Renamed in 4.0 beta 9.
130
r_ret = get_item_navigation_mesh(idx);
131
} else if (what == "navmesh_transform") { // Renamed in 4.0 beta 9.
132
r_ret = get_item_navigation_mesh_transform(idx);
133
#endif // DISABLE_DEPRECATED
134
} else if (what == "navigation_layers") {
135
r_ret = get_item_navigation_layers(idx);
136
} else if (what == "preview") {
137
r_ret = get_item_preview(idx);
138
} else {
139
return false;
140
}
141
142
return true;
143
}
144
145
void MeshLibrary::_get_property_list(List<PropertyInfo> *p_list) const {
146
for (const KeyValue<int, Item> &E : item_map) {
147
String prop_name = vformat("%s/%d/", PNAME("item"), E.key);
148
p_list->push_back(PropertyInfo(Variant::STRING, prop_name + PNAME("name")));
149
p_list->push_back(PropertyInfo(Variant::OBJECT, prop_name + PNAME("mesh"), PROPERTY_HINT_RESOURCE_TYPE, Mesh::get_class_static()));
150
p_list->push_back(PropertyInfo(Variant::TRANSFORM3D, prop_name + PNAME("mesh_transform"), PROPERTY_HINT_NONE, "suffix:m"));
151
p_list->push_back(PropertyInfo(Variant::INT, prop_name + PNAME("mesh_cast_shadow"), PROPERTY_HINT_ENUM, "Off,On,Double-Sided,Shadows Only"));
152
p_list->push_back(PropertyInfo(Variant::ARRAY, prop_name + PNAME("shapes")));
153
p_list->push_back(PropertyInfo(Variant::OBJECT, prop_name + PNAME("navigation_mesh"), PROPERTY_HINT_RESOURCE_TYPE, NavigationMesh::get_class_static()));
154
p_list->push_back(PropertyInfo(Variant::TRANSFORM3D, prop_name + PNAME("navigation_mesh_transform"), PROPERTY_HINT_NONE, "suffix:m"));
155
p_list->push_back(PropertyInfo(Variant::INT, prop_name + PNAME("navigation_layers"), PROPERTY_HINT_LAYERS_3D_NAVIGATION));
156
p_list->push_back(PropertyInfo(Variant::OBJECT, prop_name + PNAME("preview"), PROPERTY_HINT_RESOURCE_TYPE, Texture2D::get_class_static(), PROPERTY_USAGE_DEFAULT));
157
}
158
}
159
160
void MeshLibrary::create_item(int p_item) {
161
ERR_FAIL_COND(p_item < 0);
162
ERR_FAIL_COND(item_map.has(p_item));
163
item_map[p_item] = Item();
164
emit_changed();
165
notify_property_list_changed();
166
}
167
168
void MeshLibrary::set_item_name(int p_item, const String &p_name) {
169
ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
170
item_map[p_item].name = p_name;
171
emit_changed();
172
}
173
174
void MeshLibrary::set_item_mesh(int p_item, const Ref<Mesh> &p_mesh) {
175
ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
176
item_map[p_item].mesh = p_mesh;
177
emit_changed();
178
}
179
180
void MeshLibrary::set_item_mesh_transform(int p_item, const Transform3D &p_transform) {
181
ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
182
item_map[p_item].mesh_transform = p_transform;
183
emit_changed();
184
}
185
186
void MeshLibrary::set_item_mesh_cast_shadow(int p_item, RS::ShadowCastingSetting p_shadow_casting_setting) {
187
ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
188
item_map[p_item].mesh_cast_shadow = p_shadow_casting_setting;
189
emit_changed();
190
}
191
192
#ifndef PHYSICS_3D_DISABLED
193
void MeshLibrary::set_item_shapes(int p_item, const Vector<ShapeData> &p_shapes) {
194
ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
195
item_map[p_item].shapes = p_shapes;
196
emit_changed();
197
notify_property_list_changed();
198
}
199
#endif // PHYSICS_3D_DISABLED
200
201
void MeshLibrary::set_item_navigation_mesh(int p_item, const Ref<NavigationMesh> &p_navigation_mesh) {
202
ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
203
item_map[p_item].navigation_mesh = p_navigation_mesh;
204
emit_changed();
205
}
206
207
void MeshLibrary::set_item_navigation_mesh_transform(int p_item, const Transform3D &p_transform) {
208
ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
209
item_map[p_item].navigation_mesh_transform = p_transform;
210
emit_changed();
211
}
212
213
void MeshLibrary::set_item_navigation_layers(int p_item, uint32_t p_navigation_layers) {
214
ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
215
item_map[p_item].navigation_layers = p_navigation_layers;
216
emit_changed();
217
}
218
219
void MeshLibrary::set_item_preview(int p_item, const Ref<Texture2D> &p_preview) {
220
ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
221
item_map[p_item].preview = p_preview;
222
emit_changed();
223
}
224
225
String MeshLibrary::get_item_name(int p_item) const {
226
ERR_FAIL_COND_V_MSG(!item_map.has(p_item), "", "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
227
return item_map[p_item].name;
228
}
229
230
Ref<Mesh> MeshLibrary::get_item_mesh(int p_item) const {
231
ERR_FAIL_COND_V_MSG(!item_map.has(p_item), Ref<Mesh>(), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
232
return item_map[p_item].mesh;
233
}
234
235
Transform3D MeshLibrary::get_item_mesh_transform(int p_item) const {
236
ERR_FAIL_COND_V_MSG(!item_map.has(p_item), Transform3D(), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
237
return item_map[p_item].mesh_transform;
238
}
239
240
RS::ShadowCastingSetting MeshLibrary::get_item_mesh_cast_shadow(int p_item) const {
241
ERR_FAIL_COND_V_MSG(!item_map.has(p_item), RS::ShadowCastingSetting::SHADOW_CASTING_SETTING_ON, "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
242
return item_map[p_item].mesh_cast_shadow;
243
}
244
245
#ifndef PHYSICS_3D_DISABLED
246
Vector<MeshLibrary::ShapeData> MeshLibrary::get_item_shapes(int p_item) const {
247
ERR_FAIL_COND_V_MSG(!item_map.has(p_item), Vector<ShapeData>(), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
248
return item_map[p_item].shapes;
249
}
250
#endif // PHYSICS_3D_DISABLED
251
252
Ref<NavigationMesh> MeshLibrary::get_item_navigation_mesh(int p_item) const {
253
ERR_FAIL_COND_V_MSG(!item_map.has(p_item), Ref<NavigationMesh>(), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
254
return item_map[p_item].navigation_mesh;
255
}
256
257
Transform3D MeshLibrary::get_item_navigation_mesh_transform(int p_item) const {
258
ERR_FAIL_COND_V_MSG(!item_map.has(p_item), Transform3D(), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
259
return item_map[p_item].navigation_mesh_transform;
260
}
261
262
uint32_t MeshLibrary::get_item_navigation_layers(int p_item) const {
263
ERR_FAIL_COND_V_MSG(!item_map.has(p_item), 0, "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
264
return item_map[p_item].navigation_layers;
265
}
266
267
Ref<Texture2D> MeshLibrary::get_item_preview(int p_item) const {
268
ERR_FAIL_COND_V_MSG(!item_map.has(p_item), Ref<Texture2D>(), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
269
return item_map[p_item].preview;
270
}
271
272
bool MeshLibrary::has_item(int p_item) const {
273
return item_map.has(p_item);
274
}
275
276
void MeshLibrary::remove_item(int p_item) {
277
ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
278
item_map.erase(p_item);
279
notify_property_list_changed();
280
emit_changed();
281
}
282
283
void MeshLibrary::clear() {
284
item_map.clear();
285
notify_property_list_changed();
286
emit_changed();
287
}
288
289
Vector<int> MeshLibrary::get_item_list() const {
290
Vector<int> ret;
291
ret.resize(item_map.size());
292
int idx = 0;
293
for (const KeyValue<int, Item> &E : item_map) {
294
ret.write[idx++] = E.key;
295
}
296
297
return ret;
298
}
299
300
int MeshLibrary::find_item_by_name(const String &p_name) const {
301
for (const KeyValue<int, Item> &E : item_map) {
302
if (E.value.name == p_name) {
303
return E.key;
304
}
305
}
306
return -1;
307
}
308
309
int MeshLibrary::get_last_unused_item_id() const {
310
if (!item_map.size()) {
311
return 0;
312
} else {
313
return item_map.back()->key() + 1;
314
}
315
}
316
317
#ifndef PHYSICS_3D_DISABLED
318
void MeshLibrary::_set_item_shapes(int p_item, const Array &p_shapes) {
319
Array arr_shapes = p_shapes;
320
int size = p_shapes.size();
321
if (size & 1) {
322
ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
323
int prev_size = item_map[p_item].shapes.size() * 2;
324
325
if (prev_size < size) {
326
// Check if last element is a shape.
327
Ref<Shape3D> shape = arr_shapes[size - 1];
328
if (shape.is_null()) {
329
Ref<BoxShape3D> box_shape;
330
box_shape.instantiate();
331
arr_shapes[size - 1] = box_shape;
332
}
333
334
// Make sure the added element is a Transform3D.
335
arr_shapes.push_back(Transform3D());
336
size++;
337
} else {
338
size--;
339
arr_shapes.resize(size);
340
}
341
}
342
343
Vector<ShapeData> shapes;
344
for (int i = 0; i < size; i += 2) {
345
ShapeData sd;
346
sd.shape = arr_shapes[i + 0];
347
sd.local_transform = arr_shapes[i + 1];
348
349
if (sd.shape.is_valid()) {
350
shapes.push_back(sd);
351
}
352
}
353
354
set_item_shapes(p_item, shapes);
355
}
356
357
Array MeshLibrary::_get_item_shapes(int p_item) const {
358
Vector<ShapeData> shapes = get_item_shapes(p_item);
359
Array ret;
360
for (int i = 0; i < shapes.size(); i++) {
361
ret.push_back(shapes[i].shape);
362
ret.push_back(shapes[i].local_transform);
363
}
364
365
return ret;
366
}
367
#endif // PHYSICS_3D_DISABLED
368
369
void MeshLibrary::reset_state() {
370
clear();
371
}
372
void MeshLibrary::_bind_methods() {
373
ClassDB::bind_method(D_METHOD("create_item", "id"), &MeshLibrary::create_item);
374
ClassDB::bind_method(D_METHOD("set_item_name", "id", "name"), &MeshLibrary::set_item_name);
375
ClassDB::bind_method(D_METHOD("set_item_mesh", "id", "mesh"), &MeshLibrary::set_item_mesh);
376
ClassDB::bind_method(D_METHOD("set_item_mesh_transform", "id", "mesh_transform"), &MeshLibrary::set_item_mesh_transform);
377
ClassDB::bind_method(D_METHOD("set_item_mesh_cast_shadow", "id", "shadow_casting_setting"), &MeshLibrary::set_item_mesh_cast_shadow);
378
ClassDB::bind_method(D_METHOD("set_item_navigation_mesh", "id", "navigation_mesh"), &MeshLibrary::set_item_navigation_mesh);
379
ClassDB::bind_method(D_METHOD("set_item_navigation_mesh_transform", "id", "navigation_mesh"), &MeshLibrary::set_item_navigation_mesh_transform);
380
ClassDB::bind_method(D_METHOD("set_item_navigation_layers", "id", "navigation_layers"), &MeshLibrary::set_item_navigation_layers);
381
#ifndef PHYSICS_3D_DISABLED
382
ClassDB::bind_method(D_METHOD("set_item_shapes", "id", "shapes"), &MeshLibrary::_set_item_shapes);
383
#endif // PHYSICS_3D_DISABLED
384
ClassDB::bind_method(D_METHOD("set_item_preview", "id", "texture"), &MeshLibrary::set_item_preview);
385
ClassDB::bind_method(D_METHOD("get_item_name", "id"), &MeshLibrary::get_item_name);
386
ClassDB::bind_method(D_METHOD("get_item_mesh", "id"), &MeshLibrary::get_item_mesh);
387
ClassDB::bind_method(D_METHOD("get_item_mesh_transform", "id"), &MeshLibrary::get_item_mesh_transform);
388
ClassDB::bind_method(D_METHOD("get_item_mesh_cast_shadow", "id"), &MeshLibrary::get_item_mesh_cast_shadow);
389
ClassDB::bind_method(D_METHOD("get_item_navigation_mesh", "id"), &MeshLibrary::get_item_navigation_mesh);
390
ClassDB::bind_method(D_METHOD("get_item_navigation_mesh_transform", "id"), &MeshLibrary::get_item_navigation_mesh_transform);
391
ClassDB::bind_method(D_METHOD("get_item_navigation_layers", "id"), &MeshLibrary::get_item_navigation_layers);
392
#ifndef PHYSICS_3D_DISABLED
393
ClassDB::bind_method(D_METHOD("get_item_shapes", "id"), &MeshLibrary::_get_item_shapes);
394
#endif // PHYSICS_3D_DISABLED
395
ClassDB::bind_method(D_METHOD("get_item_preview", "id"), &MeshLibrary::get_item_preview);
396
ClassDB::bind_method(D_METHOD("remove_item", "id"), &MeshLibrary::remove_item);
397
ClassDB::bind_method(D_METHOD("find_item_by_name", "name"), &MeshLibrary::find_item_by_name);
398
399
ClassDB::bind_method(D_METHOD("clear"), &MeshLibrary::clear);
400
ClassDB::bind_method(D_METHOD("get_item_list"), &MeshLibrary::get_item_list);
401
ClassDB::bind_method(D_METHOD("get_last_unused_item_id"), &MeshLibrary::get_last_unused_item_id);
402
}
403
404
MeshLibrary::MeshLibrary() {
405
}
406
407
MeshLibrary::~MeshLibrary() {
408
}
409
410