- Home /
OnRenderObject is called more often than I expect.
I was doing some investigation by adding logging to OnWillRenderObject and OnRenderObject to understand when each is called. What I'm seeing is surprising me. I'm testing on GearVR, so I expected OnWillRenderObject and OnRenderObject to be called at most twice per frame, and potentially once if Unity is trying to eliminate duplicate work for each eye. Instead I'm seeing OnWillRenderObject called once per frame, but OnRenderObject called 6 times per frame. Strangely, two of the calls to OnRenderObject are occurring before OnWillRenderObject is called. I'd like to use OnRenderObject to draw some custom geometry, but not if it's going to cause the custom geometry to be drawn multiple times per frame. Can someone explain what's going on?
I added some additional logging about the state of Camera.current on each call to OnRenderObject, and what I'm seeing is that OnRenderObject is being called once per eye on 3 separate cameras. First, it's called twice (once per eye) with Camera.current == PreRender, then OnWillRenderObject is called, then it's called once per eye with Camera.current == my camera, then finally twice more (once per eye) with Camera.current == PostRender. I guess my question becomes, what are the PreRender and PostRender cameras, and should I be concerned about the performance implications?
Since this is Unity-internal stuff related to a relatively new platform, I don't know how much luck you'll have here. But the info you've already managed to find out is pretty great so I wanted to say thanks!
I'm sure you know you could do this, but you could see what happens if you do your custom rendering only when Camera.current == my camera. I'd be interested to know if it works or if something about the Pre/Post rendering cameras is necessary for the graphic to display correctly in the H$$anonymous$$D.
In terms of getting it down to one render call, I don't know how that will work, because each 3D object needs to be rendered by both left and right camera for proper stereoscopy, and if I'm understanding it correctly you can't render with two cameras in one pass.
Answer by Fydar · Feb 26, 2016 at 10:06 PM
I would simply create the bool:
public bool HasRunThisFrame = false;
and inside "OnWillRenderObject" or "OnRenderObject" (What ever one you find more consistent) and do something like this:
public void OnRenderObject () {
if (!HasRunThisFrame) {
//TODO: Draw Objects to Screen
HasRunThisFrame = true;
}
}
public void Update () {
HasRunThisFrame = false;
}
That way, "OnRenderObject" will only run once per frame so you aren't wasting CPU power on drawing stuff needlessly to the screen.
Good luck - Fydar
Answer by dougmuir · Mar 03, 2016 at 02:04 PM
Turns out the additional calls were because of the Cardboard SDK -- we support both Gear and Cardboard, and it turns out the Cardboard SDK creates a PreRender and PostRender camera even when the Cardboard SDK is disabled. It appears that the extra cameras don't actually do anything when the Cardboard SDK is disabled, but the presence of the cameras does cause extra calls to OnRenderObject.
Your answer
![](https://koobas.hobune.stream/wayback/20220612065619im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to show FPS in the screen using latest Google VR package 0 Answers
VR Single Pass Rendering + Graphics Jobs Toggled On Broken 1 Answer
Unity profiler + Gear VR oscillating rendering speed despite no change in image 0 Answers
Galaxy S7 performance issues after upgrading Unity 0 Answers
Blackscreen rendered in Vive HMD when using Render Pipelines 0 Answers