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