Search Unity

Decrease screenshot resolution

Discussion in 'Scripting' started by Serhii-Horun, Mar 20, 2019.

  1. Serhii-Horun

    Serhii-Horun

    Joined:
    Apr 12, 2015
    Posts:
    151
    Last edited: Mar 20, 2019
  2. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    I think a hack thats used is to render the game at whatever res you want for one frame, capture that and mive back to the regular res.

    Might be able to take advantage of another camera but i never tried it myself.

    Could also just scale it down
     
  3. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,639
    Yeah, instead of getting a Texture2D from CaptureScreenShotAsTexture, you could use a render texture, then you could use whatever size texture that you want.

    https://docs.unity3d.com/Manual/class-RenderTexture.html
     
    TheDevloper, lordofduct and SparrowGS like this.
  4. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,203
  5. Serhii-Horun

    Serhii-Horun

    Joined:
    Apr 12, 2015
    Posts:
    151
    TheDevloper likes this.
  6. rosskrasner

    rosskrasner

    Joined:
    Feb 18, 2019
    Posts:
    1
    Hi can you please provide example code you used? Running into the same problem and unsure how to implement Blit
     
  7. Serhii-Horun

    Serhii-Horun

    Joined:
    Apr 12, 2015
    Posts:
    151
    I don't remember regarding Blit, but for now we are using this code.
    Code (CSharp):
    1. public static void Scale(this Texture2D inputTexture, int width, int height, FilterMode mode = FilterMode.Trilinear)
    2. {
    3.     var textureRect = new Rect(0,0,width,height);
    4.     GPUScale(inputTexture,width,height,mode);
    5.  
    6.     inputTexture.Resize(width, height);
    7.     inputTexture.ReadPixels(textureRect,0,0,true);
    8.     inputTexture.Apply(false);
    9. }
    10.  
    11. private static void GPUScale(Texture2D src, int width, int height, FilterMode filterMode)
    12. {
    13.     src.filterMode = filterMode;
    14.     src.Apply(false);
    15.     var renderTexture = new RenderTexture(width, height, 32);
    16.     Graphics.SetRenderTarget(renderTexture);
    17.     GL.LoadPixelMatrix(0,1,1,0);
    18.     GL.Clear(true,true,new Color(0,0,0,0));
    19.     Graphics.DrawTexture(new Rect(0,0,1,1),src);
    20.     Object.Destroy(renderTexture);
    21. }
    22.  
    and few more fragments of code:
    Code (CSharp):
    1. var thumbnailRenderTexture = RenderTexture.GetTemporary(Screen.width, Screen.height, 0, RenderTextureFormat.ARGB32);
    2.             ScreenCapture.CaptureScreenshotIntoRenderTexture(thumbnailRenderTexture);
    3.             var composer = _cameraSystem.CameraController.CinemachineComposer;
    4.             AsyncGPUReadback.Request(thumbnailRenderTexture, 0, TextureFormat.ARGB32, (request) => OnCompleteReadback(@event, composer, request));
    5.             RenderTexture.ReleaseTemporary(thumbnailRenderTexture);
    6.            
    7.             RenderTexture.active = originalRenderTexture;
    8.             thumbnailCamera.targetTexture = cameraTexture;
    9.         }
    10.  
    11.         private void OnCompleteReadback(Event ev, CinemachineComposer composer, AsyncGPUReadbackRequest request, Action callback = null)
    12.         {
    13.             if (request.hasError)
    14.             {
    15.                 Debug.LogError("GPU readback error detected.");
    16.                 return;
    17.             }
    18.  
    19.             var originalPixels = request.GetData<uint>();
    20.  
    21.             _eventPreviewThumbnailsCreator.TakeThumbnail(ev, originalPixels, composer);
    22.             _eventTimelineThumbnailCreator.TakeThumbnail(ev, originalPixels, composer, _onComplete);
    23.             _renderTexture.Release();
    24.             _renderTexture.DiscardContents(true, true);
    25.             callback?.Invoke();
    26.         }
     
    Last edited: May 21, 2021
  8. april_4_short

    april_4_short

    Joined:
    Jul 19, 2021
    Posts:
    489
    Thank you.

    How do I copy and paste this code without the line numbers?
     
  9. WookieWookie

    WookieWookie

    Joined:
    Mar 10, 2014
    Posts:
    35
    No. This causes a massive hitch of like 5 seconds on my high end iMac.