mesh_instance.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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-2017 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. #include "scene/scene_string_names.h"
  34. #include "core_string_names.h"
  35. bool MeshInstance::_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. Map<StringName,BlendShapeTrack>::Element *E = blend_shape_tracks.find(p_name);
  41. if (E) {
  42. E->get().value=p_value;
  43. VisualServer::get_singleton()->instance_set_blend_shape_weight(get_instance(),E->get().idx,E->get().value);
  44. return true;
  45. }
  46. if (p_name.operator String().begins_with("material/")) {
  47. int idx = p_name.operator String().get_slicec('/',1).to_int();
  48. if (idx>=materials.size() || idx<0)
  49. return false;
  50. set_surface_material(idx,p_value);
  51. return true;
  52. }
  53. return false;
  54. }
  55. bool MeshInstance::_get(const StringName& p_name,Variant &r_ret) const {
  56. if (!get_instance().is_valid())
  57. return false;
  58. const Map<StringName,BlendShapeTrack>::Element *E = blend_shape_tracks.find(p_name);
  59. if (E) {
  60. r_ret = E->get().value;
  61. return true;
  62. }
  63. if (p_name.operator String().begins_with("material/")) {
  64. int idx = p_name.operator String().get_slicec('/',1).to_int();
  65. if (idx>=materials.size() || idx<0)
  66. return false;
  67. r_ret=materials[idx];
  68. return true;
  69. }
  70. return false;
  71. }
  72. void MeshInstance::_get_property_list( List<PropertyInfo> *p_list) const {
  73. List<String> ls;
  74. for(const Map<StringName,BlendShapeTrack>::Element *E=blend_shape_tracks.front();E;E=E->next()) {
  75. ls.push_back(E->key());
  76. }
  77. ls.sort();
  78. for(List<String>::Element *E=ls.front();E;E=E->next()) {
  79. p_list->push_back( PropertyInfo(Variant::REAL,E->get(),PROPERTY_HINT_RANGE,"0,1,0.01"));
  80. }
  81. if (mesh.is_valid()) {
  82. for(int i=0;i<mesh->get_surface_count();i++) {
  83. p_list->push_back( PropertyInfo(Variant::OBJECT, "material/"+itos(i), PROPERTY_HINT_RESOURCE_TYPE, "Material"));
  84. }
  85. }
  86. }
  87. void MeshInstance::set_mesh(const Ref<Mesh>& p_mesh) {
  88. if (mesh==p_mesh)
  89. return;
  90. if (mesh.is_valid()) {
  91. mesh->disconnect(CoreStringNames::get_singleton()->changed,this,SceneStringNames::get_singleton()->_mesh_changed);
  92. materials.clear();
  93. }
  94. mesh=p_mesh;
  95. blend_shape_tracks.clear();
  96. if (mesh.is_valid()) {
  97. for(int i=0;i<mesh->get_blend_shape_count();i++) {
  98. BlendShapeTrack mt;
  99. mt.idx=i;
  100. mt.value=0;
  101. blend_shape_tracks["blend_shapes/"+String(mesh->get_blend_shape_name(i))]=mt;
  102. }
  103. mesh->connect(CoreStringNames::get_singleton()->changed,this,SceneStringNames::get_singleton()->_mesh_changed);
  104. materials.resize(mesh->get_surface_count());
  105. set_base(mesh->get_rid());
  106. } else {
  107. set_base(RID());
  108. }
  109. _change_notify();
  110. }
  111. Ref<Mesh> MeshInstance::get_mesh() const {
  112. return mesh;
  113. }
  114. void MeshInstance::_resolve_skeleton_path(){
  115. if (skeleton_path.is_empty())
  116. return;
  117. Skeleton *skeleton=get_node(skeleton_path)?get_node(skeleton_path)->cast_to<Skeleton>():NULL;
  118. if (skeleton)
  119. VisualServer::get_singleton()->instance_attach_skeleton( get_instance(), skeleton->get_skeleton() );
  120. }
  121. void MeshInstance::set_skeleton_path(const NodePath &p_skeleton) {
  122. skeleton_path = p_skeleton;
  123. if (!is_inside_tree())
  124. return;
  125. _resolve_skeleton_path();
  126. }
  127. NodePath MeshInstance::get_skeleton_path() {
  128. return skeleton_path;
  129. }
  130. Rect3 MeshInstance::get_aabb() const {
  131. if (!mesh.is_null())
  132. return mesh->get_aabb();
  133. return Rect3();
  134. }
  135. PoolVector<Face3> MeshInstance::get_faces(uint32_t p_usage_flags) const {
  136. if (!(p_usage_flags&(FACES_SOLID|FACES_ENCLOSING)))
  137. return PoolVector<Face3>();
  138. if (mesh.is_null())
  139. return PoolVector<Face3>();
  140. return mesh->get_faces();
  141. }
  142. Node* MeshInstance::create_trimesh_collision_node() {
  143. if (mesh.is_null())
  144. return NULL;
  145. Ref<Shape> shape = mesh->create_trimesh_shape();
  146. if (shape.is_null())
  147. return NULL;
  148. StaticBody * static_body = memnew( StaticBody );
  149. static_body->add_shape( shape );
  150. return static_body;
  151. }
  152. void MeshInstance::create_trimesh_collision() {
  153. StaticBody* static_body = create_trimesh_collision_node()->cast_to<StaticBody>();
  154. ERR_FAIL_COND(!static_body);
  155. static_body->set_name( String(get_name()) + "_col" );
  156. add_child(static_body);
  157. if (get_owner())
  158. static_body->set_owner( get_owner() );
  159. CollisionShape *cshape = memnew( CollisionShape );
  160. cshape->set_shape(static_body->get_shape(0));
  161. static_body->add_child(cshape);
  162. if (get_owner())
  163. cshape->set_owner( get_owner() );
  164. }
  165. Node* MeshInstance::create_convex_collision_node() {
  166. if (mesh.is_null())
  167. return NULL;
  168. Ref<Shape> shape = mesh->create_convex_shape();
  169. if (shape.is_null())
  170. return NULL;
  171. StaticBody * static_body = memnew( StaticBody );
  172. static_body->add_shape( shape );
  173. return static_body;
  174. }
  175. void MeshInstance::create_convex_collision() {
  176. StaticBody* static_body = create_convex_collision_node()->cast_to<StaticBody>();
  177. ERR_FAIL_COND(!static_body);
  178. static_body->set_name( String(get_name()) + "_col" );
  179. add_child(static_body);
  180. if (get_owner())
  181. static_body->set_owner( get_owner() );
  182. CollisionShape *cshape = memnew( CollisionShape );
  183. cshape->set_shape(static_body->get_shape(0));
  184. static_body->add_child(cshape);
  185. if (get_owner())
  186. cshape->set_owner( get_owner() );
  187. }
  188. void MeshInstance::_notification(int p_what) {
  189. if (p_what==NOTIFICATION_ENTER_TREE) {
  190. _resolve_skeleton_path();
  191. }
  192. }
  193. void MeshInstance::set_surface_material(int p_surface,const Ref<Material>& p_material) {
  194. ERR_FAIL_INDEX(p_surface,materials.size());
  195. materials[p_surface]=p_material;
  196. if (materials[p_surface].is_valid())
  197. VS::get_singleton()->instance_set_surface_material(get_instance(),p_surface,materials[p_surface]->get_rid());
  198. else
  199. VS::get_singleton()->instance_set_surface_material(get_instance(),p_surface,RID());
  200. }
  201. Ref<Material> MeshInstance::get_surface_material(int p_surface) const {
  202. ERR_FAIL_INDEX_V(p_surface,materials.size(),Ref<Material>());
  203. return materials[p_surface];
  204. }
  205. void MeshInstance::_mesh_changed() {
  206. materials.resize( mesh->get_surface_count() );
  207. }
  208. void MeshInstance::_bind_methods() {
  209. ClassDB::bind_method(_MD("set_mesh","mesh:Mesh"),&MeshInstance::set_mesh);
  210. ClassDB::bind_method(_MD("get_mesh:Mesh"),&MeshInstance::get_mesh);
  211. ClassDB::bind_method(_MD("set_skeleton_path","skeleton_path:NodePath"),&MeshInstance::set_skeleton_path);
  212. ClassDB::bind_method(_MD("get_skeleton_path:NodePath"),&MeshInstance::get_skeleton_path);
  213. ClassDB::bind_method(_MD("create_trimesh_collision"),&MeshInstance::create_trimesh_collision);
  214. ClassDB::set_method_flags("MeshInstance","create_trimesh_collision",METHOD_FLAGS_DEFAULT);
  215. ClassDB::bind_method(_MD("create_convex_collision"),&MeshInstance::create_convex_collision);
  216. ClassDB::set_method_flags("MeshInstance","create_convex_collision",METHOD_FLAGS_DEFAULT);
  217. ClassDB::bind_method(_MD("_mesh_changed"),&MeshInstance::_mesh_changed);
  218. ADD_PROPERTY( PropertyInfo( Variant::OBJECT, "mesh", PROPERTY_HINT_RESOURCE_TYPE, "Mesh" ), "set_mesh", "get_mesh");
  219. ADD_PROPERTY( PropertyInfo (Variant::NODE_PATH, "skeleton"), "set_skeleton_path", "get_skeleton_path");
  220. }
  221. MeshInstance::MeshInstance()
  222. {
  223. skeleton_path=NodePath("..");
  224. }
  225. MeshInstance::~MeshInstance() {
  226. }