MegaShapeNGon.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. 
  2. using UnityEngine;
  3. [AddComponentMenu("MegaShapes/NGon")]
  4. public class MegaShapeNGon : MegaShape
  5. {
  6. public float radius = 1.0f;
  7. public float fillet = 0.0f;
  8. public int sides = 6;
  9. public bool circular = false;
  10. public bool scribe = false;
  11. const float CIRCLE_VECTOR_LENGTH = 0.5517861843f;
  12. public override string GetHelpURL() { return "?page_id=500"; }
  13. public override void MakeShape()
  14. {
  15. Matrix4x4 tm = GetMatrix();
  16. radius = Mathf.Clamp(radius, 0, float.MaxValue);
  17. sides = Mathf.Clamp(sides, 3, 100);
  18. //LimitValue( circular, MIN_CIRCULAR, MAX_CIRCULAR );
  19. //fillet = Mathf.Clamp(fillet, 0.0f, float.MaxValue);
  20. //LimitValue( scribe, CIRCUMSCRIBED, INSCRIBED );
  21. float vector;
  22. float userad = radius;
  23. // If circumscribed, modify the radius
  24. if ( scribe ) //== CIRCUMSCRIBED )
  25. userad = radius / Mathf.Cos((Mathf.PI * 2.0f) / ((float)sides * 2.0f));
  26. MegaSpline spline = NewSpline();
  27. // Determine the vector length if circular
  28. if ( circular )
  29. vector = veccalc(6.2831853f / (float)sides) * userad;
  30. else
  31. vector = 0.0f;
  32. // Now add all the necessary points
  33. if ( (fillet == 0.0f) || circular )
  34. {
  35. for ( int ix = 0; ix < sides; ++ix )
  36. {
  37. float angle = 6.2831853f * (float)ix / (float)sides;
  38. float sinfac = Mathf.Sin(angle);
  39. float cosfac = Mathf.Cos(angle);
  40. Vector3 p = new Vector3(cosfac * userad, sinfac * userad, 0.0f);
  41. Vector3 rotvec = new Vector3(sinfac * vector, -cosfac * vector, 0.0f);
  42. spline.AddKnot(p, p + rotvec, p - rotvec, tm);
  43. }
  44. spline.closed = true;
  45. }
  46. else
  47. {
  48. for ( int ix = 0; ix < sides; ++ix )
  49. {
  50. float angle = 6.2831853f * (float)ix / (float)sides;
  51. float theta2 = (Mathf.PI * (sides - 2) / sides) / 2.0f;
  52. float fang = angle + theta2;
  53. float fang2 = angle - theta2;
  54. float f = fillet * Mathf.Tan((Mathf.PI * 0.5f) - theta2);
  55. float sinfac = Mathf.Sin(angle);
  56. float cosfac = Mathf.Cos(angle);
  57. Vector3 p = new Vector3(cosfac * userad, sinfac * userad, 0.0f); //,p1,p2;
  58. Vector3 fvec1 = new Vector3(-Mathf.Cos(fang), -Mathf.Sin(fang), 0.0f) * f;
  59. Vector3 fvec2 = new Vector3(-Mathf.Cos(fang2), -Mathf.Sin(fang2), 0.0f) * f;
  60. Vector3 p1 = p + fvec1;
  61. Vector3 p2 = p + fvec2;
  62. Vector3 p1vec = fvec1 * CIRCLE_VECTOR_LENGTH;
  63. Vector3 p2vec = fvec2 * CIRCLE_VECTOR_LENGTH;
  64. spline.AddKnot(p1, p1 + p1vec, p1 - p1vec, tm);
  65. spline.AddKnot(p2, p2 - p2vec, p2 + p2vec, tm);
  66. }
  67. spline.closed = true;
  68. //spline->ComputeBezPoints();
  69. }
  70. CalcLength(); //10);
  71. }
  72. }