mob-sakai / ParticleEffectForUGUI

Render particle effect in UnityUI(uGUI). Maskable, sortable, and no extra Camera/RenderTexture/Canvas.
MIT License
4.25k stars 619 forks source link

When using sharemesh mode, same effect will display errors at different scales #333

Open showrabbit opened 5 days ago

showrabbit commented 5 days ago

When using sharemesh mode, same effect will display errors at different scales.

I have made modifications to the UIParticleUpdater and UIParticle script, tested and fixed this issue. If deemed appropriate, I can consider submitting them to the repository. The function is modified as follows:

UIParticleUpdater.Refresh

private static void Refresh()
{
    // Do not allow it to be called in the same frame.
    if (s_FrameCount == Time.frameCount) return;
    s_FrameCount = Time.frameCount;
    s_PrimaryParentScales.Clear();
    // Simulate -> Primary
    for (var i = 0; i < s_ActiveParticles.Count; i++)
    {
        var uip = s_ActiveParticles[i];
        if (!uip || !uip.canvas || !uip.isPrimary || !s_UpdatedGroupIds.Add(uip.groupId)) continue;
        var parentLossyScale = uip.transform.parent.lossyScale;
        s_PrimaryParentScales.Add(uip.groupId, parentLossyScale);
        uip.UpdateTransformScale(Vector3.one);
        uip.UpdateRenderers();
    }

    // Simulate -> Others
    for (var i = 0; i < s_ActiveParticles.Count; i++)
    {
        var uip = s_ActiveParticles[i];
        if (!uip || !uip.canvas) continue;

        if (uip.useMeshSharing && s_PrimaryParentScales.TryGetValue(uip.groupId, out var primaryScale))
        {
            var myScale = uip.transform.parent.lossyScale;
            if ((myScale.x < 0.0001 && myScale.x > -0.0001f) ||
                (myScale.y < 0.0001 && myScale.y > -0.0001f) ||
                (myScale.z < 0.0001 && myScale.z > -0.0001f))
            {
                uip.UpdateTransformScale(Vector3.one);
            }
            else
            {
                var ratio = new Vector3(primaryScale.x / myScale.x, primaryScale.y / myScale.y, primaryScale.z / myScale.z);
                uip.UpdateTransformScale(ratio);
            }
        }
        else
        {
            uip.UpdateTransformScale(Vector3.one);
        }

        if (!uip.useMeshSharing)
        {
            uip.UpdateRenderers();
        }
        else if (s_UpdatedGroupIds.Add(uip.groupId))
        {
            s_UpdatedGroupIds.Add(uip.groupId);
            var parentLossyScale = uip.transform.parent.lossyScale;
            s_PrimaryParentScales.Add(uip.groupId, parentLossyScale);
            uip.UpdateRenderers();
        }
    }

    s_UpdatedGroupIds.Clear();

    // Attract
    for (var i = 0; i < s_ActiveAttractors.Count; i++)
    {
        s_ActiveAttractors[i].Attract();
    }
}

UIParticle.UpdateTransformScale

internal void UpdateTransformScale(Vector3 ratio)
{
    _tracker.Clear();
    canvasScale = canvas.rootCanvas.transform.localScale.Inverse();
    parentScale = transform.parent.lossyScale;
    if (autoScalingMode != AutoScalingMode.Transform)
    {
        if (_isScaleStored)
        {
            transform.localScale = _storedScale;
        }

        _isScaleStored = false;
        return;
    }

    var currentScale = transform.localScale;
    if (!_isScaleStored)
    {
        _storedScale = currentScale.IsVisible() ? currentScale : Vector3.one;
        _isScaleStored = true;
    }

    _tracker.Add(this, rectTransform, DrivenTransformProperties.Scale);
    var tempScale = parentScale;
    tempScale.x *= ratio.x;
    tempScale.y *= ratio.y;
    tempScale.z *= ratio.z;
    var newScale = tempScale.Inverse();
    if (currentScale != newScale)
    {
        transform.localScale = newScale;
    }
}
mob-sakai commented 3 days ago

Thank you for your reporting! (I have made some corrections to your comment)

Pull requests are always welcome.
Please check out the develop branch. 👍

mob-sakai commented 2 days ago

I have made some adjustments and committed changes based on your comments. But, this fix may cause changes in the rendering of existing shared UIParticles. Therefore, we should be cautious about merging this into the release stream.

If you're interested, please use the fix-333 branch for installation.

"com.coffee.ui-particle": "https://github.com/mob-sakai/ParticleEffectForUGUI.git?path=Packages/src#fix-333"