using UnityEngine.Rendering; using System.Collections.Generic; using System.Linq; namespace UnityEngine.Rendering.HighDefinition { /// /// Volume component class to inherit when you implement a custom post process /// public abstract class CustomPostProcessVolumeComponent : VolumeComponent { bool m_IsInitialized = false; internal string typeName; internal string passName { get; private set; } // Keep track of all the instances alive of the custom post process component so we can release them when needed internal static HashSet instances = new HashSet(); /// /// Injection point of the custom post process in HDRP. /// public virtual CustomPostProcessInjectionPoint injectionPoint => CustomPostProcessInjectionPoint.AfterPostProcess; /// /// True if you want your custom post process to be visible in the scene view.false False otherwise. /// public virtual bool visibleInSceneView => true; /// /// Setup function, called once before render is called. /// public virtual void Setup() { } /// /// Called every frame for each camera when the post process needs to be rendered. /// /// Command Buffer used to issue your commands /// Current Camera /// Source Render Target, it contains the camera color buffer in it's current state /// Destination Render Target public abstract void Render(CommandBuffer cmd, HDCamera camera, RTHandle source, RTHandle destination); /// /// Cleanup function, called when the render pipeline is disposed. /// public virtual void Cleanup() { } /// /// Unity calls this method when the object goes out of scope. /// protected override void OnDisable() { base.OnDisable(); CleanupInternal(); } internal void CleanupInternal() { if (m_IsInitialized) Cleanup(); m_IsInitialized = false; instances.Remove(this); } internal void SetupIfNeeded() { if (!m_IsInitialized) { Setup(); m_IsInitialized = true; typeName = GetType().Name; instances.Add(this); // By default custom post process volume name is empty, so we take the type name instead for debug markers. passName = string.IsNullOrEmpty(name) ? typeName : name; } } // If the HDRP asset is destroyed or changed, we reset the post process resources internal static void CleanupAllCustomPostProcesses() { foreach (var instance in instances.ToList()) // Copy to remove elements safely instance.CleanupInternal(); } } }