diff --git a/ModelMultiShurikenPersistFX.cs b/ModelMultiShurikenPersistFX.cs index 512c5f6..b5f9650 100644 --- a/ModelMultiShurikenPersistFX.cs +++ b/ModelMultiShurikenPersistFX.cs @@ -1,4 +1,4 @@ -/* +/* * Copyright (c) 2017, Sébastien GAGGINI AKA Sarbian, France * All rights reserved. * @@ -91,14 +91,14 @@ public class ModelMultiShurikenPersistFX : EffectBehaviour // TODO Sarbian : have the init auto fill this one [Persistent] public float randomInitalVelocityOffsetMaxRadius = 0.0f; - // Enables particle declustering - // This adds a vector to particle's position based on velocity, deltaTime, and which particle of the frame is it. - // ⁙ ⁙ ⁙ ⁙ ⁙ ⁙ ⁙ - // ^ false - // SPAWNED IN ONE FRAME - // vvvvv true - // ··································· - [Persistent] public bool decluster = false; + // Enables particle declustering + // This adds a vector to particle's position based on velocity, deltaTime, and which particle of the frame is it. + // ⁙ ⁙ ⁙ ⁙ ⁙ ⁙ ⁙ + // ^ false + // SPAWNED IN ONE FRAME + // vvvvv true + // ··································· + [Persistent] public bool decluster = false; // Emits particles on LateUpdate, rather than FixedUpdate, if enabled // @@ -181,7 +181,7 @@ public int MaxActiveParticles } } - // Previous way of counting particle count relied on the particled being emitted/updated synchronously with each other. + // Previous way of counting particle count relied on the particles being emitted/updated synchronously with each other. // With 'emitOnUpdate' changes, particles can be updated either in FixedUpdate, or LateUpdate. // Used to count all currently active particles, and to display current particle count of this effect in SmokeScreen UI. public int CurrentlyActiveParticles => persistentEmitters.Sum (x => x.pe.particleCount); @@ -372,13 +372,15 @@ public void FixedUpdate() // Debug.Log(vHit2.collider.name); //} - foreach (PersistentKSPShurikenEmitter emitter in persistentEmitters) { - // This is FixedUpdate, so don't emit here if particles should emit in LateUpdate - if (!emitOnUpdate) { - emitter.EmitterOnUpdate (hostPart.Rigidbody.velocity + Krakensbane.GetFrameVelocity ()); - } - } - + for (int i = 0; i < persistentEmitters.Count; i++) + { + PersistentKSPShurikenEmitter emitter = persistentEmitters[i]; + // This is FixedUpdate, so don't emit here if particles should emit in LateUpdate + if (!emitOnUpdate) + { + emitter.EmitterOnUpdate(hostPart.Rigidbody.velocity + Krakensbane.GetFrameVelocity()); + } + } } private void UpdateInputs(float power) @@ -496,7 +498,7 @@ public void UpdateEmitters(float power) pkpe.logarithmicGrow = logGrow.Value(inputs); pkpe.logarithmicGrowScale = logGrowScale.Value(inputs); - pkpe.decluster = decluster; + pkpe.decluster = decluster; pkpe.linearGrow = linGrow.Value(inputs); @@ -572,21 +574,25 @@ public void Update() // According to https://docs.unity3d.com/Manual/ExecutionOrder.html // LateUpdate is the last thing that happens before frame draw. That makes it as synced to frame draws, as possible // LateUpdate is called after physics calculations too, so the newly emitted plume particles are right where they should be. - public void LateUpdate () { - foreach (PersistentKSPShurikenEmitter emitter in persistentEmitters) { - if (emitter.go is null) { - continue; - } + public void LateUpdate () + { + for (int i = 0; i < persistentEmitters.Count; i++) + { + PersistentKSPShurikenEmitter emitter = persistentEmitters[i]; + if (emitter.go is null) + { + continue; + } - if (emitOnUpdate) { - emitter.EmitterOnUpdate (hostPart.Rigidbody.velocity + Krakensbane.GetFrameVelocity ()); - } - } + if (emitOnUpdate) + { + emitter.EmitterOnUpdate(hostPart.Rigidbody.velocity + Krakensbane.GetFrameVelocity()); + } + } // I think it's important to call this even though it doesn't count active particles // because it calculates how many particles should be removed on next emit pass. SmokeScreenConfig.UpdateParticlesCount (); - } public override void OnInitialize() diff --git a/PersistentKSPShurikenEmitter.cs b/PersistentKSPShurikenEmitter.cs index 9673576..3256014 100644 --- a/PersistentKSPShurikenEmitter.cs +++ b/PersistentKSPShurikenEmitter.cs @@ -1,4 +1,4 @@ -/* +/* * Copyright (c) 2017, Sébastien GAGGINI AKA Sarbian, France * All rights reserved. * @@ -210,14 +210,14 @@ public class PersistentKSPShurikenEmitter PersistentEmitterManager.Add(this); } - // Detach the emitter from its parent gameobject and stop its emmission in timer seconds + // Detach the emitter from its parent gameObject and stop its emission in timer seconds public void Detach(float timer) { //Print("Detach"); endTime = Time.fixedTime + timer; if (go != null && go.transform.parent != null) { - // detach from the parent so the emmitter(and its particle) don't get removed instantly + // detach from the parent so the emitter(and its particle) don't get removed instantly go.transform.parent = null; } } @@ -304,7 +304,7 @@ private void Emit (int ThisInUpdate, int TotalInUpdate) if (decluster) { // Apply some local velocity to prevent multiple particles spawned in one frame from clumping together - // Simulates as if some particles already were emitted between frames, and travelled some distance + // Simulates as if some particles already were emitted between frames, and traveled some distance pos += ( vel * // Initial velocity (Time.deltaTime) * TimeWarp.CurrentRate * // How much time has passed. At this point this value should be the total distance to the last particle emmited in the last update @@ -684,7 +684,7 @@ private Vector3 ParticleCollision(Vector3d pPos, Vector3d pVel, int mask) Vector3 unitTangent = (hit.normal.x == 0 && hit.normal.y == 0) ? new Vector3(1, 0, 0) : Vector3.ProjectOnPlane(new Vector3(0, 0, 1), hit.normal).normalized; - Vector3 hVel = Vector3.ProjectOnPlane(pVel, hit.normal); + Vector3 hVel = Vector3.ProjectOnPlane(pVel, hit.normal); Vector3 reflectedNormalVelocity = hVel - pVel; float residualFlow = reflectedNormalVelocity.magnitude * (1 - collideRatio);