Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  4. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  5. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  6. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Particles behind meshes?

Discussion in 'Shaders' started by Dantus, Aug 8, 2011.

  1. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,668
    I would like to use a shader for the particle system and meshes. The problem is that the meshes are always drawn in front of the particles. Can someone tell me, how to avoid that?

    Code (csharp):
    1. Shader "TransparentIssue" {
    2.     Properties {
    3.         _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    4.     }
    5.    
    6.     Category {
    7.         Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
    8.         Alphatest Greater 0
    9.         Lighting Off
    10.         ColorMask RGB
    11.         Cull Off
    12.         ZWrite Off
    13.         Fog {Mode off}
    14.    
    15.         SubShader {
    16.             Pass {
    17.                 Lighting Off
    18.                 BindChannels {
    19.                     Bind "Vertex", vertex
    20.                     Bind "Texcoord", texcoord
    21.                     Bind "Color", color
    22.                 }
    23.  
    24.                 Blend SrcAlpha OneMinusSrcAlpha
    25.                 SetTexture [_MainTex] {
    26.                     combine primary * texture
    27.                 }
    28.             }
    29.         }
    30.     }
    31. }
     
  2. Daniel-Brauer

    Daniel-Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,331
    If both your particle system and your mesh are using that shader, then they will be drawn back to front. This order is imposed by virtue of being in the Transparent queue.

    If you want to force draw order, you'll have to put the mesh's or the particle system's material in a different Render Queue.
     
  3. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,668
    I also expected that behaviour. Unfortunately the particle system is always drawn behind the mesh, even if it is closer to the camera. I was looking in the reference, if something is mentioned, that the particle renderer is handled differently. But I found nothing.
     
  4. tomvds

    tomvds

    Joined:
    Oct 10, 2008
    Posts:
    1,028
    Perhaps the bounding volume of your mesh is larger than you think, placing it's center nearer to the camera than the particles. If that is the problem, you can force the correct ordering by placing the particle material in the transparent+1 queue (note that that may mess things up if you have other transparent objects besides the particles and the mesh).
     
  5. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,668
    tomvds, that's a good point. I will check that!
     
  6. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,668
    No, that's not the case. I just checked all the values and calculated the distance of the camera to the (transformed) bounding volume centers. Everything is okay.
    The centers of the bounding volumes of the particle and mesh renderers are correct, too.
     
  7. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,668
    I reproduced the issue in a very simple example package. The scene contains two cameras. One should show the mesh in front of the particles, the other one should show the opposite. But both show draw the mesh in front of the particles. If someone confirms, that there is a problem, I would fill a bug report. Thanks.
     

    Attached Files:

  8. BIG-BUG

    BIG-BUG

    Joined:
    Mar 29, 2009
    Posts:
    454
    This is a general problem with overlaying semi-transparent geometry, not a bug.
    Is it necessary that your mesh uses alpha also? With solid geometry there would be no problem.

    What is the effect you are trying to achieve? Maybe there is a solution...
     
  9. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,668
    This one was just for demonstration purposes. I am using it for a custom particle system. If the situation is like that, it can not be mixed with Unity's particle system at all, which is not really good. So there is no solution unfortunately.
    And it is not a semi-transparent geometry problem, as those objects are intended to be rendered from back to front, exactly as Daniel Brauer described it. So it actually is a bug from my point of view, as there is nothing written (or at least I could not find it), that the particle system has a special role in the rendering queue.
     
  10. Martin-Kraus

    Martin-Kraus

    Joined:
    Feb 18, 2011
    Posts:
    617
    The problem appears to be the "Receive Shadows" option. Try to deactivate it. Also, you should use sorted billboards.
     
  11. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,668
    Deactivating "Receive Shadows" has no impact. Same for sorted billboards. The mesh is still rendered in front of the particle system, even if it is behind it.
     
  12. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,668
    That's by far the coolest bug I ever found in Unity :)! If the box has a scale of (1, 1, 1), the rendering issue happens. For any other scale, no problem!

    Edit: You may also change the scale of the particle system to avoid the problem.
    Edit: Bug always occures, if both have the same scale :)
    Edit: Bug reported (Case 414572)
     

    Attached Files:

    Last edited: Aug 9, 2011
  13. Martin-Kraus

    Martin-Kraus

    Joined:
    Feb 18, 2011
    Posts:
    617
    Well, it worked for me. :)
     
  14. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,668
    Martin Kraus, maybe it has the same effect as changing a scale. I expect you own Unity Pro, such that it has an effect for you. I only have Unity. Both make no sense.

    Edit: I added it to the bug report.
     
    Last edited: Aug 9, 2011
  15. tomvds

    tomvds

    Joined:
    Oct 10, 2008
    Posts:
    1,028
    After seeing the project, I agree this is a bug. Deactivating receive shadows fixed it for me as well (Unity pro), but I do not see how that would make it less of a bug ;).
     
  16. Martin-Kraus

    Martin-Kraus

    Joined:
    Feb 18, 2011
    Posts:
    617
    I agree, it doesn't. :) BTW I'm using the free version of Unity. (Maybe one needs to restart the particle system (by starting the game mode) to see that it has an effect.)
     
  17. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,668
    The bug is getting more and more interesting :)
     
  18. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,668
    Martin Kraus, you are right. I disabled it for both, the mesh and the particle system. In that case the same issue occures again. You only mentioned one. If both have the same settings it doesn't seem to work properly.
     
  19. Martin-Kraus

    Martin-Kraus

    Joined:
    Feb 18, 2011
    Posts:
    617
    OK, I wasn't aware of that: I just deactivated it for the particle system. :)
     
  20. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,668
    It is even stranger, if you modify the distance to the camera, there is a certain point at which it may switch between correct and incorrect.

    To the Unity employee who has to fix this bug: I am going to offer you a drink if we ever meet us! :)
     
  21. Gua

    Gua

    Joined:
    Oct 29, 2012
    Posts:
    222
    Got same issue. Tried everything from this thread. Nothing helped.


    Update: The only thing that helps a bit is setting Soft Particles factor to 3
     
  22. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,668
    @Gua, since this is a very old report, it might be worth to submit another bug report, because I can also not find it in the issue tracker.
     
  23. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    4,504
    Unity's soft particle settings don't work well for small scale stuff. The ranges it allows for is mainly good for third person scales like Angry Bots. It's kind of useless for larger or smaller scales. I would suggest turning off soft particles or write your own particle shaders that don't use it.
     
  24. josh_s_dev

    josh_s_dev

    Joined:
    Oct 20, 2015
    Posts:
    14
    @bgolus Thanks! Turning off soft particles (by using the Mobile/Particles/Alpha Blended rather than Particles/Alpha Blended) brought all my particles back that had disappeared when they went behind alphatest translucent geometry!
     
  25. Aldo-V

    Aldo-V

    Joined:
    May 22, 2013
    Posts:
    66
    In my case I turned off Masking in Particle Renderer to solve this issue.