Skip to content

Instantly share code, notes, and snippets.

@sagarpatel
Created July 18, 2016 01:31
Show Gist options
  • Save sagarpatel/a692300f8ed292063326f78259add68c to your computer and use it in GitHub Desktop.
Save sagarpatel/a692300f8ed292063326f78259add68c to your computer and use it in GitHub Desktop.
void SpawnMeshStrip(int stripIndex)//, float stripDistanceFromPrevious)
{
Profiler.BeginSample("Set Active");
m_meshStripGeneratorsGOArray[stripIndex].SetActive(true);
Profiler.EndSample();
Profiler.BeginSample("Pre Loop Setup");
m_meshStripGeneratorsGOArray[stripIndex].transform.position = transform.position;
m_meshStripGeneratorsGOArray[stripIndex].transform.rotation = transform.rotation;
m_meshStripGeneratorsGOArray[stripIndex].transform.localScale = Vector3.one;
Profiler.BeginSample("Quaternion inverse multiply");
t_diffQuaternion = transform.rotation * Quaternion.Inverse(m_lastGeneratedMeshStrip_Rotation);
Profiler.EndSample();
Vector3 vertexWorldPos_Right;
Vector3 vertexWorldPos_Left;
Vector3 generatedFrontRowVertex_Right;
Vector3 generatedFrontRowVertex_Left;
Vector3 vertexUpVector_Right;
Vector3 vertexUpVector_Left;
Vector3 circleFormUp;
Profiler.EndSample();
Profiler.BeginSample("For loop");
// set vertices here
for (int i = 0; i < t_calcFrontRowVertsArray_Right.Length; i++)
{
Profiler.BeginSample("Generate baseline vertex and assign");
generatedFrontRowVertex_Right = GenerateFrontRowBaselineVertex(i, m_bendFactor);
Profiler.EndSample();
Profiler.BeginSample("Simple math");
generatedFrontRowVertex_Left = generatedFrontRowVertex_Right;
generatedFrontRowVertex_Left.x *= -1;
m_circleCenterPos.y = Mathf.Sign(m_bendFactor) * Mathf.Abs(m_circleCenterPos.y);
if (m_circleCenterPos.y > 0)
circleFormUp = (m_circleCenterPos - generatedFrontRowVertex_Right).normalized;
else
circleFormUp = (generatedFrontRowVertex_Right - m_circleCenterPos).normalized;
vertexUpVector_Right = Vector3.Lerp(Vector3.up, circleFormUp, Mathf.Abs(m_bendFactor));
vertexUpVector_Left = vertexUpVector_Right;
vertexUpVector_Left.x *= -1;
Profiler.EndSample();
Profiler.BeginSample("Bottom part of loop");
t_calcFrontRowVertsArray_Right[i] = generatedFrontRowVertex_Right + vertexUpVector_Right * m_freshHeighValues_Right[i]; //m_lastGeneratedMeshStrip_FrontRowVerticesArray_Right[i] + transform.up * m_freshHeighValues_Right[i];// + transform.forward * stripDistanceFromPrevious;
Profiler.BeginSample("Transform point");
vertexWorldPos_Right = m_lastGeneratedMeshStrip_Transform.TransformPoint(m_lastGeneratedMeshStrip_FrontRowVerticesArray_Right[i]);
Profiler.EndSample();
t_calcBackRowVertsArray_Right[i] = m_meshStripGeneratorsGOArray[stripIndex].transform.InverseTransformPoint(vertexWorldPos_Right); //t_diffQuaternion * m_lastGeneratedMeshStrip_FrontRowVerticesArray_Right[i];
t_calcFrontRowVertsArray_Left[i] = generatedFrontRowVertex_Left + vertexUpVector_Left * m_freshHeighValues_Left[i]; //m_lastGeneratedMeshStrip_FrontRowVerticesArray_Left[i] + transform.up * m_freshHeighValues_Left[i];// + transform.forward * stripDistanceFromPrevious;
Profiler.BeginSample("Transform point");
vertexWorldPos_Left = m_lastGeneratedMeshStrip_Transform.TransformPoint(m_lastGeneratedMeshStrip_FrontRowVerticesArray_Left[i]);
Profiler.EndSample();
t_calcBackRowVertsArray_Left[i] = m_meshStripGeneratorsGOArray[stripIndex].transform.InverseTransformPoint(vertexWorldPos_Left); //t_diffQuaternion * m_lastGeneratedMeshStrip_FrontRowVerticesArray_Left[i];
t_stripUpVectorsArray_Right[i] = vertexUpVector_Right;
t_stripUpVectorsArray_Left[i] = vertexUpVector_Left;
Profiler.EndSample();
}
Profiler.EndSample();
Profiler.BeginSample("Set Vertices Left/Right Fuctions");
m_meshStripGeneratorsArray[stripIndex].SetRowsVertices_Right(t_calcFrontRowVertsArray_Right, t_calcBackRowVertsArray_Right, t_stripUpVectorsArray_Right, m_freshHeighValues_Right, m_bendFactor);
m_meshStripGeneratorsArray[stripIndex].SetRowsVertices_Left(t_calcFrontRowVertsArray_Left, t_calcBackRowVertsArray_Left, t_stripUpVectorsArray_Left, m_freshHeighValues_Left, m_bendFactor);
Profiler.EndSample();
m_lastActivatedStripIndex = stripIndex;
Profiler.BeginSample("Get Row vertices");
m_lastGeneratedMeshStrip_FrontRowVerticesArray_Right = m_meshStripGeneratorsArray[stripIndex].GetFrontRowVertices_Right();
m_lastGeneratedMeshStrip_FrontRowVerticesArray_Left = m_meshStripGeneratorsArray[stripIndex].GetFrontRowVertices_Left();
Profiler.EndSample();
m_lastGeneratedMeshStrip_Rotation = m_meshStripGeneratorsGOArray[stripIndex].transform.rotation;
m_lastGeneratedMeshStrip_Transform = m_meshStripGeneratorsGOArray[stripIndex].transform;
Profiler.BeginSample("null check");
if (m_lastGeneratedMeshStrip != null)
{
// temporarily commenting out because of garbage generated cause by .normals read and GC spikes. Fix idea: calculate normals manually manually and never read, only set
// m_meshStripGeneratorsArray[stripIndex].AverageNormalsForCommonVertices(m_lastGeneratedMeshStrip.m_mesh_Up_Right, m_lastGeneratedMeshStrip.m_mesh_Up_Left);
}
Profiler.EndSample();
m_lastGeneratedMeshStrip = m_meshStripGeneratorsArray[stripIndex];
// doing origin strip stuff
// calculate vertices center
Profiler.BeginSample("Mesh origin calcs");
Vector3 averageSum = Vector3.zero;
for (int i = 0; i < m_lastGeneratedMeshStrip_FrontRowVerticesArray_Left.Length; i++)
averageSum += m_lastGeneratedMeshStrip_FrontRowVerticesArray_Left[i];
averageSum = averageSum / (float)m_lastGeneratedMeshStrip_FrontRowVerticesArray_Left.Length;
// kill the x, since center point is always x-axis centered, z should be anyways
averageSum.x = 0;
averageSum.z = m_generationDepth;
for (int i = 0; i < m_generationCenterPointVertices.Length; i++)
m_generationCenterPointVertices[i] = averageSum;
Profiler.EndSample();
Profiler.BeginSample("Set Rows Vertices, has array copy");
m_meshCreatureOriginMesh.SetRowsVertices_Right(m_generationCenterPointVertices, m_lastGeneratedMeshStrip_FrontRowVerticesArray_Right, t_stripUpVectorsArray_Right, t_zeroHeightsArray, 0);
m_meshCreatureOriginMesh.SetRowsVertices_Left(m_generationCenterPointVertices, m_lastGeneratedMeshStrip_FrontRowVerticesArray_Left, t_stripUpVectorsArray_Left, t_zeroHeightsArray, 0);
// do normals averaging?
Profiler.EndSample();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment