MegaShapeEllipse.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. 
  2. using UnityEngine;
  3. [AddComponentMenu("MegaShapes/Ellipse")]
  4. public class MegaShapeEllipse : MegaShape
  5. {
  6. public float length = 1.0f;
  7. public float width = 1.0f;
  8. const float CIRCLE_VECTOR_LENGTH = 0.5517861843f;
  9. public override string GetHelpURL() { return "?page_id=1178"; }
  10. void MakeCircle(float radius, float xmult, float ymult)
  11. {
  12. Matrix4x4 tm = GetMatrix();
  13. float vector = CIRCLE_VECTOR_LENGTH * radius;
  14. MegaSpline spline = NewSpline();
  15. Vector3 mult = new Vector3(xmult, ymult, 1.0f);
  16. for ( int ix = 0; ix < 4; ++ix )
  17. {
  18. float angle = 6.2831853f * (float)ix / 4.0f;
  19. float sinfac = Mathf.Sin(angle);
  20. float cosfac = Mathf.Cos(angle);
  21. Vector3 p = new Vector3(cosfac * radius, sinfac * radius, 0.0f);
  22. Vector3 rotvec = new Vector3(sinfac * vector, -cosfac * vector, 0.0f);
  23. spline.AddKnot(Vector3.Scale(p, mult), Vector3.Scale((p + rotvec), mult), Vector3.Scale((p - rotvec), mult), tm);
  24. }
  25. spline.closed = true;
  26. CalcLength(); //10);
  27. }
  28. public override void MakeShape()
  29. {
  30. length = Mathf.Clamp(length, 0.0f, float.MaxValue);
  31. width = Mathf.Clamp(width, 0.0f, float.MaxValue);
  32. float radius, xmult, ymult;
  33. if ( length < width )
  34. {
  35. radius = width;
  36. xmult = 1.0f;
  37. ymult = length / width;
  38. }
  39. else
  40. {
  41. if ( width < length )
  42. {
  43. radius = length;
  44. xmult = width / length;
  45. ymult = 1.0f;
  46. }
  47. else
  48. {
  49. radius = length;
  50. xmult = ymult = 1.0f;
  51. }
  52. }
  53. MakeCircle(radius / 2.0f, xmult, ymult);
  54. CalcLength(); //10);
  55. }
  56. }