MegaShapeLine.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. 
  2. using UnityEngine;
  3. [AddComponentMenu("MegaShapes/Line")]
  4. public class MegaShapeLine : MegaShape
  5. {
  6. public int points = 2;
  7. public float length = 1.0f;
  8. public float dir = 0.0f;
  9. public Transform end;
  10. public override void MakeShape()
  11. {
  12. Matrix4x4 tm = GetMatrix();
  13. // Delete all points in the existing spline
  14. MegaSpline spline = NewSpline();
  15. //Vector3 origin = Vector3.zero;
  16. float len = length;
  17. Vector3 ep = Vector3.zero;
  18. if ( end )
  19. {
  20. ep = transform.worldToLocalMatrix.MultiplyPoint(end.position);
  21. len = ep.magnitude;
  22. }
  23. else
  24. {
  25. ep.x = Mathf.Sin(Mathf.Deg2Rad * dir) * len;
  26. ep.z = Mathf.Cos(Mathf.Deg2Rad * dir) * len;
  27. }
  28. Vector3 norm = ep.normalized;
  29. if ( points < 2 )
  30. points = 2;
  31. float vlen = (len / (float)(points + 0)) / 2.0f;
  32. for ( int ix = 0; ix < points; ++ix )
  33. {
  34. float alpha = (float)ix / (float)(points - 1);
  35. //float angle = fromrad + (float)ix * angStep;
  36. //float sinfac = Mathf.Sin(Mathf.Deg2Rad * dir);
  37. //float cosfac = Mathf.Cos(Mathf.Deg2Rad * dir);
  38. Vector3 p = Vector3.Lerp(Vector3.zero, ep, alpha);
  39. //Vector3 rotvec = new Vector3(sinfac * vlen, 0.0f, cosfac * vlen);
  40. Vector3 rotvec = new Vector3(norm.x * vlen, norm.y * vlen, norm.z * vlen);
  41. Vector3 invec = p - rotvec;
  42. Vector3 outvec = p + rotvec;
  43. spline.AddKnot(p, invec, outvec, tm);
  44. }
  45. CalcLength(); //10);
  46. }
  47. }