From ea60034a001497c38389b76a7cb4aa5c1a4d6d4a Mon Sep 17 00:00:00 2001 From: Daniel Date: Sun, 7 Jan 2024 22:04:39 +0100 Subject: [PATCH] Improved quality of mapping points from cube to sphere --- src/Sphere.cs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Sphere.cs b/src/Sphere.cs index f4667cd..1c8ebc0 100644 --- a/src/Sphere.cs +++ b/src/Sphere.cs @@ -85,7 +85,7 @@ namespace Engine_silk.NET Vector2D percent = new Vector2D(x, y) / (_resolution - 1); // place vertex on correct position of the plane to easily calculate indices Vector3D vertexPosition = _localUp + (percent.X - 0.5f) * 2 * _localX + (percent.Y - 0.5f) * 2 * _localY; - vertices[i] = Vector3D.Normalize(vertexPosition); // normalise vertex position to get it to be "on the sphere" and not "on the plane" + vertices[i] = ConvertToPointOnSphere(vertexPosition); if (x != _resolution - 1 && y != _resolution - 1) // we didn't reach the bottom right point yet { @@ -107,5 +107,18 @@ namespace Engine_silk.NET _mesh.Indices = indices; _mesh.CalculateNormals(); } + + // Smooth mapping so that the points are not clumped on the former corners of the cube + // http://mathproofs.blogspot.com/2005/07/mapping-cube-to-sphere.html + private Vector3D ConvertToPointOnSphere(Vector3D point) + { + float x2 = point.X * point.X; + float y2 = point.Y * point.Y; + float z2 = point.Z * point.Z; + float newX = point.X * MathF.Sqrt(1 - y2 / 2 - z2 / 2 + (y2 * z2) / 3); + float newY = point.Y * MathF.Sqrt(1 - z2 / 2 - x2 / 2 + (z2 * x2) / 3); + float newZ = point.Z * MathF.Sqrt(1 - x2 / 2 - y2 / 2 + (x2 * y2) / 3); + return new Vector3D(newX, newY, newZ); + } } }