mesh_instance_3d.cpp 17 KB

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