Skip to content

Commit

Permalink
Avoid LOD generation crash.
Browse files Browse the repository at this point in the history
  • Loading branch information
fire committed Oct 2, 2023
1 parent 57a6813 commit 78f13c0
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion scene/resources/importer_mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,9 @@ void ImporterMesh::generate_lods(float p_normal_merge_angle, float p_normal_spli

int32_t *new_indices_ptr = new_indices.ptrw();

int current_ray_count = 0;
int64_t current_ray_count = 0;
const int64_t MAX_MESH_INDICES = pow(2, 24);
ERR_BREAK(new_index_count >= MAX_MESH_INDICES);
for (unsigned int j = 0; j < new_index_count; j += 3) {
const Vector3 &v0 = vertices_ptr[new_indices_ptr[j + 0]];
const Vector3 &v1 = vertices_ptr[new_indices_ptr[j + 1]];
Expand All @@ -522,9 +524,11 @@ void ImporterMesh::generate_lods(float p_normal_merge_angle, float p_normal_spli
Vector3 dir = face_normal / face_area;
int ray_count = CLAMP(5.0 * face_area * error_factor, 16, 64);

ERR_BREAK(current_ray_count + ray_count >= MAX_MESH_INDICES);
rays.resize(current_ray_count + ray_count);
StaticRaycaster::Ray *rays_ptr = rays.ptrw();

ERR_BREAK(current_ray_count + ray_count >= MAX_MESH_INDICES);
ray_uvs.resize(current_ray_count + ray_count);
Vector2 *ray_uvs_ptr = ray_uvs.ptr();

Expand All @@ -543,19 +547,22 @@ void ImporterMesh::generate_lods(float p_normal_merge_angle, float p_normal_spli

Vector3 org = v0 * w + v1 * u + v2 * v;
org -= dir * ray_bias;
ERR_BREAK(current_ray_count + k >= MAX_MESH_INDICES);
rays_ptr[current_ray_count + k] = StaticRaycaster::Ray(org, dir, 0.0f, ray_length);
rays_ptr[current_ray_count + k].id = j / 3;
ray_uvs_ptr[current_ray_count + k] = Vector2(u, v);
}

current_ray_count += ray_count;
ERR_BREAK(current_ray_count >= MAX_MESH_INDICES);
}

raycaster->intersect(rays);

LocalVector<Vector3> ray_normals;
LocalVector<real_t> ray_normal_weights;

ERR_BREAK(new_index_count >= MAX_MESH_INDICES);
ray_normals.resize(new_index_count);
ray_normal_weights.resize(new_index_count);

Expand Down

0 comments on commit 78f13c0

Please sign in to comment.