- Home /
How to manually resize render texture without recreating it?
I want to resize my render texture every frame, so always recreating it isn't great idea. I'm trying to create portals, and don't want them to render in full resolution, when I'm far from them.
I tried to first enable dynamic scaling, and then just change RenderTexture.width (and height), but I'm still getting errors like: "Setting width of already created render texture is not supported!".
I know about ScalableBufferManager, but I don't see way to manually resize render texture with it.
For dynamic resolution, internally Unity doesn't actually change the resolution of the render target each frame. Ins$$anonymous$$d, the render target remains at full resolution the entire time, but only small portions of it are actually rendered to. I haven't worked with dynamic resolution myself, but unfortunately it looks like you have to scale all RTs by the same factor globally (you can't resize on a per-RT basis).
I guess I could use ScalableBuffer$$anonymous$$anager by not marking render textures as dynamically scalable by default, and by enabling it for one at a time, but it would be dirty. I'm wondering what ScalableBuffer$$anonymous$$anager actually does, and if could do it myself.
$$anonymous$$y guess is that it would be something similar to SetViewport;
https://docs.unity3d.com/ScriptReference/Rendering.CommandBuffer.SetViewport.html
By setting a viewport, rendering only happens within the provided Rect. If you wanted to do this with a camera, you could set its rect or pixelRect;
https://docs.unity3d.com/ScriptReference/Camera-rect.html
https://docs.unity3d.com/ScriptReference/Camera-pixelRect.html
I haven't used command buffers, and can't get them to work, and camera's view port rectangle doesn't work with render textures. I'm probably going to end up using ScalableBuffer$$anonymous$$anager.
Answer by Quelfth · Oct 28, 2020 at 05:29 PM
I believe that dynamic scaling works automatically. You don't need to change the size, because with dynamic scaling enabled, Unity should automatically resize the render texture if your frame rate drops. Or, if you want to control the scale manually, you can use ScalableBufferManager.ResizeBuffers(width, height)
to set the scale of all render textures with the dynamically scalable flag set.
However, it seems to me that this only allows you to change the scales of all render textures simultaneously, and if you have multiple portals, it would seem inappropriate to scale all of them at the same time.
I don't believe that it is possible to change the scale of a render texture dynamically, but I'm not sure that creating render textures every frame is as expensive as you think it is. Additionally, you don't actually need to create them every frame, since you only need to create a new one when the size you want it to be is different from the size it is.
Answer by Mandoz · Oct 28, 2020 at 07:11 AM
I'm not sure whether is a right way. Anyway it woked for me.
rtex.Release(); rtex.width = 1024; rtex.height = 720; rtex.Create();
Thanks, but this does recreate the texture (rtex.Create()), which makes it way to slow for optimization.
Answer by studentutu · Jun 04, 2021 at 08:12 AM
@Mandoz @RoopeKT Just resize the actual render texture width/height = it will not allocate any more than it needs and will preserve all the references. See the Manual. It says - it is not like Texture - width/height are mutable properties! And also, you don't need to manually call .Create() if you already have a render texture created (either before or inside Unity Editor); it is best to create 1 texture ahead of time and set your target properties inside the editor. You will be able to see/preview everything
Trying to change width/height after the render texture has been created with .Create() will cause an error: "Setting width of already created render texture is not supported!". Render texture must be created before used, and I was looking for a way to resize a texture already in use.
Your answer
Follow this Question
Related Questions
Perspective Correction with Render Texture 0 Answers
Making Portal-Like Objects 2 Answers
UV Mapping - FBX meshes wont accept textures 0 Answers
RenderTexture.Create failed RenderTexture.GenerateMips failed 1 Answer
Android/IOS GUI Rescale 0 Answers