Search Unity

Adapting AlphaClipsafe to work with Diffuse materials?

Discussion in 'Shaders' started by Loud, Jan 13, 2010.

  1. Loud

    Loud

    Joined:
    Jun 5, 2009
    Posts:
    88
    I am looking at the AlphaClipsafe shader in the Unify wiki here:
    http://www.unifycommunity.com/wiki/index.php?title=AlphaClipsafe

    Basically, I need the exact same functionality but for a Diffuse shader, not a Particle shader.

    Is that functionality simple to adapt from this script? It would be much appreciated if someone could show me or point me in the right direction so I can learn to do some shaders myself. I still have no clue.
     
  2. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    It's not terribly straightforward, unfortunately. It can be done if you don't need vertex lighting, though. I can probably take a look at it this weekend.
     
  3. Loud

    Loud

    Joined:
    Jun 5, 2009
    Posts:
    88
    I don't think I need vertex lighting, but I would need lighting. It's possible with per-pixel lighting?

    That would be awesome if you could check it out! I would love to compare the shaders once you're done to maybe glean a little knowledge of shader coding.
     
  4. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    Just to let you know: I haven't forgotten about this, I've just been busy. I had a second this evening and it is half done. I'll have the rest finished sometime soon.
     
  5. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    I think this works:

    Code (csharp):
    1. Shader "Transparent Clipsafe/Diffuse" {
    2.     Properties {
    3.         _Color ("Main Color", Color) = (1,1,1,1)
    4.         _MainTex ("Base (RGB) Opacity (A)", 2D) = "white" {}
    5.         _FadeDistance ("Fade Start Distance", float) = 0.5
    6.     }
    7.  
    8.     SubShader {
    9.         Tags { "Queue" = "Transparent" }
    10.         AlphaTest Greater .01
    11.         ColorMask RGB
    12.         ZWrite Off
    13.         Fog { Color [_AddFog] }
    14.         Pass {
    15.             Name "BASE"
    16.             Tags {"LightMode" = "PixelOrNone"}
    17.             Blend SrcAlpha OneMinusSrcAlpha
    18.             Lighting Off
    19.             CGPROGRAM
    20.                 #pragma vertex vert
    21.                 #pragma fragment frag
    22.                 #pragma fragmentoption ARB_fog_exp2
    23.                 #pragma fragmentoption ARB_precision_hint_fastest
    24.                 #include "UnityCG.cginc"
    25.                
    26.                 uniform float4  _MainTex_ST,
    27.                                 _Color;
    28.                 uniform float _FadeDistance;
    29.                
    30.                 uniform sampler2D _MainTex;
    31.                
    32.                 struct v2f {
    33.                     V2F_POS_FOG;
    34.                     float2  uv;
    35.                     float4 color;
    36.                 };
    37.                
    38.                 v2f vert (appdata_base v) {
    39.                     v2f o;
    40.                     PositionFog( v.vertex, o.pos, o.fog );
    41.                     o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
    42.                     float4 viewPos = mul(glstate.matrix.modelview[0], v.vertex);
    43.                     float alpha = (-viewPos.z - _ProjectionParams.y)/_FadeDistance;
    44.                     alpha = min(alpha, 1);
    45.                     o.color = float4(_Color.rgb*_PPLAmbient, _Color.a*alpha);
    46.                     o.color.rgb *= 2;
    47.                     return o;
    48.                 }
    49.                
    50.                 float4 frag (v2f i) : COLOR {
    51.                     half4 texcol = tex2D( _MainTex, i.uv );
    52.                    
    53.                     return texcol*i.color;
    54.                 }
    55.             ENDCG
    56.         }
    57.         Pass {
    58.             Name "PPL"
    59.             Tags { "LightMode" = "Pixel" }
    60.             Blend SrcAlpha One
    61.             CGPROGRAM
    62.                 #pragma vertex vert
    63.                 #pragma fragment frag
    64.                 #pragma multi_compile_builtin
    65.                 #pragma fragmentoption ARB_fog_exp2
    66.                 #pragma fragmentoption ARB_precision_hint_fastest
    67.                 #include "UnityCG.cginc"
    68.                 #include "AutoLight.cginc"
    69.                
    70.                 uniform float4  _MainTex_ST,
    71.                                 _Color;
    72.                 uniform float _FadeDistance;
    73.                
    74.                 uniform sampler2D _MainTex;
    75.                
    76.                 struct v2f {
    77.                     V2F_POS_FOG;
    78.                     LIGHTING_COORDS
    79.                     float2  uv;
    80.                     float3  normal;
    81.                     float3  lightDir;
    82.                     float4 color;
    83.                 };
    84.                
    85.                 v2f vert (appdata_base v) {
    86.                     v2f o;
    87.                     PositionFog( v.vertex, o.pos, o.fog );
    88.                     o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
    89.                     float4 viewPos = mul(glstate.matrix.modelview[0], v.vertex);
    90.                     float alpha = (-viewPos.z - _ProjectionParams.y)/_FadeDistance;
    91.                     alpha = min(alpha, 1);
    92.                     o.color = float4(_Color.rgb, _Color.a*alpha);
    93.                     o.color.rgb *= 2;
    94.                     o.normal = v.normal;
    95.                     o.lightDir = ObjSpaceLightDir( v.vertex );
    96.                     TRANSFER_VERTEX_TO_FRAGMENT(o);
    97.                     return o;
    98.                 }
    99.                
    100.                 float4 frag (v2f i) : COLOR {
    101.                     half4 texcol = tex2D( _MainTex, i.uv );
    102.                    
    103.                     float4 light = DiffuseLight( i.lightDir, i.normal, texcol, LIGHT_ATTENUATION(i) );
    104.                    
    105.                     return float4(light.rgb, i.color.a);
    106.                 }
    107.             ENDCG
    108.         }
    109.     }
    110.    
    111.     Fallback "Diffuse"
    112. }
     
  6. Loud

    Loud

    Joined:
    Jun 5, 2009
    Posts:
    88
    Oh my goodness that is incredible! That helps my game so much! I'll post the results sometime (hopefully soon). This shader must go up on the wiki. I look forward to going through the thing and learning a little something while I'm at it.

    You are a hero among mankind good sir.
     
  7. judgementAlex

    judgementAlex

    Joined:
    Sep 9, 2013
    Posts:
    28
    Sorry for necro, but I stumbled upon this thread and ended up making a working solution for 5.0.

    I needed a shader that would fade near the camera, and since these ones don't work anymore I ended up writing my own,
    Here you go:
    Code (CSharp):
    1. Shader "Custom/Diffuse-Clipsafe" {
    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.         _FadeDistance("Fade Distance", float) = 1.0
    8.     }
    9.     SubShader {
    10.         Tags { "RenderType"="Transparent" }
    11.         LOD 200
    12.        
    13.         CGPROGRAM
    14.         // Physically based Standard lighting model, and enable shadows on all light types
    15.         #pragma surface surf Standard alpha fullforwardshadows vertex:vert
    16.  
    17.         // Use shader model 3.0 target, to get nicer looking lighting
    18.         #pragma target 3.0
    19.  
    20.         sampler2D _MainTex;
    21.  
    22.         struct Input {
    23.             float2 uv_MainTex;
    24.             float4 color;
    25.         };
    26.  
    27.         half _Glossiness;
    28.         half _Metallic;
    29.         fixed4 _Color;
    30.         float _FadeDistance;
    31.  
    32.         void vert(inout appdata_full v, out Input o) {
    33.             float4 viewPos = mul(UNITY_MATRIX_MV, v.vertex);
    34.             float alpha =  (-viewPos.z - _ProjectionParams.y) / _FadeDistance;
    35.             alpha = min(alpha, 1);
    36.  
    37.             UNITY_INITIALIZE_OUTPUT(Input, o);
    38.             o.color = float4(_Color.rgb, _Color.a * alpha);
    39.         }
    40.  
    41.        
    42.  
    43.         void surf (Input IN, inout SurfaceOutputStandard o) {
    44.             // Albedo comes from a texture tinted by color
    45.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * IN.color;
    46.             o.Albedo = c.rgb * c.a;
    47.             // Metallic and smoothness come from slider variables
    48.             o.Metallic = _Metallic;
    49.             o.Smoothness = _Glossiness;
    50.             o.Alpha = c.a;
    51.         }
    52.         ENDCG
    53.     }
    54.     FallBack "Diffuse"
    55. }
    https://github.com/veridiam/Unity-Resources/blob/master/Shaders/Diffuse-Clipsafe.shader
     
    StaffanEk likes this.
  8. StaffanEk

    StaffanEk

    Joined:
    Jul 13, 2012
    Posts:
    380
    This is great. Thanks you judgementAlex. Would it be possible to have this shader to use a normal map?