mesh_instance.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*************************************************************************/
  2. /* mesh_instance.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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.h"
  31. #include "collision_shape.h"
  32. #include "core_string_names.h"
  33. #include "physics_body.h"
  34. #include "scene/resources/material.h"
  35. #include "scene/scene_string_names.h"
  36. #include "skeleton.h"
  37. bool MeshInstance::_set(const StringName &p_name, const Variant &p_value) {
  38. //this is not _too_ bad performance wise, really. it only arrives here if the property was not set anywhere else.
  39. //add to it that it's probably found on first call to _set anyway.
  40. if (!get_instance().is_valid())
  41. return false;
  42. Map<StringName, BlendShapeTrack>::Element *E = blend_shape_tracks.find(p_name);
  43. if (E) {
  44. E->get().value = p_value;
  45. VisualServer::get_singleton()->instance_set_blend_shape_weight(get_instance(), E->get().idx, E->get().value);
  46. return true;
  47. }
  48. if (p_name.operator String().begins_with("material/")) {
  49. int idx = p_name.operator String().get_slicec('/', 1).to_int();
  50. if (idx >= materials.size() || idx < 0)
  51. return false;
  52. set_surface_material(idx, p_value);
  53. return true;
  54. }
  55. return false;
  56. }
  57. bool MeshInstance::_get(const StringName &p_name, Variant &r_ret) const {
  58. if (!get_instance().is_valid())
  59. return false;
  60. const Map<StringName, BlendShapeTrack>::Element *E = blend_shape_tracks.find(p_name);
  61. if (E) {
  62. r_ret = E->get().value;
  63. return true;
  64. }
  65. if (p_name.operator String().begins_with("material/")) {
  66. int idx = p_name.operator String().get_slicec('/', 1).to_int();
  67. if (idx >= materials.size() || idx < 0)
  68. return false;
  69. r_ret = materials[idx];
  70. return true;
  71. }
  72. return false;
  73. }
  74. void MeshInstance::_get_property_list(List<PropertyInfo> *p_list) const {
  75. List<String> ls;
  76. for (const Map<StringName, BlendShapeTrack>::Element *E = blend_shape_tracks.front(); E; E = E->next()) {
  77. ls.push_back(E->key());
  78. }
  79. ls.sort();
  80. for (List<String>::Element *E = ls.front(); E; E = E->next()) {
  81. p_list->push_back(PropertyInfo(Variant::REAL, E->get(), PROPERTY_HINT_RANGE, "0,1,0.01"));
  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, "material/" + itos(i), PROPERTY_HINT_RESOURCE_TYPE, "ShaderMaterial,SpatialMaterial"));
  86. }
  87. }
  88. }
  89. void MeshInstance::set_mesh(const Ref<Mesh> &p_mesh) {
  90. if (mesh == p_mesh)
  91. return;
  92. if (mesh.is_valid()) {
  93. mesh->disconnect(CoreStringNames::get_singleton()->changed, this, SceneStringNames::get_singleton()->_mesh_changed);
  94. materials.clear();
  95. }
  96. mesh = p_mesh;
  97. blend_shape_tracks.clear();
  98. if (mesh.is_valid()) {
  99. for (int i = 0; i < mesh->get_blend_shape_count(); i++) {
  100. BlendShapeTrack mt;
  101. mt.idx = i;
  102. mt.value = 0;
  103. blend_shape_tracks["blend_shapes/" + String(mesh->get_blend_shape_name(i))] = mt;
  104. }
  105. mesh->connect(CoreStringNames::get_singleton()->changed, this, SceneStringNames::get_singleton()->_mesh_changed);
  106. materials.resize(mesh->get_surface_count());
  107. set_base(mesh->get_rid());
  108. } else {
  109. set_base(RID());
  110. }
  111. _change_notify();
  112. }
  113. Ref<Mesh> MeshInstance::get_mesh() const {
  114. return mesh;
  115. }
  116. void MeshInstance::_resolve_skeleton_path() {
  117. if (skeleton_path.is_empty())
  118. return;
  119. Skeleton *skeleton = Object::cast_to<Skeleton>(get_node(skeleton_path));
  120. if (skeleton)
  121. VisualServer::get_singleton()->instance_attach_skeleton(get_instance(), skeleton->get_skeleton());
  122. }
  123. void MeshInstance::set_skeleton_path(const NodePath &p_skeleton) {
  124. skeleton_path = p_skeleton;
  125. if (!is_inside_tree())
  126. return;
  127. _resolve_skeleton_path();
  128. }
  129. NodePath MeshInstance::get_skeleton_path() {
  130. return skeleton_path;
  131. }
  132. Rect3 MeshInstance::get_aabb() const {
  133. if (!mesh.is_null())
  134. return mesh->get_aabb();
  135. return Rect3();
  136. }
  137. PoolVector<Face3> MeshInstance::get_faces(uint32_t p_usage_flags) const {
  138. if (!(p_usage_flags & (FACES_SOLID | FACES_ENCLOSING)))
  139. return PoolVector<Face3>();
  140. if (mesh.is_null())
  141. return PoolVector<Face3>();
  142. return mesh->get_faces();
  143. }
  144. Node *MeshInstance::create_trimesh_collision_node() {
  145. if (mesh.is_null())
  146. return NULL;
  147. Ref<Shape> shape = mesh->create_trimesh_shape();
  148. if (shape.is_null())
  149. return NULL;
  150. StaticBody *static_body = memnew(StaticBody);
  151. CollisionShape *cshape = memnew(CollisionShape);
  152. cshape->set_shape(shape);
  153. static_body->add_child(cshape);
  154. return static_body;
  155. }
  156. void MeshInstance::create_trimesh_collision() {
  157. StaticBody *static_body = Object::cast_to<StaticBody>(create_trimesh_collision_node());
  158. ERR_FAIL_COND(!static_body);
  159. static_body->set_name(String(get_name()) + "_col");
  160. add_child(static_body);
  161. if (get_owner()) {
  162. CollisionShape *cshape = Object::cast_to<CollisionShape>(static_body->get_child(0));
  163. static_body->set_owner(get_owner());
  164. cshape->set_owner(get_owner());
  165. }
  166. }
  167. Node *MeshInstance::create_convex_collision_node() {
  168. if (mesh.is_null())
  169. return NULL;
  170. Ref<Shape> shape = mesh->create_convex_shape();
  171. if (shape.is_null())
  172. return NULL;
  173. StaticBody *static_body = memnew(StaticBody);
  174. CollisionShape *cshape = memnew(CollisionShape);
  175. cshape->set_shape(shape);
  176. static_body->add_child(cshape);
  177. return static_body;
  178. }
  179. void MeshInstance::create_convex_collision() {
  180. StaticBody *static_body = Object::cast_to<StaticBody>(create_convex_collision_node());
  181. ERR_FAIL_COND(!static_body);
  182. static_body->set_name(String(get_name()) + "_col");
  183. add_child(static_body);
  184. if (get_owner()) {
  185. CollisionShape *cshape = Object::cast_to<CollisionShape>(static_body->get_child(0));
  186. static_body->set_owner(get_owner());
  187. cshape->set_owner(get_owner());
  188. }
  189. }
  190. void MeshInstance::_notification(int p_what) {
  191. if (p_what == NOTIFICATION_ENTER_TREE) {
  192. _resolve_skeleton_path();
  193. }
  194. }
  195. void MeshInstance::set_surface_material(int p_surface, const Ref<Material> &p_material) {
  196. ERR_FAIL_INDEX(p_surface, materials.size());
  197. materials[p_surface] = p_material;
  198. if (materials[p_surface].is_valid())
  199. VS::get_singleton()->instance_set_surface_material(get_instance(), p_surface, materials[p_surface]->get_rid());
  200. else
  201. VS::get_singleton()->instance_set_surface_material(get_instance(), p_surface, RID());
  202. }
  203. Ref<Material> MeshInstance::get_surface_material(int p_surface) const {
  204. ERR_FAIL_INDEX_V(p_surface, materials.size(), Ref<Material>());
  205. return materials[p_surface];
  206. }
  207. void MeshInstance::_mesh_changed() {
  208. materials.resize(mesh->get_surface_count());
  209. }
  210. void MeshInstance::create_debug_tangents() {
  211. Vector<Vector3> lines;
  212. Vector<Color> colors;
  213. Ref<Mesh> mesh = get_mesh();
  214. if (!mesh.is_valid())
  215. return;
  216. for (int i = 0; i < mesh->get_surface_count(); i++) {
  217. Array arrays = mesh->surface_get_arrays(i);
  218. Vector<Vector3> verts = arrays[Mesh::ARRAY_VERTEX];
  219. Vector<Vector3> norms = arrays[Mesh::ARRAY_NORMAL];
  220. if (norms.size() == 0)
  221. continue;
  222. Vector<float> tangents = arrays[Mesh::ARRAY_TANGENT];
  223. if (tangents.size() == 0)
  224. continue;
  225. for (int j = 0; j < verts.size(); j++) {
  226. Vector3 v = verts[j];
  227. Vector3 n = norms[j];
  228. Vector3 t = Vector3(tangents[j * 4 + 0], tangents[j * 4 + 1], tangents[j * 4 + 2]);
  229. Vector3 b = (n.cross(t)).normalized() * tangents[j * 4 + 3];
  230. lines.push_back(v); //normal
  231. colors.push_back(Color(0, 0, 1)); //color
  232. lines.push_back(v + n * 0.04); //normal
  233. colors.push_back(Color(0, 0, 1)); //color
  234. lines.push_back(v); //tangent
  235. colors.push_back(Color(1, 0, 0)); //color
  236. lines.push_back(v + t * 0.04); //tangent
  237. colors.push_back(Color(1, 0, 0)); //color
  238. lines.push_back(v); //binormal
  239. colors.push_back(Color(0, 1, 0)); //color
  240. lines.push_back(v + b * 0.04); //binormal
  241. colors.push_back(Color(0, 1, 0)); //color
  242. }
  243. }
  244. if (lines.size()) {
  245. Ref<SpatialMaterial> sm;
  246. sm.instance();
  247. sm->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  248. sm->set_flag(SpatialMaterial::FLAG_SRGB_VERTEX_COLOR, true);
  249. sm->set_flag(SpatialMaterial::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  250. Ref<ArrayMesh> am;
  251. am.instance();
  252. Array a;
  253. a.resize(Mesh::ARRAY_MAX);
  254. a[Mesh::ARRAY_VERTEX] = lines;
  255. a[Mesh::ARRAY_COLOR] = colors;
  256. am->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, a);
  257. am->surface_set_material(0, sm);
  258. MeshInstance *mi = memnew(MeshInstance);
  259. mi->set_mesh(am);
  260. mi->set_name("DebugTangents");
  261. add_child(mi);
  262. #ifdef TOOLS_ENABLED
  263. if (this == get_tree()->get_edited_scene_root())
  264. mi->set_owner(this);
  265. else
  266. mi->set_owner(get_owner());
  267. #endif
  268. }
  269. }
  270. void MeshInstance::_bind_methods() {
  271. ClassDB::bind_method(D_METHOD("set_mesh", "mesh"), &MeshInstance::set_mesh);
  272. ClassDB::bind_method(D_METHOD("get_mesh"), &MeshInstance::get_mesh);
  273. ClassDB::bind_method(D_METHOD("set_skeleton_path", "skeleton_path"), &MeshInstance::set_skeleton_path);
  274. ClassDB::bind_method(D_METHOD("get_skeleton_path"), &MeshInstance::get_skeleton_path);
  275. ClassDB::bind_method(D_METHOD("set_surface_material", "surface", "material"), &MeshInstance::set_surface_material);
  276. ClassDB::bind_method(D_METHOD("get_surface_material", "surface"), &MeshInstance::get_surface_material);
  277. ClassDB::bind_method(D_METHOD("create_trimesh_collision"), &MeshInstance::create_trimesh_collision);
  278. ClassDB::set_method_flags("MeshInstance", "create_trimesh_collision", METHOD_FLAGS_DEFAULT);
  279. ClassDB::bind_method(D_METHOD("create_convex_collision"), &MeshInstance::create_convex_collision);
  280. ClassDB::set_method_flags("MeshInstance", "create_convex_collision", METHOD_FLAGS_DEFAULT);
  281. ClassDB::bind_method(D_METHOD("_mesh_changed"), &MeshInstance::_mesh_changed);
  282. ClassDB::bind_method(D_METHOD("create_debug_tangents"), &MeshInstance::create_debug_tangents);
  283. ClassDB::set_method_flags("MeshInstance", "create_debug_tangents", METHOD_FLAGS_DEFAULT | METHOD_FLAG_EDITOR);
  284. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh", PROPERTY_HINT_RESOURCE_TYPE, "Mesh"), "set_mesh", "get_mesh");
  285. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "skeleton"), "set_skeleton_path", "get_skeleton_path");
  286. }
  287. MeshInstance::MeshInstance() {
  288. skeleton_path = NodePath("..");
  289. }
  290. MeshInstance::~MeshInstance() {
  291. }