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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Canvas UI to RenderTexture - scaling issue

Discussion in 'UGUI & TextMesh Pro' started by daverrr, Apr 20, 2015.

  1. daverrr

    daverrr

    Joined:
    Feb 4, 2014
    Posts:
    2
    My goal is to render a Canvas UI to a Render Texture so that I can then pass it through to a shader via an Image PostEffects / OnRenderImage function.

    My issue is when I assign a Render Texture as a target to the Screen Space - Camera the UI seems to scale strangely.

    I set the Render Texture to the same resolution as the Canvas Scaler but it still causes issues. When I shut the canvas scaler off however, the render texture displays the UI correctly. (This happen and is testable even when simply previewing the render texture in the inspector). [The aspect ratio is the same in the Canvas Scaler, Game window, and Render Texture ].

    Does anyone have experience with getting the canvas UI into a render texture? I feel like the source of my issue is working with the canvas scaler. Is there something I'm missing here?
     
  2. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,685
    There were a few issues with RenderTexture rendering with Canvases, the best advice is not to use a scaler when rendering to texture.
    But I agree, it does sound like a bug and should work. Best to log it as an issue using the Unity bug reporter in the editor
     
  3. bluescrn

    bluescrn

    Joined:
    Feb 25, 2013
    Posts:
    628
    As well as scaling issues, Camera.Render still seems to ignore canvases entirely? (Screen Space - Camera canvases with Scale With Screne Size scalers, in this case)

    That was my preferred option for doing one-off captures of a camera view for blur effects when using NGUI.

    I've got a fairly tricky case, really - I want to render specific cameras/canvases to a *reduced resolution* RenderTexture (probably a nice power of two, as well), as a first step in creating a blurred render of specific UI layers.

    edit - previous/related threads:

    http://forum.unity3d.com/threads/canvas-render-to-texture-problems.284549/ - What happened to that Camera.Render fix?

    http://forum.unity3d.com/threads/ui-scaling-problem-when-using-render-to-texture.292756/
     
  4. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,685
    @bluescrn If you want to to render the Canvas to a texture, you have to use one of the camera enabled Canvases.
    As far as I'm aware, the default Screen Space - Overlay doesn't render to texture on the main camera due to it's rendering path (could be wrong, but I believe this is the case)
     
  5. hprnv

    hprnv

    Joined:
    Apr 23, 2014
    Posts:
    2
    I'm ssory for necrobumping. But looks like bug or still present or appear again at last versions. I making photo app and about month ago all was good and when I update unity and launch it yesterday canvas scaler become setting strange scale value to recttransform scale (canvas with camera which use render texture).
     
  6. ljannin

    ljannin

    Joined:
    Oct 15, 2013
    Posts:
    1
    I'm also experiencing this issue.

    If you don't need the camera to move around, a (weird) workaround is to set its
    renderMode back to WorldSpace
    after it computed the layout.
     
  7. LyveIG

    LyveIG

    Joined:
    Jan 8, 2015
    Posts:
    15
    The reason for this is that the CanvasScaler always uses the screen size as reference (Screen.width, Screen.height) to calculate the scale factor, but that is obviously wrong if you have a RenderTarget of a different size. It is no problem if your render target matches the screen size, but this is most probably not the case in editor, where the screen size is your game view window size.

    Easiest way to fix this is to create a c# script, derive it from CanvasScaler, override HandleScaleWithScreenSize and implement it correctly and replace the CanvasScaler by your custom implementation afterwards.
     
    MaxEden and DanjelRicci like this.
  8. MaxEden

    MaxEden

    Joined:
    Jun 22, 2013
    Posts:
    79
    I honestly don't understand why it hasn't been fixed yet. The fix is incredibly simple.