123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353 |
- using UnityEngine;
- #if UNITY_5_5 || UNITY_5_6 || UNITY_2017 || UNITY_2018 || UNITY_2019 || UNITY_2020
- #else
- using System.Collections.Generic;
- #if !UNITY_FLASH && !UNITY_PS3 && !UNITY_METRO && !UNITY_WP8
- using System.Threading;
- #endif
- [AddComponentMenu("MegaFlow/Particle Legacy Moving")]
- public class MegaFlowParticleLegacyMoving : MonoBehaviour
- {
- public MegaFlowMovingSource msource;
- MegaFlowFrame frame;
- public int framenum;
- [Range(0.01f, 2.0f)]
- public float mass = 0.02f;
- public float area = 0.2f;
- public float dt = 0.01f;
- public float scale = 1.0f;
- public int maxparticles = 1000;
- public ParticleEmitter particle;
- Particle[] particles;
- Matrix4x4 tm;
- Matrix4x4 invtm;
- float tdt;
- float coef;
- float oomass;
- int Cores = 0;
- float usedt = 0.0f;
- public float speed = 0.0f;
- public float gravity = 0.0f;
- [ContextMenu("Help")]
- public void Help()
- {
- Application.OpenURL("http://www.west-racing.com/mf/?page_id=5896");
- }
- public void SetFrame(int f)
- {
- if ( msource && msource.source )
- {
- if ( f >= 0 && f < msource.source.frames.Count )
- {
- frame = msource.source.frames[f];
- framenum = f;
- }
- }
- }
- // Flow source should be normalized
- void RunSim(int start, int end)
- {
- Vector3 tvel;
- Vector3 acc;
- Vector3 dir;
- float fvel = 0.0f;
- float falloff = 0.0f;
- int mframe = 0;
- Matrix4x4 ftm = invtm;
- Vector3 flowpos = Vector3.zero;
- for ( int i = start; i < end; i++ )
- {
- Vector3 pos = particles[i].position;
- Vector3 vel = particles[i].velocity;
- float duration = tdt;
- bool inbounds = true;
- // Not really correct to do here but it is close enough and will save a lot of cpu time
- //flowpos = msource.FindFlowPos(pos, ref inbounds, ref ftm, ref fvel);
- flowpos = msource.FindFlowPos(pos, ref inbounds, ref ftm, ref fvel, ref mframe, ref falloff);
- while ( duration > 0.0001f )
- {
- if ( inbounds )
- {
- Vector3 airvel = ftm.MultiplyVector(frame.GetGridVel(flowpos, ref inbounds));
- if ( inbounds )
- {
- float scl = scale * fvel * falloff;
- airvel.x *= scl;
- airvel.y *= scl;
- airvel.z *= scl;
- if ( airvel.sqrMagnitude > 0.1f )
- {
- tvel.x = airvel.x - vel.x;
- tvel.y = airvel.y - vel.y;
- tvel.z = airvel.z - vel.z;
- float l = (tvel.x * tvel.x) + (tvel.y * tvel.y) + (tvel.z * tvel.z);
- if ( l != 0.0f )
- {
- //Debug.Log("got one");
- l = Mathf.Sqrt(l);
- float df = coef * l;
- l = 1.0f / l;
- dir.x = tvel.x * l;
- dir.y = tvel.y * l;
- dir.z = tvel.z * l;
- float dfm = df * oomass;
- acc.x = dir.x * dfm;
- acc.y = dir.y * dfm + gravity;
- acc.z = dir.z * dfm;
- vel.x += acc.x * usedt;
- vel.y += acc.y * usedt;
- vel.z += acc.z * usedt;
- pos.x += vel.x * usedt;
- pos.y += vel.y * usedt;
- pos.z += vel.z * usedt;
- }
- }
- else
- {
- vel.y += gravity * usedt;
- pos.y += vel.y * usedt;
- }
- }
- else
- {
- vel.y += gravity * usedt;
- pos.y += vel.y * usedt;
- }
- }
- else
- {
- vel.y += gravity * usedt;
- pos.y += vel.y * usedt;
- }
- duration -= usedt;
- }
- particles[i].position = pos;
- particles[i].velocity = vel;
- }
- }
- void Start()
- {
- if ( Cores == 0 )
- Cores = SystemInfo.processorCount - 1;
- if ( particle == null )
- particle = GetComponent<ParticleEmitter>();
- particles = new Particle[maxparticles];
- }
- #if !UNITY_FLASH && !UNITY_PS3 && !UNITY_METRO && !UNITY_WP8
- public bool UseThreading = false;
- public MegaFlowTaskInfo[] tasks;
- void MakeThreads()
- {
- if ( Cores > 0 )
- {
- isRunning = true;
- tasks = new MegaFlowTaskInfo[Cores];
- for ( int i = 0; i < Cores; i++ )
- {
- tasks[i] = new MegaFlowTaskInfo();
- tasks[i].pauseevent = new AutoResetEvent(false);
- tasks[i]._thread = new Thread(DoWork);
- tasks[i]._thread.Start(tasks[i]);
- }
- }
- }
- void Update()
- {
- if ( msource && msource.source && particle )
- {
- msource.framenum = framenum;
- framenum = Mathf.Clamp(framenum, 0, msource.source.frames.Count - 1);
- frame = msource.source.frames[framenum];
- msource.source.Prepare();
- tdt = Time.deltaTime;
- Matrix4x4 offtm = Matrix4x4.TRS(frame.size * 0.5f, Quaternion.identity, Vector3.one);
- tm = offtm * msource.source.transform.worldToLocalMatrix;
- invtm = tm.inverse;
- float p = msource.source.Density;
- float A = area;
- float Re = msource.source.Reynolds;
- oomass = 1.0f / mass;
- coef = 1.0f * p * A * Mathf.Pow(Re, -0.5f);
- usedt = dt;
- if ( usedt > tdt )
- usedt = tdt;
- int count = particle.particleCount;
- particles = particle.particles;
- if ( !UseThreading || Cores < 1 || !Application.isPlaying )
- RunSim(0, count);
- else
- {
- if ( Cores == 0 )
- Cores = SystemInfo.processorCount - 1;
- if ( tasks == null )
- MakeThreads();
- int step = count / (Cores + 1);
- if ( Cores > 0 )
- {
- int index = step;
- for ( int i = 0; i < tasks.Length; i++ )
- {
- tasks[i].start = index;
- tasks[i].end = index + step;
- index += step;
- }
- tasks[Cores - 1].end = count;
- for ( int i = 0; i < tasks.Length; i++ )
- tasks[i].pauseevent.Set();
- }
- RunSim(0, step);
- WaitJobs();
- }
- particle.particles = particles;
- }
- }
- void WaitJobs()
- {
- if ( Cores > 0 )
- {
- int count = 0;
- bool wait = false;
- do
- {
- wait = false;
- for ( int i = 0; i < tasks.Length; i++ )
- {
- if ( tasks[i].end > 0 )
- {
- wait = true;
- break;
- }
- }
- if ( wait )
- {
- count++;
- Thread.Sleep(0);
- }
- } while ( wait );
- }
- }
- void OnApplicationQuit()
- {
- if ( Application.isPlaying )
- {
- isRunning = false;
- if ( tasks != null )
- {
- for ( int i = 0; i < tasks.Length; i++ )
- {
- tasks[i].pauseevent.Set();
- while ( tasks[i]._thread.IsAlive )
- {
- }
- }
- }
- tasks = null;
- }
- }
- static bool isRunning = true;
- void DoWork(object info)
- {
- MegaFlowTaskInfo inf = (MegaFlowTaskInfo)info;
- while ( isRunning )
- {
- inf.pauseevent.WaitOne(Timeout.Infinite, false);
- RunSim(inf.start, inf.end);
- inf.end = 0;
- }
- }
- #else
- void Update()
- {
- if ( msource && msource.source && particle )
- {
- msource.framenum = framenum;
- framenum = Mathf.Clamp(framenum, 0, msource.source.frames.Count - 1);
- frame = msource.source.frames[framenum];
- source.Prepare();
- tdt = Time.deltaTime;
- Matrix4x4 offtm = Matrix4x4.TRS(frame.size * 0.5f, Quaternion.identity, Vector3.one);
- tm = offtm * msource.source.transform.worldToLocalMatrix;
- invtm = tm.inverse;
- float p = msource.source.Density;
- float A = area;
- float Re = msource.source.Reynolds;
- oomass = 1.0f / mass;
- coef = 1.0f * p * A * Mathf.Pow(Re, -0.5f);
- usedt = dt;
- if ( usedt > tdt )
- usedt = tdt;
- int count = particle.particleCount;
- particles = particle.particles;
- RunSim(0, count);
- particle.particles = particles;
- }
- }
- #endif
- }
- #endif
|