For learning purposes, I am trying to implement my own UI classes in Unity "from scratch", in the GPU using shaders. I mean, I am trying to use simple 2D sprites either positioned via shaders or created within the geometry shaders. These sprites should be, of course, displayed above all else.

I know how to create or translate the sprites within the shader. However, I am struggling with a couple of conceptual issues:

1) where exactly, relative to the camera (or to the near frustum plane), should the UI sprites be positioned? I man, thinking of the Z axis as being the camera.forward, what is the correct default distance to display the sprites at (assuming that at that location, the sprite texture would be the closes to its correct 100% zoom scale).

2) for the correct positioning+rotation, would it just be the case of, every frame, calculating in the shader such correct world position of the sprite in relation to the camera/near frustum plane, and then making the sprite rotate towards the camera? Or is there a better way of doing that, i.e. disregarding world position at all and going directly for the screen position of the sprites within the shaders, since the final step of shaders is to paint the pixels in screen-space?

Thanks for your suggestions!


1 Answer 1

  1. Don't use worldspace, use screenspace
  2. See 1

See, if you use World Space, what happens when an object gets closer to the camera than your arbitrary sprite distance? Why, it renders in front of the UI. This is bad.

Secondly, if you're using GL calls, GL natively works in Screen Space, you actually have to perform counteracting translates in order to position your sprites.

  • \$\begingroup\$ Thanks for your answer. So what you mean is that for each UI sprite I should just pass to the GPU already the screenspace position (i.e. two floats) of their vertices and directly use those for the output the vertex shader passes for the pixel/fragment shader? \$\endgroup\$ May 16, 2016 at 4:22
  • \$\begingroup\$ I'm going to say "yes" but I haven't actually done any work that deals with doing this. \$\endgroup\$ May 16, 2016 at 4:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .