Skip to content

Commit

Permalink
feat: cache modified material
Browse files Browse the repository at this point in the history
Close #94
  • Loading branch information
mob-sakai committed Oct 27, 2020
1 parent d9f9244 commit 6b397f3
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 15 deletions.
74 changes: 74 additions & 0 deletions Scripts/ModifiedMaterial.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System.Collections.Generic;
using UnityEngine;

namespace Coffee.UIParticleExtensions
{
internal class ModifiedMaterial
{
private static readonly List<MatEntry> s_Entries = new List<MatEntry>();

public static Material Add(Material baseMat, Texture texture, int id)
{
MatEntry e;
for (var i = 0; i < s_Entries.Count; ++i)
{
e = s_Entries[i];
if (e.baseMat != baseMat || e.texture != texture || e.id != id) continue;
++e.count;
return e.customMat;
}

e = new MatEntry();
e.count = 1;
e.baseMat = baseMat;
e.texture = texture;
e.id = id;
e.customMat = new Material(baseMat);
e.customMat.hideFlags = HideFlags.HideAndDontSave;
if (texture)
e.customMat.mainTexture = texture;
s_Entries.Add(e);
// Debug.LogFormat(">>>> ModifiedMaterial.Add -> count = {0} {1} {2} {3}", s_Entries.Count, baseMat, texture, id);
return e.customMat;
}

public static void Remove(Material customMat)
{
if (!customMat) return;

for (var i = 0; i < s_Entries.Count; ++i)
{
var e = s_Entries[i];
if (e.customMat != customMat) continue;
if (--e.count == 0)
{
// Debug.LogFormat(">>>> ModifiedMaterial.Add -> count = {0} {1} {2} {3}", s_Entries.Count - 1, e.customMat, e.texture, e.id);
DestroyImmediate(e.customMat);
e.baseMat = null;
e.texture = null;
s_Entries.RemoveAt(i);
}

break;
}
}

private static void DestroyImmediate(Object obj)
{
if (!obj) return;
if (Application.isEditor)
Object.DestroyImmediate(obj);
else
Object.Destroy(obj);
}

private class MatEntry
{
public Material baseMat;
public Material customMat;
public int count;
public Texture texture;
public int id;
}
}
}
11 changes: 11 additions & 0 deletions Scripts/ModifiedMaterial.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 18 additions & 15 deletions Scripts/UIParticle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public class UIParticle : MaskableGraphic
private Vector3 _cachedPosition;
private static readonly List<Material> s_TempMaterials = new List<Material>(2);
private static MaterialPropertyBlock s_Mpb;
private static readonly List<Material> s_PrevMaskMaterials = new List<Material>();
private static readonly List<Material> s_PrevModifiedMaterials = new List<Material>();


/// <summary>
Expand Down Expand Up @@ -210,21 +212,11 @@ public void RefreshParticles(GameObject root)
protected override void UpdateMaterial()
{
// Clear mask materials.
for (var i = 0; i < _maskMaterials.Count; i++)
{
StencilMaterial.Remove(_maskMaterials[i]);
_maskMaterials[i] = null;
}

s_PrevMaskMaterials.AddRange(_maskMaterials);
_maskMaterials.Clear();

// Clear modified materials.
for (var i = 0; i < _modifiedMaterials.Count; i++)
{
DestroyImmediate(_modifiedMaterials[i]);
_modifiedMaterials[i] = null;
}

s_PrevModifiedMaterials.AddRange(_modifiedMaterials);
_modifiedMaterials.Clear();

// Recalculate stencil value.
Expand All @@ -240,6 +232,12 @@ protected override void UpdateMaterial()
{
_activeMeshIndices = 0;
canvasRenderer.Clear();

foreach (var m in s_PrevMaskMaterials)
StencilMaterial.Remove(m);

foreach (var m in s_PrevModifiedMaterials)
ModifiedMaterial.Remove(m);
return;
}

Expand Down Expand Up @@ -275,6 +273,12 @@ protected override void UpdateMaterial()
canvasRenderer.SetMaterial(mat, j++);
}
}

foreach (var m in s_PrevMaskMaterials)
StencilMaterial.Remove(m);

foreach (var m in s_PrevModifiedMaterials)
ModifiedMaterial.Remove(m);
}

private Material GetModifiedMaterial(Material baseMaterial, Texture2D texture)
Expand All @@ -287,10 +291,9 @@ private Material GetModifiedMaterial(Material baseMaterial, Texture2D texture)

if (texture == null && m_AnimatableProperties.Length == 0) return baseMaterial;

baseMaterial = new Material(baseMaterial);
var id = m_AnimatableProperties.Length == 0 ? 0 : GetInstanceID();
baseMaterial = ModifiedMaterial.Add(baseMaterial, texture, id);
_modifiedMaterials.Add(baseMaterial);
if (texture)
baseMaterial.mainTexture = texture;

return baseMaterial;
}
Expand Down

0 comments on commit 6b397f3

Please sign in to comment.