height_map_shape_3d.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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) 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 "height_map_shape_3d.h"
  31. #include "core/io/image.h"
  32. #include "scene/resources/mesh.h"
  33. #include "servers/physics_server_3d.h"
  34. Vector<Vector3> HeightMapShape3D::get_debug_mesh_lines() const {
  35. Vector<Vector3> points;
  36. if ((map_width != 0) && (map_depth != 0)) {
  37. // This will be slow for large maps...
  38. // also we'll have to figure out how well bullet centers this shape...
  39. Vector2 size(map_width - 1, map_depth - 1);
  40. Vector2 start = size * -0.5;
  41. const real_t *r = map_data.ptr();
  42. // reserve some memory for our points..
  43. points.resize(((map_width - 1) * map_depth * 2) + (map_width * (map_depth - 1) * 2) + ((map_width - 1) * (map_depth - 1) * 2));
  44. // now set our points
  45. int r_offset = 0;
  46. int w_offset = 0;
  47. for (int d = 0; d < map_depth; d++) {
  48. Vector3 height(start.x, 0.0, start.y);
  49. for (int w = 0; w < map_width; w++) {
  50. height.y = r[r_offset++];
  51. if (w != map_width - 1) {
  52. points.write[w_offset++] = height;
  53. points.write[w_offset++] = Vector3(height.x + 1.0, r[r_offset], height.z);
  54. }
  55. if (d != map_depth - 1) {
  56. points.write[w_offset++] = height;
  57. points.write[w_offset++] = Vector3(height.x, r[r_offset + map_width - 1], height.z + 1.0);
  58. }
  59. if ((w != map_width - 1) && (d != map_depth - 1)) {
  60. points.write[w_offset++] = Vector3(height.x + 1.0, r[r_offset], height.z);
  61. points.write[w_offset++] = Vector3(height.x, r[r_offset + map_width - 1], height.z + 1.0);
  62. }
  63. height.x += 1.0;
  64. }
  65. start.y += 1.0;
  66. }
  67. }
  68. return points;
  69. }
  70. Ref<ArrayMesh> HeightMapShape3D::get_debug_arraymesh_faces(const Color &p_modulate) const {
  71. Vector<Vector3> verts;
  72. Vector<Color> colors;
  73. Vector<int> indices;
  74. // This will be slow for large maps...
  75. if ((map_width != 0) && (map_depth != 0)) {
  76. Vector2 size = Vector2(map_width - 1, map_depth - 1) * -0.5;
  77. const real_t *r = map_data.ptr();
  78. for (int d = 0; d <= map_depth - 2; d++) {
  79. const int this_row_offset = map_width * d;
  80. const int next_row_offset = this_row_offset + map_width;
  81. for (int w = 0; w <= map_width - 2; w++) {
  82. const float height_tl = r[next_row_offset + w];
  83. const float height_bl = r[this_row_offset + w];
  84. const float height_br = r[this_row_offset + w + 1];
  85. const float height_tr = r[next_row_offset + w + 1];
  86. const int index_offset = verts.size();
  87. verts.push_back(Vector3(size.x + w, height_tl, size.y + d + 1));
  88. verts.push_back(Vector3(size.x + w, height_bl, size.y + d));
  89. verts.push_back(Vector3(size.x + w + 1, height_br, size.y + d));
  90. verts.push_back(Vector3(size.x + w + 1, height_tr, size.y + d + 1));
  91. colors.push_back(p_modulate);
  92. colors.push_back(p_modulate);
  93. colors.push_back(p_modulate);
  94. colors.push_back(p_modulate);
  95. indices.push_back(index_offset);
  96. indices.push_back(index_offset + 1);
  97. indices.push_back(index_offset + 2);
  98. indices.push_back(index_offset);
  99. indices.push_back(index_offset + 2);
  100. indices.push_back(index_offset + 3);
  101. }
  102. }
  103. }
  104. Ref<ArrayMesh> mesh = memnew(ArrayMesh);
  105. Array a;
  106. a.resize(Mesh::ARRAY_MAX);
  107. a[RS::ARRAY_VERTEX] = verts;
  108. a[RS::ARRAY_COLOR] = colors;
  109. a[RS::ARRAY_INDEX] = indices;
  110. mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, a);
  111. return mesh;
  112. }
  113. real_t HeightMapShape3D::get_enclosing_radius() const {
  114. return Vector3(real_t(map_width), max_height - min_height, real_t(map_depth)).length();
  115. }
  116. void HeightMapShape3D::_update_shape() {
  117. Dictionary d;
  118. d["width"] = map_width;
  119. d["depth"] = map_depth;
  120. d["heights"] = map_data;
  121. d["min_height"] = min_height;
  122. d["max_height"] = max_height;
  123. PhysicsServer3D::get_singleton()->shape_set_data(get_shape(), d);
  124. Shape3D::_update_shape();
  125. }
  126. void HeightMapShape3D::set_map_width(int p_new) {
  127. if (p_new < 1) {
  128. // ignore
  129. } else if (map_width != p_new) {
  130. int was_size = map_width * map_depth;
  131. map_width = p_new;
  132. int new_size = map_width * map_depth;
  133. map_data.resize(map_width * map_depth);
  134. real_t *w = map_data.ptrw();
  135. while (was_size < new_size) {
  136. w[was_size++] = 0.0;
  137. }
  138. _update_shape();
  139. emit_changed();
  140. }
  141. }
  142. int HeightMapShape3D::get_map_width() const {
  143. return map_width;
  144. }
  145. void HeightMapShape3D::set_map_depth(int p_new) {
  146. if (p_new < 1) {
  147. // ignore
  148. } else if (map_depth != p_new) {
  149. int was_size = map_width * map_depth;
  150. map_depth = p_new;
  151. int new_size = map_width * map_depth;
  152. map_data.resize(new_size);
  153. real_t *w = map_data.ptrw();
  154. while (was_size < new_size) {
  155. w[was_size++] = 0.0;
  156. }
  157. _update_shape();
  158. emit_changed();
  159. }
  160. }
  161. int HeightMapShape3D::get_map_depth() const {
  162. return map_depth;
  163. }
  164. void HeightMapShape3D::set_map_data(Vector<real_t> p_new) {
  165. int size = (map_width * map_depth);
  166. if (p_new.size() != size) {
  167. // fail
  168. return;
  169. }
  170. // copy
  171. real_t *w = map_data.ptrw();
  172. const real_t *r = p_new.ptr();
  173. for (int i = 0; i < size; i++) {
  174. real_t val = r[i];
  175. w[i] = val;
  176. if (i == 0) {
  177. min_height = val;
  178. max_height = val;
  179. } else {
  180. if (min_height > val) {
  181. min_height = val;
  182. }
  183. if (max_height < val) {
  184. max_height = val;
  185. }
  186. }
  187. }
  188. _update_shape();
  189. emit_changed();
  190. }
  191. Vector<real_t> HeightMapShape3D::get_map_data() const {
  192. return map_data;
  193. }
  194. real_t HeightMapShape3D::get_min_height() const {
  195. return min_height;
  196. }
  197. real_t HeightMapShape3D::get_max_height() const {
  198. return max_height;
  199. }
  200. void HeightMapShape3D::update_map_data_from_image(const Ref<Image> &p_image, real_t p_height_min, real_t p_height_max) {
  201. ERR_FAIL_COND_MSG(p_image.is_null(), "Heightmap update image requires a valid Image reference.");
  202. ERR_FAIL_COND_MSG(p_image->get_format() != Image::FORMAT_RF && p_image->get_format() != Image::FORMAT_RH && p_image->get_format() != Image::FORMAT_R8, "Heightmap update image requires Image in format FORMAT_RF (32 bit), FORMAT_RH (16 bit), or FORMAT_R8 (8 bit).");
  203. ERR_FAIL_COND_MSG(p_image->get_width() < 2, "Heightmap update image requires a minimum Image width of 2.");
  204. ERR_FAIL_COND_MSG(p_image->get_height() < 2, "Heightmap update image requires a minimum Image height of 2.");
  205. ERR_FAIL_COND_MSG(p_height_min > p_height_max, "Heightmap update image requires height_max to be greater than height_min.");
  206. map_width = p_image->get_width();
  207. map_depth = p_image->get_height();
  208. map_data.resize(map_width * map_depth);
  209. real_t new_min_height = FLT_MAX;
  210. real_t new_max_height = -FLT_MAX;
  211. float remap_height_min = float(p_height_min);
  212. float remap_height_max = float(p_height_max);
  213. real_t *map_data_ptrw = map_data.ptrw();
  214. switch (p_image->get_format()) {
  215. case Image::FORMAT_RF: {
  216. const float *image_data_ptr = (float *)p_image->get_data().ptr();
  217. for (int i = 0; i < map_data.size(); i++) {
  218. float pixel_value = image_data_ptr[i];
  219. DEV_ASSERT(pixel_value >= 0.0 && pixel_value <= 1.0);
  220. real_t height_value = Math::remap(pixel_value, 0.0f, 1.0f, remap_height_min, remap_height_max);
  221. if (height_value < new_min_height) {
  222. new_min_height = height_value;
  223. }
  224. if (height_value > new_max_height) {
  225. new_max_height = height_value;
  226. }
  227. map_data_ptrw[i] = height_value;
  228. }
  229. } break;
  230. case Image::FORMAT_RH: {
  231. const uint16_t *image_data_ptr = (uint16_t *)p_image->get_data().ptr();
  232. for (int i = 0; i < map_data.size(); i++) {
  233. float pixel_value = Math::half_to_float(image_data_ptr[i]);
  234. DEV_ASSERT(pixel_value >= 0.0 && pixel_value <= 1.0);
  235. real_t height_value = Math::remap(pixel_value, 0.0f, 1.0f, remap_height_min, remap_height_max);
  236. if (height_value < new_min_height) {
  237. new_min_height = height_value;
  238. }
  239. if (height_value > new_max_height) {
  240. new_max_height = height_value;
  241. }
  242. map_data_ptrw[i] = height_value;
  243. }
  244. } break;
  245. case Image::FORMAT_R8: {
  246. const uint8_t *image_data_ptr = (uint8_t *)p_image->get_data().ptr();
  247. for (int i = 0; i < map_data.size(); i++) {
  248. float pixel_value = float(image_data_ptr[i] / 255.0);
  249. DEV_ASSERT(pixel_value >= 0.0 && pixel_value <= 1.0);
  250. real_t height_value = Math::remap(pixel_value, 0.0f, 1.0f, remap_height_min, remap_height_max);
  251. if (height_value < new_min_height) {
  252. new_min_height = height_value;
  253. }
  254. if (height_value > new_max_height) {
  255. new_max_height = height_value;
  256. }
  257. map_data_ptrw[i] = height_value;
  258. }
  259. } break;
  260. default: {
  261. return;
  262. }
  263. }
  264. min_height = new_min_height;
  265. max_height = new_max_height;
  266. _update_shape();
  267. emit_changed();
  268. }
  269. void HeightMapShape3D::_bind_methods() {
  270. ClassDB::bind_method(D_METHOD("set_map_width", "width"), &HeightMapShape3D::set_map_width);
  271. ClassDB::bind_method(D_METHOD("get_map_width"), &HeightMapShape3D::get_map_width);
  272. ClassDB::bind_method(D_METHOD("set_map_depth", "height"), &HeightMapShape3D::set_map_depth);
  273. ClassDB::bind_method(D_METHOD("get_map_depth"), &HeightMapShape3D::get_map_depth);
  274. ClassDB::bind_method(D_METHOD("set_map_data", "data"), &HeightMapShape3D::set_map_data);
  275. ClassDB::bind_method(D_METHOD("get_map_data"), &HeightMapShape3D::get_map_data);
  276. ClassDB::bind_method(D_METHOD("get_min_height"), &HeightMapShape3D::get_min_height);
  277. ClassDB::bind_method(D_METHOD("get_max_height"), &HeightMapShape3D::get_max_height);
  278. ClassDB::bind_method(D_METHOD("update_map_data_from_image", "image", "height_min", "height_max"), &HeightMapShape3D::update_map_data_from_image);
  279. ADD_PROPERTY(PropertyInfo(Variant::INT, "map_width", PROPERTY_HINT_RANGE, "1,100,1,or_greater"), "set_map_width", "get_map_width");
  280. ADD_PROPERTY(PropertyInfo(Variant::INT, "map_depth", PROPERTY_HINT_RANGE, "1,100,1,or_greater"), "set_map_depth", "get_map_depth");
  281. ADD_PROPERTY(PropertyInfo(Variant::PACKED_FLOAT32_ARRAY, "map_data"), "set_map_data", "get_map_data");
  282. }
  283. HeightMapShape3D::HeightMapShape3D() :
  284. Shape3D(PhysicsServer3D::get_singleton()->shape_create(PhysicsServer3D::SHAPE_HEIGHTMAP)) {
  285. map_data.resize(map_width * map_depth);
  286. real_t *w = map_data.ptrw();
  287. w[0] = 0.0;
  288. w[1] = 0.0;
  289. w[2] = 0.0;
  290. w[3] = 0.0;
  291. _update_shape();
  292. }