private static int getOrCreateMiddlePoint(int p1, int p2, ref List<Vector3> vertices, ref Dictionary<Int64, int> midPointCache)
{
// first check if we have it already
bool firstIsSmaller = p1 < p2;
Int64 smallerIndex = firstIsSmaller ? p1 : p2;
Int64 greaterIndex = firstIsSmaller ? p2 : p1;
Int64 key = (smallerIndex << 32) + greaterIndex;
int ret;
if (midPointCache.TryGetValue(key, out ret))
{
return ret;
}
// not in cache, calculate it
Vector3 point1 = vertices[p1];
Vector3 point2 = vertices[p2];
Vector3 middle = new Vector3(
(point1.x + point2.x) / 2.0f,
(point1.y + point2.y) / 2.0f,
(point1.z + point2.z) / 2.0f);
// add vertex makes sure point is on unit sphere
vertices.Add(middle.normalized);
int index = vertices.Count -1;
// store it, return index
midPointCache.Add(key, index);
return index;
}