fix: fix the sorting algorithm
parent
f7eac0a34f
commit
7acbf22b4d
|
@ -190,32 +190,54 @@ namespace Coffee.UIParticleExtensions
|
||||||
self.Sort((a, b) =>
|
self.Sort((a, b) =>
|
||||||
{
|
{
|
||||||
var tr = transform;
|
var tr = transform;
|
||||||
var ra = a.GetComponent<ParticleSystemRenderer>();
|
var aRenderer = a.GetComponent<ParticleSystemRenderer>();
|
||||||
var rb = b.GetComponent<ParticleSystemRenderer>();
|
var bRenderer = b.GetComponent<ParticleSystemRenderer>();
|
||||||
|
|
||||||
if (!Mathf.Approximately(ra.sortingFudge, rb.sortingFudge))
|
// Render queue: ascending
|
||||||
return ra.sortingFudge < rb.sortingFudge ? 1 : -1;
|
var aMat = aRenderer.sharedMaterial;
|
||||||
|
var bMat = bRenderer.sharedMaterial;
|
||||||
|
if (aMat.renderQueue != bMat.renderQueue)
|
||||||
|
return aMat.renderQueue - bMat.renderQueue;
|
||||||
|
|
||||||
var pa = tr.InverseTransformPoint(a.transform.position).z;
|
// Sorting layer: ascending
|
||||||
var pb = tr.InverseTransformPoint(b.transform.position).z;
|
if (aRenderer.sortingLayerID != bRenderer.sortingLayerID)
|
||||||
|
return aRenderer.sortingLayerID - bRenderer.sortingLayerID;
|
||||||
|
|
||||||
if (!Mathf.Approximately(pa, pb))
|
// Sorting order: ascending
|
||||||
return pa < pb ? 1 : -1;
|
if (aRenderer.sortingOrder != bRenderer.sortingOrder)
|
||||||
|
return aRenderer.sortingOrder - bRenderer.sortingOrder;
|
||||||
|
|
||||||
var aQueue = ra.sharedMaterial.renderQueue;
|
// Z position & sortingFudge: descending
|
||||||
var bQueue = rb.sharedMaterial.renderQueue;
|
var aTransform = a.transform;
|
||||||
if (aQueue != bQueue)
|
var bTransform = b.transform;
|
||||||
return aQueue < bQueue ? 1 : -1;
|
var aPos = tr.InverseTransformPoint(aTransform.position).z+ aRenderer.sortingFudge;
|
||||||
|
var bPos = tr.InverseTransformPoint(bTransform.position).z+ bRenderer.sortingFudge;
|
||||||
|
if (!Mathf.Approximately(aPos, bPos))
|
||||||
|
return (int)Mathf.Sign(bPos - aPos);
|
||||||
|
|
||||||
var aHash = ra.sharedMaterial.GetHashCode();
|
// Material instance ID: match
|
||||||
var bHash = rb.sharedMaterial.GetHashCode();
|
if (aMat.GetInstanceID() == bMat.GetInstanceID())
|
||||||
if (aHash != bHash)
|
return 0;
|
||||||
return aHash < bHash ? 1 : -1;
|
|
||||||
|
|
||||||
return 0;
|
// Transform: ascending
|
||||||
|
return TransformCompare(aTransform, bTransform);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static int TransformCompare(Transform a, Transform b)
|
||||||
|
{
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
if (!a && !b) return 0;
|
||||||
|
if (!a) return -1;
|
||||||
|
if (!b) return 1;
|
||||||
|
if (a.parent == b.parent) return a.GetSiblingIndex() - b.GetSiblingIndex();
|
||||||
|
|
||||||
|
a = a.parent;
|
||||||
|
b = b.parent;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static long GetMaterialHash(this ParticleSystem self, bool trail)
|
public static long GetMaterialHash(this ParticleSystem self, bool trail)
|
||||||
{
|
{
|
||||||
if (!self) return 0;
|
if (!self) return 0;
|
||||||
|
|
Loading…
Reference in New Issue