MegaShapeStar.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. 
  2. using UnityEngine;
  3. [AddComponentMenu("MegaShapes/Star")]
  4. public class MegaShapeStar : MegaShape
  5. {
  6. const float CIRCLE_VECTOR_LENGTH = 0.5517861843f;
  7. const int MIN_POINTS = 3;
  8. const int MAX_POINTS = 100;
  9. const float MIN_RADIUS = 0.0f;
  10. const float MAX_RADIUS = float.MaxValue;
  11. const float MIN_DIST = -180.0f;
  12. const float MAX_DIST = 180.0f;
  13. const int DEF_POINTS = 6;
  14. const float DEF_DIST = 0.0f;
  15. const float PI180 = 0.0174532f;
  16. public float radius1 = 2.0f;
  17. public float radius2 = 1.0f;
  18. public int points = DEF_POINTS;
  19. public float distortion = DEF_DIST;
  20. public float fillet1 = 0.0f;
  21. public float fillet2 = 0.0f;
  22. public override string GetHelpURL() { return "?page_id=396"; }
  23. public override void MakeShape()
  24. {
  25. Matrix4x4 tm = GetMatrix();
  26. Vector3 p = Vector3.zero; // The actual point
  27. float angle; // Angle of the current point
  28. radius1 = Mathf.Clamp(radius1, MIN_RADIUS, MAX_RADIUS);
  29. radius2 = Mathf.Clamp(radius2, MIN_RADIUS, MAX_RADIUS);
  30. distortion = Mathf.Clamp(distortion, MIN_DIST, MAX_DIST);
  31. points = Mathf.Clamp(points, MIN_POINTS, MAX_POINTS);
  32. fillet1 = Mathf.Clamp(fillet1, MIN_RADIUS, MAX_RADIUS);
  33. fillet2 = Mathf.Clamp(fillet2, MIN_RADIUS, MAX_RADIUS);
  34. // Delete the existing shape and create a new spline in it
  35. if ( splines.Count == 0 )
  36. {
  37. MegaSpline newspline = new MegaSpline();
  38. splines.Add(newspline);
  39. }
  40. MegaSpline spline = splines[0];
  41. spline.knots.Clear();
  42. float distort = PI180 * distortion;
  43. float PIpts = Mathf.PI / (float)points;
  44. // Now add all the necessary points
  45. for ( int ix = 0; ix < (2 * points); ++ix )
  46. {
  47. if ( (ix % 2) != 0 ) // Points for radius 1
  48. {
  49. angle = Mathf.PI * (float)ix / (float)points;
  50. p.x = Mathf.Cos(angle) * radius1;
  51. p.y = Mathf.Sin(angle) * radius1;
  52. p.z = 0.0f;
  53. if ( fillet1 > 0.0f )
  54. {
  55. float theta1 = angle - PIpts;
  56. float theta2 = angle + PIpts;
  57. float stheta1 = Mathf.Sin(theta1);
  58. float stheta2 = Mathf.Sin(theta2);
  59. float ctheta1 = Mathf.Cos(theta1);
  60. float ctheta2 = Mathf.Cos(theta2);
  61. Vector3 plast = new Vector3(radius2 * ctheta1, radius2 * stheta1, 0.0f);
  62. Vector3 pnext = new Vector3(radius2 * ctheta2, radius2 * stheta2, 0.0f);
  63. Vector3 n1 = Vector3.Normalize(plast - p) * fillet1;
  64. Vector3 n2 = Vector3.Normalize(pnext - p) * fillet1;
  65. Vector3 nk1 = n1 * CIRCLE_VECTOR_LENGTH;
  66. Vector3 nk2 = n2 * CIRCLE_VECTOR_LENGTH;
  67. Vector3 p1 = p + n1;
  68. Vector3 p2 = p + n2;
  69. spline.AddKnot(p1, p1 + nk1, p1 - nk1, tm);
  70. spline.AddKnot(p2, p2 - nk2, p2 + nk2, tm);
  71. }
  72. else
  73. spline.AddKnot(p, p, p, tm);
  74. }
  75. else // Points for radius 2 with optional angular offset
  76. {
  77. angle = PIpts * (float)ix + distort;
  78. p.x = Mathf.Cos(angle) * radius2;
  79. p.y = Mathf.Sin(angle) * radius2;
  80. p.z = 0.0f;
  81. if ( fillet2 > 0.0f )
  82. {
  83. float theta1 = angle - PIpts - distort;
  84. float theta2 = angle + PIpts + distort;
  85. float stheta1 = Mathf.Sin(theta1);
  86. float stheta2 = Mathf.Sin(theta2);
  87. float ctheta1 = Mathf.Cos(theta1);
  88. float ctheta2 = Mathf.Cos(theta2);
  89. Vector3 plast = new Vector3(radius1 * ctheta1, radius1 * stheta1, 0.0f);
  90. Vector3 pnext = new Vector3(radius1 * ctheta2, radius1 * stheta2, 0.0f);
  91. Vector3 n1 = Vector3.Normalize(plast - p) * fillet2;
  92. Vector3 n2 = Vector3.Normalize(pnext - p) * fillet2;
  93. Vector3 nk1 = n1 * CIRCLE_VECTOR_LENGTH;
  94. Vector3 nk2 = n2 * CIRCLE_VECTOR_LENGTH;
  95. Vector3 p1 = p + n1;
  96. Vector3 p2 = p + n2;
  97. spline.AddKnot(p1, p1 + nk1, p1 - nk1, tm);
  98. spline.AddKnot(p2, p2 - nk2, p2 + nk2, tm);
  99. }
  100. else
  101. spline.AddKnot(p, p, p, tm);
  102. }
  103. }
  104. spline.closed = true;
  105. CalcLength(); //10);
  106. //stepdist = spline.length / 80.0f;
  107. }
  108. }