concave_polygon_shape_3d.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /**************************************************************************/
  2. /* concave_polygon_shape_3d.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 "concave_polygon_shape_3d.h"
  31. #include "scene/resources/mesh.h"
  32. #include "servers/physics_server_3d.h"
  33. Vector<Vector3> ConcavePolygonShape3D::get_debug_mesh_lines() const {
  34. HashSet<DrawEdge, DrawEdge> edges;
  35. int index_count = faces.size();
  36. ERR_FAIL_COND_V((index_count % 3) != 0, Vector<Vector3>());
  37. const Vector3 *r = faces.ptr();
  38. for (int i = 0; i < index_count; i += 3) {
  39. for (int j = 0; j < 3; j++) {
  40. DrawEdge de(r[i + j], r[i + ((j + 1) % 3)]);
  41. edges.insert(de);
  42. }
  43. }
  44. Vector<Vector3> points;
  45. points.resize(edges.size() * 2);
  46. int idx = 0;
  47. for (const DrawEdge &E : edges) {
  48. points.write[idx + 0] = E.a;
  49. points.write[idx + 1] = E.b;
  50. idx += 2;
  51. }
  52. return points;
  53. }
  54. Ref<ArrayMesh> ConcavePolygonShape3D::get_debug_arraymesh_faces(const Color &p_modulate) const {
  55. Vector<Color> colors;
  56. for (int i = 0; i < faces.size(); i++) {
  57. colors.push_back(p_modulate);
  58. }
  59. Ref<ArrayMesh> mesh = memnew(ArrayMesh);
  60. Array a;
  61. a.resize(Mesh::ARRAY_MAX);
  62. a[RS::ARRAY_VERTEX] = faces;
  63. a[RS::ARRAY_COLOR] = colors;
  64. mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, a);
  65. return mesh;
  66. }
  67. real_t ConcavePolygonShape3D::get_enclosing_radius() const {
  68. Vector<Vector3> data = get_faces();
  69. const Vector3 *read = data.ptr();
  70. real_t r = 0.0;
  71. for (int i(0); i < data.size(); i++) {
  72. r = MAX(read[i].length_squared(), r);
  73. }
  74. return Math::sqrt(r);
  75. }
  76. void ConcavePolygonShape3D::_update_shape() {
  77. Dictionary d;
  78. d["faces"] = faces;
  79. d["backface_collision"] = backface_collision;
  80. PhysicsServer3D::get_singleton()->shape_set_data(get_shape(), d);
  81. Shape3D::_update_shape();
  82. }
  83. void ConcavePolygonShape3D::set_faces(const Vector<Vector3> &p_faces) {
  84. faces = p_faces;
  85. _update_shape();
  86. emit_changed();
  87. }
  88. Vector<Vector3> ConcavePolygonShape3D::get_faces() const {
  89. return faces;
  90. }
  91. void ConcavePolygonShape3D::set_backface_collision_enabled(bool p_enabled) {
  92. backface_collision = p_enabled;
  93. if (!faces.is_empty()) {
  94. _update_shape();
  95. emit_changed();
  96. }
  97. }
  98. bool ConcavePolygonShape3D::is_backface_collision_enabled() const {
  99. return backface_collision;
  100. }
  101. void ConcavePolygonShape3D::_bind_methods() {
  102. ClassDB::bind_method(D_METHOD("set_faces", "faces"), &ConcavePolygonShape3D::set_faces);
  103. ClassDB::bind_method(D_METHOD("get_faces"), &ConcavePolygonShape3D::get_faces);
  104. ClassDB::bind_method(D_METHOD("set_backface_collision_enabled", "enabled"), &ConcavePolygonShape3D::set_backface_collision_enabled);
  105. ClassDB::bind_method(D_METHOD("is_backface_collision_enabled"), &ConcavePolygonShape3D::is_backface_collision_enabled);
  106. ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR3_ARRAY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "set_faces", "get_faces");
  107. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "backface_collision"), "set_backface_collision_enabled", "is_backface_collision_enabled");
  108. }
  109. ConcavePolygonShape3D::ConcavePolygonShape3D() :
  110. Shape3D(PhysicsServer3D::get_singleton()->shape_create(PhysicsServer3D::SHAPE_CONCAVE_POLYGON)) {
  111. //set_planes(Vector3(1,1,1));
  112. }