- Home /
Transparent objects sorting
Edit : Clarified my question based on discussion in comments.
Hi,
I found several questions concerning transparent objects sorting, but no answers cover my needs, so here is my question :
Problem :
It seems meshes in the transparent render queue are sorted 'back to front' using their distance from the camera. It is my understanding (please correct me if I'm wrong here) that meshes should rather be sorted using only their Z coordinate in camera's space...
Example :
Let object B be in (3, 0, 1) and object A be in (2, 0, 2), both coordinates expressed in camera's space.
While B and A respectively are at sqrt(10) and sqrt(8) from the camera, their Z coordinate in camera's space respectively is 1 and 2.
Now it is clear that sorting A and B back to front by their distance will first render B then A, while sorting them by their Z coordinate in camera's space will render A then B.
If you look at the schema you can see that the expected behaviour is to have A rendered first. As stated above, it seems Unity renders B first.
Questions :
Is that a Unity's bug, or am I facing a special case ?
Is there a way to implement new render queues, assigning them a (range of) id(s), so that one can create custom sort algorithms (the two I now of being 'some magic + front to back (ids
If not, could this feature be added ?
Thanks for your help !
Answer by steinbitglis · Jun 23, 2012 at 09:15 PM
I got this mail from Unity yesterday:
Hi,
Good news! This issue has been fixed for the upcoming Unity 3.5.3 release (it will be out in a matter of days now). You have to set your camera's transparency sort mode from code to TransparencySortMode.Ortographic so that objects are sorted based on distance along the camera's view.
Best regards, Elvis Alistar Test Engineer
Edit:
## Perspective2DSortMode.boo
import UnityEngine
[ExecuteInEditMode, RequireComponent(Camera)]
class Perspective2DSortMode (MonoBehaviour):
def Awake ():
camera.transparencySortMode = TransparencySortMode.Orthographic
Yeap, I"ve received the same mail too. Anyway thanks for having forwarded it here !
Works great. Using Unity 4.1, any chance there is a way to set this in the IDE now, not via script? Would be nice if the game view preview would render this way (before I press Play).
you can always write a little script to attach to your camera and use the ExecuteInEdit$$anonymous$$ode attribute http://docs.unity3d.com/Documentation/ScriptReference/ExecuteInEdit$$anonymous$$ode.html
Hello , I've tried this script lines given by Steinbitglis, but it has no effect on my camera in Unity 5.3.3f1, I still have this bug while rendering meshes shaded with "fade" materials. Does this code still works on Unity 5.x or maybe is it already implemented in Unity 5.x ?
Ok, in fact, now in Unity 5.x, the option to make a camera "perspective" or "orthographic" is included in the parameters of any new camera : http://docs.unity3d.com/$$anonymous$$anual/CamerasOverview.html
Answer by Mortennobel · Jul 13, 2011 at 03:59 PM
You are right that the correct way to render transparent would be to sort them using the z-component in screen space. However that is not how the render pipeline works. The render pipeline render one mesh at a time using a shader. This makes it impossible to sort transparency per pixel. Therefor an approximation is used (sort geometry by distance to camera).
You can easily tweak the render queue, but you cannot solve this problem in a general way.
See:
http://unity3d.com/support/documentation/Components/SL-SubshaderTags.html http://unity3d.com/support/documentation/Components/SL-PassTags.html
Thanks for your quick answer !
Anyway, I think I have not been clear enough : by Z component, I meant Z component of object's location, not of all pixels.
Qestion 1 becomes : shouldn't the render pipeline sort meshes using their Z in camera's space ins$$anonymous$$d of their distance from the camera ?
Also, by customizing render queues I was thinking of writing new ones. Reading documentation and answers in this forum, I understand that :
- Render queues are defined by a positive integer
- Render queues with Ids <= 2500 are sorted with some (undocumented) algorithms, then front to back
- Render queues with Ids 2500 are simply sorted back to front
Question 2 becomes : can one implement its own render queue, assigning it (a range of) id(s) ?
Question 3 remains unchanged.
I'm still not sure I understand your question. Sorting meshes in world space (distance between camera and mesh) and in camera space (using the Z component) would give the same result.
Not really.
Let object A be in (-3, 0, 1) and object B be in (-2, 0, 2) both expressed in camera's space.
A is at sqrt(10) from camera and B is at sqrt(8) from the camera independently of used space. In this case it appears B should be rendered after A (if in a transparent queue) ... in my particular (and all ?) case this is not true.
But if we only consider Z order in camera's space A is at 1 and B is at 2. Here A is rendered after B, which is correct.
That's why I would like to create my own custom render queue : to sort objects the way I need it.
I just realized that I have mixed up Camera space with Screen space. You are right that ordering by the z-distance in screen space does not give the same ordering as ordering by distance (in screen or world space).
Answer by Orion 1 · May 03, 2012 at 03:47 PM
We solved the problem of sorting our transparent planes by adding an extra vertex to them that effectively shifts the center of geometry, which Unity uses to sort by.
Lets say you have a plane with 4 vertices, and you want it to be sorted further back. Just add a 5th vertex in the center (not connected to the rest) and move it backwards.
Adding new vertices colser to the camera solved it for me (i add a little far triangle too small to be viewed)
Answer by Mortennobel · Jul 14, 2011 at 10:07 AM
I just got another idea that might help you. You could consider using orthographic camera. In this case the sorting is like you want (the down side is that you do not have a perspective in your image).
Alas, I need a perspective camera.
Anyway having an orthographic camera wouldn't help, the sort algorithm being independent of used projection matrix (the projection matrix uses x y and z values in camera's space to compute x and y values in screen's space, but z remains the same).
Answer by Graham-Dunnett · Jul 14, 2011 at 10:51 AM
If you think this is a bug in Unity, please submit a bug report from the Editor and include a project that demonstrates the issue. One of the QA team will take a look and work with Devs to figure out with you if this is really a bug or not. (Saying "it seems" is not sufficiently definitive!)
Please use the feedback site at feedback.unity3d.com if you want to request a new feature.
The idea is, I don't know if it's a bug. That's why I asks for someone more experienced than I am to confirm or not before I submit a bug report.
And since I cannot put my project on hold until a potential bug is fixed, I'm also searching for an alternate solution, hence the idea that maybe custom render queues can be added. As you probably have understood, the tweaking abilities of existing ones are not enough for my needs.
As for the "it seems"... well I cannot make any more assumption ! Looking at what is rendered, the only thing I can see is a front most transparent object rendered before another, when I think (and I may be wrong) it shouldn't. And since I obviously cannot see Unity's source code I am just able to infer what's happening.
Please tell me if my description is not precise enough so that I can try to improve it.
That said, thanks for the feature request page's URL. I will no doubt use it once someone confirms me that what I need is not currently feasible.
Since I did not have any feedback from Unity concerning this problem I'm reviving the topic.
Reading at the first sentence of the 'Depth Sorting' paragraph in this article : http://www.opengl.org/wiki/Transparency_Sorting, I can confirm that objects must be sorted using their Z depth.
Now if, as above renders tends to prove, Unity is sorting them using their distance to the camera, thats a bug that should be fixed.
Given that Unity's feedback page does not provide any feedback..., where should I ask for a fix now ?
Your answer
Follow this Question
Related Questions
Need help with opacity error 0 Answers
Understanding Render Targets and Render Textures. 1 Answer
Transparent Textures 1 Answer
Unlit/Transparent is best choice? 1 Answer
make a gameobject lerp transparency? 2 Answers