PathLineDrawerCS.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * FUNCTION:
  3. * - This script is responsible for drawing the spline visually in the editor.
  4. *
  5. * USED BY:
  6. * This script is part of the CP_Straight and CP_Curve prefab which are used with
  7. * environment patches.
  8. *
  9. */
  10. using System;
  11. using UnityEngine;
  12. using System.Collections;
  13. public class PathLineDrawerCS : MonoBehaviour {
  14. public Vector3[] Parameterized_CPPositions = new Vector3[53];
  15. public float fPathLength = 3000.0f;
  16. void OnDrawGizmos ()
  17. {
  18. if(transform.Find("CP_01"))
  19. {
  20. Transform ParentGroup = transform;
  21. int i = 0;
  22. foreach (Transform child in ParentGroup)
  23. i++;
  24. int NumCPs = i;
  25. Vector3[] vectorsArray = new Vector3[NumCPs];
  26. for(i=0;i<NumCPs;i++)
  27. {
  28. if(i<9)
  29. vectorsArray[i] = ParentGroup.Find("CP_0"+(i+1)).position;
  30. else
  31. vectorsArray[i] = ParentGroup.Find("CP_"+(i+1)).position;
  32. }
  33. iTween.DrawPath(vectorsArray,Color.red);
  34. //SetCPValues();
  35. }
  36. }
  37. public void SetCPValues()
  38. {
  39. SetCurrentPatchCPs();
  40. }
  41. private void SetCurrentPatchCPs()
  42. {
  43. int i = 0;
  44. Vector3[] tempCPPositions;
  45. Transform[] AllCPs;
  46. Transform CPs_Group = transform;
  47. foreach (Transform child in CPs_Group)
  48. i++;
  49. int NumCPs = i;
  50. i = 0;
  51. AllCPs = new Transform[NumCPs];
  52. tempCPPositions = new Vector3[NumCPs];
  53. i = 0;
  54. foreach (Transform child in CPs_Group)
  55. {
  56. AllCPs[i] = child;
  57. i++;
  58. }
  59. AllCPs = SortCPsbyName(AllCPs, 0,NumCPs);
  60. for(i=0;i<NumCPs;i++)
  61. {
  62. tempCPPositions[i] = AllCPs[i].position;
  63. tempCPPositions[i].y = 0;
  64. }
  65. Parameterized_CPPositions = PathControlPointGenerator(tempCPPositions);
  66. Parameterized_CPPositions = ParameterizeCPs(Parameterized_CPPositions);
  67. fPathLength = PathLength(Parameterized_CPPositions);
  68. }
  69. private Vector3[] ParameterizeCPs(Vector3[] pts)
  70. {
  71. float i = 0.0f;
  72. float Current_TD = 0.0f; //Current total distance
  73. float TotalPathLength = PathLength(pts);
  74. float CP_Increment = TotalPathLength/50.0f;
  75. Vector3 PreviousPoint = pts[1];
  76. Vector3 CurrentPoint = PreviousPoint;
  77. Vector3[] FinalPoints = new Vector3[51];
  78. int Index = 0;
  79. FinalPoints[Index] = pts[1];
  80. Index++;
  81. for(i=0.0f;i<=1.0f;i+=0.000001f)
  82. {
  83. CurrentPoint = Interp(pts,i);
  84. Current_TD+=Vector3.Distance(CurrentPoint,PreviousPoint);
  85. if(Current_TD>=CP_Increment)
  86. {
  87. FinalPoints[Index] = CurrentPoint;
  88. Current_TD = 0;
  89. Index++;
  90. }
  91. PreviousPoint = CurrentPoint;
  92. }
  93. FinalPoints[50] = pts[pts.Length-2];
  94. FinalPoints = PathControlPointGenerator(FinalPoints);
  95. return FinalPoints;
  96. }
  97. private Vector3[] PathControlPointGenerator(Vector3[] path)
  98. {
  99. Vector3[] suppliedPath;
  100. Vector3[] vector3s;
  101. //create and store path points:
  102. suppliedPath = path;
  103. //populate calculate path;
  104. int offset = 2;
  105. vector3s = new Vector3[suppliedPath.Length+offset];
  106. System.Array.Copy(suppliedPath,0,vector3s,1,suppliedPath.Length);
  107. vector3s[0] = vector3s[1] + (vector3s[1] - vector3s[2]);
  108. vector3s[vector3s.Length-1] = vector3s[vector3s.Length-2] + (vector3s[vector3s.Length-2] - vector3s[vector3s.Length-3]);
  109. //is this a closed, continuous loop? yes? well then so let's make a continuous Catmull-Rom spline!
  110. if(vector3s[1] == vector3s[vector3s.Length-2]){
  111. Vector3[] tmpLoopSpline = new Vector3[vector3s.Length];
  112. System.Array.Copy(vector3s,tmpLoopSpline,vector3s.Length);
  113. tmpLoopSpline[0]=tmpLoopSpline[tmpLoopSpline.Length-3];
  114. tmpLoopSpline[tmpLoopSpline.Length-1]=tmpLoopSpline[2];
  115. vector3s=new Vector3[tmpLoopSpline.Length];
  116. System.Array.Copy(tmpLoopSpline,vector3s,tmpLoopSpline.Length);
  117. }
  118. return(vector3s);
  119. }
  120. //andeeee from the Unity forum's steller Catmull-Rom class ( http://forum.unity3d.com/viewtopic.php?p=218400#218400 ):
  121. private Vector3 Interp(Vector3[] pts , float t)
  122. {
  123. t = Mathf.Clamp(t,0.0f,2.0f);
  124. int numSections = pts.Length - 3;
  125. int currPt = Mathf.Min(Mathf.FloorToInt(t * numSections), numSections - 1);
  126. float u = t * numSections - currPt;
  127. Vector3 a = pts[currPt];
  128. Vector3 b = pts[currPt + 1];
  129. Vector3 c = pts[currPt + 2];
  130. Vector3 d = pts[currPt + 3];
  131. return .5f * (
  132. (-a + 3f * b - 3f * c + d) * (u * u * u)
  133. + (2f * a - 5f * b + 4f * c - d) * (u * u)
  134. + (-a + c) * u
  135. + 2f * b
  136. );
  137. }
  138. private float PathLength(Vector3[] pathPoints)
  139. {
  140. float pathLength = 0;
  141. Vector3[] vector3s = pathPoints;
  142. //Line Draw:
  143. Vector3 prevPt = Interp(vector3s,0);
  144. int SmoothAmount = pathPoints.Length*20 * 50;
  145. for (int i = 1; i <= SmoothAmount; i++)
  146. {
  147. //float pm = parseFloat(i) / SmoothAmount;
  148. float pm = i / SmoothAmount;
  149. Vector3 currPt = Interp(vector3s,pm);
  150. pathLength += Vector3.Distance(prevPt,currPt);
  151. prevPt = currPt;
  152. }
  153. return pathLength;
  154. }
  155. private Transform[] SortCPsbyName(Transform[] CPs, int startIndex, int endIndex)
  156. {
  157. ArrayList names = new ArrayList();
  158. Transform[] tempCPs = new Transform[endIndex-startIndex];
  159. int j = 0;
  160. int i = 0;
  161. for(i=startIndex;i<endIndex;i++)
  162. {
  163. tempCPs[j] = CPs[i];
  164. j++;
  165. }
  166. foreach (Transform go in tempCPs)
  167. names.Add(go.name);
  168. names.Sort();
  169. i=startIndex;
  170. foreach (String name in names)
  171. {
  172. foreach (Transform go in tempCPs)
  173. {
  174. if(go.name==name)
  175. {
  176. CPs[i] = go;
  177. break;
  178. }
  179. }
  180. i++;
  181. }
  182. return CPs;
  183. }
  184. }