1
0
Fork 0

Merge pull request #98610 from Flarkk/fix_sphere_capsule

Fix normals of very large `SphereMesh` and `CapsuleMesh`
This commit is contained in:
Rémi Verschelde 2024-12-14 18:25:17 +01:00
commit 66a2ea4718
No known key found for this signature in database
GPG Key ID: C3336907360768E1
1 changed files with 16 additions and 16 deletions

View File

@ -448,7 +448,7 @@ void CapsuleMesh::create_mesh_array(Array &p_arr, const float radius, const floa
y = 0.0;
} else {
w = Math::sin(0.5 * Math_PI * v);
y = Math::cos(0.5 * Math_PI * v) * radius;
y = Math::cos(0.5 * Math_PI * v);
}
for (i = 0; i <= radial_segments; i++) {
@ -463,9 +463,9 @@ void CapsuleMesh::create_mesh_array(Array &p_arr, const float radius, const floa
z = Math::cos(u * Math_TAU);
}
Vector3 p = Vector3(x * radius * w, y, -z * radius * w);
points.push_back(p + Vector3(0.0, 0.5 * height - radius, 0.0));
normals.push_back(p.normalized());
Vector3 p = Vector3(x * w, y, -z * w);
points.push_back(p * radius + Vector3(0.0, 0.5 * height - radius, 0.0));
normals.push_back(p);
ADD_TANGENT(-z, 0.0, -x, 1.0)
uvs.push_back(Vector2(u, v * onethird));
if (p_add_uv2) {
@ -544,10 +544,10 @@ void CapsuleMesh::create_mesh_array(Array &p_arr, const float radius, const floa
v /= (rings + 1);
if (j == (rings + 1)) {
w = 0.0;
y = -radius;
y = -1.0;
} else {
w = Math::cos(0.5 * Math_PI * v);
y = -Math::sin(0.5 * Math_PI * v) * radius;
y = -Math::sin(0.5 * Math_PI * v);
}
for (i = 0; i <= radial_segments; i++) {
@ -562,9 +562,9 @@ void CapsuleMesh::create_mesh_array(Array &p_arr, const float radius, const floa
z = Math::cos(u * Math_TAU);
}
Vector3 p = Vector3(x * radius * w, y, -z * radius * w);
points.push_back(p + Vector3(0.0, -0.5 * height + radius, 0.0));
normals.push_back(p.normalized());
Vector3 p = Vector3(x * w, y, -z * w);
points.push_back(p * radius + Vector3(0.0, -0.5 * height + radius, 0.0));
normals.push_back(p);
ADD_TANGENT(-z, 0.0, -x, 1.0)
uvs.push_back(Vector2(u, twothirds + v * onethird));
if (p_add_uv2) {
@ -1941,14 +1941,14 @@ void SphereMesh::create_mesh_array(Array &p_arr, float radius, float height, int
int i, j, prevrow, thisrow, point;
float x, y, z;
float scale = height * (is_hemisphere ? 1.0 : 0.5);
float scale = height / radius * (is_hemisphere ? 1.0 : 0.5);
// Only used if we calculate UV2
float circumference = radius * Math_TAU;
float horizontal_length = circumference + p_uv2_padding;
float center_h = 0.5 * circumference / horizontal_length;
float height_v = scale * Math_PI / ((scale * Math_PI) + p_uv2_padding);
float height_v = scale * Math_PI / ((scale * Math_PI) + p_uv2_padding / radius);
// set our bounding box
@ -1975,10 +1975,10 @@ void SphereMesh::create_mesh_array(Array &p_arr, float radius, float height, int
v /= (rings + 1);
if (j == (rings + 1)) {
w = 0.0;
y = -scale;
y = -1.0;
} else {
w = Math::sin(Math_PI * v);
y = Math::cos(Math_PI * v) * scale;
y = Math::cos(Math_PI * v);
}
for (i = 0; i <= radial_segments; i++) {
@ -1997,9 +1997,9 @@ void SphereMesh::create_mesh_array(Array &p_arr, float radius, float height, int
points.push_back(Vector3(x * radius * w, 0.0, z * radius * w));
normals.push_back(Vector3(0.0, -1.0, 0.0));
} else {
Vector3 p = Vector3(x * radius * w, y, z * radius * w);
points.push_back(p);
Vector3 normal = Vector3(x * w * scale, radius * (y / scale), z * w * scale);
Vector3 p = Vector3(x * w, y * scale, z * w);
points.push_back(p * radius);
Vector3 normal = Vector3(x * w * scale, y, z * w * scale);
normals.push_back(normal.normalized());
}
ADD_TANGENT(z, 0.0, -x, 1.0)