using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Rendering; public class MaterialEntry { public string shader; public Material material; } public abstract class LensEffectBase : MonoBehaviour { public enum EStage { Before , Iterate , After , Finished , } public abstract ERenderingEvent renderingEvents { get; } protected static Dictionary> m_MaterailPool = new Dictionary>(); private List m_InUseMaterials = new List(); #region upvalues public BodyPartRenderer curBodypartRenderer; #endregion private static MaterialEntry _ClaimMaterial(string shader) { List mats; if (m_MaterailPool.TryGetValue(shader, out mats) && mats.Count > 0) { MaterialEntry me = mats[mats.Count - 1]; mats.RemoveAt(mats.Count - 1); return me; } Material mat = new Material(Shader.Find(shader)); MaterialEntry entry = new MaterialEntry(); entry.material = mat; entry.shader = shader; return entry; } private static void _ReleaseMaterial(MaterialEntry entry) { List mats; if (!m_MaterailPool.TryGetValue(entry.shader, out mats)) { mats = new List(); m_MaterailPool.Add(entry.shader, mats); } mats.Add(entry); } protected MaterialEntry ClaimMaterial(string shader) { MaterialEntry entry = _ClaimMaterial(shader); m_InUseMaterials.Add(entry); return entry; } protected void ReleaseMaterial(MaterialEntry entry) { m_InUseMaterials.Remove(entry); _ReleaseMaterial(entry); } protected void ReleaseAllInUsedMaterials() { for(int i = 0; i < m_InUseMaterials.Count; ++i) { _ReleaseMaterial(m_InUseMaterials[i]); } m_InUseMaterials.Clear(); } public virtual void BeforeDepthTexture(EStage stage, CommandBuffer cb) { } public virtual void AfterDepthTexture(EStage stage, CommandBuffer cb) { } public virtual void BeforeDepthNormalsTexture(EStage stage, CommandBuffer cb) { } public virtual void AfterDepthNormalsTexture(EStage stage, CommandBuffer cb) { } public virtual void BeforeForwardOpaque(EStage stage, CommandBuffer cb) { } public virtual void AfterForwardOpaque(EStage stage, CommandBuffer cb) { } public virtual void BeforeImageEffectsOpaque(EStage stage, CommandBuffer cb) { } public virtual void AfterImageEffectsOpaque(EStage stage, CommandBuffer cb) { } public virtual void BeforeSkybox(EStage stage, CommandBuffer cb) { } public virtual void AfterSkybox(EStage stage, CommandBuffer cb) { } public virtual void BeforeForwardAlpha(EStage stage, CommandBuffer cb) { } public virtual void AfterForwardAlpha(EStage stage, CommandBuffer cb) { } public virtual void BeforeImageEffects(EStage stage, CommandBuffer cb) { } public virtual void AfterImageEffects(EStage stage, CommandBuffer cb) { } public virtual void BeforeEverything(EStage stage, CommandBuffer cb) { } public virtual void AfterEverything(EStage stage, CommandBuffer cb) { } // 角色渲染完毕 public virtual void OnRenderFinish() { ReleaseAllInUsedMaterials(); } }