You've already forked godot
mirror of
https://github.com/godotengine/godot.git
synced 2025-11-05 12:10:55 +00:00
Fix navmesh baking, fixes #57148
- improved mesh data calculation from standalone static colliders so that no VisualServer calls are performed - and thus no VS mutexes need to be locked in case of on-thread baking - improved the same for GridMap's static colliders
This commit is contained in:
@@ -126,6 +126,28 @@ void NavigationMeshGenerator::_add_mesh(const Ref<Mesh> &p_mesh, const Transform
|
||||
}
|
||||
}
|
||||
|
||||
void NavigationMeshGenerator::_add_mesh_array(const Array &p_array, const Transform &p_xform, Vector<float> &p_vertices, Vector<int> &p_indices) {
|
||||
PoolVector<Vector3> mesh_vertices = p_array[Mesh::ARRAY_VERTEX];
|
||||
PoolVector<Vector3>::Read vr = mesh_vertices.read();
|
||||
|
||||
PoolVector<int> mesh_indices = p_array[Mesh::ARRAY_INDEX];
|
||||
PoolVector<int>::Read ir = mesh_indices.read();
|
||||
|
||||
const int face_count = mesh_indices.size() / 3;
|
||||
const int current_vertex_count = p_vertices.size() / 3;
|
||||
|
||||
for (int j = 0; j < mesh_vertices.size(); j++) {
|
||||
_add_vertex(p_xform.xform(vr[j]), p_vertices);
|
||||
}
|
||||
|
||||
for (int j = 0; j < face_count; j++) {
|
||||
// CCW
|
||||
p_indices.push_back(current_vertex_count + (ir[j * 3 + 0]));
|
||||
p_indices.push_back(current_vertex_count + (ir[j * 3 + 2]));
|
||||
p_indices.push_back(current_vertex_count + (ir[j * 3 + 1]));
|
||||
}
|
||||
}
|
||||
|
||||
void NavigationMeshGenerator::_add_faces(const PoolVector3Array &p_faces, const Transform &p_xform, Vector<float> &p_vertices, Vector<int> &p_indices) {
|
||||
int face_count = p_faces.size() / 3;
|
||||
int current_vertex_count = p_vertices.size() / 3;
|
||||
@@ -189,43 +211,38 @@ void NavigationMeshGenerator::_parse_geometry(const Transform &p_navmesh_xform,
|
||||
|
||||
Transform transform = p_navmesh_xform * static_body->get_global_transform() * col_shape->get_transform();
|
||||
|
||||
Ref<Mesh> mesh;
|
||||
Ref<Shape> s = col_shape->get_shape();
|
||||
|
||||
BoxShape *box = Object::cast_to<BoxShape>(*s);
|
||||
if (box) {
|
||||
Ref<CubeMesh> cube_mesh;
|
||||
cube_mesh.instance();
|
||||
cube_mesh->set_size(box->get_extents() * 2.0);
|
||||
mesh = cube_mesh;
|
||||
Array arr;
|
||||
arr.resize(VS::ARRAY_MAX);
|
||||
CubeMesh::create_mesh_array(arr, box->get_extents() * 2.0);
|
||||
_add_mesh_array(arr, transform, p_vertices, p_indices);
|
||||
}
|
||||
|
||||
CapsuleShape *capsule = Object::cast_to<CapsuleShape>(*s);
|
||||
if (capsule) {
|
||||
Ref<CapsuleMesh> capsule_mesh;
|
||||
capsule_mesh.instance();
|
||||
capsule_mesh->set_radius(capsule->get_radius());
|
||||
capsule_mesh->set_mid_height(capsule->get_height() / 2.0);
|
||||
mesh = capsule_mesh;
|
||||
Array arr;
|
||||
arr.resize(VS::ARRAY_MAX);
|
||||
CapsuleMesh::create_mesh_array(arr, capsule->get_radius(), capsule->get_height() / 2.0);
|
||||
_add_mesh_array(arr, transform, p_vertices, p_indices);
|
||||
}
|
||||
|
||||
CylinderShape *cylinder = Object::cast_to<CylinderShape>(*s);
|
||||
if (cylinder) {
|
||||
Ref<CylinderMesh> cylinder_mesh;
|
||||
cylinder_mesh.instance();
|
||||
cylinder_mesh->set_height(cylinder->get_height());
|
||||
cylinder_mesh->set_bottom_radius(cylinder->get_radius());
|
||||
cylinder_mesh->set_top_radius(cylinder->get_radius());
|
||||
mesh = cylinder_mesh;
|
||||
Array arr;
|
||||
arr.resize(VS::ARRAY_MAX);
|
||||
CylinderMesh::create_mesh_array(arr, cylinder->get_radius(), cylinder->get_radius(), cylinder->get_height());
|
||||
_add_mesh_array(arr, transform, p_vertices, p_indices);
|
||||
}
|
||||
|
||||
SphereShape *sphere = Object::cast_to<SphereShape>(*s);
|
||||
if (sphere) {
|
||||
Ref<SphereMesh> sphere_mesh;
|
||||
sphere_mesh.instance();
|
||||
sphere_mesh->set_radius(sphere->get_radius());
|
||||
sphere_mesh->set_height(sphere->get_radius() * 2.0);
|
||||
mesh = sphere_mesh;
|
||||
Array arr;
|
||||
arr.resize(VS::ARRAY_MAX);
|
||||
SphereMesh::create_mesh_array(arr, sphere->get_radius(), sphere->get_radius() * 2.0);
|
||||
_add_mesh_array(arr, transform, p_vertices, p_indices);
|
||||
}
|
||||
|
||||
ConcavePolygonShape *concave_polygon = Object::cast_to<ConcavePolygonShape>(*s);
|
||||
@@ -256,10 +273,6 @@ void NavigationMeshGenerator::_parse_geometry(const Transform &p_navmesh_xform,
|
||||
_add_faces(faces, transform, p_vertices, p_indices);
|
||||
}
|
||||
}
|
||||
|
||||
if (mesh.is_valid()) {
|
||||
_add_mesh(mesh, transform, p_vertices, p_indices);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -287,44 +300,39 @@ void NavigationMeshGenerator::_parse_geometry(const Transform &p_navmesh_xform,
|
||||
RID shape = shapes[i + 1];
|
||||
PhysicsServer::ShapeType type = PhysicsServer::get_singleton()->shape_get_type(shape);
|
||||
Variant data = PhysicsServer::get_singleton()->shape_get_data(shape);
|
||||
Ref<Mesh> mesh;
|
||||
|
||||
switch (type) {
|
||||
case PhysicsServer::SHAPE_SPHERE: {
|
||||
real_t radius = data;
|
||||
Ref<SphereMesh> sphere_mesh;
|
||||
sphere_mesh.instance();
|
||||
sphere_mesh->set_radius(radius);
|
||||
sphere_mesh->set_height(radius * 2.0);
|
||||
mesh = sphere_mesh;
|
||||
Array arr;
|
||||
arr.resize(VS::ARRAY_MAX);
|
||||
SphereMesh::create_mesh_array(arr, radius, radius * 2.0);
|
||||
_add_mesh_array(arr, shapes[i], p_vertices, p_indices);
|
||||
} break;
|
||||
case PhysicsServer::SHAPE_BOX: {
|
||||
Vector3 extents = data;
|
||||
Ref<CubeMesh> box_mesh;
|
||||
box_mesh.instance();
|
||||
box_mesh->set_size(2.0 * extents);
|
||||
mesh = box_mesh;
|
||||
Array arr;
|
||||
arr.resize(VS::ARRAY_MAX);
|
||||
CubeMesh::create_mesh_array(arr, extents * 2.0);
|
||||
_add_mesh_array(arr, shapes[i], p_vertices, p_indices);
|
||||
} break;
|
||||
case PhysicsServer::SHAPE_CAPSULE: {
|
||||
Dictionary dict = data;
|
||||
real_t radius = dict["radius"];
|
||||
real_t height = dict["height"];
|
||||
Ref<CapsuleMesh> capsule_mesh;
|
||||
capsule_mesh.instance();
|
||||
capsule_mesh->set_radius(radius);
|
||||
capsule_mesh->set_mid_height(0.5 * height);
|
||||
mesh = capsule_mesh;
|
||||
Array arr;
|
||||
arr.resize(VS::ARRAY_MAX);
|
||||
CapsuleMesh::create_mesh_array(arr, radius, height * 0.5);
|
||||
_add_mesh_array(arr, shapes[i], p_vertices, p_indices);
|
||||
} break;
|
||||
case PhysicsServer::SHAPE_CYLINDER: {
|
||||
Dictionary dict = data;
|
||||
real_t radius = dict["radius"];
|
||||
real_t height = dict["height"];
|
||||
Ref<CylinderMesh> cylinder_mesh;
|
||||
cylinder_mesh.instance();
|
||||
cylinder_mesh->set_height(height);
|
||||
cylinder_mesh->set_bottom_radius(radius);
|
||||
cylinder_mesh->set_top_radius(radius);
|
||||
mesh = cylinder_mesh;
|
||||
Array arr;
|
||||
arr.resize(VS::ARRAY_MAX);
|
||||
CylinderMesh::create_mesh_array(arr, radius, radius, height);
|
||||
_add_mesh_array(arr, shapes[i], p_vertices, p_indices);
|
||||
} break;
|
||||
case PhysicsServer::SHAPE_CONVEX_POLYGON: {
|
||||
PoolVector3Array vertices = data;
|
||||
@@ -356,10 +364,6 @@ void NavigationMeshGenerator::_parse_geometry(const Transform &p_navmesh_xform,
|
||||
WARN_PRINT("Unsupported collision shape type.");
|
||||
} break;
|
||||
}
|
||||
|
||||
if (mesh.is_valid()) {
|
||||
_add_mesh(mesh, shapes[i], p_vertices, p_indices);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user