1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-11 13:10:58 +00:00

Avoid singularity when generated tangents and validate that tangents are good enough when using compression

This commit is contained in:
clayjohn
2024-02-23 13:54:20 -08:00
parent 16d61427ca
commit 781cd27fe4
5 changed files with 50 additions and 8 deletions

View File

@@ -566,7 +566,8 @@ Error RenderingServer::_surface_set_data(Array p_arrays, uint64_t p_format, uint
float angle;
Vector3 axis;
// Generate an arbitrary vector that is tangential to normal.
Vector3 tan = Vector3(0.0, 1.0, 0.0).cross(normal_src[i].normalized());
// This assumes that the normal is never (0,0,0).
Vector3 tan = Vector3(normal_src[i].z, -normal_src[i].x, normal_src[i].y).cross(normal_src[i].normalized()).normalized();
Vector4 tangent = Vector4(tan.x, tan.y, tan.z, 1.0);
_get_axis_angle(normal_src[i], tangent, angle, axis);
@@ -689,7 +690,8 @@ Error RenderingServer::_surface_set_data(Array p_arrays, uint64_t p_format, uint
// Set data for tangent.
for (int i = 0; i < p_vertex_array_len; i++) {
// Generate an arbitrary vector that is tangential to normal.
Vector3 tan = Vector3(0.0, 1.0, 0.0).cross(normal_src[i].normalized());
// This assumes that the normal is never (0,0,0).
Vector3 tan = Vector3(normal_src[i].z, -normal_src[i].x, normal_src[i].y).cross(normal_src[i].normalized()).normalized();
Vector2 res = tan.octahedron_tangent_encode(1.0);
uint16_t vector[2] = {
(uint16_t)CLAMP(res.x * 65535, 0, 65535),