Improved quality of mapping points from cube to sphere
All checks were successful
Gitea Actions Demo / Scan the project (push) Successful in 19s

This commit is contained in:
Daniel 2024-01-07 22:04:39 +01:00
parent 551ae51b45
commit ea60034a00

View File

@ -85,7 +85,7 @@ namespace Engine_silk.NET
Vector2D<float> percent = new Vector2D<float>(x, y) / (_resolution - 1); Vector2D<float> percent = new Vector2D<float>(x, y) / (_resolution - 1);
// place vertex on correct position of the plane to easily calculate indices // place vertex on correct position of the plane to easily calculate indices
Vector3D<float> vertexPosition = _localUp + (percent.X - 0.5f) * 2 * _localX + (percent.Y - 0.5f) * 2 * _localY; Vector3D<float> 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 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.Indices = indices;
_mesh.CalculateNormals(); _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<float> ConvertToPointOnSphere(Vector3D<float> 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<float>(newX, newY, newZ);
}
} }
} }