Skip to content

Commit

Permalink
feat: reset previous position on start play for world space simulation
Browse files Browse the repository at this point in the history
close #303
  • Loading branch information
mob-sakai committed Jun 18, 2024
1 parent 1581ec5 commit 0130346
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions Packages/src/Runtime/UIParticleRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ internal class UIParticleRenderer : MaskableGraphic
private UIParticle _parent;
private ParticleSystem _particleSystem;
private float _prevCanvasScale;
private Vector3 _prevPsPos;
private Vector3 _prevScale;
private Vector3? _prevPsPos;
private Vector3? _prevScale;
private Vector2Int _prevScreenSize;
private bool _prewarm;
private ParticleSystemRenderer _renderer;
Expand Down Expand Up @@ -146,6 +146,8 @@ protected override void OnDisable()
ModifiedMaterial.Remove(_modifiedMaterial);
_modifiedMaterial = null;
_currentMaterialForRendering = null;
_prevScale = null;
_prevPsPos = null;
}

public static UIParticleRenderer AddRenderer(UIParticle parent, int index)
Expand Down Expand Up @@ -296,7 +298,7 @@ public void UpdateMesh(Camera bakeCamera)
#if UNITY_EDITOR
if (!Application.isPlaying)
{
SimulateForEditor(psPos - _prevPsPos, scale);
SimulateForEditor(psPos - _prevPsPos.GetValueOrDefault(), scale);
}
else
#endif
Expand Down Expand Up @@ -581,7 +583,7 @@ private void ResolveResolutionChange(Vector3 psPos, Vector3 scale)
var isWorldSpace = _particleSystem.IsWorldSpace();
var canvasScale = _parent.canvas ? _parent.canvas.scaleFactor : 1f;
var resolutionChanged = _prevScreenSize != screenSize || _prevCanvasScale != canvasScale;
if (resolutionChanged && isWorldSpace)
if (resolutionChanged && isWorldSpace && _prevPsPos.HasValue && _prevScale.HasValue)
{
// Update particle array size and get particles.
var size = _particleSystem.particleCount;
Expand All @@ -592,8 +594,8 @@ private void ResolveResolutionChange(Vector3 psPos, Vector3 scale)
// (psPos / scale) / (prevPsPos / prevScale) -> psPos * scale.inv * prevPsPos.inv * prevScale
var modifier = psPos.GetScaled(
scale.Inverse(),
_prevPsPos.Inverse(),
_prevScale);
_prevPsPos.Value.Inverse(),
_prevScale.Value);
for (var i = 0; i < size; i++)
{
var particle = particles[i];
Expand Down Expand Up @@ -640,12 +642,12 @@ private void Simulate(Vector3 scale, bool paused)
var rateOverDistance = emission.enabled
&& 0 < emission.rateOverDistance.constant
&& 0 < emission.rateOverDistanceMultiplier;
if (rateOverDistance && !paused)
if (rateOverDistance && !paused && _prevPsPos.HasValue && _prevScale.HasValue)
{
// (For rate-over-distance emission,) Move to previous scaled position, simulate (delta = 0).
var prevScaledPos = isLocalSpace
? _prevPsPos
: _prevPsPos.GetScaled(_prevScale.Inverse());
? _prevPsPos.Value
: _prevPsPos.Value.GetScaled(_prevScale.Value.Inverse());
psTransform.SetPositionAndRotation(prevScaledPos, originWorldRotation);
_particleSystem.Simulate(0, false, false, false);
}
Expand Down

0 comments on commit 0130346

Please sign in to comment.