Skip to content

Commit

Permalink
feat: shrink rendering by material
Browse files Browse the repository at this point in the history
NOTE: Performance will be improved, but in some cases the rendering is not correct.

Close #113
  • Loading branch information
mob-sakai committed Nov 20, 2020
1 parent 2ec81da commit 46a7ddd
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
5 changes: 5 additions & 0 deletions Scripts/Editor/UIParticleEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ internal class UIParticleEditor : GraphicEditor
private SerializedProperty _spScale;
private SerializedProperty _spIgnoreCanvasScaler;
private SerializedProperty _spAnimatableProperties;
private SerializedProperty _spShrinkByMaterial;

private ReorderableList _ro;
private bool _xyzMode;
Expand Down Expand Up @@ -57,6 +58,7 @@ protected override void OnEnable()
_spScale = serializedObject.FindProperty("m_Scale3D");
_spIgnoreCanvasScaler = serializedObject.FindProperty("m_IgnoreCanvasScaler");
_spAnimatableProperties = serializedObject.FindProperty("m_AnimatableProperties");
_spShrinkByMaterial = serializedObject.FindProperty("m_ShrinkByMaterial");

var sp = serializedObject.FindProperty("m_Particles");
_ro = new ReorderableList(sp.serializedObject, sp, true, true, true, true);
Expand Down Expand Up @@ -164,6 +166,9 @@ public override void OnInspectorGUI()
t.SetMaterialDirty();
}

// ShrinkByMaterial
EditorGUILayout.PropertyField(_spShrinkByMaterial);

// Target ParticleSystems.
_ro.DoLayoutList();

Expand Down
16 changes: 15 additions & 1 deletion Scripts/UIParticle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public class UIParticle : MaskableGraphic
[Tooltip("Particles")] [SerializeField]
private List<ParticleSystem> m_Particles = new List<ParticleSystem>();

[Tooltip("Shrink rendering by material on refresh.\nNOTE: Performance will be improved, but in some cases the rendering is not correct.")] [SerializeField]
bool m_ShrinkByMaterial = false;

#if !SERIALIZE_FIELD_MASKABLE
[SerializeField] private bool m_Maskable = true;
#endif
Expand Down Expand Up @@ -81,6 +84,17 @@ public bool ignoreCanvasScaler
}
}

public bool shrinkByMaterial
{
get { return m_ShrinkByMaterial; }
set
{
if (m_ShrinkByMaterial == value) return;
m_ShrinkByMaterial = value;
RefreshParticles();
}
}

/// <summary>
/// Particle effect scale.
/// </summary>
Expand Down Expand Up @@ -217,7 +231,7 @@ public void RefreshParticles(GameObject root)
}

particles.Exec(p => p.GetComponent<ParticleSystemRenderer>().enabled = !enabled);
particles.SortForRendering(transform);
particles.SortForRendering(transform, m_ShrinkByMaterial);

SetMaterialDirty();
}
Expand Down
5 changes: 4 additions & 1 deletion Scripts/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public static CombineInstance[] Get(List<CombineInstanceEx> src, int count)

internal static class ParticleSystemExtensions
{
public static void SortForRendering(this List<ParticleSystem> self, Transform transform)
public static void SortForRendering(this List<ParticleSystem> self, Transform transform, bool sortByMaterial)
{
self.Sort((a, b) =>
{
Expand All @@ -200,6 +200,9 @@ public static void SortForRendering(this List<ParticleSystem> self, Transform tr
if (!aMat) return -1;
if (!bMat) return 1;
if (sortByMaterial)
return aMat.GetInstanceID() - bMat.GetInstanceID();
if (aMat.renderQueue != bMat.renderQueue)
return aMat.renderQueue - bMat.renderQueue;
Expand Down

0 comments on commit 46a7ddd

Please sign in to comment.