// todo : fold this into triangle constructor / creation private void SubdivideGeo(Vector3 v1, Vector3 v2, Vector3 v3, Vector2 uv1, Vector2 uv2, Vector2 uv3, Triangle parent, int depth) { if (depth == 0) { parent.AddGeoTri(new GeoTriangle(_spherePoints.Count, _sphereUvs.Count, _sphereTris.Count, GetCtrVert(parent.Id, parent.IsUp))); // Add info to parent to be able to go through all _spherePoints.Add(v1); _spherePoints.Add(v2); _spherePoints.Add(v3); _sphereUvs.Add(uv1); _sphereUvs.Add(uv2); _sphereUvs.Add(uv3); _sphereTris.Add(_spherePoints.Count - 1); _sphereTris.Add(_spherePoints.Count - 2); _sphereTris.Add(_spherePoints.Count - 3); return; } var vctr = (v1 + v2 + v3).normalized; var v12a = (v1 + (v2 - v1) / 3f).normalized; var v12b = (v1 + (v2 - v1) / 3f * 2f).normalized; var v23a = (v2 + (v3 - v2) / 3f).normalized; var v23b = (v2 + (v3 - v2) / 3f * 2f).normalized; var v13a = (v1 + (v3 - v1) / 3f).normalized; var v13b = (v1 + (v3 - v1) / 3f * 2f).normalized; var uvctr = (uv1 + uv2 + uv3) / 3f; var uv12a = (uv1 + (uv2 - uv1) / 3f); var uv12b = (uv1 + (uv2 - uv1) / 3f * 2f); var uv23a = (uv2 + (uv3 - uv2) / 3f); var uv23b = (uv2 + (uv3 - uv2) / 3f * 2f); var uv13a = (uv1 + (uv3 - uv1) / 3f); var uv13b = (uv1 + (uv3 - uv1) / 3f * 2f); var nextDepth = depth - 1; if (parent.IsUp) { SubdivideGeo(v13b, v23b, v3, uv13b, uv23b, uv3, parent.Children[SubTriangles.TriTopBot], nextDepth); // tri top SubdivideGeo(v1, v12a, v13a, uv1, uv12a, uv13a, parent.Children[SubTriangles.TriLeft], nextDepth); // tri left SubdivideGeo(v12b, v2, v23a, uv12b, uv2, uv23a, parent.Children[SubTriangles.TriRight], nextDepth); // tri right SubdivideGeo(v12a, v12b, vctr, uv12a, uv12b, uvctr, parent.Children[SubTriangles.HexBot], nextDepth); // hex bot SubdivideGeo(v13a, v12a, vctr, uv13a, uv12a, uvctr, parent.Children[SubTriangles.HexBotLeft], nextDepth); // hex bot left SubdivideGeo(vctr, v12b, v23a, uvctr, uv12b, uv23a, parent.Children[SubTriangles.HexBotRight], nextDepth); // hex bot right SubdivideGeo(vctr, v23a, v23b, uvctr, uv23a, uv23b, parent.Children[SubTriangles.HexTopRight], nextDepth); // hex top right SubdivideGeo(v13a, vctr, v13b, uv13a, uvctr, uv13b, parent.Children[SubTriangles.HexTopLeft], nextDepth); // hex top left SubdivideGeo(v13b, vctr, v23b, uv13b, uvctr, uv23b, parent.Children[SubTriangles.HexTop], nextDepth); // hex top } else { SubdivideGeo(v12b, v2, v23a, uv12b, uv2, uv23a, parent.Children[SubTriangles.TriTopBot], nextDepth); // tri top SubdivideGeo(v1, v12a, v13a, uv1, uv12a, uv13a, parent.Children[SubTriangles.TriLeft], nextDepth); // tri left SubdivideGeo(v13b, v23b, v3, uv13b, uv23b, uv3, parent.Children[SubTriangles.TriRight], nextDepth); // tri right SubdivideGeo(v12b, v23a, vctr, uv12b, uv23a, uvctr, parent.Children[SubTriangles.HexBot], nextDepth); // hex bot SubdivideGeo(v12a, v12b, vctr, uv12a, uv12b, uvctr, parent.Children[SubTriangles.HexBotLeft], nextDepth); // hex bot left SubdivideGeo(vctr, v23a, v23b, uvctr, uv23a, uv23b, parent.Children[SubTriangles.HexBotRight], nextDepth); // hex bot right SubdivideGeo(vctr, v23b, v13b, uvctr, uv23b, uv13b, parent.Children[SubTriangles.HexTopRight], nextDepth); // hex top right SubdivideGeo(v12a, vctr, v13a, uv12a, uvctr, uv13a, parent.Children[SubTriangles.HexTopLeft], nextDepth); // hex top left SubdivideGeo(v13a, vctr, v13b, uv13a, uvctr, uv13b, parent.Children[SubTriangles.HexTop], nextDepth); // hex top } }