- Home /
A 2D GUI in mid air, for oculus, tracking an object.
Think of a selection box, in an RTS. What I want, is to draw something like that, on the screen, around a 3d object, while moving. I'm using the Oculus, and using VRGUI for another part of the UI. I was thinking of having a plane in between the object, and the camera, but I'm not sure how to draw GUI on it, without using Unity's build in GUI, or NGUI. Here's an image that hopefully will help you understand:
How can I draw the selection box on a plane, or do you have any other ideas?
EDIT:
I found another way of doing it. For those curious, I decided to project the bounding boxes on a sphere around the camera, with a mesh for each corner :) http://stackoverflow.com/questions/9604132/how-to-project-a-point-on-to-a-sphere
Why are GUI or NGUI a problem? Is a GUITexture okay? Is the rest of the Quad (outside of the selection box) transparent, translucent, or opaque?
I can't really afford NGUI right now, and I'm using ongui for other hud. I used to draw the selection box on the hud, before I implemented oculus. It's way offset, and I can't figure out why, but the real problem is, when you turn your head, the selection box turns with you. That's not acceptable :P Ideally I could project it on a mesh, because the player is sitting inside a transparent sphere, but that's not really necessary. Yes, if I use a quad, the rest should be transparent. Will GUITexture work, if I can't use OnGUI? Doesn't it rely on that?
I suppose I could move the other GUI to the same plane/quad/other, but it wouldn't be as easy to read :/ I guess that's plan F. Hmm a plan E, could be to draw a line from the selected object's position, to the top right of the screen, hmm...
I don't know enough about what you are trying to do and Oculus to give you an answer. You can draw the selection box using any of 1) Quad in world space, 2) GUITexture, or 3) GUI.DrawTexture(). If you want it to stay aligned with the object, the code will be different for each implementation. From what you describe, you should be able to just put a quad with the box texture between the object and the camera at the camera's near clipping plane.
Yes, but how do I figure out where to put the rectangle? I was using worldtoscreenpoint before, but that won't work since I'm drawing it on a 3d object :I Worth noting, that the player will be rotating around (Including different heights) the object, and the object won't be just a box :/
Answer by Scribe · Aug 20, 2014 at 11:40 AM
Hey there, I have some code that does this from a while ago, it uses a Texture as the 'tracking scope' but I would sugeest you update it to use something like vectorsity or linerenderer or similar as the scaling of the texture means that the line around the object changes size as it is scaled. It would make more sense for the lines to stay the same pixel width, and only change the corner positions! (This code is probably not very efficient but I am not likely to change it anytime soon)
C# code:
public GameObject target;
public Texture2D trackingTexture;
public float padding = 0.1f;
Renderer targRenderer;
float x;
float y;
float width;
float height;
Vector3 min = Vector3.one*Mathf.Infinity;
Vector3 max = Vector3.one*Mathf.NegativeInfinity;
Camera cam;
void Start(){
targRenderer = target.GetComponent<Renderer>();
Debug.Log(targRenderer.bounds.ToString());
cam = Camera.main;
}
void Update(){
min = Vector3.one*Mathf.Infinity;
max = Vector3.one*Mathf.NegativeInfinity;
for(int i = -1; i < 2; i+=2){
for(int j = -1; j < 2; j+=2){
for(int k = -1; k < 2; k+=2){
Vector3 worldPos = Vector3.zero;
worldPos.x = targRenderer.bounds.center.x + i*(targRenderer.bounds.extents.x+padding);
worldPos.y = targRenderer.bounds.center.y + j*(targRenderer.bounds.extents.y+padding);
worldPos.z = targRenderer.bounds.center.z + k*(targRenderer.bounds.extents.z+padding);
min = Vector3.Min(min, cam.WorldToScreenPoint(worldPos));
max = Vector3.Max(max, cam.WorldToScreenPoint(worldPos));
}
}
}
Debug.Log(min);
Debug.Log(max);
x = min.x;
y = (Screen.height-max.y);
width = (max.x-min.x);
height = -(min.y-max.y);
}
void OnGUI(){
GUI.DrawTexture(new Rect(x, y, width, height), trackingTexture);
}
Scribe
Your answer
Follow this Question
Related Questions
GUI Follow RaycastHit 2 Answers
Fix a GUI's position relative to a 3D object 1 Answer
2D Arrow pointing at 3D Position 1 Answer
Plane Object as GUI - Lighting Issue 1 Answer
2d sprite bacground motion? 2 Answers