Skip to content

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

@showrabbit

Description

@showrabbit

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;
    }
}

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions