Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/3d/mesh_instance_3d.cpp
9896 views
1
/**************************************************************************/
2
/* mesh_instance_3d.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_instance_3d.h"
32
33
#include "scene/3d/skeleton_3d.h"
34
35
#ifndef PHYSICS_3D_DISABLED
36
#include "scene/3d/physics/collision_shape_3d.h"
37
#include "scene/3d/physics/static_body_3d.h"
38
#include "scene/resources/3d/concave_polygon_shape_3d.h"
39
#include "scene/resources/3d/convex_polygon_shape_3d.h"
40
#endif // PHYSICS_3D_DISABLED
41
42
#ifndef NAVIGATION_3D_DISABLED
43
#include "scene/resources/3d/navigation_mesh_source_geometry_data_3d.h"
44
#include "scene/resources/navigation_mesh.h"
45
#include "servers/navigation_server_3d.h"
46
47
Callable MeshInstance3D::_navmesh_source_geometry_parsing_callback;
48
RID MeshInstance3D::_navmesh_source_geometry_parser;
49
#endif // NAVIGATION_3D_DISABLED
50
51
bool MeshInstance3D::_set(const StringName &p_name, const Variant &p_value) {
52
//this is not _too_ bad performance wise, really. it only arrives here if the property was not set anywhere else.
53
//add to it that it's probably found on first call to _set anyway.
54
55
if (!get_instance().is_valid()) {
56
return false;
57
}
58
59
HashMap<StringName, int>::Iterator E = blend_shape_properties.find(p_name);
60
if (E) {
61
set_blend_shape_value(E->value, p_value);
62
return true;
63
}
64
65
if (p_name.operator String().begins_with("surface_material_override/")) {
66
int idx = p_name.operator String().get_slicec('/', 1).to_int();
67
68
if (idx >= surface_override_materials.size() || idx < 0) {
69
return false;
70
}
71
72
set_surface_override_material(idx, p_value);
73
return true;
74
}
75
76
return false;
77
}
78
79
bool MeshInstance3D::_get(const StringName &p_name, Variant &r_ret) const {
80
if (!get_instance().is_valid()) {
81
return false;
82
}
83
84
HashMap<StringName, int>::ConstIterator E = blend_shape_properties.find(p_name);
85
if (E) {
86
r_ret = get_blend_shape_value(E->value);
87
return true;
88
}
89
90
if (p_name.operator String().begins_with("surface_material_override/")) {
91
int idx = p_name.operator String().get_slicec('/', 1).to_int();
92
if (idx >= surface_override_materials.size() || idx < 0) {
93
return false;
94
}
95
r_ret = surface_override_materials[idx];
96
return true;
97
}
98
return false;
99
}
100
101
void MeshInstance3D::_get_property_list(List<PropertyInfo> *p_list) const {
102
for (uint32_t i = 0; i < blend_shape_tracks.size(); i++) {
103
p_list->push_back(PropertyInfo(Variant::FLOAT, vformat("blend_shapes/%s", String(mesh->get_blend_shape_name(i))), PROPERTY_HINT_RANGE, "-1,1,0.00001"));
104
}
105
if (mesh.is_valid()) {
106
for (int i = 0; i < mesh->get_surface_count(); i++) {
107
p_list->push_back(PropertyInfo(Variant::OBJECT, vformat("%s/%d", PNAME("surface_material_override"), i), PROPERTY_HINT_RESOURCE_TYPE, "BaseMaterial3D,ShaderMaterial", PROPERTY_USAGE_DEFAULT));
108
}
109
}
110
}
111
112
void MeshInstance3D::set_mesh(const Ref<Mesh> &p_mesh) {
113
if (mesh == p_mesh) {
114
return;
115
}
116
117
if (mesh.is_valid()) {
118
mesh->disconnect_changed(callable_mp(this, &MeshInstance3D::_mesh_changed));
119
}
120
121
mesh = p_mesh;
122
123
if (mesh.is_valid()) {
124
// If mesh is a PrimitiveMesh, calling get_rid on it can trigger a changed callback
125
// so do this before connecting _mesh_changed.
126
set_base(mesh->get_rid());
127
mesh->connect_changed(callable_mp(this, &MeshInstance3D::_mesh_changed));
128
_mesh_changed();
129
} else {
130
blend_shape_tracks.clear();
131
blend_shape_properties.clear();
132
set_base(RID());
133
update_gizmos();
134
}
135
136
notify_property_list_changed();
137
}
138
139
Ref<Mesh> MeshInstance3D::get_mesh() const {
140
return mesh;
141
}
142
143
int MeshInstance3D::get_blend_shape_count() const {
144
if (mesh.is_null()) {
145
return 0;
146
}
147
return mesh->get_blend_shape_count();
148
}
149
150
int MeshInstance3D::find_blend_shape_by_name(const StringName &p_name) {
151
if (mesh.is_null()) {
152
return -1;
153
}
154
for (int i = 0; i < mesh->get_blend_shape_count(); i++) {
155
if (mesh->get_blend_shape_name(i) == p_name) {
156
return i;
157
}
158
}
159
return -1;
160
}
161
162
float MeshInstance3D::get_blend_shape_value(int p_blend_shape) const {
163
ERR_FAIL_COND_V(mesh.is_null(), 0.0);
164
ERR_FAIL_INDEX_V(p_blend_shape, (int)blend_shape_tracks.size(), 0);
165
return blend_shape_tracks[p_blend_shape];
166
}
167
168
void MeshInstance3D::set_blend_shape_value(int p_blend_shape, float p_value) {
169
ERR_FAIL_COND(mesh.is_null());
170
ERR_FAIL_INDEX(p_blend_shape, (int)blend_shape_tracks.size());
171
blend_shape_tracks[p_blend_shape] = p_value;
172
RenderingServer::get_singleton()->instance_set_blend_shape_weight(get_instance(), p_blend_shape, p_value);
173
}
174
175
void MeshInstance3D::_resolve_skeleton_path() {
176
Ref<SkinReference> new_skin_reference;
177
178
if (!skeleton_path.is_empty()) {
179
Skeleton3D *skeleton = Object::cast_to<Skeleton3D>(get_node_or_null(skeleton_path)); // skeleton_path may be outdated when reparenting.
180
if (skeleton) {
181
if (skin_internal.is_null()) {
182
new_skin_reference = skeleton->register_skin(skeleton->create_skin_from_rest_transforms());
183
//a skin was created for us
184
skin_internal = new_skin_reference->get_skin();
185
notify_property_list_changed();
186
} else {
187
new_skin_reference = skeleton->register_skin(skin_internal);
188
}
189
}
190
}
191
192
skin_ref = new_skin_reference;
193
194
if (skin_ref.is_valid()) {
195
RenderingServer::get_singleton()->instance_attach_skeleton(get_instance(), skin_ref->get_skeleton());
196
} else {
197
RenderingServer::get_singleton()->instance_attach_skeleton(get_instance(), RID());
198
}
199
}
200
201
void MeshInstance3D::set_skin(const Ref<Skin> &p_skin) {
202
skin_internal = p_skin;
203
skin = p_skin;
204
if (!is_inside_tree()) {
205
return;
206
}
207
_resolve_skeleton_path();
208
}
209
210
Ref<Skin> MeshInstance3D::get_skin() const {
211
return skin;
212
}
213
214
Ref<SkinReference> MeshInstance3D::get_skin_reference() const {
215
return skin_ref;
216
}
217
218
void MeshInstance3D::set_skeleton_path(const NodePath &p_skeleton) {
219
skeleton_path = p_skeleton;
220
if (!is_inside_tree()) {
221
return;
222
}
223
_resolve_skeleton_path();
224
}
225
226
NodePath MeshInstance3D::get_skeleton_path() {
227
return skeleton_path;
228
}
229
230
AABB MeshInstance3D::get_aabb() const {
231
if (mesh.is_valid()) {
232
return mesh->get_aabb();
233
}
234
235
return AABB();
236
}
237
238
#ifndef PHYSICS_3D_DISABLED
239
Node *MeshInstance3D::create_trimesh_collision_node() {
240
if (mesh.is_null()) {
241
return nullptr;
242
}
243
244
Ref<ConcavePolygonShape3D> shape = mesh->create_trimesh_shape();
245
if (shape.is_null()) {
246
return nullptr;
247
}
248
249
StaticBody3D *static_body = memnew(StaticBody3D);
250
CollisionShape3D *cshape = memnew(CollisionShape3D);
251
cshape->set_shape(shape);
252
static_body->add_child(cshape, true);
253
return static_body;
254
}
255
256
void MeshInstance3D::create_trimesh_collision() {
257
StaticBody3D *static_body = Object::cast_to<StaticBody3D>(create_trimesh_collision_node());
258
ERR_FAIL_NULL(static_body);
259
static_body->set_name(String(get_name()) + "_col");
260
261
add_child(static_body, true);
262
if (get_owner()) {
263
CollisionShape3D *cshape = Object::cast_to<CollisionShape3D>(static_body->get_child(0));
264
static_body->set_owner(get_owner());
265
cshape->set_owner(get_owner());
266
}
267
}
268
269
Node *MeshInstance3D::create_convex_collision_node(bool p_clean, bool p_simplify) {
270
if (mesh.is_null()) {
271
return nullptr;
272
}
273
274
Ref<ConvexPolygonShape3D> shape = mesh->create_convex_shape(p_clean, p_simplify);
275
if (shape.is_null()) {
276
return nullptr;
277
}
278
279
StaticBody3D *static_body = memnew(StaticBody3D);
280
CollisionShape3D *cshape = memnew(CollisionShape3D);
281
cshape->set_shape(shape);
282
static_body->add_child(cshape, true);
283
return static_body;
284
}
285
286
void MeshInstance3D::create_convex_collision(bool p_clean, bool p_simplify) {
287
StaticBody3D *static_body = Object::cast_to<StaticBody3D>(create_convex_collision_node(p_clean, p_simplify));
288
ERR_FAIL_NULL(static_body);
289
static_body->set_name(String(get_name()) + "_col");
290
291
add_child(static_body, true);
292
if (get_owner()) {
293
CollisionShape3D *cshape = Object::cast_to<CollisionShape3D>(static_body->get_child(0));
294
static_body->set_owner(get_owner());
295
cshape->set_owner(get_owner());
296
}
297
}
298
299
Node *MeshInstance3D::create_multiple_convex_collisions_node(const Ref<MeshConvexDecompositionSettings> &p_settings) {
300
if (mesh.is_null()) {
301
return nullptr;
302
}
303
304
Ref<MeshConvexDecompositionSettings> settings;
305
if (p_settings.is_valid()) {
306
settings = p_settings;
307
} else {
308
settings.instantiate();
309
}
310
311
Vector<Ref<Shape3D>> shapes = mesh->convex_decompose(settings);
312
if (!shapes.size()) {
313
return nullptr;
314
}
315
316
StaticBody3D *static_body = memnew(StaticBody3D);
317
for (int i = 0; i < shapes.size(); i++) {
318
CollisionShape3D *cshape = memnew(CollisionShape3D);
319
cshape->set_shape(shapes[i]);
320
static_body->add_child(cshape, true);
321
}
322
return static_body;
323
}
324
325
void MeshInstance3D::create_multiple_convex_collisions(const Ref<MeshConvexDecompositionSettings> &p_settings) {
326
StaticBody3D *static_body = Object::cast_to<StaticBody3D>(create_multiple_convex_collisions_node(p_settings));
327
ERR_FAIL_NULL(static_body);
328
static_body->set_name(String(get_name()) + "_col");
329
330
add_child(static_body, true);
331
if (get_owner()) {
332
static_body->set_owner(get_owner());
333
int count = static_body->get_child_count();
334
for (int i = 0; i < count; i++) {
335
CollisionShape3D *cshape = Object::cast_to<CollisionShape3D>(static_body->get_child(i));
336
cshape->set_owner(get_owner());
337
}
338
}
339
}
340
#endif // PHYSICS_3D_DISABLED
341
342
void MeshInstance3D::_notification(int p_what) {
343
switch (p_what) {
344
case NOTIFICATION_ENTER_TREE: {
345
_resolve_skeleton_path();
346
} break;
347
case NOTIFICATION_TRANSLATION_CHANGED: {
348
if (mesh.is_valid()) {
349
mesh->notification(NOTIFICATION_TRANSLATION_CHANGED);
350
}
351
} break;
352
}
353
}
354
355
int MeshInstance3D::get_surface_override_material_count() const {
356
return surface_override_materials.size();
357
}
358
359
void MeshInstance3D::set_surface_override_material(int p_surface, const Ref<Material> &p_material) {
360
ERR_FAIL_INDEX(p_surface, surface_override_materials.size());
361
362
surface_override_materials.write[p_surface] = p_material;
363
364
if (surface_override_materials[p_surface].is_valid()) {
365
RS::get_singleton()->instance_set_surface_override_material(get_instance(), p_surface, surface_override_materials[p_surface]->get_rid());
366
} else {
367
RS::get_singleton()->instance_set_surface_override_material(get_instance(), p_surface, RID());
368
}
369
}
370
371
Ref<Material> MeshInstance3D::get_surface_override_material(int p_surface) const {
372
ERR_FAIL_INDEX_V(p_surface, surface_override_materials.size(), Ref<Material>());
373
374
return surface_override_materials[p_surface];
375
}
376
377
Ref<Material> MeshInstance3D::get_active_material(int p_surface) const {
378
Ref<Material> mat_override = get_material_override();
379
if (mat_override.is_valid()) {
380
return mat_override;
381
}
382
383
Ref<Mesh> m = get_mesh();
384
if (m.is_null() || m->get_surface_count() == 0) {
385
return Ref<Material>();
386
}
387
388
Ref<Material> surface_material = get_surface_override_material(p_surface);
389
if (surface_material.is_valid()) {
390
return surface_material;
391
}
392
393
return m->surface_get_material(p_surface);
394
}
395
396
void MeshInstance3D::_mesh_changed() {
397
ERR_FAIL_COND(mesh.is_null());
398
const int surface_count = mesh->get_surface_count();
399
400
surface_override_materials.resize(surface_count);
401
402
uint32_t initialize_bs_from = blend_shape_tracks.size();
403
blend_shape_tracks.resize(mesh->get_blend_shape_count());
404
405
if (surface_count > 0) {
406
for (uint32_t i = 0; i < blend_shape_tracks.size(); i++) {
407
blend_shape_properties["blend_shapes/" + String(mesh->get_blend_shape_name(i))] = i;
408
if (i < initialize_bs_from) {
409
set_blend_shape_value(i, blend_shape_tracks[i]);
410
} else {
411
set_blend_shape_value(i, 0);
412
}
413
}
414
}
415
416
for (int surface_index = 0; surface_index < surface_count; ++surface_index) {
417
if (surface_override_materials[surface_index].is_valid()) {
418
RS::get_singleton()->instance_set_surface_override_material(get_instance(), surface_index, surface_override_materials[surface_index]->get_rid());
419
}
420
}
421
422
update_gizmos();
423
}
424
425
MeshInstance3D *MeshInstance3D::create_debug_tangents_node() {
426
Vector<Vector3> lines;
427
Vector<Color> colors;
428
429
Ref<Mesh> m = get_mesh();
430
if (m.is_null()) {
431
return nullptr;
432
}
433
434
for (int i = 0; i < m->get_surface_count(); i++) {
435
Array arrays = m->surface_get_arrays(i);
436
ERR_CONTINUE(arrays.size() != Mesh::ARRAY_MAX);
437
438
Vector<Vector3> verts = arrays[Mesh::ARRAY_VERTEX];
439
Vector<Vector3> norms = arrays[Mesh::ARRAY_NORMAL];
440
if (norms.is_empty()) {
441
continue;
442
}
443
Vector<float> tangents = arrays[Mesh::ARRAY_TANGENT];
444
if (tangents.is_empty()) {
445
continue;
446
}
447
448
for (int j = 0; j < verts.size(); j++) {
449
Vector3 v = verts[j];
450
Vector3 n = norms[j];
451
Vector3 t = Vector3(tangents[j * 4 + 0], tangents[j * 4 + 1], tangents[j * 4 + 2]);
452
Vector3 b = (n.cross(t)).normalized() * tangents[j * 4 + 3];
453
454
lines.push_back(v); //normal
455
colors.push_back(Color(0, 0, 1)); //color
456
lines.push_back(v + n * 0.04); //normal
457
colors.push_back(Color(0, 0, 1)); //color
458
459
lines.push_back(v); //tangent
460
colors.push_back(Color(1, 0, 0)); //color
461
lines.push_back(v + t * 0.04); //tangent
462
colors.push_back(Color(1, 0, 0)); //color
463
464
lines.push_back(v); //binormal
465
colors.push_back(Color(0, 1, 0)); //color
466
lines.push_back(v + b * 0.04); //binormal
467
colors.push_back(Color(0, 1, 0)); //color
468
}
469
}
470
471
if (lines.size()) {
472
Ref<StandardMaterial3D> sm;
473
sm.instantiate();
474
475
sm->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
476
sm->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
477
sm->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
478
sm->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
479
480
Ref<ArrayMesh> am;
481
am.instantiate();
482
Array a;
483
a.resize(Mesh::ARRAY_MAX);
484
a[Mesh::ARRAY_VERTEX] = lines;
485
a[Mesh::ARRAY_COLOR] = colors;
486
487
am->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, a);
488
am->surface_set_material(0, sm);
489
490
MeshInstance3D *mi = memnew(MeshInstance3D);
491
mi->set_mesh(am);
492
mi->set_name("DebugTangents");
493
return mi;
494
}
495
496
return nullptr;
497
}
498
499
void MeshInstance3D::create_debug_tangents() {
500
MeshInstance3D *mi = create_debug_tangents_node();
501
if (!mi) {
502
return;
503
}
504
505
add_child(mi, true);
506
if (is_inside_tree() && this == get_tree()->get_edited_scene_root()) {
507
mi->set_owner(this);
508
} else {
509
mi->set_owner(get_owner());
510
}
511
}
512
513
bool MeshInstance3D::_property_can_revert(const StringName &p_name) const {
514
HashMap<StringName, int>::ConstIterator E = blend_shape_properties.find(p_name);
515
if (E) {
516
return true;
517
}
518
return false;
519
}
520
521
bool MeshInstance3D::_property_get_revert(const StringName &p_name, Variant &r_property) const {
522
HashMap<StringName, int>::ConstIterator E = blend_shape_properties.find(p_name);
523
if (E) {
524
r_property = 0.0f;
525
return true;
526
}
527
return false;
528
}
529
530
Ref<ArrayMesh> MeshInstance3D::bake_mesh_from_current_blend_shape_mix(Ref<ArrayMesh> p_existing) {
531
Ref<ArrayMesh> source_mesh = get_mesh();
532
ERR_FAIL_COND_V_MSG(source_mesh.is_null(), Ref<ArrayMesh>(), "The source mesh must be a valid ArrayMesh.");
533
534
Ref<ArrayMesh> bake_mesh;
535
536
if (p_existing.is_valid()) {
537
ERR_FAIL_COND_V_MSG(p_existing.is_null(), Ref<ArrayMesh>(), "The existing mesh must be a valid ArrayMesh.");
538
ERR_FAIL_COND_V_MSG(source_mesh == p_existing, Ref<ArrayMesh>(), "The source mesh can not be the same mesh as the existing mesh.");
539
540
bake_mesh = p_existing;
541
} else {
542
bake_mesh.instantiate();
543
}
544
545
Mesh::BlendShapeMode blend_shape_mode = source_mesh->get_blend_shape_mode();
546
int mesh_surface_count = source_mesh->get_surface_count();
547
548
bake_mesh->clear_surfaces();
549
bake_mesh->set_blend_shape_mode(blend_shape_mode);
550
551
for (int surface_index = 0; surface_index < mesh_surface_count; surface_index++) {
552
uint32_t surface_format = source_mesh->surface_get_format(surface_index);
553
554
ERR_CONTINUE(0 == (surface_format & Mesh::ARRAY_FORMAT_VERTEX));
555
556
const Array &source_mesh_arrays = source_mesh->surface_get_arrays(surface_index);
557
558
ERR_FAIL_COND_V(source_mesh_arrays.size() != RS::ARRAY_MAX, Ref<ArrayMesh>());
559
560
const Vector<Vector3> &source_mesh_vertex_array = source_mesh_arrays[Mesh::ARRAY_VERTEX];
561
const Vector<Vector3> &source_mesh_normal_array = source_mesh_arrays[Mesh::ARRAY_NORMAL];
562
const Vector<float> &source_mesh_tangent_array = source_mesh_arrays[Mesh::ARRAY_TANGENT];
563
564
Array new_mesh_arrays;
565
new_mesh_arrays.resize(Mesh::ARRAY_MAX);
566
for (int i = 0; i < source_mesh_arrays.size(); i++) {
567
if (i == Mesh::ARRAY_VERTEX || i == Mesh::ARRAY_NORMAL || i == Mesh::ARRAY_TANGENT) {
568
continue;
569
}
570
new_mesh_arrays[i] = source_mesh_arrays[i];
571
}
572
573
bool use_normal_array = source_mesh_normal_array.size() == source_mesh_vertex_array.size();
574
bool use_tangent_array = source_mesh_tangent_array.size() / 4 == source_mesh_vertex_array.size();
575
576
Vector<Vector3> lerped_vertex_array = source_mesh_vertex_array;
577
Vector<Vector3> lerped_normal_array = source_mesh_normal_array;
578
Vector<float> lerped_tangent_array = source_mesh_tangent_array;
579
580
const Vector3 *source_vertices_ptr = source_mesh_vertex_array.ptr();
581
const Vector3 *source_normals_ptr = source_mesh_normal_array.ptr();
582
const float *source_tangents_ptr = source_mesh_tangent_array.ptr();
583
584
Vector3 *lerped_vertices_ptrw = lerped_vertex_array.ptrw();
585
Vector3 *lerped_normals_ptrw = lerped_normal_array.ptrw();
586
float *lerped_tangents_ptrw = lerped_tangent_array.ptrw();
587
588
const Array &blendshapes_mesh_arrays = source_mesh->surface_get_blend_shape_arrays(surface_index);
589
int blend_shape_count = source_mesh->get_blend_shape_count();
590
ERR_FAIL_COND_V(blendshapes_mesh_arrays.size() != blend_shape_count, Ref<ArrayMesh>());
591
592
for (int blendshape_index = 0; blendshape_index < blend_shape_count; blendshape_index++) {
593
float blend_weight = get_blend_shape_value(blendshape_index);
594
if (std::abs(blend_weight) <= 0.0001) {
595
continue;
596
}
597
598
const Array &blendshape_mesh_arrays = blendshapes_mesh_arrays[blendshape_index];
599
600
const Vector<Vector3> &blendshape_vertex_array = blendshape_mesh_arrays[Mesh::ARRAY_VERTEX];
601
const Vector<Vector3> &blendshape_normal_array = blendshape_mesh_arrays[Mesh::ARRAY_NORMAL];
602
const Vector<float> &blendshape_tangent_array = blendshape_mesh_arrays[Mesh::ARRAY_TANGENT];
603
604
ERR_FAIL_COND_V(source_mesh_vertex_array.size() != blendshape_vertex_array.size(), Ref<ArrayMesh>());
605
ERR_FAIL_COND_V(source_mesh_normal_array.size() != blendshape_normal_array.size(), Ref<ArrayMesh>());
606
ERR_FAIL_COND_V(source_mesh_tangent_array.size() != blendshape_tangent_array.size(), Ref<ArrayMesh>());
607
608
const Vector3 *blendshape_vertices_ptr = blendshape_vertex_array.ptr();
609
const Vector3 *blendshape_normals_ptr = blendshape_normal_array.ptr();
610
const float *blendshape_tangents_ptr = blendshape_tangent_array.ptr();
611
612
if (blend_shape_mode == Mesh::BLEND_SHAPE_MODE_NORMALIZED) {
613
for (int i = 0; i < source_mesh_vertex_array.size(); i++) {
614
const Vector3 &source_vertex = source_vertices_ptr[i];
615
const Vector3 &blendshape_vertex = blendshape_vertices_ptr[i];
616
Vector3 lerped_vertex = source_vertex.lerp(blendshape_vertex, blend_weight) - source_vertex;
617
lerped_vertices_ptrw[i] += lerped_vertex;
618
619
if (use_normal_array) {
620
const Vector3 &source_normal = source_normals_ptr[i];
621
const Vector3 &blendshape_normal = blendshape_normals_ptr[i];
622
Vector3 lerped_normal = source_normal.lerp(blendshape_normal, blend_weight) - source_normal;
623
lerped_normals_ptrw[i] += lerped_normal;
624
}
625
626
if (use_tangent_array) {
627
int tangent_index = i * 4;
628
const Vector4 source_tangent = Vector4(
629
source_tangents_ptr[tangent_index],
630
source_tangents_ptr[tangent_index + 1],
631
source_tangents_ptr[tangent_index + 2],
632
source_tangents_ptr[tangent_index + 3]);
633
const Vector4 blendshape_tangent = Vector4(
634
blendshape_tangents_ptr[tangent_index],
635
blendshape_tangents_ptr[tangent_index + 1],
636
blendshape_tangents_ptr[tangent_index + 2],
637
blendshape_tangents_ptr[tangent_index + 3]);
638
Vector4 lerped_tangent = source_tangent.lerp(blendshape_tangent, blend_weight);
639
lerped_tangents_ptrw[tangent_index] += lerped_tangent.x;
640
lerped_tangents_ptrw[tangent_index + 1] += lerped_tangent.y;
641
lerped_tangents_ptrw[tangent_index + 2] += lerped_tangent.z;
642
lerped_tangents_ptrw[tangent_index + 3] += lerped_tangent.w;
643
}
644
}
645
} else if (blend_shape_mode == Mesh::BLEND_SHAPE_MODE_RELATIVE) {
646
for (int i = 0; i < source_mesh_vertex_array.size(); i++) {
647
const Vector3 &blendshape_vertex = blendshape_vertices_ptr[i];
648
lerped_vertices_ptrw[i] += blendshape_vertex * blend_weight;
649
650
if (use_normal_array) {
651
const Vector3 &blendshape_normal = blendshape_normals_ptr[i];
652
lerped_normals_ptrw[i] += blendshape_normal * blend_weight;
653
}
654
655
if (use_tangent_array) {
656
int tangent_index = i * 4;
657
const Vector4 blendshape_tangent = Vector4(
658
blendshape_tangents_ptr[tangent_index],
659
blendshape_tangents_ptr[tangent_index + 1],
660
blendshape_tangents_ptr[tangent_index + 2],
661
blendshape_tangents_ptr[tangent_index + 3]);
662
Vector4 lerped_tangent = blendshape_tangent * blend_weight;
663
lerped_tangents_ptrw[tangent_index] += lerped_tangent.x;
664
lerped_tangents_ptrw[tangent_index + 1] += lerped_tangent.y;
665
lerped_tangents_ptrw[tangent_index + 2] += lerped_tangent.z;
666
lerped_tangents_ptrw[tangent_index + 3] += lerped_tangent.w;
667
}
668
}
669
}
670
}
671
672
new_mesh_arrays[Mesh::ARRAY_VERTEX] = lerped_vertex_array;
673
if (use_normal_array) {
674
new_mesh_arrays[Mesh::ARRAY_NORMAL] = lerped_normal_array;
675
}
676
if (use_tangent_array) {
677
new_mesh_arrays[Mesh::ARRAY_TANGENT] = lerped_tangent_array;
678
}
679
680
bake_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, new_mesh_arrays, Array(), Dictionary(), surface_format);
681
}
682
683
return bake_mesh;
684
}
685
686
Ref<ArrayMesh> MeshInstance3D::bake_mesh_from_current_skeleton_pose(Ref<ArrayMesh> p_existing) {
687
Ref<ArrayMesh> source_mesh = get_mesh();
688
ERR_FAIL_COND_V_MSG(source_mesh.is_null(), Ref<ArrayMesh>(), "The source mesh must be a valid ArrayMesh.");
689
690
Ref<ArrayMesh> bake_mesh;
691
692
if (p_existing.is_valid()) {
693
ERR_FAIL_COND_V_MSG(source_mesh == p_existing, Ref<ArrayMesh>(), "The source mesh can not be the same mesh as the existing mesh.");
694
695
bake_mesh = p_existing;
696
} else {
697
bake_mesh.instantiate();
698
}
699
700
ERR_FAIL_COND_V_MSG(skin_ref.is_null(), Ref<ArrayMesh>(), "The source mesh must have a valid skin.");
701
ERR_FAIL_COND_V_MSG(skin_internal.is_null(), Ref<ArrayMesh>(), "The source mesh must have a valid skin.");
702
RID skeleton = skin_ref->get_skeleton();
703
ERR_FAIL_COND_V_MSG(!skeleton.is_valid(), Ref<ArrayMesh>(), "The source mesh must have its skin registered with a valid skeleton.");
704
705
const int bone_count = RenderingServer::get_singleton()->skeleton_get_bone_count(skeleton);
706
ERR_FAIL_COND_V(bone_count <= 0, Ref<ArrayMesh>());
707
ERR_FAIL_COND_V(bone_count < skin_internal->get_bind_count(), Ref<ArrayMesh>());
708
709
LocalVector<Transform3D> bone_transforms;
710
bone_transforms.resize(bone_count);
711
for (int bone_index = 0; bone_index < bone_count; bone_index++) {
712
bone_transforms[bone_index] = RenderingServer::get_singleton()->skeleton_bone_get_transform(skeleton, bone_index);
713
}
714
715
bake_mesh->clear_surfaces();
716
717
int mesh_surface_count = source_mesh->get_surface_count();
718
719
for (int surface_index = 0; surface_index < mesh_surface_count; surface_index++) {
720
ERR_CONTINUE(source_mesh->surface_get_primitive_type(surface_index) != Mesh::PRIMITIVE_TRIANGLES);
721
722
uint32_t surface_format = source_mesh->surface_get_format(surface_index);
723
724
ERR_CONTINUE(0 == (surface_format & Mesh::ARRAY_FORMAT_VERTEX));
725
ERR_CONTINUE(0 == (surface_format & Mesh::ARRAY_FORMAT_BONES));
726
ERR_CONTINUE(0 == (surface_format & Mesh::ARRAY_FORMAT_WEIGHTS));
727
728
unsigned int bones_per_vertex = surface_format & Mesh::ARRAY_FLAG_USE_8_BONE_WEIGHTS ? 8 : 4;
729
730
surface_format &= ~Mesh::ARRAY_FORMAT_BONES;
731
surface_format &= ~Mesh::ARRAY_FORMAT_WEIGHTS;
732
733
const Array &source_mesh_arrays = source_mesh->surface_get_arrays(surface_index);
734
735
ERR_FAIL_COND_V(source_mesh_arrays.size() != RS::ARRAY_MAX, Ref<ArrayMesh>());
736
737
const Vector<Vector3> &source_mesh_vertex_array = source_mesh_arrays[Mesh::ARRAY_VERTEX];
738
const Vector<Vector3> &source_mesh_normal_array = source_mesh_arrays[Mesh::ARRAY_NORMAL];
739
const Vector<float> &source_mesh_tangent_array = source_mesh_arrays[Mesh::ARRAY_TANGENT];
740
const Vector<int> &source_mesh_bones_array = source_mesh_arrays[Mesh::ARRAY_BONES];
741
const Vector<float> &source_mesh_weights_array = source_mesh_arrays[Mesh::ARRAY_WEIGHTS];
742
743
unsigned int vertex_count = source_mesh_vertex_array.size();
744
int expected_bone_array_size = vertex_count * bones_per_vertex;
745
ERR_CONTINUE(source_mesh_bones_array.size() != expected_bone_array_size);
746
ERR_CONTINUE(source_mesh_weights_array.size() != expected_bone_array_size);
747
748
Array new_mesh_arrays;
749
new_mesh_arrays.resize(Mesh::ARRAY_MAX);
750
for (int i = 0; i < source_mesh_arrays.size(); i++) {
751
if (i == Mesh::ARRAY_VERTEX || i == Mesh::ARRAY_NORMAL || i == Mesh::ARRAY_TANGENT || i == Mesh::ARRAY_BONES || i == Mesh::ARRAY_WEIGHTS) {
752
continue;
753
}
754
new_mesh_arrays[i] = source_mesh_arrays[i];
755
}
756
757
bool use_normal_array = source_mesh_normal_array.size() == source_mesh_vertex_array.size();
758
bool use_tangent_array = source_mesh_tangent_array.size() / 4 == source_mesh_vertex_array.size();
759
760
Vector<Vector3> lerped_vertex_array = source_mesh_vertex_array;
761
Vector<Vector3> lerped_normal_array = source_mesh_normal_array;
762
Vector<float> lerped_tangent_array = source_mesh_tangent_array;
763
764
const Vector3 *source_vertices_ptr = source_mesh_vertex_array.ptr();
765
const Vector3 *source_normals_ptr = source_mesh_normal_array.ptr();
766
const float *source_tangents_ptr = source_mesh_tangent_array.ptr();
767
const int *source_bones_ptr = source_mesh_bones_array.ptr();
768
const float *source_weights_ptr = source_mesh_weights_array.ptr();
769
770
Vector3 *lerped_vertices_ptrw = lerped_vertex_array.ptrw();
771
Vector3 *lerped_normals_ptrw = lerped_normal_array.ptrw();
772
float *lerped_tangents_ptrw = lerped_tangent_array.ptrw();
773
774
for (unsigned int vertex_index = 0; vertex_index < vertex_count; vertex_index++) {
775
Vector3 lerped_vertex;
776
Vector3 lerped_normal;
777
Vector3 lerped_tangent;
778
779
const Vector3 &source_vertex = source_vertices_ptr[vertex_index];
780
781
Vector3 source_normal;
782
if (use_normal_array) {
783
source_normal = source_normals_ptr[vertex_index];
784
}
785
786
int tangent_index = vertex_index * 4;
787
Vector4 source_tangent;
788
Vector3 source_tangent_vec3;
789
if (use_tangent_array) {
790
source_tangent = Vector4(
791
source_tangents_ptr[tangent_index],
792
source_tangents_ptr[tangent_index + 1],
793
source_tangents_ptr[tangent_index + 2],
794
source_tangents_ptr[tangent_index + 3]);
795
796
DEV_ASSERT(source_tangent.w == 1.0 || source_tangent.w == -1.0);
797
798
source_tangent_vec3 = Vector3(source_tangent.x, source_tangent.y, source_tangent.z);
799
}
800
801
for (unsigned int weight_index = 0; weight_index < bones_per_vertex; weight_index++) {
802
float bone_weight = source_weights_ptr[vertex_index * bones_per_vertex + weight_index];
803
if (bone_weight < FLT_EPSILON) {
804
continue;
805
}
806
int vertex_bone_index = source_bones_ptr[vertex_index * bones_per_vertex + weight_index];
807
const Transform3D &bone_transform = bone_transforms[vertex_bone_index];
808
const Basis bone_basis = bone_transform.basis.orthonormalized();
809
810
ERR_FAIL_INDEX_V(vertex_bone_index, static_cast<int>(bone_transforms.size()), Ref<ArrayMesh>());
811
812
lerped_vertex += source_vertex.lerp(bone_transform.xform(source_vertex), bone_weight) - source_vertex;
813
;
814
815
if (use_normal_array) {
816
lerped_normal += source_normal.lerp(bone_basis.xform(source_normal), bone_weight) - source_normal;
817
}
818
819
if (use_tangent_array) {
820
lerped_tangent += source_tangent_vec3.lerp(bone_basis.xform(source_tangent_vec3), bone_weight) - source_tangent_vec3;
821
}
822
}
823
824
lerped_vertices_ptrw[vertex_index] += lerped_vertex;
825
826
if (use_normal_array) {
827
lerped_normals_ptrw[vertex_index] = (source_normal + lerped_normal).normalized();
828
}
829
830
if (use_tangent_array) {
831
lerped_tangent = (source_tangent_vec3 + lerped_tangent).normalized();
832
lerped_tangents_ptrw[tangent_index] = lerped_tangent.x;
833
lerped_tangents_ptrw[tangent_index + 1] = lerped_tangent.y;
834
lerped_tangents_ptrw[tangent_index + 2] = lerped_tangent.z;
835
}
836
}
837
838
new_mesh_arrays[Mesh::ARRAY_VERTEX] = lerped_vertex_array;
839
if (use_normal_array) {
840
new_mesh_arrays[Mesh::ARRAY_NORMAL] = lerped_normal_array;
841
}
842
if (use_tangent_array) {
843
new_mesh_arrays[Mesh::ARRAY_TANGENT] = lerped_tangent_array;
844
}
845
846
bake_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, new_mesh_arrays, Array(), Dictionary(), surface_format);
847
}
848
849
return bake_mesh;
850
}
851
852
Ref<TriangleMesh> MeshInstance3D::generate_triangle_mesh() const {
853
if (mesh.is_valid()) {
854
return mesh->generate_triangle_mesh();
855
}
856
return Ref<TriangleMesh>();
857
}
858
859
#ifndef NAVIGATION_3D_DISABLED
860
void MeshInstance3D::navmesh_parse_init() {
861
ERR_FAIL_NULL(NavigationServer3D::get_singleton());
862
if (!_navmesh_source_geometry_parser.is_valid()) {
863
_navmesh_source_geometry_parsing_callback = callable_mp_static(&MeshInstance3D::navmesh_parse_source_geometry);
864
_navmesh_source_geometry_parser = NavigationServer3D::get_singleton()->source_geometry_parser_create();
865
NavigationServer3D::get_singleton()->source_geometry_parser_set_callback(_navmesh_source_geometry_parser, _navmesh_source_geometry_parsing_callback);
866
}
867
}
868
869
void MeshInstance3D::navmesh_parse_source_geometry(const Ref<NavigationMesh> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData3D> p_source_geometry_data, Node *p_node) {
870
MeshInstance3D *mesh_instance = Object::cast_to<MeshInstance3D>(p_node);
871
872
if (mesh_instance == nullptr) {
873
return;
874
}
875
876
NavigationMesh::ParsedGeometryType parsed_geometry_type = p_navigation_mesh->get_parsed_geometry_type();
877
878
if (parsed_geometry_type == NavigationMesh::PARSED_GEOMETRY_MESH_INSTANCES || parsed_geometry_type == NavigationMesh::PARSED_GEOMETRY_BOTH) {
879
Ref<Mesh> mesh = mesh_instance->get_mesh();
880
if (mesh.is_valid()) {
881
p_source_geometry_data->add_mesh(mesh, mesh_instance->get_global_transform());
882
}
883
}
884
}
885
#endif // NAVIGATION_3D_DISABLED
886
887
void MeshInstance3D::_bind_methods() {
888
ClassDB::bind_method(D_METHOD("set_mesh", "mesh"), &MeshInstance3D::set_mesh);
889
ClassDB::bind_method(D_METHOD("get_mesh"), &MeshInstance3D::get_mesh);
890
ClassDB::bind_method(D_METHOD("set_skeleton_path", "skeleton_path"), &MeshInstance3D::set_skeleton_path);
891
ClassDB::bind_method(D_METHOD("get_skeleton_path"), &MeshInstance3D::get_skeleton_path);
892
ClassDB::bind_method(D_METHOD("set_skin", "skin"), &MeshInstance3D::set_skin);
893
ClassDB::bind_method(D_METHOD("get_skin"), &MeshInstance3D::get_skin);
894
ClassDB::bind_method(D_METHOD("get_skin_reference"), &MeshInstance3D::get_skin_reference);
895
896
ClassDB::bind_method(D_METHOD("get_surface_override_material_count"), &MeshInstance3D::get_surface_override_material_count);
897
ClassDB::bind_method(D_METHOD("set_surface_override_material", "surface", "material"), &MeshInstance3D::set_surface_override_material);
898
ClassDB::bind_method(D_METHOD("get_surface_override_material", "surface"), &MeshInstance3D::get_surface_override_material);
899
ClassDB::bind_method(D_METHOD("get_active_material", "surface"), &MeshInstance3D::get_active_material);
900
901
#ifndef PHYSICS_3D_DISABLED
902
ClassDB::bind_method(D_METHOD("create_trimesh_collision"), &MeshInstance3D::create_trimesh_collision);
903
ClassDB::set_method_flags("MeshInstance3D", "create_trimesh_collision", METHOD_FLAGS_DEFAULT);
904
ClassDB::bind_method(D_METHOD("create_convex_collision", "clean", "simplify"), &MeshInstance3D::create_convex_collision, DEFVAL(true), DEFVAL(false));
905
ClassDB::set_method_flags("MeshInstance3D", "create_convex_collision", METHOD_FLAGS_DEFAULT);
906
ClassDB::bind_method(D_METHOD("create_multiple_convex_collisions", "settings"), &MeshInstance3D::create_multiple_convex_collisions, DEFVAL(Ref<MeshConvexDecompositionSettings>()));
907
ClassDB::set_method_flags("MeshInstance3D", "create_multiple_convex_collisions", METHOD_FLAGS_DEFAULT);
908
#endif // PHYSICS_3D_DISABLED
909
910
ClassDB::bind_method(D_METHOD("get_blend_shape_count"), &MeshInstance3D::get_blend_shape_count);
911
ClassDB::bind_method(D_METHOD("find_blend_shape_by_name", "name"), &MeshInstance3D::find_blend_shape_by_name);
912
ClassDB::bind_method(D_METHOD("get_blend_shape_value", "blend_shape_idx"), &MeshInstance3D::get_blend_shape_value);
913
ClassDB::bind_method(D_METHOD("set_blend_shape_value", "blend_shape_idx", "value"), &MeshInstance3D::set_blend_shape_value);
914
915
ClassDB::bind_method(D_METHOD("create_debug_tangents"), &MeshInstance3D::create_debug_tangents);
916
917
ClassDB::bind_method(D_METHOD("bake_mesh_from_current_blend_shape_mix", "existing"), &MeshInstance3D::bake_mesh_from_current_blend_shape_mix, DEFVAL(Ref<ArrayMesh>()));
918
ClassDB::bind_method(D_METHOD("bake_mesh_from_current_skeleton_pose", "existing"), &MeshInstance3D::bake_mesh_from_current_skeleton_pose, DEFVAL(Ref<ArrayMesh>()));
919
920
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh", PROPERTY_HINT_RESOURCE_TYPE, "Mesh"), "set_mesh", "get_mesh");
921
ADD_GROUP("Skeleton", "");
922
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "skin", PROPERTY_HINT_RESOURCE_TYPE, "Skin"), "set_skin", "get_skin");
923
ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "skeleton", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Skeleton3D"), "set_skeleton_path", "get_skeleton_path");
924
ADD_GROUP("", "");
925
}
926
927
MeshInstance3D::MeshInstance3D() {
928
}
929
930
MeshInstance3D::~MeshInstance3D() {
931
}
932
933