Search Unity

TextShader fog newbie question

Discussion in 'Shaders' started by cecarlsen, Apr 4, 2008.

  1. cecarlsen

    cecarlsen

    Joined:
    Jun 30, 2006
    Posts:
    863
    Hi forum

    This should be easy...

    I want to create a shader that can display self-ilumin alpha textures that are influenced by fog. Like the build in TextShader, effected by fog. On the screen-shot below, the far away dots are rendered on top of frontmost dots and I can't figure out why that is. I am new to shaders, but I have read the Unity shader documents.

    Thanks
    ~ce

    Code (csharp):
    1. Shader "SelfIluminAlphaFogged" {
    2. Properties {
    3.     _MainTex ("Texture", 2D) = "white" {}
    4.     _Color ("Color", Color) = (1,1,1,1)
    5. }
    6.  
    7. SubShader {
    8.     Lighting Off
    9.     Cull Back
    10.     ZTest Always
    11.     ZWrite Off
    12.     Blend SrcAlpha OneMinusSrcAlpha
    13.     Pass {
    14.         Color [_Color]
    15.         SetTexture [_MainTex] {
    16.             combine primary, texture * primary
    17.         }
    18.     }
    19. }
    20. }
     

    Attached Files:

  2. ProtonOne

    ProtonOne

    Joined:
    Mar 8, 2008
    Posts:
    406
    I just tried out the following, seems to be what you are looking for.

    The "ZWrite Off" causes your far away particles to possibly render in front of your close particles. You can turn on sorting for the particlesystem, but then you would have the same problem with particles and your text.

    Instead I added in the "Alphatest Greater [_Cutoff]" (and the extra property). This prevents it from writing to the depthbuffer when the alpha drops below 0.5 which seems to work good.


    Code (csharp):
    1. Shader "GUI/Fog Font" {
    2. Properties {
    3.     _MainTex ("Font Texture", 2D) = "white" {}
    4.     _Color ("Text Color", Color) = (1,1,1,1)
    5.     _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
    6. }
    7.  
    8. SubShader {
    9.     Tags {"Queue" = "Transparent"}
    10.     Lighting Off
    11.     Cull Off
    12.     //ZTest Always
    13.     //ZWrite Off
    14.     //Fog { Mode Off }
    15.     Alphatest Greater [_Cutoff]
    16.     Blend SrcAlpha OneMinusSrcAlpha
    17.     Pass {
    18.         Color [_Color]
    19.         SetTexture [_MainTex] {
    20.             combine primary, texture * primary
    21.         }
    22.     }
    23. }
    24. }
     

    Attached Files:

  3. cecarlsen

    cecarlsen

    Joined:
    Jun 30, 2006
    Posts:
    863
    Thanks Proton, it makes sense ... but now the anti-aliasing is gone so edges are jagged and the text starts jittering, apparently because of the "Alphatest Greater [_Cutoff]". Why not use the alpha channel in the texture to make the cut off? I wish I knew how.

    ~ce
     

    Attached Files:

  4. ProtonOne

    ProtonOne

    Joined:
    Mar 8, 2008
    Posts:
    406
    You can fix the aliasing problem with the particles by going to your ParticleRenderer and changing "Stretch Particles" from "Billboard" to "Sorded Billboard"

    The particles will now look fine, but where they intersect with the text will be a problem sometimes.


    The alphatest determines if the depth buffer is written to. It does use your alpha channel, but can only either write, or not write. That is why the anti-aliased parts get written to the screen the depth buffer causing them to look ugly.

    Try adjusting your font to be trilinear in the texture and crank up the ansio level, that may help.
     
  5. cecarlsen

    cecarlsen

    Joined:
    Jun 30, 2006
    Posts:
    863
    Thanks Proton, it works well with Sorted Billboard so far. Learning a lot here =)