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 "collision_shape_3d.h"
34#include "physics_body_3d.h"
35#include "scene/resources/concave_polygon_shape_3d.h"
36#include "scene/resources/convex_polygon_shape_3d.h"
37
38bool MeshInstance3D::_set(const StringName &p_name, const Variant &p_value) {
39 //this is not _too_ bad performance wise, really. it only arrives here if the property was not set anywhere else.
40 //add to it that it's probably found on first call to _set anyway.
41
42 if (!get_instance().is_valid()) {
43 return false;
44 }
45
46 HashMap<StringName, int>::Iterator E = blend_shape_properties.find(p_name);
47 if (E) {
48 set_blend_shape_value(E->value, p_value);
49 return true;
50 }
51
52 if (p_name.operator String().begins_with("surface_material_override/")) {
53 int idx = p_name.operator String().get_slicec('/', 1).to_int();
54
55 if (idx >= surface_override_materials.size() || idx < 0) {
56 return false;
57 }
58
59 set_surface_override_material(idx, p_value);
60 return true;
61 }
62
63 return false;
64}
65
66bool MeshInstance3D::_get(const StringName &p_name, Variant &r_ret) const {
67 if (!get_instance().is_valid()) {
68 return false;
69 }
70
71 HashMap<StringName, int>::ConstIterator E = blend_shape_properties.find(p_name);
72 if (E) {
73 r_ret = get_blend_shape_value(E->value);
74 return true;
75 }
76
77 if (p_name.operator String().begins_with("surface_material_override/")) {
78 int idx = p_name.operator String().get_slicec('/', 1).to_int();
79 if (idx >= surface_override_materials.size() || idx < 0) {
80 return false;
81 }
82 r_ret = surface_override_materials[idx];
83 return true;
84 }
85 return false;
86}
87
88void MeshInstance3D::_get_property_list(List<PropertyInfo> *p_list) const {
89 List<String> ls;
90 for (const KeyValue<StringName, int> &E : blend_shape_properties) {
91 ls.push_back(E.key);
92 }
93
94 ls.sort();
95
96 for (const String &E : ls) {
97 p_list->push_back(PropertyInfo(Variant::FLOAT, E, PROPERTY_HINT_RANGE, "-1,1,0.00001"));
98 }
99
100 if (mesh.is_valid()) {
101 for (int i = 0; i < mesh->get_surface_count(); i++) {
102 p_list->push_back(PropertyInfo(Variant::OBJECT, vformat("%s/%d", PNAME("surface_material_override"), i), PROPERTY_HINT_RESOURCE_TYPE, "BaseMaterial3D,ShaderMaterial", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_DEFERRED_SET_RESOURCE));
103 }
104 }
105}
106
107void MeshInstance3D::set_mesh(const Ref<Mesh> &p_mesh) {
108 if (mesh == p_mesh) {
109 return;
110 }
111
112 if (mesh.is_valid()) {
113 mesh->disconnect_changed(callable_mp(this, &MeshInstance3D::_mesh_changed));
114 }
115
116 mesh = p_mesh;
117
118 if (mesh.is_valid()) {
119 // If mesh is a PrimitiveMesh, calling get_rid on it can trigger a changed callback
120 // so do this before connecting _mesh_changed.
121 set_base(mesh->get_rid());
122 mesh->connect_changed(callable_mp(this, &MeshInstance3D::_mesh_changed));
123 _mesh_changed();
124 } else {
125 blend_shape_tracks.clear();
126 blend_shape_properties.clear();
127 set_base(RID());
128 update_gizmos();
129 }
130
131 notify_property_list_changed();
132}
133
134Ref<Mesh> MeshInstance3D::get_mesh() const {
135 return mesh;
136}
137
138int MeshInstance3D::get_blend_shape_count() const {
139 if (mesh.is_null()) {
140 return 0;
141 }
142 return mesh->get_blend_shape_count();
143}
144int MeshInstance3D::find_blend_shape_by_name(const StringName &p_name) {
145 if (mesh.is_null()) {
146 return -1;
147 }
148 for (int i = 0; i < mesh->get_blend_shape_count(); i++) {
149 if (mesh->get_blend_shape_name(i) == p_name) {
150 return i;
151 }
152 }
153 return -1;
154}
155float MeshInstance3D::get_blend_shape_value(int p_blend_shape) const {
156 ERR_FAIL_COND_V(mesh.is_null(), 0.0);
157 ERR_FAIL_INDEX_V(p_blend_shape, (int)blend_shape_tracks.size(), 0);
158 return blend_shape_tracks[p_blend_shape];
159}
160void MeshInstance3D::set_blend_shape_value(int p_blend_shape, float p_value) {
161 ERR_FAIL_COND(mesh.is_null());
162 ERR_FAIL_INDEX(p_blend_shape, (int)blend_shape_tracks.size());
163 blend_shape_tracks[p_blend_shape] = p_value;
164 RenderingServer::get_singleton()->instance_set_blend_shape_weight(get_instance(), p_blend_shape, p_value);
165}
166
167void MeshInstance3D::_resolve_skeleton_path() {
168 Ref<SkinReference> new_skin_reference;
169
170 if (!skeleton_path.is_empty()) {
171 Skeleton3D *skeleton = Object::cast_to<Skeleton3D>(get_node(skeleton_path));
172 if (skeleton) {
173 if (skin_internal.is_null()) {
174 new_skin_reference = skeleton->register_skin(skeleton->create_skin_from_rest_transforms());
175 //a skin was created for us
176 skin_internal = new_skin_reference->get_skin();
177 notify_property_list_changed();
178 } else {
179 new_skin_reference = skeleton->register_skin(skin_internal);
180 }
181 }
182 }
183
184 skin_ref = new_skin_reference;
185
186 if (skin_ref.is_valid()) {
187 RenderingServer::get_singleton()->instance_attach_skeleton(get_instance(), skin_ref->get_skeleton());
188 } else {
189 RenderingServer::get_singleton()->instance_attach_skeleton(get_instance(), RID());
190 }
191}
192
193void MeshInstance3D::set_skin(const Ref<Skin> &p_skin) {
194 skin_internal = p_skin;
195 skin = p_skin;
196 if (!is_inside_tree()) {
197 return;
198 }
199 _resolve_skeleton_path();
200}
201
202Ref<Skin> MeshInstance3D::get_skin() const {
203 return skin;
204}
205
206void MeshInstance3D::set_skeleton_path(const NodePath &p_skeleton) {
207 skeleton_path = p_skeleton;
208 if (!is_inside_tree()) {
209 return;
210 }
211 _resolve_skeleton_path();
212}
213
214NodePath MeshInstance3D::get_skeleton_path() {
215 return skeleton_path;
216}
217
218AABB MeshInstance3D::get_aabb() const {
219 if (!mesh.is_null()) {
220 return mesh->get_aabb();
221 }
222
223 return AABB();
224}
225
226Node *MeshInstance3D::create_trimesh_collision_node() {
227 if (mesh.is_null()) {
228 return nullptr;
229 }
230
231 Ref<ConcavePolygonShape3D> shape = mesh->create_trimesh_shape();
232 if (shape.is_null()) {
233 return nullptr;
234 }
235
236 StaticBody3D *static_body = memnew(StaticBody3D);
237 CollisionShape3D *cshape = memnew(CollisionShape3D);
238 cshape->set_shape(shape);
239 static_body->add_child(cshape, true);
240 return static_body;
241}
242
243void MeshInstance3D::create_trimesh_collision() {
244 StaticBody3D *static_body = Object::cast_to<StaticBody3D>(create_trimesh_collision_node());
245 ERR_FAIL_NULL(static_body);
246 static_body->set_name(String(get_name()) + "_col");
247
248 add_child(static_body, true);
249 if (get_owner()) {
250 CollisionShape3D *cshape = Object::cast_to<CollisionShape3D>(static_body->get_child(0));
251 static_body->set_owner(get_owner());
252 cshape->set_owner(get_owner());
253 }
254}
255
256Node *MeshInstance3D::create_convex_collision_node(bool p_clean, bool p_simplify) {
257 if (mesh.is_null()) {
258 return nullptr;
259 }
260
261 Ref<ConvexPolygonShape3D> shape = mesh->create_convex_shape(p_clean, p_simplify);
262 if (shape.is_null()) {
263 return nullptr;
264 }
265
266 StaticBody3D *static_body = memnew(StaticBody3D);
267 CollisionShape3D *cshape = memnew(CollisionShape3D);
268 cshape->set_shape(shape);
269 static_body->add_child(cshape, true);
270 return static_body;
271}
272
273void MeshInstance3D::create_convex_collision(bool p_clean, bool p_simplify) {
274 StaticBody3D *static_body = Object::cast_to<StaticBody3D>(create_convex_collision_node(p_clean, p_simplify));
275 ERR_FAIL_NULL(static_body);
276 static_body->set_name(String(get_name()) + "_col");
277
278 add_child(static_body, true);
279 if (get_owner()) {
280 CollisionShape3D *cshape = Object::cast_to<CollisionShape3D>(static_body->get_child(0));
281 static_body->set_owner(get_owner());
282 cshape->set_owner(get_owner());
283 }
284}
285
286Node *MeshInstance3D::create_multiple_convex_collisions_node(const Ref<MeshConvexDecompositionSettings> &p_settings) {
287 if (mesh.is_null()) {
288 return nullptr;
289 }
290
291 Ref<MeshConvexDecompositionSettings> settings;
292 if (p_settings.is_valid()) {
293 settings = p_settings;
294 } else {
295 settings.instantiate();
296 }
297
298 Vector<Ref<Shape3D>> shapes = mesh->convex_decompose(settings);
299 if (!shapes.size()) {
300 return nullptr;
301 }
302
303 StaticBody3D *static_body = memnew(StaticBody3D);
304 for (int i = 0; i < shapes.size(); i++) {
305 CollisionShape3D *cshape = memnew(CollisionShape3D);
306 cshape->set_shape(shapes[i]);
307 static_body->add_child(cshape, true);
308 }
309 return static_body;
310}
311
312void MeshInstance3D::create_multiple_convex_collisions(const Ref<MeshConvexDecompositionSettings> &p_settings) {
313 StaticBody3D *static_body = Object::cast_to<StaticBody3D>(create_multiple_convex_collisions_node(p_settings));
314 ERR_FAIL_NULL(static_body);
315 static_body->set_name(String(get_name()) + "_col");
316
317 add_child(static_body, true);
318 if (get_owner()) {
319 static_body->set_owner(get_owner());
320 int count = static_body->get_child_count();
321 for (int i = 0; i < count; i++) {
322 CollisionShape3D *cshape = Object::cast_to<CollisionShape3D>(static_body->get_child(i));
323 cshape->set_owner(get_owner());
324 }
325 }
326}
327
328void MeshInstance3D::_notification(int p_what) {
329 switch (p_what) {
330 case NOTIFICATION_ENTER_TREE: {
331 _resolve_skeleton_path();
332 } break;
333 case NOTIFICATION_TRANSLATION_CHANGED: {
334 if (mesh.is_valid()) {
335 mesh->notification(NOTIFICATION_TRANSLATION_CHANGED);
336 }
337 } break;
338 }
339}
340
341int MeshInstance3D::get_surface_override_material_count() const {
342 return surface_override_materials.size();
343}
344
345void MeshInstance3D::set_surface_override_material(int p_surface, const Ref<Material> &p_material) {
346 ERR_FAIL_INDEX(p_surface, surface_override_materials.size());
347
348 surface_override_materials.write[p_surface] = p_material;
349
350 if (surface_override_materials[p_surface].is_valid()) {
351 RS::get_singleton()->instance_set_surface_override_material(get_instance(), p_surface, surface_override_materials[p_surface]->get_rid());
352 } else {
353 RS::get_singleton()->instance_set_surface_override_material(get_instance(), p_surface, RID());
354 }
355}
356
357Ref<Material> MeshInstance3D::get_surface_override_material(int p_surface) const {
358 ERR_FAIL_INDEX_V(p_surface, surface_override_materials.size(), Ref<Material>());
359
360 return surface_override_materials[p_surface];
361}
362
363Ref<Material> MeshInstance3D::get_active_material(int p_surface) const {
364 Ref<Material> mat_override = get_material_override();
365 if (mat_override.is_valid()) {
366 return mat_override;
367 }
368
369 Ref<Material> surface_material = get_surface_override_material(p_surface);
370 if (surface_material.is_valid()) {
371 return surface_material;
372 }
373
374 Ref<Mesh> m = get_mesh();
375 if (m.is_valid()) {
376 return m->surface_get_material(p_surface);
377 }
378
379 return Ref<Material>();
380}
381
382void MeshInstance3D::_mesh_changed() {
383 ERR_FAIL_COND(mesh.is_null());
384 surface_override_materials.resize(mesh->get_surface_count());
385
386 uint32_t initialize_bs_from = blend_shape_tracks.size();
387 blend_shape_tracks.resize(mesh->get_blend_shape_count());
388
389 for (uint32_t i = 0; i < blend_shape_tracks.size(); i++) {
390 blend_shape_properties["blend_shapes/" + String(mesh->get_blend_shape_name(i))] = i;
391 if (i < initialize_bs_from) {
392 set_blend_shape_value(i, blend_shape_tracks[i]);
393 } else {
394 set_blend_shape_value(i, 0);
395 }
396 }
397
398 int surface_count = mesh->get_surface_count();
399 for (int surface_index = 0; surface_index < surface_count; ++surface_index) {
400 if (surface_override_materials[surface_index].is_valid()) {
401 RS::get_singleton()->instance_set_surface_override_material(get_instance(), surface_index, surface_override_materials[surface_index]->get_rid());
402 }
403 }
404
405 update_gizmos();
406}
407
408MeshInstance3D *MeshInstance3D::create_debug_tangents_node() {
409 Vector<Vector3> lines;
410 Vector<Color> colors;
411
412 Ref<Mesh> m = get_mesh();
413 if (!m.is_valid()) {
414 return nullptr;
415 }
416
417 for (int i = 0; i < m->get_surface_count(); i++) {
418 Array arrays = m->surface_get_arrays(i);
419 ERR_CONTINUE(arrays.size() != Mesh::ARRAY_MAX);
420
421 Vector<Vector3> verts = arrays[Mesh::ARRAY_VERTEX];
422 Vector<Vector3> norms = arrays[Mesh::ARRAY_NORMAL];
423 if (norms.size() == 0) {
424 continue;
425 }
426 Vector<float> tangents = arrays[Mesh::ARRAY_TANGENT];
427 if (tangents.size() == 0) {
428 continue;
429 }
430
431 for (int j = 0; j < verts.size(); j++) {
432 Vector3 v = verts[j];
433 Vector3 n = norms[j];
434 Vector3 t = Vector3(tangents[j * 4 + 0], tangents[j * 4 + 1], tangents[j * 4 + 2]);
435 Vector3 b = (n.cross(t)).normalized() * tangents[j * 4 + 3];
436
437 lines.push_back(v); //normal
438 colors.push_back(Color(0, 0, 1)); //color
439 lines.push_back(v + n * 0.04); //normal
440 colors.push_back(Color(0, 0, 1)); //color
441
442 lines.push_back(v); //tangent
443 colors.push_back(Color(1, 0, 0)); //color
444 lines.push_back(v + t * 0.04); //tangent
445 colors.push_back(Color(1, 0, 0)); //color
446
447 lines.push_back(v); //binormal
448 colors.push_back(Color(0, 1, 0)); //color
449 lines.push_back(v + b * 0.04); //binormal
450 colors.push_back(Color(0, 1, 0)); //color
451 }
452 }
453
454 if (lines.size()) {
455 Ref<StandardMaterial3D> sm;
456 sm.instantiate();
457
458 sm->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
459 sm->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
460 sm->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
461
462 Ref<ArrayMesh> am;
463 am.instantiate();
464 Array a;
465 a.resize(Mesh::ARRAY_MAX);
466 a[Mesh::ARRAY_VERTEX] = lines;
467 a[Mesh::ARRAY_COLOR] = colors;
468
469 am->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, a);
470 am->surface_set_material(0, sm);
471
472 MeshInstance3D *mi = memnew(MeshInstance3D);
473 mi->set_mesh(am);
474 mi->set_name("DebugTangents");
475 return mi;
476 }
477
478 return nullptr;
479}
480
481void MeshInstance3D::create_debug_tangents() {
482 MeshInstance3D *mi = create_debug_tangents_node();
483 if (!mi) {
484 return;
485 }
486
487 add_child(mi, true);
488 if (is_inside_tree() && this == get_tree()->get_edited_scene_root()) {
489 mi->set_owner(this);
490 } else {
491 mi->set_owner(get_owner());
492 }
493}
494
495void MeshInstance3D::_bind_methods() {
496 ClassDB::bind_method(D_METHOD("set_mesh", "mesh"), &MeshInstance3D::set_mesh);
497 ClassDB::bind_method(D_METHOD("get_mesh"), &MeshInstance3D::get_mesh);
498 ClassDB::bind_method(D_METHOD("set_skeleton_path", "skeleton_path"), &MeshInstance3D::set_skeleton_path);
499 ClassDB::bind_method(D_METHOD("get_skeleton_path"), &MeshInstance3D::get_skeleton_path);
500 ClassDB::bind_method(D_METHOD("set_skin", "skin"), &MeshInstance3D::set_skin);
501 ClassDB::bind_method(D_METHOD("get_skin"), &MeshInstance3D::get_skin);
502
503 ClassDB::bind_method(D_METHOD("get_surface_override_material_count"), &MeshInstance3D::get_surface_override_material_count);
504 ClassDB::bind_method(D_METHOD("set_surface_override_material", "surface", "material"), &MeshInstance3D::set_surface_override_material);
505 ClassDB::bind_method(D_METHOD("get_surface_override_material", "surface"), &MeshInstance3D::get_surface_override_material);
506 ClassDB::bind_method(D_METHOD("get_active_material", "surface"), &MeshInstance3D::get_active_material);
507
508 ClassDB::bind_method(D_METHOD("create_trimesh_collision"), &MeshInstance3D::create_trimesh_collision);
509 ClassDB::set_method_flags("MeshInstance3D", "create_trimesh_collision", METHOD_FLAGS_DEFAULT);
510 ClassDB::bind_method(D_METHOD("create_convex_collision", "clean", "simplify"), &MeshInstance3D::create_convex_collision, DEFVAL(true), DEFVAL(false));
511 ClassDB::set_method_flags("MeshInstance3D", "create_convex_collision", METHOD_FLAGS_DEFAULT);
512 ClassDB::bind_method(D_METHOD("create_multiple_convex_collisions", "settings"), &MeshInstance3D::create_multiple_convex_collisions, DEFVAL(Ref<MeshConvexDecompositionSettings>()));
513 ClassDB::set_method_flags("MeshInstance3D", "create_multiple_convex_collisions", METHOD_FLAGS_DEFAULT);
514
515 ClassDB::bind_method(D_METHOD("get_blend_shape_count"), &MeshInstance3D::get_blend_shape_count);
516 ClassDB::bind_method(D_METHOD("find_blend_shape_by_name", "name"), &MeshInstance3D::find_blend_shape_by_name);
517 ClassDB::bind_method(D_METHOD("get_blend_shape_value", "blend_shape_idx"), &MeshInstance3D::get_blend_shape_value);
518 ClassDB::bind_method(D_METHOD("set_blend_shape_value", "blend_shape_idx", "value"), &MeshInstance3D::set_blend_shape_value);
519
520 ClassDB::bind_method(D_METHOD("create_debug_tangents"), &MeshInstance3D::create_debug_tangents);
521 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh", PROPERTY_HINT_RESOURCE_TYPE, "Mesh"), "set_mesh", "get_mesh");
522 ADD_GROUP("Skeleton", "");
523 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "skin", PROPERTY_HINT_RESOURCE_TYPE, "Skin"), "set_skin", "get_skin");
524 ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "skeleton", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Skeleton3D"), "set_skeleton_path", "get_skeleton_path");
525 ADD_GROUP("", "");
526}
527
528MeshInstance3D::MeshInstance3D() {
529}
530
531MeshInstance3D::~MeshInstance3D() {
532}
533