MegaShapeHelix.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. 
  2. using UnityEngine;
  3. [AddComponentMenu("MegaShapes/Helix")]
  4. public class MegaShapeHelix : MegaShape
  5. {
  6. public float radius1 = 1.0f;
  7. public float radius2 = 0.75f;
  8. public float height = 0.0f;
  9. public float turns = 1.0f;
  10. public float bias = 0.0f;
  11. public float adjust = 0.4f;
  12. public bool clockwise = true;
  13. public int PointsPerTurn = 8;
  14. const float CIRCLE_VECTOR_LENGTH = 0.5517861843f;
  15. public override void MakeShape()
  16. {
  17. Matrix4x4 tm = GetMatrix();
  18. //lastout = 0.0f;
  19. //lastin = -9999.0f;
  20. PointsPerTurn = Mathf.Clamp(PointsPerTurn, 3, 100);
  21. // Delete all points in the existing spline
  22. MegaSpline spline = NewSpline();
  23. float fromrad = 0.0f; //from * Mathf.Deg2Rad;
  24. float torad = turns * Mathf.PI * 2.0f; //to * Mathf.Deg2Rad;
  25. // Order angles properly
  26. if ( fromrad > torad )
  27. torad += Mathf.PI * 2.0f;
  28. float totalRadians = Mathf.PI * 2.0f * turns;
  29. if ( clockwise )
  30. totalRadians *= -1.0f;
  31. int points = (int)(turns * (float)PointsPerTurn);
  32. if ( points == 0 )
  33. points = 1;
  34. float fPoints = (float)points;
  35. float totAngle = torad - fromrad;
  36. float vector1 = veccalc(totAngle / fPoints); // * radius1;
  37. float deltaRadius = radius2 - radius1;
  38. float power = 1.0f;
  39. if ( bias > 0.0f )
  40. power = bias * 9.0f + 1.0f;
  41. else
  42. {
  43. if ( bias < 0.0f )
  44. power = -bias * 9.0f + 1.0f;
  45. }
  46. // Now add all the necessary points
  47. //float angStep = totAngle / (float)POINTS_PER_TURN;
  48. for ( int ix = 0; ix <= points; ++ix )
  49. {
  50. float pct = (float)ix / fPoints;
  51. float r = radius1 + deltaRadius * pct;
  52. float hpct = pct;
  53. if ( bias > 0.0f )
  54. hpct = 1.0f - Mathf.Pow(1.0f - pct, power);
  55. else
  56. {
  57. if ( bias < 0.0f )
  58. hpct = Mathf.Pow(pct, power);
  59. }
  60. float angle = totalRadians * pct; //fromrad + (float)ix * angStep;
  61. float sinfac = Mathf.Sin(angle);
  62. float cosfac = Mathf.Cos(angle);
  63. float vector = vector1 * r;
  64. Vector3 p = new Vector3(cosfac * r, sinfac * r, height * hpct);
  65. Vector3 rotvec = new Vector3(sinfac * vector, -cosfac * vector, 0.0f);
  66. Vector3 invec = p + rotvec; //(ix == 0) ? p : p + rotvec;
  67. Vector3 outvec = p - rotvec; //(ix == points) ? p : p - rotvec;
  68. if ( !clockwise )
  69. spline.AddKnot(p, invec, outvec, tm);
  70. else
  71. spline.AddKnot(p, outvec, invec, tm);
  72. }
  73. CalcLength(); //10);
  74. //if ( reverse )
  75. //spline->Reverse(TRUE);
  76. }
  77. #if false
  78. public override void MakeShape1()
  79. {
  80. float totalRadians = Mathf.PI * 2.0f * turns;
  81. if ( clockwise )
  82. totalRadians *= -1.0f;
  83. float deltaRadius = radius2 - radius1;
  84. //float quarterTurns = turns * 4.0f;
  85. float power = 1.0f;
  86. if ( bias > 0.0f )
  87. power = bias * 9.0f + 1.0f;
  88. else
  89. {
  90. if ( bias < 0.0f )
  91. power = -bias * 9.0f + 1.0f;
  92. }
  93. MegaSpline spline = NewSpline();
  94. // Compute some helpful stuff...
  95. int points = (int)(turns * (float)PointsPerTurn);
  96. if ( points == 0 )
  97. points = 1;
  98. float fPoints = (float)points;
  99. float vector1 = veccalc(totalRadians / fPoints); //.5f;
  100. for ( int i = 0; i <= points; ++i )
  101. {
  102. float pct = (float)i / fPoints;
  103. float r = radius1 + deltaRadius * pct;
  104. float hpct = pct;
  105. if ( bias > 0.0f )
  106. hpct = 1.0f - Mathf.Pow(1.0f - pct, power);
  107. else
  108. {
  109. if ( bias < 0.0f )
  110. hpct = Mathf.Pow(pct, power);
  111. }
  112. float angle = totalRadians * pct;
  113. float cosfac = Mathf.Cos(angle);
  114. float sinfac = Mathf.Sin(angle);
  115. //float vector = CIRCLE_VECTOR_LENGTH * r;
  116. float vector = vector1 * r * adjust; //veccalc(totalRadians / fPoints) * r * 0.4f; //.5f;
  117. Vector3 p = new Vector3(cosfac * r, height * hpct, sinfac * r);
  118. Vector3 rotvec = new Vector3(sinfac * vector, 0.0f, -cosfac * vector);
  119. spline.AddKnot(p, p - rotvec, p + rotvec);
  120. }
  121. spline.closed = false;
  122. CalcLength(10);
  123. }
  124. #endif
  125. }