Skip to content

Commit baaa8ef

Browse files
committed
Change collision Vector to TypedArray
1 parent cfc1c57 commit baaa8ef

7 files changed

Lines changed: 41 additions & 41 deletions

File tree

editor/import/resource_importer_scene.cpp

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ static String _fixstr(const String &p_what, const String &p_str) {
351351
return what;
352352
}
353353

354-
static void _pre_gen_shape_list(Ref<ImporterMesh> &mesh, Vector<Ref<Shape3D>> &r_shape_list, bool p_convex) {
354+
static void _pre_gen_shape_list(Ref<ImporterMesh> &mesh, TypedArray<Ref<Shape3D>> &r_shape_list, bool p_convex) {
355355
ERR_FAIL_NULL_MSG(mesh, "Cannot generate shape list with null mesh value");
356356
ERR_FAIL_NULL_MSG(mesh->get_mesh(), "Cannot generate shape list with null mesh value");
357357
if (!p_convex) {
@@ -368,7 +368,7 @@ static void _pre_gen_shape_list(Ref<ImporterMesh> &mesh, Vector<Ref<Shape3D>> &r
368368
}
369369
}
370370

371-
Node *ResourceImporterScene::_pre_fix_node(Node *p_node, Node *p_root, HashMap<Ref<ImporterMesh>, Vector<Ref<Shape3D>>> &r_collision_map, Pair<PackedVector3Array, PackedInt32Array> *r_occluder_arrays, List<Pair<NodePath, Node *>> &r_node_renames) {
371+
Node *ResourceImporterScene::_pre_fix_node(Node *p_node, Node *p_root, HashMap<Ref<ImporterMesh>, TypedArray<Ref<Shape3D>>> &r_collision_map, Pair<PackedVector3Array, PackedInt32Array> *r_occluder_arrays, List<Pair<NodePath, Node *>> &r_node_renames) {
372372
// Children first.
373373
for (int i = 0; i < p_node->get_child_count(); i++) {
374374
Node *r = _pre_fix_node(p_node->get_child(i), p_root, r_collision_map, r_occluder_arrays, r_node_renames);
@@ -496,7 +496,7 @@ Node *ResourceImporterScene::_pre_fix_node(Node *p_node, Node *p_root, HashMap<R
496496
Ref<ImporterMesh> mesh = mi->get_mesh();
497497

498498
if (mesh.is_valid()) {
499-
Vector<Ref<Shape3D>> shapes;
499+
TypedArray<Ref<Shape3D>> shapes;
500500
if (r_collision_map.has(mesh)) {
501501
shapes = r_collision_map[mesh];
502502
} else if (_teststr(name, "colonly")) {
@@ -558,7 +558,7 @@ Node *ResourceImporterScene::_pre_fix_node(Node *p_node, Node *p_root, HashMap<R
558558
Ref<ImporterMesh> mesh = mi->get_mesh();
559559

560560
if (mesh.is_valid()) {
561-
Vector<Ref<Shape3D>> shapes;
561+
TypedArray<Ref<Shape3D>> shapes;
562562
if (r_collision_map.has(mesh)) {
563563
shapes = r_collision_map[mesh];
564564
} else {
@@ -583,7 +583,7 @@ Node *ResourceImporterScene::_pre_fix_node(Node *p_node, Node *p_root, HashMap<R
583583
Ref<ImporterMesh> mesh = mi->get_mesh();
584584

585585
if (mesh.is_valid()) {
586-
Vector<Ref<Shape3D>> shapes;
586+
TypedArray<Ref<Shape3D>> shapes;
587587
String fixed_name;
588588
if (r_collision_map.has(mesh)) {
589589
shapes = r_collision_map[mesh];
@@ -704,7 +704,7 @@ Node *ResourceImporterScene::_pre_fix_node(Node *p_node, Node *p_root, HashMap<R
704704

705705
Ref<ImporterMesh> mesh = mi->get_mesh();
706706
if (!mesh.is_null()) {
707-
Vector<Ref<Shape3D>> shapes;
707+
TypedArray<Ref<Shape3D>> shapes;
708708
if (r_collision_map.has(mesh)) {
709709
shapes = r_collision_map[mesh];
710710
} else if (_teststr(mesh->get_name(), "col")) {
@@ -900,7 +900,7 @@ Node *ResourceImporterScene::_post_fix_animations(Node *p_node, Node *p_root, co
900900
return p_node;
901901
}
902902

903-
Node *ResourceImporterScene::_post_fix_node(Node *p_node, Node *p_root, HashMap<Ref<ImporterMesh>, Vector<Ref<Shape3D>>> &collision_map, Pair<PackedVector3Array, PackedInt32Array> &r_occluder_arrays, HashSet<Ref<ImporterMesh>> &r_scanned_meshes, const Dictionary &p_node_data, const Dictionary &p_material_data, const Dictionary &p_animation_data, float p_animation_fps) {
903+
Node *ResourceImporterScene::_post_fix_node(Node *p_node, Node *p_root, HashMap<Ref<ImporterMesh>, TypedArray<Ref<Shape3D>>> &collision_map, Pair<PackedVector3Array, PackedInt32Array> &r_occluder_arrays, HashSet<Ref<ImporterMesh>> &r_scanned_meshes, const Dictionary &p_node_data, const Dictionary &p_material_data, const Dictionary &p_animation_data, float p_animation_fps) {
904904
// children first
905905
for (int i = 0; i < p_node->get_child_count(); i++) {
906906
Node *r = _post_fix_node(p_node->get_child(i), p_root, collision_map, r_occluder_arrays, r_scanned_meshes, p_node_data, p_material_data, p_animation_data, p_animation_fps);
@@ -1040,7 +1040,7 @@ Node *ResourceImporterScene::_post_fix_node(Node *p_node, Node *p_root, HashMap<
10401040
}
10411041

10421042
if (mesh_physics_mode != MeshPhysicsMode::MESH_PHYSICS_DISABLED) {
1043-
Vector<Ref<Shape3D>> shapes;
1043+
TypedArray<Ref<Shape3D>> shapes;
10441044
if (collision_map.has(m)) {
10451045
shapes = collision_map[m];
10461046
} else {
@@ -1092,7 +1092,8 @@ Node *ResourceImporterScene::_post_fix_node(Node *p_node, Node *p_root, HashMap<
10921092
}
10931093

10941094
int idx = 0;
1095-
for (const Ref<Shape3D> &E : shapes) {
1095+
for (int i = 0; i < shapes.size(); i++) {
1096+
const Ref<Shape3D> &E = shapes[i];
10961097
CollisionShape3D *cshape = memnew(CollisionShape3D);
10971098
cshape->set_shape(E);
10981099
base->add_child(cshape, true);
@@ -1884,8 +1885,9 @@ void ResourceImporterScene::_generate_meshes(Node *p_node, const Dictionary &p_m
18841885
}
18851886
}
18861887

1887-
void ResourceImporterScene::_add_shapes(Node *p_node, const Vector<Ref<Shape3D>> &p_shapes) {
1888-
for (const Ref<Shape3D> &E : p_shapes) {
1888+
void ResourceImporterScene::_add_shapes(Node *p_node, const TypedArray<Ref<Shape3D>> &p_shapes) {
1889+
for (int i = 0; i < p_shapes.size(); i++) {
1890+
const Ref<Shape3D> &E = p_shapes[i];
18891891
CollisionShape3D *cshape = memnew(CollisionShape3D);
18901892
cshape->set_shape(E);
18911893
p_node->add_child(cshape, true);
@@ -2081,7 +2083,7 @@ Node *ResourceImporterScene::pre_import(const String &p_source_file, const HashM
20812083
return nullptr;
20822084
}
20832085

2084-
HashMap<Ref<ImporterMesh>, Vector<Ref<Shape3D>>> collision_map;
2086+
HashMap<Ref<ImporterMesh>, TypedArray<Ref<Shape3D>>> collision_map;
20852087
List<Pair<NodePath, Node *>> node_renames;
20862088
_pre_fix_node(scene, scene, collision_map, nullptr, node_renames);
20872089

@@ -2162,7 +2164,7 @@ Error ResourceImporterScene::import(const String &p_source_file, const String &p
21622164
}
21632165

21642166
HashSet<Ref<ImporterMesh>> scanned_meshes;
2165-
HashMap<Ref<ImporterMesh>, Vector<Ref<Shape3D>>> collision_map;
2167+
HashMap<Ref<ImporterMesh>, TypedArray<Ref<Shape3D>>> collision_map;
21662168
Pair<PackedVector3Array, PackedInt32Array> occluder_arrays;
21672169
List<Pair<NodePath, Node *>> node_renames;
21682170

editor/import/resource_importer_scene.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ class ResourceImporterScene : public ResourceImporter {
210210

211211
void _replace_owner(Node *p_node, Node *p_scene, Node *p_new_owner);
212212
void _generate_meshes(Node *p_node, const Dictionary &p_mesh_data, bool p_generate_lods, bool p_create_shadow_meshes, LightBakeMode p_light_bake_mode, float p_lightmap_texel_size, const Vector<uint8_t> &p_src_lightmap_cache, Vector<Vector<uint8_t>> &r_lightmap_caches);
213-
void _add_shapes(Node *p_node, const Vector<Ref<Shape3D>> &p_shapes);
213+
void _add_shapes(Node *p_node, const TypedArray<Ref<Shape3D>> &p_shapes);
214214

215215
enum AnimationImportTracks {
216216
ANIMATION_IMPORT_TRACKS_IF_PRESENT,
@@ -273,9 +273,9 @@ class ResourceImporterScene : public ResourceImporter {
273273
// Import scenes *after* everything else (such as textures).
274274
virtual int get_import_order() const override { return ResourceImporter::IMPORT_ORDER_SCENE; }
275275

276-
Node *_pre_fix_node(Node *p_node, Node *p_root, HashMap<Ref<ImporterMesh>, Vector<Ref<Shape3D>>> &r_collision_map, Pair<PackedVector3Array, PackedInt32Array> *r_occluder_arrays, List<Pair<NodePath, Node *>> &r_node_renames);
276+
Node *_pre_fix_node(Node *p_node, Node *p_root, HashMap<Ref<ImporterMesh>, TypedArray<Ref<Shape3D>>> &r_collision_map, Pair<PackedVector3Array, PackedInt32Array> *r_occluder_arrays, List<Pair<NodePath, Node *>> &r_node_renames);
277277
Node *_pre_fix_animations(Node *p_node, Node *p_root, const Dictionary &p_node_data, const Dictionary &p_animation_data, float p_animation_fps);
278-
Node *_post_fix_node(Node *p_node, Node *p_root, HashMap<Ref<ImporterMesh>, Vector<Ref<Shape3D>>> &collision_map, Pair<PackedVector3Array, PackedInt32Array> &r_occluder_arrays, HashSet<Ref<ImporterMesh>> &r_scanned_meshes, const Dictionary &p_node_data, const Dictionary &p_material_data, const Dictionary &p_animation_data, float p_animation_fps);
278+
Node *_post_fix_node(Node *p_node, Node *p_root, HashMap<Ref<ImporterMesh>, TypedArray<Ref<Shape3D>>> &collision_map, Pair<PackedVector3Array, PackedInt32Array> &r_occluder_arrays, HashSet<Ref<ImporterMesh>> &r_scanned_meshes, const Dictionary &p_node_data, const Dictionary &p_material_data, const Dictionary &p_animation_data, float p_animation_fps);
279279
Node *_post_fix_animations(Node *p_node, Node *p_root, const Dictionary &p_node_data, const Dictionary &p_animation_data, float p_animation_fps);
280280

281281
Ref<Animation> _save_animation_to_file(Ref<Animation> anim, bool p_save_to_file, String p_save_to_path, bool p_keep_custom_tracks);
@@ -294,7 +294,7 @@ class ResourceImporterScene : public ResourceImporter {
294294
ResourceImporterScene(bool p_animation_import = false);
295295

296296
template <class M>
297-
static Vector<Ref<Shape3D>> get_collision_shapes(const Ref<Mesh> &p_mesh, const M &p_options);
297+
static TypedArray<Ref<Shape3D>> get_collision_shapes(const Ref<Mesh> &p_mesh, const M &p_options);
298298

299299
template <class M>
300300
static Transform3D get_collision_shapes_transform(const M &p_options);
@@ -315,7 +315,7 @@ class EditorSceneFormatImporterESCN : public EditorSceneFormatImporter {
315315
#include "scene/resources/sphere_shape_3d.h"
316316

317317
template <class M>
318-
Vector<Ref<Shape3D>> ResourceImporterScene::get_collision_shapes(const Ref<Mesh> &p_mesh, const M &p_options) {
318+
TypedArray<Ref<Shape3D>> ResourceImporterScene::get_collision_shapes(const Ref<Mesh> &p_mesh, const M &p_options) {
319319
ShapeType generate_shape_type = SHAPE_TYPE_DECOMPOSE_CONVEX;
320320
if (p_options.has(SNAME("physics/shape_type"))) {
321321
generate_shape_type = (ShapeType)p_options[SNAME("physics/shape_type")].operator int();
@@ -399,11 +399,11 @@ Vector<Ref<Shape3D>> ResourceImporterScene::get_collision_shapes(const Ref<Mesh>
399399

400400
return p_mesh->convex_decompose(decomposition_settings);
401401
} else if (generate_shape_type == SHAPE_TYPE_SIMPLE_CONVEX) {
402-
Vector<Ref<Shape3D>> shapes;
402+
TypedArray<Ref<Shape3D>> shapes;
403403
shapes.push_back(p_mesh->create_convex_shape(true, /*Passing false, otherwise VHACD will be used to simplify (Decompose) the Mesh.*/ false));
404404
return shapes;
405405
} else if (generate_shape_type == SHAPE_TYPE_TRIMESH) {
406-
Vector<Ref<Shape3D>> shapes;
406+
TypedArray<Ref<Shape3D>> shapes;
407407
shapes.push_back(p_mesh->create_trimesh_shape());
408408
return shapes;
409409
} else if (generate_shape_type == SHAPE_TYPE_BOX) {
@@ -413,7 +413,7 @@ Vector<Ref<Shape3D>> ResourceImporterScene::get_collision_shapes(const Ref<Mesh>
413413
box->set_size(p_options[SNAME("primitive/size")]);
414414
}
415415

416-
Vector<Ref<Shape3D>> shapes;
416+
TypedArray<Ref<Shape3D>> shapes;
417417
shapes.push_back(box);
418418
return shapes;
419419

@@ -424,7 +424,7 @@ Vector<Ref<Shape3D>> ResourceImporterScene::get_collision_shapes(const Ref<Mesh>
424424
sphere->set_radius(p_options[SNAME("primitive/radius")]);
425425
}
426426

427-
Vector<Ref<Shape3D>> shapes;
427+
TypedArray<Ref<Shape3D>> shapes;
428428
shapes.push_back(sphere);
429429
return shapes;
430430
} else if (generate_shape_type == SHAPE_TYPE_CYLINDER) {
@@ -437,7 +437,7 @@ Vector<Ref<Shape3D>> ResourceImporterScene::get_collision_shapes(const Ref<Mesh>
437437
cylinder->set_radius(p_options[SNAME("primitive/radius")]);
438438
}
439439

440-
Vector<Ref<Shape3D>> shapes;
440+
TypedArray<Ref<Shape3D>> shapes;
441441
shapes.push_back(cylinder);
442442
return shapes;
443443
} else if (generate_shape_type == SHAPE_TYPE_CAPSULE) {
@@ -450,11 +450,11 @@ Vector<Ref<Shape3D>> ResourceImporterScene::get_collision_shapes(const Ref<Mesh>
450450
capsule->set_radius(p_options[SNAME("primitive/radius")]);
451451
}
452452

453-
Vector<Ref<Shape3D>> shapes;
453+
TypedArray<Ref<Shape3D>> shapes;
454454
shapes.push_back(capsule);
455455
return shapes;
456456
}
457-
return Vector<Ref<Shape3D>>();
457+
return TypedArray<Ref<Shape3D>>();
458458
}
459459

460460
template <class M>

editor/import/scene_import_settings.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,12 +440,13 @@ void SceneImportSettings::_update_view_gizmos() {
440440
// This collider_view doesn't have a mesh so we need to generate a new one.
441441

442442
// Generate the mesh collider.
443-
Vector<Ref<Shape3D>> shapes = ResourceImporterScene::get_collision_shapes(mesh_node->get_mesh(), e.value.settings);
443+
TypedArray<Ref<Shape3D>> shapes = ResourceImporterScene::get_collision_shapes(mesh_node->get_mesh(), e.value.settings);
444444
const Transform3D transform = ResourceImporterScene::get_collision_shapes_transform(e.value.settings);
445445

446446
Ref<ArrayMesh> collider_view_mesh;
447447
collider_view_mesh.instantiate();
448-
for (Ref<Shape3D> shape : shapes) {
448+
for (int i = 0; i < shapes.size(); i++) {
449+
Ref<Shape3D> shape = shapes[i];
449450
Ref<ArrayMesh> debug_shape_mesh;
450451
if (shape.is_valid()) {
451452
debug_shape_mesh = shape->get_debug_mesh();

editor/plugins/mesh_instance_3d_editor_plugin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ void MeshInstance3DEditor::_menu_option(int p_option) {
211211
}
212212

213213
Mesh::ConvexDecompositionSettings settings;
214-
Vector<Ref<Shape3D>> shapes = mesh->convex_decompose(settings);
214+
TypedArray<Ref<Shape3D>> shapes = mesh->convex_decompose(settings);
215215

216216
if (!shapes.size()) {
217217
err_dialog->set_text(TTR("Couldn't create any collision shapes."));

scene/3d/mesh_instance_3d.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ Node *MeshInstance3D::create_multiple_convex_collisions_node() {
285285
}
286286

287287
Mesh::ConvexDecompositionSettings settings;
288-
Vector<Ref<Shape3D>> shapes = mesh->convex_decompose(settings);
288+
TypedArray<Ref<Shape3D>> shapes = mesh->convex_decompose(settings);
289289
if (!shapes.size()) {
290290
return nullptr;
291291
}
@@ -305,16 +305,12 @@ TypedArray<Ref<Shape3D>> MeshInstance3D::create_convex_collisions_array() {
305305
}
306306

307307
Mesh::ConvexDecompositionSettings settings;
308-
Vector<Ref<Shape3D>> shapes = mesh->convex_decompose(settings);
308+
TypedArray<Ref<Shape3D>> shapes = mesh->convex_decompose(settings);
309309
if (!shapes.size()) {
310310
return TypedArray<Ref<Shape3D>>();
311311
}
312312

313-
TypedArray<Ref<Shape3D>> arr;
314-
for (int i = 0; i < shapes.size(); i++) {
315-
arr.push_back(*shapes[i]);
316-
}
317-
return arr;
313+
return shapes;
318314
}
319315

320316
void MeshInstance3D::create_multiple_convex_collisions() {

scene/resources/mesh.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ Ref<Shape3D> Mesh::create_convex_shape(bool p_clean, bool p_simplify) const {
392392
if (p_simplify) {
393393
ConvexDecompositionSettings settings;
394394
settings.max_convex_hulls = 1;
395-
Vector<Ref<Shape3D>> decomposed = convex_decompose(settings);
395+
TypedArray<Ref<Shape3D>> decomposed = convex_decompose(settings);
396396
if (decomposed.size() == 1) {
397397
return decomposed[0];
398398
} else {
@@ -748,11 +748,11 @@ void Mesh::clear_cache() const {
748748
debug_lines.clear();
749749
}
750750

751-
Vector<Ref<Shape3D>> Mesh::convex_decompose(const ConvexDecompositionSettings &p_settings) const {
752-
ERR_FAIL_COND_V(!convex_decomposition_function, Vector<Ref<Shape3D>>());
751+
TypedArray<Ref<Shape3D>> Mesh::convex_decompose(const ConvexDecompositionSettings &p_settings) const {
752+
ERR_FAIL_COND_V(!convex_decomposition_function, TypedArray<Ref<Shape3D>>());
753753

754754
Ref<TriangleMesh> tm = generate_triangle_mesh();
755-
ERR_FAIL_COND_V(!tm.is_valid(), Vector<Ref<Shape3D>>());
755+
ERR_FAIL_COND_V(!tm.is_valid(), TypedArray<Ref<Shape3D>>());
756756

757757
const Vector<TriangleMesh::Triangle> &triangles = tm->get_triangles();
758758
int triangle_count = triangles.size();
@@ -773,7 +773,7 @@ Vector<Ref<Shape3D>> Mesh::convex_decompose(const ConvexDecompositionSettings &p
773773

774774
Vector<Vector<Vector3>> decomposed = convex_decomposition_function((real_t *)vertices.ptr(), vertex_count, indices.ptr(), triangle_count, p_settings, nullptr);
775775

776-
Vector<Ref<Shape3D>> ret;
776+
TypedArray<Ref<Shape3D>> ret;
777777

778778
for (int i = 0; i < decomposed.size(); i++) {
779779
Ref<ConvexPolygonShape3D> shape;

scene/resources/mesh.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "core/io/resource.h"
3535
#include "core/math/face3.h"
3636
#include "core/math/triangle_mesh.h"
37+
#include "core/variant/typed_array.h"
3738
#include "scene/resources/material.h"
3839
#include "scene/resources/shape_3d.h"
3940
#include "servers/rendering_server.h"
@@ -210,7 +211,7 @@ class Mesh : public Resource {
210211

211212
static ConvexDecompositionFunc convex_decomposition_function;
212213

213-
Vector<Ref<Shape3D>> convex_decompose(const ConvexDecompositionSettings &p_settings) const;
214+
TypedArray<Ref<Shape3D>> convex_decompose(const ConvexDecompositionSettings &p_settings) const;
214215
Ref<Shape3D> create_convex_shape(bool p_clean = true, bool p_simplify = false) const;
215216
Ref<Shape3D> create_trimesh_shape() const;
216217

0 commit comments

Comments
 (0)