| 1 | /**************************************************************************/ |
| 2 | /* mesh.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 "mesh.h" |
| 32 | |
| 33 | #include "core/math/convex_hull.h" |
| 34 | #include "core/templates/pair.h" |
| 35 | #include "scene/resources/surface_tool.h" |
| 36 | |
| 37 | #ifndef PHYSICS_3D_DISABLED |
| 38 | #include "scene/resources/3d/concave_polygon_shape_3d.h" |
| 39 | #include "scene/resources/3d/convex_polygon_shape_3d.h" |
| 40 | #endif // PHYSICS_3D_DISABLED |
| 41 | |
| 42 | void MeshConvexDecompositionSettings::set_max_concavity(real_t p_max_concavity) { |
| 43 | max_concavity = CLAMP(p_max_concavity, 0.001, 1.0); |
| 44 | } |
| 45 | |
| 46 | real_t MeshConvexDecompositionSettings::get_max_concavity() const { |
| 47 | return max_concavity; |
| 48 | } |
| 49 | |
| 50 | void MeshConvexDecompositionSettings::set_symmetry_planes_clipping_bias(real_t p_symmetry_planes_clipping_bias) { |
| 51 | symmetry_planes_clipping_bias = CLAMP(p_symmetry_planes_clipping_bias, 0.0, 1.0); |
| 52 | } |
| 53 | |
| 54 | real_t MeshConvexDecompositionSettings::get_symmetry_planes_clipping_bias() const { |
| 55 | return symmetry_planes_clipping_bias; |
| 56 | } |
| 57 | |
| 58 | void MeshConvexDecompositionSettings::set_revolution_axes_clipping_bias(real_t p_revolution_axes_clipping_bias) { |
| 59 | revolution_axes_clipping_bias = CLAMP(p_revolution_axes_clipping_bias, 0.0, 1.0); |
| 60 | } |
| 61 | |
| 62 | real_t MeshConvexDecompositionSettings::get_revolution_axes_clipping_bias() const { |
| 63 | return revolution_axes_clipping_bias; |
| 64 | } |
| 65 | |
| 66 | void MeshConvexDecompositionSettings::set_min_volume_per_convex_hull(real_t p_min_volume_per_convex_hull) { |
| 67 | min_volume_per_convex_hull = CLAMP(p_min_volume_per_convex_hull, 0.0001, 0.01); |
| 68 | } |
| 69 | |
| 70 | real_t MeshConvexDecompositionSettings::get_min_volume_per_convex_hull() const { |
| 71 | return min_volume_per_convex_hull; |
| 72 | } |
| 73 | |
| 74 | void MeshConvexDecompositionSettings::set_resolution(uint32_t p_resolution) { |
| 75 | resolution = p_resolution < 10'000 ? 10'000 : (p_resolution > 100'000 ? 100'000 : p_resolution); |
| 76 | } |
| 77 | |
| 78 | uint32_t MeshConvexDecompositionSettings::get_resolution() const { |
| 79 | return resolution; |
| 80 | } |
| 81 | |
| 82 | void MeshConvexDecompositionSettings::set_max_num_vertices_per_convex_hull(uint32_t p_max_num_vertices_per_convex_hull) { |
| 83 | max_num_vertices_per_convex_hull = p_max_num_vertices_per_convex_hull < 4 ? 4 : (p_max_num_vertices_per_convex_hull > 1024 ? 1024 : p_max_num_vertices_per_convex_hull); |
| 84 | } |
| 85 | |
| 86 | uint32_t MeshConvexDecompositionSettings::get_max_num_vertices_per_convex_hull() const { |
| 87 | return max_num_vertices_per_convex_hull; |
| 88 | } |
| 89 | |
| 90 | void MeshConvexDecompositionSettings::set_plane_downsampling(uint32_t p_plane_downsampling) { |
| 91 | plane_downsampling = p_plane_downsampling < 1 ? 1 : (p_plane_downsampling > 16 ? 16 : p_plane_downsampling); |
| 92 | } |
| 93 | |
| 94 | uint32_t MeshConvexDecompositionSettings::get_plane_downsampling() const { |
| 95 | return plane_downsampling; |
| 96 | } |
| 97 | |
| 98 | void MeshConvexDecompositionSettings::set_convex_hull_downsampling(uint32_t p_convex_hull_downsampling) { |
| 99 | convex_hull_downsampling = p_convex_hull_downsampling < 1 ? 1 : (p_convex_hull_downsampling > 16 ? 16 : p_convex_hull_downsampling); |
| 100 | } |
| 101 | |
| 102 | uint32_t MeshConvexDecompositionSettings::get_convex_hull_downsampling() const { |
| 103 | return convex_hull_downsampling; |
| 104 | } |
| 105 | |
| 106 | void MeshConvexDecompositionSettings::set_normalize_mesh(bool p_normalize_mesh) { |
| 107 | normalize_mesh = p_normalize_mesh; |
| 108 | } |
| 109 | |
| 110 | bool MeshConvexDecompositionSettings::get_normalize_mesh() const { |
| 111 | return normalize_mesh; |
| 112 | } |
| 113 | |
| 114 | void MeshConvexDecompositionSettings::set_mode(Mode p_mode) { |
| 115 | mode = p_mode; |
| 116 | } |
| 117 | |
| 118 | MeshConvexDecompositionSettings::Mode MeshConvexDecompositionSettings::get_mode() const { |
| 119 | return mode; |
| 120 | } |
| 121 | |
| 122 | void MeshConvexDecompositionSettings::set_convex_hull_approximation(bool p_convex_hull_approximation) { |
| 123 | convex_hull_approximation = p_convex_hull_approximation; |
| 124 | } |
| 125 | |
| 126 | bool MeshConvexDecompositionSettings::get_convex_hull_approximation() const { |
| 127 | return convex_hull_approximation; |
| 128 | } |
| 129 | |
| 130 | void MeshConvexDecompositionSettings::set_max_convex_hulls(uint32_t p_max_convex_hulls) { |
| 131 | max_convex_hulls = p_max_convex_hulls < 1 ? 1 : (p_max_convex_hulls > 32 ? 32 : p_max_convex_hulls); |
| 132 | } |
| 133 | |
| 134 | uint32_t MeshConvexDecompositionSettings::get_max_convex_hulls() const { |
| 135 | return max_convex_hulls; |
| 136 | } |
| 137 | |
| 138 | void MeshConvexDecompositionSettings::set_project_hull_vertices(bool p_project_hull_vertices) { |
| 139 | project_hull_vertices = p_project_hull_vertices; |
| 140 | } |
| 141 | |
| 142 | bool MeshConvexDecompositionSettings::get_project_hull_vertices() const { |
| 143 | return project_hull_vertices; |
| 144 | } |
| 145 | |
| 146 | void MeshConvexDecompositionSettings::_bind_methods() { |
| 147 | ClassDB::bind_method(D_METHOD("set_max_concavity", "max_concavity"), &MeshConvexDecompositionSettings::set_max_concavity); |
| 148 | ClassDB::bind_method(D_METHOD("get_max_concavity"), &MeshConvexDecompositionSettings::get_max_concavity); |
| 149 | |
| 150 | ClassDB::bind_method(D_METHOD("set_symmetry_planes_clipping_bias", "symmetry_planes_clipping_bias"), &MeshConvexDecompositionSettings::set_symmetry_planes_clipping_bias); |
| 151 | ClassDB::bind_method(D_METHOD("get_symmetry_planes_clipping_bias"), &MeshConvexDecompositionSettings::get_symmetry_planes_clipping_bias); |
| 152 | |
| 153 | ClassDB::bind_method(D_METHOD("set_revolution_axes_clipping_bias", "revolution_axes_clipping_bias"), &MeshConvexDecompositionSettings::set_revolution_axes_clipping_bias); |
| 154 | ClassDB::bind_method(D_METHOD("get_revolution_axes_clipping_bias"), &MeshConvexDecompositionSettings::get_revolution_axes_clipping_bias); |
| 155 | |
| 156 | ClassDB::bind_method(D_METHOD("set_min_volume_per_convex_hull", "min_volume_per_convex_hull"), &MeshConvexDecompositionSettings::set_min_volume_per_convex_hull); |
| 157 | ClassDB::bind_method(D_METHOD("get_min_volume_per_convex_hull"), &MeshConvexDecompositionSettings::get_min_volume_per_convex_hull); |
| 158 | |
| 159 | ClassDB::bind_method(D_METHOD("set_resolution", "min_volume_per_convex_hull"), &MeshConvexDecompositionSettings::set_resolution); |
| 160 | ClassDB::bind_method(D_METHOD("get_resolution"), &MeshConvexDecompositionSettings::get_resolution); |
| 161 | |
| 162 | ClassDB::bind_method(D_METHOD("set_max_num_vertices_per_convex_hull", "max_num_vertices_per_convex_hull"), &MeshConvexDecompositionSettings::set_max_num_vertices_per_convex_hull); |
| 163 | ClassDB::bind_method(D_METHOD("get_max_num_vertices_per_convex_hull"), &MeshConvexDecompositionSettings::get_max_num_vertices_per_convex_hull); |
| 164 | |
| 165 | ClassDB::bind_method(D_METHOD("set_plane_downsampling", "plane_downsampling"), &MeshConvexDecompositionSettings::set_plane_downsampling); |
| 166 | ClassDB::bind_method(D_METHOD("get_plane_downsampling"), &MeshConvexDecompositionSettings::get_plane_downsampling); |
| 167 | |
| 168 | ClassDB::bind_method(D_METHOD("set_convex_hull_downsampling", "convex_hull_downsampling"), &MeshConvexDecompositionSettings::set_convex_hull_downsampling); |
| 169 | ClassDB::bind_method(D_METHOD("get_convex_hull_downsampling"), &MeshConvexDecompositionSettings::get_convex_hull_downsampling); |
| 170 | |
| 171 | ClassDB::bind_method(D_METHOD("set_normalize_mesh", "normalize_mesh"), &MeshConvexDecompositionSettings::set_normalize_mesh); |
| 172 | ClassDB::bind_method(D_METHOD("get_normalize_mesh"), &MeshConvexDecompositionSettings::get_normalize_mesh); |
| 173 | |
| 174 | ClassDB::bind_method(D_METHOD("set_mode", "mode"), &MeshConvexDecompositionSettings::set_mode); |
| 175 | ClassDB::bind_method(D_METHOD("get_mode"), &MeshConvexDecompositionSettings::get_mode); |
| 176 | |
| 177 | ClassDB::bind_method(D_METHOD("set_convex_hull_approximation", "convex_hull_approximation"), &MeshConvexDecompositionSettings::set_convex_hull_approximation); |
| 178 | ClassDB::bind_method(D_METHOD("get_convex_hull_approximation"), &MeshConvexDecompositionSettings::get_convex_hull_approximation); |
| 179 | |
| 180 | ClassDB::bind_method(D_METHOD("set_max_convex_hulls", "max_convex_hulls"), &MeshConvexDecompositionSettings::set_max_convex_hulls); |
| 181 | ClassDB::bind_method(D_METHOD("get_max_convex_hulls"), &MeshConvexDecompositionSettings::get_max_convex_hulls); |
| 182 | |
| 183 | ClassDB::bind_method(D_METHOD("set_project_hull_vertices", "project_hull_vertices"), &MeshConvexDecompositionSettings::set_project_hull_vertices); |
| 184 | ClassDB::bind_method(D_METHOD("get_project_hull_vertices"), &MeshConvexDecompositionSettings::get_project_hull_vertices); |
| 185 | |
| 186 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "max_concavity", PROPERTY_HINT_RANGE, "0.001,1.0,0.001"), "set_max_concavity", "get_max_concavity"); |
| 187 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "symmetry_planes_clipping_bias", PROPERTY_HINT_RANGE, "0.0,1.0,0.01"), "set_symmetry_planes_clipping_bias", "get_symmetry_planes_clipping_bias"); |
| 188 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "revolution_axes_clipping_bias", PROPERTY_HINT_RANGE, "0.0,1.0,0.01"), "set_revolution_axes_clipping_bias", "get_revolution_axes_clipping_bias"); |
| 189 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "min_volume_per_convex_hull", PROPERTY_HINT_RANGE, "0.0001,0.01,0.0001"), "set_min_volume_per_convex_hull", "get_min_volume_per_convex_hull"); |
| 190 | ADD_PROPERTY(PropertyInfo(Variant::INT, "resolution"), "set_resolution", "get_resolution"); |
| 191 | ADD_PROPERTY(PropertyInfo(Variant::INT, "max_num_vertices_per_convex_hull"), "set_max_num_vertices_per_convex_hull", "get_max_num_vertices_per_convex_hull"); |
| 192 | ADD_PROPERTY(PropertyInfo(Variant::INT, "plane_downsampling", PROPERTY_HINT_RANGE, "1,16,1"), "set_plane_downsampling", "get_plane_downsampling"); |
| 193 | ADD_PROPERTY(PropertyInfo(Variant::INT, "convex_hull_downsampling", PROPERTY_HINT_RANGE, "1,16,1"), "set_convex_hull_downsampling", "get_convex_hull_downsampling"); |
| 194 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "normalize_mesh"), "set_normalize_mesh", "get_normalize_mesh"); |
| 195 | ADD_PROPERTY(PropertyInfo(Variant::INT, "mode", PROPERTY_HINT_ENUM, "Voxel,Tetrahedron"), "set_mode", "get_mode"); |
| 196 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "convex_hull_approximation"), "set_convex_hull_approximation", "get_convex_hull_approximation"); |
| 197 | ADD_PROPERTY(PropertyInfo(Variant::INT, "max_convex_hulls"), "set_max_convex_hulls", "get_max_convex_hulls"); |
| 198 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "project_hull_vertices"), "set_project_hull_vertices", "get_project_hull_vertices"); |
| 199 | |
| 200 | BIND_ENUM_CONSTANT(CONVEX_DECOMPOSITION_MODE_VOXEL); |
| 201 | BIND_ENUM_CONSTANT(CONVEX_DECOMPOSITION_MODE_TETRAHEDRON); |
| 202 | } |
| 203 | |
| 204 | #ifndef PHYSICS_3D_DISABLED |
| 205 | Mesh::ConvexDecompositionFunc Mesh::convex_decomposition_function = nullptr; |
| 206 | #endif // PHYSICS_3D_DISABLED |
| 207 | |
| 208 | int Mesh::get_surface_count() const { |
| 209 | int ret = 0; |
| 210 | GDVIRTUAL_CALL(_get_surface_count, ret); |
| 211 | return ret; |
| 212 | } |
| 213 | |
| 214 | int Mesh::surface_get_array_len(int p_idx) const { |
| 215 | int ret = 0; |
| 216 | GDVIRTUAL_CALL(_surface_get_array_len, p_idx, ret); |
| 217 | return ret; |
| 218 | } |
| 219 | |
| 220 | int Mesh::surface_get_array_index_len(int p_idx) const { |
| 221 | int ret = 0; |
| 222 | GDVIRTUAL_CALL(_surface_get_array_index_len, p_idx, ret); |
| 223 | return ret; |
| 224 | } |
| 225 | |
| 226 | Array Mesh::surface_get_arrays(int p_surface) const { |
| 227 | Array ret; |
| 228 | GDVIRTUAL_CALL(_surface_get_arrays, p_surface, ret); |
| 229 | return ret; |
| 230 | } |
| 231 | |
| 232 | TypedArray<Array> Mesh::surface_get_blend_shape_arrays(int p_surface) const { |
| 233 | TypedArray<Array> ret; |
| 234 | GDVIRTUAL_CALL(_surface_get_blend_shape_arrays, p_surface, ret); |
| 235 | return ret; |
| 236 | } |
| 237 | |
| 238 | Dictionary Mesh::surface_get_lods(int p_surface) const { |
| 239 | Dictionary ret; |
| 240 | GDVIRTUAL_CALL(_surface_get_lods, p_surface, ret); |
| 241 | return ret; |
| 242 | } |
| 243 | |
| 244 | BitField<Mesh::ArrayFormat> Mesh::surface_get_format(int p_idx) const { |
| 245 | uint32_t ret = 0; |
| 246 | GDVIRTUAL_CALL(_surface_get_format, p_idx, ret); |
| 247 | return ret; |
| 248 | } |
| 249 | |
| 250 | Mesh::PrimitiveType Mesh::surface_get_primitive_type(int p_idx) const { |
| 251 | uint32_t ret = PRIMITIVE_MAX; |
| 252 | GDVIRTUAL_CALL(_surface_get_primitive_type, p_idx, ret); |
| 253 | return (Mesh::PrimitiveType)ret; |
| 254 | } |
| 255 | |
| 256 | void Mesh::surface_set_material(int p_idx, const Ref<Material> &p_material) { |
| 257 | GDVIRTUAL_CALL(_surface_set_material, p_idx, p_material); |
| 258 | } |
| 259 | |
| 260 | Ref<Material> Mesh::surface_get_material(int p_idx) const { |
| 261 | Ref<Material> ret; |
| 262 | GDVIRTUAL_CALL(_surface_get_material, p_idx, ret); |
| 263 | return ret; |
| 264 | } |
| 265 | |
| 266 | int Mesh::get_blend_shape_count() const { |
| 267 | int ret = 0; |
| 268 | GDVIRTUAL_CALL(_get_blend_shape_count, ret); |
| 269 | return ret; |
| 270 | } |
| 271 | |
| 272 | StringName Mesh::get_blend_shape_name(int p_index) const { |
| 273 | StringName ret; |
| 274 | GDVIRTUAL_CALL(_get_blend_shape_name, p_index, ret); |
| 275 | return ret; |
| 276 | } |
| 277 | |
| 278 | void Mesh::set_blend_shape_name(int p_index, const StringName &p_name) { |
| 279 | GDVIRTUAL_CALL(_set_blend_shape_name, p_index, p_name); |
| 280 | } |
| 281 | |
| 282 | AABB Mesh::get_aabb() const { |
| 283 | AABB ret; |
| 284 | GDVIRTUAL_CALL(_get_aabb, ret); |
| 285 | return ret; |
| 286 | } |
| 287 | |
| 288 | Ref<TriangleMesh> Mesh::generate_triangle_mesh() const { |
| 289 | if (triangle_mesh.is_valid()) { |
| 290 | return triangle_mesh; |
| 291 | } |
| 292 | |
| 293 | int faces_size = 0; |
| 294 | |
| 295 | for (int i = 0; i < get_surface_count(); i++) { |
| 296 | switch (surface_get_primitive_type(i)) { |
| 297 | case PRIMITIVE_TRIANGLES: { |
| 298 | int len = (surface_get_format(i) & ARRAY_FORMAT_INDEX) ? surface_get_array_index_len(i) : surface_get_array_len(i); |
| 299 | // Don't error if zero, it's valid (we'll just skip it later). |
| 300 | ERR_CONTINUE_MSG((len % 3) != 0, vformat("Ignoring surface %d, incorrect %s count: %d (for PRIMITIVE_TRIANGLES).", i, (surface_get_format(i) & ARRAY_FORMAT_INDEX) ? "index" : "vertex", len)); |
| 301 | faces_size += len; |
| 302 | } break; |
| 303 | case PRIMITIVE_TRIANGLE_STRIP: { |
| 304 | int len = (surface_get_format(i) & ARRAY_FORMAT_INDEX) ? surface_get_array_index_len(i) : surface_get_array_len(i); |
| 305 | // Don't error if zero, it's valid (we'll just skip it later). |
| 306 | ERR_CONTINUE_MSG(len != 0 && len < 3, vformat("Ignoring surface %d, incorrect %s count: %d (for PRIMITIVE_TRIANGLE_STRIP).", i, (surface_get_format(i) & ARRAY_FORMAT_INDEX) ? "index" : "vertex", len)); |
| 307 | faces_size += (len == 0) ? 0 : (len - 2) * 3; |
| 308 | } break; |
| 309 | default: { |
| 310 | } break; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | if (faces_size == 0) { |
| 315 | return triangle_mesh; |
| 316 | } |
| 317 | |
| 318 | Vector<Vector3> faces; |
| 319 | faces.resize(faces_size); |
| 320 | Vector<int32_t> surface_indices; |
| 321 | surface_indices.resize(faces_size / 3); |
| 322 | Vector3 *facesw = faces.ptrw(); |
| 323 | int32_t *surface_indicesw = surface_indices.ptrw(); |
| 324 | |
| 325 | int widx = 0; |
| 326 | |
| 327 | for (int i = 0; i < get_surface_count(); i++) { |
| 328 | Mesh::PrimitiveType primitive = surface_get_primitive_type(i); |
| 329 | if (primitive != PRIMITIVE_TRIANGLES && primitive != PRIMITIVE_TRIANGLE_STRIP) { |
| 330 | continue; |
| 331 | } |
| 332 | int len = (surface_get_format(i) & ARRAY_FORMAT_INDEX) ? surface_get_array_index_len(i) : surface_get_array_len(i); |
| 333 | if ((primitive == PRIMITIVE_TRIANGLES && (len == 0 || (len % 3) != 0)) || |
| 334 | (primitive == PRIMITIVE_TRIANGLE_STRIP && len < 3) || |
| 335 | (surface_get_format(i) & ARRAY_FLAG_USES_EMPTY_VERTEX_ARRAY)) { |
| 336 | // Error was already shown, just skip (including zero). |
| 337 | continue; |
| 338 | } |
| 339 | |
| 340 | Array a = surface_get_arrays(i); |
| 341 | ERR_FAIL_COND_V(a.is_empty(), Ref<TriangleMesh>()); |
| 342 | |
| 343 | int vc = surface_get_array_len(i); |
| 344 | Vector<Vector3> vertices = a[ARRAY_VERTEX]; |
| 345 | ERR_FAIL_COND_V(vertices.is_empty(), Ref<TriangleMesh>()); |
| 346 | const Vector3 *vr = vertices.ptr(); |
| 347 | |
| 348 | int32_t from_index = widx / 3; |
| 349 | |
| 350 | if (surface_get_format(i) & ARRAY_FORMAT_INDEX) { |
| 351 | int ic = surface_get_array_index_len(i); |
| 352 | Vector<int> indices = a[ARRAY_INDEX]; |
| 353 | const int *ir = indices.ptr(); |
| 354 | |
| 355 | if (primitive == PRIMITIVE_TRIANGLES) { |
| 356 | for (int j = 0; j < ic; j++) { |
| 357 | int index = ir[j]; |
| 358 | ERR_FAIL_COND_V(index >= vc, Ref<TriangleMesh>()); |
| 359 | facesw[widx++] = vr[index]; |
| 360 | } |
| 361 | } else { // PRIMITIVE_TRIANGLE_STRIP |
| 362 | for (int j = 2; j < ic; j++) { |
| 363 | if (j % 2 == 0) { |
| 364 | facesw[widx++] = vr[ir[j - 2]]; |
| 365 | facesw[widx++] = vr[ir[j - 1]]; |
| 366 | facesw[widx++] = vr[ir[j]]; |
| 367 | } else { |
| 368 | facesw[widx++] = vr[ir[j - 2]]; |
| 369 | facesw[widx++] = vr[ir[j]]; |
| 370 | facesw[widx++] = vr[ir[j - 1]]; |
| 371 | } |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | } else { |
| 376 | if (primitive == PRIMITIVE_TRIANGLES) { |
| 377 | for (int j = 0; j < vc; j++) { |
| 378 | facesw[widx++] = vr[j]; |
| 379 | } |
| 380 | } else { // PRIMITIVE_TRIANGLE_STRIP |
| 381 | for (int j = 2; j < vc; j++) { |
| 382 | if (j % 2 == 0) { |
| 383 | facesw[widx++] = vr[j - 2]; |
| 384 | facesw[widx++] = vr[j - 1]; |
| 385 | facesw[widx++] = vr[j]; |
| 386 | } else { |
| 387 | facesw[widx++] = vr[j - 2]; |
| 388 | facesw[widx++] = vr[j]; |
| 389 | facesw[widx++] = vr[j - 1]; |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | int32_t to_index = widx / 3; |
| 396 | |
| 397 | for (int j = from_index; j < to_index; j++) { |
| 398 | surface_indicesw[j] = i; |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | triangle_mesh.instantiate(); |
| 403 | triangle_mesh->create(faces); |
| 404 | |
| 405 | return triangle_mesh; |
| 406 | } |
| 407 | |
| 408 | Ref<TriangleMesh> Mesh::generate_surface_triangle_mesh(int p_surface) const { |
| 409 | ERR_FAIL_INDEX_V(p_surface, get_surface_count(), Ref<TriangleMesh>()); |
| 410 | |
| 411 | if (surface_triangle_meshes.size() != get_surface_count()) { |
| 412 | surface_triangle_meshes.resize(get_surface_count()); |
| 413 | } |
| 414 | |
| 415 | if (surface_triangle_meshes[p_surface].is_valid()) { |
| 416 | return surface_triangle_meshes[p_surface]; |
| 417 | } |
| 418 | |
| 419 | int facecount = 0; |
| 420 | |
| 421 | if (surface_get_primitive_type(p_surface) != PRIMITIVE_TRIANGLES) { |
| 422 | return Ref<TriangleMesh>(); |
| 423 | } |
| 424 | |
| 425 | if (surface_get_format(p_surface) & ARRAY_FORMAT_INDEX) { |
| 426 | facecount += surface_get_array_index_len(p_surface); |
| 427 | } else { |
| 428 | facecount += surface_get_array_len(p_surface); |
| 429 | } |
| 430 | |
| 431 | Vector<Vector3> faces; |
| 432 | faces.resize(facecount); |
| 433 | Vector3 *facesw = faces.ptrw(); |
| 434 | |
| 435 | Array a = surface_get_arrays(p_surface); |
| 436 | ERR_FAIL_COND_V(a.is_empty(), Ref<TriangleMesh>()); |
| 437 | |
| 438 | int vc = surface_get_array_len(p_surface); |
| 439 | Vector<Vector3> vertices = a[ARRAY_VERTEX]; |
| 440 | const Vector3 *vr = vertices.ptr(); |
| 441 | int widx = 0; |
| 442 | |
| 443 | if (surface_get_format(p_surface) & ARRAY_FORMAT_INDEX) { |
| 444 | int ic = surface_get_array_index_len(p_surface); |
| 445 | Vector<int> indices = a[ARRAY_INDEX]; |
| 446 | const int *ir = indices.ptr(); |
| 447 | |
| 448 | for (int j = 0; j < ic; j++) { |
| 449 | int index = ir[j]; |
| 450 | facesw[widx++] = vr[index]; |
| 451 | } |
| 452 | |
| 453 | } else { |
| 454 | for (int j = 0; j < vc; j++) { |
| 455 | facesw[widx++] = vr[j]; |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | Ref<TriangleMesh> tr_mesh = Ref<TriangleMesh>(memnew(TriangleMesh)); |
| 460 | tr_mesh->create(faces); |
| 461 | surface_triangle_meshes.set(p_surface, tr_mesh); |
| 462 | |
| 463 | return tr_mesh; |
| 464 | } |
| 465 | |
| 466 | void Mesh::generate_debug_mesh_lines(Vector<Vector3> &r_lines) { |
| 467 | if (debug_lines.size() > 0) { |
| 468 | r_lines = debug_lines; |
| 469 | return; |
| 470 | } |
| 471 | |
| 472 | Ref<TriangleMesh> tm = generate_triangle_mesh(); |
| 473 | if (tm.is_null()) { |
| 474 | return; |
| 475 | } |
| 476 | |
| 477 | Vector<int> triangle_indices; |
| 478 | tm->get_indices(&triangle_indices); |
| 479 | const int triangles_num = tm->get_triangles().size(); |
| 480 | Vector<Vector3> vertices = tm->get_vertices(); |
| 481 | |
| 482 | debug_lines.resize(tm->get_triangles().size() * 6); // 3 lines x 2 points each line |
| 483 | |
| 484 | const int *ind_r = triangle_indices.ptr(); |
| 485 | const Vector3 *ver_r = vertices.ptr(); |
| 486 | for (int j = 0, x = 0, i = 0; i < triangles_num; j += 6, x += 3, ++i) { |
| 487 | // Triangle line 1 |
| 488 | debug_lines.write[j + 0] = ver_r[ind_r[x + 0]]; |
| 489 | debug_lines.write[j + 1] = ver_r[ind_r[x + 1]]; |
| 490 | |
| 491 | // Triangle line 2 |
| 492 | debug_lines.write[j + 2] = ver_r[ind_r[x + 1]]; |
| 493 | debug_lines.write[j + 3] = ver_r[ind_r[x + 2]]; |
| 494 | |
| 495 | // Triangle line 3 |
| 496 | debug_lines.write[j + 4] = ver_r[ind_r[x + 2]]; |
| 497 | debug_lines.write[j + 5] = ver_r[ind_r[x + 0]]; |
| 498 | } |
| 499 | |
| 500 | r_lines = debug_lines; |
| 501 | } |
| 502 | |
| 503 | void Mesh::generate_debug_mesh_indices(Vector<Vector3> &r_points) { |
| 504 | Ref<TriangleMesh> tm = generate_triangle_mesh(); |
| 505 | if (tm.is_null()) { |
| 506 | return; |
| 507 | } |
| 508 | |
| 509 | Vector<Vector3> vertices = tm->get_vertices(); |
| 510 | |
| 511 | int vertices_size = vertices.size(); |
| 512 | r_points.resize(vertices_size); |
| 513 | for (int i = 0; i < vertices_size; ++i) { |
| 514 | r_points.write[i] = vertices[i]; |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | Vector<Vector3> Mesh::_get_faces() const { |
| 519 | return Variant(get_faces()); |
| 520 | } |
| 521 | |
| 522 | Vector<Face3> Mesh::get_faces() const { |
| 523 | Ref<TriangleMesh> tm = generate_triangle_mesh(); |
| 524 | if (tm.is_valid()) { |
| 525 | return tm->get_faces(); |
| 526 | } |
| 527 | return Vector<Face3>(); |
| 528 | } |
| 529 | |
| 530 | Vector<Face3> Mesh::get_surface_faces(int p_surface) const { |
| 531 | Ref<TriangleMesh> tm = generate_surface_triangle_mesh(p_surface); |
| 532 | if (tm.is_valid()) { |
| 533 | return tm->get_faces(); |
| 534 | } |
| 535 | return Vector<Face3>(); |
| 536 | } |
| 537 | |
| 538 | #ifndef PHYSICS_3D_DISABLED |
| 539 | Ref<ConvexPolygonShape3D> Mesh::create_convex_shape(bool p_clean, bool p_simplify) const { |
| 540 | if (p_simplify) { |
| 541 | Ref<MeshConvexDecompositionSettings> settings = Ref<MeshConvexDecompositionSettings>(); |
| 542 | settings.instantiate(); |
| 543 | settings->set_max_convex_hulls(1); |
| 544 | settings->set_max_concavity(1.0); |
| 545 | Vector<Ref<Shape3D>> decomposed = convex_decompose(settings); |
| 546 | if (decomposed.size() == 1) { |
| 547 | return decomposed[0]; |
| 548 | } else { |
| 549 | ERR_PRINT("Convex shape simplification failed, falling back to simpler process."); |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | Vector<Vector3> vertices; |
| 554 | for (int i = 0; i < get_surface_count(); i++) { |
| 555 | Array a = surface_get_arrays(i); |
| 556 | ERR_FAIL_COND_V(a.is_empty(), Ref<ConvexPolygonShape3D>()); |
| 557 | Vector<Vector3> v = a[ARRAY_VERTEX]; |
| 558 | vertices.append_array(v); |
| 559 | } |
| 560 | |
| 561 | Ref<ConvexPolygonShape3D> shape = memnew(ConvexPolygonShape3D); |
| 562 | |
| 563 | if (p_clean) { |
| 564 | Geometry3D::MeshData md; |
| 565 | Error err = ConvexHullComputer::convex_hull(vertices, md); |
| 566 | if (err == OK) { |
| 567 | shape->set_points(Vector<Vector3>(md.vertices)); |
| 568 | return shape; |
| 569 | } else { |
| 570 | ERR_PRINT("Convex shape cleaning failed, falling back to simpler process."); |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | shape->set_points(vertices); |
| 575 | return shape; |
| 576 | } |
| 577 | |
| 578 | Ref<ConcavePolygonShape3D> Mesh::create_trimesh_shape() const { |
| 579 | Vector<Face3> faces = get_faces(); |
| 580 | if (faces.is_empty()) { |
| 581 | return Ref<ConcavePolygonShape3D>(); |
| 582 | } |
| 583 | |
| 584 | Vector<Vector3> face_points; |
| 585 | face_points.resize(faces.size() * 3); |
| 586 | |
| 587 | for (int i = 0; i < face_points.size(); i += 3) { |
| 588 | Face3 f = faces.get(i / 3); |
| 589 | face_points.set(i, f.vertex[0]); |
| 590 | face_points.set(i + 1, f.vertex[1]); |
| 591 | face_points.set(i + 2, f.vertex[2]); |
| 592 | } |
| 593 | |
| 594 | Ref<ConcavePolygonShape3D> shape = memnew(ConcavePolygonShape3D); |
| 595 | shape->set_faces(face_points); |
| 596 | return shape; |
| 597 | } |
| 598 | #endif // PHYSICS_3D_DISABLED |
| 599 | |
| 600 | Ref<Mesh> Mesh::create_outline(float p_margin) const { |
| 601 | Array arrays; |
| 602 | int index_accum = 0; |
| 603 | for (int i = 0; i < get_surface_count(); i++) { |
| 604 | if (surface_get_primitive_type(i) != PRIMITIVE_TRIANGLES) { |
| 605 | continue; |
| 606 | } |
| 607 | |
| 608 | Array a = surface_get_arrays(i); |
| 609 | ERR_FAIL_COND_V(a.is_empty(), Ref<ArrayMesh>()); |
| 610 | |
| 611 | if (i == 0) { |
| 612 | arrays = a; |
| 613 | Vector<Vector3> v = a[ARRAY_VERTEX]; |
| 614 | index_accum += v.size(); |
| 615 | } else { |
| 616 | int vcount = 0; |
| 617 | for (int j = 0; j < arrays.size(); j++) { |
| 618 | if (arrays[j].get_type() == Variant::NIL || a[j].get_type() == Variant::NIL) { |
| 619 | //mismatch, do not use |
| 620 | arrays[j] = Variant(); |
| 621 | continue; |
| 622 | } |
| 623 | |
| 624 | switch (j) { |
| 625 | case ARRAY_VERTEX: |
| 626 | case ARRAY_NORMAL: { |
| 627 | Vector<Vector3> dst = arrays[j]; |
| 628 | Vector<Vector3> src = a[j]; |
| 629 | if (j == ARRAY_VERTEX) { |
| 630 | vcount = src.size(); |
| 631 | } |
| 632 | if (dst.is_empty() || src.is_empty()) { |
| 633 | arrays[j] = Variant(); |
| 634 | continue; |
| 635 | } |
| 636 | dst.append_array(src); |
| 637 | arrays[j] = dst; |
| 638 | } break; |
| 639 | case ARRAY_TANGENT: |
| 640 | case ARRAY_BONES: |
| 641 | case ARRAY_WEIGHTS: { |
| 642 | Vector<real_t> dst = arrays[j]; |
| 643 | Vector<real_t> src = a[j]; |
| 644 | if (dst.is_empty() || src.is_empty()) { |
| 645 | arrays[j] = Variant(); |
| 646 | continue; |
| 647 | } |
| 648 | dst.append_array(src); |
| 649 | arrays[j] = dst; |
| 650 | |
| 651 | } break; |
| 652 | case ARRAY_COLOR: { |
| 653 | Vector<Color> dst = arrays[j]; |
| 654 | Vector<Color> src = a[j]; |
| 655 | if (dst.is_empty() || src.is_empty()) { |
| 656 | arrays[j] = Variant(); |
| 657 | continue; |
| 658 | } |
| 659 | dst.append_array(src); |
| 660 | arrays[j] = dst; |
| 661 | |
| 662 | } break; |
| 663 | case ARRAY_TEX_UV: |
| 664 | case ARRAY_TEX_UV2: { |
| 665 | Vector<Vector2> dst = arrays[j]; |
| 666 | Vector<Vector2> src = a[j]; |
| 667 | if (dst.is_empty() || src.is_empty()) { |
| 668 | arrays[j] = Variant(); |
| 669 | continue; |
| 670 | } |
| 671 | dst.append_array(src); |
| 672 | arrays[j] = dst; |
| 673 | |
| 674 | } break; |
| 675 | case ARRAY_INDEX: { |
| 676 | Vector<int> dst = arrays[j]; |
| 677 | Vector<int> src = a[j]; |
| 678 | if (dst.is_empty() || src.is_empty()) { |
| 679 | arrays[j] = Variant(); |
| 680 | continue; |
| 681 | } |
| 682 | { |
| 683 | int ss = src.size(); |
| 684 | int *w = src.ptrw(); |
| 685 | for (int k = 0; k < ss; k++) { |
| 686 | w[k] += index_accum; |
| 687 | } |
| 688 | } |
| 689 | dst.append_array(src); |
| 690 | arrays[j] = dst; |
| 691 | index_accum += vcount; |
| 692 | |
| 693 | } break; |
| 694 | } |
| 695 | } |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | ERR_FAIL_COND_V(arrays.size() != ARRAY_MAX, Ref<ArrayMesh>()); |
| 700 | |
| 701 | { |
| 702 | int *ir = nullptr; |
| 703 | Vector<int> indices = arrays[ARRAY_INDEX]; |
| 704 | bool has_indices = false; |
| 705 | Vector<Vector3> vertices = arrays[ARRAY_VERTEX]; |
| 706 | int vc = vertices.size(); |
| 707 | ERR_FAIL_COND_V(!vc, Ref<ArrayMesh>()); |
| 708 | Vector3 *r = vertices.ptrw(); |
| 709 | |
| 710 | if (indices.size()) { |
| 711 | ERR_FAIL_COND_V(indices.size() % 3 != 0, Ref<ArrayMesh>()); |
| 712 | vc = indices.size(); |
| 713 | ir = indices.ptrw(); |
| 714 | has_indices = true; |
| 715 | } else { |
| 716 | // Ensure there are enough vertices to construct at least one triangle. |
| 717 | ERR_FAIL_COND_V(vertices.size() % 3 != 0, Ref<ArrayMesh>()); |
| 718 | } |
| 719 | |
| 720 | HashMap<Vector3, Vector3> normal_accum; |
| 721 | |
| 722 | //fill normals with triangle normals |
| 723 | for (int i = 0; i < vc; i += 3) { |
| 724 | Vector3 t[3]; |
| 725 | |
| 726 | if (has_indices) { |
| 727 | t[0] = r[ir[i + 0]]; |
| 728 | t[1] = r[ir[i + 1]]; |
| 729 | t[2] = r[ir[i + 2]]; |
| 730 | } else { |
| 731 | t[0] = r[i + 0]; |
| 732 | t[1] = r[i + 1]; |
| 733 | t[2] = r[i + 2]; |
| 734 | } |
| 735 | |
| 736 | Vector3 n = Plane(t[0], t[1], t[2]).normal; |
| 737 | |
| 738 | for (int j = 0; j < 3; j++) { |
| 739 | HashMap<Vector3, Vector3>::Iterator E = normal_accum.find(t[j]); |
| 740 | if (!E) { |
| 741 | normal_accum[t[j]] = n; |
| 742 | } else { |
| 743 | float d = n.dot(E->value); |
| 744 | if (d < 1.0) { |
| 745 | E->value += n * (1.0 - d); |
| 746 | } |
| 747 | //E->get()+=n; |
| 748 | } |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | //normalize |
| 753 | |
| 754 | for (KeyValue<Vector3, Vector3> &E : normal_accum) { |
| 755 | E.value.normalize(); |
| 756 | } |
| 757 | |
| 758 | //displace normals |
| 759 | int vc2 = vertices.size(); |
| 760 | |
| 761 | for (int i = 0; i < vc2; i++) { |
| 762 | Vector3 t = r[i]; |
| 763 | |
| 764 | HashMap<Vector3, Vector3>::Iterator E = normal_accum.find(t); |
| 765 | ERR_CONTINUE(!E); |
| 766 | |
| 767 | t += E->value * p_margin; |
| 768 | r[i] = t; |
| 769 | } |
| 770 | |
| 771 | arrays[ARRAY_VERTEX] = vertices; |
| 772 | |
| 773 | if (!has_indices) { |
| 774 | Vector<int> new_indices; |
| 775 | new_indices.resize(vertices.size()); |
| 776 | int *iw = new_indices.ptrw(); |
| 777 | |
| 778 | for (int j = 0; j < vc2; j += 3) { |
| 779 | iw[j] = j; |
| 780 | iw[j + 1] = j + 2; |
| 781 | iw[j + 2] = j + 1; |
| 782 | } |
| 783 | |
| 784 | arrays[ARRAY_INDEX] = new_indices; |
| 785 | |
| 786 | } else { |
| 787 | for (int j = 0; j < vc; j += 3) { |
| 788 | SWAP(ir[j + 1], ir[j + 2]); |
| 789 | } |
| 790 | arrays[ARRAY_INDEX] = indices; |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | Ref<ArrayMesh> newmesh = memnew(ArrayMesh); |
| 795 | newmesh->add_surface_from_arrays(PRIMITIVE_TRIANGLES, arrays); |
| 796 | return newmesh; |
| 797 | } |
| 798 | |
| 799 | void Mesh::set_lightmap_size_hint(const Size2i &p_size) { |
| 800 | lightmap_size_hint = p_size; |
| 801 | } |
| 802 | |
| 803 | Size2i Mesh::get_lightmap_size_hint() const { |
| 804 | return lightmap_size_hint; |
| 805 | } |
| 806 | |
| 807 | Ref<Resource> Mesh::create_placeholder() const { |
| 808 | Ref<PlaceholderMesh> placeholder; |
| 809 | placeholder.instantiate(); |
| 810 | placeholder->set_aabb(get_aabb()); |
| 811 | return placeholder; |
| 812 | } |
| 813 | |
| 814 | void Mesh::_bind_methods() { |
| 815 | ClassDB::bind_method(D_METHOD("set_lightmap_size_hint", "size"), &Mesh::set_lightmap_size_hint); |
| 816 | ClassDB::bind_method(D_METHOD("get_lightmap_size_hint"), &Mesh::get_lightmap_size_hint); |
| 817 | ClassDB::bind_method(D_METHOD("get_aabb"), &Mesh::get_aabb); |
| 818 | ClassDB::bind_method(D_METHOD("get_faces"), &Mesh::_get_faces); |
| 819 | |
| 820 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "lightmap_size_hint"), "set_lightmap_size_hint", "get_lightmap_size_hint"); |
| 821 | |
| 822 | ClassDB::bind_method(D_METHOD("get_surface_count"), &Mesh::get_surface_count); |
| 823 | ClassDB::bind_method(D_METHOD("surface_get_arrays", "surf_idx"), &Mesh::surface_get_arrays); |
| 824 | ClassDB::bind_method(D_METHOD("surface_get_blend_shape_arrays", "surf_idx"), &Mesh::surface_get_blend_shape_arrays); |
| 825 | ClassDB::bind_method(D_METHOD("surface_set_material", "surf_idx", "material"), &Mesh::surface_set_material); |
| 826 | ClassDB::bind_method(D_METHOD("surface_get_material", "surf_idx"), &Mesh::surface_get_material); |
| 827 | ClassDB::bind_method(D_METHOD("create_placeholder"), &Mesh::create_placeholder); |
| 828 | |
| 829 | BIND_ENUM_CONSTANT(PRIMITIVE_POINTS); |
| 830 | BIND_ENUM_CONSTANT(PRIMITIVE_LINES); |
| 831 | BIND_ENUM_CONSTANT(PRIMITIVE_LINE_STRIP); |
| 832 | BIND_ENUM_CONSTANT(PRIMITIVE_TRIANGLES); |
| 833 | BIND_ENUM_CONSTANT(PRIMITIVE_TRIANGLE_STRIP); |
| 834 | |
| 835 | BIND_ENUM_CONSTANT(ARRAY_VERTEX); |
| 836 | BIND_ENUM_CONSTANT(ARRAY_NORMAL); |
| 837 | BIND_ENUM_CONSTANT(ARRAY_TANGENT); |
| 838 | BIND_ENUM_CONSTANT(ARRAY_COLOR); |
| 839 | BIND_ENUM_CONSTANT(ARRAY_TEX_UV); |
| 840 | BIND_ENUM_CONSTANT(ARRAY_TEX_UV2); |
| 841 | BIND_ENUM_CONSTANT(ARRAY_CUSTOM0); |
| 842 | BIND_ENUM_CONSTANT(ARRAY_CUSTOM1); |
| 843 | BIND_ENUM_CONSTANT(ARRAY_CUSTOM2); |
| 844 | BIND_ENUM_CONSTANT(ARRAY_CUSTOM3); |
| 845 | BIND_ENUM_CONSTANT(ARRAY_BONES); |
| 846 | BIND_ENUM_CONSTANT(ARRAY_WEIGHTS); |
| 847 | BIND_ENUM_CONSTANT(ARRAY_INDEX); |
| 848 | BIND_ENUM_CONSTANT(ARRAY_MAX); |
| 849 | |
| 850 | BIND_ENUM_CONSTANT(ARRAY_CUSTOM_RGBA8_UNORM); |
| 851 | BIND_ENUM_CONSTANT(ARRAY_CUSTOM_RGBA8_SNORM); |
| 852 | BIND_ENUM_CONSTANT(ARRAY_CUSTOM_RG_HALF); |
| 853 | BIND_ENUM_CONSTANT(ARRAY_CUSTOM_RGBA_HALF); |
| 854 | BIND_ENUM_CONSTANT(ARRAY_CUSTOM_R_FLOAT); |
| 855 | BIND_ENUM_CONSTANT(ARRAY_CUSTOM_RG_FLOAT); |
| 856 | BIND_ENUM_CONSTANT(ARRAY_CUSTOM_RGB_FLOAT); |
| 857 | BIND_ENUM_CONSTANT(ARRAY_CUSTOM_RGBA_FLOAT); |
| 858 | BIND_ENUM_CONSTANT(ARRAY_CUSTOM_MAX); |
| 859 | |
| 860 | BIND_BITFIELD_FLAG(ARRAY_FORMAT_VERTEX); |
| 861 | BIND_BITFIELD_FLAG(ARRAY_FORMAT_NORMAL); |
| 862 | BIND_BITFIELD_FLAG(ARRAY_FORMAT_TANGENT); |
| 863 | BIND_BITFIELD_FLAG(ARRAY_FORMAT_COLOR); |
| 864 | BIND_BITFIELD_FLAG(ARRAY_FORMAT_TEX_UV); |
| 865 | BIND_BITFIELD_FLAG(ARRAY_FORMAT_TEX_UV2); |
| 866 | BIND_BITFIELD_FLAG(ARRAY_FORMAT_CUSTOM0); |
| 867 | BIND_BITFIELD_FLAG(ARRAY_FORMAT_CUSTOM1); |
| 868 | BIND_BITFIELD_FLAG(ARRAY_FORMAT_CUSTOM2); |
| 869 | BIND_BITFIELD_FLAG(ARRAY_FORMAT_CUSTOM3); |
| 870 | BIND_BITFIELD_FLAG(ARRAY_FORMAT_BONES); |
| 871 | BIND_BITFIELD_FLAG(ARRAY_FORMAT_WEIGHTS); |
| 872 | BIND_BITFIELD_FLAG(ARRAY_FORMAT_INDEX); |
| 873 | |
| 874 | BIND_BITFIELD_FLAG(ARRAY_FORMAT_BLEND_SHAPE_MASK); |
| 875 | |
| 876 | BIND_BITFIELD_FLAG(ARRAY_FORMAT_CUSTOM_BASE); |
| 877 | BIND_BITFIELD_FLAG(ARRAY_FORMAT_CUSTOM_BITS); |
| 878 | BIND_BITFIELD_FLAG(ARRAY_FORMAT_CUSTOM0_SHIFT); |
| 879 | BIND_BITFIELD_FLAG(ARRAY_FORMAT_CUSTOM1_SHIFT); |
| 880 | BIND_BITFIELD_FLAG(ARRAY_FORMAT_CUSTOM2_SHIFT); |
| 881 | BIND_BITFIELD_FLAG(ARRAY_FORMAT_CUSTOM3_SHIFT); |
| 882 | |
| 883 | BIND_BITFIELD_FLAG(ARRAY_FORMAT_CUSTOM_MASK); |
| 884 | BIND_BITFIELD_FLAG(ARRAY_COMPRESS_FLAGS_BASE); |
| 885 | |
| 886 | BIND_BITFIELD_FLAG(ARRAY_FLAG_USE_2D_VERTICES); |
| 887 | BIND_BITFIELD_FLAG(ARRAY_FLAG_USE_DYNAMIC_UPDATE); |
| 888 | BIND_BITFIELD_FLAG(ARRAY_FLAG_USE_8_BONE_WEIGHTS); |
| 889 | BIND_BITFIELD_FLAG(ARRAY_FLAG_USES_EMPTY_VERTEX_ARRAY); |
| 890 | |
| 891 | BIND_BITFIELD_FLAG(ARRAY_FLAG_COMPRESS_ATTRIBUTES); |
| 892 | |
| 893 | BIND_ENUM_CONSTANT(BLEND_SHAPE_MODE_NORMALIZED); |
| 894 | BIND_ENUM_CONSTANT(BLEND_SHAPE_MODE_RELATIVE); |
| 895 | |
| 896 | GDVIRTUAL_BIND(_get_surface_count) |
| 897 | GDVIRTUAL_BIND(_surface_get_array_len, "index") |
| 898 | GDVIRTUAL_BIND(_surface_get_array_index_len, "index") |
| 899 | GDVIRTUAL_BIND(_surface_get_arrays, "index") |
| 900 | GDVIRTUAL_BIND(_surface_get_blend_shape_arrays, "index") |
| 901 | GDVIRTUAL_BIND(_surface_get_lods, "index") |
| 902 | GDVIRTUAL_BIND(_surface_get_format, "index") |
| 903 | GDVIRTUAL_BIND(_surface_get_primitive_type, "index") |
| 904 | GDVIRTUAL_BIND(_surface_set_material, "index", "material") |
| 905 | GDVIRTUAL_BIND(_surface_get_material, "index") |
| 906 | GDVIRTUAL_BIND(_get_blend_shape_count) |
| 907 | GDVIRTUAL_BIND(_get_blend_shape_name, "index") |
| 908 | GDVIRTUAL_BIND(_set_blend_shape_name, "index", "name") |
| 909 | GDVIRTUAL_BIND(_get_aabb) |
| 910 | } |
| 911 | |
| 912 | void Mesh::clear_cache() const { |
| 913 | triangle_mesh.unref(); |
| 914 | debug_lines.clear(); |
| 915 | } |
| 916 | |
| 917 | #ifndef PHYSICS_3D_DISABLED |
| 918 | Vector<Ref<Shape3D>> Mesh::convex_decompose(const Ref<MeshConvexDecompositionSettings> &p_settings) const { |
| 919 | ERR_FAIL_NULL_V(convex_decomposition_function, Vector<Ref<Shape3D>>()); |
| 920 | |
| 921 | Ref<TriangleMesh> tm = generate_triangle_mesh(); |
| 922 | ERR_FAIL_COND_V(tm.is_null(), Vector<Ref<Shape3D>>()); |
| 923 | |
| 924 | const Vector<TriangleMesh::Triangle> &triangles = tm->get_triangles(); |
| 925 | int triangle_count = triangles.size(); |
| 926 | |
| 927 | Vector<uint32_t> indices; |
| 928 | { |
| 929 | indices.resize(triangle_count * 3); |
| 930 | uint32_t *w = indices.ptrw(); |
| 931 | for (int i = 0; i < triangle_count; i++) { |
| 932 | for (int j = 0; j < 3; j++) { |
| 933 | w[i * 3 + j] = triangles[i].indices[j]; |
| 934 | } |
| 935 | } |
| 936 | } |
| 937 | |
| 938 | const Vector<Vector3> &vertices = tm->get_vertices(); |
| 939 | int vertex_count = vertices.size(); |
| 940 | |
| 941 | Vector<Vector<Vector3>> decomposed = convex_decomposition_function((real_t *)vertices.ptr(), vertex_count, indices.ptr(), triangle_count, p_settings, nullptr); |
| 942 | |
| 943 | Vector<Ref<Shape3D>> ret; |
| 944 | |
| 945 | for (int i = 0; i < decomposed.size(); i++) { |
| 946 | Ref<ConvexPolygonShape3D> shape; |
| 947 | shape.instantiate(); |
| 948 | shape->set_points(decomposed[i]); |
| 949 | ret.push_back(shape); |
| 950 | } |
| 951 | |
| 952 | return ret; |
| 953 | } |
| 954 | #endif // PHYSICS_3D_DISABLED |
| 955 | |
| 956 | int Mesh::get_builtin_bind_pose_count() const { |
| 957 | return 0; |
| 958 | } |
| 959 | |
| 960 | Transform3D Mesh::get_builtin_bind_pose(int p_index) const { |
| 961 | return Transform3D(); |
| 962 | } |
| 963 | |
| 964 | Mesh::Mesh() { |
| 965 | } |
| 966 | |
| 967 | enum OldArrayType { |
| 968 | OLD_ARRAY_VERTEX, |
| 969 | OLD_ARRAY_NORMAL, |
| 970 | OLD_ARRAY_TANGENT, |
| 971 | OLD_ARRAY_COLOR, |
| 972 | OLD_ARRAY_TEX_UV, |
| 973 | OLD_ARRAY_TEX_UV2, |
| 974 | OLD_ARRAY_BONES, |
| 975 | OLD_ARRAY_WEIGHTS, |
| 976 | OLD_ARRAY_INDEX, |
| 977 | OLD_ARRAY_MAX, |
| 978 | }; |
| 979 | |
| 980 | enum OldArrayFormat { |
| 981 | /* OLD_ARRAY FORMAT FLAGS */ |
| 982 | OLD_ARRAY_FORMAT_VERTEX = 1 << OLD_ARRAY_VERTEX, // mandatory |
| 983 | OLD_ARRAY_FORMAT_NORMAL = 1 << OLD_ARRAY_NORMAL, |
| 984 | OLD_ARRAY_FORMAT_TANGENT = 1 << OLD_ARRAY_TANGENT, |
| 985 | OLD_ARRAY_FORMAT_COLOR = 1 << OLD_ARRAY_COLOR, |
| 986 | OLD_ARRAY_FORMAT_TEX_UV = 1 << OLD_ARRAY_TEX_UV, |
| 987 | OLD_ARRAY_FORMAT_TEX_UV2 = 1 << OLD_ARRAY_TEX_UV2, |
| 988 | OLD_ARRAY_FORMAT_BONES = 1 << OLD_ARRAY_BONES, |
| 989 | OLD_ARRAY_FORMAT_WEIGHTS = 1 << OLD_ARRAY_WEIGHTS, |
| 990 | OLD_ARRAY_FORMAT_INDEX = 1 << OLD_ARRAY_INDEX, |
| 991 | |
| 992 | OLD_ARRAY_COMPRESS_BASE = (OLD_ARRAY_INDEX + 1), |
| 993 | OLD_ARRAY_COMPRESS_VERTEX = 1 << (OLD_ARRAY_VERTEX + (int32_t)OLD_ARRAY_COMPRESS_BASE), // mandatory |
| 994 | OLD_ARRAY_COMPRESS_NORMAL = 1 << (OLD_ARRAY_NORMAL + (int32_t)OLD_ARRAY_COMPRESS_BASE), |
| 995 | OLD_ARRAY_COMPRESS_TANGENT = 1 << (OLD_ARRAY_TANGENT + (int32_t)OLD_ARRAY_COMPRESS_BASE), |
| 996 | OLD_ARRAY_COMPRESS_COLOR = 1 << (OLD_ARRAY_COLOR + (int32_t)OLD_ARRAY_COMPRESS_BASE), |
| 997 | OLD_ARRAY_COMPRESS_TEX_UV = 1 << (OLD_ARRAY_TEX_UV + (int32_t)OLD_ARRAY_COMPRESS_BASE), |
| 998 | OLD_ARRAY_COMPRESS_TEX_UV2 = 1 << (OLD_ARRAY_TEX_UV2 + (int32_t)OLD_ARRAY_COMPRESS_BASE), |
| 999 | OLD_ARRAY_COMPRESS_BONES = 1 << (OLD_ARRAY_BONES + (int32_t)OLD_ARRAY_COMPRESS_BASE), |
| 1000 | OLD_ARRAY_COMPRESS_WEIGHTS = 1 << (OLD_ARRAY_WEIGHTS + (int32_t)OLD_ARRAY_COMPRESS_BASE), |
| 1001 | OLD_ARRAY_COMPRESS_INDEX = 1 << (OLD_ARRAY_INDEX + (int32_t)OLD_ARRAY_COMPRESS_BASE), |
| 1002 | |
| 1003 | OLD_ARRAY_FLAG_USE_2D_VERTICES = OLD_ARRAY_COMPRESS_INDEX << 1, |
| 1004 | OLD_ARRAY_FLAG_USE_16_BIT_BONES = OLD_ARRAY_COMPRESS_INDEX << 2, |
| 1005 | OLD_ARRAY_FLAG_USE_DYNAMIC_UPDATE = OLD_ARRAY_COMPRESS_INDEX << 3, |
| 1006 | OLD_ARRAY_FLAG_USE_OCTAHEDRAL_COMPRESSION = OLD_ARRAY_COMPRESS_INDEX << 4, |
| 1007 | }; |
| 1008 | |
| 1009 | #ifndef DISABLE_DEPRECATED |
| 1010 | static Array _convert_old_array(const Array &p_old) { |
| 1011 | Array new_array; |
| 1012 | new_array.resize(Mesh::ARRAY_MAX); |
| 1013 | new_array[Mesh::ARRAY_VERTEX] = p_old[OLD_ARRAY_VERTEX]; |
| 1014 | new_array[Mesh::ARRAY_NORMAL] = p_old[OLD_ARRAY_NORMAL]; |
| 1015 | new_array[Mesh::ARRAY_TANGENT] = p_old[OLD_ARRAY_TANGENT]; |
| 1016 | new_array[Mesh::ARRAY_COLOR] = p_old[OLD_ARRAY_COLOR]; |
| 1017 | new_array[Mesh::ARRAY_TEX_UV] = p_old[OLD_ARRAY_TEX_UV]; |
| 1018 | new_array[Mesh::ARRAY_TEX_UV2] = p_old[OLD_ARRAY_TEX_UV2]; |
| 1019 | new_array[Mesh::ARRAY_BONES] = p_old[OLD_ARRAY_BONES]; |
| 1020 | new_array[Mesh::ARRAY_WEIGHTS] = p_old[OLD_ARRAY_WEIGHTS]; |
| 1021 | new_array[Mesh::ARRAY_INDEX] = p_old[OLD_ARRAY_INDEX]; |
| 1022 | return new_array; |
| 1023 | } |
| 1024 | |
| 1025 | static Mesh::PrimitiveType _old_primitives[7] = { |
| 1026 | Mesh::PRIMITIVE_POINTS, |
| 1027 | Mesh::PRIMITIVE_LINES, |
| 1028 | Mesh::PRIMITIVE_LINE_STRIP, |
| 1029 | Mesh::PRIMITIVE_LINES, |
| 1030 | Mesh::PRIMITIVE_TRIANGLES, |
| 1031 | Mesh::PRIMITIVE_TRIANGLE_STRIP, |
| 1032 | Mesh::PRIMITIVE_TRIANGLE_STRIP |
| 1033 | }; |
| 1034 | #endif // DISABLE_DEPRECATED |
| 1035 | |
| 1036 | void _fix_array_compatibility(const Vector<uint8_t> &p_src, uint64_t p_old_format, uint64_t p_new_format, uint32_t p_elements, Vector<uint8_t> &vertex_data, Vector<uint8_t> &attribute_data, Vector<uint8_t> &skin_data) { |
| 1037 | uint32_t dst_vertex_stride; |
| 1038 | uint32_t dst_normal_tangent_stride; |
| 1039 | uint32_t dst_attribute_stride; |
| 1040 | uint32_t dst_skin_stride; |
| 1041 | uint32_t dst_offsets[Mesh::ARRAY_MAX]; |
| 1042 | RenderingServer::get_singleton()->mesh_surface_make_offsets_from_format(p_new_format & (~RS::ARRAY_FORMAT_INDEX), p_elements, 0, dst_offsets, dst_vertex_stride, dst_normal_tangent_stride, dst_attribute_stride, dst_skin_stride); |
| 1043 | |
| 1044 | vertex_data.resize((dst_vertex_stride + dst_normal_tangent_stride) * p_elements); |
| 1045 | attribute_data.resize(dst_attribute_stride * p_elements); |
| 1046 | skin_data.resize(dst_skin_stride * p_elements); |
| 1047 | |
| 1048 | uint8_t *dst_vertex_ptr = vertex_data.ptrw(); |
| 1049 | uint8_t *dst_attribute_ptr = attribute_data.ptrw(); |
| 1050 | uint8_t *dst_skin_ptr = skin_data.ptrw(); |
| 1051 | |
| 1052 | const uint8_t *src_vertex_ptr = p_src.ptr(); |
| 1053 | uint32_t src_vertex_stride = p_src.size() / p_elements; |
| 1054 | |
| 1055 | uint32_t src_offset = 0; |
| 1056 | for (uint32_t j = 0; j < OLD_ARRAY_INDEX; j++) { |
| 1057 | if (!(p_old_format & (1ULL << j))) { |
| 1058 | continue; |
| 1059 | } |
| 1060 | switch (j) { |
| 1061 | case OLD_ARRAY_VERTEX: { |
| 1062 | if (p_old_format & OLD_ARRAY_FLAG_USE_2D_VERTICES) { |
| 1063 | if (p_old_format & OLD_ARRAY_COMPRESS_VERTEX) { |
| 1064 | for (uint32_t i = 0; i < p_elements; i++) { |
| 1065 | const uint16_t *src = (const uint16_t *)&src_vertex_ptr[i * src_vertex_stride]; |
| 1066 | float *dst = (float *)&dst_vertex_ptr[i * dst_vertex_stride]; |
| 1067 | dst[0] = Math::half_to_float(src[0]); |
| 1068 | dst[1] = Math::half_to_float(src[1]); |
| 1069 | } |
| 1070 | src_offset += sizeof(uint16_t) * 2; |
| 1071 | } else { |
| 1072 | for (uint32_t i = 0; i < p_elements; i++) { |
| 1073 | const float *src = (const float *)&src_vertex_ptr[i * src_vertex_stride]; |
| 1074 | float *dst = (float *)&dst_vertex_ptr[i * dst_vertex_stride]; |
| 1075 | dst[0] = src[0]; |
| 1076 | dst[1] = src[1]; |
| 1077 | } |
| 1078 | src_offset += sizeof(float) * 2; |
| 1079 | } |
| 1080 | } else { |
| 1081 | if (p_old_format & OLD_ARRAY_COMPRESS_VERTEX) { |
| 1082 | for (uint32_t i = 0; i < p_elements; i++) { |
| 1083 | const uint16_t *src = (const uint16_t *)&src_vertex_ptr[i * src_vertex_stride]; |
| 1084 | float *dst = (float *)&dst_vertex_ptr[i * dst_vertex_stride]; |
| 1085 | dst[0] = Math::half_to_float(src[0]); |
| 1086 | dst[1] = Math::half_to_float(src[1]); |
| 1087 | dst[2] = Math::half_to_float(src[2]); |
| 1088 | } |
| 1089 | src_offset += sizeof(uint16_t) * 4; //+pad |
| 1090 | } else { |
| 1091 | for (uint32_t i = 0; i < p_elements; i++) { |
| 1092 | const float *src = (const float *)&src_vertex_ptr[i * src_vertex_stride]; |
| 1093 | float *dst = (float *)&dst_vertex_ptr[i * dst_vertex_stride]; |
| 1094 | dst[0] = src[0]; |
| 1095 | dst[1] = src[1]; |
| 1096 | dst[2] = src[2]; |
| 1097 | } |
| 1098 | src_offset += sizeof(float) * 3; |
| 1099 | } |
| 1100 | } |
| 1101 | } break; |
| 1102 | case OLD_ARRAY_NORMAL: { |
| 1103 | if (p_old_format & OLD_ARRAY_FLAG_USE_OCTAHEDRAL_COMPRESSION) { |
| 1104 | if ((p_old_format & OLD_ARRAY_COMPRESS_NORMAL) && (p_old_format & OLD_ARRAY_FORMAT_TANGENT) && (p_old_format & OLD_ARRAY_COMPRESS_TANGENT)) { |
| 1105 | for (uint32_t i = 0; i < p_elements; i++) { |
| 1106 | const int8_t *src = (const int8_t *)&src_vertex_ptr[i * src_vertex_stride + src_offset]; |
| 1107 | int16_t *dst = (int16_t *)&dst_vertex_ptr[i * dst_normal_tangent_stride + dst_offsets[Mesh::ARRAY_NORMAL]]; |
| 1108 | |
| 1109 | dst[0] = (int16_t)CLAMP(src[0] / 127.0f * 32767, -32768, 32767); |
| 1110 | dst[1] = (int16_t)CLAMP(src[1] / 127.0f * 32767, -32768, 32767); |
| 1111 | } |
| 1112 | src_offset += sizeof(int8_t) * 2; |
| 1113 | } else { |
| 1114 | for (uint32_t i = 0; i < p_elements; i++) { |
| 1115 | const int16_t *src = (const int16_t *)&src_vertex_ptr[i * src_vertex_stride + src_offset]; |
| 1116 | int16_t *dst = (int16_t *)&dst_vertex_ptr[i * dst_normal_tangent_stride + dst_offsets[Mesh::ARRAY_NORMAL]]; |
| 1117 | |
| 1118 | dst[0] = src[0]; |
| 1119 | dst[1] = src[1]; |
| 1120 | } |
| 1121 | src_offset += sizeof(int16_t) * 2; |
| 1122 | } |
| 1123 | } else { // No Octahedral compression |
| 1124 | if (p_old_format & OLD_ARRAY_COMPRESS_NORMAL) { |
| 1125 | for (uint32_t i = 0; i < p_elements; i++) { |
| 1126 | const int8_t *src = (const int8_t *)&src_vertex_ptr[i * src_vertex_stride + src_offset]; |
| 1127 | const Vector3 original_normal(src[0], src[1], src[2]); |
| 1128 | Vector2 res = original_normal.octahedron_encode(); |
| 1129 | |
| 1130 | uint16_t *dst = (uint16_t *)&dst_vertex_ptr[i * dst_normal_tangent_stride + dst_offsets[Mesh::ARRAY_NORMAL]]; |
| 1131 | dst[0] = (uint16_t)CLAMP(res.x * 65535, 0, 65535); |
| 1132 | dst[1] = (uint16_t)CLAMP(res.y * 65535, 0, 65535); |
| 1133 | } |
| 1134 | src_offset += sizeof(uint8_t) * 4; // 1 byte padding |
| 1135 | } else { |
| 1136 | for (uint32_t i = 0; i < p_elements; i++) { |
| 1137 | const float *src = (const float *)&src_vertex_ptr[i * src_vertex_stride + src_offset]; |
| 1138 | const Vector3 original_normal(src[0], src[1], src[2]); |
| 1139 | Vector2 res = original_normal.octahedron_encode(); |
| 1140 | |
| 1141 | uint16_t *dst = (uint16_t *)&dst_vertex_ptr[i * dst_normal_tangent_stride + dst_offsets[Mesh::ARRAY_NORMAL]]; |
| 1142 | dst[0] = (uint16_t)CLAMP(res.x * 65535, 0, 65535); |
| 1143 | dst[1] = (uint16_t)CLAMP(res.y * 65535, 0, 65535); |
| 1144 | } |
| 1145 | src_offset += sizeof(float) * 3; |
| 1146 | } |
| 1147 | } |
| 1148 | |
| 1149 | } break; |
| 1150 | case OLD_ARRAY_TANGENT: { |
| 1151 | if (p_old_format & OLD_ARRAY_FLAG_USE_OCTAHEDRAL_COMPRESSION) { |
| 1152 | if (p_old_format & OLD_ARRAY_COMPRESS_TANGENT) { // int8 SNORM -> uint16 UNORM |
| 1153 | for (uint32_t i = 0; i < p_elements; i++) { |
| 1154 | const int8_t *src = (const int8_t *)&src_vertex_ptr[i * src_vertex_stride + src_offset]; |
| 1155 | uint16_t *dst = (uint16_t *)&dst_vertex_ptr[i * dst_normal_tangent_stride + dst_offsets[Mesh::ARRAY_TANGENT]]; |
| 1156 | |
| 1157 | dst[0] = (uint16_t)CLAMP((src[0] / 127.0f * .5f + .5f) * 65535, 0, 65535); |
| 1158 | dst[1] = (uint16_t)CLAMP((src[1] / 127.0f * .5f + .5f) * 65535, 0, 65535); |
| 1159 | } |
| 1160 | src_offset += sizeof(uint8_t) * 2; |
| 1161 | } else { // int16 SNORM -> uint16 UNORM |
| 1162 | for (uint32_t i = 0; i < p_elements; i++) { |
| 1163 | const int16_t *src = (const int16_t *)&src_vertex_ptr[i * src_vertex_stride + src_offset]; |
| 1164 | uint16_t *dst = (uint16_t *)&dst_vertex_ptr[i * dst_normal_tangent_stride + dst_offsets[Mesh::ARRAY_TANGENT]]; |
| 1165 | |
| 1166 | dst[0] = (uint16_t)CLAMP((src[0] / 32767.0f * .5f + .5f) * 65535, 0, 65535); |
| 1167 | dst[1] = (uint16_t)CLAMP((src[1] / 32767.0f * .5f + .5f) * 65535, 0, 65535); |
| 1168 | } |
| 1169 | src_offset += sizeof(uint16_t) * 2; |
| 1170 | } |
| 1171 | } else { // No Octahedral compression |
| 1172 | if (p_old_format & OLD_ARRAY_COMPRESS_TANGENT) { |
| 1173 | for (uint32_t i = 0; i < p_elements; i++) { |
| 1174 | const int8_t *src = (const int8_t *)&src_vertex_ptr[i * src_vertex_stride + src_offset]; |
| 1175 | const Vector3 original_tangent(src[0], src[1], src[2]); |
| 1176 | Vector2 res = original_tangent.octahedron_tangent_encode(src[3]); |
| 1177 | |
| 1178 | uint16_t *dst = (uint16_t *)&dst_vertex_ptr[i * dst_normal_tangent_stride + dst_offsets[Mesh::ARRAY_NORMAL]]; |
| 1179 | dst[0] = (uint16_t)CLAMP(res.x * 65535, 0, 65535); |
| 1180 | dst[1] = (uint16_t)CLAMP(res.y * 65535, 0, 65535); |
| 1181 | if (dst[0] == 0 && dst[1] == 65535) { |
| 1182 | // (1, 1) and (0, 1) decode to the same value, but (0, 1) messes with our compression detection. |
| 1183 | // So we sanitize here. |
| 1184 | dst[0] = 65535; |
| 1185 | } |
| 1186 | } |
| 1187 | src_offset += sizeof(uint8_t) * 4; |
| 1188 | } else { |
| 1189 | for (uint32_t i = 0; i < p_elements; i++) { |
| 1190 | const float *src = (const float *)&src_vertex_ptr[i * src_vertex_stride + src_offset]; |
| 1191 | const Vector3 original_tangent(src[0], src[1], src[2]); |
| 1192 | Vector2 res = original_tangent.octahedron_tangent_encode(src[3]); |
| 1193 | |
| 1194 | uint16_t *dst = (uint16_t *)&dst_vertex_ptr[i * dst_normal_tangent_stride + dst_offsets[Mesh::ARRAY_NORMAL]]; |
| 1195 | dst[0] = (uint16_t)CLAMP(res.x * 65535, 0, 65535); |
| 1196 | dst[1] = (uint16_t)CLAMP(res.y * 65535, 0, 65535); |
| 1197 | if (dst[0] == 0 && dst[1] == 65535) { |
| 1198 | // (1, 1) and (0, 1) decode to the same value, but (0, 1) messes with our compression detection. |
| 1199 | // So we sanitize here. |
| 1200 | dst[0] = 65535; |
| 1201 | } |
| 1202 | } |
| 1203 | src_offset += sizeof(float) * 4; |
| 1204 | } |
| 1205 | } |
| 1206 | } break; |
| 1207 | case OLD_ARRAY_COLOR: { |
| 1208 | if (p_old_format & OLD_ARRAY_COMPRESS_COLOR) { |
| 1209 | for (uint32_t i = 0; i < p_elements; i++) { |
| 1210 | const uint32_t *src = (const uint32_t *)&src_vertex_ptr[i * src_vertex_stride + src_offset]; |
| 1211 | uint32_t *dst = (uint32_t *)&dst_attribute_ptr[i * dst_attribute_stride + dst_offsets[Mesh::ARRAY_COLOR]]; |
| 1212 | |
| 1213 | *dst = *src; |
| 1214 | } |
| 1215 | src_offset += sizeof(uint32_t); |
| 1216 | } else { |
| 1217 | for (uint32_t i = 0; i < p_elements; i++) { |
| 1218 | const float *src = (const float *)&src_vertex_ptr[i * src_vertex_stride + src_offset]; |
| 1219 | uint8_t *dst = (uint8_t *)&dst_attribute_ptr[i * dst_attribute_stride + dst_offsets[Mesh::ARRAY_COLOR]]; |
| 1220 | |
| 1221 | dst[0] = uint8_t(CLAMP(src[0] * 255.0, 0.0, 255.0)); |
| 1222 | dst[1] = uint8_t(CLAMP(src[1] * 255.0, 0.0, 255.0)); |
| 1223 | dst[2] = uint8_t(CLAMP(src[2] * 255.0, 0.0, 255.0)); |
| 1224 | dst[3] = uint8_t(CLAMP(src[3] * 255.0, 0.0, 255.0)); |
| 1225 | } |
| 1226 | src_offset += sizeof(float) * 4; |
| 1227 | } |
| 1228 | } break; |
| 1229 | case OLD_ARRAY_TEX_UV: { |
| 1230 | if (p_old_format & OLD_ARRAY_COMPRESS_TEX_UV) { |
| 1231 | for (uint32_t i = 0; i < p_elements; i++) { |
| 1232 | const uint16_t *src = (const uint16_t *)&src_vertex_ptr[i * src_vertex_stride + src_offset]; |
| 1233 | float *dst = (float *)&dst_attribute_ptr[i * dst_attribute_stride + dst_offsets[Mesh::ARRAY_TEX_UV]]; |
| 1234 | |
| 1235 | dst[0] = Math::half_to_float(src[0]); |
| 1236 | dst[1] = Math::half_to_float(src[1]); |
| 1237 | } |
| 1238 | src_offset += sizeof(uint16_t) * 2; |
| 1239 | } else { |
| 1240 | for (uint32_t i = 0; i < p_elements; i++) { |
| 1241 | const float *src = (const float *)&src_vertex_ptr[i * src_vertex_stride + src_offset]; |
| 1242 | float *dst = (float *)&dst_attribute_ptr[i * dst_attribute_stride + dst_offsets[Mesh::ARRAY_TEX_UV]]; |
| 1243 | |
| 1244 | dst[0] = src[0]; |
| 1245 | dst[1] = src[1]; |
| 1246 | } |
| 1247 | src_offset += sizeof(float) * 2; |
| 1248 | } |
| 1249 | |
| 1250 | } break; |
| 1251 | case OLD_ARRAY_TEX_UV2: { |
| 1252 | if (p_old_format & OLD_ARRAY_COMPRESS_TEX_UV2) { |
| 1253 | for (uint32_t i = 0; i < p_elements; i++) { |
| 1254 | const uint16_t *src = (const uint16_t *)&src_vertex_ptr[i * src_vertex_stride + src_offset]; |
| 1255 | float *dst = (float *)&dst_attribute_ptr[i * dst_attribute_stride + dst_offsets[Mesh::ARRAY_TEX_UV2]]; |
| 1256 | |
| 1257 | dst[0] = Math::half_to_float(src[0]); |
| 1258 | dst[1] = Math::half_to_float(src[1]); |
| 1259 | } |
| 1260 | src_offset += sizeof(uint16_t) * 2; |
| 1261 | } else { |
| 1262 | for (uint32_t i = 0; i < p_elements; i++) { |
| 1263 | const float *src = (const float *)&src_vertex_ptr[i * src_vertex_stride + src_offset]; |
| 1264 | float *dst = (float *)&dst_attribute_ptr[i * dst_attribute_stride + dst_offsets[Mesh::ARRAY_TEX_UV2]]; |
| 1265 | |
| 1266 | dst[0] = src[0]; |
| 1267 | dst[1] = src[1]; |
| 1268 | } |
| 1269 | src_offset += sizeof(float) * 2; |
| 1270 | } |
| 1271 | } break; |
| 1272 | case OLD_ARRAY_BONES: { |
| 1273 | if (p_old_format & OLD_ARRAY_FLAG_USE_16_BIT_BONES) { |
| 1274 | for (uint32_t i = 0; i < p_elements; i++) { |
| 1275 | const uint16_t *src = (const uint16_t *)&src_vertex_ptr[i * src_vertex_stride + src_offset]; |
| 1276 | uint16_t *dst = (uint16_t *)&dst_skin_ptr[i * dst_skin_stride + dst_offsets[Mesh::ARRAY_BONES]]; |
| 1277 | |
| 1278 | dst[0] = src[0]; |
| 1279 | dst[1] = src[1]; |
| 1280 | dst[2] = src[2]; |
| 1281 | dst[3] = src[3]; |
| 1282 | } |
| 1283 | src_offset += sizeof(uint16_t) * 4; |
| 1284 | } else { |
| 1285 | for (uint32_t i = 0; i < p_elements; i++) { |
| 1286 | const uint8_t *src = (const uint8_t *)&src_vertex_ptr[i * src_vertex_stride + src_offset]; |
| 1287 | uint16_t *dst = (uint16_t *)&dst_skin_ptr[i * dst_skin_stride + dst_offsets[Mesh::ARRAY_BONES]]; |
| 1288 | |
| 1289 | dst[0] = src[0]; |
| 1290 | dst[1] = src[1]; |
| 1291 | dst[2] = src[2]; |
| 1292 | dst[3] = src[3]; |
| 1293 | } |
| 1294 | src_offset += sizeof(uint8_t) * 4; |
| 1295 | } |
| 1296 | } break; |
| 1297 | case OLD_ARRAY_WEIGHTS: { |
| 1298 | if (p_old_format & OLD_ARRAY_COMPRESS_WEIGHTS) { |
| 1299 | for (uint32_t i = 0; i < p_elements; i++) { |
| 1300 | const uint16_t *src = (const uint16_t *)&src_vertex_ptr[i * src_vertex_stride + src_offset]; |
| 1301 | uint16_t *dst = (uint16_t *)&dst_skin_ptr[i * dst_skin_stride + dst_offsets[Mesh::ARRAY_WEIGHTS]]; |
| 1302 | |
| 1303 | dst[0] = src[0]; |
| 1304 | dst[1] = src[1]; |
| 1305 | dst[2] = src[2]; |
| 1306 | dst[3] = src[3]; |
| 1307 | } |
| 1308 | src_offset += sizeof(uint16_t) * 4; |
| 1309 | } else { |
| 1310 | for (uint32_t i = 0; i < p_elements; i++) { |
| 1311 | const float *src = (const float *)&src_vertex_ptr[i * src_vertex_stride + src_offset]; |
| 1312 | uint16_t *dst = (uint16_t *)&dst_skin_ptr[i * dst_skin_stride + dst_offsets[Mesh::ARRAY_WEIGHTS]]; |
| 1313 | |
| 1314 | dst[0] = uint16_t(CLAMP(src[0] * 65535.0, 0, 65535.0)); |
| 1315 | dst[1] = uint16_t(CLAMP(src[1] * 65535.0, 0, 65535.0)); |
| 1316 | dst[2] = uint16_t(CLAMP(src[2] * 65535.0, 0, 65535.0)); |
| 1317 | dst[3] = uint16_t(CLAMP(src[3] * 65535.0, 0, 65535.0)); |
| 1318 | } |
| 1319 | src_offset += sizeof(float) * 4; |
| 1320 | } |
| 1321 | } break; |
| 1322 | default: { |
| 1323 | } |
| 1324 | } |
| 1325 | } |
| 1326 | } |
| 1327 | |
| 1328 | bool ArrayMesh::_set(const StringName &p_name, const Variant &p_value) { |
| 1329 | String sname = p_name; |
| 1330 | |
| 1331 | if (sname.begins_with("surface_")) { |
| 1332 | int sl = sname.find_char('/'); |
| 1333 | if (sl == -1) { |
| 1334 | return false; |
| 1335 | } |
| 1336 | int idx = sname.substr(8, sl - 8).to_int(); |
| 1337 | |
| 1338 | String what = sname.get_slicec('/', 1); |
| 1339 | if (what == "material") { |
| 1340 | surface_set_material(idx, p_value); |
| 1341 | } else if (what == "name") { |
| 1342 | surface_set_name(idx, p_value); |
| 1343 | } |
| 1344 | return true; |
| 1345 | } |
| 1346 | |
| 1347 | #ifndef DISABLE_DEPRECATED |
| 1348 | // Kept for compatibility from 3.x to 4.0. |
| 1349 | if (!sname.begins_with("surfaces")) { |
| 1350 | return false; |
| 1351 | } |
| 1352 | |
| 1353 | WARN_DEPRECATED_MSG(vformat( |
| 1354 | "Mesh uses old surface format, which is deprecated (and loads slower). Consider re-importing or re-saving the scene. Path: \"%s\"", |
| 1355 | get_path())); |
| 1356 | |
| 1357 | int idx = sname.get_slicec('/', 1).to_int(); |
| 1358 | String what = sname.get_slicec('/', 2); |
| 1359 | |
| 1360 | if (idx == surfaces.size()) { |
| 1361 | //create |
| 1362 | Dictionary d = p_value; |
| 1363 | ERR_FAIL_COND_V(!d.has("primitive"), false); |
| 1364 | |
| 1365 | if (d.has("arrays")) { |
| 1366 | //oldest format (2.x) |
| 1367 | ERR_FAIL_COND_V(!d.has("morph_arrays"), false); |
| 1368 | Array morph_arrays = d["morph_arrays"]; |
| 1369 | for (int i = 0; i < morph_arrays.size(); i++) { |
| 1370 | morph_arrays[i] = _convert_old_array(morph_arrays[i]); |
| 1371 | } |
| 1372 | add_surface_from_arrays(_old_primitives[int(d["primitive"])], _convert_old_array(d["arrays"]), morph_arrays); |
| 1373 | |
| 1374 | } else if (d.has("array_data")) { |
| 1375 | //print_line("array data (old style"); |
| 1376 | //older format (3.x) |
| 1377 | Vector<uint8_t> array_data = d["array_data"]; |
| 1378 | Vector<uint8_t> array_index_data; |
| 1379 | if (d.has("array_index_data")) { |
| 1380 | array_index_data = d["array_index_data"]; |
| 1381 | } |
| 1382 | |
| 1383 | ERR_FAIL_COND_V(!d.has("format"), false); |
| 1384 | uint64_t old_format = d["format"]; |
| 1385 | |
| 1386 | uint32_t primitive = d["primitive"]; |
| 1387 | |
| 1388 | primitive = _old_primitives[primitive]; //compatibility |
| 1389 | |
| 1390 | ERR_FAIL_COND_V(!d.has("vertex_count"), false); |
| 1391 | int vertex_count = d["vertex_count"]; |
| 1392 | |
| 1393 | uint64_t new_format = ARRAY_FORMAT_VERTEX | ARRAY_FLAG_FORMAT_CURRENT_VERSION; |
| 1394 | |
| 1395 | if (old_format & OLD_ARRAY_FORMAT_NORMAL) { |
| 1396 | new_format |= ARRAY_FORMAT_NORMAL; |
| 1397 | } |
| 1398 | if (old_format & OLD_ARRAY_FORMAT_TANGENT) { |
| 1399 | new_format |= ARRAY_FORMAT_TANGENT; |
| 1400 | } |
| 1401 | if (old_format & OLD_ARRAY_FORMAT_COLOR) { |
| 1402 | new_format |= ARRAY_FORMAT_COLOR; |
| 1403 | } |
| 1404 | if (old_format & OLD_ARRAY_FORMAT_TEX_UV) { |
| 1405 | new_format |= ARRAY_FORMAT_TEX_UV; |
| 1406 | } |
| 1407 | if (old_format & OLD_ARRAY_FORMAT_TEX_UV2) { |
| 1408 | new_format |= ARRAY_FORMAT_TEX_UV2; |
| 1409 | } |
| 1410 | if (old_format & OLD_ARRAY_FORMAT_BONES) { |
| 1411 | new_format |= ARRAY_FORMAT_BONES; |
| 1412 | } |
| 1413 | if (old_format & OLD_ARRAY_FORMAT_WEIGHTS) { |
| 1414 | new_format |= ARRAY_FORMAT_WEIGHTS; |
| 1415 | } |
| 1416 | if (old_format & OLD_ARRAY_FORMAT_INDEX) { |
| 1417 | new_format |= ARRAY_FORMAT_INDEX; |
| 1418 | } |
| 1419 | if (old_format & OLD_ARRAY_FLAG_USE_2D_VERTICES) { |
| 1420 | new_format |= OLD_ARRAY_FLAG_USE_2D_VERTICES; |
| 1421 | } |
| 1422 | |
| 1423 | Vector<uint8_t> vertex_array; |
| 1424 | Vector<uint8_t> attribute_array; |
| 1425 | Vector<uint8_t> skin_array; |
| 1426 | |
| 1427 | _fix_array_compatibility(array_data, old_format, new_format, vertex_count, vertex_array, attribute_array, skin_array); |
| 1428 | |
| 1429 | int index_count = 0; |
| 1430 | if (d.has("index_count")) { |
| 1431 | index_count = d["index_count"]; |
| 1432 | } |
| 1433 | |
| 1434 | Vector<uint8_t> blend_shapes_new; |
| 1435 | |
| 1436 | if (d.has("blend_shape_data")) { |
| 1437 | Array blend_shape_data = d["blend_shape_data"]; |
| 1438 | for (int i = 0; i < blend_shape_data.size(); i++) { |
| 1439 | Vector<uint8_t> blend_vertex_array; |
| 1440 | Vector<uint8_t> blend_attribute_array; |
| 1441 | Vector<uint8_t> blend_skin_array; |
| 1442 | |
| 1443 | Vector<uint8_t> shape = blend_shape_data[i]; |
| 1444 | _fix_array_compatibility(shape, old_format, new_format, vertex_count, blend_vertex_array, blend_attribute_array, blend_skin_array); |
| 1445 | |
| 1446 | blend_shapes_new.append_array(blend_vertex_array); |
| 1447 | } |
| 1448 | } |
| 1449 | |
| 1450 | //clear unused flags |
| 1451 | print_verbose("Mesh format pre-conversion: " + itos(old_format)); |
| 1452 | |
| 1453 | print_verbose("Mesh format post-conversion: " + itos(new_format)); |
| 1454 | |
| 1455 | ERR_FAIL_COND_V(!d.has("aabb"), false); |
| 1456 | AABB aabb_new = d["aabb"]; |
| 1457 | |
| 1458 | Vector<AABB> bone_aabb; |
| 1459 | if (d.has("skeleton_aabb")) { |
| 1460 | Array baabb = d["skeleton_aabb"]; |
| 1461 | bone_aabb.resize(baabb.size()); |
| 1462 | |
| 1463 | for (int i = 0; i < baabb.size(); i++) { |
| 1464 | bone_aabb.write[i] = baabb[i]; |
| 1465 | } |
| 1466 | } |
| 1467 | |
| 1468 | add_surface(new_format, PrimitiveType(primitive), vertex_array, attribute_array, skin_array, vertex_count, array_index_data, index_count, aabb_new, blend_shapes_new, bone_aabb); |
| 1469 | |
| 1470 | } else { |
| 1471 | ERR_FAIL_V(false); |
| 1472 | } |
| 1473 | |
| 1474 | if (d.has("material")) { |
| 1475 | surface_set_material(idx, d["material"]); |
| 1476 | } |
| 1477 | if (d.has("name")) { |
| 1478 | surface_set_name(idx, d["name"]); |
| 1479 | } |
| 1480 | |
| 1481 | return true; |
| 1482 | } |
| 1483 | #endif // DISABLE_DEPRECATED |
| 1484 | |
| 1485 | return false; |
| 1486 | } |
| 1487 | |
| 1488 | void ArrayMesh::_set_blend_shape_names(const PackedStringArray &p_names) { |
| 1489 | ERR_FAIL_COND(surfaces.size() > 0); |
| 1490 | |
| 1491 | blend_shapes.resize(p_names.size()); |
| 1492 | for (int i = 0; i < p_names.size(); i++) { |
| 1493 | blend_shapes.write[i] = p_names[i]; |
| 1494 | } |
| 1495 | |
| 1496 | if (mesh.is_valid()) { |
| 1497 | RS::get_singleton()->mesh_set_blend_shape_count(mesh, blend_shapes.size()); |
| 1498 | } |
| 1499 | } |
| 1500 | |
| 1501 | PackedStringArray ArrayMesh::_get_blend_shape_names() const { |
| 1502 | PackedStringArray sarr; |
| 1503 | sarr.resize(blend_shapes.size()); |
| 1504 | for (int i = 0; i < blend_shapes.size(); i++) { |
| 1505 | sarr.write[i] = blend_shapes[i]; |
| 1506 | } |
| 1507 | return sarr; |
| 1508 | } |
| 1509 | |
| 1510 | Array ArrayMesh::_get_surfaces() const { |
| 1511 | if (mesh.is_null()) { |
| 1512 | return Array(); |
| 1513 | } |
| 1514 | |
| 1515 | Array ret; |
| 1516 | for (int i = 0; i < surfaces.size(); i++) { |
| 1517 | RenderingServer::SurfaceData surface = RS::get_singleton()->mesh_get_surface(mesh, i); |
| 1518 | Dictionary data; |
| 1519 | data["format"] = surface.format; |
| 1520 | data["primitive"] = surface.primitive; |
| 1521 | data["vertex_data"] = surface.vertex_data; |
| 1522 | data["vertex_count"] = surface.vertex_count; |
| 1523 | if (surface.skin_data.size()) { |
| 1524 | data["skin_data"] = surface.skin_data; |
| 1525 | } |
| 1526 | if (surface.attribute_data.size()) { |
| 1527 | data["attribute_data"] = surface.attribute_data; |
| 1528 | } |
| 1529 | data["aabb"] = surface.aabb; |
| 1530 | data["uv_scale"] = surface.uv_scale; |
| 1531 | if (surface.index_count) { |
| 1532 | data["index_data"] = surface.index_data; |
| 1533 | data["index_count"] = surface.index_count; |
| 1534 | }; |
| 1535 | |
| 1536 | Array lods; |
| 1537 | for (int j = 0; j < surface.lods.size(); j++) { |
| 1538 | lods.push_back(surface.lods[j].edge_length); |
| 1539 | lods.push_back(surface.lods[j].index_data); |
| 1540 | } |
| 1541 | |
| 1542 | if (lods.size()) { |
| 1543 | data["lods"] = lods; |
| 1544 | } |
| 1545 | |
| 1546 | Array bone_aabbs; |
| 1547 | for (int j = 0; j < surface.bone_aabbs.size(); j++) { |
| 1548 | bone_aabbs.push_back(surface.bone_aabbs[j]); |
| 1549 | } |
| 1550 | if (bone_aabbs.size()) { |
| 1551 | data["bone_aabbs"] = bone_aabbs; |
| 1552 | } |
| 1553 | |
| 1554 | if (surface.blend_shape_data.size()) { |
| 1555 | data["blend_shapes"] = surface.blend_shape_data; |
| 1556 | } |
| 1557 | |
| 1558 | if (surfaces[i].material.is_valid()) { |
| 1559 | data["material"] = surfaces[i].material; |
| 1560 | } |
| 1561 | |
| 1562 | if (!surfaces[i].name.is_empty()) { |
| 1563 | data["name"] = surfaces[i].name; |
| 1564 | } |
| 1565 | |
| 1566 | if (surfaces[i].is_2d) { |
| 1567 | data["2d"] = true; |
| 1568 | } |
| 1569 | |
| 1570 | ret.push_back(data); |
| 1571 | } |
| 1572 | |
| 1573 | return ret; |
| 1574 | } |
| 1575 | |
| 1576 | void ArrayMesh::_create_if_empty() const { |
| 1577 | if (!mesh.is_valid()) { |
| 1578 | mesh = RS::get_singleton()->mesh_create(); |
| 1579 | RS::get_singleton()->mesh_set_blend_shape_mode(mesh, (RS::BlendShapeMode)blend_shape_mode); |
| 1580 | RS::get_singleton()->mesh_set_blend_shape_count(mesh, blend_shapes.size()); |
| 1581 | RS::get_singleton()->mesh_set_path(mesh, get_path()); |
| 1582 | } |
| 1583 | } |
| 1584 | |
| 1585 | void ArrayMesh::_set_surfaces(const Array &p_surfaces) { |
| 1586 | Vector<RS::SurfaceData> surface_data; |
| 1587 | Vector<Ref<Material>> surface_materials; |
| 1588 | Vector<String> surface_names; |
| 1589 | Vector<bool> surface_2d; |
| 1590 | |
| 1591 | for (int i = 0; i < p_surfaces.size(); i++) { |
| 1592 | RS::SurfaceData surface; |
| 1593 | Dictionary d = p_surfaces[i]; |
| 1594 | ERR_FAIL_COND(!d.has("format")); |
| 1595 | ERR_FAIL_COND(!d.has("primitive")); |
| 1596 | ERR_FAIL_COND(!d.has("vertex_data")); |
| 1597 | ERR_FAIL_COND(!d.has("vertex_count")); |
| 1598 | ERR_FAIL_COND(!d.has("aabb")); |
| 1599 | surface.format = d["format"]; |
| 1600 | surface.primitive = RS::PrimitiveType(int(d["primitive"])); |
| 1601 | surface.vertex_data = d["vertex_data"]; |
| 1602 | surface.vertex_count = d["vertex_count"]; |
| 1603 | if (d.has("attribute_data")) { |
| 1604 | surface.attribute_data = d["attribute_data"]; |
| 1605 | } |
| 1606 | if (d.has("skin_data")) { |
| 1607 | surface.skin_data = d["skin_data"]; |
| 1608 | } |
| 1609 | surface.aabb = d["aabb"]; |
| 1610 | |
| 1611 | if (d.has("uv_scale")) { |
| 1612 | surface.uv_scale = d["uv_scale"]; |
| 1613 | } |
| 1614 | |
| 1615 | if (d.has("index_data")) { |
| 1616 | ERR_FAIL_COND(!d.has("index_count")); |
| 1617 | surface.index_data = d["index_data"]; |
| 1618 | surface.index_count = d["index_count"]; |
| 1619 | } |
| 1620 | |
| 1621 | if (d.has("lods")) { |
| 1622 | Array lods = d["lods"]; |
| 1623 | ERR_FAIL_COND(lods.size() & 1); //must be even |
| 1624 | for (int j = 0; j < lods.size(); j += 2) { |
| 1625 | RS::SurfaceData::LOD lod; |
| 1626 | lod.edge_length = lods[j + 0]; |
| 1627 | lod.index_data = lods[j + 1]; |
| 1628 | surface.lods.push_back(lod); |
| 1629 | } |
| 1630 | } |
| 1631 | |
| 1632 | if (d.has("bone_aabbs")) { |
| 1633 | Array bone_aabbs = d["bone_aabbs"]; |
| 1634 | for (int j = 0; j < bone_aabbs.size(); j++) { |
| 1635 | surface.bone_aabbs.push_back(bone_aabbs[j]); |
| 1636 | } |
| 1637 | } |
| 1638 | |
| 1639 | if (d.has("blend_shapes")) { |
| 1640 | surface.blend_shape_data = d["blend_shapes"]; |
| 1641 | } |
| 1642 | |
| 1643 | Ref<Material> material; |
| 1644 | if (d.has("material")) { |
| 1645 | material = d["material"]; |
| 1646 | if (material.is_valid()) { |
| 1647 | surface.material = material->get_rid(); |
| 1648 | } |
| 1649 | } |
| 1650 | |
| 1651 | String surf_name; |
| 1652 | if (d.has("name")) { |
| 1653 | surf_name = d["name"]; |
| 1654 | } |
| 1655 | |
| 1656 | bool _2d = false; |
| 1657 | if (d.has("2d")) { |
| 1658 | _2d = d["2d"]; |
| 1659 | } |
| 1660 | |
| 1661 | #ifndef DISABLE_DEPRECATED |
| 1662 | uint64_t surface_version = surface.format & (ARRAY_FLAG_FORMAT_VERSION_MASK << ARRAY_FLAG_FORMAT_VERSION_SHIFT); |
| 1663 | if (surface_version != ARRAY_FLAG_FORMAT_CURRENT_VERSION) { |
| 1664 | RS::get_singleton()->fix_surface_compatibility(surface, get_path()); |
| 1665 | surface_version = surface.format & (RS::ARRAY_FLAG_FORMAT_VERSION_MASK << RS::ARRAY_FLAG_FORMAT_VERSION_SHIFT); |
| 1666 | ERR_FAIL_COND_MSG(surface_version != RS::ARRAY_FLAG_FORMAT_CURRENT_VERSION, |
| 1667 | vformat("Surface version provided (%d) does not match current version (%d).", |
| 1668 | (surface_version >> RS::ARRAY_FLAG_FORMAT_VERSION_SHIFT) & RS::ARRAY_FLAG_FORMAT_VERSION_MASK, |
| 1669 | (RS::ARRAY_FLAG_FORMAT_CURRENT_VERSION >> RS::ARRAY_FLAG_FORMAT_VERSION_SHIFT) & RS::ARRAY_FLAG_FORMAT_VERSION_MASK)); |
| 1670 | } |
| 1671 | #endif |
| 1672 | |
| 1673 | surface_data.push_back(surface); |
| 1674 | surface_materials.push_back(material); |
| 1675 | surface_names.push_back(surf_name); |
| 1676 | surface_2d.push_back(_2d); |
| 1677 | } |
| 1678 | |
| 1679 | if (mesh.is_valid()) { |
| 1680 | //if mesh exists, it needs to be updated |
| 1681 | RS::get_singleton()->mesh_clear(mesh); |
| 1682 | for (int i = 0; i < surface_data.size(); i++) { |
| 1683 | RS::get_singleton()->mesh_add_surface(mesh, surface_data[i]); |
| 1684 | } |
| 1685 | } else { |
| 1686 | // if mesh does not exist (first time this is loaded, most likely), |
| 1687 | // we can create it with a single call, which is a lot more efficient and thread friendly |
| 1688 | mesh = RS::get_singleton()->mesh_create_from_surfaces(surface_data, blend_shapes.size()); |
| 1689 | RS::get_singleton()->mesh_set_blend_shape_mode(mesh, (RS::BlendShapeMode)blend_shape_mode); |
| 1690 | RS::get_singleton()->mesh_set_path(mesh, get_path()); |
| 1691 | } |
| 1692 | |
| 1693 | surfaces.clear(); |
| 1694 | clear_cache(); |
| 1695 | |
| 1696 | aabb = AABB(); |
| 1697 | for (int i = 0; i < surface_data.size(); i++) { |
| 1698 | Surface s; |
| 1699 | s.aabb = surface_data[i].aabb; |
| 1700 | if (i == 0) { |
| 1701 | aabb = s.aabb; |
| 1702 | } else { |
| 1703 | aabb.merge_with(s.aabb); |
| 1704 | } |
| 1705 | |
| 1706 | s.material = surface_materials[i]; |
| 1707 | s.is_2d = surface_2d[i]; |
| 1708 | s.name = surface_names[i]; |
| 1709 | |
| 1710 | s.format = surface_data[i].format; |
| 1711 | s.primitive = PrimitiveType(surface_data[i].primitive); |
| 1712 | s.array_length = surface_data[i].vertex_count; |
| 1713 | s.index_array_length = surface_data[i].index_count; |
| 1714 | |
| 1715 | surfaces.push_back(s); |
| 1716 | } |
| 1717 | } |
| 1718 | |
| 1719 | bool ArrayMesh::_get(const StringName &p_name, Variant &r_ret) const { |
| 1720 | if (_is_generated()) { |
| 1721 | return false; |
| 1722 | } |
| 1723 | |
| 1724 | String sname = p_name; |
| 1725 | if (sname.begins_with("surface_")) { |
| 1726 | int sl = sname.find_char('/'); |
| 1727 | if (sl == -1) { |
| 1728 | return false; |
| 1729 | } |
| 1730 | int idx = sname.substr(8, sl - 8).to_int(); |
| 1731 | String what = sname.get_slicec('/', 1); |
| 1732 | if (what == "material") { |
| 1733 | r_ret = surface_get_material(idx); |
| 1734 | } else if (what == "name") { |
| 1735 | r_ret = surface_get_name(idx); |
| 1736 | } |
| 1737 | return true; |
| 1738 | } |
| 1739 | |
| 1740 | return false; |
| 1741 | } |
| 1742 | |
| 1743 | void ArrayMesh::reset_state() { |
| 1744 | clear_surfaces(); |
| 1745 | clear_blend_shapes(); |
| 1746 | |
| 1747 | aabb = AABB(); |
| 1748 | blend_shape_mode = BLEND_SHAPE_MODE_RELATIVE; |
| 1749 | custom_aabb = AABB(); |
| 1750 | } |
| 1751 | |
| 1752 | void ArrayMesh::_get_property_list(List<PropertyInfo> *p_list) const { |
| 1753 | if (_is_generated()) { |
| 1754 | return; |
| 1755 | } |
| 1756 | |
| 1757 | for (int i = 0; i < surfaces.size(); i++) { |
| 1758 | p_list->push_back(PropertyInfo(Variant::STRING, "surface_" + itos(i) + "/name", PROPERTY_HINT_NO_NODEPATH, "", PROPERTY_USAGE_EDITOR)); |
| 1759 | if (surfaces[i].is_2d) { |
| 1760 | p_list->push_back(PropertyInfo(Variant::OBJECT, "surface_" + itos(i) + "/material", PROPERTY_HINT_RESOURCE_TYPE, "CanvasItemMaterial,ShaderMaterial", PROPERTY_USAGE_EDITOR)); |
| 1761 | } else { |
| 1762 | p_list->push_back(PropertyInfo(Variant::OBJECT, "surface_" + itos(i) + "/material", PROPERTY_HINT_RESOURCE_TYPE, "BaseMaterial3D,ShaderMaterial", PROPERTY_USAGE_EDITOR)); |
| 1763 | } |
| 1764 | } |
| 1765 | } |
| 1766 | |
| 1767 | void ArrayMesh::_recompute_aabb() { |
| 1768 | // regenerate AABB |
| 1769 | aabb = AABB(); |
| 1770 | |
| 1771 | for (int i = 0; i < surfaces.size(); i++) { |
| 1772 | if (i == 0) { |
| 1773 | aabb = surfaces[i].aabb; |
| 1774 | } else { |
| 1775 | aabb.merge_with(surfaces[i].aabb); |
| 1776 | } |
| 1777 | } |
| 1778 | } |
| 1779 | |
| 1780 | // TODO: Need to add binding to add_surface using future MeshSurfaceData object. |
| 1781 | void ArrayMesh::add_surface(BitField<ArrayFormat> p_format, PrimitiveType p_primitive, const Vector<uint8_t> &p_array, const Vector<uint8_t> &p_attribute_array, const Vector<uint8_t> &p_skin_array, int p_vertex_count, const Vector<uint8_t> &p_index_array, int p_index_count, const AABB &p_aabb, const Vector<uint8_t> &p_blend_shape_data, const Vector<AABB> &p_bone_aabbs, const Vector<RS::SurfaceData::LOD> &p_lods, const Vector4 p_uv_scale) { |
| 1782 | ERR_FAIL_COND(surfaces.size() == RS::MAX_MESH_SURFACES); |
| 1783 | _create_if_empty(); |
| 1784 | |
| 1785 | Surface s; |
| 1786 | s.aabb = p_aabb; |
| 1787 | s.is_2d = p_format & ARRAY_FLAG_USE_2D_VERTICES; |
| 1788 | s.primitive = p_primitive; |
| 1789 | s.array_length = p_vertex_count; |
| 1790 | s.index_array_length = p_index_count; |
| 1791 | s.format = p_format; |
| 1792 | |
| 1793 | surfaces.push_back(s); |
| 1794 | _recompute_aabb(); |
| 1795 | |
| 1796 | RS::SurfaceData sd; |
| 1797 | sd.format = p_format; |
| 1798 | sd.primitive = RS::PrimitiveType(p_primitive); |
| 1799 | sd.aabb = p_aabb; |
| 1800 | sd.vertex_count = p_vertex_count; |
| 1801 | sd.vertex_data = p_array; |
| 1802 | sd.attribute_data = p_attribute_array; |
| 1803 | sd.skin_data = p_skin_array; |
| 1804 | sd.index_count = p_index_count; |
| 1805 | sd.index_data = p_index_array; |
| 1806 | sd.blend_shape_data = p_blend_shape_data; |
| 1807 | sd.bone_aabbs = p_bone_aabbs; |
| 1808 | sd.lods = p_lods; |
| 1809 | sd.uv_scale = p_uv_scale; |
| 1810 | |
| 1811 | RenderingServer::get_singleton()->mesh_add_surface(mesh, sd); |
| 1812 | |
| 1813 | clear_cache(); |
| 1814 | notify_property_list_changed(); |
| 1815 | emit_changed(); |
| 1816 | } |
| 1817 | |
| 1818 | void ArrayMesh::add_surface_from_arrays(PrimitiveType p_primitive, const Array &p_arrays, const TypedArray<Array> &p_blend_shapes, const Dictionary &p_lods, BitField<ArrayFormat> p_flags) { |
| 1819 | ERR_FAIL_COND(p_blend_shapes.size() != blend_shapes.size()); |
| 1820 | ERR_FAIL_COND(p_arrays.size() != ARRAY_MAX); |
| 1821 | |
| 1822 | RS::SurfaceData surface; |
| 1823 | |
| 1824 | Error err = RS::get_singleton()->mesh_create_surface_data_from_arrays(&surface, (RenderingServer::PrimitiveType)p_primitive, p_arrays, p_blend_shapes, p_lods, p_flags); |
| 1825 | ERR_FAIL_COND(err != OK); |
| 1826 | |
| 1827 | /* Debug code. |
| 1828 | print_line("format: " + itos(surface.format)); |
| 1829 | print_line("aabb: " + surface.aabb); |
| 1830 | print_line("array size: " + itos(surface.vertex_data.size())); |
| 1831 | print_line("vertex count: " + itos(surface.vertex_count)); |
| 1832 | print_line("index size: " + itos(surface.index_data.size())); |
| 1833 | print_line("index count: " + itos(surface.index_count)); |
| 1834 | print_line("primitive: " + itos(surface.primitive)); |
| 1835 | */ |
| 1836 | |
| 1837 | add_surface(surface.format, PrimitiveType(surface.primitive), surface.vertex_data, surface.attribute_data, surface.skin_data, surface.vertex_count, surface.index_data, surface.index_count, surface.aabb, surface.blend_shape_data, surface.bone_aabbs, surface.lods, surface.uv_scale); |
| 1838 | } |
| 1839 | |
| 1840 | Array ArrayMesh::surface_get_arrays(int p_surface) const { |
| 1841 | ERR_FAIL_INDEX_V(p_surface, surfaces.size(), Array()); |
| 1842 | return RenderingServer::get_singleton()->mesh_surface_get_arrays(mesh, p_surface); |
| 1843 | } |
| 1844 | |
| 1845 | TypedArray<Array> ArrayMesh::surface_get_blend_shape_arrays(int p_surface) const { |
| 1846 | ERR_FAIL_INDEX_V(p_surface, surfaces.size(), TypedArray<Array>()); |
| 1847 | return RenderingServer::get_singleton()->mesh_surface_get_blend_shape_arrays(mesh, p_surface); |
| 1848 | } |
| 1849 | |
| 1850 | Dictionary ArrayMesh::surface_get_lods(int p_surface) const { |
| 1851 | ERR_FAIL_INDEX_V(p_surface, surfaces.size(), Dictionary()); |
| 1852 | return RenderingServer::get_singleton()->mesh_surface_get_lods(mesh, p_surface); |
| 1853 | } |
| 1854 | |
| 1855 | int ArrayMesh::get_surface_count() const { |
| 1856 | return surfaces.size(); |
| 1857 | } |
| 1858 | |
| 1859 | void ArrayMesh::add_blend_shape(const StringName &p_name) { |
| 1860 | ERR_FAIL_COND_MSG(surfaces.size(), "Can't add a shape key count if surfaces are already created."); |
| 1861 | |
| 1862 | StringName shape_name = p_name; |
| 1863 | |
| 1864 | if (blend_shapes.has(shape_name)) { |
| 1865 | int count = 2; |
| 1866 | do { |
| 1867 | shape_name = String(p_name) + " " + itos(count); |
| 1868 | count++; |
| 1869 | } while (blend_shapes.has(shape_name)); |
| 1870 | } |
| 1871 | |
| 1872 | blend_shapes.push_back(shape_name); |
| 1873 | |
| 1874 | if (mesh.is_valid()) { |
| 1875 | RS::get_singleton()->mesh_set_blend_shape_count(mesh, blend_shapes.size()); |
| 1876 | } |
| 1877 | } |
| 1878 | |
| 1879 | int ArrayMesh::get_blend_shape_count() const { |
| 1880 | return blend_shapes.size(); |
| 1881 | } |
| 1882 | |
| 1883 | StringName ArrayMesh::get_blend_shape_name(int p_index) const { |
| 1884 | ERR_FAIL_INDEX_V(p_index, blend_shapes.size(), StringName()); |
| 1885 | return blend_shapes[p_index]; |
| 1886 | } |
| 1887 | |
| 1888 | void ArrayMesh::set_blend_shape_name(int p_index, const StringName &p_name) { |
| 1889 | ERR_FAIL_INDEX(p_index, blend_shapes.size()); |
| 1890 | |
| 1891 | StringName shape_name = p_name; |
| 1892 | int found = blend_shapes.find(shape_name); |
| 1893 | if (found != -1 && found != p_index) { |
| 1894 | int count = 2; |
| 1895 | do { |
| 1896 | shape_name = String(p_name) + " " + itos(count); |
| 1897 | count++; |
| 1898 | } while (blend_shapes.has(shape_name)); |
| 1899 | } |
| 1900 | |
| 1901 | blend_shapes.write[p_index] = shape_name; |
| 1902 | } |
| 1903 | |
| 1904 | void ArrayMesh::clear_blend_shapes() { |
| 1905 | ERR_FAIL_COND_MSG(surfaces.size(), "Can't set shape key count if surfaces are already created."); |
| 1906 | |
| 1907 | blend_shapes.clear(); |
| 1908 | |
| 1909 | if (mesh.is_valid()) { |
| 1910 | RS::get_singleton()->mesh_set_blend_shape_count(mesh, 0); |
| 1911 | } |
| 1912 | } |
| 1913 | |
| 1914 | void ArrayMesh::set_blend_shape_mode(BlendShapeMode p_mode) { |
| 1915 | blend_shape_mode = p_mode; |
| 1916 | if (mesh.is_valid()) { |
| 1917 | RS::get_singleton()->mesh_set_blend_shape_mode(mesh, (RS::BlendShapeMode)p_mode); |
| 1918 | } |
| 1919 | } |
| 1920 | |
| 1921 | ArrayMesh::BlendShapeMode ArrayMesh::get_blend_shape_mode() const { |
| 1922 | return blend_shape_mode; |
| 1923 | } |
| 1924 | |
| 1925 | int ArrayMesh::surface_get_array_len(int p_idx) const { |
| 1926 | ERR_FAIL_INDEX_V(p_idx, surfaces.size(), -1); |
| 1927 | return surfaces[p_idx].array_length; |
| 1928 | } |
| 1929 | |
| 1930 | int ArrayMesh::surface_get_array_index_len(int p_idx) const { |
| 1931 | ERR_FAIL_INDEX_V(p_idx, surfaces.size(), -1); |
| 1932 | return surfaces[p_idx].index_array_length; |
| 1933 | } |
| 1934 | |
| 1935 | BitField<Mesh::ArrayFormat> ArrayMesh::surface_get_format(int p_idx) const { |
| 1936 | ERR_FAIL_INDEX_V(p_idx, surfaces.size(), 0); |
| 1937 | return surfaces[p_idx].format; |
| 1938 | } |
| 1939 | |
| 1940 | ArrayMesh::PrimitiveType ArrayMesh::surface_get_primitive_type(int p_idx) const { |
| 1941 | ERR_FAIL_INDEX_V(p_idx, surfaces.size(), PRIMITIVE_LINES); |
| 1942 | return surfaces[p_idx].primitive; |
| 1943 | } |
| 1944 | |
| 1945 | void ArrayMesh::surface_set_material(int p_idx, const Ref<Material> &p_material) { |
| 1946 | ERR_FAIL_INDEX(p_idx, surfaces.size()); |
| 1947 | if (surfaces[p_idx].material == p_material) { |
| 1948 | return; |
| 1949 | } |
| 1950 | surfaces.write[p_idx].material = p_material; |
| 1951 | RenderingServer::get_singleton()->mesh_surface_set_material(mesh, p_idx, p_material.is_null() ? RID() : p_material->get_rid()); |
| 1952 | |
| 1953 | emit_changed(); |
| 1954 | } |
| 1955 | |
| 1956 | int ArrayMesh::surface_find_by_name(const String &p_name) const { |
| 1957 | for (int i = 0; i < surfaces.size(); i++) { |
| 1958 | if (surfaces[i].name == p_name) { |
| 1959 | return i; |
| 1960 | } |
| 1961 | } |
| 1962 | return -1; |
| 1963 | } |
| 1964 | |
| 1965 | void ArrayMesh::surface_set_name(int p_idx, const String &p_name) { |
| 1966 | ERR_FAIL_INDEX(p_idx, surfaces.size()); |
| 1967 | |
| 1968 | surfaces.write[p_idx].name = p_name; |
| 1969 | emit_changed(); |
| 1970 | } |
| 1971 | |
| 1972 | String ArrayMesh::surface_get_name(int p_idx) const { |
| 1973 | ERR_FAIL_INDEX_V(p_idx, surfaces.size(), String()); |
| 1974 | return surfaces[p_idx].name; |
| 1975 | } |
| 1976 | |
| 1977 | void ArrayMesh::surface_update_vertex_region(int p_surface, int p_offset, const Vector<uint8_t> &p_data) { |
| 1978 | ERR_FAIL_INDEX(p_surface, surfaces.size()); |
| 1979 | RS::get_singleton()->mesh_surface_update_vertex_region(mesh, p_surface, p_offset, p_data); |
| 1980 | emit_changed(); |
| 1981 | } |
| 1982 | |
| 1983 | void ArrayMesh::surface_update_attribute_region(int p_surface, int p_offset, const Vector<uint8_t> &p_data) { |
| 1984 | ERR_FAIL_INDEX(p_surface, surfaces.size()); |
| 1985 | RS::get_singleton()->mesh_surface_update_attribute_region(mesh, p_surface, p_offset, p_data); |
| 1986 | emit_changed(); |
| 1987 | } |
| 1988 | |
| 1989 | void ArrayMesh::surface_update_skin_region(int p_surface, int p_offset, const Vector<uint8_t> &p_data) { |
| 1990 | ERR_FAIL_INDEX(p_surface, surfaces.size()); |
| 1991 | RS::get_singleton()->mesh_surface_update_skin_region(mesh, p_surface, p_offset, p_data); |
| 1992 | emit_changed(); |
| 1993 | } |
| 1994 | |
| 1995 | void ArrayMesh::surface_set_custom_aabb(int p_idx, const AABB &p_aabb) { |
| 1996 | ERR_FAIL_INDEX(p_idx, surfaces.size()); |
| 1997 | surfaces.write[p_idx].aabb = p_aabb; |
| 1998 | // set custom aabb too? |
| 1999 | emit_changed(); |
| 2000 | } |
| 2001 | |
| 2002 | Ref<Material> ArrayMesh::surface_get_material(int p_idx) const { |
| 2003 | ERR_FAIL_INDEX_V(p_idx, surfaces.size(), Ref<Material>()); |
| 2004 | return surfaces[p_idx].material; |
| 2005 | } |
| 2006 | |
| 2007 | RID ArrayMesh::get_rid() const { |
| 2008 | _create_if_empty(); |
| 2009 | return mesh; |
| 2010 | } |
| 2011 | |
| 2012 | AABB ArrayMesh::get_aabb() const { |
| 2013 | return aabb; |
| 2014 | } |
| 2015 | |
| 2016 | void ArrayMesh::clear_surfaces() { |
| 2017 | if (!mesh.is_valid()) { |
| 2018 | return; |
| 2019 | } |
| 2020 | RS::get_singleton()->mesh_clear(mesh); |
| 2021 | surfaces.clear(); |
| 2022 | aabb = AABB(); |
| 2023 | } |
| 2024 | |
| 2025 | void ArrayMesh::surface_remove(int p_surface) { |
| 2026 | ERR_FAIL_INDEX(p_surface, surfaces.size()); |
| 2027 | RS::get_singleton()->mesh_surface_remove(mesh, p_surface); |
| 2028 | surfaces.remove_at(p_surface); |
| 2029 | |
| 2030 | clear_cache(); |
| 2031 | _recompute_aabb(); |
| 2032 | notify_property_list_changed(); |
| 2033 | emit_changed(); |
| 2034 | } |
| 2035 | |
| 2036 | void ArrayMesh::set_custom_aabb(const AABB &p_custom) { |
| 2037 | _create_if_empty(); |
| 2038 | custom_aabb = p_custom; |
| 2039 | RS::get_singleton()->mesh_set_custom_aabb(mesh, custom_aabb); |
| 2040 | emit_changed(); |
| 2041 | } |
| 2042 | |
| 2043 | AABB ArrayMesh::get_custom_aabb() const { |
| 2044 | return custom_aabb; |
| 2045 | } |
| 2046 | |
| 2047 | void ArrayMesh::regen_normal_maps() { |
| 2048 | if (surfaces.is_empty()) { |
| 2049 | return; |
| 2050 | } |
| 2051 | Vector<Ref<SurfaceTool>> surfs; |
| 2052 | Vector<uint64_t> formats; |
| 2053 | for (int i = 0; i < get_surface_count(); i++) { |
| 2054 | Ref<SurfaceTool> st = memnew(SurfaceTool); |
| 2055 | st->create_from(Ref<ArrayMesh>(this), i); |
| 2056 | surfs.push_back(st); |
| 2057 | formats.push_back(surface_get_format(i)); |
| 2058 | } |
| 2059 | |
| 2060 | clear_surfaces(); |
| 2061 | |
| 2062 | for (int i = 0; i < surfs.size(); i++) { |
| 2063 | surfs.write[i]->generate_tangents(); |
| 2064 | surfs.write[i]->commit(Ref<ArrayMesh>(this), formats[i]); |
| 2065 | } |
| 2066 | } |
| 2067 | |
| 2068 | //dirty hack |
| 2069 | bool (*array_mesh_lightmap_unwrap_callback)(float p_texel_size, const float *p_vertices, const float *p_normals, int p_vertex_count, const int *p_indices, int p_index_count, const uint8_t *p_cache_data, bool *r_use_cache, uint8_t **r_mesh_cache, int *r_mesh_cache_size, float **r_uv, int **r_vertex, int *r_vertex_count, int **r_index, int *r_index_count, int *r_size_hint_x, int *r_size_hint_y) = nullptr; |
| 2070 | |
| 2071 | struct ArrayMeshLightmapSurface { |
| 2072 | Ref<Material> material; |
| 2073 | LocalVector<SurfaceTool::Vertex> vertices; |
| 2074 | Mesh::PrimitiveType primitive = Mesh::PrimitiveType::PRIMITIVE_MAX; |
| 2075 | uint64_t format = 0; |
| 2076 | }; |
| 2077 | |
| 2078 | Error ArrayMesh::lightmap_unwrap(const Transform3D &p_base_transform, float p_texel_size) { |
| 2079 | Vector<uint8_t> null_cache; |
| 2080 | return lightmap_unwrap_cached(p_base_transform, p_texel_size, null_cache, null_cache, false); |
| 2081 | } |
| 2082 | |
| 2083 | Error ArrayMesh::lightmap_unwrap_cached(const Transform3D &p_base_transform, float p_texel_size, const Vector<uint8_t> &p_src_cache, Vector<uint8_t> &r_dst_cache, bool p_generate_cache) { |
| 2084 | ERR_FAIL_NULL_V(array_mesh_lightmap_unwrap_callback, ERR_UNCONFIGURED); |
| 2085 | ERR_FAIL_COND_V_MSG(blend_shapes.size() != 0, ERR_UNAVAILABLE, "Can't unwrap mesh with blend shapes."); |
| 2086 | ERR_FAIL_COND_V_MSG(p_texel_size <= 0.0f, ERR_PARAMETER_RANGE_ERROR, "Texel size must be greater than 0."); |
| 2087 | |
| 2088 | LocalVector<float> vertices; |
| 2089 | LocalVector<float> normals; |
| 2090 | LocalVector<int> indices; |
| 2091 | LocalVector<float> uv; |
| 2092 | LocalVector<Pair<int, int>> uv_indices; |
| 2093 | |
| 2094 | Vector<ArrayMeshLightmapSurface> lightmap_surfaces; |
| 2095 | |
| 2096 | // Keep only the scale |
| 2097 | Basis basis = p_base_transform.get_basis(); |
| 2098 | Vector3 scale = Vector3(basis.get_column(0).length(), basis.get_column(1).length(), basis.get_column(2).length()); |
| 2099 | |
| 2100 | Transform3D transform; |
| 2101 | transform.scale(scale); |
| 2102 | |
| 2103 | Basis normal_basis = transform.basis.inverse().transposed(); |
| 2104 | |
| 2105 | for (int i = 0; i < get_surface_count(); i++) { |
| 2106 | ArrayMeshLightmapSurface s; |
| 2107 | s.primitive = surface_get_primitive_type(i); |
| 2108 | |
| 2109 | ERR_FAIL_COND_V_MSG(s.primitive != Mesh::PRIMITIVE_TRIANGLES, ERR_UNAVAILABLE, "Only triangles are supported for lightmap unwrap."); |
| 2110 | s.format = surface_get_format(i); |
| 2111 | ERR_FAIL_COND_V_MSG(!(s.format & ARRAY_FORMAT_NORMAL), ERR_UNAVAILABLE, "Normals are required for lightmap unwrap."); |
| 2112 | |
| 2113 | Array arrays = surface_get_arrays(i); |
| 2114 | s.material = surface_get_material(i); |
| 2115 | SurfaceTool::create_vertex_array_from_arrays(arrays, s.vertices, &s.format); |
| 2116 | |
| 2117 | PackedVector3Array rvertices = arrays[Mesh::ARRAY_VERTEX]; |
| 2118 | int vc = rvertices.size(); |
| 2119 | |
| 2120 | PackedVector3Array rnormals = arrays[Mesh::ARRAY_NORMAL]; |
| 2121 | |
| 2122 | int vertex_ofs = vertices.size() / 3; |
| 2123 | |
| 2124 | vertices.resize((vertex_ofs + vc) * 3); |
| 2125 | normals.resize((vertex_ofs + vc) * 3); |
| 2126 | uv_indices.resize(vertex_ofs + vc); |
| 2127 | |
| 2128 | for (int j = 0; j < vc; j++) { |
| 2129 | Vector3 v = transform.xform(rvertices[j]); |
| 2130 | Vector3 n = normal_basis.xform(rnormals[j]).normalized(); |
| 2131 | |
| 2132 | vertices[(j + vertex_ofs) * 3 + 0] = v.x; |
| 2133 | vertices[(j + vertex_ofs) * 3 + 1] = v.y; |
| 2134 | vertices[(j + vertex_ofs) * 3 + 2] = v.z; |
| 2135 | normals[(j + vertex_ofs) * 3 + 0] = n.x; |
| 2136 | normals[(j + vertex_ofs) * 3 + 1] = n.y; |
| 2137 | normals[(j + vertex_ofs) * 3 + 2] = n.z; |
| 2138 | uv_indices[j + vertex_ofs] = Pair<int, int>(i, j); |
| 2139 | } |
| 2140 | |
| 2141 | PackedInt32Array rindices = arrays[Mesh::ARRAY_INDEX]; |
| 2142 | int ic = rindices.size(); |
| 2143 | |
| 2144 | float eps = 1.19209290e-7F; // Taken from xatlas.h |
| 2145 | if (ic == 0) { |
| 2146 | for (int j = 0; j < vc / 3; j++) { |
| 2147 | Vector3 p0 = transform.xform(rvertices[j * 3 + 0]); |
| 2148 | Vector3 p1 = transform.xform(rvertices[j * 3 + 1]); |
| 2149 | Vector3 p2 = transform.xform(rvertices[j * 3 + 2]); |
| 2150 | |
| 2151 | if ((p0 - p1).length_squared() < eps || (p1 - p2).length_squared() < eps || (p2 - p0).length_squared() < eps) { |
| 2152 | continue; |
| 2153 | } |
| 2154 | |
| 2155 | indices.push_back(vertex_ofs + j * 3 + 0); |
| 2156 | indices.push_back(vertex_ofs + j * 3 + 1); |
| 2157 | indices.push_back(vertex_ofs + j * 3 + 2); |
| 2158 | } |
| 2159 | |
| 2160 | } else { |
| 2161 | for (int j = 0; j < ic / 3; j++) { |
| 2162 | Vector3 p0 = transform.xform(rvertices[rindices[j * 3 + 0]]); |
| 2163 | Vector3 p1 = transform.xform(rvertices[rindices[j * 3 + 1]]); |
| 2164 | Vector3 p2 = transform.xform(rvertices[rindices[j * 3 + 2]]); |
| 2165 | |
| 2166 | if ((p0 - p1).length_squared() < eps || (p1 - p2).length_squared() < eps || (p2 - p0).length_squared() < eps) { |
| 2167 | continue; |
| 2168 | } |
| 2169 | |
| 2170 | indices.push_back(vertex_ofs + rindices[j * 3 + 0]); |
| 2171 | indices.push_back(vertex_ofs + rindices[j * 3 + 1]); |
| 2172 | indices.push_back(vertex_ofs + rindices[j * 3 + 2]); |
| 2173 | } |
| 2174 | } |
| 2175 | |
| 2176 | lightmap_surfaces.push_back(s); |
| 2177 | } |
| 2178 | |
| 2179 | //unwrap |
| 2180 | |
| 2181 | bool use_cache = p_generate_cache; // Used to request cache generation and to know if cache was used |
| 2182 | uint8_t *gen_cache; |
| 2183 | int gen_cache_size; |
| 2184 | float *gen_uvs; |
| 2185 | int *gen_vertices; |
| 2186 | int *gen_indices; |
| 2187 | int gen_vertex_count; |
| 2188 | int gen_index_count; |
| 2189 | int size_x; |
| 2190 | int size_y; |
| 2191 | |
| 2192 | bool ok = array_mesh_lightmap_unwrap_callback(p_texel_size, vertices.ptr(), normals.ptr(), vertices.size() / 3, indices.ptr(), indices.size(), p_src_cache.ptr(), &use_cache, &gen_cache, &gen_cache_size, &gen_uvs, &gen_vertices, &gen_vertex_count, &gen_indices, &gen_index_count, &size_x, &size_y); |
| 2193 | |
| 2194 | if (!ok) { |
| 2195 | return ERR_CANT_CREATE; |
| 2196 | } |
| 2197 | |
| 2198 | clear_surfaces(); |
| 2199 | |
| 2200 | //create surfacetools for each surface.. |
| 2201 | LocalVector<Ref<SurfaceTool>> surfaces_tools; |
| 2202 | |
| 2203 | for (int i = 0; i < lightmap_surfaces.size(); i++) { |
| 2204 | Ref<SurfaceTool> st; |
| 2205 | st.instantiate(); |
| 2206 | st->begin(Mesh::PRIMITIVE_TRIANGLES); |
| 2207 | st->set_material(lightmap_surfaces[i].material); |
| 2208 | surfaces_tools.push_back(st); //stay there |
| 2209 | } |
| 2210 | |
| 2211 | print_verbose("Mesh: Gen indices: " + itos(gen_index_count)); |
| 2212 | |
| 2213 | //go through all indices |
| 2214 | for (int i = 0; i < gen_index_count; i += 3) { |
| 2215 | ERR_FAIL_INDEX_V(gen_vertices[gen_indices[i + 0]], (int)uv_indices.size(), ERR_BUG); |
| 2216 | ERR_FAIL_INDEX_V(gen_vertices[gen_indices[i + 1]], (int)uv_indices.size(), ERR_BUG); |
| 2217 | ERR_FAIL_INDEX_V(gen_vertices[gen_indices[i + 2]], (int)uv_indices.size(), ERR_BUG); |
| 2218 | |
| 2219 | ERR_FAIL_COND_V(uv_indices[gen_vertices[gen_indices[i + 0]]].first != uv_indices[gen_vertices[gen_indices[i + 1]]].first || uv_indices[gen_vertices[gen_indices[i + 0]]].first != uv_indices[gen_vertices[gen_indices[i + 2]]].first, ERR_BUG); |
| 2220 | |
| 2221 | int surface = uv_indices[gen_vertices[gen_indices[i + 0]]].first; |
| 2222 | |
| 2223 | for (int j = 0; j < 3; j++) { |
| 2224 | SurfaceTool::Vertex v = lightmap_surfaces[surface].vertices[uv_indices[gen_vertices[gen_indices[i + j]]].second]; |
| 2225 | |
| 2226 | if (lightmap_surfaces[surface].format & ARRAY_FORMAT_COLOR) { |
| 2227 | surfaces_tools[surface]->set_color(v.color); |
| 2228 | } |
| 2229 | if (lightmap_surfaces[surface].format & ARRAY_FORMAT_TEX_UV) { |
| 2230 | surfaces_tools[surface]->set_uv(v.uv); |
| 2231 | } |
| 2232 | if (lightmap_surfaces[surface].format & ARRAY_FORMAT_NORMAL) { |
| 2233 | surfaces_tools[surface]->set_normal(v.normal); |
| 2234 | } |
| 2235 | if (lightmap_surfaces[surface].format & ARRAY_FORMAT_TANGENT) { |
| 2236 | Plane t; |
| 2237 | t.normal = v.tangent; |
| 2238 | t.d = v.binormal.dot(v.normal.cross(v.tangent)) < 0 ? -1 : 1; |
| 2239 | surfaces_tools[surface]->set_tangent(t); |
| 2240 | } |
| 2241 | if (lightmap_surfaces[surface].format & ARRAY_FORMAT_BONES) { |
| 2242 | surfaces_tools[surface]->set_bones(v.bones); |
| 2243 | } |
| 2244 | if (lightmap_surfaces[surface].format & ARRAY_FORMAT_WEIGHTS) { |
| 2245 | surfaces_tools[surface]->set_weights(v.weights); |
| 2246 | } |
| 2247 | |
| 2248 | Vector2 uv2(gen_uvs[gen_indices[i + j] * 2 + 0], gen_uvs[gen_indices[i + j] * 2 + 1]); |
| 2249 | surfaces_tools[surface]->set_uv2(uv2); |
| 2250 | |
| 2251 | surfaces_tools[surface]->add_vertex(v.vertex); |
| 2252 | } |
| 2253 | } |
| 2254 | |
| 2255 | //generate surfaces |
| 2256 | for (unsigned int i = 0; i < surfaces_tools.size(); i++) { |
| 2257 | surfaces_tools[i]->index(); |
| 2258 | surfaces_tools[i]->commit(Ref<ArrayMesh>((ArrayMesh *)this), lightmap_surfaces[i].format); |
| 2259 | } |
| 2260 | |
| 2261 | set_lightmap_size_hint(Size2(size_x, size_y)); |
| 2262 | |
| 2263 | if (gen_cache_size > 0) { |
| 2264 | r_dst_cache.resize(gen_cache_size); |
| 2265 | memcpy(r_dst_cache.ptrw(), gen_cache, gen_cache_size); |
| 2266 | memfree(gen_cache); |
| 2267 | } |
| 2268 | |
| 2269 | if (!use_cache) { |
| 2270 | // Cache was not used, free the buffers |
| 2271 | memfree(gen_vertices); |
| 2272 | memfree(gen_indices); |
| 2273 | memfree(gen_uvs); |
| 2274 | } |
| 2275 | |
| 2276 | return OK; |
| 2277 | } |
| 2278 | |
| 2279 | void ArrayMesh::set_shadow_mesh(const Ref<ArrayMesh> &p_mesh) { |
| 2280 | ERR_FAIL_COND_MSG(p_mesh == this, "Cannot set a mesh as its own shadow mesh."); |
| 2281 | shadow_mesh = p_mesh; |
| 2282 | if (shadow_mesh.is_valid()) { |
| 2283 | RS::get_singleton()->mesh_set_shadow_mesh(mesh, shadow_mesh->get_rid()); |
| 2284 | } else { |
| 2285 | RS::get_singleton()->mesh_set_shadow_mesh(mesh, RID()); |
| 2286 | } |
| 2287 | } |
| 2288 | |
| 2289 | Ref<ArrayMesh> ArrayMesh::get_shadow_mesh() const { |
| 2290 | return shadow_mesh; |
| 2291 | } |
| 2292 | |
| 2293 | void ArrayMesh::_bind_methods() { |
| 2294 | ClassDB::bind_method(D_METHOD("add_blend_shape", "name"), &ArrayMesh::add_blend_shape); |
| 2295 | ClassDB::bind_method(D_METHOD("get_blend_shape_count"), &ArrayMesh::get_blend_shape_count); |
| 2296 | ClassDB::bind_method(D_METHOD("get_blend_shape_name", "index"), &ArrayMesh::get_blend_shape_name); |
| 2297 | ClassDB::bind_method(D_METHOD("set_blend_shape_name", "index", "name"), &ArrayMesh::set_blend_shape_name); |
| 2298 | ClassDB::bind_method(D_METHOD("clear_blend_shapes"), &ArrayMesh::clear_blend_shapes); |
| 2299 | ClassDB::bind_method(D_METHOD("set_blend_shape_mode", "mode"), &ArrayMesh::set_blend_shape_mode); |
| 2300 | ClassDB::bind_method(D_METHOD("get_blend_shape_mode"), &ArrayMesh::get_blend_shape_mode); |
| 2301 | |
| 2302 | ClassDB::bind_method(D_METHOD("add_surface_from_arrays", "primitive", "arrays", "blend_shapes", "lods", "flags"), &ArrayMesh::add_surface_from_arrays, DEFVAL(Array()), DEFVAL(Dictionary()), DEFVAL(0)); |
| 2303 | ClassDB::bind_method(D_METHOD("clear_surfaces"), &ArrayMesh::clear_surfaces); |
| 2304 | ClassDB::bind_method(D_METHOD("surface_remove", "surf_idx"), &ArrayMesh::surface_remove); |
| 2305 | ClassDB::bind_method(D_METHOD("surface_update_vertex_region", "surf_idx", "offset", "data"), &ArrayMesh::surface_update_vertex_region); |
| 2306 | ClassDB::bind_method(D_METHOD("surface_update_attribute_region", "surf_idx", "offset", "data"), &ArrayMesh::surface_update_attribute_region); |
| 2307 | ClassDB::bind_method(D_METHOD("surface_update_skin_region", "surf_idx", "offset", "data"), &ArrayMesh::surface_update_skin_region); |
| 2308 | ClassDB::bind_method(D_METHOD("surface_get_array_len", "surf_idx"), &ArrayMesh::surface_get_array_len); |
| 2309 | ClassDB::bind_method(D_METHOD("surface_get_array_index_len", "surf_idx"), &ArrayMesh::surface_get_array_index_len); |
| 2310 | ClassDB::bind_method(D_METHOD("surface_get_format", "surf_idx"), &ArrayMesh::surface_get_format); |
| 2311 | ClassDB::bind_method(D_METHOD("surface_get_primitive_type", "surf_idx"), &ArrayMesh::surface_get_primitive_type); |
| 2312 | ClassDB::bind_method(D_METHOD("surface_find_by_name", "name"), &ArrayMesh::surface_find_by_name); |
| 2313 | ClassDB::bind_method(D_METHOD("surface_set_name", "surf_idx", "name"), &ArrayMesh::surface_set_name); |
| 2314 | ClassDB::bind_method(D_METHOD("surface_get_name", "surf_idx"), &ArrayMesh::surface_get_name); |
| 2315 | #ifndef PHYSICS_3D_DISABLED |
| 2316 | ClassDB::bind_method(D_METHOD("create_trimesh_shape"), &ArrayMesh::create_trimesh_shape); |
| 2317 | ClassDB::bind_method(D_METHOD("create_convex_shape", "clean", "simplify"), &ArrayMesh::create_convex_shape, DEFVAL(true), DEFVAL(false)); |
| 2318 | #endif // PHYSICS_3D_DISABLED |
| 2319 | ClassDB::bind_method(D_METHOD("create_outline", "margin"), &ArrayMesh::create_outline); |
| 2320 | ClassDB::bind_method(D_METHOD("regen_normal_maps"), &ArrayMesh::regen_normal_maps); |
| 2321 | ClassDB::set_method_flags(get_class_static(), StringName("regen_normal_maps"), METHOD_FLAGS_DEFAULT | METHOD_FLAG_EDITOR); |
| 2322 | ClassDB::bind_method(D_METHOD("lightmap_unwrap", "transform", "texel_size"), &ArrayMesh::lightmap_unwrap); |
| 2323 | ClassDB::set_method_flags(get_class_static(), StringName("lightmap_unwrap"), METHOD_FLAGS_DEFAULT | METHOD_FLAG_EDITOR); |
| 2324 | ClassDB::bind_method(D_METHOD("generate_triangle_mesh"), &ArrayMesh::generate_triangle_mesh); |
| 2325 | |
| 2326 | ClassDB::bind_method(D_METHOD("set_custom_aabb", "aabb"), &ArrayMesh::set_custom_aabb); |
| 2327 | ClassDB::bind_method(D_METHOD("get_custom_aabb"), &ArrayMesh::get_custom_aabb); |
| 2328 | |
| 2329 | ClassDB::bind_method(D_METHOD("set_shadow_mesh", "mesh"), &ArrayMesh::set_shadow_mesh); |
| 2330 | ClassDB::bind_method(D_METHOD("get_shadow_mesh"), &ArrayMesh::get_shadow_mesh); |
| 2331 | |
| 2332 | ClassDB::bind_method(D_METHOD("_set_blend_shape_names", "blend_shape_names"), &ArrayMesh::_set_blend_shape_names); |
| 2333 | ClassDB::bind_method(D_METHOD("_get_blend_shape_names"), &ArrayMesh::_get_blend_shape_names); |
| 2334 | |
| 2335 | ClassDB::bind_method(D_METHOD("_set_surfaces", "surfaces"), &ArrayMesh::_set_surfaces); |
| 2336 | ClassDB::bind_method(D_METHOD("_get_surfaces"), &ArrayMesh::_get_surfaces); |
| 2337 | |
| 2338 | ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "_blend_shape_names", PROPERTY_HINT_NO_NODEPATH, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_blend_shape_names", "_get_blend_shape_names"); |
| 2339 | ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "_surfaces", PROPERTY_HINT_NO_NODEPATH, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_surfaces", "_get_surfaces"); |
| 2340 | ADD_PROPERTY(PropertyInfo(Variant::INT, "blend_shape_mode", PROPERTY_HINT_ENUM, "Normalized,Relative"), "set_blend_shape_mode", "get_blend_shape_mode"); |
| 2341 | ADD_PROPERTY(PropertyInfo(Variant::AABB, "custom_aabb", PROPERTY_HINT_NO_NODEPATH, "suffix:m"), "set_custom_aabb", "get_custom_aabb"); |
| 2342 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shadow_mesh", PROPERTY_HINT_RESOURCE_TYPE, "ArrayMesh"), "set_shadow_mesh", "get_shadow_mesh"); |
| 2343 | } |
| 2344 | |
| 2345 | void ArrayMesh::reload_from_file() { |
| 2346 | RenderingServer::get_singleton()->mesh_clear(mesh); |
| 2347 | surfaces.clear(); |
| 2348 | clear_blend_shapes(); |
| 2349 | clear_cache(); |
| 2350 | |
| 2351 | Resource::reload_from_file(); |
| 2352 | |
| 2353 | notify_property_list_changed(); |
| 2354 | } |
| 2355 | |
| 2356 | ArrayMesh::ArrayMesh() { |
| 2357 | //mesh is now created on demand |
| 2358 | //mesh = RenderingServer::get_singleton()->mesh_create(); |
| 2359 | } |
| 2360 | |
| 2361 | ArrayMesh::~ArrayMesh() { |
| 2362 | if (mesh.is_valid()) { |
| 2363 | ERR_FAIL_NULL(RenderingServer::get_singleton()); |
| 2364 | RenderingServer::get_singleton()->free_rid(mesh); |
| 2365 | } |
| 2366 | } |
| 2367 | /////////////// |
| 2368 | |
| 2369 | void PlaceholderMesh::_bind_methods() { |
| 2370 | ClassDB::bind_method(D_METHOD("set_aabb", "aabb"), &PlaceholderMesh::set_aabb); |
| 2371 | ADD_PROPERTY(PropertyInfo(Variant::AABB, "aabb", PROPERTY_HINT_NONE, "suffix:m"), "set_aabb", "get_aabb"); |
| 2372 | } |
| 2373 | |
| 2374 | PlaceholderMesh::PlaceholderMesh() { |
| 2375 | rid = RS::get_singleton()->mesh_create(); |
| 2376 | } |
| 2377 | |
| 2378 | PlaceholderMesh::~PlaceholderMesh() { |
| 2379 | ERR_FAIL_NULL(RenderingServer::get_singleton()); |
| 2380 | RS::get_singleton()->free_rid(rid); |
| 2381 | } |