From 1c33dac1255dca4ee00214fcfd6e10cf79256c73 Mon Sep 17 00:00:00 2001 From: mob-sakai <12690315+mob-sakai@users.noreply.github.com> Date: Wed, 1 Jan 2025 10:05:46 +0900 Subject: [PATCH] fix: sub-emitter's `inherit velocity` module doubles at runtime close #349 --- Runtime/UIParticle.cs | 6 +++-- Runtime/UIParticleRenderer.cs | 10 ++++---- Runtime/Utilities/ParticleSystemExtensions.cs | 25 +++++++++++++++++++ 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/Runtime/UIParticle.cs b/Runtime/UIParticle.cs index 5ad390d..f0a2bed 100644 --- a/Runtime/UIParticle.cs +++ b/Runtime/UIParticle.cs @@ -577,12 +577,14 @@ namespace Coffee.UIExtensions { var ps = particleSystems[i]; if (!ps) continue; - GetRenderer(j++).Set(this, ps, false); + + var mainEmitter = ps.GetMainEmitter(particleSystems); + GetRenderer(j++).Set(this, ps, false, mainEmitter); // If the trail is enabled, set it additionally. if (ps.trails.enabled) { - GetRenderer(j++).Set(this, ps, true); + GetRenderer(j++).Set(this, ps, true, mainEmitter); } } } diff --git a/Runtime/UIParticleRenderer.cs b/Runtime/UIParticleRenderer.cs index 80936b8..e85d258 100644 --- a/Runtime/UIParticleRenderer.cs +++ b/Runtime/UIParticleRenderer.cs @@ -40,6 +40,7 @@ namespace Coffee.UIExtensions private Vector2Int _prevScreenSize; private bool _preWarm; private ParticleSystemRenderer _renderer; + private ParticleSystem _mainEmitter; public override Texture mainTexture => _isTrail ? null : _particleSystem.GetTextureForSprite(); @@ -112,6 +113,7 @@ namespace Coffee.UIExtensions _parent = null; _particleSystem = null; _renderer = null; + _mainEmitter = null; if (0 <= index) { _index = index; @@ -223,7 +225,7 @@ namespace Coffee.UIExtensions return _modifiedMaterial; } - public void Set(UIParticle parent, ParticleSystem ps, bool isTrail) + public void Set(UIParticle parent, ParticleSystem ps, bool isTrail, ParticleSystem mainEmitter) { _parent = parent; maskable = parent.maskable; @@ -246,10 +248,7 @@ namespace Coffee.UIExtensions ps.TryGetComponent(out _renderer); _renderer.enabled = false; - - //_emitter = emitter; _isTrail = isTrail; - _renderer.GetSharedMaterials(s_Materials); material = s_Materials[isTrail ? 1 : 0]; s_Materials.Clear(); @@ -266,6 +265,7 @@ namespace Coffee.UIExtensions _prevScreenSize = new Vector2Int(Screen.width, Screen.height); _prevCanvasScale = canvas ? canvas.scaleFactor : 1f; _delay = true; + _mainEmitter = mainEmitter; canvasRenderer.SetTexture(null); @@ -303,7 +303,7 @@ namespace Coffee.UIExtensions // Simulate particles. Profiler.BeginSample("[UIParticle] Bake Mesh > Simulate Particles"); - if (!_isTrail && _parent.canSimulate) + if (!_isTrail && _parent.canSimulate && !_mainEmitter) { #if UNITY_EDITOR if (!Application.isPlaying) diff --git a/Runtime/Utilities/ParticleSystemExtensions.cs b/Runtime/Utilities/ParticleSystemExtensions.cs index 48329f0..b9c08a0 100644 --- a/Runtime/Utilities/ParticleSystemExtensions.cs +++ b/Runtime/Utilities/ParticleSystemExtensions.cs @@ -171,5 +171,30 @@ namespace Coffee.UIParticleInternal action.Invoke(p); } } + + public static ParticleSystem GetMainEmitter(this ParticleSystem self, List list) + { + if (!self || list == null || list.Count == 0) return null; + + for (var i = 0; i < list.Count; i++) + { + var parent = list[i]; + if (parent != self && IsSubEmitterOf(self, parent)) return parent; + } + + return null; + } + + public static bool IsSubEmitterOf(this ParticleSystem self, ParticleSystem parent) + { + var subEmitters = parent.subEmitters; + var count = subEmitters.subEmittersCount; + for (var i = 0; i < count; i++) + { + if (subEmitters.GetSubEmitterSystem(i) == self) return true; + } + + return false; + } } }