# Drawing circles at the background of a 3D scene and updating their size

So, here is what I am trying to do in Unity 5 (using C#). I want to draw a a 2D circle at the background of a 3D scene and apply a texture/image to fill it.

Just like it would be if the circle was in the GUI, but now being always behind everything else instead of always in front (think of a moon in the sky of a piloting game, or a distant planet in a space game, etc).

What would be the fastest way to draw such a circle and, of course, update its radius depending on the camera distance? Of course, I don't want to use a sphere, I want to learn how to that with a simple circle that gets bigger or smaller depending on the distance of the viewer.

• Did you try to setting radius according to the distance between camera and moon. (Vector3.distance)? – Hamza Hasan Dec 14 '15 at 19:29
• You may want to use OnPreRender() to immediately draw your (masked) texture. I think that will achieve the effect you want. Then the scene will draw on top. However the best idea might be to draw the whatever to a texture and that texture is then used as the skybox. – Draco18s no longer trusts SE Jan 27 '16 at 21:03
• @Draco18s the first suggestions seems promising. The problem with your second is that it would become impossible to rescale the circle without scaling all things that are draw in the skybox, right? Or are you suggesting run-time redrawing the skybox's texture? – Louis15 Jan 29 '16 at 1:25
• @Louis15 No no, the skybox is a dynamic texture (a render texture). It gets redrawn every frame. – Draco18s no longer trusts SE Jan 29 '16 at 15:07

Ok, so just of the top of my head your going to want to make a new circle(2d) and apply a texture to this then write something along these lines (and attach it to the object)

Pseudo code:

private gameobject player
private float scale;

update{
face the player (using transform.lookat(player))
float distance = vector3.distance(transfrom.position,player.transform.position);
transform.scale = distance / scale;
}


The scale in this case changes as the player gets closer or further away using the global scale variable to change the sensitivity of it. Obviously i haven't tested it but in 5 min this would be my initial solution.

If you wanted to make it more efficient, then only face the player every few updates, possibly only make it call recursively every few seconds (depending how far away the player is/ how fast the player is).

An answer is better than none... :)

• Many thanks. What you are suggesting is the creation of a circle sprite that could be just put at the very far background and updated in terms of scale and rotation as needed? – Louis15 Jan 29 '16 at 1:28
• yep, fairly simple. Alternatively you could have it be a fixed distance away from the camera but then you might run into clipping problems as you move around... – user3797758 Jan 29 '16 at 18:29

You probably need a skybox or a skydome. A skybox is a box around the camera, and is always centered around it. Basically a model, wich isn't get transformed by the view matrix.

• I know skyboxes, but how exactly that could help me doing what I've asked, i.e. having a circle at the background whose size can be updated as described? – Louis15 Jan 29 '16 at 1:26
• Dinamically generated textures? – Bálint Jan 29 '16 at 6:25