- Home /
Secondary camera with lower frame rate, or render every x frames
I'd like to render a secondary camera with a lower frame rate than the main one. I tried different things but still don't have a solution.
I tried calling Camera.Render manually, but then I don't know how to disable camera's automatic rendering.
I also tried setting Camera.clearFlags to Nothing and cullingMask to zero, and configure it correctly every x frames, but then the main camera clears the secondary camera.
I guess I can render to texture and only show copies of the texture every x frames, but I would like to optimize, so the camera doesn't render when it isn't necessary.
Do you have any other advice? Thank you.
Answer by mikelortega · Apr 15, 2016 at 09:41 AM
Just in case someone finds it useful. I added script to the secondary camera that catches the source RenderTexture every x frames and saves it. Then, every frame, the destination texture is writen with the saved one.
void OnRenderImage(RenderTexture source, RenderTexture destination)
{
if (m_SavedTexture == null)
m_SavedTexture = Instantiate(source) as RenderTexture;
if (Time.frameCount % 20 == 0)
Graphics.Blit(source, m_SavedTexture);
Graphics.Blit(m_SavedTexture, destination);
}
The only problem is that the camera keeps rendering every frame, but it just paints the frame on the screen every x frames. This drawback might also be interesting, so the camera keeps an stable frame rate and doesn't generate peaks.
I would have thought that a manual Camera.Render() should have worked fine on an InvokeRepeating/coroutine loop. To prevent the camera "automatically" rendering every frame, you keep it disabled.
The problem if I disable the camera is that it disappears, I'm not rendering to texture. I guess I'm missing something. Thank you for the advice! I really appreciate.
Answer by shawnblais · Nov 02, 2017 at 05:13 AM
This script is working great for me, you can keep the Camera enabled in the Editor, the script will disable it on Start();
[RequireComponent(typeof(Camera))]
public class ManualCameraRenderer : MonoBehaviour {
public int fps = 20;
float elapsed;
Camera cam;
void Start () {
cam = GetComponent<Camera>();
cam.enabled = false;
}
void Update () {
elapsed += Time.deltaTime;
if (elapsed > 1 / fps) {
elapsed = 0;
cam.Render();
}
}
}
@shawnblais How do I attach this to a camera? Sorry if this sounds incredibly naive but I'm trying to get just a camera to render to a lower frame rate without affecting the software cursor or UI, and I tried attaching this script to my camera, but nothing showed. Should I be changing the rendertexture as well? I haven't done anything with rendertextures yet so I'm a little confused exactly how to get this working!
This is just an optimization, so the Camera can render at a different FPS than the main application. To test, just remove the script completely, and let the Camera run at full frameRate, if everything works correctly, add the script to lower the FPS. Also make sure you've set fps higher than 0 or it will throw an error.
Is this script meant to be used with a second camera rendering to a RenderTexture? I've got a setup with a second camera child to the main camera viewing a raw image render texture. Is this how this script should be setup? I tried adding it to the second camera and the frame-rate stays the same, and it says "no cameras rendering" if I add it to the main camera.
Thanks for this answer and it helps me tremendously. I amend slightly at line 13 by dividing the Delta Time by Time Scale, so the refresh rate stays consistent during slow motion or fast-forward.
elapsed += Time.deltaTime / Time.timeScale;
Why not use Time.unscaledDeltaTime ins$$anonymous$$d?
I recommend making the fps a float, or changing "if (elapsed > 1 / fps) " to "if (elapsed > 1f / fps)". Currently you are getting some loss of precision, and a lot of fps values don't work (mainly low ones) 1/fps will be an integer, not a float as desired.
Answer by CaseyLee · Nov 07, 2016 at 01:14 PM
The strategy to disable the camera and only enable it when its rendering doesn't work well (at all) in the editor, but if you feel like re-examining your solution make sure when testing it you build out.
Your answer
Follow this Question
Related Questions
Broadcast rendered camera through networking. 1 Answer
Make Camera not render individual GameObject? 1 Answer
How to only render things behind a mesh. 0 Answers
Draw Camera to Editor Window 1 Answer
Rendering players per camera 1 Answer