From a508c3bb86ad6694722868303385b20adc914134 Mon Sep 17 00:00:00 2001 From: mob-sakai Date: Fri, 2 Oct 2020 15:20:55 +0900 Subject: [PATCH] feat: support 3D scaling close #105 --- .../Scripts/Editor/UIParticleEditor.cs | 44 ++++++++++++++++++- Packages/UIParticle/Scripts/UIParticle.cs | 31 ++++++++++++- .../UIParticle/Scripts/UIParticleUpdater.cs | 12 +++-- 3 files changed, 79 insertions(+), 8 deletions(-) diff --git a/Packages/UIParticle/Scripts/Editor/UIParticleEditor.cs b/Packages/UIParticle/Scripts/Editor/UIParticleEditor.cs index 3db7b6f..3e19277 100644 --- a/Packages/UIParticle/Scripts/Editor/UIParticleEditor.cs +++ b/Packages/UIParticle/Scripts/Editor/UIParticleEditor.cs @@ -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 s_TempParents = new List(); private static readonly List s_TempChildren = new List(); @@ -28,6 +30,7 @@ internal class UIParticleEditor : GraphicEditor private SerializedProperty _spAnimatableProperties; private ReorderableList _ro; + private bool _xyzMode; private static readonly List s_MaskablePropertyNames = new List { @@ -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"); @@ -138,7 +141,7 @@ public override void OnInspectorGUI() } // Scale - EditorGUILayout.PropertyField(_spScale); + _xyzMode = DrawFloatOrVector3Field(_spScale, _xyzMode); // AnimatableProperties var mats = current.particles @@ -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; + } } } diff --git a/Packages/UIParticle/Scripts/UIParticle.cs b/Packages/UIParticle/Scripts/UIParticle.cs index 04db9ca..6a826d0 100755 --- a/Packages/UIParticle/Scripts/UIParticle.cs +++ b/Packages/UIParticle/Scripts/UIParticle.cs @@ -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]; @@ -74,8 +77,27 @@ public bool ignoreCanvasScaler /// 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); + } + } + + /// + /// Particle effect scale. + /// + 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 @@ -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; diff --git a/Packages/UIParticle/Scripts/UIParticleUpdater.cs b/Packages/UIParticle/Scripts/UIParticleUpdater.cs index 6eee462..3fc0391 100755 --- a/Packages/UIParticle/Scripts/UIParticleUpdater.cs +++ b/Packages/UIParticle/Scripts/UIParticleUpdater.cs @@ -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++)