- Home /
Billboard world space text
We have fairly large scenes generated from data files. In the scene there are multiple static objects with labels on them (TextMesh). Currently we're doing a lookat operation to make them face the camera and a distance operation to make them scale with the camera distance. The camera is able to move with complete freedom.
I'm looking for ideas on how to improve this as the distance operation is killing our FPS and the rotation is too. We'd like the text to always be the same size and always be facing the camera. It seems like an overlay UI would be good for this, but I don't know how we would fix the UI in world space but always have the labels in the proper position and face the camera. A bit of a conundrum.
Thanks!
Answer by Namey5 · Oct 23, 2016 at 05:46 AM
It sounds like what you are trying to achieve would be easily doable through a world-space positioned GUI element.
//A C# example
void OnGUI ()
{
Vector2 worldPoint = Camera.main.WorldToScreenPoint (transform.position);
GUI.Label (new Rect (worldPoint.x - 100, (Screen.height - worldPoint.y) - 50, 200, 100), "Text to display.");
}
You would just attach this script to every object that would have text, and in combination with the [ExecuteInEditMode] attribute it would work in practically all scenarios. This example works based off the "Main Camera" tagged camera but you can replace all elements in this to your needs.
Thanks, not sure why I've been avoiding this, but looks like it is likely the simplest solution. I'll give it a go and report back.
O$$anonymous$$, so I tested this out in an isolated project and it's almost working with a couple drawbacks.
Our objects can easily be overlapping. Do you have any suggestions on how to deal with depth of both the GUI label and meshes? There will often be the case that a mesh should or label should be drawn behind. There are many "layers" of this.
Is there a good way to ensure these are always drawn behind imGUI or do I need a separate camera for the imGUI that always renders on top?
Thanks!
If you are attaching this script to many objects, then you can use the GUI.depth variable to stack them manually, i.e.
public int depth = 0;
void OnGUI ()
{
GUI.depth = depth;
...
I'm not sure what you mean by 'mesh'. Are you using text meshes? As for the imGUI, I'll be perfectly honest and say that I've never used a combination of the 2 in any one project, so I'm not entirely sure. I'll do some research onto that and get back to you.
I strongly recommend to use Camera.current ins$$anonymous$$d of Camera.main for performance reasons.
Camera.main uses FindObjectWithTag and if that's called every frame it's very performance consu$$anonymous$$g. Also, a benefit with Camera.current is that it takes the current camera that is rendering, so no need to worry about tags.
Agreed that Camera.main has performance issues and the reference to the camera should be gotten by other methods, but Unity's documentation does not recommend using Camera.current outside of very specific overrides, which do not include OnGUI: https://docs.unity3d.com/ScriptReference/Camera-current.html
Camera.current is just a pointer to the most recent camera to render, so there isn't really a performance impact in using it and chances are it would be setup properly for OnGUI (I can't remember if I$$anonymous$$GUI is drawn per camera or just drawn straight into the final rendertarget). If you needed to, you could just cache Camera.main on first call and use it from there;
Camera cam;
void OnGUI ()
{
if (!cam)
cam = Camera.main;
...
Your answer
Follow this Question
Related Questions
How to per-camera billboard World Space UI Canvas? 0 Answers
Button OnClick() Missing Code Error, How do I fix this? 1 Answer
How do i make my ui elements stay the same size and position on all andorid devices? 1 Answer
Issue where if I click a button then press W,A,S,D the value of my sliders go down by 10 or up by 10 0 Answers
Accessing a Variable in another script for (SLIDER) 1 Answer