Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using UnityStandardAssets.ImageEffects;
- public class Downsample : MonoBehaviour
- {
- [ExecuteInEditMode]
- //The generated render texture
- RenderTexture downsampleRenderTexture;
- //One or several cameras that capture the game scene
- public Camera[] renderSourceCameras;
- //How much to downsample with
- public float downsampleFactor = 0.5f;
- //For UI renders, override with a pre-generated render texture. This is not used for the main camera.
- public RenderTexture customRenderTexture;
- //Read from playerPrefs to get specified downsample factor
- PlayerPrefsManager playerPrefsManager;
- void Start()
- {
- //Force update if the setting changes at runtime
- playerPrefsManager = PlayerPrefsManager.Instance;
- playerPrefsManager.settingsChanged.AddListener(SettingChanged);
- //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.
- float dpiFactor = Mathf.Clamp((100 / (Screen.dpi + 4)), 0.1f, 1f);
- //Downsample by player pref * dpi compensation
- downsampleFactor = playerPrefsManager.resolutions[playerPrefsManager.currentResolution] * dpiFactor;
- //Update render texture dimensions and settings
- UpdateRenderTexture();
- }
- //Only runs at start or when setting changes
- void UpdateRenderTexture()
- {
- //Create a new render texture, either with screen dimensions (for main camera) or with custom dimensions for UI rendertextures
- if (customRenderTexture == null)
- downsampleRenderTexture = new RenderTexture((int)((float)Screen.width * downsampleFactor), (int)((float)Screen.height * downsampleFactor), 24, RenderTextureFormat.DefaultHDR);
- else
- downsampleRenderTexture = new RenderTexture((int)((float)customRenderTexture.width * downsampleFactor), (int)((float)customRenderTexture.height * downsampleFactor), 24, RenderTextureFormat.DefaultHDR);
- //Turn of AA and mipmapping, and use point filtering for pixelated look
- downsampleRenderTexture.antiAliasing = 1;
- downsampleRenderTexture.useMipMap = false;
- downsampleRenderTexture.filterMode = FilterMode.Point;
- //Set the actual render texture for the cameras
- for (int i = 0; i < renderSourceCameras.Length; i++)
- {
- renderSourceCameras[i].targetTexture = downsampleRenderTexture;
- }
- }
- //When setting changes, force a texture change
- private void SettingChanged()
- {
- downsampleFactor = playerPrefsManager.resolutions[playerPrefsManager.currentResolution];
- UpdateRenderTexture();
- }
- //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
- void OnRenderImage(RenderTexture source, RenderTexture destination)
- {
- if (gameObject.activeSelf)
- {
- //Render each camera
- for (int i = 0; i < renderSourceCameras.Length; i++)
- {
- renderSourceCameras[i].Render();
- }
- //Blit
- Graphics.Blit(downsampleRenderTexture, destination);
- }
- }
- //After each render, discard contents
- void OnPostRender()
- {
- if(downsampleRenderTexture != null)
- {
- downsampleRenderTexture.DiscardContents();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement