Search Unity

My custom shader does not work in Unity 5

Discussion in 'Shaders' started by Vojta Koci, Mar 4, 2015.

  1. Vojta Koci

    Vojta Koci

    Joined:
    Nov 19, 2013
    Posts:
    64
    Hi there!
    I have a little problem. As the headline says, my custom shader stopped working in Unity 5.
    Code (CSharp):
    1. Shader "Custom/FoldInvisSprite"
    2. {
    3.     Properties
    4.     {
    5.         [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
    6.         _Color ("Tint", Color) = (1,1,1,1)
    7.         [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
    8.         _a ("a", float) = 100
    9.         _b ("b", float) = 100
    10.         _under ("u", int) = 1
    11.     }
    12.  
    13.     SubShader
    14.     {
    15.         Tags
    16.         {
    17.             "Queue"="Transparent"
    18.             "IgnoreProjector"="True"
    19.             "RenderType"="Transparent"
    20.             "PreviewType"="Plane"
    21.             "CanUseSpriteAtlas"="True"
    22.         }
    23.  
    24.         Cull Off
    25.         Lighting On
    26.         ZWrite Off
    27.         Fog { Mode Off }
    28.         Blend SrcAlpha OneMinusSrcAlpha
    29.  
    30.         Pass
    31.         {
    32.         CGPROGRAM
    33.             // Upgrade NOTE: excluded shader from DX11 and Xbox360; has structs without semantics (struct v2f members _a,_b)
    34.             #pragma exclude_renderers d3d11 xbox360
    35.             #pragma vertex vert
    36.             #pragma fragment frag
    37.             #pragma multi_compile DUMMY PIXELSNAP_ON
    38.             #include "UnityCG.cginc"
    39.            
    40.             struct appdata_t
    41.             {
    42.                 float4 vertex   : POSITION;
    43.                 float4 color    : COLOR;
    44.                 float2 texcoord : TEXCOORD0;
    45.             };
    46.  
    47.             struct v2f
    48.             {
    49.                 float4 vertex   : SV_POSITION;
    50.                 fixed4 color    : COLOR;
    51.                 half2 texcoord  : TEXCOORD0;
    52.             };
    53.            
    54.             fixed4 _Color;
    55.  
    56.             v2f vert(appdata_t IN)
    57.             {
    58.                 v2f OUT;
    59.                 OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
    60.                 OUT.texcoord = IN.texcoord;
    61.                 OUT.color = IN.color * _Color;
    62.                 #ifdef PIXELSNAP_ON
    63.                 OUT.vertex = UnityPixelSnap (OUT.vertex);
    64.                 #endif
    65.  
    66.                 return OUT;
    67.             }
    68.  
    69.             sampler2D _MainTex;
    70.             float _a;
    71.             float _b;
    72.             float _under;
    73.  
    74.             fixed4 frag(v2f IN) : COLOR
    75.             {
    76.             if((_a * IN.texcoord.x + _b - IN.texcoord.y) * _under > 0){
    77.                     return tex2D(_MainTex, IN.texcoord) * IN.color;
    78.                    
    79.                 }
    80.             else
    81.                 return 0;
    82.             }
    83.            
    84.         ENDCG
    85.         }
    86.     }
    87. }
    I can see just black rectangles instead of sprites. Does anyone have any clue where the problem is?
    I'd be grateful for any help.

    Thanks!
    Vojta
     
  2. jackson31

    jackson31

    Joined:
    Aug 25, 2010
    Posts:
    28
    My custom shader also doesn't work, it seems that "Blend SrcAlpha OneMinusSrcAlpha" is getting completely ignored
     
  3. Gameccino

    Gameccino

    Joined:
    Aug 1, 2012
    Posts:
    40
    me too,same issue after unity5 with Blend SrcAlpha OneMinusSrcAlpha , all other Blend states are working as expected
     
    Yash987654321 likes this.
  4. imaginaryhuman

    imaginaryhuman

    Joined:
    Mar 21, 2010
    Posts:
    5,834
    I know apparently some things changed with shaders in 5, like compiling to hlsl or glsl or something automatically? I wonder if that switch-over has produced some new bugs.
     
  5. drjamiearron

    drjamiearron

    Joined:
    Jun 8, 2012
    Posts:
    134
    I'm also having issues with this it seems. I'm glad I'm not the only one :)
     
  6. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    Blending can be different for each pass of the shader. It is therefore supposed to go inside the Pass block. The same actually goes for some other settings. In the past it also worked if set in the Subshader block, but they might have cleaned this up. That could be why it's ignored in the shader above.
     
  7. jackson31

    jackson31

    Joined:
    Aug 25, 2010
    Posts:
    28
    I tried moving those settings around, the Blend SrcAlpha OneMinusSrcAlpha doesn't seem to work no matter where I paste it :S
     
  8. Vojta Koci

    Vojta Koci

    Joined:
    Nov 19, 2013
    Posts:
    64
    Frankly, I'm not sure, what you mean... I tried many things, no positive result...
     
  9. Jonny-Roy

    Jonny-Roy

    Joined:
    May 29, 2013
    Posts:
    666
    Here you go this should fix it, although not sure where you got the shader but it's a bit un-optimised in places. Anyway, let me know if this works:


    Code (CSharp):
    1. Shader "Custom/FoldInvisSprite"
    2. {
    3.     Properties
    4.     {
    5.         [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
    6.         _Color ("Tint", Color) = (1,1,1,1)
    7.         [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
    8.         _a ("a", float) = 100
    9.         _b ("b", float) = 100
    10.         _under ("u", int) = 1
    11.     }
    12.  
    13.     SubShader
    14.     {
    15.         Tags
    16.         {
    17.             "Queue"="Transparent"
    18.             "IgnoreProjector"="True"
    19.             "RenderType"="Transparent"
    20.             "PreviewType"="Plane"
    21.             "CanUseSpriteAtlas"="True"
    22.         }
    23.         Cull Off
    24.         Lighting On
    25.         ZWrite Off
    26.         Fog { Mode Off }
    27.         Blend SrcAlpha OneMinusSrcAlpha
    28.         Pass
    29.         {
    30.         CGPROGRAM
    31.             // Upgrade NOTE: excluded shader from DX11 and Xbox360; has structs without semantics (struct v2f members _a,_b)
    32.             #pragma vertex vert
    33.             #pragma fragment frag
    34.             #pragma multi_compile DUMMY PIXELSNAP_ON
    35.             #include "UnityCG.cginc"
    36.          
    37.             struct appdata_t
    38.             {
    39.                 float4 vertex   : POSITION;
    40.                 float4 color    : COLOR;
    41.                 float2 texcoord : TEXCOORD0;
    42.             };
    43.             struct v2f
    44.             {
    45.                 float4 vertex   : SV_POSITION;
    46.                 fixed4 color    : COLOR;
    47.                 half2 texcoord  : TEXCOORD0;
    48.             };
    49.          
    50.             fixed4 _Color;
    51.             v2f vert(appdata_t IN)
    52.             {
    53.                 v2f OUT;
    54.                 OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
    55.                 OUT.texcoord = IN.texcoord;
    56.                 OUT.color = IN.color * _Color;
    57.                 #ifdef PIXELSNAP_ON
    58.                     OUT.vertex = UnityPixelSnap (OUT.vertex);
    59.                 #endif
    60.                 return OUT;
    61.             }
    62.             sampler2D _MainTex;
    63.             float _a;
    64.             float _b;
    65.             float _under;
    66.             fixed4 frag(v2f IN) : COLOR
    67.             {
    68.                 if((_a * IN.texcoord.x + _b - IN.texcoord.y) * _under > 0)
    69.                 {
    70.                     return tex2D(_MainTex, IN.texcoord) * IN.color;
    71.                  
    72.                 }
    73.                 else
    74.                 {
    75.                     return 0;
    76.                 }
    77.             }
    78.          
    79.         ENDCG
    80.         }
    81.     }
    82. }
    If it works okay I can optimise it for you if you need. Anyone else with any shaders that don't work post them and I'll fix them for you.
     
    redstonegames and Nanako like this.
  10. Vojta Koci

    Vojta Koci

    Joined:
    Nov 19, 2013
    Posts:
    64
    Damn, sir Jonny Roy, I want that brain of yours. Works fine. Where do you think the code is unoptimised? I may try to find someone else to help me with optimising this, since I don't wanna waste your time. Just this is a huge help, thank you so much!
     
  11. Physicsdan

    Physicsdan

    Joined:
    Mar 7, 2015
    Posts:
    1
    Anyway this is my first post I wanted to say that I had a shader problem (I'm not a good shader writer) but I just wanted to share some knowledge if someone had the same problem with the shaders

    I changed all these things and my shader started to work again.
    OLD: #pragma surface surf Lambert
    NEW: #pragma surface surf Standard fullforwardshadows

    OLD: void surf(Input IN, inout SurfaceOutput o) {
    NEW: void surf (Input IN, inout SurfaceOutputStandard o) {

    Maybe it will help you with your shaders
     
    FrogGraderMan likes this.
  12. cygnusfear

    cygnusfear

    Joined:
    Oct 24, 2012
    Posts:
    14
    I am running into the same issues trying to rewrite a shader I've written for Unity 4, blending seems broken.

    We wanted a splatmap shader that allowed for more than 4 textures, so after attempting arcane magic with packing floats into RGBA in order to do 8 or more textures I ended up simply layering materials. A simple One OneMinusSrcAlpha blend used to do the trick but in Unity 5 I'm completely at a loss. The blend mode simply doesn't seem to work, and any other blendmode makes my terrain see-through.

    I'm trying to adapt the Terrain shader from the Viking demo here; but I can neither get the blending to work as described above and my shadows receiving seem to be broken.

    If anyone can help that would be greatly appreciated.

    Code (CSharp):
    1. Shader "TerrainSurface"
    2. {
    3.     Properties
    4.     {
    5.         _Control ("Control (R)", 2D) = "red" {}
    6.  
    7.         _WorldSplatScale("WorldSplat Scale", Float) = 0.3
    8.         _WorldSplatOffset("Offset XY", Vector) = (0.5, 0.5, 0.0, 0.0)
    9.  
    10.         _EdgeMask("Edge Mask", 2D) = "white" {}
    11.         _EdgeMaskScale("Edge Mask Scale", Float) = 2
    12.  
    13.         _MaskSoftness ("Mask Softness", Float ) = 1.23
    14.         _MaskMinStep ("Mask Min Step", Float ) = 0.3
    15.         _MaskMaxStep ("Mask Max Step", Float ) = 0.5
    16.    
    17.         _Splat0 ("Albedo 0", 2D) = "white" {}
    18.  
    19.         _Splat0Scale("Scale", Float) = 0.2
    20.  
    21.         _Splat1 ("Albedo 1", 2D) = "white" {}
    22.  
    23.         _Splat1Scale("Scale", Float) = 0.2
    24.  
    25.         _Mask ("Mask", 2D) = "black" {}
    26.    
    27.         _NormalMap ("NormalMap 0", 2D) = "bump" {}
    28.         _NormalMap2 ("NormalMap 1", 2D) = "bump" {}
    29.        
    30.         [LM_Specular] [LM_Glossiness] _SpecGlossMap("Specular 0", 2D) = "white" {}
    31.         [LM_Specular] [LM_Glossiness] _SpecGlossMap2("Specular 1", 2D) = "white" {}
    32.     }
    33.  
    34.     // SM3+
    35.     SubShader
    36.     {
    37.         Tags { "RenderType" = "Opaque" }
    38.  
    39.         Blend One OneMinusSrcAlpha
    40.        
    41.         CGPROGRAM
    42.         #pragma surface surf StandardSpecular fullforwardshadows addshadow
    43.         // until texCubeLOD is solved
    44.         #pragma exclude_renderers gles
    45.         #pragma target 3.0
    46.  
    47.                
    48.         struct Input
    49.         {
    50.             float4 color;
    51.             float2 uv_MainTex;
    52.             float2 uv_MainTex2;
    53.             float2 uv_Mask;
    54.             float3 worldPos;
    55.             float3 worldNormal;
    56.         };
    57.  
    58.         sampler2D _Control;
    59.  
    60.         float _WorldSplatScale;
    61.         float4 _WorldSplatOffset;
    62.            
    63.         sampler2D _EdgeMask;
    64.         float _EdgeMaskScale;
    65.  
    66.         float _MaskSoftness;
    67.         float _MaskMinStep;
    68.         float _MaskMaxStep;
    69.  
    70.         sampler2D _Splat0;
    71.         sampler2D _Splat1;
    72.  
    73.         float _Splat0Scale;
    74.         float _Splat1Scale;
    75.  
    76.         sampler2D _NormalMap;
    77.         sampler2D _NormalMap2;
    78.    
    79.         sampler2D _SpecGlossMap;
    80.         sampler2D _SpecGlossMap2;
    81.    
    82.         sampler2D _Mask;
    83.    
    84.         void surf (Input IN, inout SurfaceOutputStandardSpecular o)
    85.         {
    86.             float3 projNormal = saturate(pow(IN.worldNormal * 1.4, 4));
    87.             float3 mask = tex2D(_Control, (IN.worldPos.xz * _WorldSplatScale * float2(1.0, -1.0))+_WorldSplatOffset.xy);
    88.  
    89.             float3 edgeMask = tex2D(_EdgeMask, IN.worldPos.xz * _EdgeMaskScale);
    90.             mask = 1-clamp(1-(1-mask.r * (edgeMask)) + mask.r,0,1);
    91.             mask *= mask * _MaskSoftness;
    92.             mask = smoothstep(max(0,_MaskMinStep),max(0,_MaskMaxStep),mask);
    93.  
    94.             fixed blend = tex2D(_Mask, IN.uv_Mask).a;
    95.    
    96.             fixed4 albedo1 = tex2D(_Splat0, IN.worldPos.xz * _Splat0Scale);
    97.             fixed4 spec1    = tex2D(_SpecGlossMap, IN.worldPos.xz * _Splat0Scale);
    98.              fixed3 normal1 = UnpackNormal (tex2D (_NormalMap, IN.worldPos.xz * _Splat0Scale));
    99.  
    100.             fixed4 albedo2 = tex2D(_Splat1, IN.worldPos.xz * _Splat0Scale);
    101.             fixed4 spec2 = tex2D(_SpecGlossMap2, IN.worldPos.xz * _Splat0Scale);
    102.              fixed3 normal2 = UnpackNormal (tex2D (_NormalMap2, IN.worldPos.xz * _Splat0Scale));
    103.  
    104.              fixed4 specGloss = lerp (spec1, spec2, blend);
    105.            
    106.             o.Albedo         = lerp (albedo1, albedo2, blend);
    107.              o.Specular         = mask.r * specGloss.rgb;
    108.             o.Smoothness     = mask.r * specGloss.a;
    109.               o.Normal         = mask.r * lerp (normal1, normal2, blend);
    110.               o.Alpha            = mask.r;
    111.         }
    112.         ENDCG
    113.     }
    114.  
    115.     CustomEditor "BlendShaderGUI"
    116. }
     
  13. Xoduz

    Xoduz

    Joined:
    Apr 6, 2013
    Posts:
    135
  14. drjamiearron

    drjamiearron

    Joined:
    Jun 8, 2012
    Posts:
    134
  15. kiron46

    kiron46

    Joined:
    Mar 4, 2015
    Posts:
    7

    Hey Jonny Roy, thanks so much for helping everyone out! That's so amazing of you given shader programming's high barrier of entry. A friend of mine recently gave me some shader code and like everyone else, every object with the shader turns black on builds. Could you take a look at mine? It gets errors like

    "Program 'frag_surf', error X4506: ps_4_0_level_9_1 input limit (8) exceeded, shader uses 9 inputs.
    Compiling Fragment program with DIRECTIONAL SHADOWS_SCREEN SHADOWS_NATIVE LIGHTMAP_OFF DIRLIGHTMAP_OFF DYNAMICLIGHTMAP_OFF FOG_LINEAR"

    and

    "Too many math instructions for SM2.0 (66 needed, max is 64). Try #pragma target 3.0
    Compiling Fragment program with DIRECTIONAL SHADOWS_OFF LIGHTMAP_OFF DIRLIGHTMAP_OFF DYNAMICLIGHTMAP_OFF FOG_LINEAR"


    Here's the code:

    Code (CSharp):
    1. Shader "RimLightingScroll"
    2. {
    3.     Properties
    4.     {
    5. _DiffuseColor("_DiffuseColor", Color) = (1,0,0,1)
    6. _RimColor("_RimColor", Color) = (0,0.1188812,1,1)
    7. _RimPower("_RimPower", Range(0.1,3) ) = 1.707772
    8. _Glossiness("_Glossiness", Range(0.1,1) ) = 0.4300518
    9. _SpecularColor("_SpecularColor", Color) = (0.3006994,1,0,1)
    10. _MainTex("_MainTex", 2D) = "white" {}
    11. _ScanA("ScanA", 2D) = "black" {}
    12. _ScanB("ScanB", 2D) = "black" {}
    13. _ScanForeground("ScanForeground", 2D) = "black" {}
    14. _ScanColor("ScanColor", Color) = (1,1,1,1)
    15.  
    16.     }
    17.  
    18.     SubShader
    19.     {
    20.         Tags
    21.         {
    22. "Queue"="Geometry"
    23. "IgnoreProjector"="False"
    24. "RenderType"="Opaque"
    25.  
    26.         }
    27.  
    28.      
    29. Cull Back
    30. ZWrite On
    31. ZTest LEqual
    32. ColorMask RGBA
    33. Fog{
    34. }
    35.  
    36.  
    37.         CGPROGRAM
    38. #pragma surface surf BlinnPhongEditor  vertex:vert
    39. #pragma target 2.0
    40.  
    41.  
    42. float4 _DiffuseColor;
    43. float4 _RimColor;
    44. float _RimPower;
    45. float _Glossiness;
    46. float4 _SpecularColor;
    47. sampler2D _MainTex;
    48. sampler2D _ScanA;
    49. sampler2D _ScanB;
    50. sampler2D _ScanForeground;
    51. float4 _ScanColor;
    52.  
    53.             struct EditorSurfaceOutput {
    54.                 half3 Albedo;
    55.                 half3 Normal;
    56.                 half3 Emission;
    57.                 half3 Gloss;
    58.                 half Specular;
    59.                 half Alpha;
    60.                 half4 Custom;
    61.             };
    62.          
    63.             inline half4 LightingBlinnPhongEditor_PrePass (EditorSurfaceOutput s, half4 light)
    64.             {
    65. half3 spec = light.a * s.Gloss;
    66. half4 c;
    67. c.rgb = (s.Albedo * light.rgb + light.rgb * spec);
    68. c.a = s.Alpha;
    69. return c;
    70.  
    71.             }
    72.  
    73.             inline half4 LightingBlinnPhongEditor (EditorSurfaceOutput s, half3 lightDir, half3 viewDir, half atten)
    74.             {
    75.                 half3 h = normalize (lightDir + viewDir);
    76.              
    77.                 half diff = max (0, dot ( lightDir, s.Normal ));
    78.              
    79.                 float nh = max (0, dot (s.Normal, h));
    80.                 float spec = pow (nh, s.Specular*128.0);
    81.              
    82.                 half4 res;
    83.                 res.rgb = _LightColor0.rgb * diff;
    84.                 res.w = spec * Luminance (_LightColor0.rgb);
    85.                 res *= atten * 2.0;
    86.  
    87.                 return LightingBlinnPhongEditor_PrePass( s, res );
    88.             }
    89.          
    90.             struct Input {
    91.                 float2 uv_MainTex;
    92. float4 color : COLOR;
    93. float2 uv_ScanForeground;
    94. float2 uv_ScanB;
    95. float2 uv_ScanA;
    96. float3 viewDir;
    97.  
    98.             };
    99.  
    100.             void vert (inout appdata_full v, out Input o) {
    101.             UNITY_INITIALIZE_OUTPUT(Input,o);
    102. float4 VertexOutputMaster0_0_NoInput = float4(0,0,0,0);
    103. float4 VertexOutputMaster0_1_NoInput = float4(0,0,0,0);
    104. float4 VertexOutputMaster0_2_NoInput = float4(0,0,0,0);
    105. float4 VertexOutputMaster0_3_NoInput = float4(0,0,0,0);
    106.  
    107.  
    108.             }
    109.          
    110.  
    111.             void surf (Input IN, inout EditorSurfaceOutput o) {
    112.                 o.Normal = float3(0.0,0.0,1.0);
    113.                 o.Alpha = 1.0;
    114.                 o.Albedo = 0.0;
    115.                 o.Emission = 0.0;
    116.                 o.Gloss = 0.0;
    117.                 o.Specular = 0.0;
    118.                 o.Custom = 0.0;
    119.              
    120. float4 Sampled2D0=tex2D(_MainTex,IN.uv_MainTex.xy);
    121. float4 Multiply1=IN.color * _DiffuseColor;
    122. float4 Multiply3=Sampled2D0 * Multiply1;
    123. float4 Sampled2D3=tex2D(_ScanForeground,IN.uv_ScanForeground.xy);
    124. float4 Sampled2D2=tex2D(_ScanB,IN.uv_ScanB.xy);
    125. float4 Sampled2D1=tex2D(_ScanA,IN.uv_ScanA.xy);
    126. float4 Add0=Sampled2D2 + Sampled2D1;
    127. float4 Multiply4=Sampled2D3 * Add0;
    128. float4 Multiply5=_ScanColor * Multiply4;
    129. float4 Fresnel0_1_NoInput = float4(0,0,1,1);
    130. float4 Fresnel0=(1.0 - dot( normalize( float4( IN.viewDir.x, IN.viewDir.y,IN.viewDir.z,1.0 ).xyz), normalize( Fresnel0_1_NoInput.xyz ) )).xxxx;
    131. float4 Pow0=pow(Fresnel0,_RimPower.xxxx);
    132. float4 Multiply0=_RimColor * Pow0;
    133. float4 Multiply2=IN.color * Multiply0;
    134. float4 Add1=Multiply5 + Multiply2;
    135. float4 Master0_1_NoInput = float4(0,0,1,1);
    136. float4 Master0_5_NoInput = float4(1,1,1,1);
    137. float4 Master0_7_NoInput = float4(0,0,0,0);
    138. float4 Master0_6_NoInput = float4(1,1,1,1);
    139. o.Albedo = Multiply3;
    140. o.Emission = Add1;
    141. o.Specular = _Glossiness.xxxx;
    142. o.Gloss = _SpecularColor;
    143.  
    144.                 o.Normal = normalize(o.Normal);
    145.             }
    146.         ENDCG
    147.     }
    148.     Fallback "Diffuse"
    149. }
     
  16. Jonny-Roy

    Jonny-Roy

    Joined:
    May 29, 2013
    Posts:
    666
    Well, it's mainly this section:

    Code (CSharp):
    1. if((_a * IN.texcoord.x + _b - IN.texcoord.y) * _under > 0)
    2.                 {
    3.                     return tex2D(_MainTex, IN.texcoord) * IN.color;
    4.                  
    5.                 }
    6.                 else
    7.                 {
    8.                     return 0;
    9.                 }
    With shader code the parts in the frag function basically get called for every pixel and so need to be as efficient as possible, "if" statements are one of the slowest functions you can call, so should be avoided, basically replace the above with this:


    Code (CSharp):
    1. float blankVal=(_a * IN.texcoord.x + _b - IN.texcoord.y) * _under;
    2.                 return tex2D(_MainTex, IN.texcoord) * IN.color * (1 - step(blankVal, 0.0));
    Basically with if, the shader runs both bits of code every time, then decides which value to go with after it evaluates the if, it's just because they batch process multiple pixels at one, I've changed this to use the step command which basically compares the first value to the second and return 0 if the second is greater than or equal to the second otherwise 1 (which is why I did 1 - the step), it should give the same result and be quite a bit faster.
     
  17. Jonny-Roy

    Jonny-Roy

    Joined:
    May 29, 2013
    Posts:
    666
    Hi Cygnusfear,

    I'd need more of a repro showing the issue with this as it's a little more complex and specific, if you can upload a simple sample project I'll see what I can do.

    Thanks,

    Jon
     
    cygnusfear likes this.
  18. Jonny-Roy

    Jonny-Roy

    Joined:
    May 29, 2013
    Posts:
    666
    Actually as a guess...try changing this:
    #pragma surface surf StandardSpecular fullforwardshadows addshadow

    To this:
    #pragma surface surf StandardSpecular alpha:blend fullforwardshadows addshadow

    Not sure if it will work, but might be the issue.
     
  19. Jonny-Roy

    Jonny-Roy

    Joined:
    May 29, 2013
    Posts:
    666
    No problem Kiron, out of interest are you targeting windows store with this? Or just iOS, android? It's a slightly trickier one to fix this one, but depending on what your targeting depends on how easy the fix is!
     
  20. kiron46

    kiron46

    Joined:
    Mar 4, 2015
    Posts:
    7
    Well, we are targeting the Steam platform at the moment, but in the future we're looking to do iOS and Android. It would be great if you can just help us out with a PC solution and we'll worry about mobile later.

    Thanks so much for helping!

    And just curious, do you recommend any tutorials/learning resources for starting to program shaders for Unity? It seems to be a very uncommonly educated topic in programming, like specific knowledge for triple A studios
     
    Last edited: Mar 10, 2015
  21. cygnusfear

    cygnusfear

    Joined:
    Oct 24, 2012
    Posts:
    14
    Hey Jonny thank you for taking the time to help out, I've prepared a scene showing the issue which I'll send you by PM!

    Cheers,
    Alex
     
  22. cygnusfear

    cygnusfear

    Joined:
    Oct 24, 2012
    Posts:
    14
    Hey Jonny,

    Not sure if you got the message I sent, the test scene I'm talking about can be found here in case you'd still like to help:

    http://alexandermangel.com/dropbox/shaderproblem.zip

    Cheers,
    Alex
     
  23. K0ndor

    K0ndor

    Joined:
    Mar 12, 2015
    Posts:
    8
    Please Johny Roy, could you help me with this shader? I switched to Unity 5 today and I'm sad to see my buildings in pink :(. Only thing I managed to do on my own was making them black...

    Code (CSharp):
    1.  
    2. Shader "buildingshader" {
    3.     Properties {
    4.         _uvMultiplier ("UV multiplier", Vector) = (1,1,0)
    5.         _cameraPosition ("Camera position", Vector) = (1.939751,0.1998241,-4.33008)
    6.         _wallFrequencies ("Wall freq", Vector) = (1,1,1)
    7.        
    8.         _ceilingTexture  ("Ceiling texture", 2D) = "white" { TexGen EyeLinear }
    9.         _floorTexture ("Floor texture", 2D) = "red" { TexGen EyeLinear }
    10.         _wallXYTexture ("wallXY texture", 2D) = "black" { TexGen EyeLinear }
    11.         _wallZYTexture ("wallZY texture", 2D) = "green" { TexGen EyeLinear }
    12.         _diffuseTexture ("Diffuse texture", 2D) = "green" { TexGen EyeLinear }    
    13.         _noiseTexture( "Noise texture", 2D) = "green" { TexGen EyeLinear }
    14.        
    15.         _CubeTex("Cubemap day", CUBE) = "" { TexGen CubeReflect}
    16.         _CubeTex2("Cubemap night", CUBE) = "" { TexGen CubeReflect}      
    17.     }
    18.     SubShader {
    19.          Pass {
    20.        
    21.              CGPROGRAM
    22.            
    23.             #pragma target 3.0
    24.             #pragma exclude_renderers xbox360
    25.             #pragma vertex vert
    26.             #pragma fragment frag
    27.            
    28.             #include "UnityCG.cginc"
    29.            
    30.             struct v2f {
    31.            
    32.                 float4 pos:    SV_POSITION;
    33.                 float2 uv:TEXCOORD0;
    34.                 float3 positionCopy:TEXCOORD1;
    35.                 float3 reflection:TEXCOORD2;
    36.                 float4 lighting;
    37.             };
    38.  
    39.             float3 _uvMultiplier;
    40.             float3 _cameraPosition;
    41.             float3 _wallFrequencies;
    42.             float _lightThreshold;
    43.            
    44.             v2f vert (appdata_base v)
    45.             {
    46.                 v2f o;
    47.                
    48.                 o.pos = mul (UNITY_MATRIX_MVP, v.vertex) ;
    49.                 o.uv = v.texcoord * float2(_uvMultiplier);
    50.                 o.positionCopy = float3(v.vertex);
    51.                                
    52.                 float3 cameraPosition = _WorldSpaceCameraPos;
    53.                
    54.                 float4 worldPosition = mul(_Object2World, v.vertex);
    55.                 float3 worldNormal = mul(float3x3(_Object2World), v.normal);
    56.                
    57.                 o.reflection = reflect(worldPosition - cameraPosition , worldNormal);
    58.                
    59.                 // Calculate lighting on the exterior of the building with a hard-coded directed light.
    60.                 float lightStrength = dot(v.normal, float3(0.5, 0.33166, 0.8));
    61.                 o.lighting = saturate(lightStrength) * float4(1, 1, 0.9, 1) * (1-_lightThreshold);
    62.                
    63.                 // Add some ambient lighting.
    64.                 o.lighting += float4(0.3, 0.3, 0.4, 1);
    65.                
    66.                 return o;
    67.             }
    68.  
    69.            
    70.             sampler2D _ceilingTexture;
    71.             sampler2D _floorTexture;
    72.             sampler2D _wallXYTexture;
    73.             sampler2D _wallZYTexture;
    74.             sampler2D _diffuseTexture;
    75.             sampler2D _noiseTexture;
    76.             samplerCUBE _CubeTex;
    77.             samplerCUBE _CubeTex2;
    78.            
    79.             half4 frag (v2f i) : COLOR
    80.             {
    81.                
    82.                 //position in object space
    83.                 float3 direction = i.positionCopy - _cameraPosition;
    84.        
    85.                 //multiply by 0.999 to prevent last wall from beeing displayed. Fix this?
    86.                 float3 corner = floor(i.positionCopy *_wallFrequencies * 0.999);
    87.                 float3 walls = corner + step(float3(0, 0, 0), direction);
    88.                 walls /= _wallFrequencies;
    89.                 corner /= _wallFrequencies;
    90.                
    91.                 float3 rayFractions = (float3(walls.x, walls.y,walls.z) - _cameraPosition) / direction;
    92.                 float2 intersectionXY = (_cameraPosition + rayFractions.z * direction).xy;
    93.                 float2 intersectionXZ = (_cameraPosition + rayFractions.y * direction).xz;
    94.                 float2 intersectionZY = (_cameraPosition + rayFractions.x * direction).zy;
    95.                
    96.                 float4 ceilingColour = tex2D(_ceilingTexture, intersectionXZ);
    97.                 float4 floorColour = tex2D(_floorTexture, intersectionXZ);
    98.                 float4 verticalColour = lerp(floorColour, ceilingColour, step(0, direction.y));
    99.                
    100.                 //random texture on wall xy
    101.                 float zNoise = tex2D(_noiseTexture, float2(corner.z/64,0)).r;
    102.                 float yNoise = tex2D(_noiseTexture, float2(corner.y/64 + zNoise,0)).r;
    103.                 float noiseXY = tex2D(_noiseTexture, float2(corner.x/64 + yNoise,0)).r;
    104.            
    105.                 noiseXY = floor(noiseXY * 4) / 4;
    106.                 float2 atlasIndexXY;
    107.                 atlasIndexXY[0] = floor(noiseXY * 2) / 2;
    108.                 atlasIndexXY[1] = (noiseXY - atlasIndexXY[0]) * 2;
    109.                
    110.                 //put the intersection into room space, so that it comes within [0, 1]
    111.                 intersectionXY = (intersectionXY - corner.xy) * _wallFrequencies.xy;
    112.                
    113.                 //use the texture coordinate to read from the correct texture in the atlas
    114.                 float4 wallXYColour = 0.8 * tex2D(_wallXYTexture, atlasIndexXY + intersectionXY / 2);
    115.                
    116.                 //random texture on wall ZY
    117.                 float zNoise2 = tex2D(_noiseTexture, float2(corner.z/64,0)).g;
    118.                 float yNoise2 = tex2D(_noiseTexture, float2(corner.y/64 + zNoise2,0)).g;
    119.                 float noiseZY = tex2D(_noiseTexture, float2(corner.x/64 + yNoise2,0)).g;
    120.                 float2 atlasIndexZY;
    121.                 atlasIndexZY[0] = floor(noiseZY * 2) / 2;
    122.                 atlasIndexZY[1] = 0;//(noiseZY - atlasIndexZY[0]) * 2;
    123.                
    124.                 //put the intersection into room space, so that it comes within [0, 1]
    125.                 intersectionZY = (intersectionZY - corner.zy) * _wallFrequencies.zy;
    126.                
    127.                 //use the texture coordinate to read from the correct texture in the atlas
    128.                 float4 wallZYColour = 0.8 * tex2D(_wallZYTexture, atlasIndexZY + intersectionZY / 2);
    129.        
    130.                 //decide wich wall is closest to camera
    131.                 float xVSz = step(rayFractions.x, rayFractions.z);
    132.                 float4 interiorColour = lerp(wallXYColour, wallZYColour, xVSz);
    133.                 float rayFraction_xVSz = lerp(rayFractions.z, rayFractions.x, xVSz);
    134.                
    135.                 //calculate variation in the lighting per room
    136.                 float3 noises = float3(
    137.                     tex2D(_noiseTexture, float2(corner.x, corner.y)/64).r,
    138.                     tex2D(_noiseTexture, float2(corner.z, corner.x)/64).r,
    139.                     tex2D(_noiseTexture, float2(corner.y, corner.z)/64).r
    140.                 );
    141.                
    142.                 float lightVariation = step((noises.x + noises.y + noises.z)/3, _lightThreshold*0.6f)* 0.3 + noises.x;
    143.                
    144.                 float xzVSy = step(rayFraction_xVSz, rayFractions.y);
    145.                 //floor/ceiling or walls
    146.                 interiorColour = lerp(verticalColour, interiorColour, xzVSy)* lightVariation;
    147.                
    148.                 //blend colors
    149.                 float4 diffuseColour = tex2D(_diffuseTexture, i.uv);
    150.                 float4 wallColour = diffuseColour * i.lighting;
    151.                 float4 cubeColour = lerp(float4(0,0,0,1), lerp(texCUBE(_CubeTex, i.reflection ),texCUBE(_CubeTex2, i.reflection ),_lightThreshold),1.0F);
    152.                 float4 windowColour = cubeColour + interiorColour;
    153.                
    154.                 return lerp(wallColour, windowColour, diffuseColour.a);
    155.                
    156.             }
    157.  
    158.             ENDCG  
    159.         }
    160.     }
    161. }
    162.  
     
  24. smurley

    smurley

    Joined:
    Nov 29, 2012
    Posts:
    3
    Jonny Roy, could you please help us with this shader too? It's making all of my materials disappear:

    Shader"TransparentVertexLit" {
    Properties {
    _Color ("MainColor", Color) = (1,1,1,1)
    _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    }

    SubShader {
    Tags {"RenderType"="Transparent""Queue"="Transparent"}
    //Renderintodepthbufferonly
    Pass {
    ColorMask0
    }
    //Rendernormally
    Pass {
    ZWriteOff
    BlendSrcAlphaOneMinusSrcAlpha
    ColorMaskRGB
    Material {
    Diffuse [_Color]
    Ambient [_Color]
    }
    LightingOn
    SetTexture [_MainTex] {
    Combinetexture * primaryDOUBLE, texture * primary
    }
    }
    }
    }

    Thanks!
     
  25. smurley

    smurley

    Joined:
    Nov 29, 2012
    Posts:
    3
    Jonny Roy...please ignore my request. I imported the new Standard shader into my project and used that. Thanks anyway.
     
    Jonny-Roy likes this.
  26. Aren

    Aren

    Joined:
    May 10, 2013
    Posts:
    5
    Hello, I followed a tutorial for creating a shader with anisotropic specular using a normal map. However, while the effect of the specular is good enough, the mainTex doesn't preserve the alpha value completely. The albedo is visible, the specular is visible, but the shadows behind the object using this shader are also visible. It´s like the object is transparent for shadows. How do I fix it? I'm new coding shaders and my brain is still processing how I did what I did. @Jonny Roy could you please throw some light into this?
    Im using Unity 5.0.1.

    Code (CSharp):
    1. Shader "Custom/Anisotropic" {
    2.     Properties
    3.     {
    4.         _MainTint ("Diffuse Tint", Color)=(1,1,1,1)
    5.         _MainTex ("Base (RGB)", 2D) = "white" {}
    6.         _SpecularColor ("Specular Color", Color) = (0,0,0,0)
    7.         _Specular ("Specular Amount", Range(0,1)) = 0.5
    8.         _SpecPower ("Specular Power", Range(0,1)) = 0.5
    9.         _AnisoDir ("Anisotropic Direction", 2D) = "white" {}
    10.         _AnisoOffset ("Anisotropic Offset", Range(-1,1)) = -0.2
    11.     }
    12.    
    13.     Subshader
    14.     {
    15.         Tags { "RenderType" = "Opaque" }
    16.        
    17.         CGPROGRAM
    18.         sampler2D _MainTex, _AnisoDir;
    19.         float4 _MainTint, _SpecularColor;
    20.         float _Specular, _SpecPower, _AnisoOffset;
    21.            
    22.    
    23.         struct SurfaceAnisoOutput
    24.         {
    25.             fixed3 Albedo;
    26.             fixed3 Normal;
    27.             fixed3 AnisoDirection;
    28.             fixed3 Emission;
    29.             half Specular;
    30.             fixed Gloss;
    31.             fixed Alpha;
    32.         };  
    33.        
    34.         inline fixed4 LightingAnisotropic (SurfaceAnisoOutput s, fixed3 lightDir, half3 viewDir, fixed atten)
    35.         {  
    36.             fixed3 halfVector = normalize(normalize(lightDir) + normalize(viewDir));
    37.             float NdotL = saturate(dot(s.Normal,lightDir));
    38.        
    39.             fixed HdotA = dot (normalize(s.Normal + s.AnisoDirection) , halfVector );
    40.             //float bridge = HdotA + _AnisoOffset;
    41.             //bridge*=180;
    42.             //float aniso = max(0, sin(radians(bridge)));
    43.             float aniso = max(0, sin(radians((HdotA + _AnisoOffset)* 180)));
    44.        
    45.             //float spec = saturate (pow(aniso, s.Gloss * 128)* s.Specular);    //I changed this...
    46.             float spec = saturate (pow(aniso, _SpecPower * 128)* s.Specular);    //...into this since the _SpecPower wasn't been use.
    47.        
    48.             fixed4 c;
    49.             c.rgb = ((s.Albedo * _LightColor0.rgb * NdotL)+(_LightColor0.rgb * _SpecularColor.rgb * spec))*(atten * 2);
    50.             c.a = 1.0;
    51.             return c;
    52.         }
    53.        
    54.         #pragma surface surf Anisotropic
    55.         #pragma target 3.0
    56.        
    57.         struct Input
    58.         {
    59.             float2 uv_MainTex;
    60.             float2 uv_AnisoDir;
    61.         };
    62.    
    63.         void surf (Input IN, inout SurfaceAnisoOutput o)
    64.         {
    65.             half4 c = tex2D(_MainTex, IN.uv_MainTex) * _MainTint;
    66.             float3 anisoTex = UnpackNormal(tex2D(_AnisoDir, IN.uv_AnisoDir));
    67.        
    68.             o.AnisoDirection = anisoTex;
    69.             o.Specular = _Specular;
    70.             o.Gloss = _Specular;
    71.             o.Albedo = c.rgb;
    72.             o.Alpha = c.a;
    73.         }
    74.    
    75.     ENDCG
    76.     }
    77.    
    78. }
    79.  
    AnisoSpecHelp.jpg

    Thanks in advance.
     
  27. Zicandar

    Zicandar

    Joined:
    Feb 10, 2014
    Posts:
    388
    Try outputting 1 in the alpha channel?
    Also might want to set the queue to geometry.

    Or are you talking about the glass thing not getting shadows?
    If so, well, unity decided that they don't feel like supporting shadow receiving transparent stuff. (Even tho it's really easy to do!) I understand transparent stuff not CASTING shadows, but not receiving, that's just stupid.
     
  28. Aren

    Aren

    Joined:
    May 10, 2013
    Posts:
    5
    1. Already tried that. Didn't work.
    2. Didn't work either.
    3. No, transparent objects do cast shadows on other objects, the intensity of it depends on the alpha value. What I point in the image is that the cylinder using the shader I did is not receiving the shadows casted by the "glass thing", and neither it's casting shadows itself.
     
  29. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    Add a fallback to enable the shadow passes:
    Code (csharp):
    1. Fallback "Mobile/Diffuse"
    (Has nothing to do with Unity 5 by the way. Was the same in 4.)
     
  30. Aren

    Aren

    Joined:
    May 10, 2013
    Posts:
    5
    @jvo3dc Thank you! that did the trick.

    Aniso.jpg

    Now, I will try to use the same normal as a normal normal (got it?) and add some reflections.
     
  31. Rbhaniwal

    Rbhaniwal

    Joined:
    Jan 23, 2015
    Posts:
    33
    @ jonney roy
    hello, Plz help me too.My shader working well in unity 4 but in unity 5 it does nothing.
    i didn't know anything about Shaders. plz help me.

    this is my shader :

    Shader"Custom/FogOfWar" {
    Properties {
    _Color ("MainColor", Color) = (1,1,1,1)
    _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    _FogRadius ("FogRadius", Float) = 1.0
    _FogMaxRadius("FogMaxRadius", Float) = 0.5
    _Player1_Pos ("_Player1_Pos", Vector) = (0,0,0,1)
    _Player2_Pos ("_Player2_Pos", Vector) = (0,0,0,1)
    _Player3_Pos ("_Player3_Pos", Vector) = (0,0,0,1)
    }

    SubShader {
    Tags {"Queue"="Transparent""IgnoreProjector"="True""RenderType"="Transparent"}
    LOD200
    BlendSrcAlphaOneMinusSrcAlpha
    CullOff

    CGPROGRAM
    #pragmasurfacesurfLambertvertex:vert

    sampler2D_MainTex;
    fixed4_Color;
    float_FogRadius;
    float_FogMaxRadius;
    float4_Player1_Pos;
    float4_Player2_Pos;
    float4_Player3_Pos;

    structInput {
    float2uv_MainTex;
    float2location;
    };

    floatpowerForPos(float4pos, float2nearVertex);

    voidvert(inoutappdata_fullvertexData, outInputoutData) {
    float4pos = mul(UNITY_MATRIX_MVP, vertexData.vertex);
    float4posWorld = mul(_Object2World, vertexData.vertex);
    outData.uv_MainTex = vertexData.texcoord;
    outData.location = posWorld.xz;
    }

    voidsurf (InputIN, inoutSurfaceOutputo)

    {
    fixed4baseColor = tex2D(_MainTex, IN.uv_MainTex) * _Color;

    floatalpha = (1.0 - (baseColor.a + powerForPos(_Player1_Pos, IN.location) + powerForPos(_Player2_Pos, IN.location) + powerForPos(_Player3_Pos, IN.location)));

    o.Albedo = baseColor.rgb;
    o.Alpha = alpha;
    }

    //return0if (pos - nearVertex) > _FogRadius
    floatpowerForPos(float4pos, float2nearVertex) {
    floatatten = clamp(_FogRadius - length(pos.xz - nearVertex.xy), 0.0, _FogRadius);

    return (1.0/_FogMaxRadius)*atten/_FogRadius;
    }

    ENDCG
    }

    Fallback"Transparent/VertexLit"
    }
     
  32. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    Can you post your shader in a code block? It seems to be missing a lot of spaces.
     
  33. Rbhaniwal

    Rbhaniwal

    Joined:
    Jan 23, 2015
    Posts:
    33
    my shader --->

    Code (CSharp):
    1. Shader "Custom/FogOfWar" {
    2. Properties {
    3.     _Color ("Main Color", Color) = (1,1,1,1)
    4.     _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    5.     _FogRadius ("FogRadius", Float) = 1.0
    6.     _FogMaxRadius("FogMaxRadius", Float) = 0.5
    7.     _Player1_Pos ("_Player1_Pos", Vector) = (0,0,0,1)
    8.     _Player2_Pos ("_Player2_Pos", Vector) = (0,0,0,1)
    9.     _Player3_Pos ("_Player3_Pos", Vector) = (0,0,0,1)
    10. }
    11.  
    12. SubShader {
    13.     Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
    14.     LOD 200
    15.     Blend SrcAlpha OneMinusSrcAlpha
    16.     Cull Off
    17.  
    18.     CGPROGRAM
    19.     #pragma surface surf Lambert vertex:vert
    20.  
    21.     sampler2D _MainTex;
    22.     fixed4     _Color;
    23.     float     _FogRadius;
    24.     float     _FogMaxRadius;
    25.     float4     _Player1_Pos;
    26.     float4     _Player2_Pos;
    27.     float4     _Player3_Pos;
    28.    
    29.     struct Input {
    30.         float2 uv_MainTex;
    31.         float2 location;
    32.     };
    33.    
    34.     float powerForPos(float4 pos, float2 nearVertex);
    35.    
    36.     void vert(inout appdata_full vertexData, out Input outData) {
    37.         float4 pos = mul(UNITY_MATRIX_MVP, vertexData.vertex);
    38.         float4 posWorld = mul(_Object2World, vertexData.vertex);
    39.         outData.uv_MainTex = vertexData.texcoord;
    40.         outData.location = posWorld.xz;
    41.     }
    42.    
    43.     void surf (Input IN, inout SurfaceOutput o)
    44.    
    45.     {
    46.         fixed4 baseColor = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    47.        
    48.         float alpha = (1.0 - (baseColor.a + powerForPos(_Player1_Pos, IN.location) + powerForPos(_Player2_Pos, IN.location) + powerForPos(_Player3_Pos, IN.location)));
    49.        
    50.         o.Albedo = baseColor.rgb;
    51.         o.Alpha = alpha;
    52.     }
    53.    
    54.     //return 0 if (pos - nearVertex) > _FogRadius
    55.     float powerForPos(float4 pos, float2 nearVertex) {
    56.         float atten = clamp(_FogRadius - length(pos.xz - nearVertex.xy), 0.0, _FogRadius);
    57.        
    58.         return (1.0/_FogMaxRadius)*atten/_FogRadius;
    59.     }
    60.    
    61.     ENDCG
    62. }
    63.  
    64. Fallback "Transparent/VertexLit"
    65. }
     
  34. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    What kind of errors do you get?
     
  35. SoftwareGeezers

    SoftwareGeezers

    Joined:
    Jun 22, 2013
    Posts:
    902
    For those with issues with transparency, I fixed mine just by setting the alpha to use s.alpha as per this Upgrade Guide.
     
  36. bakno

    bakno

    Joined:
    Mar 18, 2007
    Posts:
    604
    Hi

    Can you please help me with this shader?

    Shader "Terrain/BiBlended" {
    Properties {
    _MainTex ("Base (RGB)", 2D) = "white" {}
    _DetailTex ("Detail (RGB)", 2D) = "white" {}
    _TrackText ("Track (RGB)", 2D) = "white" {}
    _BlendMap1 ("BlendMap1 (RGBA)", 2D) = "white" {}
    _TrackMap ("TrackMap (RGBA)", 2D) = "white" {}
    }
    SubShader {
    Tags { "RenderType"="Opaque" }
    LOD 200

    CGPROGRAM
    #pragma surface surf Lambert

    sampler2D _MainTex;
    sampler2D _DetailTex;
    sampler2D _TrackText;
    sampler2D _BlendMap1;
    sampler2D _TrackMap;

    struct Input {
    float2 uv_MainTex;
    float2 uv_DetailTex;
    float2 uv_TrackText;
    float2 uv_BlendMap1;
    float2 uv_TrackMap;
    };

    void surf (Input IN, inout SurfaceOutput o) {
    float3 t1 = tex2D (_DetailTex, IN.uv_DetailTex).rgb;
    float3 t2 = tex2D (_MainTex, IN.uv_MainTex).rgb;
    float3 t3 = tex2D (_TrackText, IN.uv_TrackText).rgb;

    float4 nt1 = float4 (t1[0], t1[1], t1[2], 1);
    float4 nt2 = float4 (t2[0], t2[1], t2[2], 1);
    float4 nt3 = float4 (t3[0], t3[1], t3[2], 1);

    float4 map1 = tex2D (_BlendMap1, IN.uv_BlendMap1).rgba;
    float4 map2 = tex2D (_TrackMap, IN.uv_TrackMap).rgba;

    float4 res = (nt1 * map1.a) + (nt2 * (1 - map1.a));
    res = (res * map2.a) + (nt3 * (1 - map2.a));

    o.Albedo = res * 0.8;
    }
    ENDCG
    }
    FallBack "Diffuse"
    }
     
  37. redstonegames

    redstonegames

    Joined:
    Sep 19, 2015
    Posts:
    11
    @Jonny-Roy May you help me to convert both of these shaders (Unity 4 to Unity 5)? I really appreciate any help you can provide.

    Code (CSharp):
    1. Shader "Custom/Stencil/Default RefEqualOne"
    2. {
    3.     Properties
    4.     {
    5.         [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
    6.         _Color ("Tint", Color) = (1,1,1,1)
    7.         [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
    8.     }
    9.  
    10.     SubShader
    11.     {
    12.         Tags
    13.         {
    14.             "Queue"="Transparent"
    15.             "IgnoreProjector"="True"
    16.             "RenderType"="Transparent"
    17.             "PreviewType"="Plane"
    18.             "CanUseSpriteAtlas"="True"
    19.         }
    20.  
    21.         Cull Off
    22.         Lighting Off
    23.         ZWrite Off
    24.         Blend One OneMinusSrcAlpha
    25.      
    26.         Stencil
    27.         {
    28.             Ref 1
    29.             Comp equal
    30.             Pass keep
    31.         }
    32.  
    33.         Pass
    34.         {
    35.         CGPROGRAM
    36.             #pragma vertex vert
    37.             #pragma fragment frag
    38.             #pragma multi_compile _ PIXELSNAP_ON
    39.             #include "UnityCG.cginc"
    40.          
    41.             struct appdata_t
    42.             {
    43.                 float4 vertex   : POSITION;
    44.                 float4 color    : COLOR;
    45.                 float2 texcoord : TEXCOORD0;
    46.             };
    47.  
    48.             struct v2f
    49.             {
    50.                 float4 vertex   : SV_POSITION;
    51.                 fixed4 color    : COLOR;
    52.                 half2 texcoord  : TEXCOORD0;
    53.             };
    54.          
    55.             fixed4 _Color;
    56.  
    57.             v2f vert(appdata_t IN)
    58.             {
    59.                 v2f OUT;
    60.                 OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
    61.                 OUT.texcoord = IN.texcoord;
    62.                 OUT.color = IN.color * _Color;
    63.                 #ifdef PIXELSNAP_ON
    64.                     OUT.vertex = UnityPixelSnap (OUT.vertex);
    65.                 #endif
    66.  
    67.                 return OUT;
    68.             }
    69.  
    70.             sampler2D _MainTex;
    71.             sampler2D _AlphaTex;
    72.             float _AlphaSplitEnabled;
    73.  
    74.             fixed4 SampleSpriteTexture (float2 uv)
    75.             {
    76.                 fixed4 color = tex2D (_MainTex, uv);
    77.                 if (_AlphaSplitEnabled)
    78.                     color.a = tex2D (_AlphaTex, uv).r;
    79.  
    80.                 return color;
    81.             }
    82.  
    83.             fixed4 frag(v2f IN) : SV_Target
    84.             {
    85.                 fixed4 c = SampleSpriteTexture (IN.texcoord) * IN.color;
    86.                 c.rgb *= c.a;
    87.                 return c;
    88.             }
    89.         ENDCG
    90.         }
    91.     }
    92. }
    93.  
    Code (CSharp):
    1. Shader "Custom/Stencil/Mask OneZLess"
    2. {
    3.     Properties
    4.     {
    5.         [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
    6.         _Color ("Tint", Color) = (1,1,1,1)
    7.         [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
    8.     }
    9.  
    10.     SubShader
    11.     {
    12.         Tags
    13.         {
    14.             "Queue"="Transparent"
    15.             "IgnoreProjector"="True"
    16.             "RenderType"="Transparent"
    17.             "PreviewType"="Plane"
    18.             "CanUseSpriteAtlas"="True"
    19.         }
    20.  
    21.         Cull Off
    22.         Lighting Off
    23.         ZWrite Off
    24.         Blend One OneMinusSrcAlpha
    25.      
    26.         Stencil
    27.         {
    28.             Ref 1
    29.             Comp always
    30.             Pass replace
    31.         }
    32.  
    33.         Pass
    34.         {
    35.         CGPROGRAM
    36.             #pragma vertex vert
    37.             #pragma fragment frag
    38.             #pragma multi_compile _ PIXELSNAP_ON
    39.             #include "UnityCG.cginc"
    40.          
    41.             struct appdata_t
    42.             {
    43.                 float4 vertex   : POSITION;
    44.                 float4 color    : COLOR;
    45.                 float2 texcoord : TEXCOORD0;
    46.             };
    47.  
    48.             struct v2f
    49.             {
    50.                 float4 vertex   : SV_POSITION;
    51.                 fixed4 color    : COLOR;
    52.                 half2 texcoord  : TEXCOORD0;
    53.             };
    54.          
    55.             fixed4 _Color;
    56.  
    57.             v2f vert(appdata_t IN)
    58.             {
    59.                 v2f OUT;
    60.                 OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
    61.                 OUT.texcoord = IN.texcoord;
    62.                 OUT.color = IN.color * _Color;
    63.                 #ifdef PIXELSNAP_ON
    64.                     OUT.vertex = UnityPixelSnap (OUT.vertex);
    65.                 #endif
    66.  
    67.                 return OUT;
    68.             }
    69.  
    70.             sampler2D _MainTex;
    71.             sampler2D _AlphaTex;
    72.             float _AlphaSplitEnabled;
    73.  
    74.             fixed4 SampleSpriteTexture (float2 uv)
    75.             {
    76.                 fixed4 color = tex2D (_MainTex, uv);
    77.                 if (_AlphaSplitEnabled)
    78.                     color.a = tex2D (_AlphaTex, uv).r;
    79.  
    80.                 return color;
    81.             }
    82.  
    83.             fixed4 frag(v2f IN) : SV_Target
    84.             {
    85.                 fixed4 c = SampleSpriteTexture (IN.texcoord) * IN.color;
    86.                 c.rgb *= c.a;
    87.                 if (c.a < 0.75) discard;
    88.                 return c;
    89.             }
    90.         ENDCG
    91.         }
    92.     }
    93. }
     
    Last edited: Jan 19, 2017