Search Unity

Custom Surface Shader not received shadow

Discussion in 'Shaders' started by eduroam, Jul 2, 2015.

  1. eduroam

    eduroam

    Joined:
    Jul 2, 2015
    Posts:
    48
    Hi.
    I have this code.
    Code (CSharp):
    1. Shader "Custom/shProbe01" {
    2.     Properties {    
    3.         _Color ("Color", Color) = (1,1,1,1)
    4.         _AmbientColor ("Ambient Color", Color) = (0,0,0,0)
    5.         _SpecCC ("Specular Color", Color) = (1,1,1,1)
    6.         _TextureCP ("Composicion Textura", 2D) = "white"{}            
    7.         //_TextureNP ("NormalMap", 2D) = "white"{}            
    8.         _SpecPower ("Specular Power", Range(0,128)) = 30
    9.         _SpecIntensity ("Specular Intensity", Range(0,10)) = 5
    10.         _RepeatSpecText ("Repeat Specular Tex", Range(0,50)) = 20
    11.         _RepeatCCText ("Repeat Color Tex", Range(0,50)) = 13
    12.         _NumberAct ("Number Mark", Range(0,1)) = 0
    13.     }
    14.  
    15.     SubShader {
    16.         Tags {
    17.             "RenderType"="Opaque"
    18.          }
    19.         CGPROGRAM
    20.      
    21.         #pragma surface surf Phong
    22.         #pragma target 3.0
    23.  
    24.  
    25.         fixed4 _Color;
    26.         fixed4 _SpecCC;
    27.         fixed4 _AmbientColor;
    28.         sampler2D _TextureCP;
    29.         //sampler2D _TextureNP;
    30.         fixed _SpecPower;
    31.         fixed _SpecIntensity;
    32.         fixed _RepeatSpecText;
    33.         fixed _RepeatCCText;
    34.         fixed _NumberAct;
    35.      
    36.         struct Input {
    37.             float4 color : COLOR;
    38.             float2 uv_TextureCP;
    39.             float2 uv_TextureNP;
    40.         };
    41.  
    42.         inline fixed4 LightingPhong ( SurfaceOutput s, fixed3 lightDir, half3 viewDir, fixed atten){
    43.      
    44.             fixed dotDiff = dot(s.Normal, lightDir);
    45.             fixed3 reflectionVector = normalize( 2.0*s.Normal*dotDiff - lightDir );
    46.          
    47.             float spec = pow ( max ( 0, dot(reflectionVector, viewDir)), _SpecPower);
    48.             fixed3 finalSpec = clamp(0.0,1.0,s.Specular * _SpecCC * spec);
    49.          
    50.             fixed4 c;
    51.             c.rgb =  (_AmbientColor*s.Albedo) + (_Color * s.Albedo * _LightColor0.rgb * dotDiff * atten * 2) + (_LightColor0.rgb * finalSpec * atten * 2);
    52.             c.a = 1.0;
    53.             return c;
    54.          
    55.         }
    56.      
    57.         void surf (Input IN, inout SurfaceOutput o) {
    58.             fixed4 finalCC;
    59.             fixed4 finalSP;
    60.             fixed4 finalMK;
    61.          
    62.             finalSP = tex2D(_TextureCP, IN.uv_TextureCP*_RepeatSpecText);
    63.             finalCC = tex2D(_TextureCP, IN.uv_TextureCP*_RepeatCCText);
    64.             finalMK = tex2D(_TextureCP, IN.uv_TextureCP);
    65.  
    66.             //o.Normal = UnpackNormal(tex2D (_TextureNP, IN.uv_TextureNP));
    67.             if (_NumberAct > 0.0)
    68.                 o.Albedo = (1.0-finalMK.g)*lerp(finalCC.b, float4(0.594,0.594,0.594,0.0), 0.5);
    69.             else
    70.                 o.Albedo = (1.0-finalMK.a)*lerp(finalCC.b, float4(0.594,0.594,0.594,0.0), 0.5);
    71.              
    72.             o.Specular = _SpecIntensity*finalSP.r;
    73.              
    74.             o.Alpha = 1.0;
    75.         }
    76.         ENDCG
    77.     }
    78.     FallBack "Diffuse"
    79. }
    But the objects that I applied this shader not received shadow. See image file.

    I need to activate something?. Thx
     

    Attached Files:

  2. UnityGuillaume

    UnityGuillaume

    Unity Technologies

    Joined:
    Mar 16, 2015
    Posts:
    123
    If you are in forward rendering, by default, surface shader only support one shadow from the main directional light.

    To add shadowing for all type of light, add to your #pragma surface line, are the end :

    fullforwardshadows

    See here for a full list of param tyou can give to the #pragma surface sirective : http://docs.unity3d.com/Manual/SL-SurfaceShaders.html

    Also worth double checking that your object MeshRenderer have receive shadow checked! =D
     
  3. eduroam

    eduroam

    Joined:
    Jul 2, 2015
    Posts:
    48
    Hi. thanks for your reply. :)

    1. Add fullforwardshadows... done!.

    #pragma surface surf Phong fullforwardshadows

    2. Checking MeshRenderer have receive shadow... done!.

    But the mesh still doesnt receive shadow :(. Even if I use directional light.

    Maybe some setting in Unity that is not activated?
     

    Attached Files:

  4. UnityGuillaume

    UnityGuillaume

    Unity Technologies

    Joined:
    Mar 16, 2015
    Posts:
    123
    Hum I don't think so, your other object receive shadow, so any quality setting must be properly set.

    I just copy pasted your shader and object having it does received shadow on my end...
    What is the version of Unity you're using? Can you test in latest (5.1.1p3) if it's not that one? (maybe install it in another folder, and do a copy of your project, to not mess things up)