Search Unity

SkinShader, How To add Transparency Properties.

Discussion in 'Shaders' started by ganon, Nov 23, 2009.

  1. ganon

    ganon

    Joined:
    Mar 20, 2009
    Posts:
    22
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    What problems are you having in particular?
     
  3. ganon

    ganon

    Joined:
    Mar 20, 2009
    Posts:
    22
    Hi andeeee.
    I need to apply transparency effect on SkinSpecular.shader:

    Code (csharp):
    1.  
    2. Shader "Skin/Specular" {
    3.  
    4. Properties {
    5.     _Color ("Main Color", Color) = (1,1,1,1)
    6.     _SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 1)
    7.     _Shininess ("Shininess", Range (0.01, 1)) = 0.078125
    8.     _MainTex ("Base (RGB) Gloss (A)", 2D) = "white" {}
    9.     _Wrap ("Wrap lighting", Range(0,1)) = 0.5
    10.     _RimColor ("Rim Color", Color) = (0.26,0.19,0.16,0.0)
    11.     _RimPower ("Rim Power", Range(0.5,8.0)) = 3.0
    12. }
    13.  
    14. // ------------------------------------------------------------------
    15. // Fragment program
    16.  
    17. SubShader {
    18.     Blend AppSrcAdd AppDstAdd
    19.     Fog { Color [_AddFog] }
    20.     TexCount 4
    21.    
    22.    
    23.     // Ambient pass
    24.     // This does ambient + rim lighting. Note that if you have vertexlit
    25.     // lights in the scene, then standard vertex lighting will be used instead,
    26.     // so you'll lose rim lighting.
    27.     Pass {
    28.         Name "BASE"
    29.         Tags {"LightMode" = "PixelOrNone"}
    30. CGPROGRAM
    31. #pragma fragment frag
    32. #pragma vertex vert
    33. #pragma fragmentoption ARB_fog_exp2
    34. #pragma fragmentoption ARB_precision_hint_fastest
    35. #include "UnityCG.cginc"
    36.  
    37. struct v2f {
    38.     V2F_POS_FOG;
    39.     float2    uv;
    40.     float3    viewDir;
    41.     float3    normal;
    42. };
    43.  
    44. float4 _MainTex_ST;
    45.  
    46. v2f vert (appdata_base v)
    47. {
    48.     v2f o;
    49.     PositionFog( v.vertex, o.pos, o.fog );
    50.     o.normal = v.normal;
    51.     o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
    52.     o.viewDir = ObjSpaceViewDir( v.vertex );
    53.     return o;
    54. }
    55.  
    56. uniform float4 _Color;
    57. uniform float4 _RimColor;
    58. uniform float _RimPower;
    59.  
    60. uniform sampler2D _MainTex;
    61.  
    62. float4 frag (v2f i) : COLOR
    63. {
    64.     i.normal = normalize(i.normal);
    65.     i.viewDir = normalize(i.viewDir);
    66.     half4 texcol = tex2D( _MainTex, i.uv );
    67.    
    68.     // rim factor
    69.     float rim = 1.0 - saturate(dot( i.normal, i.viewDir ));
    70.    
    71.     half3 ambient = texcol.rgb * _PPLAmbient.rgb * 2;
    72.     half3 rimcolor = pow( rim, _RimPower ) * _RimColor.rgb;
    73.     return float4( ambient + rimcolor, texcol.a * _Color.a );
    74. }
    75. ENDCG
    76.     }
    77.    
    78.    
    79.     // Vertex lights
    80.     Pass {
    81.         Name "BASE"
    82.         Tags {"LightMode" = "Vertex"}
    83.         Lighting On
    84.         Material {
    85.             Diffuse [_Color]
    86.             Emission [_PPLAmbient]
    87.             Specular [_SpecColor]
    88.             Shininess [_Shininess]
    89.         }
    90.         SeparateSpecular On
    91.  
    92. CGPROGRAM
    93. #pragma fragment frag
    94. #pragma fragmentoption ARB_fog_exp2
    95. #pragma fragmentoption ARB_precision_hint_fastest
    96.  
    97. #include "UnityCG.cginc"
    98.  
    99. uniform sampler2D _MainTex;
    100.  
    101. half4 frag (v2f_vertex_lit i) : COLOR {
    102.     return VertexLight( i, _MainTex );
    103. }
    104. ENDCG
    105.  
    106.     }
    107.    
    108.    
    109.     // Pixel lights
    110.     Pass {
    111.         Name "PPL"
    112.         Tags { "LightMode" = "Pixel" }
    113. CGPROGRAM
    114. #pragma fragment frag
    115. #pragma vertex vert
    116. #pragma multi_compile_builtin_noshadows
    117. #pragma fragmentoption ARB_fog_exp2
    118. #pragma fragmentoption ARB_precision_hint_fastest
    119. #include "UnityCG.cginc"
    120. #include "AutoLight.cginc"
    121.  
    122. struct v2f {
    123.     V2F_POS_FOG;
    124.     LIGHTING_COORDS
    125.     float3    uvK; // xy = UV, z = specular K
    126.     float3    viewDir;
    127.     float3    normal;
    128.     float3    lightDir;
    129. };
    130.  
    131. uniform float _Shininess;
    132. uniform float4 _MainTex_ST;
    133.  
    134. v2f vert (appdata_base v)
    135. {
    136.     v2f o;
    137.     PositionFog( v.vertex, o.pos, o.fog );
    138.     o.normal = v.normal;
    139.     o.uvK.xy = TRANSFORM_TEX(v.texcoord,_MainTex);
    140.     o.uvK.z = _Shininess * 128;
    141.     o.lightDir = ObjSpaceLightDir( v.vertex );
    142.     o.viewDir = ObjSpaceViewDir( v.vertex );
    143.     TRANSFER_VERTEX_TO_FRAGMENT(o);
    144.     return o;
    145. }
    146.  
    147. uniform sampler2D _MainTex;
    148. uniform float _Wrap;
    149.  
    150. // Mostly copied from UnityCG.cginc; modified to use wrap lighting
    151. // instead of regular diffuse.
    152. inline half4 SpecularLightWrap( half3 lightDir, half3 viewDir, half3 normal, half4 color, float specK, half atten )
    153. {
    154.     normal = normalize(normal);
    155.     lightDir = normalize(lightDir);
    156.     viewDir = normalize(viewDir);
    157.     half3 h = normalize( lightDir + viewDir );
    158.    
    159.     // This would be regular diffuse component
    160.     //half diffuse = dot( normal, lightDir );
    161.    
    162.     // This is "wrap diffuse" lighting.
    163.     // Note that if you know the _Wrap parameter you will use, the
    164.     // shader can be optimized. E.g. if _Wrap is 1.0, then putting that
    165.     // and simplifying the math becomes just
    166.     //   half diffuse = dot(normal, lightDir) * 0.5 + 0.5;
    167.     half diffuse = max(0.0, (dot(normal, lightDir) + _Wrap) / (1.0 + _Wrap));
    168.    
    169.     float nh = saturate( dot( h, normal ) );
    170.     float spec = pow( nh, specK ) * color.a;
    171.     spec *= diffuse;
    172.    
    173.     half4 c;
    174.     c.rgb = (color.rgb * _ModelLightColor0.rgb * diffuse + _SpecularLightColor0.rgb * spec) * (atten * 2);
    175.     c.a = _SpecularLightColor0.a * spec * atten; // specular passes by default put highlights to overbright
    176.     return c;
    177. }
    178.  
    179.  
    180. half4 frag (v2f i) : COLOR
    181. {  
    182.     half4 texcol = tex2D( _MainTex, i.uvK.xy );
    183.     return SpecularLightWrap( i.lightDir, i.viewDir, i.normal, texcol, i.uvK.z, LIGHT_ATTENUATION(i) );
    184. }
    185. ENDCG
    186.  
    187.     }
    188. }
    189.  
    190. Fallback "Specular"
    191.  
    192. }
     
  4. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    This shader is using the alpha channel from the main texture for specular or "gloss" data (you'll notice the texture is named "Base (RGB) Gloss (A)". You will need to add a second texture to use for transparency.