public bool ConstructNodes(List<VertexPositionColorNormalNormal> vertices, ref int n_index, int threaded = 0)
{
if (size == 1)
return ConstructLeaf(ref vertices, ref n_index);
type = NodeType.Internal;
int child_size = size / 2;
bool has_children = false;
Task[] threads = new Task[8];
bool[] return_values = new bool[8];
for (int i = 0; i < 8; i++)
{
this.index = n_index++;
Vector3 child_pos = Utilities.TCornerDeltas[i];
children[i] = new OctreeNode(position + child_pos * (float)child_size, child_size, NodeType.Internal);
children[i].child_index = i;
int index = i;
if (threaded > 0 && size > 2)
{
threads[index] = Task.Factory.StartNew(
() =>
{
int temp = 0;
return_values[index] = children[index].ConstructNodes(vertices, ref temp, threaded - 1);
if (!return_values[index])
children[index] = null;
}, TaskCreationOptions.AttachedToParent);
//threads[index].Start();
}
else
{
if (children[i].ConstructNodes(vertices, ref n_index, 0))
has_children = true;
else
children[i] = null;
}
}
if (threaded > 0 && size > 2)
{
for (int i = 0; i < 8; i++)
{
threads[i].Wait();
if (return_values[i])
has_children = true;
}
}
return has_children;
}