Search Unity

Unity 4.6 - WP8 Transparency Shader for Fog

Discussion in 'Shaders' started by dzkdes, Jan 25, 2016.

  1. dzkdes

    dzkdes

    Joined:
    Apr 18, 2015
    Posts:
    18
    Hello,

    Since Unity 4.6 doesn't support fog on WP8 platforms, I have added fog support to all of my shaders thanks to this blog: http://www.software7.com/blog/unity-custom-fog-shader-on-windows-phone-8/

    However, when I try to add fog support to my transparency shader, I get an error. I am new to shaders. Any help to fix this error will be appraciated. (I use echologin shaders in my project, but he dropped support and I can't reach him)

    Thanks in advance..

    Here is the shader (modified for fog support):

    Code (CSharp):
    1. Shader "old-echoLogin/Transparent/Cutout/20-Color-WP8_v2"
    2. {
    3.     Properties
    4.        {
    5.         _TransparencyLM ("Transparency LM", 2D) = "gray" {}
    6.         _echoRGBA ( "RGBA Multiply", Vector )            = ( 1, 1, 1, 1 )  
    7.        }
    8.     //=========================================================================
    9.     SubShader
    10.     {
    11.         Tags {"Queue"="Transparent+1" "IgnoreProjector"="True" "RenderType"="TransparentCutout"}
    12.         Fog {Mode  Off}
    13.  
    14.         Pass
    15.         {  
    16.             Name "BASE"
    17.                ZWrite Off
    18.                Blend SrcAlpha OneMinusSrcAlpha
    19.               Cull Off
    20.            
    21.             CGPROGRAM
    22.          
    23.              #pragma vertex vert vertex:fogVertex finalcolor:fogColor
    24.             #pragma fragment frag
    25.             #pragma fragmentoption ARB_precision_hint_fastest
    26.             #pragma multi_compile_fwdbase
    27.  
    28.             uniform half4 unity_FogColor;
    29.             uniform half4 unity_FogStart;
    30.             uniform half4 unity_FogEnd;
    31.             uniform half4 unity_FogDensity;
    32.  
    33.             #include "UnityCG.cginc"
    34.  
    35.             sampler2D    _TransparencyLM;
    36.             float4        _TransparencyLM_ST;
    37.             float4        _TransparencyLM_TexelSize;
    38.  
    39. #ifndef LIGHTMAP_OFF
    40.             sampler2D   unity_Lightmap;
    41.             float4       unity_LightmapST;
    42. #endif
    43.             float4        _echoRGBA;
    44.  
    45.              struct VertInput
    46.             {
    47.                 float4 vertex    : POSITION;
    48.                 float2 texcoord    : TEXCOORD0;
    49.                 float4 color    : COLOR;
    50. #ifndef LIGHTMAP_OFF
    51.                   float4 texcoord1: TEXCOORD1;
    52. #endif
    53.                 half fogFactor;
    54.             };
    55.  
    56.             void fogVertex(inout appdata_full v, out VertInput data)
    57.             {
    58.                 UNITY_INITIALIZE_OUTPUT(VertInput, data);
    59.                 float cameraVertDist = length(mul(UNITY_MATRIX_MV, v.vertex).xyz);
    60.                 data.fogFactor = saturate((unity_FogEnd.x - cameraVertDist) / (unity_FogEnd.x - unity_FogStart.x));          
    61.             }
    62.  
    63.             void fogColor(VertInput IN, SurfaceOutput o, inout fixed4 color)
    64.             {
    65.                 color.rgb = lerp(unity_FogColor.rgb, color.rgb, IN.fogFactor);
    66.             }
    67.  
    68.                struct Varys
    69.             {
    70.                 half4 pos        : SV_POSITION;
    71.                 half2 tc1        : TEXCOORD0;
    72.                 fixed4 vcolor    : TEXCOORD1;
    73. #ifndef LIGHTMAP_OFF
    74.                 half2 tc3        : TEXCOORD3;
    75. #endif
    76.             };
    77.  
    78.              //=============================================
    79.             Varys vert ( VertInput ad )
    80.             {
    81.                 Varys v;
    82.  
    83.                 v.vcolor        = ad.color * _echoRGBA;
    84.                 v.pos    = mul ( UNITY_MATRIX_MVP, ad.vertex );
    85.                   v.tc1     = ( _TransparencyLM_ST.xy * ad.texcoord.xy ) + _TransparencyLM_ST.zw;
    86.  
    87. #ifndef LIGHTMAP_OFF
    88.                    v.tc3     = ( unity_LightmapST.xy * ad.texcoord1.xy ) + unity_LightmapST.zw;
    89. #endif
    90.                 return v;
    91.             }
    92.    
    93.              //=============================================
    94.             fixed4 frag ( Varys v ):COLOR
    95.             {
    96.                 fixed4 fcolor = tex2D ( _TransparencyLM, v.tc1 );
    97.      
    98. #ifndef LIGHTMAP_OFF
    99.                 fcolor.xyz *= DecodeLightmap ( tex2D ( unity_Lightmap, v.tc3 ) );
    100. #endif
    101.                 return fcolor * v.vcolor;
    102.             }
    103.  
    104.             ENDCG
    105.         }
    106.      }
    107. }
    108.  
    And here is the error I am getting:
    Shader error in 'old-echoLogin/Transparent/Cutout/20-Color-WP8_v2': syntax error, unexpected ',' at token "," type name expected at token "," at line 78

    Line 78 is this line:
    void fogColor(VertInput IN, SurfaceOutput o, inout fixed4 color)
     
  2. Michal_

    Michal_

    Joined:
    Jan 14, 2015
    Posts:
    365
    Yeah, that guide of yours works only with surface shaders. Maybe this will work. I didn't test it, I don't have 4.6.
    Code (CSharp):
    1. Shader "old-echoLogin/Transparent/Cutout/20-Color-WP8_v2"
    2. {
    3.     Properties
    4.        {
    5.         _TransparencyLM ("Transparency LM", 2D) = "gray" {}
    6.         _echoRGBA ( "RGBA Multiply", Vector )            = ( 1, 1, 1, 1 )
    7.        }
    8.     //=========================================================================
    9.     SubShader
    10.     {
    11.         Tags {"Queue"="Transparent+1" "IgnoreProjector"="True" "RenderType"="TransparentCutout"}
    12.         Fog {Mode  Off}
    13.         Pass
    14.         {
    15.             Name "BASE"
    16.                ZWrite Off
    17.                Blend SrcAlpha OneMinusSrcAlpha
    18.               Cull Off
    19.          
    20.             CGPROGRAM
    21.        
    22.             #pragma vertex vert
    23.             #pragma fragment frag
    24.             #pragma fragmentoption ARB_precision_hint_fastest
    25.             #pragma multi_compile_fwdbase
    26.             uniform half4 unity_FogColor;
    27.             uniform half4 unity_FogStart;
    28.             uniform half4 unity_FogEnd;
    29.             uniform half4 unity_FogDensity;
    30.             #include "UnityCG.cginc"
    31.             sampler2D    _TransparencyLM;
    32.             float4        _TransparencyLM_ST;
    33.             float4        _TransparencyLM_TexelSize;
    34. #ifndef LIGHTMAP_OFF
    35.             sampler2D   unity_Lightmap;
    36.             float4       unity_LightmapST;
    37. #endif
    38.             float4        _echoRGBA;
    39.              struct VertInput
    40.             {
    41.                 float4 vertex    : POSITION;
    42.                 float2 texcoord    : TEXCOORD0;
    43.                 float4 color    : COLOR0;
    44.             };
    45.                struct Varys
    46.             {
    47.                 half4 pos        : SV_POSITION;
    48.                 half2 tc1        : TEXCOORD0;
    49.                 fixed4 vcolor    : TEXCOORD1;
    50.                 half fogFactor : TEXCOORD2;
    51. #ifndef LIGHTMAP_OFF
    52.                 half2 tc3        : TEXCOORD3;
    53. #endif
    54.             };
    55.                        
    56.             //=============================================
    57.             Varys vert ( VertInput ad )
    58.             {
    59.                 Varys v;
    60.                 v.vcolor        = ad.color * _echoRGBA;
    61.                 v.pos    = mul ( UNITY_MATRIX_MVP, ad.vertex );
    62.                   v.tc1     = ( _TransparencyLM_ST.xy * ad.texcoord.xy ) + _TransparencyLM_ST.zw;
    63. #ifndef LIGHTMAP_OFF
    64.                    v.tc3     = ( unity_LightmapST.xy * ad.texcoord1.xy ) + unity_LightmapST.zw;
    65. #endif
    66.  
    67.                 float cameraVertDist = length(mul(UNITY_MATRIX_MV, ad.vertex).xyz);
    68.                 v.fogFactor = saturate((unity_FogEnd.x - cameraVertDist) / (unity_FogEnd.x - unity_FogStart.x));
    69.  
    70.                 return v;
    71.             }
    72.  
    73.              //=============================================
    74.             fixed4 frag ( Varys v ):COLOR
    75.             {
    76.                 fixed4 fcolor = tex2D ( _TransparencyLM, v.tc1 );
    77.    
    78. #ifndef LIGHTMAP_OFF
    79.                 fcolor.xyz *= DecodeLightmap ( tex2D ( unity_Lightmap, v.tc3 ) );
    80. #endif
    81.                 fixed4 color =  fcolor * v.vcolor;
    82.  
    83.                 color.rgb = lerp(unity_FogColor.rgb, color.rgb, v.fogFactor);
    84.                 return color;
    85.             }
    86.             ENDCG
    87.         }
    88.      }
    89. }
     
  3. dzkdes

    dzkdes

    Joined:
    Apr 18, 2015
    Posts:
    18
    Thanks a lot. That really worked!