From b454bfc6df5eab356c3d597f9e31f3e4342dbfb6 Mon Sep 17 00:00:00 2001 From: Jakob Hellermann Date: Fri, 8 Jan 2021 09:45:12 +0100 Subject: [PATCH] explain icosphere number of points calculation without citing BY-NC license --- .../bevy_render/src/mesh/shape/icosphere.rs | 33 +++++++++++++++++-- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/crates/bevy_render/src/mesh/shape/icosphere.rs b/crates/bevy_render/src/mesh/shape/icosphere.rs index 26867f9e28a3a..d4356259ca474 100644 --- a/crates/bevy_render/src/mesh/shape/icosphere.rs +++ b/crates/bevy_render/src/mesh/shape/icosphere.rs @@ -26,9 +26,36 @@ impl Default for Icosphere { impl From for Mesh { fn from(sphere: Icosphere) -> Self { if sphere.subdivisions >= 80 { - // https://oeis.org/A005901 - let subdivisions = sphere.subdivisions + 1; - let number_of_resulting_points = (subdivisions * subdivisions * 10) + 2; + /* + Number of triangles: + N = 20 + + Number of edges: + E = 30 + + Number of vertices: + V = 12 + + Number of points within a triangle (triangular numbers): + inner(s) = (s^2 + s) / 2 + + Number of points on an edge: + edges(s) = s + + Add up all vertices on the surface: + vertices(s) = edges(s) * E + inner(s - 1) * N + V + + Expand and simplify. Notice that the triangular number formula has roots at -1, and 0, so translating it one to the right fixes it. + subdivisions(s) = 30s + 20((s^2 - 2s + 1 + s - 1) / 2) + 12 + subdivisions(s) = 30s + 10s^2 - 10s + 12 + subdivisions(s) = 10(s^2 + 2s) + 12 + + Factor an (s + 1) term to simplify in terms of calculation + subdivisions(s) = 10(s + 1)^2 + 12 - 10 + resulting_vertices(s) = 10(s + 1)^2 + 2 + */ + let temp = sphere.subdivisions + 1; + let number_of_resulting_points = temp * temp * 10 + 2; panic!( "Cannot create an icosphere of {} subdivisions due to there being too many vertices being generated: {}. (Limited to 65535 vertices or 79 subdivisions)",