Hi all,
Unity Version: 5.4.0b18-HTP
I have to create a molecule with 8550 atoms so 8550 spheres and then create lines for connect this atoms.
Steps:
1- I instantiated 8550 atoms (gameobjects - prefabs) and 9000 lines (gameobjects - prefabs) for object pooling method.
2- Parse a text for get the positions of the 8550 atoms in a list. Takes 2 and 3 seconds
3- Re position of the 8550 atoms with the 8550 records in the list:
for (int i = 0; i < atomPositionList.Count; i++)
{
atomContainer.transform.GetChild(i).transform.position = atomPositionList[i];
atomContainer.transform.GetChild(i).name = atomList[i].atomName;
SetLines(i, atomList[i].elementSymbol); //BOTTLENECK HERE
}
If i don’t use SetLines method Takes 1 and 2 seconds
4- The problem (bottleneck) started when i have to calculate the position of the lines (i don’t have this data, so i have to calculate this).
private void SetLines(int currentAtom, string element)
{
Vector3 currentPosition = atomPositionList[currentAtom];
//SET RADIUS CONNECTION
if (element == "N")
{
radiusConnection = radiusDefault;
}
else if (element == "S")
{
radiusConnection = radiusMedium;
}
else
{
radiusConnection = radiusDefault;
}
var atoms = atomPositionList.Where(atom => Vector3.Distance(atom, currentPosition) <= radiusConnection && currentPosition != atom && !CheckIfLineExist(currentPosition, atom));
foreach (var atom in atoms)
{
SetLine(currentPosition, atom);
}
}
The problem here is for every atom (8550) i have to loop another 8550 times for every one for calculate the closest atom for create the line. Takes 10 a 12 seconds. Too much iterations
P.D: Before someone says don’t use ‘string’ for compare the element, use a enum (yes i know it, but this is not the problem).
5- Then group and split the atoms and lines for create combined meshes for boost the performance. Takes 150 and 30 Milliseconds
Any advice for a better way to calculate the lines?
Can i pass the complete calculation of all lines in the GPU for reduce calculation time?
Sorry for my english is not my native lenguaje.
Greetings.-