• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by mdeletrain · Jul 13, 2011 at 03:41 PM · transparencytransparentsortingrenderqueue

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.

W$$anonymous$$le 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, w$$anonymous$$le 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.

Schema

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 t$$anonymous$$s feature be added ?

Thanks for your help !

Comment
colinf
syclamoth
Bocaj
steinbitglis
devin8
cregox
numberkruncher
cutjules
joethephish

People who like this

9 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

6 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by steinbitglis · Jun 23, 2012 at 09:15 PM

I got t$$anonymous$$s mail from Unity yesterday:

Hi,

Good news! T$$anonymous$$s 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.Ortograp$$anonymous$$c 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.Orthograp$$anonymous$$c
Comment
Wolfram
numberkruncher
joethephish

People who like this

3 Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mdeletrain · Jun 25, 2012 at 08:32 AM 0
Share

Yeap, I"ve received the same mail too. Anyway thanks for having forwarded it here !

avatar image shopguy · Apr 24, 2013 at 02:33 AM 0
Share

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).

avatar image Jeff-Kesselman · Jan 03, 2014 at 07:20 AM 0
Share

you can always write a little script to attach to your camera and use the ExecuteInEditMode attribute http://docs.unity3d.com/Documentation/ScriptReference/ExecuteInEditMode.html

avatar image cutjules · Mar 11, 2016 at 04:17 PM 0
Share

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 ?

avatar image cutjules cutjules · Mar 17, 2016 at 03:07 PM 0
Share

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/Manual/CamerasOverview.html

avatar image

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. T$$anonymous$$s 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 t$$anonymous$$s problem in a general way.

See:

http://unity3d.com/support/documentation/Components/SL-SubshaderTags.html http://unity3d.com/support/documentation/Components/SL-PassTags.html

Comment
Eric5h5

People who like this

1 Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mdeletrain · Jul 13, 2011 at 05:16 PM 0
Share

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 instead 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.

avatar image Mortennobel · Jul 13, 2011 at 05:21 PM 0
Share

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.

avatar image mdeletrain · Jul 13, 2011 at 05:32 PM 0
Share

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.

avatar image Mortennobel · Jul 13, 2011 at 05:57 PM 0
Share

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).

avatar image

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 s$$anonymous$$fts the center of geometry, w$$anonymous$$ch Unity uses to sort by.

Comment
Bunny83

People who like this

1 Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jjrobert · Sep 06, 2012 at 05:09 PM 0
Share

Could you elaborate on how this is done? Thanks :)

avatar image Orion · Sep 07, 2012 at 09:40 AM 0
Share

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.

avatar image Docboy · Nov 02, 2013 at 12:55 AM 0
Share

Adding new vertices colser to the camera solved it for me (i add a little far triangle too small to be viewed)

avatar image

Answer by Mortennobel · Jul 14, 2011 at 10:07 AM

I just got another idea that might help you. You could consider using orthograp$$anonymous$$c camera. In t$$anonymous$$s case the sorting is like you want (the down side is that you do not have a perspective in your image).

Comment

People who like this

0 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mdeletrain · Jul 14, 2011 at 10:13 AM 1
Share

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).

avatar image

Answer by Graham-Dunnett · Jul 14, 2011 at 10:51 AM

If you t$$anonymous$$nk t$$anonymous$$s 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 t$$anonymous$$s 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.

Comment

People who like this

0 Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mdeletrain · Jul 14, 2011 at 11:14 AM 0
Share

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.

avatar image mdeletrain · Nov 21, 2011 at 11:05 AM 0
Share

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 ?

  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make an object transparent in script? 3 Answers

Objects behind transparent objects (sometimes) not drawn. 0 Answers

alpha sorting issues 1 Answer

Why does the 3D Text shader show specular at certain angles? 2 Answers

Rendering an entire object as transparent 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges