Search Unity

Difficulty with shading using custom vert-frag shader

Discussion in 'Shaders' started by AisuStraiku, Apr 15, 2014.

  1. AisuStraiku

    AisuStraiku

    Joined:
    Apr 15, 2014
    Posts:
    3
    Simple shader I wrote to skew objects in my scene and get lit by a directional light. I'm having trouble getting my objects to cast shadows with this shader attached to them. Any help would be appreciated.

    Code (csharp):
    1. Shader "_Shaders/VertSkew"
    2. {
    3.     Properties
    4.     {
    5.         _Color ("Main Color", Color) = (1,1,1,1)
    6.         _Mod ("ShaderMod", Float) = 0.5
    7.         _MainTex ("Main Texture", 2D) = "white" {}     
    8.     }
    9.  
    10.     SubShader
    11.     {
    12.         Tags {"LightMode"="ForwardBase" }
    13.         Pass
    14.         {
    15. CGPROGRAM
    16. #pragma vertex vert
    17. #pragma fragment frag
    18. #pragma fragmentoption ARB_precision_hint_fastest
    19. #pragma multi_compile SHADOWS_NATIVE SHADOWS_CUBE
    20.  
    21. #include "UnityCG.cginc"            
    22. #include "Prime31.cginc"
    23.  
    24. //uniforms
    25. uniform fixed4 _Color;
    26. uniform float _Mod;
    27. uniform sampler2D _MainTex;
    28. uniform float4 _MainTex_ST;
    29. uniform fixed3 _LightColor0;
    30.  
    31. struct vertexInput
    32. {
    33.     float4 vertex : POSITION;
    34.     float4 color : COLOR0;
    35.     float3 normal : NORMAL;
    36.     float4 texcoord : TEXCOORD0;
    37. };    
    38.  
    39. struct fragmentInput
    40. {
    41.     float4 pos : SV_POSITION;
    42.     float4 color : COLOR0;
    43.     half2 uv : TEXCOORD0;
    44. };        
    45.  
    46.            
    47.  
    48. fragmentInput vert(vertexInput v)
    49. {
    50.     fragmentInput o;
    51.    
    52.     o.pos = mul (_Object2World, v.vertex);
    53.    
    54.     float4 pos = mul(_Object2World, v.vertex);
    55.  
    56.     float y = pos.y;
    57.     float x = pos.x;                    
    58.     float z = pos.z;                    
    59.    
    60.     pos.y = y + (z * _Mod);
    61.     pos.x = x;                    
    62.     pos.z = z;              
    63.     o.pos = mul(UNITY_MATRIX_VP, pos);  
    64.    
    65.     o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
    66.  
    67.     float3 normalDirection = NORMAL_TO_WORLD(v.normal);
    68.  
    69.     float3 lightDirection = normalize(_WorldSpaceLightPos0.xyz);
    70.  
    71.     float3 diffuse = _LightColor0.xyz * _Color.rgb * max(0.0, dot(normalDirection,lightDirection));
    72.  
    73.     o.color = half4(diffuse, 1.0); 
    74.  
    75.     return o;
    76.  
    77. }            
    78.  
    79.            
    80.  
    81. fixed4 frag(fragmentInput i) : COLOR
    82. {
    83.     return tex2D (_MainTex, i.uv) * i.color;
    84. }
    85.                
    86. ENDCG
    87.  
    88.         }//End Pass
    89.  
    90.     }//End SubShader
    91.  
    92.     FallBack "Diffuse"
    93. }
     
  2. WhiskyJoe

    WhiskyJoe

    Joined:
    Aug 21, 2012
    Posts:
    143
    Literally 2 topics down someone posted the same kind of question..
     
  3. AisuStraiku

    AisuStraiku

    Joined:
    Apr 15, 2014
    Posts:
    3
    yes, I was having trouble implementing it into my own shader and I don't know enough about those equations to know what was going wrong.

    Here was my broken attempt at getting shadows, all I get is a list of errors

    Edit: Shader works now except the shadow isn't being translated the same way the verts are

    Code (csharp):
    1. Shader "_Shaders/VertSkew"
    2. {
    3.     Properties
    4.     {
    5.         _Color ("Main Color", Color) = (1,1,1,1)
    6.         _Mod ("ShaderMod", Float) = 0.5
    7.         _MainTex ("Main Texture", 2D) = "white" {}     
    8.     }
    9.  
    10.     SubShader
    11.     {
    12.         Tags {"LightMode"="ForwardBase" }
    13.         Pass
    14.         {
    15. CGPROGRAM
    16. #pragma vertex vert
    17. #pragma fragment frag
    18. #pragma fragmentoption ARB_precision_hint_fastest
    19. #pragma multi_compile_fwdbase
    20.  
    21. #include "UnityCG.cginc"            
    22. #include "Prime31.cginc"
    23. #include "AutoLight.cginc"
    24.  
    25. //uniforms
    26. uniform fixed4 _Color;
    27. uniform float _Mod;
    28. uniform sampler2D _MainTex;
    29. uniform float4 _MainTex_ST;
    30. uniform fixed3 _LightColor0;
    31.  
    32. struct vertexInput
    33. {
    34.     float4 vertex : POSITION;
    35.     float3 normal : NORMAL;
    36.     float4 texcoord : TEXCOORD0;
    37. };    
    38.  
    39. struct fragmentInput
    40. {
    41.     float4 pos : SV_POSITION;
    42.     float4 color : COLOR0;
    43.     half2 uv : TEXCOORD0;
    44.     LIGHTING_COORDS(1,2)
    45. };        
    46.  
    47.            
    48.  
    49. fragmentInput vert(vertexInput v)
    50. {
    51.     fragmentInput o;
    52.    
    53.     o.pos = mul (_Object2World, v.vertex);
    54.    
    55.     float4 pos = mul(_Object2World, v.vertex);
    56.  
    57.     float y = pos.y;
    58.     float x = pos.x;                    
    59.     float z = pos.z;                    
    60.    
    61.     pos.y = y + (z * _Mod);
    62.     pos.x = x;                    
    63.     pos.z = z;              
    64.     o.pos = mul(UNITY_MATRIX_VP, pos);  
    65.    
    66.     o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
    67.  
    68.     float3 normalDirection = NORMAL_TO_WORLD(v.normal);
    69.  
    70.     float3 lightDirection = normalize(_WorldSpaceLightPos0.xyz);
    71.  
    72.     float3 diffuse = _LightColor0.xyz * _Color.rgb * max(0.0, dot(normalDirection,lightDirection));
    73.  
    74.     o.color = half4(diffuse, 1.0); 
    75.  
    76.     TRANSFER_VERTEX_TO_FRAGMENT(o);
    77.  
    78.     return o;
    79. }                      
    80.  
    81. fixed4 frag(fragmentInput i) : COLOR
    82. {
    83.     float atten =  LIGHT_ATTENUATION(i);
    84.     return tex2D (_MainTex, i.uv) * i.color * atten;
    85. }
    86.                
    87. ENDCG
    88.  
    89.         }//End Pass
    90.  
    91.     }//End SubShader
    92.  
    93.     FallBack "VertexLit"
    94. }
     
    Last edited: Apr 15, 2014
  4. mouurusai

    mouurusai

    Joined:
    Dec 2, 2011
    Posts:
    350
    You should rewrite Shadowcaster/receiver pass, current one comes from fallback shader.
     
  5. IronMathbook

    IronMathbook

    Joined:
    Apr 12, 2014
    Posts:
    60
    I have a slightly different problem, the shader won't receive shadows but it can cast them. I think it is normal for it to cast them though. I'm not using
    LIGHT_ATTENUATION so I have no idea why you are having problems. I though shadows were built in to it.
     
  6. AisuStraiku

    AisuStraiku

    Joined:
    Apr 15, 2014
    Posts:
    3
    i wrote this shadow caster/receiver pass to move the shadows around but my logic is wrong somwhere obviously since they don't show up with this

    Code (csharp):
    1. Pass {
    2.  
    3.     Name "ShadowCaster"
    4.  
    5.     Tags { "LightMode" = "ShadowCaster" }
    6.  
    7.            
    8.  
    9.     Fog {Mode Off}
    10.  
    11.     ZWrite On ZTest LEqual Cull Off
    12.  
    13.     Offset 1, 1
    14.  
    15.      
    16.  
    17.     CGPROGRAM
    18.  
    19.     #pragma vertex vert
    20.  
    21.     #pragma fragment frag
    22.  
    23.     #pragma multi_compile_shadowcaster
    24.  
    25.     #pragma fragmentoption ARB_precision_hint_fastest
    26.  
    27.     #include "UnityCG.cginc"
    28.    
    29.     uniform float _VertexSkew;            
    30.  
    31.     struct v2f {
    32.  
    33.         V2F_SHADOW_CASTER;
    34.  
    35.     };
    36.  
    37.              
    38.  
    39.     v2f vert( appdata_base v ) {
    40.  
    41.         v2f o;
    42.                    
    43.         o.pos = mul (_Object2World, v.vertex);
    44.        
    45.         float4 pos = mul(_Object2World, v.vertex);
    46.  
    47.         float y = pos.y;
    48.         float x = pos.x;                    
    49.         float z = pos.z;                    
    50.        
    51.        
    52.        
    53.         pos.y = y + (z * _VertexSkew);
    54.         pos.x = x;                    
    55.         pos.z = z;              
    56.  
    57.         o.pos = mul(UNITY_MATRIX_VP, pos);  
    58.         //o.vec = o.pos.xyz;
    59.  
    60.         TRANSFER_SHADOW_CASTER(o)
    61.  
    62.         return o;
    63.  
    64.     }
    65.  
    66.              
    67.  
    68.     float4 frag( v2f i ) : COLOR {
    69.  
    70.         SHADOW_CASTER_FRAGMENT(i)
    71.  
    72.     }
    73.  
    74.     ENDCG
    75.  
    76. }
    77.  
    78.        
    79.  
    80. // Pass to render object as a shadow collector
    81.  
    82. Pass {
    83.  
    84.     Name "ShadowCollector"
    85.  
    86.     Tags { "LightMode" = "ShadowCollector" }
    87.  
    88.            
    89.  
    90.     Fog {Mode Off}
    91.  
    92.     ZWrite On ZTest LEqual
    93.  
    94.      
    95.  
    96.     CGPROGRAM
    97.  
    98.     #pragma vertex vert
    99.  
    100.     #pragma fragment frag
    101.  
    102.     #pragma fragmentoption ARB_precision_hint_fastest
    103.  
    104.     #pragma multi_compile_shadowcollector
    105.  
    106.        
    107.  
    108.     #define SHADOW_COLLECTOR_PASS
    109.  
    110.     #include "UnityCG.cginc"
    111.    
    112.     uniform float _VertexSkew;
    113.  
    114.  
    115.  
    116.     struct appdata {
    117.  
    118.         float4 vertex : POSITION;
    119.  
    120.     };
    121.  
    122.              
    123.  
    124.     struct v2f {
    125.  
    126.         V2F_SHADOW_COLLECTOR;
    127.  
    128.     };
    129.  
    130.              
    131.  
    132.     v2f vert (appdata v) {
    133.  
    134.        v2f o;
    135.                    
    136.         o.pos = mul (_Object2World, v.vertex);
    137.        
    138.         float4 pos = mul(_Object2World, v.vertex);
    139.  
    140.         float y = pos.y;
    141.         float x = pos.x;                    
    142.         float z = pos.z;                    
    143.        
    144.        
    145.        
    146.         pos.y = y + (z * _VertexSkew);
    147.         pos.x = x;                    
    148.         pos.z = z;              
    149.  
    150.         o.pos = mul(UNITY_MATRIX_VP, pos);  
    151.         o.vec = o.pos.xyz;    
    152.  
    153.         TRANSFER_SHADOW_COLLECTOR(o)
    154.  
    155.         return o;
    156.  
    157.     }
    158.  
    159.              
    160.  
    161.     fixed4 frag (v2f i) : COLOR {
    162.  
    163.         SHADOW_COLLECTOR_FRAGMENT(i)
    164.  
    165.     }
    166.  
    167.     ENDCG
    168.  
    169. }
    170.  
    171.     }//End SubShader
    172.  
    173.     FallBack "Diffuse"
    174. }