Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by mikelortega · Apr 04, 2016 at 12:02 PM · camerarenderframerate

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tanoshimi · Apr 15, 2016 at 10:23 AM 1
Share

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.

avatar image mikelortega tanoshimi · Apr 15, 2016 at 11:32 AM 0
Share

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.

avatar image
4

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();
         }
     }
 }
Comment
Add comment · Show 14 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Chris-Trueman · Feb 27, 2018 at 12:15 PM 0
Share

Thanks this helped me get my fps back.

avatar image Zebbi · May 10, 2018 at 08:13 PM 0
Share

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

avatar image shawnblais Zebbi · May 10, 2018 at 09:49 PM 0
Share

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.

avatar image Zebbi shawnblais · May 12, 2018 at 05:32 PM 0
Share

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.

Show more comments
avatar image ifurkend · Jul 26, 2018 at 01:37 PM 0
Share

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;
avatar image codestage ifurkend · Apr 20, 2020 at 10:24 AM 0
Share

Why not use Time.unscaledDeltaTime ins$$anonymous$$d?

avatar image NoteMEdown · Jan 03, 2021 at 12:29 AM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

69 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges