Search Unity

Question Calling a Blit multiple times from the same class?

Discussion in 'Image Effects' started by Deleted User, Jan 16, 2023.

  1. Deleted User

    Deleted User

    Guest

    Hello forum, I've been following along with this under water shader image effect tutorial

    And I was wondering, what would happen if I separated the underwater logic instead to make a general class in which multiple screen effects could be calling upon and grabbing that same Blit. Is this considered something bad for performance, or useless to be doing? In the separate UnderwaterEffect script I simply activate and deactivate that effect. I'm thinking that I might come across a situation later in which multiple of screen effects are happening, and I thought it'd be better to grab it from a single class instead. But I'm not entirely sure this is good. What do you think about this?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections.Generic;
    3. [ExecuteInEditMode, ImageEffectAllowedInSceneView]
    4. public class BlitImageEffect : MonoBehaviour
    5. {
    6.     private List<Material> _mats = new List<Material>();
    7.     public Material AddBlitEffect
    8.     {
    9.         set
    10.         {
    11.             _mats.Add(value);
    12.         }
    13.     }
    14.     public Material RemoveBlitEffect
    15.     {
    16.         set
    17.         {
    18.             _mats.Remove(value);
    19.         }
    20.     }
    21.     private void OnRenderImage(RenderTexture source, RenderTexture destination)
    22.     {
    23.         if (_mats.Count <= 0) { Graphics.Blit(source, destination); return; }
    24.         foreach(Material mat in _mats)
    25.         {
    26.             if (mat==null) continue;
    27.             Graphics.Blit(source, destination, mat);
    28.         }
    29.     }
    30. }
    Code (CSharp):
    1. using UnityEngine;
    2. public class UnderwaterEffect : MonoBehaviour
    3. {
    4.     [SerializeField] private Material underwaterMaterial;
    5.     private BlitImageEffect blitController;
    6.     private void OnEnable()
    7.     {
    8.         GameObject cam = Game.Instance.GetCamera;
    9.         blitController = cam.GetComponent<BlitImageEffect>();
    10.         blitController.AddBlitEffect = underwaterMaterial;
    11.     }
    12.     private void OnDisable()
    13.     {
    14.         blitController.RemoveBlitEffect = underwaterMaterial;
    15.     }
    16. }