From cd2983d12bba66fb28ae9c89a92297bbb89c6d58 Mon Sep 17 00:00:00 2001 From: ayanchavand Date: Wed, 10 Jul 2024 17:37:31 +0530 Subject: [PATCH 1/7] sphere fix --- scene/resources/3d/primitive_meshes.cpp | 88 ++++++++++++++++--------- 1 file changed, 58 insertions(+), 30 deletions(-) diff --git a/scene/resources/3d/primitive_meshes.cpp b/scene/resources/3d/primitive_meshes.cpp index ee772f960a00..b0897748df10 100644 --- a/scene/resources/3d/primitive_meshes.cpp +++ b/scene/resources/3d/primitive_meshes.cpp @@ -41,6 +41,30 @@ /** PrimitiveMesh */ +const double EPSILON = 1e-7; + +double remove_negative_zero(double value) { + return std::abs(value) < EPSILON ? 0.0 : value; +} + +double custom_sin(double x) { + x = std::fmod(x, Math_TAU); + if (x < 0) x += Math_TAU; + return remove_negative_zero(std::sin(x)); +} + +double custom_cos(double x) { + x = std::fmod(x, Math_TAU); + if (x < 0) x += Math_TAU; + return remove_negative_zero(std::cos(x)); +} + +Vector3 remove_negative_zero_vector3(const Vector3& v) { + return Vector3(remove_negative_zero(v.x), + remove_negative_zero(v.y), + remove_negative_zero(v.z)); +} + void PrimitiveMesh::_update() const { Array arr; if (GDVIRTUAL_CALL(_create_mesh_array, arr)) { @@ -415,8 +439,8 @@ void CapsuleMesh::create_mesh_array(Array &p_arr, const float radius, const floa u = i; u /= radial_segments; - x = -sin(u * Math_TAU); - z = cos(u * Math_TAU); + x = -custom_sin(u * Math_TAU); + z = custom_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)); @@ -966,6 +990,8 @@ void CylinderMesh::_create_mesh_array(Array &p_arr) const { void CylinderMesh::create_mesh_array(Array &p_arr, float top_radius, float bottom_radius, float height, int radial_segments, int rings, bool cap_top, bool cap_bottom, bool p_add_uv2, const float p_uv2_padding) { int i, j, prevrow, thisrow, point; float x, y, z, u, v, radius, radius_h; + const double EPSILON = 1e-10; + // Only used if we calculate UV2 float top_circumference = top_radius * Math_PI * 2.0; @@ -1011,8 +1037,8 @@ void CylinderMesh::create_mesh_array(Array &p_arr, float top_radius, float botto u = i; u /= radial_segments; - x = sin(u * Math_TAU); - z = cos(u * Math_TAU); + x = custom_sin(u * Math_TAU); + z = custom_cos(u * Math_TAU); Vector3 p = Vector3(x * radius, y, z * radius); points.push_back(p); @@ -1813,32 +1839,34 @@ void SphereMesh::create_mesh_array(Array &p_arr, float radius, float height, int thisrow = 0; prevrow = 0; - for (j = 0; j <= (rings + 1); j++) { - float v = j; - float w; - - v /= (rings + 1); - w = sin(Math_PI * v); - y = scale * cos(Math_PI * v); - - for (i = 0; i <= radial_segments; i++) { - float u = i; - u /= radial_segments; - - x = sin(u * Math_TAU); - z = cos(u * Math_TAU); - - if (is_hemisphere && y < 0.0) { - 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); - normals.push_back(normal.normalized()); - } - ADD_TANGENT(z, 0.0, -x, 1.0) - uvs.push_back(Vector2(u, v)); + for (j = 0; j <= (rings + 1); j++) { + float v = static_cast(j) / (rings + 1); + float phi = M_PI * v; + float w = custom_sin(phi); + y = scale * custom_cos(phi); + + for (i = 0; i <= radial_segments; i++) { + float u = static_cast(i) / radial_segments; + float theta = u * Math_TAU; + x = custom_sin(theta); + z = custom_cos(theta); + + Vector3 p; + Vector3 normal; + + if (is_hemisphere && y < 0.0) { + p = remove_negative_zero_vector3(Vector3(x * radius * w, 0.0, z * radius * w)); + normal = Vector3(0.0, -1.0, 0.0); + } else { + p = remove_negative_zero_vector3(Vector3(x * radius * w, y, z * radius * w)); + normal = remove_negative_zero_vector3(Vector3(x * w * scale, radius * (y / scale), z * w * scale)); + normal.normalize(); + } + + points.push_back(p); + normals.push_back(normal); + ADD_TANGENT(remove_negative_zero(z), 0.0, remove_negative_zero(-x), 1.0) + uvs.push_back(Vector2(u, v)); if (p_add_uv2) { float w_h = w * 2.0 * center_h; uv2s.push_back(Vector2(center_h + ((u - 0.5) * w_h), v * height_v)); From 313b4654dbca902d077237d654c48c88f719c028 Mon Sep 17 00:00:00 2001 From: ayanchavand Date: Thu, 11 Jul 2024 12:23:09 +0530 Subject: [PATCH 2/7] sphere fix --- scene/resources/3d/primitive_meshes.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scene/resources/3d/primitive_meshes.cpp b/scene/resources/3d/primitive_meshes.cpp index b0897748df10..32fcd1457a3b 100644 --- a/scene/resources/3d/primitive_meshes.cpp +++ b/scene/resources/3d/primitive_meshes.cpp @@ -439,8 +439,8 @@ void CapsuleMesh::create_mesh_array(Array &p_arr, const float radius, const floa u = i; u /= radial_segments; - x = -custom_sin(u * Math_TAU); - z = custom_cos(u * Math_TAU); + x = -sin(u * Math_TAU); + z = 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)); From 2de440c4efdaedbe23f3071fbc618f233d68495c Mon Sep 17 00:00:00 2001 From: Ayan Chavand <71554089+ayanchavand@users.noreply.github.com> Date: Thu, 11 Jul 2024 14:34:20 +0530 Subject: [PATCH 3/7] indentation Co-authored-by: A Thousand Ships <96648715+AThousandShips@users.noreply.github.com> --- scene/resources/3d/primitive_meshes.cpp | 30 +++++++++++++------------ 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/scene/resources/3d/primitive_meshes.cpp b/scene/resources/3d/primitive_meshes.cpp index 32fcd1457a3b..b00903cbe055 100644 --- a/scene/resources/3d/primitive_meshes.cpp +++ b/scene/resources/3d/primitive_meshes.cpp @@ -43,26 +43,28 @@ */ const double EPSILON = 1e-7; -double remove_negative_zero(double value) { - return std::abs(value) < EPSILON ? 0.0 : value; +static double _remove_negative_zero(double p_value) { + return Math::abs(p_value) < EPSILON ? 0.0 : p_value; } -double custom_sin(double x) { - x = std::fmod(x, Math_TAU); - if (x < 0) x += Math_TAU; - return remove_negative_zero(std::sin(x)); +static double _custom_sin(double p_x) { + p_x = Math::fmod(p_x, Math_TAU); + if (p_x < 0) { + p_x += Math_TAU; + } + return _remove_negative_zero(Math::sin(p_x)); } -double custom_cos(double x) { - x = std::fmod(x, Math_TAU); - if (x < 0) x += Math_TAU; - return remove_negative_zero(std::cos(x)); +static double _custom_cos(double p_x) { + p_x = Math::fmod(p_x, Math_TAU); + if (p_x < 0) { + p_x += Math_TAU; + } + return _remove_negative_zero(Math::cos(p_x)); } -Vector3 remove_negative_zero_vector3(const Vector3& v) { - return Vector3(remove_negative_zero(v.x), - remove_negative_zero(v.y), - remove_negative_zero(v.z)); +static Vector3 _remove_negative_zero_vector3(const Vector3 &p_v) { + return Vector3(_remove_negative_zero(p_v.x), _remove_negative_zero(p_v.y), _remove_negative_zero(p_v.z)); } void PrimitiveMesh::_update() const { From baaaa66f1a54802f1c4deba7ebfc6bffb6eb63f1 Mon Sep 17 00:00:00 2001 From: Ayan Chavand <71554089+ayanchavand@users.noreply.github.com> Date: Thu, 11 Jul 2024 14:34:51 +0530 Subject: [PATCH 4/7] formatting Co-authored-by: A Thousand Ships <96648715+AThousandShips@users.noreply.github.com> --- scene/resources/3d/primitive_meshes.cpp | 56 ++++++++++++------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/scene/resources/3d/primitive_meshes.cpp b/scene/resources/3d/primitive_meshes.cpp index b00903cbe055..849ab5541310 100644 --- a/scene/resources/3d/primitive_meshes.cpp +++ b/scene/resources/3d/primitive_meshes.cpp @@ -1841,34 +1841,34 @@ void SphereMesh::create_mesh_array(Array &p_arr, float radius, float height, int thisrow = 0; prevrow = 0; - for (j = 0; j <= (rings + 1); j++) { - float v = static_cast(j) / (rings + 1); - float phi = M_PI * v; - float w = custom_sin(phi); - y = scale * custom_cos(phi); - - for (i = 0; i <= radial_segments; i++) { - float u = static_cast(i) / radial_segments; - float theta = u * Math_TAU; - x = custom_sin(theta); - z = custom_cos(theta); - - Vector3 p; - Vector3 normal; - - if (is_hemisphere && y < 0.0) { - p = remove_negative_zero_vector3(Vector3(x * radius * w, 0.0, z * radius * w)); - normal = Vector3(0.0, -1.0, 0.0); - } else { - p = remove_negative_zero_vector3(Vector3(x * radius * w, y, z * radius * w)); - normal = remove_negative_zero_vector3(Vector3(x * w * scale, radius * (y / scale), z * w * scale)); - normal.normalize(); - } - - points.push_back(p); - normals.push_back(normal); - ADD_TANGENT(remove_negative_zero(z), 0.0, remove_negative_zero(-x), 1.0) - uvs.push_back(Vector2(u, v)); + for (j = 0; j <= (rings + 1); j++) { + float v = static_cast(j) / (rings + 1); + float phi = M_PI * v; + float w = _custom_sin(phi); + y = scale * _custom_cos(phi); + + for (i = 0; i <= radial_segments; i++) { + float u = static_cast(i) / radial_segments; + float theta = u * Math_TAU; + x = _custom_sin(theta); + z = _custom_cos(theta); + + Vector3 p; + Vector3 normal; + + if (is_hemisphere && y < 0.0) { + p = _remove_negative_zero_vector3(Vector3(x * radius * w, 0.0, z * radius * w)); + normal = Vector3(0.0, -1.0, 0.0); + } else { + p = _remove_negative_zero_vector3(Vector3(x * radius * w, y, z * radius * w)); + normal = _remove_negative_zero_vector3(Vector3(x * w * scale, radius * (y / scale), z * w * scale)); + normal.normalize(); + } + + points.push_back(p); + normals.push_back(normal); + ADD_TANGENT(_remove_negative_zero(z), 0.0, _remove_negative_zero(-x), 1.0) + uvs.push_back(Vector2(u, v)); if (p_add_uv2) { float w_h = w * 2.0 * center_h; uv2s.push_back(Vector2(center_h + ((u - 0.5) * w_h), v * height_v)); From da6d6201630b50bb36a713e39a5b911256fc09fe Mon Sep 17 00:00:00 2001 From: Ayan Chavand <71554089+ayanchavand@users.noreply.github.com> Date: Thu, 11 Jul 2024 14:35:11 +0530 Subject: [PATCH 5/7] function name change Co-authored-by: A Thousand Ships <96648715+AThousandShips@users.noreply.github.com> --- scene/resources/3d/primitive_meshes.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scene/resources/3d/primitive_meshes.cpp b/scene/resources/3d/primitive_meshes.cpp index 849ab5541310..616b373f9673 100644 --- a/scene/resources/3d/primitive_meshes.cpp +++ b/scene/resources/3d/primitive_meshes.cpp @@ -1039,8 +1039,8 @@ void CylinderMesh::create_mesh_array(Array &p_arr, float top_radius, float botto u = i; u /= radial_segments; - x = custom_sin(u * Math_TAU); - z = custom_cos(u * Math_TAU); + x = _custom_sin(u * Math_TAU); + z = _custom_cos(u * Math_TAU); Vector3 p = Vector3(x * radius, y, z * radius); points.push_back(p); From 883a86e357dcd5faae044a3c6387cd79eeeb4394 Mon Sep 17 00:00:00 2001 From: ayanchavand Date: Thu, 11 Jul 2024 15:01:43 +0530 Subject: [PATCH 6/7] removed unused Epsilon --- scene/resources/3d/primitive_meshes.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/scene/resources/3d/primitive_meshes.cpp b/scene/resources/3d/primitive_meshes.cpp index 616b373f9673..043c00e82d86 100644 --- a/scene/resources/3d/primitive_meshes.cpp +++ b/scene/resources/3d/primitive_meshes.cpp @@ -992,8 +992,6 @@ void CylinderMesh::_create_mesh_array(Array &p_arr) const { void CylinderMesh::create_mesh_array(Array &p_arr, float top_radius, float bottom_radius, float height, int radial_segments, int rings, bool cap_top, bool cap_bottom, bool p_add_uv2, const float p_uv2_padding) { int i, j, prevrow, thisrow, point; float x, y, z, u, v, radius, radius_h; - const double EPSILON = 1e-10; - // Only used if we calculate UV2 float top_circumference = top_radius * Math_PI * 2.0; From 3f19b4baca448ccc6cc8c060f7e1def142c9f5ee Mon Sep 17 00:00:00 2001 From: Ayan Chavand <71554089+ayanchavand@users.noreply.github.com> Date: Thu, 11 Jul 2024 15:15:04 +0530 Subject: [PATCH 7/7] Math_PI Co-authored-by: A Thousand Ships <96648715+AThousandShips@users.noreply.github.com> --- scene/resources/3d/primitive_meshes.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scene/resources/3d/primitive_meshes.cpp b/scene/resources/3d/primitive_meshes.cpp index 043c00e82d86..65883ca49aba 100644 --- a/scene/resources/3d/primitive_meshes.cpp +++ b/scene/resources/3d/primitive_meshes.cpp @@ -1841,7 +1841,7 @@ void SphereMesh::create_mesh_array(Array &p_arr, float radius, float height, int prevrow = 0; for (j = 0; j <= (rings + 1); j++) { float v = static_cast(j) / (rings + 1); - float phi = M_PI * v; + float phi = Math_PI * v; float w = _custom_sin(phi); y = scale * _custom_cos(phi);