Search Unity

Could I get some help implementing Alpha Transparency on a Specular Vertex Shader?

Discussion in 'Shaders' started by manny003, Jul 6, 2019.

  1. manny003

    manny003

    Joined:
    Mar 18, 2014
    Posts:
    67
    I have the following mobile shader I wrote that renders objects with specular highlight and using the color from the mesh's vertex color (this is a requirement). I can also apply the shader Color property as a tint. I have all of these working.

    However, I also need to apply the Alpha from the Color Property to fade the object in and out but it's not working.

    Can someone help me out?

    Thanks in advance.

    Code (csharp):
    1.  Shader "Custom/MySpecularVertex" {
    2.     Properties {
    3.         _Color ("Color", Color) = (1,1,1,1)
    4.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    5.         _Shininess ("Shininess", Range (0.03, 1)) = 0.078125
    6.     }
    7.     SubShader {
    8.         Tags {
    9.             "RenderType" = "Transparent"
    10.             "Queue" = "Transparent"
    11.         }
    12.         LOD 250
    13.    
    14. CGPROGRAM
    15.         // Physically based Standard lighting model, and enable shadows on all light types
    16.         //#pragma surface surf Standard fullforwardshadows
    17.          #pragma surface surf MobileBlinnPhong exclude_path:prepass nolightmap noforwardadd halfasview
    18.  
    19.         // Use shader model 3.0 target, to get nicer looking lighting
    20.         #pragma target 3.0
    21.         inline fixed4 LightingMobileBlinnPhong (SurfaceOutput s, fixed3 lightDir, fixed3 halfDir, fixed atten)
    22.         {
    23.             fixed diff = max (0, dot (s.Normal, lightDir));
    24.             fixed nh = max (0, dot (s.Normal, halfDir));
    25.             fixed spec = pow (nh, s.Specular*128) * s.Gloss;
    26.            
    27.             fixed4 c;
    28.             c.rgb = (s.Albedo * _LightColor0.rgb * diff + _LightColor0.rgb * spec) * (atten*2);
    29.             c.a = 0.0;
    30.             return c;
    31.         }
    32.  
    33.         sampler2D _MainTex;
    34.         struct Input {
    35.             float2 uv_MainTex;
    36.             float4 vcolor : COLOR;
    37.         };
    38.         half _Shininess;
    39.         fixed4 _Color;
    40.         void surf (Input IN, inout SurfaceOutput o) {
    41.             // Albedo comes from a texture tinted by color
    42.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    43.             //o.Albedo = c.rgb * IN.vcolor;
    44.             o.Albedo = c.rgb * IN.vcolor;
    45.             o.Gloss = c.a;
    46.             o.Alpha = _Color.a;
    47.             o.Specular = _Shininess;
    48.  
    49.         }
    50. ENDCG
    51.     }
    52.     FallBack "Mobile/VertexLit"
    53. }
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    7,002