Debian Codegraph @

scene/3d/mesh_instance_3d.cpp

raw symbols SCIP
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_3d/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 #ifndef DISABLE_DEPRECATED
346 if (upgrading_skeleton_compat) {
347 if (skeleton_path.is_empty() && Object::cast_to<Skeleton3D>(get_parent())) {
348 skeleton_path = NodePath("..");
349 }
350 }
351 #endif
352 _resolve_skeleton_path();
353 } break;
354 case NOTIFICATION_TRANSLATION_CHANGED: {
355 if (mesh.is_valid()) {
356 mesh->notification(NOTIFICATION_TRANSLATION_CHANGED);
357 }
358 } break;
359 }
360 }
361
362 int MeshInstance3D::get_surface_override_material_count() const {
363 return surface_override_materials.size();
364 }
365
366 void MeshInstance3D::set_surface_override_material(int p_surface, const Ref<Material> &p_material) {
367 ERR_FAIL_INDEX(p_surface, surface_override_materials.size());
368
369 surface_override_materials.write[p_surface] = p_material;
370
371 if (surface_override_materials[p_surface].is_valid()) {
372 RS::get_singleton()->instance_set_surface_override_material(get_instance(), p_surface, surface_override_materials[p_surface]->get_rid());
373 } else {
374 RS::get_singleton()->instance_set_surface_override_material(get_instance(), p_surface, RID());
375 }
376 }
377
378 Ref<Material> MeshInstance3D::get_surface_override_material(int p_surface) const {
379 ERR_FAIL_INDEX_V(p_surface, surface_override_materials.size(), Ref<Material>());
380
381 return surface_override_materials[p_surface];
382 }
383
384 Ref<Material> MeshInstance3D::get_active_material(int p_surface) const {
385 Ref<Material> mat_override = get_material_override();
386 if (mat_override.is_valid()) {
387 return mat_override;
388 }
389
390 Ref<Mesh> m = get_mesh();
391 if (m.is_null() || m->get_surface_count() == 0) {
392 return Ref<Material>();
393 }
394
395 Ref<Material> surface_material = get_surface_override_material(p_surface);
396 if (surface_material.is_valid()) {
397 return surface_material;
398 }
399
400 return m->surface_get_material(p_surface);
401 }
402
403 void MeshInstance3D::_mesh_changed() {
404 ERR_FAIL_COND(mesh.is_null());
405 const int surface_count = mesh->get_surface_count();
406
407 surface_override_materials.resize(surface_count);
408
409 uint32_t initialize_bs_from = blend_shape_tracks.size();
410 blend_shape_tracks.resize(mesh->get_blend_shape_count());
411
412 if (surface_count > 0) {
413 for (uint32_t i = 0; i < blend_shape_tracks.size(); i++) {
414 blend_shape_properties["blend_shapes/" + String(mesh->get_blend_shape_name(i))] = i;
415 if (i < initialize_bs_from) {
416 set_blend_shape_value(i, blend_shape_tracks[i]);
417 } else {
418 set_blend_shape_value(i, 0);
419 }
420 }
421 }
422
423 for (int surface_index = 0; surface_index < surface_count; ++surface_index) {
424 if (surface_override_materials[surface_index].is_valid()) {
425 RS::get_singleton()->instance_set_surface_override_material(get_instance(), surface_index, surface_override_materials[surface_index]->get_rid());
426 }
427 }
428
429 update_gizmos();
430 }
431
432 MeshInstance3D *MeshInstance3D::create_debug_tangents_node() {
433 Vector<Vector3> lines;
434 Vector<Color> colors;
435
436 Ref<Mesh> m = get_mesh();
437 if (m.is_null()) {
438 return nullptr;
439 }
440
441 for (int i = 0; i < m->get_surface_count(); i++) {
442 Array arrays = m->surface_get_arrays(i);
443 ERR_CONTINUE(arrays.size() != Mesh::ARRAY_MAX);
444
445 Vector<Vector3> verts = arrays[Mesh::ARRAY_VERTEX];
446 Vector<Vector3> norms = arrays[Mesh::ARRAY_NORMAL];
447 if (norms.is_empty()) {
448 continue;
449 }
450 Vector<float> tangents = arrays[Mesh::ARRAY_TANGENT];
451 if (tangents.is_empty()) {
452 continue;
453 }
454
455 for (int j = 0; j < verts.size(); j++) {
456 Vector3 v = verts[j];
457 Vector3 n = norms[j];
458 Vector3 t = Vector3(tangents[j * 4 + 0], tangents[j * 4 + 1], tangents[j * 4 + 2]);
459 Vector3 b = (n.cross(t)).normalized() * tangents[j * 4 + 3];
460
461 lines.push_back(v); //normal
462 colors.push_back(Color(0, 0, 1)); //color
463 lines.push_back(v + n * 0.04); //normal
464 colors.push_back(Color(0, 0, 1)); //color
465
466 lines.push_back(v); //tangent
467 colors.push_back(Color(1, 0, 0)); //color
468 lines.push_back(v + t * 0.04); //tangent
469 colors.push_back(Color(1, 0, 0)); //color
470
471 lines.push_back(v); //binormal
472 colors.push_back(Color(0, 1, 0)); //color
473 lines.push_back(v + b * 0.04); //binormal
474 colors.push_back(Color(0, 1, 0)); //color
475 }
476 }
477
478 if (lines.size()) {
479 Ref<StandardMaterial3D> sm;
480 sm.instantiate();
481
482 sm->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
483 sm->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
484 sm->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
485 sm->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
486
487 Ref<ArrayMesh> am;
488 am.instantiate();
489 Array a;
490 a.resize(Mesh::ARRAY_MAX);
491 a[Mesh::ARRAY_VERTEX] = lines;
492 a[Mesh::ARRAY_COLOR] = colors;
493
494 am->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, a);
495 am->surface_set_material(0, sm);
496
497 MeshInstance3D *mi = memnew(MeshInstance3D);
498 mi->set_mesh(am);
499 mi->set_name("DebugTangents");
500 return mi;
501 }
502
503 return nullptr;
504 }
505
506 void MeshInstance3D::create_debug_tangents() {
507 MeshInstance3D *mi = create_debug_tangents_node();
508 if (!mi) {
509 return;
510 }
511
512 add_child(mi, true);
513 if (is_inside_tree() && this == get_tree()->get_edited_scene_root()) {
514 mi->set_owner(this);
515 } else {
516 mi->set_owner(get_owner());
517 }
518 }
519
520 bool MeshInstance3D::_property_can_revert(const StringName &p_name) const {
521 HashMap<StringName, int>::ConstIterator E = blend_shape_properties.find(p_name);
522 if (E) {
523 return true;
524 }
525 return false;
526 }
527
528 bool MeshInstance3D::_property_get_revert(const StringName &p_name, Variant &r_property) const {
529 HashMap<StringName, int>::ConstIterator E = blend_shape_properties.find(p_name);
530 if (E) {
531 r_property = 0.0f;
532 return true;
533 }
534 return false;
535 }
536
537 Ref<ArrayMesh> MeshInstance3D::bake_mesh_from_current_blend_shape_mix(Ref<ArrayMesh> p_existing) {
538 Ref<ArrayMesh> source_mesh = get_mesh();
539 ERR_FAIL_COND_V_MSG(source_mesh.is_null(), Ref<ArrayMesh>(), "The source mesh must be a valid ArrayMesh.");
540
541 Ref<ArrayMesh> bake_mesh;
542
543 if (p_existing.is_valid()) {
544 ERR_FAIL_COND_V_MSG(p_existing.is_null(), Ref<ArrayMesh>(), "The existing mesh must be a valid ArrayMesh.");
545 ERR_FAIL_COND_V_MSG(source_mesh == p_existing, Ref<ArrayMesh>(), "The source mesh can not be the same mesh as the existing mesh.");
546
547 bake_mesh = p_existing;
548 } else {
549 bake_mesh.instantiate();
550 }
551
552 Mesh::BlendShapeMode blend_shape_mode = source_mesh->get_blend_shape_mode();
553 int mesh_surface_count = source_mesh->get_surface_count();
554
555 bake_mesh->clear_surfaces();
556 bake_mesh->set_blend_shape_mode(blend_shape_mode);
557
558 for (int surface_index = 0; surface_index < mesh_surface_count; surface_index++) {
559 uint32_t surface_format = source_mesh->surface_get_format(surface_index);
560
561 ERR_CONTINUE(0 == (surface_format & Mesh::ARRAY_FORMAT_VERTEX));
562
563 const Array &source_mesh_arrays = source_mesh->surface_get_arrays(surface_index);
564
565 ERR_FAIL_COND_V(source_mesh_arrays.size() != RS::ARRAY_MAX, Ref<ArrayMesh>());
566
567 const Vector<Vector3> &source_mesh_vertex_array = source_mesh_arrays[Mesh::ARRAY_VERTEX];
568 const Vector<Vector3> &source_mesh_normal_array = source_mesh_arrays[Mesh::ARRAY_NORMAL];
569 const Vector<float> &source_mesh_tangent_array = source_mesh_arrays[Mesh::ARRAY_TANGENT];
570
571 Array new_mesh_arrays;
572 new_mesh_arrays.resize(Mesh::ARRAY_MAX);
573 for (int i = 0; i < source_mesh_arrays.size(); i++) {
574 if (i == Mesh::ARRAY_VERTEX || i == Mesh::ARRAY_NORMAL || i == Mesh::ARRAY_TANGENT) {
575 continue;
576 }
577 new_mesh_arrays[i] = source_mesh_arrays[i];
578 }
579
580 bool use_normal_array = source_mesh_normal_array.size() == source_mesh_vertex_array.size();
581 bool use_tangent_array = source_mesh_tangent_array.size() / 4 == source_mesh_vertex_array.size();
582
583 Vector<Vector3> lerped_vertex_array = source_mesh_vertex_array;
584 Vector<Vector3> lerped_normal_array = source_mesh_normal_array;
585 Vector<float> lerped_tangent_array = source_mesh_tangent_array;
586
587 const Vector3 *source_vertices_ptr = source_mesh_vertex_array.ptr();
588 const Vector3 *source_normals_ptr = source_mesh_normal_array.ptr();
589 const float *source_tangents_ptr = source_mesh_tangent_array.ptr();
590
591 Vector3 *lerped_vertices_ptrw = lerped_vertex_array.ptrw();
592 Vector3 *lerped_normals_ptrw = lerped_normal_array.ptrw();
593 float *lerped_tangents_ptrw = lerped_tangent_array.ptrw();
594
595 const Array &blendshapes_mesh_arrays = source_mesh->surface_get_blend_shape_arrays(surface_index);
596 int blend_shape_count = source_mesh->get_blend_shape_count();
597 ERR_FAIL_COND_V(blendshapes_mesh_arrays.size() != blend_shape_count, Ref<ArrayMesh>());
598
599 for (int blendshape_index = 0; blendshape_index < blend_shape_count; blendshape_index++) {
600 float blend_weight = get_blend_shape_value(blendshape_index);
601 if (std::abs(blend_weight) <= 0.0001) {
602 continue;
603 }
604
605 const Array &blendshape_mesh_arrays = blendshapes_mesh_arrays[blendshape_index];
606
607 const Vector<Vector3> &blendshape_vertex_array = blendshape_mesh_arrays[Mesh::ARRAY_VERTEX];
608 const Vector<Vector3> &blendshape_normal_array = blendshape_mesh_arrays[Mesh::ARRAY_NORMAL];
609 const Vector<float> &blendshape_tangent_array = blendshape_mesh_arrays[Mesh::ARRAY_TANGENT];
610
611 ERR_FAIL_COND_V(source_mesh_vertex_array.size() != blendshape_vertex_array.size(), Ref<ArrayMesh>());
612 ERR_FAIL_COND_V(source_mesh_normal_array.size() != blendshape_normal_array.size(), Ref<ArrayMesh>());
613 ERR_FAIL_COND_V(source_mesh_tangent_array.size() != blendshape_tangent_array.size(), Ref<ArrayMesh>());
614
615 const Vector3 *blendshape_vertices_ptr = blendshape_vertex_array.ptr();
616 const Vector3 *blendshape_normals_ptr = blendshape_normal_array.ptr();
617 const float *blendshape_tangents_ptr = blendshape_tangent_array.ptr();
618
619 if (blend_shape_mode == Mesh::BLEND_SHAPE_MODE_NORMALIZED) {
620 for (int i = 0; i < source_mesh_vertex_array.size(); i++) {
621 const Vector3 &source_vertex = source_vertices_ptr[i];
622 const Vector3 &blendshape_vertex = blendshape_vertices_ptr[i];
623 Vector3 lerped_vertex = source_vertex.lerp(blendshape_vertex, blend_weight) - source_vertex;
624 lerped_vertices_ptrw[i] += lerped_vertex;
625
626 if (use_normal_array) {
627 const Vector3 &source_normal = source_normals_ptr[i];
628 const Vector3 &blendshape_normal = blendshape_normals_ptr[i];
629 Vector3 lerped_normal = source_normal.lerp(blendshape_normal, blend_weight) - source_normal;
630 lerped_normals_ptrw[i] += lerped_normal;
631 }
632
633 if (use_tangent_array) {
634 int tangent_index = i * 4;
635 const Vector4 source_tangent = Vector4(
636 source_tangents_ptr[tangent_index],
637 source_tangents_ptr[tangent_index + 1],
638 source_tangents_ptr[tangent_index + 2],
639 source_tangents_ptr[tangent_index + 3]);
640 const Vector4 blendshape_tangent = Vector4(
641 blendshape_tangents_ptr[tangent_index],
642 blendshape_tangents_ptr[tangent_index + 1],
643 blendshape_tangents_ptr[tangent_index + 2],
644 blendshape_tangents_ptr[tangent_index + 3]);
645 Vector4 lerped_tangent = source_tangent.lerp(blendshape_tangent, blend_weight);
646 lerped_tangents_ptrw[tangent_index] += lerped_tangent.x;
647 lerped_tangents_ptrw[tangent_index + 1] += lerped_tangent.y;
648 lerped_tangents_ptrw[tangent_index + 2] += lerped_tangent.z;
649 lerped_tangents_ptrw[tangent_index + 3] += lerped_tangent.w;
650 }
651 }
652 } else if (blend_shape_mode == Mesh::BLEND_SHAPE_MODE_RELATIVE) {
653 for (int i = 0; i < source_mesh_vertex_array.size(); i++) {
654 const Vector3 &blendshape_vertex = blendshape_vertices_ptr[i];
655 lerped_vertices_ptrw[i] += blendshape_vertex * blend_weight;
656
657 if (use_normal_array) {
658 const Vector3 &blendshape_normal = blendshape_normals_ptr[i];
659 lerped_normals_ptrw[i] += blendshape_normal * blend_weight;
660 }
661
662 if (use_tangent_array) {
663 int tangent_index = i * 4;
664 const Vector4 blendshape_tangent = Vector4(
665 blendshape_tangents_ptr[tangent_index],
666 blendshape_tangents_ptr[tangent_index + 1],
667 blendshape_tangents_ptr[tangent_index + 2],
668 blendshape_tangents_ptr[tangent_index + 3]);
669 Vector4 lerped_tangent = blendshape_tangent * blend_weight;
670 lerped_tangents_ptrw[tangent_index] += lerped_tangent.x;
671 lerped_tangents_ptrw[tangent_index + 1] += lerped_tangent.y;
672 lerped_tangents_ptrw[tangent_index + 2] += lerped_tangent.z;
673 lerped_tangents_ptrw[tangent_index + 3] += lerped_tangent.w;
674 }
675 }
676 }
677 }
678
679 new_mesh_arrays[Mesh::ARRAY_VERTEX] = lerped_vertex_array;
680 if (use_normal_array) {
681 new_mesh_arrays[Mesh::ARRAY_NORMAL] = lerped_normal_array;
682 }
683 if (use_tangent_array) {
684 new_mesh_arrays[Mesh::ARRAY_TANGENT] = lerped_tangent_array;
685 }
686
687 bake_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, new_mesh_arrays, Array(), Dictionary(), surface_format);
688 }
689
690 return bake_mesh;
691 }
692
693 Ref<ArrayMesh> MeshInstance3D::bake_mesh_from_current_skeleton_pose(Ref<ArrayMesh> p_existing) {
694 Ref<ArrayMesh> source_mesh = get_mesh();
695 ERR_FAIL_COND_V_MSG(source_mesh.is_null(), Ref<ArrayMesh>(), "The source mesh must be a valid ArrayMesh.");
696
697 Ref<ArrayMesh> bake_mesh;
698
699 if (p_existing.is_valid()) {
700 ERR_FAIL_COND_V_MSG(source_mesh == p_existing, Ref<ArrayMesh>(), "The source mesh can not be the same mesh as the existing mesh.");
701
702 bake_mesh = p_existing;
703 } else {
704 bake_mesh.instantiate();
705 }
706
707 ERR_FAIL_COND_V_MSG(skin_ref.is_null(), Ref<ArrayMesh>(), "The source mesh must have a valid skin.");
708 ERR_FAIL_COND_V_MSG(skin_internal.is_null(), Ref<ArrayMesh>(), "The source mesh must have a valid skin.");
709 RID skeleton = skin_ref->get_skeleton();
710 ERR_FAIL_COND_V_MSG(!skeleton.is_valid(), Ref<ArrayMesh>(), "The source mesh must have its skin registered with a valid skeleton.");
711
712 const int bone_count = RenderingServer::get_singleton()->skeleton_get_bone_count(skeleton);
713 ERR_FAIL_COND_V(bone_count <= 0, Ref<ArrayMesh>());
714 ERR_FAIL_COND_V(bone_count < skin_internal->get_bind_count(), Ref<ArrayMesh>());
715
716 LocalVector<Transform3D> bone_transforms;
717 bone_transforms.resize(bone_count);
718 for (int bone_index = 0; bone_index < bone_count; bone_index++) {
719 bone_transforms[bone_index] = RenderingServer::get_singleton()->skeleton_bone_get_transform(skeleton, bone_index);
720 }
721
722 bake_mesh->clear_surfaces();
723
724 int mesh_surface_count = source_mesh->get_surface_count();
725
726 for (int surface_index = 0; surface_index < mesh_surface_count; surface_index++) {
727 ERR_CONTINUE(source_mesh->surface_get_primitive_type(surface_index) != Mesh::PRIMITIVE_TRIANGLES);
728
729 uint32_t surface_format = source_mesh->surface_get_format(surface_index);
730
731 ERR_CONTINUE(0 == (surface_format & Mesh::ARRAY_FORMAT_VERTEX));
732 ERR_CONTINUE(0 == (surface_format & Mesh::ARRAY_FORMAT_BONES));
733 ERR_CONTINUE(0 == (surface_format & Mesh::ARRAY_FORMAT_WEIGHTS));
734
735 unsigned int bones_per_vertex = surface_format & Mesh::ARRAY_FLAG_USE_8_BONE_WEIGHTS ? 8 : 4;
736
737 surface_format &= ~Mesh::ARRAY_FORMAT_BONES;
738 surface_format &= ~Mesh::ARRAY_FORMAT_WEIGHTS;
739
740 const Array &source_mesh_arrays = source_mesh->surface_get_arrays(surface_index);
741
742 ERR_FAIL_COND_V(source_mesh_arrays.size() != RS::ARRAY_MAX, Ref<ArrayMesh>());
743
744 const Vector<Vector3> &source_mesh_vertex_array = source_mesh_arrays[Mesh::ARRAY_VERTEX];
745 const Vector<Vector3> &source_mesh_normal_array = source_mesh_arrays[Mesh::ARRAY_NORMAL];
746 const Vector<float> &source_mesh_tangent_array = source_mesh_arrays[Mesh::ARRAY_TANGENT];
747 const Vector<int> &source_mesh_bones_array = source_mesh_arrays[Mesh::ARRAY_BONES];
748 const Vector<float> &source_mesh_weights_array = source_mesh_arrays[Mesh::ARRAY_WEIGHTS];
749
750 unsigned int vertex_count = source_mesh_vertex_array.size();
751 int expected_bone_array_size = vertex_count * bones_per_vertex;
752 ERR_CONTINUE(source_mesh_bones_array.size() != expected_bone_array_size);
753 ERR_CONTINUE(source_mesh_weights_array.size() != expected_bone_array_size);
754
755 Array new_mesh_arrays;
756 new_mesh_arrays.resize(Mesh::ARRAY_MAX);
757 for (int i = 0; i < source_mesh_arrays.size(); i++) {
758 if (i == Mesh::ARRAY_VERTEX || i == Mesh::ARRAY_NORMAL || i == Mesh::ARRAY_TANGENT || i == Mesh::ARRAY_BONES || i == Mesh::ARRAY_WEIGHTS) {
759 continue;
760 }
761 new_mesh_arrays[i] = source_mesh_arrays[i];
762 }
763
764 bool use_normal_array = source_mesh_normal_array.size() == source_mesh_vertex_array.size();
765 bool use_tangent_array = source_mesh_tangent_array.size() / 4 == source_mesh_vertex_array.size();
766
767 Vector<Vector3> lerped_vertex_array = source_mesh_vertex_array;
768 Vector<Vector3> lerped_normal_array = source_mesh_normal_array;
769 Vector<float> lerped_tangent_array = source_mesh_tangent_array;
770
771 const Vector3 *source_vertices_ptr = source_mesh_vertex_array.ptr();
772 const Vector3 *source_normals_ptr = source_mesh_normal_array.ptr();
773 const float *source_tangents_ptr = source_mesh_tangent_array.ptr();
774 const int *source_bones_ptr = source_mesh_bones_array.ptr();
775 const float *source_weights_ptr = source_mesh_weights_array.ptr();
776
777 Vector3 *lerped_vertices_ptrw = lerped_vertex_array.ptrw();
778 Vector3 *lerped_normals_ptrw = lerped_normal_array.ptrw();
779 float *lerped_tangents_ptrw = lerped_tangent_array.ptrw();
780
781 for (unsigned int vertex_index = 0; vertex_index < vertex_count; vertex_index++) {
782 Vector3 lerped_vertex;
783 Vector3 lerped_normal;
784 Vector3 lerped_tangent;
785
786 const Vector3 &source_vertex = source_vertices_ptr[vertex_index];
787
788 Vector3 source_normal;
789 if (use_normal_array) {
790 source_normal = source_normals_ptr[vertex_index];
791 }
792
793 int tangent_index = vertex_index * 4;
794 Vector4 source_tangent;
795 Vector3 source_tangent_vec3;
796 if (use_tangent_array) {
797 source_tangent = Vector4(
798 source_tangents_ptr[tangent_index],
799 source_tangents_ptr[tangent_index + 1],
800 source_tangents_ptr[tangent_index + 2],
801 source_tangents_ptr[tangent_index + 3]);
802
803 DEV_ASSERT(source_tangent.w == 1.0 || source_tangent.w == -1.0);
804
805 source_tangent_vec3 = Vector3(source_tangent.x, source_tangent.y, source_tangent.z);
806 }
807
808 for (unsigned int weight_index = 0; weight_index < bones_per_vertex; weight_index++) {
809 float bone_weight = source_weights_ptr[vertex_index * bones_per_vertex + weight_index];
810 if (bone_weight < FLT_EPSILON) {
811 continue;
812 }
813 int vertex_bone_index = source_bones_ptr[vertex_index * bones_per_vertex + weight_index];
814 const Transform3D &bone_transform = bone_transforms[vertex_bone_index];
815 const Basis bone_basis = bone_transform.basis.orthonormalized();
816
817 ERR_FAIL_INDEX_V(vertex_bone_index, static_cast<int>(bone_transforms.size()), Ref<ArrayMesh>());
818
819 lerped_vertex += source_vertex.lerp(bone_transform.xform(source_vertex), bone_weight) - source_vertex;
820 ;
821
822 if (use_normal_array) {
823 lerped_normal += source_normal.lerp(bone_basis.xform(source_normal), bone_weight) - source_normal;
824 }
825
826 if (use_tangent_array) {
827 lerped_tangent += source_tangent_vec3.lerp(bone_basis.xform(source_tangent_vec3), bone_weight) - source_tangent_vec3;
828 }
829 }
830
831 lerped_vertices_ptrw[vertex_index] += lerped_vertex;
832
833 if (use_normal_array) {
834 lerped_normals_ptrw[vertex_index] = (source_normal + lerped_normal).normalized();
835 }
836
837 if (use_tangent_array) {
838 lerped_tangent = (source_tangent_vec3 + lerped_tangent).normalized();
839 lerped_tangents_ptrw[tangent_index] = lerped_tangent.x;
840 lerped_tangents_ptrw[tangent_index + 1] = lerped_tangent.y;
841 lerped_tangents_ptrw[tangent_index + 2] = lerped_tangent.z;
842 }
843 }
844
845 new_mesh_arrays[Mesh::ARRAY_VERTEX] = lerped_vertex_array;
846 if (use_normal_array) {
847 new_mesh_arrays[Mesh::ARRAY_NORMAL] = lerped_normal_array;
848 }
849 if (use_tangent_array) {
850 new_mesh_arrays[Mesh::ARRAY_TANGENT] = lerped_tangent_array;
851 }
852
853 bake_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, new_mesh_arrays, Array(), Dictionary(), surface_format);
854 }
855
856 return bake_mesh;
857 }
858
859 Ref<TriangleMesh> MeshInstance3D::generate_triangle_mesh() const {
860 if (mesh.is_valid()) {
861 return mesh->generate_triangle_mesh();
862 }
863 return Ref<TriangleMesh>();
864 }
865
866 #ifndef NAVIGATION_3D_DISABLED
867 void MeshInstance3D::navmesh_parse_init() {
868 ERR_FAIL_NULL(NavigationServer3D::get_singleton());
869 if (!_navmesh_source_geometry_parser.is_valid()) {
870 _navmesh_source_geometry_parsing_callback = callable_mp_static(&MeshInstance3D::navmesh_parse_source_geometry);
871 _navmesh_source_geometry_parser = NavigationServer3D::get_singleton()->source_geometry_parser_create();
872 NavigationServer3D::get_singleton()->source_geometry_parser_set_callback(_navmesh_source_geometry_parser, _navmesh_source_geometry_parsing_callback);
873 }
874 }
875
876 void MeshInstance3D::navmesh_parse_source_geometry(const Ref<NavigationMesh> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData3D> p_source_geometry_data, Node *p_node) {
877 MeshInstance3D *mesh_instance = Object::cast_to<MeshInstance3D>(p_node);
878
879 if (mesh_instance == nullptr) {
880 return;
881 }
882
883 NavigationMesh::ParsedGeometryType parsed_geometry_type = p_navigation_mesh->get_parsed_geometry_type();
884
885 if (parsed_geometry_type == NavigationMesh::PARSED_GEOMETRY_MESH_INSTANCES || parsed_geometry_type == NavigationMesh::PARSED_GEOMETRY_BOTH) {
886 Ref<Mesh> mesh = mesh_instance->get_mesh();
887 if (mesh.is_valid()) {
888 p_source_geometry_data->add_mesh(mesh, mesh_instance->get_global_transform());
889 }
890 }
891 }
892 #endif // NAVIGATION_3D_DISABLED
893
894 void MeshInstance3D::_bind_methods() {
895 ClassDB::bind_method(D_METHOD("set_mesh", "mesh"), &MeshInstance3D::set_mesh);
896 ClassDB::bind_method(D_METHOD("get_mesh"), &MeshInstance3D::get_mesh);
897 ClassDB::bind_method(D_METHOD("set_skeleton_path", "skeleton_path"), &MeshInstance3D::set_skeleton_path);
898 ClassDB::bind_method(D_METHOD("get_skeleton_path"), &MeshInstance3D::get_skeleton_path);
899 ClassDB::bind_method(D_METHOD("set_skin", "skin"), &MeshInstance3D::set_skin);
900 ClassDB::bind_method(D_METHOD("get_skin"), &MeshInstance3D::get_skin);
901 ClassDB::bind_method(D_METHOD("get_skin_reference"), &MeshInstance3D::get_skin_reference);
902
903 ClassDB::bind_method(D_METHOD("get_surface_override_material_count"), &MeshInstance3D::get_surface_override_material_count);
904 ClassDB::bind_method(D_METHOD("set_surface_override_material", "surface", "material"), &MeshInstance3D::set_surface_override_material);
905 ClassDB::bind_method(D_METHOD("get_surface_override_material", "surface"), &MeshInstance3D::get_surface_override_material);
906 ClassDB::bind_method(D_METHOD("get_active_material", "surface"), &MeshInstance3D::get_active_material);
907
908 #ifndef PHYSICS_3D_DISABLED
909 ClassDB::bind_method(D_METHOD("create_trimesh_collision"), &MeshInstance3D::create_trimesh_collision);
910 ClassDB::set_method_flags("MeshInstance3D", "create_trimesh_collision", METHOD_FLAGS_DEFAULT);
911 ClassDB::bind_method(D_METHOD("create_convex_collision", "clean", "simplify"), &MeshInstance3D::create_convex_collision, DEFVAL(true), DEFVAL(false));
912 ClassDB::set_method_flags("MeshInstance3D", "create_convex_collision", METHOD_FLAGS_DEFAULT);
913 ClassDB::bind_method(D_METHOD("create_multiple_convex_collisions", "settings"), &MeshInstance3D::create_multiple_convex_collisions, DEFVAL(Ref<MeshConvexDecompositionSettings>()));
914 ClassDB::set_method_flags("MeshInstance3D", "create_multiple_convex_collisions", METHOD_FLAGS_DEFAULT);
915 #endif // PHYSICS_3D_DISABLED
916
917 ClassDB::bind_method(D_METHOD("get_blend_shape_count"), &MeshInstance3D::get_blend_shape_count);
918 ClassDB::bind_method(D_METHOD("find_blend_shape_by_name", "name"), &MeshInstance3D::find_blend_shape_by_name);
919 ClassDB::bind_method(D_METHOD("get_blend_shape_value", "blend_shape_idx"), &MeshInstance3D::get_blend_shape_value);
920 ClassDB::bind_method(D_METHOD("set_blend_shape_value", "blend_shape_idx", "value"), &MeshInstance3D::set_blend_shape_value);
921
922 ClassDB::bind_method(D_METHOD("create_debug_tangents"), &MeshInstance3D::create_debug_tangents);
923
924 ClassDB::bind_method(D_METHOD("bake_mesh_from_current_blend_shape_mix", "existing"), &MeshInstance3D::bake_mesh_from_current_blend_shape_mix, DEFVAL(Ref<ArrayMesh>()));
925 ClassDB::bind_method(D_METHOD("bake_mesh_from_current_skeleton_pose", "existing"), &MeshInstance3D::bake_mesh_from_current_skeleton_pose, DEFVAL(Ref<ArrayMesh>()));
926
927 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh", PROPERTY_HINT_RESOURCE_TYPE, "Mesh"), "set_mesh", "get_mesh");
928 ADD_GROUP("Skeleton", "");
929 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "skin", PROPERTY_HINT_RESOURCE_TYPE, "Skin"), "set_skin", "get_skin");
930 ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "skeleton", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Skeleton3D"), "set_skeleton_path", "get_skeleton_path");
931 ADD_GROUP("", "");
932 }
933
934 MeshInstance3D::MeshInstance3D() {
935 _define_ancestry(AncestralClass::MESH_INSTANCE_3D);
936 #ifndef DISABLE_DEPRECATED
937 if (use_parent_skeleton_compat) {
938 skeleton_path = NodePath("..");
939 }
940 #endif
941 }
942
943 MeshInstance3D::~MeshInstance3D() {
944 }