ParticleForceField.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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. public abstract class ParticleForceField : MonoBehaviour
  20. {
  21. // =================================
  22. // Nested classes and structures.
  23. // =================================
  24. // ...
  25. protected struct GetForceParameters
  26. {
  27. public float distanceToForceFieldCenterSqr;
  28. public Vector3 scaledDirectionToForceFieldCenter;
  29. public Vector3 particlePosition;
  30. }
  31. // =================================
  32. // Variables.
  33. // =================================
  34. // ...
  35. [Header("Common Controls")]
  36. [Tooltip("Force field spherical range.")]
  37. public float radius = Mathf.Infinity;
  38. [Tooltip("Maximum baseline force.")]
  39. public float force = 5.0f;
  40. [Tooltip("Internal force field position offset.")]
  41. public Vector3 center = Vector3.zero;
  42. public float scaledRadius
  43. {
  44. get
  45. {
  46. return radius * transform.lossyScale.x;
  47. }
  48. }
  49. float _radius;
  50. float radiusSqr;
  51. float forceDeltaTime;
  52. Vector3 transformPosition;
  53. float[] particleSystemExternalForcesMultipliers;
  54. [Tooltip("Force scale as determined by distance to individual particles.")]
  55. public AnimationCurve forceOverDistance = new AnimationCurve(
  56. new Keyframe(0.0f, 1.0f),
  57. new Keyframe(1.0f, 1.0f)
  58. );
  59. // If (attached to a particle system): forces will be LOCAL.
  60. // Else if (attached to a particle system): forces will be SELECTIVE.
  61. // Else: forces will be GLOBAL.
  62. new ParticleSystem particleSystem;
  63. [Tooltip(
  64. "If nothing no particle systems are assigned, this force field will operate globally on ALL particle systems in the scene (NOT recommended).\n\n" +
  65. "If attached to a particle system, the force field will operate only on that system.\n\n" +
  66. "If specific particle systems are assigned, then the force field will operate on those systems only, even if attached to a particle system.")]
  67. public List<ParticleSystem> _particleSystems;
  68. int particleSystemsCount;
  69. // All required particle systems that will actually be used.
  70. List<ParticleSystem> particleSystems = new List<ParticleSystem>();
  71. // Particles in each system.
  72. // Second dimension initialized to max particle count
  73. // and not modified unless max particle count for that system changes.
  74. // Prevents allocations each frame.
  75. ParticleSystem.Particle[][] particleSystemParticles;
  76. ParticleSystem.MainModule[] particleSystemMainModules;
  77. Renderer[] particleSystemRenderers;
  78. // ^I could also just put the system and the module in a struct and make a 2D array
  79. // instead of having a seperate array for the modules.
  80. // Current iteration of the particle systems when looping through all of them.
  81. // Useful to derived classes (like for the vortex particle affector).
  82. protected ParticleSystem currentParticleSystem;
  83. // Parameters used by derived force classes.
  84. protected GetForceParameters parameters;
  85. // Update even when entire particle system is invisible?
  86. // Default: FALSE -> if all particles are invisible/offscreen, update will not execute.
  87. [Tooltip(
  88. "If TRUE, update even if target particle system(s) are invisible/offscreen.\n\n" +
  89. "If FALSE, update only if particles of the target system(s) are visible/onscreen.")]
  90. public bool alwaysUpdate = false;
  91. // =================================
  92. // Functions.
  93. // =================================
  94. // ...
  95. protected virtual void Awake()
  96. {
  97. }
  98. // ...
  99. protected virtual void Start()
  100. {
  101. particleSystem = GetComponent<ParticleSystem>();
  102. }
  103. // Called once per particle system, before entering second loop for its particles.
  104. // Used for setting up based on particle system-specific data.
  105. protected virtual void PerParticleSystemSetup()
  106. {
  107. }
  108. // Direction is NOT normalized.
  109. protected virtual Vector3 GetForce()
  110. {
  111. return Vector3.zero;
  112. }
  113. // ...
  114. protected virtual void Update()
  115. {
  116. }
  117. // Add/remove from public list of particles being managed.
  118. // Remember that adding specific systems will override all
  119. // other contexts (global and pure local).
  120. // Duplicates are not checked (intentionally).
  121. public void AddParticleSystem(ParticleSystem particleSystem)
  122. {
  123. _particleSystems.Add(particleSystem);
  124. }
  125. public void RemoveParticleSystem(ParticleSystem particleSystem)
  126. {
  127. _particleSystems.Remove(particleSystem);
  128. }
  129. // ...
  130. protected virtual void LateUpdate()
  131. {
  132. _radius = scaledRadius;
  133. radiusSqr = _radius * _radius;
  134. forceDeltaTime = force * Time.deltaTime;
  135. transformPosition = transform.position + center;
  136. // SELECTIVE.
  137. // If manually assigned a set of systems, use those no matter what.
  138. if (_particleSystems.Count != 0)
  139. {
  140. // If editor array size changed, clear and add again.
  141. if (particleSystems.Count != _particleSystems.Count)
  142. {
  143. particleSystems.Clear();
  144. particleSystems.AddRange(_particleSystems);
  145. }
  146. // Else if array size is the same, then re-assign from
  147. // the editor array. I do this in case the elements are different
  148. // even though the size is the same.
  149. else
  150. {
  151. for (int i = 0; i < _particleSystems.Count; i++)
  152. {
  153. particleSystems[i] = _particleSystems[i];
  154. }
  155. }
  156. }
  157. // LOCAL.
  158. // Else if attached to particle system, use only that.
  159. // Obviously, this will only happen if there are no systems specified in the array.
  160. else if (particleSystem)
  161. {
  162. // If just one element, assign as local PS component.
  163. if (particleSystems.Count == 1)
  164. {
  165. particleSystems[0] = particleSystem;
  166. }
  167. // Else, clear entire array and add only the one.
  168. else
  169. {
  170. particleSystems.Clear();
  171. particleSystems.Add(particleSystem);
  172. }
  173. }
  174. // GLOBAL.
  175. // Else, take all the ones from the entire scene.
  176. // This is the most expensive since it searches the entire scene
  177. // and also requires an allocation for every frame due to not knowing
  178. // if the particle systems are all the same from the last frame unless
  179. // I had a list to compare to from last frame. In that case, I'm not sure
  180. // if the performance would be better or worse. Do a test later?
  181. else
  182. {
  183. particleSystems.Clear();
  184. particleSystems.AddRange(FindObjectsOfType<ParticleSystem>());
  185. }
  186. parameters = new GetForceParameters();
  187. particleSystemsCount = particleSystems.Count;
  188. // If first frame (array is null) or length is less than the number of systems, initialize size of array.
  189. // I never shrink the array. Not sure if that's potentially super bad? I could always throw in a public
  190. // bool as an option to allow shrinking since there's a performance benefit for each, but depends on the
  191. // implementation case.
  192. if (particleSystemParticles == null || particleSystemParticles.Length < particleSystemsCount)
  193. {
  194. particleSystemParticles = new ParticleSystem.Particle[particleSystemsCount][];
  195. particleSystemMainModules = new ParticleSystem.MainModule[particleSystemsCount];
  196. particleSystemRenderers = new Renderer[particleSystemsCount];
  197. particleSystemExternalForcesMultipliers = new float[particleSystemsCount];
  198. for (int i = 0; i < particleSystemsCount; i++)
  199. {
  200. particleSystemMainModules[i] = particleSystems[i].main;
  201. particleSystemRenderers[i] = particleSystems[i].GetComponent<Renderer>();
  202. particleSystemExternalForcesMultipliers[i] = particleSystems[i].externalForces.multiplier;
  203. }
  204. }
  205. for (int i = 0; i < particleSystemsCount; i++)
  206. {
  207. if (!particleSystemRenderers[i].isVisible && !alwaysUpdate)
  208. {
  209. continue;
  210. }
  211. int maxParticles = particleSystemMainModules[i].maxParticles;
  212. if (particleSystemParticles[i] == null || particleSystemParticles[i].Length < maxParticles)
  213. {
  214. particleSystemParticles[i] = new ParticleSystem.Particle[maxParticles];
  215. }
  216. currentParticleSystem = particleSystems[i];
  217. PerParticleSystemSetup();
  218. int particleCount = currentParticleSystem.GetParticles(particleSystemParticles[i]);
  219. ParticleSystemSimulationSpace simulationSpace = particleSystemMainModules[i].simulationSpace;
  220. ParticleSystemScalingMode scalingMode = particleSystemMainModules[i].scalingMode;
  221. // I could also store the transforms in an array similar to what I do with modules.
  222. // Or, put all of those together into a struct and make an array out of that since
  223. // they'll always be assigned/updated at the same time.
  224. Transform currentParticleSystemTransform = currentParticleSystem.transform;
  225. Transform customSimulationSpaceTransform = particleSystemMainModules[i].customSimulationSpace;
  226. // If in world space, there's no need to do any of the extra calculations... simplify the loop!
  227. if (simulationSpace == ParticleSystemSimulationSpace.World)
  228. {
  229. for (int j = 0; j < particleCount; j++)
  230. {
  231. parameters.particlePosition = particleSystemParticles[i][j].position;
  232. parameters.scaledDirectionToForceFieldCenter.x = transformPosition.x - parameters.particlePosition.x;
  233. parameters.scaledDirectionToForceFieldCenter.y = transformPosition.y - parameters.particlePosition.y;
  234. parameters.scaledDirectionToForceFieldCenter.z = transformPosition.z - parameters.particlePosition.z;
  235. parameters.distanceToForceFieldCenterSqr = parameters.scaledDirectionToForceFieldCenter.sqrMagnitude;
  236. if (parameters.distanceToForceFieldCenterSqr < radiusSqr)
  237. {
  238. float distanceToCenterNormalized = parameters.distanceToForceFieldCenterSqr / radiusSqr;
  239. float distanceScale = forceOverDistance.Evaluate(distanceToCenterNormalized);
  240. Vector3 force = GetForce();
  241. float forceScale = (forceDeltaTime * distanceScale) * particleSystemExternalForcesMultipliers[i];
  242. force.x *= forceScale;
  243. force.y *= forceScale;
  244. force.z *= forceScale;
  245. Vector3 particleVelocity = particleSystemParticles[i][j].velocity;
  246. particleVelocity.x += force.x;
  247. particleVelocity.y += force.y;
  248. particleVelocity.z += force.z;
  249. particleSystemParticles[i][j].velocity = particleVelocity;
  250. }
  251. }
  252. }
  253. else
  254. {
  255. Vector3 particleSystemPosition = Vector3.zero;
  256. Quaternion particleSystemRotation = Quaternion.identity;
  257. Vector3 particleSystemLocalScale = Vector3.one;
  258. Transform simulationSpaceTransform = currentParticleSystemTransform;
  259. switch (simulationSpace)
  260. {
  261. case ParticleSystemSimulationSpace.Local:
  262. {
  263. particleSystemPosition = simulationSpaceTransform.position;
  264. particleSystemRotation = simulationSpaceTransform.rotation;
  265. particleSystemLocalScale = simulationSpaceTransform.localScale;
  266. break;
  267. }
  268. case ParticleSystemSimulationSpace.Custom:
  269. {
  270. simulationSpaceTransform = customSimulationSpaceTransform;
  271. particleSystemPosition = simulationSpaceTransform.position;
  272. particleSystemRotation = simulationSpaceTransform.rotation;
  273. particleSystemLocalScale = simulationSpaceTransform.localScale;
  274. break;
  275. }
  276. default:
  277. {
  278. throw new System.NotSupportedException(
  279. string.Format("Unsupported scaling mode '{0}'.", simulationSpace));
  280. }
  281. }
  282. for (int j = 0; j < particleCount; j++)
  283. {
  284. parameters.particlePosition = particleSystemParticles[i][j].position;
  285. switch (simulationSpace)
  286. {
  287. case ParticleSystemSimulationSpace.Local:
  288. case ParticleSystemSimulationSpace.Custom:
  289. {
  290. switch (scalingMode)
  291. {
  292. case ParticleSystemScalingMode.Hierarchy:
  293. {
  294. parameters.particlePosition = simulationSpaceTransform.TransformPoint(particleSystemParticles[i][j].position);
  295. break;
  296. }
  297. case ParticleSystemScalingMode.Local:
  298. {
  299. // Order is important.
  300. parameters.particlePosition = Vector3.Scale(parameters.particlePosition, particleSystemLocalScale);
  301. parameters.particlePosition = particleSystemRotation * parameters.particlePosition;
  302. parameters.particlePosition = parameters.particlePosition + particleSystemPosition;
  303. break;
  304. }
  305. case ParticleSystemScalingMode.Shape:
  306. {
  307. parameters.particlePosition = particleSystemRotation * parameters.particlePosition;
  308. parameters.particlePosition = parameters.particlePosition + particleSystemPosition;
  309. break;
  310. }
  311. default:
  312. {
  313. throw new System.NotSupportedException(
  314. string.Format("Unsupported scaling mode '{0}'.", scalingMode));
  315. }
  316. }
  317. break;
  318. }
  319. }
  320. parameters.scaledDirectionToForceFieldCenter.x = transformPosition.x - parameters.particlePosition.x;
  321. parameters.scaledDirectionToForceFieldCenter.y = transformPosition.y - parameters.particlePosition.y;
  322. parameters.scaledDirectionToForceFieldCenter.z = transformPosition.z - parameters.particlePosition.z;
  323. parameters.distanceToForceFieldCenterSqr = parameters.scaledDirectionToForceFieldCenter.sqrMagnitude;
  324. //particleSystemParticles[i][j].velocity += forceDeltaTime * Vector3.Normalize(parameters.scaledDirectionToForceFieldCenter);
  325. if (parameters.distanceToForceFieldCenterSqr < radiusSqr)
  326. {
  327. // 0.0f -> 0.99...f;
  328. float distanceToCenterNormalized = parameters.distanceToForceFieldCenterSqr / radiusSqr;
  329. // Evaluating a curve within a loop which is very likely to exceed a few thousand
  330. // iterations produces a noticeable FPS drop (around minus 2 - 5). Might be a worthwhile
  331. // optimization to check outside all loops if the curve is constant (all keyframes same value),
  332. // and then run a different block of code if true that uses that value as a stored float without
  333. // having to call Evaluate(t).
  334. float distanceScale = forceOverDistance.Evaluate(distanceToCenterNormalized);
  335. // Expanded vector operations for optimization. I think this is already done by
  336. // the compiler, but it's nice to have for the editor anyway.
  337. Vector3 force = GetForce();
  338. float forceScale = (forceDeltaTime * distanceScale) * particleSystemExternalForcesMultipliers[i];
  339. force.x *= forceScale;
  340. force.y *= forceScale;
  341. force.z *= forceScale;
  342. switch (simulationSpace)
  343. {
  344. case ParticleSystemSimulationSpace.Local:
  345. case ParticleSystemSimulationSpace.Custom:
  346. {
  347. switch (scalingMode)
  348. {
  349. case ParticleSystemScalingMode.Hierarchy:
  350. {
  351. force = simulationSpaceTransform.InverseTransformVector(force);
  352. break;
  353. }
  354. case ParticleSystemScalingMode.Local:
  355. {
  356. // Order is important.
  357. // Notice how rotation and scale orders are reversed.
  358. force = Quaternion.Inverse(particleSystemRotation) * force;
  359. force = Vector3.Scale(force, new Vector3(
  360. 1.0f / particleSystemLocalScale.x,
  361. 1.0f / particleSystemLocalScale.y,
  362. 1.0f / particleSystemLocalScale.z));
  363. break;
  364. }
  365. case ParticleSystemScalingMode.Shape:
  366. {
  367. force = Quaternion.Inverse(particleSystemRotation) * force;
  368. break;
  369. }
  370. // This would technically never execute since it's checked earlier (above).
  371. default:
  372. {
  373. throw new System.NotSupportedException(
  374. string.Format("Unsupported scaling mode '{0}'.", scalingMode));
  375. }
  376. }
  377. break;
  378. }
  379. }
  380. Vector3 particleVelocity = particleSystemParticles[i][j].velocity;
  381. particleVelocity.x += force.x;
  382. particleVelocity.y += force.y;
  383. particleVelocity.z += force.z;
  384. particleSystemParticles[i][j].velocity = particleVelocity;
  385. }
  386. }
  387. }
  388. currentParticleSystem.SetParticles(particleSystemParticles[i], particleCount);
  389. }
  390. }
  391. // ...
  392. void OnApplicationQuit()
  393. {
  394. }
  395. // ...
  396. protected virtual void OnDrawGizmosSelected()
  397. {
  398. Gizmos.color = Color.green;
  399. Gizmos.DrawWireSphere(transform.position + center, scaledRadius);
  400. }
  401. // =================================
  402. // End functions.
  403. // =================================
  404. }
  405. // =================================
  406. // End namespace.
  407. // =================================
  408. }
  409. }
  410. }
  411. // =================================
  412. // --END-- //
  413. // =================================