Skip to content

Commit

Permalink
feat: support 3D scaling
Browse files Browse the repository at this point in the history
close #105
  • Loading branch information
mob-sakai committed Oct 4, 2020
1 parent 2e69974 commit a508c3b
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 8 deletions.
44 changes: 42 additions & 2 deletions Packages/UIParticle/Scripts/Editor/UIParticleEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ internal class UIParticleEditor : GraphicEditor
private static readonly GUIContent s_ContentFix = new GUIContent("Fix");
private static readonly GUIContent s_ContentMaterial = new GUIContent("Material");
private static readonly GUIContent s_ContentTrailMaterial = new GUIContent("Trail Material");
private static readonly GUIContent s_Content3D = new GUIContent("3D");
private static readonly GUIContent s_ContentScale = new GUIContent("Scale");
private static readonly List<UIParticle> s_TempParents = new List<UIParticle>();
private static readonly List<UIParticle> s_TempChildren = new List<UIParticle>();

Expand All @@ -28,6 +30,7 @@ internal class UIParticleEditor : GraphicEditor
private SerializedProperty _spAnimatableProperties;

private ReorderableList _ro;
private bool _xyzMode;

private static readonly List<string> s_MaskablePropertyNames = new List<string>
{
Expand All @@ -49,7 +52,7 @@ internal class UIParticleEditor : GraphicEditor
protected override void OnEnable()
{
base.OnEnable();
_spScale = serializedObject.FindProperty("m_Scale");
_spScale = serializedObject.FindProperty("m_Scale3D");
_spIgnoreCanvasScaler = serializedObject.FindProperty("m_IgnoreCanvasScaler");
_spAnimatableProperties = serializedObject.FindProperty("m_AnimatableProperties");

Expand Down Expand Up @@ -138,7 +141,7 @@ public override void OnInspectorGUI()
}

// Scale
EditorGUILayout.PropertyField(_spScale);
_xyzMode = DrawFloatOrVector3Field(_spScale, _xyzMode);

// AnimatableProperties
var mats = current.particles
Expand Down Expand Up @@ -220,5 +223,42 @@ bool FixButton(bool show, string text)
}
}
}

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

x.floatValue = Mathf.Max(0.001f, x.floatValue);
y.floatValue = Mathf.Max(0.001f, y.floatValue);
z.floatValue = Mathf.Max(0.001f, z.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;
}
}
}
31 changes: 29 additions & 2 deletions Packages/UIParticle/Scripts/UIParticle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public class UIParticle : MaskableGraphic
[Tooltip("Particle effect scale")] [SerializeField]
float m_Scale = 100;

[Tooltip("Particle effect scale")] [SerializeField]
private Vector3 m_Scale3D;

[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];

Expand Down Expand Up @@ -74,8 +77,27 @@ public bool ignoreCanvasScaler
/// </summary>
public float scale
{
get { return m_Scale; }
set { m_Scale = Mathf.Max(0.001f, value); }
get { return m_Scale3D.x; }
set
{
m_Scale = Mathf.Max(0.001f, value);
m_Scale3D = new Vector3(m_Scale, m_Scale, m_Scale);
}
}

/// <summary>
/// Particle effect scale.
/// </summary>
public Vector3 scale3D
{
get { return m_Scale3D; }
set
{
if (m_Scale3D == value) return;
m_Scale3D.x = Mathf.Max(0.001f, value.x);
m_Scale3D.y = Mathf.Max(0.001f, value.y);
m_Scale3D.z = Mathf.Max(0.001f, value.z);
}
}

internal Mesh bakedMesh
Expand Down Expand Up @@ -418,6 +440,11 @@ void ISerializationCallbackReceiver.OnBeforeSerialize()

void ISerializationCallbackReceiver.OnAfterDeserialize()
{
if (m_Scale3D == Vector3.zero)
{
scale = m_Scale;
}

UnityEditor.EditorApplication.delayCall += () =>
{
if (Application.isPlaying || !this) return;
Expand Down
12 changes: 8 additions & 4 deletions Packages/UIParticle/Scripts/UIParticleUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,17 @@ private static void BakeMesh(UIParticle particle)
var rootMatrix = Matrix4x4.Rotate(root.rotation).inverse
* Matrix4x4.Scale(root.lossyScale).inverse;
var scale = particle.ignoreCanvasScaler
? particle.canvas.rootCanvas.transform.localScale.x * particle.scale
: particle.scale;
var scaleMatrix = Matrix4x4.Scale(scale * Vector3.one);
? Vector3.Scale( particle.canvas.rootCanvas.transform.localScale, particle.scale3D)
: particle.scale3D;
var scaleMatrix = Matrix4x4.Scale(scale);

// Cache position
var position = particle.transform.position;
var diff = (position - particle.cachedPosition) * (1 - 1 / scale);
var diff = position - particle.cachedPosition;
diff.x *= 1f - 1f / Mathf.Max(0.001f, scale.x);
diff.y *= 1f - 1f / Mathf.Max(0.001f, scale.y);
diff.z *= 1f - 1f / Mathf.Max(0.001f, scale.z);

particle.cachedPosition = position;

for (var i = 0; i < particle.particles.Count; i++)
Expand Down

0 comments on commit a508c3b

Please sign in to comment.