mesh_instance.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*************************************************************************/
  2. /* mesh_instance.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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/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. update_gizmo();
  112. _change_notify();
  113. }
  114. Ref<Mesh> MeshInstance::get_mesh() const {
  115. return mesh;
  116. }
  117. void MeshInstance::_resolve_skeleton_path() {
  118. if (skeleton_path.is_empty())
  119. return;
  120. Skeleton *skeleton = Object::cast_to<Skeleton>(get_node(skeleton_path));
  121. if (skeleton)
  122. VisualServer::get_singleton()->instance_attach_skeleton(get_instance(), skeleton->get_skeleton());
  123. }
  124. void MeshInstance::set_skeleton_path(const NodePath &p_skeleton) {
  125. skeleton_path = p_skeleton;
  126. if (!is_inside_tree())
  127. return;
  128. _resolve_skeleton_path();
  129. }
  130. NodePath MeshInstance::get_skeleton_path() {
  131. return skeleton_path;
  132. }
  133. AABB MeshInstance::get_aabb() const {
  134. if (!mesh.is_null())
  135. return mesh->get_aabb();
  136. return AABB();
  137. }
  138. PoolVector<Face3> MeshInstance::get_faces(uint32_t p_usage_flags) const {
  139. if (!(p_usage_flags & (FACES_SOLID | FACES_ENCLOSING)))
  140. return PoolVector<Face3>();
  141. if (mesh.is_null())
  142. return PoolVector<Face3>();
  143. return mesh->get_faces();
  144. }
  145. Node *MeshInstance::create_trimesh_collision_node() {
  146. if (mesh.is_null())
  147. return NULL;
  148. Ref<Shape> shape = mesh->create_trimesh_shape();
  149. if (shape.is_null())
  150. return NULL;
  151. StaticBody *static_body = memnew(StaticBody);
  152. CollisionShape *cshape = memnew(CollisionShape);
  153. cshape->set_shape(shape);
  154. static_body->add_child(cshape);
  155. return static_body;
  156. }
  157. void MeshInstance::create_trimesh_collision() {
  158. StaticBody *static_body = Object::cast_to<StaticBody>(create_trimesh_collision_node());
  159. ERR_FAIL_COND(!static_body);
  160. static_body->set_name(String(get_name()) + "_col");
  161. add_child(static_body);
  162. if (get_owner()) {
  163. CollisionShape *cshape = Object::cast_to<CollisionShape>(static_body->get_child(0));
  164. static_body->set_owner(get_owner());
  165. cshape->set_owner(get_owner());
  166. }
  167. }
  168. Node *MeshInstance::create_convex_collision_node() {
  169. if (mesh.is_null())
  170. return NULL;
  171. Ref<Shape> shape = mesh->create_convex_shape();
  172. if (shape.is_null())
  173. return NULL;
  174. StaticBody *static_body = memnew(StaticBody);
  175. CollisionShape *cshape = memnew(CollisionShape);
  176. cshape->set_shape(shape);
  177. static_body->add_child(cshape);
  178. return static_body;
  179. }
  180. void MeshInstance::create_convex_collision() {
  181. StaticBody *static_body = Object::cast_to<StaticBody>(create_convex_collision_node());
  182. ERR_FAIL_COND(!static_body);
  183. static_body->set_name(String(get_name()) + "_col");
  184. add_child(static_body);
  185. if (get_owner()) {
  186. CollisionShape *cshape = Object::cast_to<CollisionShape>(static_body->get_child(0));
  187. static_body->set_owner(get_owner());
  188. cshape->set_owner(get_owner());
  189. }
  190. }
  191. void MeshInstance::_notification(int p_what) {
  192. if (p_what == NOTIFICATION_ENTER_TREE) {
  193. _resolve_skeleton_path();
  194. }
  195. }
  196. int MeshInstance::get_surface_material_count() const {
  197. return materials.size();
  198. }
  199. void MeshInstance::set_surface_material(int p_surface, const Ref<Material> &p_material) {
  200. ERR_FAIL_INDEX(p_surface, materials.size());
  201. materials.write[p_surface] = p_material;
  202. if (materials[p_surface].is_valid())
  203. VS::get_singleton()->instance_set_surface_material(get_instance(), p_surface, materials[p_surface]->get_rid());
  204. else
  205. VS::get_singleton()->instance_set_surface_material(get_instance(), p_surface, RID());
  206. }
  207. Ref<Material> MeshInstance::get_surface_material(int p_surface) const {
  208. ERR_FAIL_INDEX_V(p_surface, materials.size(), Ref<Material>());
  209. return materials[p_surface];
  210. }
  211. void MeshInstance::_mesh_changed() {
  212. materials.resize(mesh->get_surface_count());
  213. }
  214. void MeshInstance::create_debug_tangents() {
  215. Vector<Vector3> lines;
  216. Vector<Color> colors;
  217. Ref<Mesh> mesh = get_mesh();
  218. if (!mesh.is_valid())
  219. return;
  220. for (int i = 0; i < mesh->get_surface_count(); i++) {
  221. Array arrays = mesh->surface_get_arrays(i);
  222. Vector<Vector3> verts = arrays[Mesh::ARRAY_VERTEX];
  223. Vector<Vector3> norms = arrays[Mesh::ARRAY_NORMAL];
  224. if (norms.size() == 0)
  225. continue;
  226. Vector<float> tangents = arrays[Mesh::ARRAY_TANGENT];
  227. if (tangents.size() == 0)
  228. continue;
  229. for (int j = 0; j < verts.size(); j++) {
  230. Vector3 v = verts[j];
  231. Vector3 n = norms[j];
  232. Vector3 t = Vector3(tangents[j * 4 + 0], tangents[j * 4 + 1], tangents[j * 4 + 2]);
  233. Vector3 b = (n.cross(t)).normalized() * tangents[j * 4 + 3];
  234. lines.push_back(v); //normal
  235. colors.push_back(Color(0, 0, 1)); //color
  236. lines.push_back(v + n * 0.04); //normal
  237. colors.push_back(Color(0, 0, 1)); //color
  238. lines.push_back(v); //tangent
  239. colors.push_back(Color(1, 0, 0)); //color
  240. lines.push_back(v + t * 0.04); //tangent
  241. colors.push_back(Color(1, 0, 0)); //color
  242. lines.push_back(v); //binormal
  243. colors.push_back(Color(0, 1, 0)); //color
  244. lines.push_back(v + b * 0.04); //binormal
  245. colors.push_back(Color(0, 1, 0)); //color
  246. }
  247. }
  248. if (lines.size()) {
  249. Ref<SpatialMaterial> sm;
  250. sm.instance();
  251. sm->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  252. sm->set_flag(SpatialMaterial::FLAG_SRGB_VERTEX_COLOR, true);
  253. sm->set_flag(SpatialMaterial::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  254. Ref<ArrayMesh> am;
  255. am.instance();
  256. Array a;
  257. a.resize(Mesh::ARRAY_MAX);
  258. a[Mesh::ARRAY_VERTEX] = lines;
  259. a[Mesh::ARRAY_COLOR] = colors;
  260. am->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, a);
  261. am->surface_set_material(0, sm);
  262. MeshInstance *mi = memnew(MeshInstance);
  263. mi->set_mesh(am);
  264. mi->set_name("DebugTangents");
  265. add_child(mi);
  266. #ifdef TOOLS_ENABLED
  267. if (this == get_tree()->get_edited_scene_root())
  268. mi->set_owner(this);
  269. else
  270. mi->set_owner(get_owner());
  271. #endif
  272. }
  273. }
  274. void MeshInstance::_bind_methods() {
  275. ClassDB::bind_method(D_METHOD("set_mesh", "mesh"), &MeshInstance::set_mesh);
  276. ClassDB::bind_method(D_METHOD("get_mesh"), &MeshInstance::get_mesh);
  277. ClassDB::bind_method(D_METHOD("set_skeleton_path", "skeleton_path"), &MeshInstance::set_skeleton_path);
  278. ClassDB::bind_method(D_METHOD("get_skeleton_path"), &MeshInstance::get_skeleton_path);
  279. ClassDB::bind_method(D_METHOD("get_surface_material_count"), &MeshInstance::get_surface_material_count);
  280. ClassDB::bind_method(D_METHOD("set_surface_material", "surface", "material"), &MeshInstance::set_surface_material);
  281. ClassDB::bind_method(D_METHOD("get_surface_material", "surface"), &MeshInstance::get_surface_material);
  282. ClassDB::bind_method(D_METHOD("create_trimesh_collision"), &MeshInstance::create_trimesh_collision);
  283. ClassDB::set_method_flags("MeshInstance", "create_trimesh_collision", METHOD_FLAGS_DEFAULT);
  284. ClassDB::bind_method(D_METHOD("create_convex_collision"), &MeshInstance::create_convex_collision);
  285. ClassDB::set_method_flags("MeshInstance", "create_convex_collision", METHOD_FLAGS_DEFAULT);
  286. ClassDB::bind_method(D_METHOD("_mesh_changed"), &MeshInstance::_mesh_changed);
  287. ClassDB::bind_method(D_METHOD("create_debug_tangents"), &MeshInstance::create_debug_tangents);
  288. ClassDB::set_method_flags("MeshInstance", "create_debug_tangents", METHOD_FLAGS_DEFAULT | METHOD_FLAG_EDITOR);
  289. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh", PROPERTY_HINT_RESOURCE_TYPE, "Mesh"), "set_mesh", "get_mesh");
  290. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "skeleton", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Skeleton"), "set_skeleton_path", "get_skeleton_path");
  291. }
  292. MeshInstance::MeshInstance() {
  293. skeleton_path = NodePath("..");
  294. }
  295. MeshInstance::~MeshInstance() {
  296. }