Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

why can't be transparent?

Discussion in 'Shaders' started by liuyu, May 5, 2015.

  1. liuyu

    liuyu

    Joined:
    Oct 14, 2014
    Posts:
    28
    I have write a shader:
    Code (CSharp):
    1. Shader "Custom/Test_NewShader" {
    2.     Properties {
    3.         _Color ("Color", Color) = (1,1,1,1)
    4.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    5.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    6.         _Metallic ("Metallic", Range(0,1)) = 0.0
    7.     }
    8.     SubShader {
    9.         Tags { "RenderType"="Transparent" }
    10.         LOD 200
    11.        
    12.         Blend SrcAlpha OneMinusSrcAlpha
    13.        
    14.         CGPROGRAM
    15.         #pragma surface surf Standard fullforwardshadows
    16.    
    17.         sampler2D _MainTex;
    18.  
    19.         struct Input {
    20.             float2 uv_MainTex;
    21.         };
    22.  
    23.         half _Glossiness;
    24.         half _Metallic;
    25.         fixed4 _Color;
    26.  
    27.         void surf (Input IN, inout SurfaceOutputStandard o) {
    28.             // Albedo comes from a texture tinted by color
    29.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    30.             o.Albedo = c.rgb;
    31.             // Metallic and smoothness come from slider variables
    32.             o.Metallic = _Metallic;
    33.             o.Smoothness = _Glossiness;
    34.             o.Alpha = c.a;
    35.         }
    36.        
    37.         ENDCG
    38.     }
    39.     FallBack "Diffuse"
    40. }
    41.  
    I'm using " Blend SrcAlpha OneMinusSrcAlpha ", but why the object using the Test_NewShader can't be transparent?
     
  2. UnityGuillaume

    UnityGuillaume

    Unity Technologies

    Joined:
    Mar 16, 2015
    Posts:
    123
    Surface shader actually don't use the Blend keyword (well they can but not the easiest way), but an alpha directive.

    Replace

    Code (CSharp):
    1.  #pragma surface surf Standard fullforwardshadows
    by

    Code (CSharp):
    1.  #pragma surface surf Standard fullforwardshadows alpha
    that way surface shader conserve alpha all along and set all the blending/zwrite state etc.

    This will assume premultiplied alpha. If you want "old style" alpha fading, use "alpha:fade"
     
    torvald-mgt likes this.
  3. liuyu

    liuyu

    Joined:
    Oct 14, 2014
    Posts:
    28
    Oh, Yes! It's correct! But I have two question:
    1, why the Surface shader don't use blend keyword? and now, if I coun't use "#pragma surface surf Standard fullforwardshadows alpha", how can I make object to be alpha? Or must using the keyworld: alpha?
    2, The object using the shader is alpha, but it's shadowcast is still not alpha. how to make it's shadowcast is hollow out by alpha is bigger than alpha ref (5).
     
  4. liuyu

    liuyu

    Joined:
    Oct 14, 2014
    Posts:
    28

    The shadowcaster is wrong。how make it correct? It can using alphat test in shadow cast。
     
  5. UnityGuillaume

    UnityGuillaume

    Unity Technologies

    Joined:
    Mar 16, 2015
    Posts:
    123
    Shadowcasting pass try to find a subshader with a certain tag ("LightMode" = "ShadowCaster" ) which is usually a very simple shader rendering just depth.
    Your shader obviously don't have that tags, so it will try to find it in the fallback. And your fallback is "Diffuse" which don't have Alpha cutout.

    So change your fallback to "Legacy Shaders/Transparent/Cutout/VertexLit" That shader contain an Alpha Cutout shader casting pass (you can check it in the builtin shader here https://unity3d.com/get-unity/download/archive just click on your plateform and choose "builtin shader". It's the one called AlphaTest-VertexLit.shader)

    /!\ Don't forget to have a Properties called _Cutoff so the shader know what alpha value to cutoff. If you want to use another parameter, just copy pass the vertex lit shadercaste CGPROGRAM in a subshader with the tag!



    And surface shader could use the blend keyword, it's just that by default surface shader output 1 as alpha (for diverse reason of optimisation & pipeline). alpha keyword actually set everything needed for alpha (ZWrite, Queue & Blend) and keep the alpha you give it.

    if you want to use Blend in a "classic" shader :

    Tags { "Queue" = "Transparent" } // or "Queue"="AlphaTest" for Alpha Test Cutout
    ZWrite Off
    Blend SrcBlend DestBlend

    and to use THAT is in a surface shader, add "keepalpha" to the #pragma surface it tell the surface shader to NOT output 1 and keep whatever alpha you put there.
     
    Sparrowfc likes this.