height_map_shape.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*************************************************************************/
  2. /* height_map_shape.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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.h"
  31. #include "servers/physics_server.h"
  32. Vector<Vector3> HeightMapShape::get_debug_mesh_lines() {
  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. PoolRealArray::Read r = map_data.read();
  40. // reserve some memory for our points..
  41. points.resize(((map_width - 1) * map_depth * 2) + (map_width * (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. height.x += 1.0;
  58. }
  59. start.y += 1.0;
  60. }
  61. }
  62. return points;
  63. }
  64. void HeightMapShape::_update_shape() {
  65. Dictionary d;
  66. d["width"] = map_width;
  67. d["depth"] = map_depth;
  68. d["heights"] = map_data;
  69. d["min_height"] = min_height;
  70. d["max_height"] = max_height;
  71. PhysicsServer::get_singleton()->shape_set_data(get_shape(), d);
  72. Shape::_update_shape();
  73. }
  74. void HeightMapShape::set_map_width(int p_new) {
  75. if (p_new < 1) {
  76. // ignore
  77. } else if (map_width != p_new) {
  78. int was_size = map_width * map_depth;
  79. map_width = p_new;
  80. int new_size = map_width * map_depth;
  81. map_data.resize(map_width * map_depth);
  82. PoolRealArray::Write w = map_data.write();
  83. while (was_size < new_size) {
  84. w[was_size++] = 0.0;
  85. }
  86. _update_shape();
  87. notify_change_to_owners();
  88. _change_notify("map_width");
  89. _change_notify("map_data");
  90. }
  91. }
  92. int HeightMapShape::get_map_width() const {
  93. return map_width;
  94. }
  95. void HeightMapShape::set_map_depth(int p_new) {
  96. if (p_new < 1) {
  97. // ignore
  98. } else if (map_depth != p_new) {
  99. int was_size = map_width * map_depth;
  100. map_depth = p_new;
  101. int new_size = map_width * map_depth;
  102. map_data.resize(new_size);
  103. PoolRealArray::Write w = map_data.write();
  104. while (was_size < new_size) {
  105. w[was_size++] = 0.0;
  106. }
  107. _update_shape();
  108. notify_change_to_owners();
  109. _change_notify("map_depth");
  110. _change_notify("map_data");
  111. }
  112. }
  113. int HeightMapShape::get_map_depth() const {
  114. return map_depth;
  115. }
  116. void HeightMapShape::set_map_data(PoolRealArray p_new) {
  117. int size = (map_width * map_depth);
  118. if (p_new.size() != size) {
  119. // fail
  120. return;
  121. }
  122. // copy
  123. PoolRealArray::Write w = map_data.write();
  124. PoolRealArray::Read r = p_new.read();
  125. for (int i = 0; i < size; i++) {
  126. float val = r[i];
  127. w[i] = val;
  128. if (i == 0) {
  129. min_height = val;
  130. max_height = val;
  131. } else {
  132. if (min_height > val)
  133. min_height = val;
  134. if (max_height < val)
  135. max_height = val;
  136. }
  137. }
  138. _update_shape();
  139. notify_change_to_owners();
  140. _change_notify("map_data");
  141. }
  142. PoolRealArray HeightMapShape::get_map_data() const {
  143. return map_data;
  144. }
  145. void HeightMapShape::_bind_methods() {
  146. ClassDB::bind_method(D_METHOD("set_map_width", "width"), &HeightMapShape::set_map_width);
  147. ClassDB::bind_method(D_METHOD("get_map_width"), &HeightMapShape::get_map_width);
  148. ClassDB::bind_method(D_METHOD("set_map_depth", "height"), &HeightMapShape::set_map_depth);
  149. ClassDB::bind_method(D_METHOD("get_map_depth"), &HeightMapShape::get_map_depth);
  150. ClassDB::bind_method(D_METHOD("set_map_data", "data"), &HeightMapShape::set_map_data);
  151. ClassDB::bind_method(D_METHOD("get_map_data"), &HeightMapShape::get_map_data);
  152. ADD_PROPERTY(PropertyInfo(Variant::INT, "map_width", PROPERTY_HINT_RANGE, "1,4096,1"), "set_map_width", "get_map_width");
  153. ADD_PROPERTY(PropertyInfo(Variant::INT, "map_depth", PROPERTY_HINT_RANGE, "1,4096,1"), "set_map_depth", "get_map_depth");
  154. ADD_PROPERTY(PropertyInfo(Variant::POOL_REAL_ARRAY, "map_data"), "set_map_data", "get_map_data");
  155. }
  156. HeightMapShape::HeightMapShape() :
  157. Shape(PhysicsServer::get_singleton()->shape_create(PhysicsServer::SHAPE_HEIGHTMAP)) {
  158. map_width = 2;
  159. map_depth = 2;
  160. map_data.resize(map_width * map_depth);
  161. PoolRealArray::Write w = map_data.write();
  162. w[0] = 0.0;
  163. w[1] = 0.0;
  164. w[2] = 0.0;
  165. w[3] = 0.0;
  166. min_height = 0.0;
  167. max_height = 0.0;
  168. _update_shape();
  169. }