Search Unity

Procedural mesh with Fade Shader rendering surface in wrong order

Discussion in 'General Graphics' started by Insurecti, Jun 26, 2019.

  1. Insurecti

    Insurecti

    Joined:
    Apr 13, 2018
    Posts:
    5
    Hello people!

    I'm making an hexagonal grid map with procedural mesh scripting. This is my first play with procedural meshes.
    Thing is: it has a gradient-to-transparent texture to create a "fading into the deep" effect on the bottom of it, so the Standard-Opaque shader won't do.

    I thought Standard-Fade would work, but it gets all messed up. Tried all available shaders and rendering modes, and played with the configurations, but nothing does the trick.

    Here's it under Standard-Opaque, just fine:
    upload_2019-6-26_19-51-18.png

    Now under Standard-Fade:
    upload_2019-6-26_19-53-8.png

    It really gets messed up when looking from the higher sides. The lower surfaces are being rendered on top of the higher ones:
    upload_2019-6-26_19-54-8.png upload_2019-6-26_19-56-32.png

    Can someone please advise me on this? I'm really dummy with Shaders and why it is behaving like this is a total mystery to me xD
     
  2. Insurecti

    Insurecti

    Joined:
    Apr 13, 2018
    Posts:
    5
    Well... Tried some new sets of keywords on Google and found this:
    https://docs.unity3d.com/Manual/SL-CullAndDepth.html

    Look under "Transparent shader with depth writes".

    The transparent Shaders ignore Z-Order. Just write a new Shader, using the code provided on the link. It will enable Z-Order and call the standard transparent Shader. Worked like a charm here
     
  3. bart_the_13th

    bart_the_13th

    Joined:
    Jan 16, 2012
    Posts:
    498
    why not, instead of fading to transparent, you fade to background color?
     
    richardkettlewell likes this.
  4. Insurecti

    Insurecti

    Joined:
    Apr 13, 2018
    Posts:
    5
    Interesting... Did not even knew there was such an option.

    How would I do this? :oops: Instead of fading to transparent, fade to the same color as background? If so, I would not be able to use it, since I plan on having a lot of color customization, including the map and background, operating through material colour manipulation alone.
     
  5. bart_the_13th

    bart_the_13th

    Joined:
    Jan 16, 2012
    Posts:
    498
    Since I don't think you can get camera background color from shader, you can set the background color in the shader from script using material.SetColor("_BackgroundColor", camera.backgroundColor).
    Of course, you'll need a custom shader for this, but I think it shouldn't be too hard to modify from you current shader...
    Maybe changing/adding from
    Code (csharp):
    1.  
    2. col = tex2D(_MainTex, uv);
    3.  
    to
    Code (csharp):
    1.  
    2. col = tex2D(_MainTex, uv);
    3. col.rgb = lerp(col.rgb, _BackgroundColor, col.a);
    4. col.a = 1;
    5.