Search Unity

Optimize ImageEffects

Discussion in 'Image Effects' started by Rusfighter, Aug 16, 2016.

  1. Rusfighter

    Rusfighter

    Joined:
    Jan 18, 2014
    Posts:
    60
    Hello guys,

    I am currently developing a mobile game and one of the aspects of my game is the radial blur image effect.

    I wonder if my optimalization is used correctly.

    The idea is to have a script on the camera that renders the camera view to a lower resolution RenderTexture and after the image effects are applied. However i am not sure if this working correctly since i do not know if the view is correctly rendered to RenderTexture and also to the camera buffer. Another point is if the temporary render texture should be refreshed every frame with GetTemporary().

    Here is my script:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. [RequireComponent(typeof(Camera))]
    4. public class ImageEffectOptimalization : MonoBehaviour {
    5.  
    6.     public enum Factor
    7.     {
    8.         Full = 1,
    9.         Half = 2,
    10.         Third = 3,
    11.         Fourth = 4
    12.     }
    13.  
    14.     public Factor m_Factor = Factor.Full;
    15.  
    16.     private RenderTexture m_tex;
    17.     private Camera m_camera;
    18.  
    19.     void Awake()
    20.     {
    21.         m_camera = GetComponent<Camera>();
    22.     }
    23.  
    24.     void OnPreRender()
    25.     {
    26.         if (m_Factor == Factor.Full)
    27.             return;
    28.         m_tex = RenderTexture.GetTemporary((int)(m_camera.pixelWidth / (float) m_Factor), (int)(m_camera.pixelHeight / (float)m_Factor));
    29.         m_camera.targetTexture = m_tex;
    30.     }
    31.  
    32.     void OnPostRender()
    33.     {
    34.         if (m_Factor == Factor.Full)
    35.             return;
    36.         m_camera.targetTexture = null;
    37.         RenderTexture.ReleaseTemporary(m_tex);
    38.     }
    39. }
    40.  
    If you have any other tips and techniques to optimize image effects for mobile I would appreciate if shared.

    Thanks,

    Ilija
     
  2. colin299

    colin299

    Joined:
    Sep 2, 2013
    Posts:
    181
    What is the use of the RenderTexture m_tex ?
    I think you at least need to render m_tex back to framebuffer
    Code (CSharp):
    1. void OnPostRender()
    2.     {
    3.         if (m_Factor == Factor.Full)
    4.             return;
    5.         m_camera.targetTexture = null;
    6.  
    7.           //I added this line
    8.         Graphics.Blit(m_tex,null as RenderTexture,imageEffectMaterial,imageEffectMaterialPassNum);
    9.  
    10.         RenderTexture.ReleaseTemporary(m_tex);
    11.     }