| 1 | /**************************************************************************/ |
| 2 | /* multimesh.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 "multimesh.h" |
| 32 | |
| 33 | #include "servers/rendering_server.h" |
| 34 | |
| 35 | #ifndef DISABLE_DEPRECATED |
| 36 | // Kept for compatibility from 3.x to 4.0. |
| 37 | |
| 38 | void MultiMesh::_set_transform_array(const Vector<Vector3> &p_array) { |
| 39 | if (transform_format != TRANSFORM_3D) { |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | const Vector<Vector3> &xforms = p_array; |
| 44 | int len = xforms.size(); |
| 45 | ERR_FAIL_COND((len / 4) != instance_count); |
| 46 | if (len == 0) { |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | const Vector3 *r = xforms.ptr(); |
| 51 | |
| 52 | for (int i = 0; i < len / 4; i++) { |
| 53 | Transform3D t; |
| 54 | t.basis[0] = r[i * 4 + 0]; |
| 55 | t.basis[1] = r[i * 4 + 1]; |
| 56 | t.basis[2] = r[i * 4 + 2]; |
| 57 | t.origin = r[i * 4 + 3]; |
| 58 | |
| 59 | set_instance_transform(i, t); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | Vector<Vector3> MultiMesh::_get_transform_array() const { |
| 64 | if (transform_format != TRANSFORM_3D) { |
| 65 | return Vector<Vector3>(); |
| 66 | } |
| 67 | |
| 68 | if (instance_count == 0) { |
| 69 | return Vector<Vector3>(); |
| 70 | } |
| 71 | |
| 72 | Vector<Vector3> xforms; |
| 73 | xforms.resize(instance_count * 4); |
| 74 | |
| 75 | Vector3 *w = xforms.ptrw(); |
| 76 | |
| 77 | for (int i = 0; i < instance_count; i++) { |
| 78 | Transform3D t = get_instance_transform(i); |
| 79 | w[i * 4 + 0] = t.basis[0]; |
| 80 | w[i * 4 + 1] = t.basis[1]; |
| 81 | w[i * 4 + 2] = t.basis[2]; |
| 82 | w[i * 4 + 3] = t.origin; |
| 83 | } |
| 84 | |
| 85 | return xforms; |
| 86 | } |
| 87 | |
| 88 | void MultiMesh::_set_transform_2d_array(const Vector<Vector2> &p_array) { |
| 89 | if (transform_format != TRANSFORM_2D) { |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | const Vector<Vector2> &xforms = p_array; |
| 94 | int len = xforms.size(); |
| 95 | ERR_FAIL_COND((len / 3) != instance_count); |
| 96 | if (len == 0) { |
| 97 | return; |
| 98 | } |
| 99 | |
| 100 | const Vector2 *r = xforms.ptr(); |
| 101 | |
| 102 | for (int i = 0; i < len / 3; i++) { |
| 103 | Transform2D t; |
| 104 | t.columns[0] = r[i * 3 + 0]; |
| 105 | t.columns[1] = r[i * 3 + 1]; |
| 106 | t.columns[2] = r[i * 3 + 2]; |
| 107 | |
| 108 | set_instance_transform_2d(i, t); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | Vector<Vector2> MultiMesh::_get_transform_2d_array() const { |
| 113 | if (transform_format != TRANSFORM_2D) { |
| 114 | return Vector<Vector2>(); |
| 115 | } |
| 116 | |
| 117 | if (instance_count == 0) { |
| 118 | return Vector<Vector2>(); |
| 119 | } |
| 120 | |
| 121 | Vector<Vector2> xforms; |
| 122 | xforms.resize(instance_count * 3); |
| 123 | |
| 124 | Vector2 *w = xforms.ptrw(); |
| 125 | |
| 126 | for (int i = 0; i < instance_count; i++) { |
| 127 | Transform2D t = get_instance_transform_2d(i); |
| 128 | w[i * 3 + 0] = t.columns[0]; |
| 129 | w[i * 3 + 1] = t.columns[1]; |
| 130 | w[i * 3 + 2] = t.columns[2]; |
| 131 | } |
| 132 | |
| 133 | return xforms; |
| 134 | } |
| 135 | |
| 136 | void MultiMesh::_set_color_array(const Vector<Color> &p_array) { |
| 137 | const Vector<Color> &colors = p_array; |
| 138 | int len = colors.size(); |
| 139 | if (len == 0) { |
| 140 | return; |
| 141 | } |
| 142 | ERR_FAIL_COND(len != instance_count); |
| 143 | |
| 144 | const Color *r = colors.ptr(); |
| 145 | |
| 146 | for (int i = 0; i < len; i++) { |
| 147 | set_instance_color(i, r[i]); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | Vector<Color> MultiMesh::_get_color_array() const { |
| 152 | if (instance_count == 0 || !use_colors) { |
| 153 | return Vector<Color>(); |
| 154 | } |
| 155 | |
| 156 | Vector<Color> colors; |
| 157 | colors.resize(instance_count); |
| 158 | |
| 159 | for (int i = 0; i < instance_count; i++) { |
| 160 | colors.set(i, get_instance_color(i)); |
| 161 | } |
| 162 | |
| 163 | return colors; |
| 164 | } |
| 165 | |
| 166 | void MultiMesh::_set_custom_data_array(const Vector<Color> &p_array) { |
| 167 | const Vector<Color> &custom_datas = p_array; |
| 168 | int len = custom_datas.size(); |
| 169 | if (len == 0) { |
| 170 | return; |
| 171 | } |
| 172 | ERR_FAIL_COND(len != instance_count); |
| 173 | |
| 174 | const Color *r = custom_datas.ptr(); |
| 175 | |
| 176 | for (int i = 0; i < len; i++) { |
| 177 | set_instance_custom_data(i, r[i]); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | Vector<Color> MultiMesh::_get_custom_data_array() const { |
| 182 | if (instance_count == 0 || !use_custom_data) { |
| 183 | return Vector<Color>(); |
| 184 | } |
| 185 | |
| 186 | Vector<Color> custom_datas; |
| 187 | custom_datas.resize(instance_count); |
| 188 | |
| 189 | for (int i = 0; i < instance_count; i++) { |
| 190 | custom_datas.set(i, get_instance_custom_data(i)); |
| 191 | } |
| 192 | |
| 193 | return custom_datas; |
| 194 | } |
| 195 | #endif // DISABLE_DEPRECATED |
| 196 | |
| 197 | void MultiMesh::set_buffer(const Vector<float> &p_buffer) { |
| 198 | RS::get_singleton()->multimesh_set_buffer(multimesh, p_buffer); |
| 199 | } |
| 200 | |
| 201 | Vector<float> MultiMesh::get_buffer() const { |
| 202 | return RS::get_singleton()->multimesh_get_buffer(multimesh); |
| 203 | } |
| 204 | |
| 205 | void MultiMesh::set_mesh(const Ref<Mesh> &p_mesh) { |
| 206 | mesh = p_mesh; |
| 207 | if (!mesh.is_null()) { |
| 208 | RenderingServer::get_singleton()->multimesh_set_mesh(multimesh, mesh->get_rid()); |
| 209 | } else { |
| 210 | RenderingServer::get_singleton()->multimesh_set_mesh(multimesh, RID()); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | Ref<Mesh> MultiMesh::get_mesh() const { |
| 215 | return mesh; |
| 216 | } |
| 217 | |
| 218 | void MultiMesh::set_instance_count(int p_count) { |
| 219 | ERR_FAIL_COND(p_count < 0); |
| 220 | RenderingServer::get_singleton()->multimesh_allocate_data(multimesh, p_count, RS::MultimeshTransformFormat(transform_format), use_colors, use_custom_data); |
| 221 | instance_count = p_count; |
| 222 | } |
| 223 | |
| 224 | int MultiMesh::get_instance_count() const { |
| 225 | return instance_count; |
| 226 | } |
| 227 | |
| 228 | void MultiMesh::set_visible_instance_count(int p_count) { |
| 229 | ERR_FAIL_COND(p_count < -1); |
| 230 | ERR_FAIL_COND(p_count > instance_count); |
| 231 | RenderingServer::get_singleton()->multimesh_set_visible_instances(multimesh, p_count); |
| 232 | visible_instance_count = p_count; |
| 233 | } |
| 234 | |
| 235 | int MultiMesh::get_visible_instance_count() const { |
| 236 | return visible_instance_count; |
| 237 | } |
| 238 | |
| 239 | void MultiMesh::set_instance_transform(int p_instance, const Transform3D &p_transform) { |
| 240 | RenderingServer::get_singleton()->multimesh_instance_set_transform(multimesh, p_instance, p_transform); |
| 241 | } |
| 242 | |
| 243 | void MultiMesh::set_instance_transform_2d(int p_instance, const Transform2D &p_transform) { |
| 244 | RenderingServer::get_singleton()->multimesh_instance_set_transform_2d(multimesh, p_instance, p_transform); |
| 245 | emit_changed(); |
| 246 | } |
| 247 | |
| 248 | Transform3D MultiMesh::get_instance_transform(int p_instance) const { |
| 249 | return RenderingServer::get_singleton()->multimesh_instance_get_transform(multimesh, p_instance); |
| 250 | } |
| 251 | |
| 252 | Transform2D MultiMesh::get_instance_transform_2d(int p_instance) const { |
| 253 | return RenderingServer::get_singleton()->multimesh_instance_get_transform_2d(multimesh, p_instance); |
| 254 | } |
| 255 | |
| 256 | void MultiMesh::set_instance_color(int p_instance, const Color &p_color) { |
| 257 | RenderingServer::get_singleton()->multimesh_instance_set_color(multimesh, p_instance, p_color); |
| 258 | } |
| 259 | |
| 260 | Color MultiMesh::get_instance_color(int p_instance) const { |
| 261 | return RenderingServer::get_singleton()->multimesh_instance_get_color(multimesh, p_instance); |
| 262 | } |
| 263 | |
| 264 | void MultiMesh::set_instance_custom_data(int p_instance, const Color &p_custom_data) { |
| 265 | RenderingServer::get_singleton()->multimesh_instance_set_custom_data(multimesh, p_instance, p_custom_data); |
| 266 | } |
| 267 | |
| 268 | Color MultiMesh::get_instance_custom_data(int p_instance) const { |
| 269 | return RenderingServer::get_singleton()->multimesh_instance_get_custom_data(multimesh, p_instance); |
| 270 | } |
| 271 | |
| 272 | AABB MultiMesh::get_aabb() const { |
| 273 | return RenderingServer::get_singleton()->multimesh_get_aabb(multimesh); |
| 274 | } |
| 275 | |
| 276 | RID MultiMesh::get_rid() const { |
| 277 | return multimesh; |
| 278 | } |
| 279 | |
| 280 | void MultiMesh::set_use_colors(bool p_enable) { |
| 281 | ERR_FAIL_COND(instance_count > 0); |
| 282 | use_colors = p_enable; |
| 283 | } |
| 284 | |
| 285 | bool MultiMesh::is_using_colors() const { |
| 286 | return use_colors; |
| 287 | } |
| 288 | |
| 289 | void MultiMesh::set_use_custom_data(bool p_enable) { |
| 290 | ERR_FAIL_COND(instance_count > 0); |
| 291 | use_custom_data = p_enable; |
| 292 | } |
| 293 | |
| 294 | bool MultiMesh::is_using_custom_data() const { |
| 295 | return use_custom_data; |
| 296 | } |
| 297 | |
| 298 | void MultiMesh::set_transform_format(TransformFormat p_transform_format) { |
| 299 | ERR_FAIL_COND(instance_count > 0); |
| 300 | transform_format = p_transform_format; |
| 301 | } |
| 302 | |
| 303 | MultiMesh::TransformFormat MultiMesh::get_transform_format() const { |
| 304 | return transform_format; |
| 305 | } |
| 306 | |
| 307 | void MultiMesh::_bind_methods() { |
| 308 | ClassDB::bind_method(D_METHOD("set_mesh" , "mesh" ), &MultiMesh::set_mesh); |
| 309 | ClassDB::bind_method(D_METHOD("get_mesh" ), &MultiMesh::get_mesh); |
| 310 | ClassDB::bind_method(D_METHOD("set_use_colors" , "enable" ), &MultiMesh::set_use_colors); |
| 311 | ClassDB::bind_method(D_METHOD("is_using_colors" ), &MultiMesh::is_using_colors); |
| 312 | ClassDB::bind_method(D_METHOD("set_use_custom_data" , "enable" ), &MultiMesh::set_use_custom_data); |
| 313 | ClassDB::bind_method(D_METHOD("is_using_custom_data" ), &MultiMesh::is_using_custom_data); |
| 314 | ClassDB::bind_method(D_METHOD("set_transform_format" , "format" ), &MultiMesh::set_transform_format); |
| 315 | ClassDB::bind_method(D_METHOD("get_transform_format" ), &MultiMesh::get_transform_format); |
| 316 | |
| 317 | ClassDB::bind_method(D_METHOD("set_instance_count" , "count" ), &MultiMesh::set_instance_count); |
| 318 | ClassDB::bind_method(D_METHOD("get_instance_count" ), &MultiMesh::get_instance_count); |
| 319 | ClassDB::bind_method(D_METHOD("set_visible_instance_count" , "count" ), &MultiMesh::set_visible_instance_count); |
| 320 | ClassDB::bind_method(D_METHOD("get_visible_instance_count" ), &MultiMesh::get_visible_instance_count); |
| 321 | ClassDB::bind_method(D_METHOD("set_instance_transform" , "instance" , "transform" ), &MultiMesh::set_instance_transform); |
| 322 | ClassDB::bind_method(D_METHOD("set_instance_transform_2d" , "instance" , "transform" ), &MultiMesh::set_instance_transform_2d); |
| 323 | ClassDB::bind_method(D_METHOD("get_instance_transform" , "instance" ), &MultiMesh::get_instance_transform); |
| 324 | ClassDB::bind_method(D_METHOD("get_instance_transform_2d" , "instance" ), &MultiMesh::get_instance_transform_2d); |
| 325 | ClassDB::bind_method(D_METHOD("set_instance_color" , "instance" , "color" ), &MultiMesh::set_instance_color); |
| 326 | ClassDB::bind_method(D_METHOD("get_instance_color" , "instance" ), &MultiMesh::get_instance_color); |
| 327 | ClassDB::bind_method(D_METHOD("set_instance_custom_data" , "instance" , "custom_data" ), &MultiMesh::set_instance_custom_data); |
| 328 | ClassDB::bind_method(D_METHOD("get_instance_custom_data" , "instance" ), &MultiMesh::get_instance_custom_data); |
| 329 | ClassDB::bind_method(D_METHOD("get_aabb" ), &MultiMesh::get_aabb); |
| 330 | |
| 331 | ClassDB::bind_method(D_METHOD("get_buffer" ), &MultiMesh::get_buffer); |
| 332 | ClassDB::bind_method(D_METHOD("set_buffer" , "buffer" ), &MultiMesh::set_buffer); |
| 333 | |
| 334 | ADD_PROPERTY(PropertyInfo(Variant::INT, "transform_format" , PROPERTY_HINT_ENUM, "2D,3D" ), "set_transform_format" , "get_transform_format" ); |
| 335 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_colors" ), "set_use_colors" , "is_using_colors" ); |
| 336 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_custom_data" ), "set_use_custom_data" , "is_using_custom_data" ); |
| 337 | ADD_PROPERTY(PropertyInfo(Variant::INT, "instance_count" , PROPERTY_HINT_RANGE, "0,16384,1,or_greater" ), "set_instance_count" , "get_instance_count" ); |
| 338 | ADD_PROPERTY(PropertyInfo(Variant::INT, "visible_instance_count" , PROPERTY_HINT_RANGE, "-1,16384,1,or_greater" ), "set_visible_instance_count" , "get_visible_instance_count" ); |
| 339 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh" , PROPERTY_HINT_RESOURCE_TYPE, "Mesh" ), "set_mesh" , "get_mesh" ); |
| 340 | ADD_PROPERTY(PropertyInfo(Variant::PACKED_FLOAT32_ARRAY, "buffer" , PROPERTY_HINT_NONE), "set_buffer" , "get_buffer" ); |
| 341 | |
| 342 | #ifndef DISABLE_DEPRECATED |
| 343 | // Kept for compatibility from 3.x to 4.0. |
| 344 | ClassDB::bind_method(D_METHOD("_set_transform_array" , "array" ), &MultiMesh::_set_transform_array); |
| 345 | ClassDB::bind_method(D_METHOD("_get_transform_array" ), &MultiMesh::_get_transform_array); |
| 346 | ClassDB::bind_method(D_METHOD("_set_transform_2d_array" , "array" ), &MultiMesh::_set_transform_2d_array); |
| 347 | ClassDB::bind_method(D_METHOD("_get_transform_2d_array" ), &MultiMesh::_get_transform_2d_array); |
| 348 | ClassDB::bind_method(D_METHOD("_set_color_array" , "array" ), &MultiMesh::_set_color_array); |
| 349 | ClassDB::bind_method(D_METHOD("_get_color_array" ), &MultiMesh::_get_color_array); |
| 350 | ClassDB::bind_method(D_METHOD("_set_custom_data_array" , "array" ), &MultiMesh::_set_custom_data_array); |
| 351 | ClassDB::bind_method(D_METHOD("_get_custom_data_array" ), &MultiMesh::_get_custom_data_array); |
| 352 | |
| 353 | ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR3_ARRAY, "transform_array" , PROPERTY_HINT_NONE, "" , PROPERTY_USAGE_NONE), "_set_transform_array" , "_get_transform_array" ); |
| 354 | ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR2_ARRAY, "transform_2d_array" , PROPERTY_HINT_NONE, "" , PROPERTY_USAGE_NONE), "_set_transform_2d_array" , "_get_transform_2d_array" ); |
| 355 | ADD_PROPERTY(PropertyInfo(Variant::PACKED_COLOR_ARRAY, "color_array" , PROPERTY_HINT_NONE, "" , PROPERTY_USAGE_NONE), "_set_color_array" , "_get_color_array" ); |
| 356 | ADD_PROPERTY(PropertyInfo(Variant::PACKED_COLOR_ARRAY, "custom_data_array" , PROPERTY_HINT_NONE, "" , PROPERTY_USAGE_NONE), "_set_custom_data_array" , "_get_custom_data_array" ); |
| 357 | #endif |
| 358 | |
| 359 | BIND_ENUM_CONSTANT(TRANSFORM_2D); |
| 360 | BIND_ENUM_CONSTANT(TRANSFORM_3D); |
| 361 | } |
| 362 | |
| 363 | MultiMesh::MultiMesh() { |
| 364 | multimesh = RenderingServer::get_singleton()->multimesh_create(); |
| 365 | } |
| 366 | |
| 367 | MultiMesh::~MultiMesh() { |
| 368 | ERR_FAIL_NULL(RenderingServer::get_singleton()); |
| 369 | RenderingServer::get_singleton()->free(multimesh); |
| 370 | } |
| 371 | |