Canvas UI to RenderTexture - scaling issue

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?

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

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:

Canvas render-to-texture problems - Unity Engine - Unity Discussions - What happened to that Camera.Render fix?

@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)

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).

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.

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.

2 Likes

I honestly don’t understand why it hasn’t been fixed yet. The fix is incredibly simple.