MegaShapeRectangle.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. 
  2. using UnityEngine;
  3. [AddComponentMenu("MegaShapes/Rectangle")]
  4. public class MegaShapeRectangle : MegaShape
  5. {
  6. public float length = 1.0f;
  7. public float width = 1.0f;
  8. public float fillet = 0.0f;
  9. const float CIRCLE_VECTOR_LENGTH = 0.5517861843f;
  10. public override string GetHelpURL() { return "?page_id=1189"; }
  11. public override void MakeShape()
  12. {
  13. Matrix4x4 tm = GetMatrix();
  14. length = Mathf.Clamp(length, 0.0f, float.MaxValue);
  15. width = Mathf.Clamp(width, 0.0f, float.MaxValue);
  16. fillet = Mathf.Clamp(fillet, 0.0f, float.MaxValue);
  17. MegaSpline spline = NewSpline();
  18. float l2 = length / 2.0f;
  19. float w2 = width / 2.0f;
  20. Vector3 p = new Vector3(w2, l2, 0.0f);
  21. if ( fillet > 0.0f )
  22. {
  23. float cf = fillet * CIRCLE_VECTOR_LENGTH;
  24. Vector3 wvec = new Vector3(fillet, 0.0f, 0.0f);
  25. Vector3 lvec = new Vector3(0.0f, fillet, 0.0f);
  26. Vector3 cwvec = new Vector3(cf, 0.0f, 0.0f);
  27. Vector3 clvec = new Vector3(0.0f, cf, 0.0f);
  28. Vector3 p3 = p - lvec;
  29. spline.AddKnot(p3, p3 - clvec, p3 + clvec, tm);
  30. p = p - wvec;
  31. spline.AddKnot(p, p + cwvec, p - cwvec, tm);
  32. p = new Vector3(-w2, l2, 0.0f);
  33. Vector3 p2 = p + wvec;
  34. spline.AddKnot(p2, p2 + cwvec, p2 - cwvec, tm);
  35. p = p - lvec;
  36. spline.AddKnot(p, p + clvec, p - clvec, tm);
  37. p = new Vector3(-w2, -l2, 0.0f);
  38. p3 = p + lvec;
  39. spline.AddKnot(p3, p3 + clvec, p3 - clvec, tm);
  40. p = p + wvec;
  41. spline.AddKnot(p, p - cwvec, p + cwvec, tm);
  42. p = new Vector3(w2, -l2, 0.0f);
  43. p3 = p - wvec;
  44. spline.AddKnot(p3, p3 - cwvec, p3 + cwvec, tm);
  45. p = p + lvec;
  46. spline.AddKnot(p, p - clvec, p + clvec, tm);
  47. }
  48. else
  49. {
  50. spline.AddKnot(p, p, p, tm);
  51. p = new Vector3(-w2, l2, 0.0f);
  52. spline.AddKnot(p, p, p, tm);
  53. p = new Vector3(-w2, -l2, 0.0f);
  54. spline.AddKnot(p, p, p, tm);
  55. p = new Vector3(w2, -l2, 0.0f);
  56. spline.AddKnot(p, p, p, tm);
  57. }
  58. spline.closed = true;
  59. CalcLength(); //10);
  60. }
  61. }