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

Switching between regular alpha and alpha fade in shader pragma

Discussion in 'Shaders' started by cosmologosaurusrex, Oct 4, 2019.

  1. cosmologosaurusrex

    cosmologosaurusrex

    Joined:
    Jul 1, 2015
    Posts:
    50
    Hi! I want a glas like material that i can fade out to complete invisibility. I understand that fade lets me do that, but i want it to also be glas like at higher alpha values. I would want it to switch from

    #pragma surface surf Standard alpha
    to
    #pragma surface surf Standard alpha:fade
    when the alpha value goes below 0.2f.

    I dont think the shader allows any comparisons or logic in this part of the code.
    It would also be even better if the material could have two sliders, one for transparent, and one for fade.
    But i think the material can not be both transparent and have fade. Thx

    Edit: For now i just swap material with code at a certain alpha value.
     
    Last edited: Oct 5, 2019
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    You are correct, you cannot switch between those two alpha modes in a surface shader.

    However you can replicate both using
    alpha:premul
    (which is what
    alpha
    alone is actually using when you're also using the Standard shading model). Really the difference between
    alpha:fade
    and
    alpha:premul
    is in fade the alpha lowers the opacity of everything, where as premul only lowers the opacity of the albedo and the specular remains at full strength. So the solution is to use StandardSpecular instead of Standard and fade out the specular color to black below 0.2 alpha.

    Your basic StandardSpecular Surface shader looks like this:
    https://forum.unity.com/threads/create-new-specular-shader.427796/#post-2765505

    If you want to continue using the metallic workflow, you can use the built in DiffuseAndSpecularFromMetallic function.
    Code (csharp):
    1. half4 albedo = tex2D(_MainTex, IN.uv_MainTex);
    2. half4 metallicGloss = tex2D(_MetallicGlossMap, IN.uv_MainTex);
    3. half3 specColor = 0;
    4. half oneMinusReflectivity = 0;
    5. o.Albedo = DiffuseAndSpecularFromMetallic(albedo, metallicGloss.r, specColor, oneMinusReflectivity);
    6. o.Specular = specColor;
    To fade out, rescale the alpha so that 0.2 to 0.0 is a 1.0 to 0.0 range and multiply the specular color:
    Code (csharp):
    1. o.Specular = specColor * saturate(o.Alpha / 0.2);
    The result will be the diffuse lighting will get a little bit brighter while it's fading out, so it's not quite exactly the same, but it shouldn't be noticeable. Alternatively you might be able to use a final color modifier and do:
    Code (csharp):
    1. void customFade(Input IN, SurfaceOutputStandardSpecular o, inout half4 color)
    2. {
    3.     color.rgb *= saturate(o.Alpha / 0.2);
    4. }
    But I can't remember exactly when the final color modifier runs and that may cause the shader to go prematurely dark when fading out instead of ever so slightly too bright.
     
  3. cosmologosaurusrex

    cosmologosaurusrex

    Joined:
    Jul 1, 2015
    Posts:
    50
    GlossSpecArm.png

    Thx So much. You are an absolute unit! I have seen you on other threads and your posts are so usefull :)
     
    bgolus likes this.