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

Opaque Glossiness on Transparent Shader

Discussion in 'Shaders' started by Emre, May 10, 2013.

  1. Emre

    Emre

    Joined:
    Jul 8, 2012
    Posts:
    21
    Hi, I am trying to write a simple sea shader for a top down game. I am using the alpha channel of the MainTex to control the water transparency to create a deepness - shallowness effect. What I want is find a way to add glossiness to o.Alpha to make the water shiny even on the most transparent areas.

    Anyone know how to do this?


    Code (csharp):
    1.  
    2. Shader "Custom/Water" {
    3. Properties {
    4.     _Color ("Main Color", Color) = (1,1,1,1)
    5.     _MainTex ("Base (RGB) Opacity(A)", 2D) = "white" {}
    6.     _BumpMap ("Normalmap", 2D) = "bump" {}
    7.     _Reflection ("Reflection", 2D) = "gray" {}
    8.     _Reflectivity ("Reflectivity", Range (0.0, 1.0)) = 1.0
    9.     _Shininess ("Shininess", Range (0.03, 3)) = 0.078
    10.     _WaveSpeed ("Wave speed (map1 x,y; map2 x,y)", Vector) = (0.027,0.032,-0.029,-0.03)
    11.     _Scale ("Scale", Range (1.0, 0.001)) = 0.1
    12. }
    13.  
    14. SubShader {
    15.     Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" } //Transparent
    16.     LOD 400
    17.    
    18.     CGPROGRAM
    19.     #pragma surface surf MobileBlinnPhong alpha exclude_path:prepass nolightmap noforwardadd halfasview novertexlights noambient
    20.    
    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 = dot(s.Normal.xyz, halfDir);
    30.         return c;
    31.     }
    32.  
    33.     sampler2D _MainTex;
    34.     sampler2D _BumpMap;
    35.     sampler2D _Reflection;
    36.     fixed4 _Color;
    37.     half _Shininess;
    38.     fixed _Reflectivity;
    39.     fixed4 _WaveSpeed;
    40.     fixed _Scale;
    41.    
    42.     struct Input {
    43.         float2 uv_MainTex;
    44.         float3 worldPos;
    45.         float4 screenPos;
    46.     };
    47.    
    48.     void surf (Input IN, inout SurfaceOutput o) {
    49.  
    50.         float2 screenUV = IN.screenPos.xy / IN.screenPos.w;
    51.         o.Albedo = _Color.rgb + (tex2D (_Reflection, screenUV).rgb * _Reflectivity);
    52.        
    53.         o.Gloss = _Color.a;
    54.         o.Specular = _Shininess;
    55.        
    56.         float2 waveUV = IN.worldPos.xz * _Scale;
    57.         o.Normal = UnpackNormal(tex2D(_BumpMap, ( waveUV + (_WaveSpeed.xy * _Time.y) )));
    58.         o.Normal += UnpackNormal(tex2D(_BumpMap, 0.5 * ( waveUV + (_WaveSpeed.zw * _Time.y) )));
    59.         o.Normal = normalize(o.Normal);
    60.        
    61.         o.Alpha += tex2D (_MainTex, IN.uv_MainTex).a;
    62.     }
    63.     ENDCG
    64. }
    65.     FallBack "Mobile/Diffuse"
    66. }
    67.  
    68.  
    69.