mesh_instance_3d.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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 "core/core_string_names.h"
  33. #include "physics_body_3d.h"
  34. #include "scene/resources/concave_polygon_shape_3d.h"
  35. #include "scene/resources/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. List<String> ls;
  78. for (const KeyValue<StringName, int> &E : blend_shape_properties) {
  79. ls.push_back(E.key);
  80. }
  81. ls.sort();
  82. for (const String &E : ls) {
  83. p_list->push_back(PropertyInfo(Variant::FLOAT, E, 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 | PROPERTY_USAGE_DEFERRED_SET_RESOURCE));
  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(CoreStringNames::get_singleton()->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(CoreStringNames::get_singleton()->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. void MeshInstance3D::set_skeleton_path(const NodePath &p_skeleton) {
  178. skeleton_path = p_skeleton;
  179. if (!is_inside_tree()) {
  180. return;
  181. }
  182. _resolve_skeleton_path();
  183. }
  184. NodePath MeshInstance3D::get_skeleton_path() {
  185. return skeleton_path;
  186. }
  187. AABB MeshInstance3D::get_aabb() const {
  188. if (!mesh.is_null()) {
  189. return mesh->get_aabb();
  190. }
  191. return AABB();
  192. }
  193. Node *MeshInstance3D::create_trimesh_collision_node() {
  194. if (mesh.is_null()) {
  195. return nullptr;
  196. }
  197. Ref<ConcavePolygonShape3D> shape = mesh->create_trimesh_shape();
  198. if (shape.is_null()) {
  199. return nullptr;
  200. }
  201. StaticBody3D *static_body = memnew(StaticBody3D);
  202. CollisionShape3D *cshape = memnew(CollisionShape3D);
  203. cshape->set_shape(shape);
  204. static_body->add_child(cshape, true);
  205. return static_body;
  206. }
  207. void MeshInstance3D::create_trimesh_collision() {
  208. StaticBody3D *static_body = Object::cast_to<StaticBody3D>(create_trimesh_collision_node());
  209. ERR_FAIL_COND(!static_body);
  210. static_body->set_name(String(get_name()) + "_col");
  211. add_child(static_body, true);
  212. if (get_owner()) {
  213. CollisionShape3D *cshape = Object::cast_to<CollisionShape3D>(static_body->get_child(0));
  214. static_body->set_owner(get_owner());
  215. cshape->set_owner(get_owner());
  216. }
  217. }
  218. Node *MeshInstance3D::create_convex_collision_node(bool p_clean, bool p_simplify) {
  219. if (mesh.is_null()) {
  220. return nullptr;
  221. }
  222. Ref<ConvexPolygonShape3D> shape = mesh->create_convex_shape(p_clean, p_simplify);
  223. if (shape.is_null()) {
  224. return nullptr;
  225. }
  226. StaticBody3D *static_body = memnew(StaticBody3D);
  227. CollisionShape3D *cshape = memnew(CollisionShape3D);
  228. cshape->set_shape(shape);
  229. static_body->add_child(cshape, true);
  230. return static_body;
  231. }
  232. void MeshInstance3D::create_convex_collision(bool p_clean, bool p_simplify) {
  233. StaticBody3D *static_body = Object::cast_to<StaticBody3D>(create_convex_collision_node(p_clean, p_simplify));
  234. ERR_FAIL_COND(!static_body);
  235. static_body->set_name(String(get_name()) + "_col");
  236. add_child(static_body, true);
  237. if (get_owner()) {
  238. CollisionShape3D *cshape = Object::cast_to<CollisionShape3D>(static_body->get_child(0));
  239. static_body->set_owner(get_owner());
  240. cshape->set_owner(get_owner());
  241. }
  242. }
  243. Node *MeshInstance3D::create_multiple_convex_collisions_node(const Ref<MeshConvexDecompositionSettings> &p_settings) {
  244. if (mesh.is_null()) {
  245. return nullptr;
  246. }
  247. Ref<MeshConvexDecompositionSettings> settings;
  248. if (p_settings.is_valid()) {
  249. settings = p_settings;
  250. } else {
  251. settings.instantiate();
  252. }
  253. Vector<Ref<Shape3D>> shapes = mesh->convex_decompose(settings);
  254. if (!shapes.size()) {
  255. return nullptr;
  256. }
  257. StaticBody3D *static_body = memnew(StaticBody3D);
  258. for (int i = 0; i < shapes.size(); i++) {
  259. CollisionShape3D *cshape = memnew(CollisionShape3D);
  260. cshape->set_shape(shapes[i]);
  261. static_body->add_child(cshape, true);
  262. }
  263. return static_body;
  264. }
  265. void MeshInstance3D::create_multiple_convex_collisions(const Ref<MeshConvexDecompositionSettings> &p_settings) {
  266. StaticBody3D *static_body = Object::cast_to<StaticBody3D>(create_multiple_convex_collisions_node(p_settings));
  267. ERR_FAIL_COND(!static_body);
  268. static_body->set_name(String(get_name()) + "_col");
  269. add_child(static_body, true);
  270. if (get_owner()) {
  271. static_body->set_owner(get_owner());
  272. int count = static_body->get_child_count();
  273. for (int i = 0; i < count; i++) {
  274. CollisionShape3D *cshape = Object::cast_to<CollisionShape3D>(static_body->get_child(i));
  275. cshape->set_owner(get_owner());
  276. }
  277. }
  278. }
  279. void MeshInstance3D::_notification(int p_what) {
  280. switch (p_what) {
  281. case NOTIFICATION_ENTER_TREE: {
  282. _resolve_skeleton_path();
  283. } break;
  284. case NOTIFICATION_TRANSLATION_CHANGED: {
  285. if (mesh.is_valid()) {
  286. mesh->notification(NOTIFICATION_TRANSLATION_CHANGED);
  287. }
  288. } break;
  289. }
  290. }
  291. int MeshInstance3D::get_surface_override_material_count() const {
  292. return surface_override_materials.size();
  293. }
  294. void MeshInstance3D::set_surface_override_material(int p_surface, const Ref<Material> &p_material) {
  295. ERR_FAIL_INDEX(p_surface, surface_override_materials.size());
  296. surface_override_materials.write[p_surface] = p_material;
  297. if (surface_override_materials[p_surface].is_valid()) {
  298. RS::get_singleton()->instance_set_surface_override_material(get_instance(), p_surface, surface_override_materials[p_surface]->get_rid());
  299. } else {
  300. RS::get_singleton()->instance_set_surface_override_material(get_instance(), p_surface, RID());
  301. }
  302. }
  303. Ref<Material> MeshInstance3D::get_surface_override_material(int p_surface) const {
  304. ERR_FAIL_INDEX_V(p_surface, surface_override_materials.size(), Ref<Material>());
  305. return surface_override_materials[p_surface];
  306. }
  307. Ref<Material> MeshInstance3D::get_active_material(int p_surface) const {
  308. Ref<Material> mat_override = get_material_override();
  309. if (mat_override.is_valid()) {
  310. return mat_override;
  311. }
  312. Ref<Material> surface_material = get_surface_override_material(p_surface);
  313. if (surface_material.is_valid()) {
  314. return surface_material;
  315. }
  316. Ref<Mesh> m = get_mesh();
  317. if (m.is_valid()) {
  318. return m->surface_get_material(p_surface);
  319. }
  320. return Ref<Material>();
  321. }
  322. void MeshInstance3D::_mesh_changed() {
  323. ERR_FAIL_COND(mesh.is_null());
  324. surface_override_materials.resize(mesh->get_surface_count());
  325. uint32_t initialize_bs_from = blend_shape_tracks.size();
  326. blend_shape_tracks.resize(mesh->get_blend_shape_count());
  327. for (uint32_t i = 0; i < blend_shape_tracks.size(); i++) {
  328. blend_shape_properties["blend_shapes/" + String(mesh->get_blend_shape_name(i))] = i;
  329. if (i < initialize_bs_from) {
  330. set_blend_shape_value(i, blend_shape_tracks[i]);
  331. } else {
  332. set_blend_shape_value(i, 0);
  333. }
  334. }
  335. int surface_count = mesh->get_surface_count();
  336. for (int surface_index = 0; surface_index < surface_count; ++surface_index) {
  337. if (surface_override_materials[surface_index].is_valid()) {
  338. RS::get_singleton()->instance_set_surface_override_material(get_instance(), surface_index, surface_override_materials[surface_index]->get_rid());
  339. }
  340. }
  341. update_gizmos();
  342. }
  343. MeshInstance3D *MeshInstance3D::create_debug_tangents_node() {
  344. Vector<Vector3> lines;
  345. Vector<Color> colors;
  346. Ref<Mesh> m = get_mesh();
  347. if (!m.is_valid()) {
  348. return nullptr;
  349. }
  350. for (int i = 0; i < m->get_surface_count(); i++) {
  351. Array arrays = m->surface_get_arrays(i);
  352. ERR_CONTINUE(arrays.size() != Mesh::ARRAY_MAX);
  353. Vector<Vector3> verts = arrays[Mesh::ARRAY_VERTEX];
  354. Vector<Vector3> norms = arrays[Mesh::ARRAY_NORMAL];
  355. if (norms.size() == 0) {
  356. continue;
  357. }
  358. Vector<float> tangents = arrays[Mesh::ARRAY_TANGENT];
  359. if (tangents.size() == 0) {
  360. continue;
  361. }
  362. for (int j = 0; j < verts.size(); j++) {
  363. Vector3 v = verts[j];
  364. Vector3 n = norms[j];
  365. Vector3 t = Vector3(tangents[j * 4 + 0], tangents[j * 4 + 1], tangents[j * 4 + 2]);
  366. Vector3 b = (n.cross(t)).normalized() * tangents[j * 4 + 3];
  367. lines.push_back(v); //normal
  368. colors.push_back(Color(0, 0, 1)); //color
  369. lines.push_back(v + n * 0.04); //normal
  370. colors.push_back(Color(0, 0, 1)); //color
  371. lines.push_back(v); //tangent
  372. colors.push_back(Color(1, 0, 0)); //color
  373. lines.push_back(v + t * 0.04); //tangent
  374. colors.push_back(Color(1, 0, 0)); //color
  375. lines.push_back(v); //binormal
  376. colors.push_back(Color(0, 1, 0)); //color
  377. lines.push_back(v + b * 0.04); //binormal
  378. colors.push_back(Color(0, 1, 0)); //color
  379. }
  380. }
  381. if (lines.size()) {
  382. Ref<StandardMaterial3D> sm;
  383. sm.instantiate();
  384. sm->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  385. sm->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
  386. sm->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, 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. }