Skip to content

Commit

Permalink
fix: sub-emitter's inherit velocity module doubles at runtime
Browse files Browse the repository at this point in the history
close #249
  • Loading branch information
mob-sakai committed Jan 3, 2025
1 parent 90d966e commit 27551a4
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
6 changes: 4 additions & 2 deletions Packages/src/Runtime/UIParticle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -577,12 +577,14 @@ public void RefreshParticles(List<ParticleSystem> particleSystems)
{
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);
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions Packages/src/Runtime/UIParticleRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ internal class UIParticleRenderer : MaskableGraphic
private Vector2Int _prevScreenSize;
private bool _preWarm;
private ParticleSystemRenderer _renderer;
private ParticleSystem _mainEmitter;

public override Texture mainTexture => _isTrail ? null : _particleSystem.GetTextureForSprite();

Expand Down Expand Up @@ -112,6 +113,7 @@ public void Reset(int index = -1)
_parent = null;
_particleSystem = null;
_renderer = null;
_mainEmitter = null;
if (0 <= index)
{
_index = index;
Expand Down Expand Up @@ -223,7 +225,7 @@ public override Material GetModifiedMaterial(Material baseMaterial)
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;
Expand All @@ -246,10 +248,7 @@ public void Set(UIParticle parent, ParticleSystem ps, bool isTrail)

ps.TryGetComponent(out _renderer);
_renderer.enabled = false;

//_emitter = emitter;
_isTrail = isTrail;

_renderer.GetSharedMaterials(s_Materials);
material = s_Materials[isTrail ? 1 : 0];
s_Materials.Clear();
Expand All @@ -266,6 +265,7 @@ public void Set(UIParticle parent, ParticleSystem ps, bool isTrail)
_prevScreenSize = new Vector2Int(Screen.width, Screen.height);
_prevCanvasScale = canvas ? canvas.scaleFactor : 1f;
_delay = true;
_mainEmitter = mainEmitter;

canvasRenderer.SetTexture(null);

Expand Down Expand Up @@ -303,7 +303,7 @@ public void UpdateMesh(Camera bakeCamera)

// Simulate particles.
Profiler.BeginSample("[UIParticle] Bake Mesh > Simulate Particles");
if (!_isTrail && _parent.canSimulate)
if (!_isTrail && _parent.canSimulate && !_mainEmitter)
{
#if UNITY_EDITOR
if (!Application.isPlaying)
Expand Down
25 changes: 25 additions & 0 deletions Packages/src/Runtime/Utilities/ParticleSystemExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,5 +171,30 @@ public static void Exec(this List<ParticleSystem> self, Action<ParticleSystem> a
action.Invoke(p);
}
}

public static ParticleSystem GetMainEmitter(this ParticleSystem self, List<ParticleSystem> 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;
}
}
}

0 comments on commit 27551a4

Please sign in to comment.