mesh_instance_3d.cpp 34 KB

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