fix: when using mesh sharing, the `Transform.scale` of the primary UIParticle affects the replicas.

close #333

NOTE: This fix may cause changes in the rendering of existing UIParticles.
fix-333
mob-sakai 2024-09-26 18:11:20 +09:00
parent 109caed657
commit 67c7e816a7
2 changed files with 25 additions and 10 deletions

View File

@ -586,7 +586,7 @@ namespace Coffee.UIExtensions
}
}
internal void UpdateTransformScale()
internal void UpdateTransformScale(Vector3 ratio)
{
_tracker.Clear();
canvasScale = canvas.rootCanvas.transform.localScale.Inverse();
@ -610,7 +610,7 @@ namespace Coffee.UIExtensions
}
_tracker.Add(this, rectTransform, DrivenTransformProperties.Scale);
var newScale = parentScale.Inverse();
var newScale = parentScale.GetScaled(ratio).Inverse();
if (currentScale != newScale)
{
transform.localScale = newScale;

View File

@ -1,4 +1,5 @@
using System.Collections.Generic;
using Coffee.UIParticleExtensions;
using UnityEditor;
using UnityEngine;
@ -8,7 +9,7 @@ namespace Coffee.UIExtensions
{
private static readonly List<UIParticle> s_ActiveParticles = new List<UIParticle>();
private static readonly List<UIParticleAttractor> s_ActiveAttractors = new List<UIParticleAttractor>();
private static readonly HashSet<int> s_UpdatedGroupIds = new HashSet<int>();
private static readonly Dictionary<int, Vector3> s_GroupScales = new Dictionary<int, Vector3>();
private static int s_FrameCount;
public static int uiParticleCount => s_ActiveParticles.Count;
@ -53,14 +54,17 @@ namespace Coffee.UIExtensions
// Do not allow it to be called in the same frame.
if (s_FrameCount == Time.frameCount) return;
s_FrameCount = Time.frameCount;
s_GroupScales.Clear();
// Simulate -> Primary
for (var i = 0; i < s_ActiveParticles.Count; i++)
{
// Process only primary UIParticles.
var uip = s_ActiveParticles[i];
if (!uip || !uip.canvas || !uip.isPrimary || !s_UpdatedGroupIds.Add(uip.groupId)) continue;
if (!uip || !uip.canvas || !uip.isPrimary || s_GroupScales.ContainsKey(uip.groupId)) continue;
uip.UpdateTransformScale();
s_GroupScales.Add(uip.groupId, uip.transform.parent.lossyScale);
uip.UpdateTransformScale(Vector3.one);
uip.UpdateRenderers();
}
@ -70,20 +74,31 @@ namespace Coffee.UIExtensions
var uip = s_ActiveParticles[i];
if (!uip || !uip.canvas) continue;
uip.UpdateTransformScale();
// Case 1: Not using mesh sharing.
if (!uip.useMeshSharing)
{
uip.UpdateTransformScale(Vector3.one);
uip.UpdateRenderers();
}
else if (s_UpdatedGroupIds.Add(uip.groupId))
// Case 2: Using mesh sharing as primary.
else if (!s_GroupScales.TryGetValue(uip.groupId, out var groupScale))
{
s_GroupScales.Add(uip.groupId, uip.transform.parent.lossyScale);
uip.UpdateTransformScale(Vector3.one);
uip.UpdateRenderers();
}
// Case 3: Using mesh sharing as replica. (Only scaling)
else
{
var parentScale = uip.transform.parent.lossyScale;
var ratio = parentScale.IsVisible()
? groupScale.GetScaled(parentScale.Inverse())
: Vector3.one;
ratio = Vector3.one;
uip.UpdateTransformScale(ratio);
}
}
s_UpdatedGroupIds.Clear();
// Attract
for (var i = 0; i < s_ActiveAttractors.Count; i++)
{