Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Confused about RenderTextures and Camera.Render()

Discussion in 'General Graphics' started by Fluxes, Sep 19, 2023.

  1. Fluxes

    Fluxes

    Joined:
    Nov 30, 2018
    Posts:
    5
    I am working on a tool that creates icons of GameObjects.

    A basic rundown of what it does:
    It uses the scene view to align the snapshot.
    A temporary camera is created and its position/rotation is set to the same as the scene view camera
    I do this to change a few settings on the camera (I dont want to mess with the scene view camera)

    After calling
    Render()
    on the camera and setting
    RenderTexture.active
    , I get exactly what I need when I call
    Texture2D snap = new Texture2D( m_SelectedIconSize, m_SelectedIconSize, TextureFormat.RGBA64, false );
    snap.ReadPixels( _snapArea, 0, 0, false );


    Is setting the active render texture the only way to get a camera render ?

    It feels weird to save the current render texture, set the active one manually, read the pixels and set it back to the saved one.

    I tried doing the same with
    Graphics.CopyTexture( _cam.targetTexture, 0, 0, (int)_snapArea.x, (int)_snapArea.y, (int)_snapArea.width, (int)_snapArea.height, snap, 0, 0, 0, 0 );
    but the result is always grey.
    I guess thats because it doesnt actually render the camera to its render texture.

    Frankly, I dont get it :confused:
     
    Last edited: Sep 19, 2023
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
  3. Fluxes

    Fluxes

    Joined:
    Nov 30, 2018
    Posts:
    5
    Ok I found this.

    I use
    Texture2D.ReadPixels()
    and
    Texture2D.EncodeToPNG()
    which both run on the CPU.
    Graphics.CopyTexture()
    runs on the GPU.

    But just looking at the
    AsyncGPUReadback.RequestIntoNativeArray
    page makes me wanna leave it as it is, because I have a feeling the code looks even more hacky afterwards.

    Maybe I will try to make my own
    ReadPixels()
    .