Search Unity

Question Custom Resolution and Viewport Scaling

Discussion in 'Scripting' started by JathamTheGreat, Aug 22, 2022.

  1. JathamTheGreat

    JathamTheGreat

    Joined:
    Apr 15, 2018
    Posts:
    4
    I'm writing a custom render pipeline right now. I want to render at a lower resolution to have a kind of retro style. My main issue is that I want the viewport to have the same aspect ratio as the reference resolution. I also want the player to be able to resize the window instead of having preset resolutions, so I want to leave the extra space caused by the viewport scale empty.

    This is my current RenderPipeline script, right now it only renders at the given resolution. I can't find any way to scale the viewport that doesn't wrap the render texture.

    I've tried setting the camera viewports, I've tried setting the scale and offset of the blit, I've tried setting the viewport with the command buffer. None of these give the desired result.

    Code (CSharp):
    1.  
    2. using UnityEditor;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6. using UnityEngine.Rendering;
    7.  
    8. public class ResRescalerPipeline : RenderPipeline
    9. {
    10.     private ResRescalerAsset asset;
    11.  
    12.     public ResRescalerPipeline(ResRescalerAsset _asset)
    13.     {
    14.         asset = _asset;
    15.     }
    16.  
    17.     protected override void Render(ScriptableRenderContext context, Camera[] cameras)
    18.     {
    19.         // This is where you can write custom rendering code. Customize this method to customize your SRP.
    20.         // Create and schedule a command to clear the current render target
    21.         CommandBuffer cmd = new CommandBuffer();
    22.  
    23.         // Constants used in the camera render loop.
    24.         RenderTextureDescriptor rtDesc = new RenderTextureDescriptor(asset.resolution.x, asset.resolution.y, RenderTextureFormat.Default, 24);
    25.         int rtID = Shader.PropertyToID("_LowResScreen");
    26.  
    27.         foreach (Camera camera in cameras)
    28.         {
    29.             // Update the value of built-in shader variables, based on the current Camera
    30.             context.SetupCameraProperties(camera);
    31.  
    32.             cmd.name = "Setup";
    33.             if(camera.cameraType == CameraType.Game)
    34.             {
    35.                 cmd.GetTemporaryRT(rtID, rtDesc);
    36.                 cmd.SetRenderTarget(rtID);
    37.             }
    38.             cmd.ClearRenderTarget(true, true, camera.backgroundColor);
    39.             context.ExecuteCommandBuffer(cmd);
    40.             cmd.Clear();
    41.  
    42.             // Get the culling parameters from the current Camera
    43.             camera.TryGetCullingParameters(out var cullingParameters);
    44.             // Use the culling parameters to perform a cull operation, and store the results
    45.             CullingResults cullingResults = context.Cull(ref cullingParameters);
    46.  
    47.             // Tell Unity which geometry to draw, based on its LightMode Pass tag value
    48.             ShaderTagId shaderTagId = new ShaderTagId("Base");
    49.  
    50.             // Tell Unity how to sort the geometry, based on the current Camera
    51.             SortingSettings sortingSettings = new SortingSettings(camera);
    52.             // Create a DrawingSettings struct that describes which geometry to draw and how to draw it
    53.             DrawingSettings drawingSettings = new DrawingSettings(shaderTagId, sortingSettings);
    54.             // Tell Unity how to filter the culling results, to further specify which geometry to draw
    55.             // Use FilteringSettings.defaultValue to specify no filtering
    56.             FilteringSettings filteringSettings = FilteringSettings.defaultValue;
    57.      
    58.             // Schedule a command to draw the geometry, based on the settings you have defined
    59.             context.DrawRenderers(cullingResults, ref drawingSettings, ref filteringSettings);
    60.          
    61.             // Schedule a command to draw the Skybox if required
    62.             if (camera.clearFlags == CameraClearFlags.Skybox && RenderSettings.skybox != null)
    63.             {
    64.                 context.DrawSkybox(camera);
    65.             }
    66.             if (Handles.ShouldRenderGizmos()) {
    67.                 context.DrawGizmos(camera, GizmoSubset.PreImageEffects);
    68.                 context.DrawGizmos(camera, GizmoSubset.PostImageEffects);
    69.             }
    70.  
    71.             cmd.name = "Blit";
    72.             if(camera.cameraType == CameraType.Game)
    73.             {
    74.                 cmd.Blit(rtID, BuiltinRenderTextureType.CameraTarget);
    75.             }
    76.             context.ExecuteCommandBuffer(cmd);
    77.             cmd.Clear();
    78.  
    79.             // Instruct the graphics API to perform all scheduled commands
    80.             context.Submit();
    81.         }
    82.     }
    83. }
    84.  
    Unity version is 2020.3.21f1

    I'm not sure what to do here, I'm not even sure if using a temporary render texture is the best idea. Any help would be much appreciated.
     
    Last edited: Aug 22, 2022