ParticleAffector.cs 25 KB

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