Search Unity

adding shadows to a simple fragment shader

Discussion in 'Shaders' started by ZoomDomain, Sep 5, 2013.

  1. ZoomDomain

    ZoomDomain

    Joined:
    Aug 30, 2012
    Posts:
    150
    I have written this procedural textures fragment shader with shadows, and it is totally white without shadows as if light attenuation value is 1.0 on every pixel?
    Code (csharp):
    1.                 float4 frag(v2f i) : COLOR {
    2.                     half atten = LIGHT_ATTENUATION(i);
    3.                     return half4(atten,atten,atten,0.0);
    4.                 }



    Code (csharp):
    1. Shader "Custom/Pattern" {
    2.     Properties {
    3.  
    4.     }
    5.  
    6.     SubShader {
    7.         Pass {
    8.             CGPROGRAM
    9.  
    10.  
    11.                 #pragma debug
    12.                 #pragma vertex vert
    13.                 #pragma fragment frag
    14.                 #pragma target 3.0
    15.                 #pragma exclude_renderers gles
    16.                 #pragma multi_compile_fwdadd_fullshadows
    17.                 #include "UnityCG.cginc"
    18.                 #include "AutoLight.cginc"
    19.                
    20.  
    21.                 //user defined variables
    22.                 struct input {
    23.                         float2 texcoord : TEXCOORD0;
    24.                 };
    25.                  struct v2f {
    26.                     float4 pos : SV_POSITION;
    27.                     float3 color : COLOR0;
    28.                     float2 texcoord : TEXCOORD0;
    29.                     LIGHTING_COORDS(3,4)
    30.                 };
    31.  
    32.                 v2f vert (appdata_full v)
    33.                 {
    34.                     v2f o;
    35.                    o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
    36.                    TRANSFER_VERTEX_TO_FRAGMENT(o);
    37.                    o.texcoord = v.texcoord;
    38.                     return o;
    39.                 }
    40.                 // fragment function
    41.                    float4 frag(v2f i) : COLOR {
    42.                                                    
    43.                           half r1 = sin(i.texcoord.x*555);//sinus stripey textures
    44.                         half atten = LIGHT_ATTENUATION(i);
    45.                         return half4(atten,atten,atten,0.0);
    46.                    
    47.                 }
    48.             ENDCG
    49.         }
    50.     }
    51.  
    52.     fallback "Diffuse"
    53. }
     
    Last edited: Sep 6, 2013
  2. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    I believe you need a LightMode pass tag if you want any of the lighting environment values to be set up.
     
  3. ZoomDomain

    ZoomDomain

    Joined:
    Aug 30, 2012
    Posts:
    150
    Thank you. I added this line and tried various modes:
    Tags { "LightMode" = "ForwardBase" }

    The light attenuation value is still the same for every pixel of the mesh.
     
  4. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    Because your multi_compile pragma is telling it to be a ForwardAdd pass, not a ForwardBase.

    The LightMode tag and the multi_compile pragmas need to be talking about the same pass.
     
  5. ZoomDomain

    ZoomDomain

    Joined:
    Aug 30, 2012
    Posts:
    150
    Thanks, I had actually tried all the different possible light modes, I am a reasonably thorough! I still couldn't get shadow information, what can I be missing? could you copy the shader code onto a sphere and find the error? It would permit me to progress on code for an organic and cartoon shader texture generator I wish to do:)
     
  6. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
  7. ZoomDomain

    ZoomDomain

    Joined:
    Aug 30, 2012
    Posts:
    150
    do I need unity pro?

    Thanks! i checked it all, my code still runs wrong, it uses same statements and methods as you, except it' shorter and simpler, I tried all code permutations I could, there is a weird error, no shadows still. I'll start from scratch on someone else's.
     
    Last edited: Sep 6, 2013
  8. ZoomDomain

    ZoomDomain

    Joined:
    Aug 30, 2012
    Posts:
    150
    your code in the link you posted also displays everything without shadows, all white. my scene has directional lights, it's in unity free. This didn't Show any shadows, it is your bump mapped code word for word, just the first pass:
    Code (csharp):
    1. Shader "ForwardRendering" {
    2.  
    3.     Properties {
    4.  
    5.         _Color ("Main Color", Color) = (1,1,1,1)
    6.  
    7.         _SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 1)
    8.  
    9.         _Shininess ("Shininess", Range (0.03, 1)) = 0.078125
    10.  
    11.         _MainTex ("Base (RGB) Alpha (A)", 2D) = "white" {}
    12.  
    13.         _BumpMap ("Normalmap", 2D) = "bump" {}
    14.  
    15.     }
    16.  
    17.     SubShader {
    18.  
    19.         Tags {"Queue" = "Geometry" "RenderType" = "Opaque"}
    20.  
    21.  
    22.  
    23.         Pass {
    24.  
    25.             Tags {"LightMode" = "ForwardBase"}                      // This Pass tag is important or Unity may not give it the correct light information.
    26.  
    27.             CGPROGRAM
    28.  
    29.                 #pragma vertex vert
    30.  
    31.                 #pragma fragment frag
    32.  
    33.                 #pragma multi_compile_fwdbase                       // This line tells Unity to compile this pass for forward base.
    34.  
    35.                 #pragma fragmentoption ARB_fog_exp2
    36.  
    37.                 #pragma fragmentoption ARB_precision_hint_fastest
    38.  
    39.                 #pragma target 3.0
    40.  
    41.                
    42.  
    43.                 #include "UnityCG.cginc"
    44.  
    45.                 #include "AutoLight.cginc"
    46.  
    47.                
    48.  
    49.                 struct v2f
    50.  
    51.                 {
    52.  
    53.                     float4  pos         : SV_POSITION;
    54.  
    55.                     float2  uv          : TEXCOORD0;
    56.  
    57.                     float3  viewDir     : TEXCOORD1;
    58.  
    59.                     float3  lightDir    : TEXCOORD2;
    60.  
    61.                     LIGHTING_COORDS(3,4)                            // Macro to send shadow  attenuation to the vertex shader.
    62.  
    63.                 };
    64.  
    65.  
    66.  
    67.                 v2f vert (appdata_tan v)
    68.  
    69.                 {
    70.  
    71.                     v2f o;
    72.  
    73.                    
    74.  
    75.                     o.pos = mul( UNITY_MATRIX_MVP, v.vertex);
    76.  
    77.                     o.uv = v.texcoord.xy;
    78.  
    79.                     TANGENT_SPACE_ROTATION;                         // Macro for unity to build the Object>Tangent rotation matrix "rotation".
    80.  
    81.                     o.viewDir = mul(rotation, ObjSpaceViewDir(v.vertex));
    82.  
    83.                     o.lightDir = mul(rotation, ObjSpaceLightDir(v.vertex));
    84.  
    85.  
    86.  
    87.                     TRANSFER_VERTEX_TO_FRAGMENT(o);                 // Macro to send shadow  attenuation to the fragment shader.
    88.  
    89.                     return o;
    90.  
    91.                 }
    92.  
    93.  
    94.  
    95.                 sampler2D _MainTex;
    96.  
    97.                 sampler2D _BumpMap;
    98.  
    99.                 fixed4 _Color;
    100.  
    101.                 half _Shininess;
    102.  
    103.  
    104.  
    105.                 fixed4 _SpecColor;
    106.  
    107.                 fixed4 _LightColor0; // Colour of the light used in this pass.
    108.  
    109.  
    110.  
    111.                 fixed4 frag(v2f i) : COLOR
    112.  
    113.                 {
    114.  
    115.                     i.viewDir = normalize(i.viewDir);
    116.  
    117.                     i.lightDir = normalize(i.lightDir);
    118.  
    119.                    
    120.  
    121.                     fixed atten = LIGHT_ATTENUATION(i); // Macro to get you the combined shadow  attenuation value.
    122.  
    123.  
    124.  
    125.                     fixed4 tex = tex2D(_MainTex, i.uv);
    126.  
    127.                     fixed gloss = tex.a;
    128.  
    129.                     tex *= _Color;
    130.  
    131.                     fixed3 normal = UnpackNormal(tex2D(_BumpMap, i.uv));
    132.  
    133.  
    134.  
    135.                     half3 h = normalize(i.lightDir + i.viewDir);
    136.  
    137.                    
    138.  
    139.                     fixed diff = saturate(dot(normal, i.lightDir));
    140.  
    141.                    
    142.  
    143.                     float nh = saturate(dot (normal, h));
    144.  
    145.                     float spec = pow(nh, _Shininess * 128.0) * gloss;
    146.  
    147.                    
    148.  
    149.                     fixed4 c;
    150.  
    151.                     c.rgb = UNITY_LIGHTMODEL_AMBIENT.rgb * 2 * tex.rgb;         // Ambient term. Only do this in Forward Base. It only needs calculating once.
    152.  
    153.                     c.rgb += (tex.rgb * _LightColor0.rgb * diff + _LightColor0.rgb * _SpecColor.rgb * spec) * (atten * 2); // Diffuse and specular.
    154.  
    155.                     c.a = tex.a + _LightColor0.a * _SpecColor.a * spec * atten;
    156.  
    157.                     return c;
    158.  
    159.                 }
    160.  
    161.  
    162.             ENDCG
    163.         }
    164.     }
    165.  
    166.     fallback "Diffuse"
    167. }
     
  9. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    To get shadows in Free you need Unity 4.2, that will give you hard shadows from one directional light, assuming you have the light set to cast hard shadows.

    In Unity Free before version 4.2 you can't have shadows at all.


    Also FallBack might be case sensitive, you have it in lower case. It's the fallback that gives it the shadow caster/receiver. Without those, it won't cast or receive shadows at all, no matter what you do.
     
    Last edited: Sep 6, 2013
  10. ZoomDomain

    ZoomDomain

    Joined:
    Aug 30, 2012
    Posts:
    150
    Thanks, I just want to have meshes with a dark side, and a bright side toward the light. Is that also shadows and unity 4.2?

    in this pic, the snow thing is normally a landscape. it's the previous code.
     

    Attached Files:

    Last edited: Sep 6, 2013
  11. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    I think for that you want the clamped dot product of the normal and the light direction.
     
  12. ZoomDomain

    ZoomDomain

    Joined:
    Aug 30, 2012
    Posts:
    150
    thanks, that's a brilliant idea. that's indeed the solution.