MegaBox3.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. 
  2. using UnityEngine;
  3. [System.Serializable]
  4. public class MegaBox3
  5. {
  6. public Vector3 center;
  7. public Vector3 min;
  8. public Vector3 max;
  9. public float radius = 0.0f;
  10. public Vector3[] verts = new Vector3[8];
  11. public Vector3 Size()
  12. {
  13. return max - min;
  14. }
  15. public void SetSize(Vector3 size)
  16. {
  17. min = -(size * 0.5f);
  18. max = (size * 0.5f);
  19. center = Vector3.zero;
  20. radius = size.magnitude;
  21. CalcVerts();
  22. }
  23. public float Radius()
  24. {
  25. if ( radius <= 0.0f )
  26. radius = max.magnitude;
  27. return radius;
  28. }
  29. public override string ToString()
  30. {
  31. return "cen " + center + " min " + min + " max " + max;
  32. }
  33. Vector3 GetVert(int i)
  34. {
  35. Vector3 extents = Size() * 0.5f;
  36. switch ( i )
  37. {
  38. case 0: return center + extents;
  39. case 1: return center + Vector3.Scale(extents, new Vector3(-1, 1, 1));
  40. case 2: return center + Vector3.Scale(extents, new Vector3(1, 1, -1));
  41. case 3: return center + Vector3.Scale(extents, new Vector3(-1, 1, -1));
  42. case 4: return center + Vector3.Scale(extents, new Vector3(1, -1, 1));
  43. case 5: return center + Vector3.Scale(extents, new Vector3(-1, -1, 1));
  44. case 6: return center + Vector3.Scale(extents, new Vector3(1, -1, -1));
  45. default: return center + Vector3.Scale(extents, new Vector3(-1, -1, -1));
  46. }
  47. }
  48. void CalcVerts()
  49. {
  50. for ( int i = 0; i < 8; i++ )
  51. verts[i] = GetVert(i);
  52. }
  53. public Vector3 this[int index]
  54. {
  55. get
  56. {
  57. return verts[index];
  58. }
  59. set
  60. {
  61. verts[index] = value;
  62. }
  63. }
  64. static public Vector3 GetVert(Bounds bounds, int i)
  65. {
  66. switch ( i )
  67. {
  68. case 0: return bounds.center + bounds.extents;
  69. case 1: return bounds.center + Vector3.Scale(bounds.extents, new Vector3(-1, 1, 1));
  70. case 2: return bounds.center + Vector3.Scale(bounds.extents, new Vector3(1, 1, -1));
  71. case 3: return bounds.center + Vector3.Scale(bounds.extents, new Vector3(-1, 1, -1));
  72. case 4: return bounds.center + Vector3.Scale(bounds.extents, new Vector3(1, -1, 1));
  73. case 5: return bounds.center + Vector3.Scale(bounds.extents, new Vector3(-1, -1, 1));
  74. case 6: return bounds.center + Vector3.Scale(bounds.extents, new Vector3(1, -1, -1));
  75. default: return bounds.center + Vector3.Scale(bounds.extents, new Vector3(-1, -1, -1));
  76. }
  77. }
  78. }