SHARE
TWEET

Untitled

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