123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
-
- // =================================
- // Namespaces.
- // =================================
- using UnityEngine;
- using System.Threading;
- // =================================
- // Define namespace.
- // =================================
- namespace MirzaBeig
- {
- namespace Scripting
- {
- namespace Effects
- {
- // =================================
- // Classes.
- // =================================
- //[ExecuteInEditMode]
- [System.Serializable]
- public class ParticleAffectorMT : MonoBehaviour
- {
- // =================================
- // Nested classes and structures.
- // =================================
- // ...
- // =================================
- // Variables.
- // =================================
- // ...
- public float force = 1.0f;
- public float speed = 1.0f;
- new ParticleSystem particleSystem;
- //Vector3 particleSystemPosition;
- ParticleSystem.Particle[] particles;
- float randomX;
- float randomY;
- float randomZ;
- float offsetX;
- float offsetY;
- float offsetZ;
- float deltaTime;
- Thread t;
- readonly object locker = new object();
- //bool running;
- bool processing;
- bool isDoneAssigning;
- // =================================
- // Functions.
- // =================================
- // ...
- void Awake()
- {
- }
- // ...
- void Start()
- {
- particleSystem = GetComponent<ParticleSystem>();
- randomX = Random.Range(-32.0f, 32.0f);
- randomY = Random.Range(-32.0f, 32.0f);
- randomZ = Random.Range(-32.0f, 32.0f);
- //running = true;
- t = new Thread(process);
- t.Start();
- isDoneAssigning = true;
- }
- // ...
- void LateUpdate()
- {
- lock (locker)
- {
- if (!processing && isDoneAssigning)
- {
- // Init.
- //print(1);
- particles = new ParticleSystem.Particle[particleSystem.particleCount];
- particleSystem.GetParticles(particles);
- float time = Time.time;
- deltaTime = Time.deltaTime;
- offsetX = (time * speed) * randomX;
- offsetY = (time * speed) * randomY;
- offsetZ = (time * speed) * randomZ;
- //particleSystemPosition = particleSystem.transform.position;
- processing = true;
- isDoneAssigning = false;
- // Pause else particles may be lost or created between
- // time thread started, and time it finished.
- // If not paused, then re-assigning particles from before
- // may cause jitter as they are reset to the last known
- // positions, while also replacing potentially new particles.
- //particleSystem.Pause();
- }
- }
- if (t.ThreadState == ThreadState.Stopped)
- {
- t = new Thread(process);
- t.Start();
- }
- //process();
- lock (locker)
- {
- if (!processing && !isDoneAssigning)
- {
- // Assign.
- //print(3);
- //print("-----");
- particleSystem.SetParticles(particles, particles.Length);
- isDoneAssigning = true;
- //particleSystem.Play();
- }
- }
- }
- // ...
- void process()
- {
- //while (running)
- //{
- lock (locker)
- {
- if (processing)
- {
- // Process.
- //print(2);
- for (int i = 0; i < particles.Length; i++)
- {
- ParticleSystem.Particle particle = particles[i];
- //Vector3 particleWorldPosition = particleSystemPosition + particle.position;
- Vector3 particlePosition = particle.position;
- Vector3 force = new Vector3(
- Noise.perlin(offsetX + particlePosition.x, offsetX + particlePosition.y, offsetX + particlePosition.z),
- Noise.perlin(offsetY + particlePosition.x, offsetY + particlePosition.y, offsetY + particlePosition.z),
- Noise.perlin(offsetZ + particlePosition.x, offsetZ + particlePosition.y, offsetZ + particlePosition.z)
- ) * this.force;
- force *= deltaTime;
- particle.velocity += force;
- particles[i] = particle;
- }
- processing = false;
- }
- //Thread.Sleep(10);
- }
- //Thread.Sleep(10);
- //}
- }
- // ...
- void OnDisable()
- {
- //running = false;
- }
- void OnApplicationQuit()
- {
- //running = false;
- }
- // =================================
- // End functions.
- // =================================
- }
- // =================================
- // End namespace.
- // =================================
- }
- }
- }
- // =================================
- // --END-- //
- // =================================
|