mesh_instance_3d.cpp 35 KB

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