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

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

(cherry picked from commit 781cd27fe4)
This commit is contained in:
clayjohn
2024-02-23 13:54:20 -08:00
committed by Rémi Verschelde
parent 43eae4312d
commit 6ba9c6bf6d
5 changed files with 50 additions and 8 deletions

View File

@@ -565,7 +565,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);
@@ -688,7 +689,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),