- Home /
Custom GUI System RenderTexture (Ortographic Cam) vs Perspective
Hello, I'm trying to implement my own GUI-System into Unity.
I don't want to use OnGUI because of the well known performance issues.
Altough I have a license for EZGUI, i'd like to create my own system so i can learn something new.
As I see it, there are 2 Approaches:
Using a second orthographic camera for the GUI and overlaying it on the main camera using a Render Texture.
Keep everything in the Perspective Camera and calculating the correct Distance/Scale of the Object for it to be pixel perfect.
Most people seem to go for the 1st approach. Isn't there quite a bit of Overhead in this, because of the render texture?
I prefer approach 2. I already made several tests and it works surprisingly well. I just calculate the correct Scale of the Object (plane) from the distance to camera (with some trigonometry). This way the texture is pixel perfect (after moving it 0.5 pixels, took me forever to figure this one out...). Even drag and drop works like a charm. For MouseOver- or Click-effects I just move the UV coordinates of the mesh (I don't use Material Offset, so the Objects can still be batched).
I know this approach is quite a bit more complicated (and also more difficult to maintain) but I think performance-wise it's better.
What are your thoughts?
Should I continue (I intend to code a full framework like EZGUI) like this or should I Switch to the Orthographic Approach with a RenderTexture and second camera. What are the Overheads of a Rendertexture?
I can't see those options out-perfor$$anonymous$$g OnGUI. I suggest you run some benchmarks before you get too far.
Why not just use a second orthographic camera rendering on top of your perspective camera clearing depth only?
How would I just render the second camera on top of the main cam, without using a Rendertexture? Is that possible? I was looking for something like that but didn't find anything. Wouldn't it be some overhead just because there's a second camera to render?
I will do some benchmarks then. I think the main gain in performance should come from all of it being just 1 drawcall (using a texture atlas), that's the reason why EZGUI is faster than OnGUI. The calculations only happen at Awake(), after that everything is just static planes in front of the camera.
submitted answer with step-by-step instructions of how to set that up.
Answer by equalsequals · Aug 15, 2011 at 08:57 PM
Like I said in my comment, just use a second camera.
Create a new camera, set it to be orthographic. (Also, remove any unneeded components such as GUILayer, FlareLayer, AudioListener)
To maintain pixel-perfect set the orthographic size to be exactly one half of the screen height (ie if res is 1024x768, ortho size is 384)
Create a new layer and call it "UI" or something - set the GUI Camera's culling to cull only this new layer.
Now on the clear flags of the camera, set that to "depth only".
Select the other camera, set it's culling layers to not cull "UI".
Now any UI element should be set to the layer UI, and fall within the GUI Camera's frustum.
Note - You should also ensure that the depth of your UI camera is greater than your Main Camera, else you won't see anything.
Hope that helps.
==
Oh wow, I didn't know that two cameras just automatically render over each other (with different depth settings). I tried this once and it didn't work, i probably forgot something... So I thought this would only be possible with a render texture.
Thanks for your Guide, I'll try it out. I probably wasted a LOT of time with all this trigonometry stuff...
I found my mistake, forgot the "depth only"-flag when testing this. Now I feel stupid...
I tried this and everything is good, except for one thing. When I set the ortho size to 360 (im working in 720) all my gui objects get suuuper tiny and I have to make them huge in order to see them. I set the size to 1 and its more reasonable, I guess for me it doesnt even matter if I have pixel-perfect. But hopefully you can clarify. Thanks for the great walkthrough!
OrthographicSize is in meters (or whatever your world units are), not in pixels (which is what I assume you mean by "working in 720").
Answer by JeroenAnimator · Nov 13, 2012 at 05:55 PM
Ok that works great but I have a question. I use a custom gui setup with an overlay orthographic camera the way you explained.
However I use OnmouseOver events for my buttons that are gameobjects with collider's. This works fine with a perspective camera but orthographic doesn't seem to trigger the onmouseover event.
However I need to use an orthographic camera to keep the gui buttons in position with different aspect ratio's. Any ideas?
Hi There.
Please don't post comments as answers. Post comments by clicking the [add new comment] button, a window then open for you to type in. Answer fields are for answers only, as this is a knowledge base.
You can convert this answer to a comment (or just edit your original question), you'll also get a better chance of getting an actual answer if the main list shows none or one answer in blue =]