Skip to content

Commit

Permalink
Merge pull request #444 from solidsign/feature/UIParticleSystem_use_l…
Browse files Browse the repository at this point in the history
…engthScale

Add using lengthScale to UIParticleSystem
  • Loading branch information
SimonDarksideJ authored Jul 22, 2023
2 parents 0a42433 + 0dd1e6d commit 90fd224
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions Runtime/Scripts/Effects/UIParticleSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ public class UIParticleSystem : MaskableGraphic
[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;
Expand Down Expand Up @@ -91,7 +94,7 @@ protected bool Initialize()
pRenderer = pSystem.GetComponent<ParticleSystemRenderer>();
if (pRenderer != null)
pRenderer.enabled = false;

if (material == null)
{
var foundShader = ShaderLibrary.GetShaderInstance("UI Extensions/Particles/Additive");
Expand Down Expand Up @@ -183,8 +186,6 @@ protected override void OnPopulateMesh(VertexHelper vh)
#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;

Expand Down Expand Up @@ -280,13 +281,29 @@ protected override void OnPopulateMesh(VertexHelper vh)
_quad[3].color = color;
_quad[3].uv0 = temp;


float rotation = -particle.rotation * Mathf.Deg2Rad;
var lengthScale = pRenderer.lengthScale;
if (_useLengthScale)
{
// 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;
Expand Down Expand Up @@ -339,7 +356,7 @@ protected override void OnPopulateMesh(VertexHelper vh)
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;
Expand Down

0 comments on commit 90fd224

Please sign in to comment.