height_map_shape_3d.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*************************************************************************/
  2. /* height_map_shape_3d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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 "height_map_shape_3d.h"
  31. #include "servers/physics_server_3d.h"
  32. Vector<Vector3> HeightMapShape3D::get_debug_mesh_lines() const {
  33. Vector<Vector3> points;
  34. if ((map_width != 0) && (map_depth != 0)) {
  35. // This will be slow for large maps...
  36. // also we'll have to figure out how well bullet centers this shape...
  37. Vector2 size(map_width - 1, map_depth - 1);
  38. Vector2 start = size * -0.5;
  39. const real_t *r = map_data.ptr();
  40. // reserve some memory for our points..
  41. points.resize(((map_width - 1) * map_depth * 2) + (map_width * (map_depth - 1) * 2) + ((map_width - 1) * (map_depth - 1) * 2));
  42. // now set our points
  43. int r_offset = 0;
  44. int w_offset = 0;
  45. for (int d = 0; d < map_depth; d++) {
  46. Vector3 height(start.x, 0.0, start.y);
  47. for (int w = 0; w < map_width; w++) {
  48. height.y = r[r_offset++];
  49. if (w != map_width - 1) {
  50. points.write[w_offset++] = height;
  51. points.write[w_offset++] = Vector3(height.x + 1.0, r[r_offset], height.z);
  52. }
  53. if (d != map_depth - 1) {
  54. points.write[w_offset++] = height;
  55. points.write[w_offset++] = Vector3(height.x, r[r_offset + map_width - 1], height.z + 1.0);
  56. }
  57. if ((w != map_width - 1) && (d != map_depth - 1)) {
  58. points.write[w_offset++] = Vector3(height.x + 1.0, r[r_offset], height.z);
  59. points.write[w_offset++] = Vector3(height.x, r[r_offset + map_width - 1], height.z + 1.0);
  60. }
  61. height.x += 1.0;
  62. }
  63. start.y += 1.0;
  64. }
  65. }
  66. return points;
  67. }
  68. real_t HeightMapShape3D::get_enclosing_radius() const {
  69. return Vector3(real_t(map_width), max_height - min_height, real_t(map_depth)).length();
  70. }
  71. void HeightMapShape3D::_update_shape() {
  72. Dictionary d;
  73. d["width"] = map_width;
  74. d["depth"] = map_depth;
  75. d["heights"] = map_data;
  76. d["min_height"] = min_height;
  77. d["max_height"] = max_height;
  78. PhysicsServer3D::get_singleton()->shape_set_data(get_shape(), d);
  79. Shape3D::_update_shape();
  80. }
  81. void HeightMapShape3D::set_map_width(int p_new) {
  82. if (p_new < 1) {
  83. // ignore
  84. } else if (map_width != p_new) {
  85. int was_size = map_width * map_depth;
  86. map_width = p_new;
  87. int new_size = map_width * map_depth;
  88. map_data.resize(map_width * map_depth);
  89. real_t *w = map_data.ptrw();
  90. while (was_size < new_size) {
  91. w[was_size++] = 0.0;
  92. }
  93. _update_shape();
  94. notify_change_to_owners();
  95. }
  96. }
  97. int HeightMapShape3D::get_map_width() const {
  98. return map_width;
  99. }
  100. void HeightMapShape3D::set_map_depth(int p_new) {
  101. if (p_new < 1) {
  102. // ignore
  103. } else if (map_depth != p_new) {
  104. int was_size = map_width * map_depth;
  105. map_depth = p_new;
  106. int new_size = map_width * map_depth;
  107. map_data.resize(new_size);
  108. real_t *w = map_data.ptrw();
  109. while (was_size < new_size) {
  110. w[was_size++] = 0.0;
  111. }
  112. _update_shape();
  113. notify_change_to_owners();
  114. }
  115. }
  116. int HeightMapShape3D::get_map_depth() const {
  117. return map_depth;
  118. }
  119. void HeightMapShape3D::set_map_data(Vector<real_t> p_new) {
  120. int size = (map_width * map_depth);
  121. if (p_new.size() != size) {
  122. // fail
  123. return;
  124. }
  125. // copy
  126. real_t *w = map_data.ptrw();
  127. const real_t *r = p_new.ptr();
  128. for (int i = 0; i < size; i++) {
  129. real_t val = r[i];
  130. w[i] = val;
  131. if (i == 0) {
  132. min_height = val;
  133. max_height = val;
  134. } else {
  135. if (min_height > val) {
  136. min_height = val;
  137. }
  138. if (max_height < val) {
  139. max_height = val;
  140. }
  141. }
  142. }
  143. _update_shape();
  144. notify_change_to_owners();
  145. }
  146. Vector<real_t> HeightMapShape3D::get_map_data() const {
  147. return map_data;
  148. }
  149. void HeightMapShape3D::_bind_methods() {
  150. ClassDB::bind_method(D_METHOD("set_map_width", "width"), &HeightMapShape3D::set_map_width);
  151. ClassDB::bind_method(D_METHOD("get_map_width"), &HeightMapShape3D::get_map_width);
  152. ClassDB::bind_method(D_METHOD("set_map_depth", "height"), &HeightMapShape3D::set_map_depth);
  153. ClassDB::bind_method(D_METHOD("get_map_depth"), &HeightMapShape3D::get_map_depth);
  154. ClassDB::bind_method(D_METHOD("set_map_data", "data"), &HeightMapShape3D::set_map_data);
  155. ClassDB::bind_method(D_METHOD("get_map_data"), &HeightMapShape3D::get_map_data);
  156. ADD_PROPERTY(PropertyInfo(Variant::INT, "map_width", PROPERTY_HINT_RANGE, "0.001,100,0.001,or_greater"), "set_map_width", "get_map_width");
  157. ADD_PROPERTY(PropertyInfo(Variant::INT, "map_depth", PROPERTY_HINT_RANGE, "0.001,100,0.001,or_greater"), "set_map_depth", "get_map_depth");
  158. ADD_PROPERTY(PropertyInfo(Variant::PACKED_FLOAT32_ARRAY, "map_data"), "set_map_data", "get_map_data");
  159. }
  160. HeightMapShape3D::HeightMapShape3D() :
  161. Shape3D(PhysicsServer3D::get_singleton()->shape_create(PhysicsServer3D::SHAPE_HEIGHTMAP)) {
  162. map_data.resize(map_width * map_depth);
  163. real_t *w = map_data.ptrw();
  164. w[0] = 0.0;
  165. w[1] = 0.0;
  166. w[2] = 0.0;
  167. w[3] = 0.0;
  168. _update_shape();
  169. }