mesh_instance_3d.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  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_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(skeleton_path));
  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. _resolve_skeleton_path();
  292. } break;
  293. case NOTIFICATION_TRANSLATION_CHANGED: {
  294. if (mesh.is_valid()) {
  295. mesh->notification(NOTIFICATION_TRANSLATION_CHANGED);
  296. }
  297. } break;
  298. }
  299. }
  300. int MeshInstance3D::get_surface_override_material_count() const {
  301. return surface_override_materials.size();
  302. }
  303. void MeshInstance3D::set_surface_override_material(int p_surface, const Ref<Material> &p_material) {
  304. ERR_FAIL_INDEX(p_surface, surface_override_materials.size());
  305. surface_override_materials.write[p_surface] = p_material;
  306. if (surface_override_materials[p_surface].is_valid()) {
  307. RS::get_singleton()->instance_set_surface_override_material(get_instance(), p_surface, surface_override_materials[p_surface]->get_rid());
  308. } else {
  309. RS::get_singleton()->instance_set_surface_override_material(get_instance(), p_surface, RID());
  310. }
  311. }
  312. Ref<Material> MeshInstance3D::get_surface_override_material(int p_surface) const {
  313. ERR_FAIL_INDEX_V(p_surface, surface_override_materials.size(), Ref<Material>());
  314. return surface_override_materials[p_surface];
  315. }
  316. Ref<Material> MeshInstance3D::get_active_material(int p_surface) const {
  317. Ref<Material> mat_override = get_material_override();
  318. if (mat_override.is_valid()) {
  319. return mat_override;
  320. }
  321. Ref<Material> surface_material = get_surface_override_material(p_surface);
  322. if (surface_material.is_valid()) {
  323. return surface_material;
  324. }
  325. Ref<Mesh> m = get_mesh();
  326. if (m.is_valid()) {
  327. return m->surface_get_material(p_surface);
  328. }
  329. return Ref<Material>();
  330. }
  331. void MeshInstance3D::_mesh_changed() {
  332. ERR_FAIL_COND(mesh.is_null());
  333. const int surface_count = mesh->get_surface_count();
  334. surface_override_materials.resize(surface_count);
  335. uint32_t initialize_bs_from = blend_shape_tracks.size();
  336. blend_shape_tracks.resize(mesh->get_blend_shape_count());
  337. if (surface_count > 0) {
  338. for (uint32_t i = 0; i < blend_shape_tracks.size(); i++) {
  339. blend_shape_properties["blend_shapes/" + String(mesh->get_blend_shape_name(i))] = i;
  340. if (i < initialize_bs_from) {
  341. set_blend_shape_value(i, blend_shape_tracks[i]);
  342. } else {
  343. set_blend_shape_value(i, 0);
  344. }
  345. }
  346. }
  347. for (int surface_index = 0; surface_index < surface_count; ++surface_index) {
  348. if (surface_override_materials[surface_index].is_valid()) {
  349. RS::get_singleton()->instance_set_surface_override_material(get_instance(), surface_index, surface_override_materials[surface_index]->get_rid());
  350. }
  351. }
  352. update_gizmos();
  353. }
  354. MeshInstance3D *MeshInstance3D::create_debug_tangents_node() {
  355. Vector<Vector3> lines;
  356. Vector<Color> colors;
  357. Ref<Mesh> m = get_mesh();
  358. if (m.is_null()) {
  359. return nullptr;
  360. }
  361. for (int i = 0; i < m->get_surface_count(); i++) {
  362. Array arrays = m->surface_get_arrays(i);
  363. ERR_CONTINUE(arrays.size() != Mesh::ARRAY_MAX);
  364. Vector<Vector3> verts = arrays[Mesh::ARRAY_VERTEX];
  365. Vector<Vector3> norms = arrays[Mesh::ARRAY_NORMAL];
  366. if (norms.is_empty()) {
  367. continue;
  368. }
  369. Vector<float> tangents = arrays[Mesh::ARRAY_TANGENT];
  370. if (tangents.is_empty()) {
  371. continue;
  372. }
  373. for (int j = 0; j < verts.size(); j++) {
  374. Vector3 v = verts[j];
  375. Vector3 n = norms[j];
  376. Vector3 t = Vector3(tangents[j * 4 + 0], tangents[j * 4 + 1], tangents[j * 4 + 2]);
  377. Vector3 b = (n.cross(t)).normalized() * tangents[j * 4 + 3];
  378. lines.push_back(v); //normal
  379. colors.push_back(Color(0, 0, 1)); //color
  380. lines.push_back(v + n * 0.04); //normal
  381. colors.push_back(Color(0, 0, 1)); //color
  382. lines.push_back(v); //tangent
  383. colors.push_back(Color(1, 0, 0)); //color
  384. lines.push_back(v + t * 0.04); //tangent
  385. colors.push_back(Color(1, 0, 0)); //color
  386. lines.push_back(v); //binormal
  387. colors.push_back(Color(0, 1, 0)); //color
  388. lines.push_back(v + b * 0.04); //binormal
  389. colors.push_back(Color(0, 1, 0)); //color
  390. }
  391. }
  392. if (lines.size()) {
  393. Ref<StandardMaterial3D> sm;
  394. sm.instantiate();
  395. sm->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  396. sm->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
  397. sm->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  398. sm->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
  399. Ref<ArrayMesh> am;
  400. am.instantiate();
  401. Array a;
  402. a.resize(Mesh::ARRAY_MAX);
  403. a[Mesh::ARRAY_VERTEX] = lines;
  404. a[Mesh::ARRAY_COLOR] = colors;
  405. am->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, a);
  406. am->surface_set_material(0, sm);
  407. MeshInstance3D *mi = memnew(MeshInstance3D);
  408. mi->set_mesh(am);
  409. mi->set_name("DebugTangents");
  410. return mi;
  411. }
  412. return nullptr;
  413. }
  414. void MeshInstance3D::create_debug_tangents() {
  415. MeshInstance3D *mi = create_debug_tangents_node();
  416. if (!mi) {
  417. return;
  418. }
  419. add_child(mi, true);
  420. if (is_inside_tree() && this == get_tree()->get_edited_scene_root()) {
  421. mi->set_owner(this);
  422. } else {
  423. mi->set_owner(get_owner());
  424. }
  425. }
  426. bool MeshInstance3D::_property_can_revert(const StringName &p_name) const {
  427. HashMap<StringName, int>::ConstIterator E = blend_shape_properties.find(p_name);
  428. if (E) {
  429. return true;
  430. }
  431. return false;
  432. }
  433. bool MeshInstance3D::_property_get_revert(const StringName &p_name, Variant &r_property) const {
  434. HashMap<StringName, int>::ConstIterator E = blend_shape_properties.find(p_name);
  435. if (E) {
  436. r_property = 0.0f;
  437. return true;
  438. }
  439. return false;
  440. }
  441. Ref<ArrayMesh> MeshInstance3D::bake_mesh_from_current_blend_shape_mix(Ref<ArrayMesh> p_existing) {
  442. Ref<ArrayMesh> source_mesh = get_mesh();
  443. ERR_FAIL_COND_V_MSG(source_mesh.is_null(), Ref<ArrayMesh>(), "The source mesh must be a valid ArrayMesh.");
  444. Ref<ArrayMesh> bake_mesh;
  445. if (p_existing.is_valid()) {
  446. ERR_FAIL_COND_V_MSG(p_existing.is_null(), Ref<ArrayMesh>(), "The existing mesh must be a valid ArrayMesh.");
  447. ERR_FAIL_COND_V_MSG(source_mesh == p_existing, Ref<ArrayMesh>(), "The source mesh can not be the same mesh as the existing mesh.");
  448. bake_mesh = p_existing;
  449. } else {
  450. bake_mesh.instantiate();
  451. }
  452. Mesh::BlendShapeMode blend_shape_mode = source_mesh->get_blend_shape_mode();
  453. int mesh_surface_count = source_mesh->get_surface_count();
  454. bake_mesh->clear_surfaces();
  455. bake_mesh->set_blend_shape_mode(blend_shape_mode);
  456. for (int surface_index = 0; surface_index < mesh_surface_count; surface_index++) {
  457. uint32_t surface_format = source_mesh->surface_get_format(surface_index);
  458. ERR_CONTINUE(0 == (surface_format & Mesh::ARRAY_FORMAT_VERTEX));
  459. const Array &source_mesh_arrays = source_mesh->surface_get_arrays(surface_index);
  460. ERR_FAIL_COND_V(source_mesh_arrays.size() != RS::ARRAY_MAX, Ref<ArrayMesh>());
  461. const Vector<Vector3> &source_mesh_vertex_array = source_mesh_arrays[Mesh::ARRAY_VERTEX];
  462. const Vector<Vector3> &source_mesh_normal_array = source_mesh_arrays[Mesh::ARRAY_NORMAL];
  463. const Vector<float> &source_mesh_tangent_array = source_mesh_arrays[Mesh::ARRAY_TANGENT];
  464. Array new_mesh_arrays;
  465. new_mesh_arrays.resize(Mesh::ARRAY_MAX);
  466. for (int i = 0; i < source_mesh_arrays.size(); i++) {
  467. if (i == Mesh::ARRAY_VERTEX || i == Mesh::ARRAY_NORMAL || i == Mesh::ARRAY_TANGENT) {
  468. continue;
  469. }
  470. new_mesh_arrays[i] = source_mesh_arrays[i];
  471. }
  472. bool use_normal_array = source_mesh_normal_array.size() == source_mesh_vertex_array.size();
  473. bool use_tangent_array = source_mesh_tangent_array.size() / 4 == source_mesh_vertex_array.size();
  474. Vector<Vector3> lerped_vertex_array = source_mesh_vertex_array;
  475. Vector<Vector3> lerped_normal_array = source_mesh_normal_array;
  476. Vector<float> lerped_tangent_array = source_mesh_tangent_array;
  477. const Vector3 *source_vertices_ptr = source_mesh_vertex_array.ptr();
  478. const Vector3 *source_normals_ptr = source_mesh_normal_array.ptr();
  479. const float *source_tangents_ptr = source_mesh_tangent_array.ptr();
  480. Vector3 *lerped_vertices_ptrw = lerped_vertex_array.ptrw();
  481. Vector3 *lerped_normals_ptrw = lerped_normal_array.ptrw();
  482. float *lerped_tangents_ptrw = lerped_tangent_array.ptrw();
  483. const Array &blendshapes_mesh_arrays = source_mesh->surface_get_blend_shape_arrays(surface_index);
  484. int blend_shape_count = source_mesh->get_blend_shape_count();
  485. ERR_FAIL_COND_V(blendshapes_mesh_arrays.size() != blend_shape_count, Ref<ArrayMesh>());
  486. for (int blendshape_index = 0; blendshape_index < blend_shape_count; blendshape_index++) {
  487. float blend_weight = get_blend_shape_value(blendshape_index);
  488. if (std::abs(blend_weight) <= 0.0001) {
  489. continue;
  490. }
  491. const Array &blendshape_mesh_arrays = blendshapes_mesh_arrays[blendshape_index];
  492. const Vector<Vector3> &blendshape_vertex_array = blendshape_mesh_arrays[Mesh::ARRAY_VERTEX];
  493. const Vector<Vector3> &blendshape_normal_array = blendshape_mesh_arrays[Mesh::ARRAY_NORMAL];
  494. const Vector<float> &blendshape_tangent_array = blendshape_mesh_arrays[Mesh::ARRAY_TANGENT];
  495. ERR_FAIL_COND_V(source_mesh_vertex_array.size() != blendshape_vertex_array.size(), Ref<ArrayMesh>());
  496. ERR_FAIL_COND_V(source_mesh_normal_array.size() != blendshape_normal_array.size(), Ref<ArrayMesh>());
  497. ERR_FAIL_COND_V(source_mesh_tangent_array.size() != blendshape_tangent_array.size(), Ref<ArrayMesh>());
  498. const Vector3 *blendshape_vertices_ptr = blendshape_vertex_array.ptr();
  499. const Vector3 *blendshape_normals_ptr = blendshape_normal_array.ptr();
  500. const float *blendshape_tangents_ptr = blendshape_tangent_array.ptr();
  501. if (blend_shape_mode == Mesh::BLEND_SHAPE_MODE_NORMALIZED) {
  502. for (int i = 0; i < source_mesh_vertex_array.size(); i++) {
  503. const Vector3 &source_vertex = source_vertices_ptr[i];
  504. const Vector3 &blendshape_vertex = blendshape_vertices_ptr[i];
  505. Vector3 lerped_vertex = source_vertex.lerp(blendshape_vertex, blend_weight) - source_vertex;
  506. lerped_vertices_ptrw[i] += lerped_vertex;
  507. if (use_normal_array) {
  508. const Vector3 &source_normal = source_normals_ptr[i];
  509. const Vector3 &blendshape_normal = blendshape_normals_ptr[i];
  510. Vector3 lerped_normal = source_normal.lerp(blendshape_normal, blend_weight) - source_normal;
  511. lerped_normals_ptrw[i] += lerped_normal;
  512. }
  513. if (use_tangent_array) {
  514. int tangent_index = i * 4;
  515. const Vector4 source_tangent = Vector4(
  516. source_tangents_ptr[tangent_index],
  517. source_tangents_ptr[tangent_index + 1],
  518. source_tangents_ptr[tangent_index + 2],
  519. source_tangents_ptr[tangent_index + 3]);
  520. const Vector4 blendshape_tangent = Vector4(
  521. blendshape_tangents_ptr[tangent_index],
  522. blendshape_tangents_ptr[tangent_index + 1],
  523. blendshape_tangents_ptr[tangent_index + 2],
  524. blendshape_tangents_ptr[tangent_index + 3]);
  525. Vector4 lerped_tangent = source_tangent.lerp(blendshape_tangent, blend_weight);
  526. lerped_tangents_ptrw[tangent_index] += lerped_tangent.x;
  527. lerped_tangents_ptrw[tangent_index + 1] += lerped_tangent.y;
  528. lerped_tangents_ptrw[tangent_index + 2] += lerped_tangent.z;
  529. lerped_tangents_ptrw[tangent_index + 3] += lerped_tangent.w;
  530. }
  531. }
  532. } else if (blend_shape_mode == Mesh::BLEND_SHAPE_MODE_RELATIVE) {
  533. for (int i = 0; i < source_mesh_vertex_array.size(); i++) {
  534. const Vector3 &blendshape_vertex = blendshape_vertices_ptr[i];
  535. lerped_vertices_ptrw[i] += blendshape_vertex * blend_weight;
  536. if (use_normal_array) {
  537. const Vector3 &blendshape_normal = blendshape_normals_ptr[i];
  538. lerped_normals_ptrw[i] += blendshape_normal * blend_weight;
  539. }
  540. if (use_tangent_array) {
  541. int tangent_index = i * 4;
  542. const Vector4 blendshape_tangent = Vector4(
  543. blendshape_tangents_ptr[tangent_index],
  544. blendshape_tangents_ptr[tangent_index + 1],
  545. blendshape_tangents_ptr[tangent_index + 2],
  546. blendshape_tangents_ptr[tangent_index + 3]);
  547. Vector4 lerped_tangent = blendshape_tangent * blend_weight;
  548. lerped_tangents_ptrw[tangent_index] += lerped_tangent.x;
  549. lerped_tangents_ptrw[tangent_index + 1] += lerped_tangent.y;
  550. lerped_tangents_ptrw[tangent_index + 2] += lerped_tangent.z;
  551. lerped_tangents_ptrw[tangent_index + 3] += lerped_tangent.w;
  552. }
  553. }
  554. }
  555. }
  556. new_mesh_arrays[Mesh::ARRAY_VERTEX] = lerped_vertex_array;
  557. if (use_normal_array) {
  558. new_mesh_arrays[Mesh::ARRAY_NORMAL] = lerped_normal_array;
  559. }
  560. if (use_tangent_array) {
  561. new_mesh_arrays[Mesh::ARRAY_TANGENT] = lerped_tangent_array;
  562. }
  563. bake_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, new_mesh_arrays, Array(), Dictionary(), surface_format);
  564. }
  565. return bake_mesh;
  566. }
  567. Ref<ArrayMesh> MeshInstance3D::bake_mesh_from_current_skeleton_pose(Ref<ArrayMesh> p_existing) {
  568. Ref<ArrayMesh> source_mesh = get_mesh();
  569. ERR_FAIL_COND_V_MSG(source_mesh.is_null(), Ref<ArrayMesh>(), "The source mesh must be a valid ArrayMesh.");
  570. Ref<ArrayMesh> bake_mesh;
  571. if (p_existing.is_valid()) {
  572. ERR_FAIL_COND_V_MSG(source_mesh == p_existing, Ref<ArrayMesh>(), "The source mesh can not be the same mesh as the existing mesh.");
  573. bake_mesh = p_existing;
  574. } else {
  575. bake_mesh.instantiate();
  576. }
  577. ERR_FAIL_COND_V_MSG(skin_ref.is_null(), Ref<ArrayMesh>(), "The source mesh must have a valid skin.");
  578. ERR_FAIL_COND_V_MSG(skin_internal.is_null(), Ref<ArrayMesh>(), "The source mesh must have a valid skin.");
  579. RID skeleton = skin_ref->get_skeleton();
  580. ERR_FAIL_COND_V_MSG(!skeleton.is_valid(), Ref<ArrayMesh>(), "The source mesh must have its skin registered with a valid skeleton.");
  581. const int bone_count = RenderingServer::get_singleton()->skeleton_get_bone_count(skeleton);
  582. ERR_FAIL_COND_V(bone_count <= 0, Ref<ArrayMesh>());
  583. ERR_FAIL_COND_V(bone_count < skin_internal->get_bind_count(), Ref<ArrayMesh>());
  584. LocalVector<Transform3D> bone_transforms;
  585. bone_transforms.resize(bone_count);
  586. for (int bone_index = 0; bone_index < bone_count; bone_index++) {
  587. bone_transforms[bone_index] = RenderingServer::get_singleton()->skeleton_bone_get_transform(skeleton, bone_index);
  588. }
  589. bake_mesh->clear_surfaces();
  590. int mesh_surface_count = source_mesh->get_surface_count();
  591. for (int surface_index = 0; surface_index < mesh_surface_count; surface_index++) {
  592. ERR_CONTINUE(source_mesh->surface_get_primitive_type(surface_index) != Mesh::PRIMITIVE_TRIANGLES);
  593. uint32_t surface_format = source_mesh->surface_get_format(surface_index);
  594. ERR_CONTINUE(0 == (surface_format & Mesh::ARRAY_FORMAT_VERTEX));
  595. ERR_CONTINUE(0 == (surface_format & Mesh::ARRAY_FORMAT_BONES));
  596. ERR_CONTINUE(0 == (surface_format & Mesh::ARRAY_FORMAT_WEIGHTS));
  597. unsigned int bones_per_vertex = surface_format & Mesh::ARRAY_FLAG_USE_8_BONE_WEIGHTS ? 8 : 4;
  598. surface_format &= ~Mesh::ARRAY_FORMAT_BONES;
  599. surface_format &= ~Mesh::ARRAY_FORMAT_WEIGHTS;
  600. const Array &source_mesh_arrays = source_mesh->surface_get_arrays(surface_index);
  601. ERR_FAIL_COND_V(source_mesh_arrays.size() != RS::ARRAY_MAX, Ref<ArrayMesh>());
  602. const Vector<Vector3> &source_mesh_vertex_array = source_mesh_arrays[Mesh::ARRAY_VERTEX];
  603. const Vector<Vector3> &source_mesh_normal_array = source_mesh_arrays[Mesh::ARRAY_NORMAL];
  604. const Vector<float> &source_mesh_tangent_array = source_mesh_arrays[Mesh::ARRAY_TANGENT];
  605. const Vector<int> &source_mesh_bones_array = source_mesh_arrays[Mesh::ARRAY_BONES];
  606. const Vector<float> &source_mesh_weights_array = source_mesh_arrays[Mesh::ARRAY_WEIGHTS];
  607. unsigned int vertex_count = source_mesh_vertex_array.size();
  608. int expected_bone_array_size = vertex_count * bones_per_vertex;
  609. ERR_CONTINUE(source_mesh_bones_array.size() != expected_bone_array_size);
  610. ERR_CONTINUE(source_mesh_weights_array.size() != expected_bone_array_size);
  611. Array new_mesh_arrays;
  612. new_mesh_arrays.resize(Mesh::ARRAY_MAX);
  613. for (int i = 0; i < source_mesh_arrays.size(); i++) {
  614. if (i == Mesh::ARRAY_VERTEX || i == Mesh::ARRAY_NORMAL || i == Mesh::ARRAY_TANGENT || i == Mesh::ARRAY_BONES || i == Mesh::ARRAY_WEIGHTS) {
  615. continue;
  616. }
  617. new_mesh_arrays[i] = source_mesh_arrays[i];
  618. }
  619. bool use_normal_array = source_mesh_normal_array.size() == source_mesh_vertex_array.size();
  620. bool use_tangent_array = source_mesh_tangent_array.size() / 4 == source_mesh_vertex_array.size();
  621. Vector<Vector3> lerped_vertex_array = source_mesh_vertex_array;
  622. Vector<Vector3> lerped_normal_array = source_mesh_normal_array;
  623. Vector<float> lerped_tangent_array = source_mesh_tangent_array;
  624. const Vector3 *source_vertices_ptr = source_mesh_vertex_array.ptr();
  625. const Vector3 *source_normals_ptr = source_mesh_normal_array.ptr();
  626. const float *source_tangents_ptr = source_mesh_tangent_array.ptr();
  627. const int *source_bones_ptr = source_mesh_bones_array.ptr();
  628. const float *source_weights_ptr = source_mesh_weights_array.ptr();
  629. Vector3 *lerped_vertices_ptrw = lerped_vertex_array.ptrw();
  630. Vector3 *lerped_normals_ptrw = lerped_normal_array.ptrw();
  631. float *lerped_tangents_ptrw = lerped_tangent_array.ptrw();
  632. for (unsigned int vertex_index = 0; vertex_index < vertex_count; vertex_index++) {
  633. Vector3 lerped_vertex;
  634. Vector3 lerped_normal;
  635. Vector3 lerped_tangent;
  636. const Vector3 &source_vertex = source_vertices_ptr[vertex_index];
  637. Vector3 source_normal;
  638. if (use_normal_array) {
  639. source_normal = source_normals_ptr[vertex_index];
  640. }
  641. int tangent_index = vertex_index * 4;
  642. Vector4 source_tangent;
  643. Vector3 source_tangent_vec3;
  644. if (use_tangent_array) {
  645. source_tangent = Vector4(
  646. source_tangents_ptr[tangent_index],
  647. source_tangents_ptr[tangent_index + 1],
  648. source_tangents_ptr[tangent_index + 2],
  649. source_tangents_ptr[tangent_index + 3]);
  650. DEV_ASSERT(source_tangent.w == 1.0 || source_tangent.w == -1.0);
  651. source_tangent_vec3 = Vector3(source_tangent.x, source_tangent.y, source_tangent.z);
  652. }
  653. for (unsigned int weight_index = 0; weight_index < bones_per_vertex; weight_index++) {
  654. float bone_weight = source_weights_ptr[vertex_index * bones_per_vertex + weight_index];
  655. if (bone_weight < FLT_EPSILON) {
  656. continue;
  657. }
  658. int vertex_bone_index = source_bones_ptr[vertex_index * bones_per_vertex + weight_index];
  659. const Transform3D &bone_transform = bone_transforms[vertex_bone_index];
  660. const Basis bone_basis = bone_transform.basis.orthonormalized();
  661. ERR_FAIL_INDEX_V(vertex_bone_index, static_cast<int>(bone_transforms.size()), Ref<ArrayMesh>());
  662. lerped_vertex += source_vertex.lerp(bone_transform.xform(source_vertex), bone_weight) - source_vertex;
  663. ;
  664. if (use_normal_array) {
  665. lerped_normal += source_normal.lerp(bone_basis.xform(source_normal), bone_weight) - source_normal;
  666. }
  667. if (use_tangent_array) {
  668. lerped_tangent += source_tangent_vec3.lerp(bone_basis.xform(source_tangent_vec3), bone_weight) - source_tangent_vec3;
  669. }
  670. }
  671. lerped_vertices_ptrw[vertex_index] += lerped_vertex;
  672. if (use_normal_array) {
  673. lerped_normals_ptrw[vertex_index] = (source_normal + lerped_normal).normalized();
  674. }
  675. if (use_tangent_array) {
  676. lerped_tangent = (source_tangent_vec3 + lerped_tangent).normalized();
  677. lerped_tangents_ptrw[tangent_index] = lerped_tangent.x;
  678. lerped_tangents_ptrw[tangent_index + 1] = lerped_tangent.y;
  679. lerped_tangents_ptrw[tangent_index + 2] = lerped_tangent.z;
  680. }
  681. }
  682. new_mesh_arrays[Mesh::ARRAY_VERTEX] = lerped_vertex_array;
  683. if (use_normal_array) {
  684. new_mesh_arrays[Mesh::ARRAY_NORMAL] = lerped_normal_array;
  685. }
  686. if (use_tangent_array) {
  687. new_mesh_arrays[Mesh::ARRAY_TANGENT] = lerped_tangent_array;
  688. }
  689. bake_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, new_mesh_arrays, Array(), Dictionary(), surface_format);
  690. }
  691. return bake_mesh;
  692. }
  693. Ref<TriangleMesh> MeshInstance3D::generate_triangle_mesh() const {
  694. if (mesh.is_valid()) {
  695. return mesh->generate_triangle_mesh();
  696. }
  697. return Ref<TriangleMesh>();
  698. }
  699. #ifndef NAVIGATION_3D_DISABLED
  700. void MeshInstance3D::navmesh_parse_init() {
  701. ERR_FAIL_NULL(NavigationServer3D::get_singleton());
  702. if (!_navmesh_source_geometry_parser.is_valid()) {
  703. _navmesh_source_geometry_parsing_callback = callable_mp_static(&MeshInstance3D::navmesh_parse_source_geometry);
  704. _navmesh_source_geometry_parser = NavigationServer3D::get_singleton()->source_geometry_parser_create();
  705. NavigationServer3D::get_singleton()->source_geometry_parser_set_callback(_navmesh_source_geometry_parser, _navmesh_source_geometry_parsing_callback);
  706. }
  707. }
  708. void MeshInstance3D::navmesh_parse_source_geometry(const Ref<NavigationMesh> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData3D> p_source_geometry_data, Node *p_node) {
  709. MeshInstance3D *mesh_instance = Object::cast_to<MeshInstance3D>(p_node);
  710. if (mesh_instance == nullptr) {
  711. return;
  712. }
  713. NavigationMesh::ParsedGeometryType parsed_geometry_type = p_navigation_mesh->get_parsed_geometry_type();
  714. if (parsed_geometry_type == NavigationMesh::PARSED_GEOMETRY_MESH_INSTANCES || parsed_geometry_type == NavigationMesh::PARSED_GEOMETRY_BOTH) {
  715. Ref<Mesh> mesh = mesh_instance->get_mesh();
  716. if (mesh.is_valid()) {
  717. p_source_geometry_data->add_mesh(mesh, mesh_instance->get_global_transform());
  718. }
  719. }
  720. }
  721. #endif // NAVIGATION_3D_DISABLED
  722. void MeshInstance3D::_bind_methods() {
  723. ClassDB::bind_method(D_METHOD("set_mesh", "mesh"), &MeshInstance3D::set_mesh);
  724. ClassDB::bind_method(D_METHOD("get_mesh"), &MeshInstance3D::get_mesh);
  725. ClassDB::bind_method(D_METHOD("set_skeleton_path", "skeleton_path"), &MeshInstance3D::set_skeleton_path);
  726. ClassDB::bind_method(D_METHOD("get_skeleton_path"), &MeshInstance3D::get_skeleton_path);
  727. ClassDB::bind_method(D_METHOD("set_skin", "skin"), &MeshInstance3D::set_skin);
  728. ClassDB::bind_method(D_METHOD("get_skin"), &MeshInstance3D::get_skin);
  729. ClassDB::bind_method(D_METHOD("get_skin_reference"), &MeshInstance3D::get_skin_reference);
  730. ClassDB::bind_method(D_METHOD("get_surface_override_material_count"), &MeshInstance3D::get_surface_override_material_count);
  731. ClassDB::bind_method(D_METHOD("set_surface_override_material", "surface", "material"), &MeshInstance3D::set_surface_override_material);
  732. ClassDB::bind_method(D_METHOD("get_surface_override_material", "surface"), &MeshInstance3D::get_surface_override_material);
  733. ClassDB::bind_method(D_METHOD("get_active_material", "surface"), &MeshInstance3D::get_active_material);
  734. #ifndef PHYSICS_3D_DISABLED
  735. ClassDB::bind_method(D_METHOD("create_trimesh_collision"), &MeshInstance3D::create_trimesh_collision);
  736. ClassDB::set_method_flags("MeshInstance3D", "create_trimesh_collision", METHOD_FLAGS_DEFAULT);
  737. ClassDB::bind_method(D_METHOD("create_convex_collision", "clean", "simplify"), &MeshInstance3D::create_convex_collision, DEFVAL(true), DEFVAL(false));
  738. ClassDB::set_method_flags("MeshInstance3D", "create_convex_collision", METHOD_FLAGS_DEFAULT);
  739. ClassDB::bind_method(D_METHOD("create_multiple_convex_collisions", "settings"), &MeshInstance3D::create_multiple_convex_collisions, DEFVAL(Ref<MeshConvexDecompositionSettings>()));
  740. ClassDB::set_method_flags("MeshInstance3D", "create_multiple_convex_collisions", METHOD_FLAGS_DEFAULT);
  741. #endif // PHYSICS_3D_DISABLED
  742. ClassDB::bind_method(D_METHOD("get_blend_shape_count"), &MeshInstance3D::get_blend_shape_count);
  743. ClassDB::bind_method(D_METHOD("find_blend_shape_by_name", "name"), &MeshInstance3D::find_blend_shape_by_name);
  744. ClassDB::bind_method(D_METHOD("get_blend_shape_value", "blend_shape_idx"), &MeshInstance3D::get_blend_shape_value);
  745. ClassDB::bind_method(D_METHOD("set_blend_shape_value", "blend_shape_idx", "value"), &MeshInstance3D::set_blend_shape_value);
  746. ClassDB::bind_method(D_METHOD("create_debug_tangents"), &MeshInstance3D::create_debug_tangents);
  747. ClassDB::bind_method(D_METHOD("bake_mesh_from_current_blend_shape_mix", "existing"), &MeshInstance3D::bake_mesh_from_current_blend_shape_mix, DEFVAL(Ref<ArrayMesh>()));
  748. ClassDB::bind_method(D_METHOD("bake_mesh_from_current_skeleton_pose", "existing"), &MeshInstance3D::bake_mesh_from_current_skeleton_pose, DEFVAL(Ref<ArrayMesh>()));
  749. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh", PROPERTY_HINT_RESOURCE_TYPE, "Mesh"), "set_mesh", "get_mesh");
  750. ADD_GROUP("Skeleton", "");
  751. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "skin", PROPERTY_HINT_RESOURCE_TYPE, "Skin"), "set_skin", "get_skin");
  752. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "skeleton", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Skeleton3D"), "set_skeleton_path", "get_skeleton_path");
  753. ADD_GROUP("", "");
  754. }
  755. MeshInstance3D::MeshInstance3D() {
  756. }
  757. MeshInstance3D::~MeshInstance3D() {
  758. }