convex_polygon_shape_3d.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**************************************************************************/
  2. /* convex_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 "convex_polygon_shape_3d.h"
  31. #include "core/math/convex_hull.h"
  32. #include "scene/resources/mesh.h"
  33. #include "servers/physics_server_3d.h"
  34. Vector<Vector3> ConvexPolygonShape3D::get_debug_mesh_lines() const {
  35. Vector<Vector3> poly_points = get_points();
  36. if (poly_points.size() > 1) { // Need at least 2 points for a line.
  37. Vector<Vector3> varr = Variant(poly_points);
  38. Geometry3D::MeshData md;
  39. Error err = ConvexHullComputer::convex_hull(varr, md);
  40. if (err == OK) {
  41. Vector<Vector3> lines;
  42. lines.resize(md.edges.size() * 2);
  43. for (uint32_t i = 0; i < md.edges.size(); i++) {
  44. lines.write[i * 2 + 0] = md.vertices[md.edges[i].vertex_a];
  45. lines.write[i * 2 + 1] = md.vertices[md.edges[i].vertex_b];
  46. }
  47. return lines;
  48. }
  49. }
  50. return Vector<Vector3>();
  51. }
  52. Ref<ArrayMesh> ConvexPolygonShape3D::get_debug_arraymesh_faces(const Color &p_modulate) const {
  53. const Vector<Vector3> hull_points = get_points();
  54. Vector<Vector3> verts;
  55. Vector<Color> colors;
  56. Vector<int> indices;
  57. if (hull_points.size() >= 3) {
  58. Geometry3D::MeshData md;
  59. Error err = ConvexHullComputer::convex_hull(hull_points, md);
  60. if (err == OK) {
  61. verts = md.vertices;
  62. for (int i = 0; i < verts.size(); i++) {
  63. colors.push_back(p_modulate);
  64. }
  65. for (const Geometry3D::MeshData::Face &face : md.faces) {
  66. const int first_point = face.indices[0];
  67. const int indices_count = face.indices.size();
  68. for (int i = 1; i < indices_count - 1; i++) {
  69. indices.push_back(first_point);
  70. indices.push_back(face.indices[i]);
  71. indices.push_back(face.indices[i + 1]);
  72. }
  73. }
  74. }
  75. }
  76. Ref<ArrayMesh> mesh = memnew(ArrayMesh);
  77. Array a;
  78. a.resize(Mesh::ARRAY_MAX);
  79. a[RS::ARRAY_VERTEX] = verts;
  80. a[RS::ARRAY_COLOR] = colors;
  81. a[RS::ARRAY_INDEX] = indices;
  82. mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, a);
  83. return mesh;
  84. }
  85. real_t ConvexPolygonShape3D::get_enclosing_radius() const {
  86. Vector<Vector3> data = get_points();
  87. const Vector3 *read = data.ptr();
  88. real_t r = 0.0;
  89. for (int i(0); i < data.size(); i++) {
  90. r = MAX(read[i].length_squared(), r);
  91. }
  92. return Math::sqrt(r);
  93. }
  94. void ConvexPolygonShape3D::_update_shape() {
  95. PhysicsServer3D::get_singleton()->shape_set_data(get_shape(), points);
  96. Shape3D::_update_shape();
  97. }
  98. void ConvexPolygonShape3D::set_points(const Vector<Vector3> &p_points) {
  99. points = p_points;
  100. _update_shape();
  101. emit_changed();
  102. }
  103. Vector<Vector3> ConvexPolygonShape3D::get_points() const {
  104. return points;
  105. }
  106. void ConvexPolygonShape3D::_bind_methods() {
  107. ClassDB::bind_method(D_METHOD("set_points", "points"), &ConvexPolygonShape3D::set_points);
  108. ClassDB::bind_method(D_METHOD("get_points"), &ConvexPolygonShape3D::get_points);
  109. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "points"), "set_points", "get_points");
  110. }
  111. ConvexPolygonShape3D::ConvexPolygonShape3D() :
  112. Shape3D(PhysicsServer3D::get_singleton()->shape_create(PhysicsServer3D::SHAPE_CONVEX_POLYGON)) {
  113. }