Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Alpha Problems

Discussion in 'Shaders' started by nm8shun, Aug 14, 2007.

  1. nm8shun

    nm8shun

    Joined:
    Jul 14, 2007
    Posts:
    476
    OK.....Since I'm prelighting everything in my scene, I tried turning off all the lights, and just turning on Ambient Light in the Render Settings.

    Unfortunately, I'm getting this (as shown in the attached screenshot). Basically you are seeing the extruded inner side of the smoke stack there. I'm using Alpha/Diffuse (the prebuilt one) as the shader, and am having a number of problems like that around this shape.

    How can I avoid this/fix it? Am I using the shaders incorrectly?
     

    Attached Files:

  2. nm8shun

    nm8shun

    Joined:
    Jul 14, 2007
    Posts:
    476
    Also notice the strangeness on the roof. There are a lot of shapes that sit atop the roof, but if the roof is behind the shape, it's invisible - the shapes only become visible, when the roof (also an Alpha/Diffuse shader) are not behind it.
     
  3. antenna-tree

    antenna-tree

    Joined:
    Oct 30, 2005
    Posts:
    5,324
    Alpha/Diffuse does not write to the Z Depth Buffer, that's why you're seeing the strange sorting problems in your scene where objects that should be in front get rendered behind.
     
  4. nm8shun

    nm8shun

    Joined:
    Jul 14, 2007
    Posts:
    476
    Are there any Alpha Shaders that are better (and work with Z Buffers) then?
     
  5. antenna-tree

    antenna-tree

    Joined:
    Oct 30, 2005
    Posts:
    5,324
    You can grab the Alpha/Diffuse shader source file from here and change the "Zwrite Off" on line 7 to "Zwrite On ", but that won't solve every situation... you'll still get some sorting issues. From what I understand getting Alphas to sort properly 100 percent of the time is really tricky. The best bet is to use Alphas sparingly and only with planes or simple geometry where the sorting is very straightforward (examples would be windows, iron gates etc). For straight binary alpha channels (Alpha Test, either you see through it or you don't) commenting out line 10 on the Alpha/Diffuse shader might help your problem.

    [Edit] Also the Two Pass Vegetation shader on the WIKI could help... it does the binary Alpha Test plus a second pass for 8-bit Transparency.
     
  6. Foxxis

    Foxxis

    Joined:
    Jun 27, 2006
    Posts:
    1,108
    I've had sorting problems too - and it is indeed tricky. I ended up "forcing" the sorting by adding offset geometry to the objects that were acting up.

    Basically, IIRC Unity sorts based on an objects calculated geometric center. This leads to a problem if the object is larger than surrounding objects and uses alpha, since in some cases the pretty rough sorting algorithm will return something that looks wrong. By adjusting the geometry of the "misbehaving object" you can force it to always be behind or in front of other geometry. The only way I found to trick unity in this way was by adding a polygon to the object in a location where it would shift the geometrical center enough. I made sure the normal was facing the right way so it would never be visible.

    However, this trick will only work well for you if the object you're having problems with is predictable, ie. you know it should always be behind or in front of certain objects. In my case it was a water plane which sorted weirdly, so it was easy to solve.

    Anyway, not sure this applies to your case, but I thought I'd add it since it can be a good workaround.

    To solve these problems in a good way, I'd really REALLY like some form of layer system for transparency - or a much improved sorting algorithm. My guess is that both these improvements would cost too much with current hardware. *sniff* :)
     
  7. NCarter

    NCarter

    Joined:
    Sep 3, 2005
    Posts:
    686
    I don't understand why that model needs an alpha shader in the first place, since it doesn't seem to have any transparent parts in the screenshot. My suggestion would be to use a non-alpha shader for the obviously opaque parts of the model and only use an alpha shader for pieces which have transparency (the windows?).

    This means more materials, and thus more rendering passes for this object, but I think that would still be cheaper than using blending for the entire mesh, since you have to pay the full cost for blending even if it results in opaque pixels. This is especially true if you're standing so close to it that it fills the screen, as this can use up a lot of fill rate.
     
  8. nm8shun

    nm8shun

    Joined:
    Jul 14, 2007
    Posts:
    476
    Thanks for the explanations everyone.

    THe model needs Alpha as I need to have it fade in and out at different times of the project. Without Alpha, no fade.
     
  9. nm8shun

    nm8shun

    Joined:
    Jul 14, 2007
    Posts:
    476
    Hey antenna tree...that Z Buffer trick worked perfectly! Thanks much! I guess I dont' understand why it's off in the first place - but turning it on fixed all my problems. Thanks again!
     
  10. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    The model does not need to use Alpha all the time, just when it is fading out. See Material.shader for a similar example. Using Alpha shaders only when they are actually needed will make things faster as well.

    The z buffer trick is off for Alpha shaders for one reason: it does not work with partially transparent models (i.e. 50% alpha). Z buffer is either written to, or not written to; that means the object either occludes other objects completely, or does not occlude them at all. The trick is perfect for trees, foliage and fences, where parts of the object are fully opaque or fully transparent. But it does not quite work for partially transparent things, or for fading out.
     
  11. nm8shun

    nm8shun

    Joined:
    Jul 14, 2007
    Posts:
    476
    Wow. Interesting. Thanks for the explanation.