Guest User

Untitled

a guest
Apr 22nd, 2019
208
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class Downsample : MonoBehaviour
  2. {
  3.     [ExecuteInEditMode]
  4.  
  5.     //The generated render texture
  6.     RenderTexture downsampleRenderTexture;
  7.  
  8.     //One or several cameras that capture the game scene
  9.     public Camera[] renderSourceCameras;
  10.  
  11.     //How much to downsample with
  12.     public float downsampleFactor = 0.5f;
  13.  
  14.     //For UI renders, override with a pre-generated render texture. This is not used for the main camera.
  15.     public RenderTexture customRenderTexture;
  16.  
  17.     //Read from playerPrefs to get specified downsample factor
  18.     PlayerPrefsManager playerPrefsManager;
  19. ;
  20.  
  21.     void Start()
  22.     {
  23.         //Force update if the setting changes at runtime
  24.         playerPrefsManager = PlayerPrefsManager.Instance;
  25.         playerPrefsManager.settingsChanged.AddListener(SettingChanged);
  26.  
  27.         //Take DPI into account, and downsample more for higher DPI. Ensure that the factor is never greater than 1 and never less than 0.1.
  28.         float dpiFactor = Mathf.Clamp(0.3f + (100 / (Screen.dpi + 4)), 0.1f, 1f);
  29.  
  30.         //Downsample by player pref * dpi compensation
  31.         downsampleFactor = playerPrefsManager.resolutions[playerPrefsManager.currentResolution] * dpiFactor;
  32.        
  33.         //Update render texture dimensions and settings
  34.         UpdateRenderTexture();
  35.     }
  36.  
  37.     //Only runs at start or when setting changes
  38.     void UpdateRenderTexture()
  39.     {
  40.         //Create a new render texture, either with screen dimensions (for main camera) or with custom dimensions for UI rendertextures
  41.         if (customRenderTexture == null)
  42.             downsampleRenderTexture = new RenderTexture((int)((float)Screen.width * downsampleFactor), (int)((float)Screen.height * downsampleFactor), 24, RenderTextureFormat.DefaultHDR);
  43.         else
  44.             downsampleRenderTexture = new RenderTexture((int)((float)customRenderTexture.width * downsampleFactor), (int)((float)customRenderTexture.height * downsampleFactor), 24, RenderTextureFormat.DefaultHDR);
  45.  
  46.         //Turn of AA and mipmapping, and use point filtering for pixelated look
  47.         downsampleRenderTexture.antiAliasing = 1;
  48.         downsampleRenderTexture.useMipMap = false;
  49.         downsampleRenderTexture.filterMode = FilterMode.Point;
  50.  
  51.     }
  52.  
  53.     //When setting changes, force a texture change
  54.     private void SettingChanged()
  55.     {
  56.         downsampleFactor = playerPrefsManager.resolutions[playerPrefsManager.currentResolution];
  57.         UpdateRenderTexture();
  58.     }
  59.  
  60.     //The actual render happens here. On each render, as long as the gameobject is active, set the target texture to the generated render texture, render the scene, and perform a blit
  61.     void OnRenderImage(RenderTexture source, RenderTexture destination)
  62.     {
  63.         if (gameObject.activeSelf)
  64.         {
  65.             for (int i = 0; i < renderSourceCameras.Length; i++)
  66.             {
  67.                 renderSourceCameras[i].targetTexture = downsampleRenderTexture;
  68.                 renderSourceCameras[i].Render();
  69.                 renderSourceCameras[i].targetTexture = null;
  70.             }
  71.  
  72.             Graphics.Blit(downsampleRenderTexture, destination);
  73.         }
  74.  
  75.     }
  76.  
  77.    //After each render, discard contents
  78.    void OnPostRender()
  79.     {
  80.        
  81.         if(downsampleRenderTexture != null)
  82.         {
  83.             downsampleRenderTexture.DiscardContents();
  84.         }
  85.        
  86.     }
  87. }
RAW Paste Data