mesh_instance_3d.cpp 18 KB

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