Transparent over transparent

Discussion in 'ShaderLab' started by qholmes, Jul 4, 2011.

  1. qholmes

    qholmes

    Member

    Joined:
    Sep 17, 2010
    Messages:
    162
    I have a common issue i think with a transparent shader overtop of a transparent shader... when you fade out the top object the object underneath all of a sudden appears.. some kind of depth sort issue or something..

    Would it be possible to have a shader that switches between transparent and not based on weather the alpha value is 1 or not?

    I can write some logic to switch the material right before it fades but that is more of a patch then a solution although i think it is the common solution.

    I think the switching materials is also a performance thing... would the auto switching one be good for performance or bad?

    ??

    Q
    Last edited: Jul 4, 2011
  2. Alvarus

    Alvarus

    New Member

    Joined:
    Apr 10, 2009
    Messages:
    203
  3. qholmes

    qholmes

    Member

    Joined:
    Sep 17, 2010
    Messages:
    162
    Hmmmm will try that thanks..

    Do transparent shaders that are totally opaque cost more to render? I thought i had read that somewhere.

    Q
  4. qholmes

    qholmes

    Member

    Joined:
    Sep 17, 2010
    Messages:
    162
    Hey that worked perfect! just layer the transparent shader properly with the Queue numbers and you are good to go. It means a few more materials but it works smooth.

    Would still like to find out the performance hits of transparent shaders etc... i am sure it is somewhere in the docs.... speaking of Documentation... why is there no search in the main manuals... only in the scripting reference one?

    Q
  5. Noisecrime

    Noisecrime

    Member

    Joined:
    Apr 7, 2010
    Messages:
    1,265
    With regard to transparent shaders, they do have more of a performance hit than a non-transparent shader, as they can no longer write to the zbuffer (unless using alpha testing not blending). This means you'll get no early outs of the rendering as the card doesn't know if any part of the triangle you are drawing is actually obscured. Thereby over-draw becomes an issue and then there is the blending itself. In general though if you need a transparent shader, then you need it and there is no point worrying about performance. However you need to be aware that over-draw can become a potential issue if you have lots of overlapping polygons.

    I'm not sure if there are any optimisations in the gpu for when you say have a fully opaque or fully transparent texture in a transparent shader. In other words if there is any optimisation for when the texture would make no difference ( 0% opacity) or would simply over-write everything (100% opacity), though regardless you'll still suffer from lack of writing to the zbuffer.
  6. qholmes

    qholmes

    Member

    Joined:
    Sep 17, 2010
    Messages:
    162
    I was just reading through the shader docs actually...

    i guess i was thinking that it would be smart to have a shader that just checked to see if the Opacity was at 100% and if so then just use a normal shader instead... get all the benefits of the opaque shader back.. But i dont know about shaders so maybe that just does not make any sense.. I have tons of overlapping polys.. my project is a machine or instrument really and i am looking at parts of it and having parts move out of the way and some fade out to see things underneath.. so this is why i ran into this problem really quick... i may have 5 layers of things over top of each other that i want to fade out to see what is underneath. But in general running they are opaque so i thought for performance switching to a normal shader would be better..

    I can set my project up to switch the shader in code no problem i think but was wondering if there was a better way or if it was even worth it

    Thanks

    Q
  7. Noisecrime

    Noisecrime

    Member

    Joined:
    Apr 7, 2010
    Messages:
    1,265
    Switching shaders would be the simplest and most appropriate solution for your case. As long as you're not changing shaders every frame I would imagine it would help as long as overdraw is an issue. Honestly thats the sort of thing you'd need to profile.

    I think there might be some alternatives, but heavily dependant upon exact nature of your project.For example you could simply hide the lower layers if they aren't going to be visible, thus avoid rendering them completely.

    It really comes down to render order and over-draw. For example if your layers are rendered back to front (as they would be for transparent shaders) then if the top layer is opaque that should help, as the transparent shaders will be rendered after non-transparent ones, thus the zbuffer will be filled and those lower layers wont be rendered.

    FYI, as far as i'm aware Unity uses the standard method of first collecting all materials to be rendered, then rendering non-transparent meshes first, populating the zbuffer, then rendering transparent meshes, sorted back to front to ensure correct render order. At least for forward rendering.

    Again though, i wouldn't worry too much at this stage, unless you are already getting performance problems.
  8. qholmes

    qholmes

    Member

    Joined:
    Sep 17, 2010
    Messages:
    162
    Cool thanks!!

    Q
  9. qholmes

    qholmes

    Member

    Joined:
    Sep 17, 2010
    Messages:
    162
    I am just playing with this again....

    I managed to get my transparent fighting issue to calm down if i use two different shaders but if i use different materials but same shaders then it does not work of course.. So instead of having to make a bunch of shaders could i expose that value to the Material so that it could set it there? Wouldn't that make more sense?

    Q
  10. qholmes

    qholmes

    Member

    Joined:
    Sep 17, 2010
    Messages:
    162
    I also see that i can set the Material.RenderQueue in script so is there a way to just set it absolute in the inspector instead? I dont need to do it on the fly.

    Q