Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Problem with casting shadows on custom shader

Discussion in 'Shaders' started by -DarkTiger-, Mar 1, 2017.

  1. -DarkTiger-

    -DarkTiger-

    Joined:
    May 6, 2015
    Posts:
    8
    Hello,
    I have a problem with my custom shader to cast and receive shadows..
    This shader works on procedural sphere mesh to draw texture based on height/distance from center to simulate a small planet.


    Code (CSharp):
    1.  Shader "Custom/planet"
    2.     {
    3.         Properties
    4.         {
    5.             _Tex0 ("Tex 0", 2D) = "white" {}
    6.             _Tex1 ("Tex 1", 2D) = "white" {}
    7.             _Tex2 ("Tex 2", 2D) = "white" {}
    8.             _Tex3 ("Tex 3", 2D) = "white" {}
    9.             _Tex4 ("Tex 4", 2D) = "white" {}
    10.             _Tex5 ("Tex 5", 2D) = "white" {}
    11.             _Tex6 ("Tex 6", 2D) = "white" {}
    12.  
    13.             _Blend0to1and1to2 ("Blend between 0 and 1, 1 and 2", Vector) = (0,1,2,3)
    14.             _Blend2to3and3to4 ("Blend between 2 and 3, 3 and 4", Vector) = (0,1,2,3)
    15.             _Blend4to5and5to6 ("Blend between 4 and 5, 5 and 6", Vector) = (0,1,2,3)  
    16.         }
    17.  
    18.         SubShader
    19.         {
    20.             Lighting On
    21.             Tags{ "Queue" = "Geometry" "RenderType" = "Opaque" }
    22.             Pass
    23.             {
    24.                 Blend SrcAlpha OneMinusSrcAlpha
    25.                 Tags{ "LightMode" = "ForwardBase" }
    26.                 CGPROGRAM
    27.                     #pragma vertex vert
    28.                     #pragma fragment frag
    29.                     #include "UnityCG.cginc"
    30.                     #include "AutoLight.cginc"
    31.                     #pragma target 3.0
    32.                     sampler2D _Tex0;
    33.                     sampler2D _Tex1;
    34.                     sampler2D _Tex2;
    35.                     sampler2D _Tex3;
    36.                     sampler2D _Tex4;
    37.                     sampler2D _Tex5;
    38.                     sampler2D _Tex6;
    39.                     float4 _Blend0to1and1to2;
    40.                     float4 _Blend2to3and3to4;
    41.                     float4 _Blend4to5and5to6;
    42.                     uniform float4 _Tex0_ST;
    43.          
    44.                     struct v2f
    45.                     {
    46.                         float4 pos : SV_POSITION;
    47.                         float2 uv : TEXCOORD0;
    48.                         float4 col : COLOR;
    49.                         LIGHTING_COORDS(1, 2)
    50.                     };
    51.                  
    52.                     v2f vert (appdata_base vInput)
    53.                     {
    54.                         v2f OUT;
    55.                         OUT.pos = mul (UNITY_MATRIX_MVP, vInput.vertex);
    56.                         OUT.uv = TRANSFORM_TEX(vInput.texcoord, _Tex0);
    57.                         OUT.col = length(vInput.vertex);
    58.                         TRANSFER_VERTEX_TO_FRAGMENT(OUT);
    59.                         return OUT;
    60.                     }
    61.                  
    62.                     half4 frag (v2f fInput) : COLOR
    63.                     {
    64.                         half4 c0 = tex2D (_Tex0, fInput.uv);
    65.                         half4 c1 = tex2D (_Tex1, fInput.uv);
    66.                         half4 c2 = tex2D (_Tex2, fInput.uv);
    67.                         half4 c3 = tex2D (_Tex3, fInput.uv);
    68.                         half4 c4 = tex2D (_Tex4, fInput.uv);
    69.                         half4 c5 = tex2D (_Tex5, fInput.uv);
    70.                         half4 c6 = tex2D (_Tex6, fInput.uv);
    71.    
    72.                         fixed atten = LIGHT_ATTENUATION(fInput);
    73.                        
    74.                         if (fInput.col.x < _Blend0to1and1to2.x) return c0;
    75.                        
    76.                         if (fInput.col.x > _Blend0to1and1to2.x && fInput.col.x < _Blend0to1and1to2.y) return lerp(c0,c1,((fInput.col.x - _Blend0to1and1to2.x)/(_Blend0to1and1to2.y-_Blend0to1and1to2.x)));
    77.                         if (fInput.col.x > _Blend0to1and1to2.y && fInput.col.x < _Blend0to1and1to2.z) return c1;
    78.                         if (fInput.col.x > _Blend0to1and1to2.z && fInput.col.x < _Blend0to1and1to2.w) return lerp(c1,c2,((fInput.col.x - _Blend0to1and1to2.z)/(_Blend0to1and1to2.w-_Blend0to1and1to2.z)));
    79.                        
    80.                         if (fInput.col.x > _Blend0to1and1to2.w && fInput.col.x < _Blend2to3and3to4.x) return c2;
    81.                        
    82.                         if (fInput.col.x > _Blend2to3and3to4.x && fInput.col.x < _Blend2to3and3to4.y) return lerp(c2,c3,((fInput.col.x - _Blend2to3and3to4.x)/(_Blend2to3and3to4.y-_Blend2to3and3to4.x)));
    83.                         if (fInput.col.x > _Blend2to3and3to4.y && fInput.col.x < _Blend2to3and3to4.z) return c3;
    84.                         if (fInput.col.x > _Blend2to3and3to4.z && fInput.col.x < _Blend2to3and3to4.w) return lerp(c3,c4,((fInput.col.x - _Blend2to3and3to4.z)/(_Blend2to3and3to4.w-_Blend2to3and3to4.z)));
    85.                        
    86.                         if (fInput.col.x > _Blend2to3and3to4.w && fInput.col.x < _Blend4to5and5to6.x) return c4;
    87.                        
    88.                         if (fInput.col.x > _Blend4to5and5to6.x && fInput.col.x < _Blend4to5and5to6.y) return lerp(c4,c5,((fInput.col.x - _Blend4to5and5to6.x)/(_Blend4to5and5to6.y-_Blend4to5and5to6.x)));
    89.                         if (fInput.col.x > _Blend4to5and5to6.y && fInput.col.x < _Blend4to5and5to6.z) return c5;
    90.                         if (fInput.col.x > _Blend4to5and5to6.z && fInput.col.x < _Blend4to5and5to6.w) return lerp(c5,c6,((fInput.col.x - _Blend4to5and5to6.z)/(_Blend4to5and5to6.w-_Blend4to5and5to6.z)));
    91.                                                
    92.                         return c6;
    93.                     }
    94.                 ENDCG
    95.             }
    96.         }
    97.         FallBack "VertexLit"
    98.     }

    All works well but no shadows from gameobjects like trees..
    What is wrong or missing in the shader code?
    A light directional and parameters are setted up in scene for cast shadows.

    Thanks!

    No Shadow.png
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
  3. -DarkTiger-

    -DarkTiger-

    Joined:
    May 6, 2015
    Posts:
    8
    After some hours of work and test i have fixed the problem, Thanks!