Skip to content

Commit 4c34813

Browse files
committed
Fix very large radius SphereMesh and CapsuleMesh normals
1 parent ba2c5c1 commit 4c34813

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

scene/resources/3d/primitive_meshes.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ void CapsuleMesh::create_mesh_array(Array &p_arr, const float radius, const floa
448448
y = 0.0;
449449
} else {
450450
w = Math::sin(0.5 * Math_PI * v);
451-
y = Math::cos(0.5 * Math_PI * v) * radius;
451+
y = Math::cos(0.5 * Math_PI * v);
452452
}
453453

454454
for (i = 0; i <= radial_segments; i++) {
@@ -463,9 +463,9 @@ void CapsuleMesh::create_mesh_array(Array &p_arr, const float radius, const floa
463463
z = Math::cos(u * Math_TAU);
464464
}
465465

466-
Vector3 p = Vector3(x * radius * w, y, -z * radius * w);
467-
points.push_back(p + Vector3(0.0, 0.5 * height - radius, 0.0));
468-
normals.push_back(p.normalized());
466+
Vector3 p = Vector3(x * w, y, -z * w);
467+
points.push_back(p * radius + Vector3(0.0, 0.5 * height - radius, 0.0));
468+
normals.push_back(p);
469469
ADD_TANGENT(-z, 0.0, -x, 1.0)
470470
uvs.push_back(Vector2(u, v * onethird));
471471
if (p_add_uv2) {
@@ -544,10 +544,10 @@ void CapsuleMesh::create_mesh_array(Array &p_arr, const float radius, const floa
544544
v /= (rings + 1);
545545
if (j == (rings + 1)) {
546546
w = 0.0;
547-
y = -radius;
547+
y = -1.0;
548548
} else {
549549
w = Math::cos(0.5 * Math_PI * v);
550-
y = -Math::sin(0.5 * Math_PI * v) * radius;
550+
y = -Math::sin(0.5 * Math_PI * v);
551551
}
552552

553553
for (i = 0; i <= radial_segments; i++) {
@@ -562,9 +562,9 @@ void CapsuleMesh::create_mesh_array(Array &p_arr, const float radius, const floa
562562
z = Math::cos(u * Math_TAU);
563563
}
564564

565-
Vector3 p = Vector3(x * radius * w, y, -z * radius * w);
566-
points.push_back(p + Vector3(0.0, -0.5 * height + radius, 0.0));
567-
normals.push_back(p.normalized());
565+
Vector3 p = Vector3(x * w, y, -z * w);
566+
points.push_back(p * radius + Vector3(0.0, -0.5 * height + radius, 0.0));
567+
normals.push_back(p);
568568
ADD_TANGENT(-z, 0.0, -x, 1.0)
569569
uvs.push_back(Vector2(u, twothirds + v * onethird));
570570
if (p_add_uv2) {
@@ -1941,14 +1941,14 @@ void SphereMesh::create_mesh_array(Array &p_arr, float radius, float height, int
19411941
int i, j, prevrow, thisrow, point;
19421942
float x, y, z;
19431943

1944-
float scale = height * (is_hemisphere ? 1.0 : 0.5);
1944+
float scale = height / radius * (is_hemisphere ? 1.0 : 0.5);
19451945

19461946
// Only used if we calculate UV2
19471947
float circumference = radius * Math_TAU;
19481948
float horizontal_length = circumference + p_uv2_padding;
19491949
float center_h = 0.5 * circumference / horizontal_length;
19501950

1951-
float height_v = scale * Math_PI / ((scale * Math_PI) + p_uv2_padding);
1951+
float height_v = scale * Math_PI / ((scale * Math_PI) + p_uv2_padding / radius);
19521952

19531953
// set our bounding box
19541954

@@ -1975,10 +1975,10 @@ void SphereMesh::create_mesh_array(Array &p_arr, float radius, float height, int
19751975
v /= (rings + 1);
19761976
if (j == (rings + 1)) {
19771977
w = 0.0;
1978-
y = -scale;
1978+
y = -1.0;
19791979
} else {
19801980
w = Math::sin(Math_PI * v);
1981-
y = Math::cos(Math_PI * v) * scale;
1981+
y = Math::cos(Math_PI * v);
19821982
}
19831983

19841984
for (i = 0; i <= radial_segments; i++) {
@@ -1997,9 +1997,9 @@ void SphereMesh::create_mesh_array(Array &p_arr, float radius, float height, int
19971997
points.push_back(Vector3(x * radius * w, 0.0, z * radius * w));
19981998
normals.push_back(Vector3(0.0, -1.0, 0.0));
19991999
} else {
2000-
Vector3 p = Vector3(x * radius * w, y, z * radius * w);
2001-
points.push_back(p);
2002-
Vector3 normal = Vector3(x * w * scale, radius * (y / scale), z * w * scale);
2000+
Vector3 p = Vector3(x * w, y * scale, z * w);
2001+
points.push_back(p * radius);
2002+
Vector3 normal = Vector3(x * w * scale, y, z * w * scale);
20032003
normals.push_back(normal.normalized());
20042004
}
20052005
ADD_TANGENT(z, 0.0, -x, 1.0)

0 commit comments

Comments
 (0)