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.
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();
}
}
}
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.
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.