I’m working on a project where I am importing some mocap data of people doing some actions into Unity, the idea is to play through the data with each point leaving a trail as it moves.
[EDIT] This is to be used with the Oculus rift, so I’m using the Oculus OVRcamera prefab
The way I have set it up so far, at start up I read each mocap file(one per frame - 325 frames) and store the position vectors in a huge 2D array, one dimension for frame the other for the points(325 frames by 982 points).
Then I create a mesh per frame and this is where my troubles start.
I iterate through the frames and instantiate a basic mesh at each point (from the mocap data) for that frame, I then combine those instantiated objects into one mesh and destroy the original instantiations.
BUT I can only do this if my frame range in 100, any more and Unity just crashes and closes down.
void createLayers()
{
// iterate through each frame
for (int i =0; i<m_frameRange; i++)
{
// temporary game object to be parent of instantiated meshes,
// used for combining meshes
GameObject _layers = new GameObject();
// array of game objects to hold the combined meshes
m_mergedMeshes *= new GameObject("Layer_"+i.ToString("D4"));*
-
// iterate through each point*
-
for(int j=0;j<m_totalPoints;j++)*
-
{*
-
GameObject tmp = Instantiate(m_origMesh);*
-
tmp.transform.position = Vec4ToVec3 (m_pointData [m_startFrame+i,j]);*
-
tmp.transform.parent = _layers.transform;*
-
}*
-
combinePoints(_layers,i);*
-
Destroy (_layers); *
-
}*
}
My combinePoints functions is:
-
void combinePoints(GameObject _objects, int index)*
-
{*
//_object is the parent object of the instantiated objects
// index is related to which frame this mesh is -
MeshFilter[] meshFilters = _objects.GetComponentsInChildren<MeshFilter> ();*
-
Transform[] meshTrans = _objects.GetComponentsInChildren<Transform> ();*
-
CombineInstance[] combine = new CombineInstance[meshFilters.Length];*
-
int id = 0;*
-
for (int i=0; i<meshFilters.Length; i++)*
-
{*
_ if(meshFilters*.sharedMesh == null){continue;}_
_ combine[id].mesh = meshFilters.sharedMesh;
combine[id].transform = meshFilters.transform.localToWorldMatrix;
meshFilters.gameObject.SetActive(false);
id++;
}*_
* m_mergedMeshes [index].AddComponent ();
m_mergedMeshes [index].GetComponent().mesh = new Mesh();
m_mergedMeshes [index].AddComponent ();*
* m_mergedMeshes [index].GetComponent().mesh.CombineMeshes(combine,true,true);
m_mergedMeshes [index].SetActive (true);
m_mergedMeshes [index].GetComponent ().enabled = false;*
* }*
Is this due to me running out of memory or not doing this in an efficient way?
Any help would be much appreciated!
Also my plan for creating the tails was to turn on or off the meshes depending on the current frame, this way no objects have to be created dynamically which could potentially slow down the FPS. I tried using a particle system to emit meshes but it was way too slow with the number of points I had! Any other ideas of ways to create these trails would be much appreciated also!
Thanks in advance