world_boundary_shape_3d.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /**************************************************************************/
  2. /* world_boundary_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 "world_boundary_shape_3d.h"
  31. #include "scene/resources/mesh.h"
  32. #include "servers/physics_server_3d.h"
  33. Vector<Vector3> WorldBoundaryShape3D::get_debug_mesh_lines() const {
  34. Plane p = get_plane();
  35. Vector3 n1 = p.get_any_perpendicular_normal();
  36. Vector3 n2 = p.normal.cross(n1).normalized();
  37. Vector3 pface[4] = {
  38. p.normal * p.d + n1 * 10.0 + n2 * 10.0,
  39. p.normal * p.d + n1 * 10.0 + n2 * -10.0,
  40. p.normal * p.d + n1 * -10.0 + n2 * -10.0,
  41. p.normal * p.d + n1 * -10.0 + n2 * 10.0,
  42. };
  43. Vector<Vector3> points = {
  44. pface[0],
  45. pface[1],
  46. pface[1],
  47. pface[2],
  48. pface[2],
  49. pface[3],
  50. pface[3],
  51. pface[0],
  52. p.normal * p.d,
  53. p.normal * p.d + p.normal * 3
  54. };
  55. return points;
  56. }
  57. Ref<ArrayMesh> WorldBoundaryShape3D::get_debug_arraymesh_faces(const Color &p_modulate) const {
  58. Plane p = get_plane();
  59. Vector3 n1 = p.get_any_perpendicular_normal();
  60. Vector3 n2 = p.normal.cross(n1).normalized();
  61. Vector3 pface[4] = {
  62. p.normal * p.d + n1 * 10.0 + n2 * 10.0,
  63. p.normal * p.d + n1 * 10.0 + n2 * -10.0,
  64. p.normal * p.d + n1 * -10.0 + n2 * -10.0,
  65. p.normal * p.d + n1 * -10.0 + n2 * 10.0,
  66. };
  67. Vector<Vector3> points = {
  68. pface[0],
  69. pface[1],
  70. pface[2],
  71. pface[3],
  72. };
  73. Vector<Color> colors = {
  74. p_modulate,
  75. p_modulate,
  76. p_modulate,
  77. p_modulate,
  78. };
  79. Vector<int> indices = {
  80. 0,
  81. 1,
  82. 2,
  83. 0,
  84. 2,
  85. 3,
  86. };
  87. Ref<ArrayMesh> mesh = memnew(ArrayMesh);
  88. Array a;
  89. a.resize(Mesh::ARRAY_MAX);
  90. a[RS::ARRAY_VERTEX] = points;
  91. a[RS::ARRAY_COLOR] = colors;
  92. a[RS::ARRAY_INDEX] = indices;
  93. mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, a);
  94. return mesh;
  95. }
  96. void WorldBoundaryShape3D::_update_shape() {
  97. PhysicsServer3D::get_singleton()->shape_set_data(get_shape(), plane);
  98. Shape3D::_update_shape();
  99. }
  100. void WorldBoundaryShape3D::set_plane(const Plane &p_plane) {
  101. plane = p_plane;
  102. _update_shape();
  103. emit_changed();
  104. }
  105. const Plane &WorldBoundaryShape3D::get_plane() const {
  106. return plane;
  107. }
  108. void WorldBoundaryShape3D::_bind_methods() {
  109. ClassDB::bind_method(D_METHOD("set_plane", "plane"), &WorldBoundaryShape3D::set_plane);
  110. ClassDB::bind_method(D_METHOD("get_plane"), &WorldBoundaryShape3D::get_plane);
  111. ADD_PROPERTY(PropertyInfo(Variant::PLANE, "plane", PROPERTY_HINT_NONE, "suffix:m"), "set_plane", "get_plane");
  112. }
  113. WorldBoundaryShape3D::WorldBoundaryShape3D() :
  114. Shape3D(PhysicsServer3D::get_singleton()->shape_create(PhysicsServer3D::SHAPE_WORLD_BOUNDARY)) {
  115. set_plane(Plane(0, 1, 0, 0));
  116. }