|
| 1 | +namespace CSG.Geometry |
| 2 | +{ |
| 3 | + using System; |
| 4 | + using System.Numerics; |
| 5 | + |
| 6 | + public static class Sphere |
| 7 | + { |
| 8 | + public static void CreateSolid(this IGeometryBuilder builder) |
| 9 | + => CreateSolid(builder, 1.0f, 12); |
| 10 | + |
| 11 | + public static void CreateSolid(this IGeometryBuilder builder, float radius) |
| 12 | + => CreateSolid(builder, radius, 12); |
| 13 | + |
| 14 | + public static void CreateSolid(this IGeometryBuilder builder, float radius, int tessellation) |
| 15 | + { |
| 16 | + int verticalSegments = tessellation; |
| 17 | + int horizontalSegments = tessellation * 2; |
| 18 | + |
| 19 | + // Start with a single vertex at the bottom of the sphere. |
| 20 | + builder.AddVertex(Vector3.UnitY, -Vector3.UnitY); |
| 21 | + |
| 22 | + // Create rings of vertices at progressively higher latitudes. |
| 23 | + for (int i = 0; i < verticalSegments - 1; ++i) |
| 24 | + { |
| 25 | + float latitude = ((i + 1.0f) * Algorithms.Helpers.Pi / verticalSegments) - Algorithms.Helpers.PiOver2; |
| 26 | + |
| 27 | + float dy = MathF.Sin(latitude); |
| 28 | + float dxz = MathF.Cos(latitude); |
| 29 | + |
| 30 | + // Create a single ring of vertices at this latitude. |
| 31 | + for (int j = 0; j < horizontalSegments; j++) |
| 32 | + { |
| 33 | + float longitude = j * Algorithms.Helpers.TwoPi / horizontalSegments; |
| 34 | + |
| 35 | + float dx = MathF.Cos(longitude) * dxz; |
| 36 | + float dz = MathF.Sin(longitude) * dxz; |
| 37 | + |
| 38 | + Vector3 normal = new Vector3(dx, dy, dz) * radius; |
| 39 | + |
| 40 | + builder.AddVertex(normal, normal); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + // Finish with a single vertex at the top of the sphere. |
| 45 | + builder.AddVertex(Vector3.UnitY, Vector3.UnitY); |
| 46 | + |
| 47 | + // Create a fan connecting the bottom vertex to the bottom latitude ring. |
| 48 | + for (int i = 0; i < horizontalSegments; ++i) |
| 49 | + { |
| 50 | + builder.AddIndex(0); |
| 51 | + builder.AddIndex(1 + ((i + 1) % horizontalSegments)); |
| 52 | + builder.AddIndex(1 + i); |
| 53 | + } |
| 54 | + |
| 55 | + // Fill the sphere body with triangles joining each pair of latitude rings. |
| 56 | + for (int i = 0; i < verticalSegments - 2; ++i) |
| 57 | + { |
| 58 | + for (int j = 0; j < horizontalSegments; j++) |
| 59 | + { |
| 60 | + int nextI = i + 1; |
| 61 | + int nextJ = (j + 1) % horizontalSegments; |
| 62 | + |
| 63 | + builder.AddIndex(1 + (i * horizontalSegments) + j); |
| 64 | + builder.AddIndex(1 + (i * horizontalSegments) + nextJ); |
| 65 | + builder.AddIndex(1 + (nextI * horizontalSegments) + j); |
| 66 | + |
| 67 | + builder.AddIndex(1 + (i * horizontalSegments) + nextJ); |
| 68 | + builder.AddIndex(1 + (nextI * horizontalSegments) + nextJ); |
| 69 | + builder.AddIndex(1 + (nextI * horizontalSegments) + j); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + // Create a fan connecting the top vertex to the top latitude ring. |
| 74 | + for (int i = 0; i < horizontalSegments; ++i) |
| 75 | + { |
| 76 | + builder.AddIndex(builder.CurrentVertex - 1); |
| 77 | + builder.AddIndex(builder.CurrentVertex - 2 - ((i + 1) % horizontalSegments)); |
| 78 | + builder.AddIndex(builder.CurrentVertex - 2 - i); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments