From 2a925fcf65c4925ded102acbe036f5d246d0522b Mon Sep 17 00:00:00 2001 From: "eldar.zakaryaev" Date: Wed, 5 Jul 2023 11:11:44 +0300 Subject: [PATCH] Add using lengthScale to UIParticleSystem --- Runtime/Scripts/Effects/UIParticleSystem.cs | 31 +++++++++++++++++---- 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/Runtime/Scripts/Effects/UIParticleSystem.cs b/Runtime/Scripts/Effects/UIParticleSystem.cs index 4083b02..2b24889 100644 --- a/Runtime/Scripts/Effects/UIParticleSystem.cs +++ b/Runtime/Scripts/Effects/UIParticleSystem.cs @@ -16,6 +16,9 @@ namespace UnityEngine.UI.Extensions [Tooltip("Enables 3d rotation for the particles")] public bool use3dRotation = false; + [Tooltip("Enables using Renderer.lengthScale parameter")] + public bool _useLengthScale = false; + private Transform _transform; private ParticleSystem pSystem; private ParticleSystem.Particle[] particles; @@ -26,6 +29,7 @@ namespace UnityEngine.UI.Extensions private Vector2 textureSheetAnimationFrameSize; private ParticleSystemRenderer pRenderer; private bool isInitialised = false; + private float _lengthScale; private Material currentMaterial; @@ -91,7 +95,8 @@ namespace UnityEngine.UI.Extensions pRenderer = pSystem.GetComponent(); if (pRenderer != null) pRenderer.enabled = false; - + + _lengthScale = pRenderer.lengthScale; if (material == null) { var foundShader = ShaderLibrary.GetShaderInstance("UI Extensions/Particles/Additive"); @@ -183,8 +188,6 @@ namespace UnityEngine.UI.Extensions #else Vector2 position = (pSystem.simulationSpace == ParticleSystemSimulationSpace.Local ? particle.position : _transform.InverseTransformPoint(particle.position)); #endif - float rotation = -particle.rotation * Mathf.Deg2Rad; - float rotation90 = rotation + Mathf.PI / 2; Color32 color = particle.GetCurrentColor(pSystem); float size = particle.GetCurrentSize(pSystem) * 0.5f; @@ -280,13 +283,29 @@ namespace UnityEngine.UI.Extensions _quad[3].color = color; _quad[3].uv0 = temp; + + float rotation = -particle.rotation * Mathf.Deg2Rad; + var lengthScale = _lengthScale; + if (_useLengthScale) // this flag is mostly to secure old behaviour, it probably can be replaced with _lengthScale != 1 + { + // rotate towards velocity + var normalizedVelocity = particle.velocity.normalized; + rotation = Mathf.Atan2(normalizedVelocity.y, normalizedVelocity.x); + } + else + { + lengthScale = 1f; + } + + float rotation90 = rotation + Mathf.PI / 2; + if (rotation == 0) { // no rotation corner1.x = position.x - size; - corner1.y = position.y - size; + corner1.y = position.y - size * lengthScale; corner2.x = position.x + size; - corner2.y = position.y + size; + corner2.y = position.y + size * lengthScale; temp.x = corner1.x; temp.y = corner1.y; @@ -339,7 +358,7 @@ namespace UnityEngine.UI.Extensions else { // apply rotation - Vector2 right = new Vector2(Mathf.Cos(rotation), Mathf.Sin(rotation)) * size; + Vector2 right = new Vector2(Mathf.Cos(rotation), Mathf.Sin(rotation)) * size * lengthScale; Vector2 up = new Vector2(Mathf.Cos(rotation90), Mathf.Sin(rotation90)) * size; _quad[0].position = position - right - up;