ParticleFlocking.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. 
  2. // =================================
  3. // Namespaces.
  4. // =================================
  5. using UnityEngine;
  6. using System.Collections.Generic;
  7. // =================================
  8. // Define namespace.
  9. // =================================
  10. namespace MirzaBeig
  11. {
  12. namespace Scripting
  13. {
  14. namespace Effects
  15. {
  16. // =================================
  17. // Classes.
  18. // =================================
  19. [RequireComponent(typeof(ParticleSystem))]
  20. public class ParticleFlocking : MonoBehaviour
  21. {
  22. // =================================
  23. // Nested classes and structures.
  24. // =================================
  25. // ...
  26. public struct Voxel
  27. {
  28. public Bounds bounds;
  29. public int[] particles;
  30. public int particleCount;
  31. }
  32. // =================================
  33. // Variables.
  34. // =================================
  35. // ...
  36. [Header("N^2 Mode Settings")]
  37. public float maxDistance = 0.5f;
  38. [Header("Forces")]
  39. public float cohesion = 0.5f;
  40. public float separation = 0.25f;
  41. [Header("Voxel Mode Settings")]
  42. public bool useVoxels = true;
  43. public bool voxelLocalCenterFromBounds = true;
  44. public float voxelVolume = 8.0f;
  45. public int voxelsPerAxis = 5;
  46. int previousVoxelsPerAxisValue;
  47. Voxel[] voxels;
  48. new ParticleSystem particleSystem;
  49. ParticleSystem.Particle[] particles;
  50. Vector3[] particlePositions;
  51. ParticleSystem.MainModule particleSystemMainModule;
  52. [Header("General Performance Settings")]
  53. [Range(0.0f, 1.0f)]
  54. public float delay = 0.0f;
  55. float timer;
  56. public bool alwaysUpdate = false;
  57. bool visible;
  58. // =================================
  59. // Functions.
  60. // =================================
  61. // ...
  62. void Start()
  63. {
  64. particleSystem = GetComponent<ParticleSystem>();
  65. particleSystemMainModule = particleSystem.main;
  66. }
  67. // ...
  68. void OnBecameVisible()
  69. {
  70. visible = true;
  71. }
  72. void OnBecameInvisible()
  73. {
  74. visible = false;
  75. }
  76. // ...
  77. void buildVoxelGrid()
  78. {
  79. int voxelCount =
  80. voxelsPerAxis * voxelsPerAxis * voxelsPerAxis;
  81. voxels = new Voxel[voxelCount];
  82. float voxelSize = voxelVolume / voxelsPerAxis;
  83. float voxelSizeHalf = voxelSize / 2.0f;
  84. float voxelVolumeHalf = voxelVolume / 2.0f;
  85. Vector3 positionBase = transform.position;
  86. int i = 0;
  87. for (int x = 0; x < voxelsPerAxis; x++)
  88. {
  89. float posX = (-voxelVolumeHalf + voxelSizeHalf) + (x * voxelSize);
  90. for (int y = 0; y < voxelsPerAxis; y++)
  91. {
  92. float posY = (-voxelVolumeHalf + voxelSizeHalf) + (y * voxelSize);
  93. for (int z = 0; z < voxelsPerAxis; z++)
  94. {
  95. float posZ = (-voxelVolumeHalf + voxelSizeHalf) + (z * voxelSize);
  96. voxels[i].particleCount = 0;
  97. voxels[i].bounds = new Bounds(positionBase + new Vector3(posX, posY, posZ), Vector3.one * voxelSize);
  98. i++;
  99. }
  100. }
  101. }
  102. }
  103. // ...
  104. void LateUpdate()
  105. {
  106. if (alwaysUpdate || visible)
  107. {
  108. if (useVoxels)
  109. {
  110. int voxelCount =
  111. voxelsPerAxis * voxelsPerAxis * voxelsPerAxis;
  112. if (voxels == null || voxels.Length < voxelCount)
  113. {
  114. buildVoxelGrid();
  115. }
  116. }
  117. int maxParticles = particleSystemMainModule.maxParticles;
  118. if (particles == null || particles.Length < maxParticles)
  119. {
  120. particles = new ParticleSystem.Particle[maxParticles];
  121. particlePositions = new Vector3[maxParticles];
  122. if (useVoxels)
  123. {
  124. for (int i = 0; i < voxels.Length; i++)
  125. {
  126. voxels[i].particles = new int[maxParticles];
  127. }
  128. }
  129. }
  130. timer += Time.deltaTime;
  131. if (timer >= delay)
  132. {
  133. float deltaTime = timer;
  134. timer = 0.0f;
  135. particleSystem.GetParticles(particles);
  136. int particleCount = particleSystem.particleCount;
  137. float cohesionDeltaTime = cohesion * deltaTime;
  138. float separationDeltaTime = separation * deltaTime;
  139. for (int i = 0; i < particleCount; i++)
  140. {
  141. particlePositions[i] = particles[i].position;
  142. }
  143. if (useVoxels)
  144. {
  145. int voxelCount = voxels.Length;
  146. float voxelSize = voxelVolume / voxelsPerAxis;
  147. for (int i = 0; i < particleCount; i++)
  148. {
  149. for (int j = 0; j < voxelCount; j++)
  150. {
  151. if (voxels[j].bounds.Contains(particlePositions[i]))
  152. {
  153. voxels[j].particles[voxels[j].particleCount] = i;
  154. voxels[j].particleCount++;
  155. break;
  156. }
  157. }
  158. }
  159. for (int i = 0; i < voxelCount; i++)
  160. {
  161. if (voxels[i].particleCount > 1)
  162. {
  163. for (int j = 0; j < voxels[i].particleCount; j++)
  164. {
  165. Vector3 directionToLocalCenter;
  166. Vector3 localCenter = particlePositions[voxels[i].particles[j]];
  167. if (voxelLocalCenterFromBounds)
  168. {
  169. directionToLocalCenter = voxels[i].bounds.center - particlePositions[voxels[i].particles[j]];
  170. }
  171. else
  172. {
  173. for (int k = 0; k < voxels[i].particleCount; k++)
  174. {
  175. if (k == j)
  176. {
  177. continue;
  178. }
  179. localCenter += particlePositions[voxels[i].particles[k]];
  180. }
  181. localCenter /= voxels[i].particleCount;
  182. directionToLocalCenter = localCenter - particlePositions[voxels[i].particles[j]];
  183. }
  184. float distanceToLocalCenterSqr = directionToLocalCenter.sqrMagnitude;
  185. directionToLocalCenter.Normalize();
  186. Vector3 force = Vector3.zero;
  187. force += directionToLocalCenter * cohesionDeltaTime;
  188. force -= directionToLocalCenter * ((1.0f - (distanceToLocalCenterSqr / voxelSize)) * separationDeltaTime);
  189. Vector3 particleVelocity = particles[voxels[i].particles[j]].velocity;
  190. particleVelocity.x += force.x;
  191. particleVelocity.y += force.y;
  192. particleVelocity.z += force.z;
  193. particles[voxels[i].particles[j]].velocity = particleVelocity;
  194. }
  195. voxels[i].particleCount = 0;
  196. }
  197. }
  198. }
  199. else
  200. {
  201. float maxDistanceSqr = maxDistance * maxDistance;
  202. Vector3 p1p2_difference;
  203. for (int i = 0; i < particleCount; i++)
  204. {
  205. int localCount = 1;
  206. Vector3 localCenter = particlePositions[i];
  207. for (int j = 0; j < particleCount; j++)
  208. {
  209. if (j == i)
  210. {
  211. continue;
  212. }
  213. p1p2_difference.x = particlePositions[i].x - particlePositions[j].x;
  214. p1p2_difference.y = particlePositions[i].y - particlePositions[j].y;
  215. p1p2_difference.z = particlePositions[i].z - particlePositions[j].z;
  216. float distanceSqr = Vector3.SqrMagnitude(p1p2_difference);
  217. // TODO: Expand vector arithmetic for performance.
  218. if (distanceSqr <= maxDistanceSqr)
  219. {
  220. localCount++;
  221. localCenter += particlePositions[j];
  222. }
  223. }
  224. if (localCount != 1)
  225. {
  226. localCenter /= localCount;
  227. Vector3 directionToLocalCenter = localCenter - particlePositions[i];
  228. float distanceToLocalCenterSqr = directionToLocalCenter.sqrMagnitude;
  229. directionToLocalCenter.Normalize();
  230. Vector3 force = Vector3.zero;
  231. force += directionToLocalCenter * cohesionDeltaTime;
  232. force -= directionToLocalCenter * ((1.0f - (distanceToLocalCenterSqr / maxDistanceSqr)) * separationDeltaTime);
  233. Vector3 particleVelocity = particles[i].velocity;
  234. particleVelocity.x += force.x;
  235. particleVelocity.y += force.y;
  236. particleVelocity.z += force.z;
  237. particles[i].velocity = particleVelocity;
  238. }
  239. //Vector3 velocity = particles[i].velocity;
  240. //if (velocity != Vector3.zero)
  241. //{
  242. // particles[i].rotation3D = Quaternion.LookRotation(velocity, Vector3.up).eulerAngles;
  243. //}
  244. }
  245. }
  246. particleSystem.SetParticles(particles, particleCount);
  247. }
  248. }
  249. }
  250. // ...
  251. void OnDrawGizmosSelected()
  252. {
  253. //buildVoxelGrid();
  254. //for (int i = 0; i < voxels.Length; i++)
  255. //{
  256. // Gizmos.DrawWireCube(voxels[i].bounds.center, voxels[i].bounds.size);
  257. //}
  258. float size = voxelVolume / voxelsPerAxis;
  259. float sizeHalf = size / 2.0f;
  260. float totalSizeHalf = voxelVolume / 2.0f;
  261. Vector3 positionBase = transform.position;
  262. Gizmos.color = Color.red;
  263. Gizmos.DrawWireCube(positionBase, Vector3.one * voxelVolume);
  264. Gizmos.color = Color.white;
  265. for (int x = 0; x < voxelsPerAxis; x++)
  266. {
  267. float posX = (-totalSizeHalf + sizeHalf) + (x * size);
  268. for (int y = 0; y < voxelsPerAxis; y++)
  269. {
  270. float posY = (-totalSizeHalf + sizeHalf) + (y * size);
  271. for (int z = 0; z < voxelsPerAxis; z++)
  272. {
  273. float posZ = (-totalSizeHalf + sizeHalf) + (z * size);
  274. Gizmos.DrawWireCube(positionBase + new Vector3(posX, posY, posZ), Vector3.one * size);
  275. }
  276. }
  277. }
  278. }
  279. // =================================
  280. // End functions.
  281. // =================================
  282. }
  283. // =================================
  284. // End namespace.
  285. // =================================
  286. }
  287. }
  288. }
  289. // =================================
  290. // --END-- //
  291. // =================================