noradninja

CameraResolutionScaler.cs

Aug 16th, 2023 (edited)
1,304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.35 KB | None | 0 0
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Rendering;
  4.  
  5. [ExecuteInEditMode]
  6. public class CameraResolutionScaler : MonoBehaviour
  7. {
  8.  
  9.     // public float downscaleFactor = 2f; // Factor by which to downscale the framebuffer
  10.     // private int width;
  11.     // private int height;
  12.     // private Camera camera;
  13.     // public RenderTexture downscaleTexture;
  14.     // private CommandBuffer downscaleCommandBuffer;
  15.     // private Material blitMaterial;
  16.     //
  17.     // private void Awake()
  18.     // {
  19.     //     camera = GetComponent<Camera>();
  20.     //     blitMaterial = new Material(Shader.Find("Blitter"));
  21.     // }
  22.     // private void OnPreRender() //before we render anything
  23.     // {
  24.     //     width = 512;//Mathf.RoundToInt(camera.pixelWidth / downscaleFactor);
  25.     //     height = 256; //Mathf.RoundToInt(camera.pixelHeight / downscaleFactor);
  26.     //     Rect viewport = new Rect(0, 0, width, height);
  27.     //
  28.     //     camera.pixelRect = viewport;
  29.     //     downscaleCommandBuffer = new CommandBuffer();
  30.     //     camera.AddCommandBuffer(CameraEvent.BeforeForwardOpaque, downscaleCommandBuffer);
  31.     //    
  32.     //     downscaleCommandBuffer.Clear();
  33.     //     downscaleCommandBuffer.SetRenderTarget(downscaleTexture);
  34.     // }
  35.     //
  36.     // private void OnPostRender() //immediately after render but before drawing to framebuffer
  37.     // {
  38.     //     downscaleCommandBuffer.Blit(BuiltinRenderTextureType.CameraTarget, downscaleTexture);
  39.     //     camera.pixelRect = new Rect(0, 0, 960, 544);
  40.     //     downscaleCommandBuffer.SetRenderTarget(BuiltinRenderTextureType.CameraTarget);
  41.     //     downscaleCommandBuffer.Blit(downscaleTexture, BuiltinRenderTextureType.CameraTarget);
  42.     //     camera.RemoveCommandBuffer(CameraEvent.BeforeForwardOpaque, downscaleCommandBuffer); //drop cb
  43.     // }
  44.     public enum currentResolution
  45.     {
  46.         [Tooltip("960x544")] Full,
  47.         [Tooltip("720x408")] Mid,
  48.         [Tooltip("640x363")] Low,
  49.         [Tooltip("480x272")] PSP
  50.     }
  51.  
  52.     public enum internalResolution
  53.     {
  54.         [Tooltip("960x544")] Full,
  55.         [Tooltip("720x408")] Mid,
  56.         [Tooltip("640x363")] Low,
  57.         [Tooltip("480x272")] PSP
  58.     }
  59.  
  60.     public bool enableInternalResolution = true;
  61.     public internalResolution InternalResolution;
  62.     public currentResolution screenResolution;
  63.     public FilterMode filterMode = FilterMode.Trilinear;
  64.    
  65.     private new Camera camera;
  66.     private Rect originalRect;
  67.     //private float renderDivisor;
  68.     private RenderTexture renderTex;
  69.     private Rect scaledRect;
  70.     private int width;
  71.     private int height;
  72.    
  73.    
  74.     private void Awake()
  75.     {
  76.         camera = GetComponent<Camera>();
  77.  
  78.         if (!Application.isEditor)
  79.             switch (screenResolution)
  80.             {
  81.                 //set resolution and 30Hz vsync
  82.                 case currentResolution.Full:
  83.                     Screen.SetResolution(960, 544, true);
  84.                     QualitySettings.vSyncCount = 2;
  85.                     break;
  86.                 case currentResolution.Mid:
  87.                     Screen.SetResolution(720, 408, true);
  88.                     QualitySettings.vSyncCount = 2;
  89.                     break;
  90.                 case currentResolution.Low:
  91.                     Screen.SetResolution(640, 368, true);
  92.                     QualitySettings.vSyncCount = 2;
  93.                     break;
  94.                 case currentResolution.PSP:
  95.                     Screen.SetResolution(480, 272, true);
  96.                     QualitySettings.vSyncCount = 2;
  97.                     break;
  98.             }
  99.         else //disable vsync in Editor
  100.             QualitySettings.vSyncCount = 1;
  101.     }
  102.  
  103.     private void OnDisable()
  104.     {
  105.         camera.pixelRect = originalRect;
  106.     }
  107.    
  108.     private void OnDestroy()
  109.     {
  110.         camera.pixelRect = originalRect;
  111.     }
  112.  
  113.     private void OnPreRender()
  114.     {
  115.         if (enableInternalResolution)
  116.         {
  117.             switch (InternalResolution) //set up values for RT
  118.             {
  119.                 case internalResolution.Full:
  120.                     width = 960;
  121.                     height = 544;
  122.                     //renderDivisor = 1;
  123.                     break;
  124.                 case internalResolution.Mid:
  125.                     width = 720;
  126.                     height = 408;
  127.                     //renderDivisor = 1.334f;
  128.                     break;
  129.                 case internalResolution.Low:
  130.                     width = 640;
  131.                     height = 368;
  132.                     //renderDivisor = 1.5f;
  133.                     break;
  134.                 case internalResolution.PSP:
  135.                     width = 380;
  136.                     height = 215;
  137.                     //renderDivisor = 2.0f;
  138.                     break;
  139.             }
  140.  
  141.             // rect is 0 to 1
  142.             // pixelRect is 0 to renderSize
  143.             originalRect = camera.pixelRect;
  144.             scaledRect.Set(0, 0, width,height);
  145.             camera.pixelRect = scaledRect;
  146.         }
  147.     }
  148.  
  149.     private void OnRenderImage(RenderTexture src, RenderTexture dest)
  150.     {
  151.         // Luckily, looks like using OnRenderImage automatically makes the camera render to a TempBuffer of the size of the camera.pixelRect
  152.    
  153.         camera.pixelRect = originalRect;
  154.         src.filterMode = filterMode;
  155.         Graphics.Blit(src, dest); // Blit from the low res to the framebuffer
  156.     }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment