mesh_instance_3d.cpp 33 KB

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