Skip to content

Commit

Permalink
feat: support 3d scaling
Browse files Browse the repository at this point in the history
  • Loading branch information
mob-sakai committed Aug 19, 2020
1 parent a83e647 commit 42a84bc
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 2 deletions.
37 changes: 37 additions & 0 deletions Packages/UIParticle/Scripts/Editor/UIParticleEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ internal class UIParticleEditor : GraphicEditor
private static readonly GUIContent s_ContentParticleMaterial = new GUIContent("Particle Material", "The material for rendering particles");
private static readonly GUIContent s_ContentTrailMaterial = new GUIContent("Trail Material", "The material for rendering particle trails");
private static readonly GUIContent s_ContentAdvancedOptions = new GUIContent("Advanced Options");
private static readonly GUIContent s_Content3D = new GUIContent("3D");
private static readonly GUIContent s_ContentScale = new GUIContent("Scale");
private static readonly List<ParticleSystem> s_ParticleSystems = new List<ParticleSystem>();

private SerializedProperty _spParticleSystem;
Expand Down Expand Up @@ -97,6 +99,8 @@ public override void OnInspectorGUI()
EditorGUILayout.Space();
EditorGUILayout.LabelField(s_ContentAdvancedOptions, EditorStyles.boldLabel);

_xyzMode = DrawFloatOrVector3Field(_spScale3D, _xyzMode);

EditorGUILayout.PropertyField(_spIgnoreCanvasScaler);

// AnimatableProperties
Expand Down Expand Up @@ -139,5 +143,38 @@ public override void OnInspectorGUI()

serializedObject.ApplyModifiedProperties();
}

private static bool DrawFloatOrVector3Field(SerializedProperty sp, bool showXyz)
{
var x = sp.FindPropertyRelative("x");
var y = sp.FindPropertyRelative("y");
var z = sp.FindPropertyRelative("z");

showXyz |= !Mathf.Approximately(x.floatValue, y.floatValue) ||
!Mathf.Approximately(y.floatValue, z.floatValue) ||
y.hasMultipleDifferentValues ||
z.hasMultipleDifferentValues;

EditorGUILayout.BeginHorizontal();
if (showXyz)
{
EditorGUILayout.PropertyField(sp);
}
else
{
EditorGUI.BeginChangeCheck();
EditorGUILayout.PropertyField(x, s_ContentScale);
if (EditorGUI.EndChangeCheck())
z.floatValue = y.floatValue = x.floatValue;
}

EditorGUI.BeginChangeCheck();
showXyz = GUILayout.Toggle(showXyz, s_Content3D, EditorStyles.miniButton, GUILayout.Width(30));
if (EditorGUI.EndChangeCheck() && !showXyz)
z.floatValue = y.floatValue = x.floatValue;
EditorGUILayout.EndHorizontal();

return showXyz;
}
}
}
16 changes: 14 additions & 2 deletions Packages/UIParticle/Scripts/UIParticle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public class UIParticle : MaskableGraphic
[Tooltip("Animatable material properties. If you want to change the material properties of the ParticleSystem in Animation, enable it.")] [SerializeField]
internal AnimatableProperty[] m_AnimatableProperties = new AnimatableProperty[0];

[Tooltip("Particle effect scale")] [SerializeField]
internal Vector3 m_Scale3D = Vector3.one;

private readonly Material[] _maskMaterials = new Material[2];
private DrivenRectTransformTracker _tracker;
private Mesh _bakedMesh;
Expand Down Expand Up @@ -72,8 +75,17 @@ internal ParticleSystemRenderer cachedRenderer
/// </summary>
public float scale
{
get { return m_Scale; }
set { m_Scale = value; }
get { return m_Scale3D.x; }
set { m_Scale3D.Set(value, value, value); }
}

/// <summary>
/// Particle effect scale.
/// </summary>
public Vector3 scale3D
{
get { return m_Scale3D; }
set { m_Scale3D = value; }
}

internal bool isTrailParticle
Expand Down
28 changes: 28 additions & 0 deletions Packages/UIParticle/Scripts/UIParticleUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ private static void Refresh(UIParticle particle)
{
if (!particle) return;

Profiler.BeginSample("Modify scale");
ModifyScale(particle);
Profiler.EndSample();

if (!particle.isValid) return;

Profiler.BeginSample("Update trail particle");
Expand Down Expand Up @@ -85,6 +89,30 @@ private static void Refresh(UIParticle particle)
Profiler.EndSample();
}

private static void ModifyScale(UIParticle particle)
{
if (particle.isTrailParticle) return;

var modifiedScale = particle.m_Scale3D;

// Ignore Canvas scaling.
if (particle.ignoreCanvasScaler && particle.canvas)
{
var s = particle.canvas.rootCanvas.transform.localScale;
var sInv = new Vector3(
Mathf.Approximately(s.x, 0) ? 1 : 1 / s.x,
Mathf.Approximately(s.y, 0) ? 1 : 1 / s.y,
Mathf.Approximately(s.z, 0) ? 1 : 1 / s.z);
modifiedScale = Vector3.Scale(modifiedScale, sInv);
}

// Scale is already modified.
var tr = particle.transform;
if (Mathf.Approximately((tr.localScale - modifiedScale).sqrMagnitude, 0)) return;

tr.localScale = modifiedScale;
}

private static void UpdateMeshAndTexture(UIParticle particle)
{
// Update mesh.
Expand Down

0 comments on commit 42a84bc

Please sign in to comment.