mesh_instance.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*************************************************************************/
  2. /* mesh_instance.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "mesh_instance.h"
  30. #include "skeleton.h"
  31. #include "physics_body.h"
  32. #include "body_shape.h"
  33. bool MeshInstance::_set(const StringName& p_name, const Variant& p_value) {
  34. //this is not _too_ bad performance wise, really. it only arrives here if the property was not set anywhere else.
  35. //add to it that it's probably found on first call to _set anyway.
  36. if (!get_instance().is_valid())
  37. return false;
  38. Map<StringName,MorphTrack>::Element *E = morph_tracks.find(p_name);
  39. if (!E)
  40. return false;
  41. E->get().value=p_value;
  42. VisualServer::get_singleton()->instance_set_morph_target_weight(get_instance(),E->get().idx,E->get().value);
  43. return true;
  44. }
  45. bool MeshInstance::_get(const StringName& p_name,Variant &r_ret) const {
  46. if (!get_instance().is_valid())
  47. return false;
  48. const Map<StringName,MorphTrack>::Element *E = morph_tracks.find(p_name);
  49. if (!E)
  50. return false;
  51. r_ret = E->get().value;
  52. return true;
  53. }
  54. void MeshInstance::_get_property_list( List<PropertyInfo> *p_list) const {
  55. List<String> ls;
  56. for(const Map<StringName,MorphTrack>::Element *E=morph_tracks.front();E;E=E->next()) {
  57. ls.push_back(E->key());
  58. }
  59. ls.sort();;
  60. for(List<String>::Element *E=ls.front();E;E=E->next()) {
  61. p_list->push_back( PropertyInfo(Variant::REAL,E->get(),PROPERTY_HINT_RANGE,"0,1,0.01"));
  62. }
  63. }
  64. void MeshInstance::set_mesh(const Ref<Mesh>& p_mesh) {
  65. mesh=p_mesh;
  66. morph_tracks.clear();
  67. if (mesh.is_valid()) {
  68. for(int i=0;i<mesh->get_morph_target_count();i++) {
  69. MorphTrack mt;
  70. mt.idx=i;
  71. mt.value=0;
  72. morph_tracks["morph/"+String(mesh->get_morph_target_name(i))]=mt;
  73. }
  74. set_base(mesh->get_rid());
  75. } else {
  76. set_base(RID());
  77. }
  78. _change_notify("mesh");
  79. }
  80. Ref<Mesh> MeshInstance::get_mesh() const {
  81. return mesh;
  82. }
  83. void MeshInstance::_resolve_skeleton_path(){
  84. if (skeleton_path.is_empty())
  85. return;
  86. Skeleton *skeleton=get_node(skeleton_path)?get_node(skeleton_path)->cast_to<Skeleton>():NULL;
  87. if (skeleton)
  88. VisualServer::get_singleton()->instance_attach_skeleton( get_instance(), skeleton->get_skeleton() );
  89. }
  90. void MeshInstance::set_skeleton_path(const NodePath &p_skeleton) {
  91. skeleton_path = p_skeleton;
  92. if (!is_inside_tree())
  93. return;
  94. _resolve_skeleton_path();
  95. }
  96. NodePath MeshInstance::get_skeleton_path() {
  97. return skeleton_path;
  98. }
  99. AABB MeshInstance::get_aabb() const {
  100. if (!mesh.is_null())
  101. return mesh->get_aabb();
  102. return AABB();
  103. }
  104. DVector<Face3> MeshInstance::get_faces(uint32_t p_usage_flags) const {
  105. if (!(p_usage_flags&(FACES_SOLID|FACES_ENCLOSING)))
  106. return DVector<Face3>();
  107. if (mesh.is_null())
  108. return DVector<Face3>();
  109. return mesh->get_faces();
  110. }
  111. Node* MeshInstance::create_trimesh_collision_node() {
  112. if (mesh.is_null())
  113. return NULL;
  114. Ref<Shape> shape = mesh->create_trimesh_shape();
  115. if (shape.is_null())
  116. return NULL;
  117. StaticBody * static_body = memnew( StaticBody );
  118. static_body->add_shape( shape );
  119. return static_body;
  120. }
  121. void MeshInstance::create_trimesh_collision() {
  122. StaticBody* static_body = create_trimesh_collision_node()->cast_to<StaticBody>();
  123. ERR_FAIL_COND(!static_body);
  124. static_body->set_name( String(get_name()) + "_col" );
  125. add_child(static_body);
  126. if (get_owner())
  127. static_body->set_owner( get_owner() );
  128. CollisionShape *cshape = memnew( CollisionShape );
  129. cshape->set_shape(static_body->get_shape(0));
  130. static_body->add_child(cshape);
  131. if (get_owner())
  132. cshape->set_owner( get_owner() );
  133. }
  134. Node* MeshInstance::create_convex_collision_node() {
  135. if (mesh.is_null())
  136. return NULL;
  137. Ref<Shape> shape = mesh->create_convex_shape();
  138. if (shape.is_null())
  139. return NULL;
  140. StaticBody * static_body = memnew( StaticBody );
  141. static_body->add_shape( shape );
  142. return static_body;
  143. }
  144. void MeshInstance::create_convex_collision() {
  145. StaticBody* static_body = create_convex_collision_node()->cast_to<StaticBody>();
  146. ERR_FAIL_COND(!static_body);
  147. static_body->set_name( String(get_name()) + "_col" );
  148. add_child(static_body);
  149. if (get_owner())
  150. static_body->set_owner( get_owner() );
  151. CollisionShape *cshape = memnew( CollisionShape );
  152. cshape->set_shape(static_body->get_shape(0));
  153. static_body->add_child(cshape);
  154. if (get_owner())
  155. cshape->set_owner( get_owner() );
  156. }
  157. void MeshInstance::_notification(int p_what) {
  158. if (p_what==NOTIFICATION_ENTER_TREE) {
  159. _resolve_skeleton_path();
  160. }
  161. }
  162. void MeshInstance::_bind_methods() {
  163. ObjectTypeDB::bind_method(_MD("set_mesh","mesh:Mesh"),&MeshInstance::set_mesh);
  164. ObjectTypeDB::bind_method(_MD("get_mesh:Mesh"),&MeshInstance::get_mesh);
  165. ObjectTypeDB::bind_method(_MD("set_skeleton_path","skeleton_path:NodePath"),&MeshInstance::set_skeleton_path);
  166. ObjectTypeDB::bind_method(_MD("get_skeleton_path:NodePath"),&MeshInstance::get_skeleton_path);
  167. ObjectTypeDB::bind_method(_MD("get_aabb"),&MeshInstance::get_aabb);
  168. ObjectTypeDB::bind_method(_MD("create_trimesh_collision"),&MeshInstance::create_trimesh_collision);
  169. ObjectTypeDB::set_method_flags("MeshInstance","create_trimesh_collision",METHOD_FLAGS_DEFAULT);
  170. ObjectTypeDB::bind_method(_MD("create_convex_collision"),&MeshInstance::create_convex_collision);
  171. ObjectTypeDB::set_method_flags("MeshInstance","create_convex_collision",METHOD_FLAGS_DEFAULT);
  172. ADD_PROPERTY( PropertyInfo( Variant::OBJECT, "mesh/mesh", PROPERTY_HINT_RESOURCE_TYPE, "Mesh" ), _SCS("set_mesh"), _SCS("get_mesh"));
  173. ADD_PROPERTY( PropertyInfo (Variant::NODE_PATH, "mesh/skeleton"), _SCS("set_skeleton_path"), _SCS("get_skeleton_path"));
  174. }
  175. MeshInstance::MeshInstance()
  176. {
  177. skeleton_path=NodePath("..");
  178. }
  179. MeshInstance::~MeshInstance() {
  180. }