Merge pull request #93916 from zeux/aabb-zfight

Fix AABB computation for position compression to not depend on vertex order
This commit is contained in:
Rémi Verschelde 2024-07-04 23:27:13 +02:00
commit 099b9b2e85
No known key found for this signature in database
GPG Key ID: C3336907360768E1
1 changed files with 18 additions and 16 deletions

View File

@ -347,6 +347,22 @@ void _get_tbn_from_axis_angle(const Vector3 &p_axis, float p_angle, Vector3 &r_n
r_normal = tbn.rows[2];
}
AABB _compute_aabb_from_points(const Vector3 *p_data, int p_length) {
if (p_length == 0) {
return AABB();
}
Vector3 min = p_data[0];
Vector3 max = p_data[0];
for (int i = 1; i < p_length; ++i) {
min = min.min(p_data[i]);
max = max.max(p_data[i]);
}
return AABB(min, max - min);
}
Error RenderingServer::_surface_set_data(Array p_arrays, uint64_t p_format, uint32_t *p_offsets, uint32_t p_vertex_stride, uint32_t p_normal_stride, uint32_t p_attrib_stride, uint32_t p_skin_stride, Vector<uint8_t> &r_vertex_array, Vector<uint8_t> &r_attrib_array, Vector<uint8_t> &r_skin_array, int p_vertex_array_len, Vector<uint8_t> &r_index_array, int p_index_array_len, AABB &r_aabb, Vector<AABB> &r_bone_aabb, Vector4 &r_uv_scale) {
uint8_t *vw = r_vertex_array.ptrw();
uint8_t *aw = r_attrib_array.ptrw();
@ -440,18 +456,10 @@ Error RenderingServer::_surface_set_data(Array p_arrays, uint64_t p_format, uint
const Vector3 *src = array.ptr();
r_aabb = AABB();
r_aabb = _compute_aabb_from_points(src, p_vertex_array_len);
r_aabb.size = r_aabb.size.max(SMALL_VEC3);
if (p_format & ARRAY_FLAG_COMPRESS_ATTRIBUTES) {
// First we need to generate the AABB for the entire surface.
for (int i = 0; i < p_vertex_array_len; i++) {
if (i == 0) {
r_aabb = AABB(src[i], SMALL_VEC3);
} else {
r_aabb.expand_to(src[i]);
}
}
if (!(p_format & RS::ARRAY_FORMAT_NORMAL)) {
// Early out if we are only setting vertex positions.
for (int i = 0; i < p_vertex_array_len; i++) {
@ -592,12 +600,6 @@ Error RenderingServer::_surface_set_data(Array p_arrays, uint64_t p_format, uint
float vector[3] = { (float)src[i].x, (float)src[i].y, (float)src[i].z };
memcpy(&vw[p_offsets[ai] + i * p_vertex_stride], vector, sizeof(float) * 3);
if (i == 0) {
r_aabb = AABB(src[i], SMALL_VEC3);
} else {
r_aabb.expand_to(src[i]);
}
}
}
}