MegaFlowTest.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. 
  2. using UnityEngine;
  3. [ExecuteInEditMode]
  4. public class MegaFlowTest : MonoBehaviour
  5. {
  6. public float size = 1.0f;
  7. Vector3[] points = new Vector3[8];
  8. Vector3[] dirs = new Vector3[8];
  9. [Range(0.0f, 1.0f)]
  10. public float x = 0.0f;
  11. [Range(0.0f, 1.0f)]
  12. public float y = 0.0f;
  13. [Range(0.0f, 1.0f)]
  14. public float z = 0.0f;
  15. void Start()
  16. {
  17. MakeCube();
  18. }
  19. public void MakeCube()
  20. {
  21. Vector3 min = new Vector3(-size, -size, -size);
  22. Vector3 max = new Vector3(size, size, size);
  23. points[0] = new Vector3(min.x, min.y, min.z);
  24. points[1] = new Vector3(max.x, min.y, min.z);
  25. points[2] = new Vector3(max.x, max.y, min.z);
  26. points[3] = new Vector3(min.x, max.y, min.z);
  27. points[4] = new Vector3(min.x, min.y, max.z);
  28. points[5] = new Vector3(max.x, min.y, max.z);
  29. points[6] = new Vector3(max.x, max.y, max.z);
  30. points[7] = new Vector3(min.x, max.y, max.z);
  31. for ( int i = 0; i < 8; i++ )
  32. {
  33. dirs[i] = Random.onUnitSphere;
  34. }
  35. }
  36. void Update()
  37. {
  38. //MakeCube();
  39. }
  40. public Vector3 GetDir()
  41. {
  42. float xr = x;
  43. float yr = y;
  44. float zr = z;
  45. Vector3 V000 = dirs[0];
  46. Vector3 V100 = dirs[1];
  47. Vector3 V110 = dirs[2];
  48. Vector3 V010 = dirs[3];
  49. Vector3 V001 = dirs[4];
  50. Vector3 V101 = dirs[5];
  51. Vector3 V111 = dirs[6];
  52. Vector3 V011 = dirs[7];
  53. float omx = 1.0f - xr;
  54. float omy = 1.0f - yr;
  55. float omz = 1.0f - zr;
  56. Vector3 Vxyz;
  57. float c1 = omx * omy * omz;
  58. float c2 = xr * omy * omz;
  59. float c3 = omx * yr * omz;
  60. float c4 = omx * omy * zr;
  61. float c5 = xr * omy * zr;
  62. float c6 = omx * yr * zr;
  63. float c7 = xr * yr * omz;
  64. float c8 = xr * yr * zr;
  65. Vxyz.x = V000.x * c1 + V100.x * c2 + V010.x * c3 + V001.x * c4 + V101.x * c5 + V011.x * c6 + V110.x * c7 + V111.x * c8;
  66. Vxyz.y = V000.y * c1 + V100.y * c2 + V010.y * c3 + V001.y * c4 + V101.y * c5 + V011.y * c6 + V110.y * c7 + V111.y * c8;
  67. Vxyz.z = V000.z * c1 + V100.z * c2 + V010.z * c3 + V001.z * c4 + V101.z * c5 + V011.z * c6 + V110.z * c7 + V111.z * c8;
  68. return Vxyz;
  69. }
  70. public Vector3 GetPos()
  71. {
  72. float xr = x;
  73. float yr = y;
  74. float zr = z;
  75. Vector3 V000 = points[0];
  76. Vector3 V100 = points[1];
  77. Vector3 V110 = points[2];
  78. Vector3 V010 = points[3];
  79. Vector3 V001 = points[4];
  80. Vector3 V101 = points[5];
  81. Vector3 V111 = points[6];
  82. Vector3 V011 = points[7];
  83. float omx = 1.0f - xr;
  84. float omy = 1.0f - yr;
  85. float omz = 1.0f - zr;
  86. Vector3 Vxyz;
  87. float c1 = omx * omy * omz;
  88. float c2 = xr * omy * omz;
  89. float c3 = omx * yr * omz;
  90. float c4 = omx * omy * zr;
  91. float c5 = xr * omy * zr;
  92. float c6 = omx * yr * zr;
  93. float c7 = xr * yr * omz;
  94. float c8 = xr * yr * zr;
  95. Vxyz.x = V000.x * c1 + V100.x * c2 + V010.x * c3 + V001.x * c4 + V101.x * c5 + V011.x * c6 + V110.x * c7 + V111.x * c8;
  96. Vxyz.y = V000.y * c1 + V100.y * c2 + V010.y * c3 + V001.y * c4 + V101.y * c5 + V011.y * c6 + V110.y * c7 + V111.y * c8;
  97. Vxyz.z = V000.z * c1 + V100.z * c2 + V010.z * c3 + V001.z * c4 + V101.z * c5 + V011.z * c6 + V110.z * c7 + V111.z * c8;
  98. return Vxyz;
  99. }
  100. public void DrawGizmo()
  101. {
  102. Gizmos.matrix = transform.localToWorldMatrix;
  103. Gizmos.DrawLine(points[0], points[1]);
  104. Gizmos.DrawLine(points[1], points[2]);
  105. Gizmos.DrawLine(points[2], points[3]);
  106. Gizmos.DrawLine(points[3], points[0]);
  107. Gizmos.DrawLine(points[4], points[5]);
  108. Gizmos.DrawLine(points[5], points[6]);
  109. Gizmos.DrawLine(points[6], points[7]);
  110. Gizmos.DrawLine(points[7], points[4]);
  111. Gizmos.DrawLine(points[0], points[4]);
  112. Gizmos.DrawLine(points[1], points[5]);
  113. Gizmos.DrawLine(points[2], points[6]);
  114. Gizmos.DrawLine(points[3], points[7]);
  115. for ( int i = 0; i < 8; i++ )
  116. {
  117. Gizmos.DrawRay(points[i], dirs[i] * 4.0f);
  118. }
  119. Vector3 pos = GetPos();
  120. Vector3 dir = GetDir();
  121. Gizmos.DrawRay(pos, dir * 4.0f);
  122. Gizmos.matrix = Matrix4x4.identity;
  123. }
  124. }