SquareDrawer.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using UnityEngine;
  2. namespace Popcron
  3. {
  4. public class SquareDrawer : Drawer
  5. {
  6. public override int Draw(ref Vector3[] buffer, params object[] values)
  7. {
  8. Vector2 position = default;
  9. if (values[0] is Vector2 p2)
  10. {
  11. position = p2;
  12. }
  13. else if (values[0] is Vector3 p3)
  14. {
  15. position = p3;
  16. }
  17. Quaternion rotation = (Quaternion)values[1];
  18. Vector2 size = default;
  19. if (values[2] is Vector2 s2)
  20. {
  21. size = s2;
  22. }
  23. else if (values[2] is Vector3 s3)
  24. {
  25. size = s3;
  26. }
  27. size *= 0.5f;
  28. Vector2 point1 = new Vector3(position.x - size.x, position.y - size.y);
  29. Vector2 point2 = new Vector3(position.x + size.x, position.y - size.y);
  30. Vector2 point3 = new Vector3(position.x + size.x, position.y + size.y);
  31. Vector2 point4 = new Vector3(position.x - size.x, position.y + size.y);
  32. point1 = rotation * (point1 - position);
  33. point1 += position;
  34. point2 = rotation * (point2 - position);
  35. point2 += position;
  36. point3 = rotation * (point3 - position);
  37. point3 += position;
  38. point4 = rotation * (point4 - position);
  39. point4 += position;
  40. //square
  41. buffer[0] = point1;
  42. buffer[1] = point2;
  43. buffer[2] = point2;
  44. buffer[3] = point3;
  45. buffer[4] = point3;
  46. buffer[5] = point4;
  47. //loop back to start
  48. buffer[6] = point4;
  49. buffer[7] = point1;
  50. return 8;
  51. }
  52. }
  53. }