MegaShapeLine.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. float len = length;
  16. Vector3 ep = Vector3.zero;
  17. if ( end )
  18. {
  19. ep = transform.worldToLocalMatrix.MultiplyPoint(end.position);
  20. len = ep.magnitude;
  21. }
  22. else
  23. {
  24. ep.x = Mathf.Sin(Mathf.Deg2Rad * dir) * len;
  25. ep.z = Mathf.Cos(Mathf.Deg2Rad * dir) * len;
  26. }
  27. Vector3 norm = ep.normalized;
  28. if ( points < 2 )
  29. points = 2;
  30. float vlen = (len / (float)(points + 0)) / 2.0f;
  31. for ( int ix = 0; ix < points; ++ix )
  32. {
  33. float alpha = (float)ix / (float)(points - 1);
  34. //float sinfac = Mathf.Sin(Mathf.Deg2Rad * dir);
  35. //float cosfac = Mathf.Cos(Mathf.Deg2Rad * dir);
  36. Vector3 p = Vector3.Lerp(Vector3.zero, ep, alpha);
  37. Vector3 rotvec = new Vector3(norm.x * vlen, norm.y * vlen, norm.z * vlen);
  38. Vector3 invec = p - rotvec;
  39. Vector3 outvec = p + rotvec;
  40. spline.AddKnot(p, invec, outvec, tm);
  41. }
  42. CalcLength(); //10);
  43. }
  44. }