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

how to add shadows

Discussion in 'Shaders' started by AR_Rizvi, Aug 16, 2013.

  1. AR_Rizvi

    AR_Rizvi

    Joined:
    Aug 12, 2013
    Posts:
    40
    hi guys... I am pretty new to shader writng but i am seting up a shader in which i have alpha blending with decal and a normal map But the problem is that i am not able to cast or recive any shadows from it this is where i dnt knw what to do nd how to do it i am stuck here plz help me out with this shader

    code given below

    Shader "AdditiveAlphaMask_decal_bump" {
    Properties {
    _MainColor ("Main Color", Color) = (1,1,1,1)
    _MainTex ("Main Alpha Texture", 2D) = "white" {}
    _AlphaMap ("Additional Alpha Map (Greyscale)", 2D) = "white" {}
    _DecalTex ("Decal (RGBA)", 2D) = "black" {}
    _BumpMap ("Normalmap", 2D) = "bump" {}

    }

    SubShader {
    Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
    Blend SrcAlpha OneMinusSrcAlpha
    AlphaTest Greater .01
    ColorMask RGB
    Cull Off Lighting Off ZWrite Off Fog { Color (0,0,0,0) }
    BindChannels {
    Bind "Color", color
    Bind "Vertex", vertex
    Bind "TexCoord", texcoord
    }
    LOD 200

    CGPROGRAM
    #pragma surface surf Lambert


    sampler2D _MainTex;
    sampler2D _AlphaMap;
    sampler2D _DecalTex;
    sampler2D _BumpMap;
    half _Shininess;
    fixed4 _MainColor;

    struct Input {
    float2 uv_MainTex;
    float2 uv_AlphaMap;
    float2 uv_DecalTex;
    float2 uv_BumpMap;
    };

    void surf (Input IN, inout SurfaceOutput o) {
    half4 c = tex2D (_MainTex, IN.uv_MainTex) * _MainColor;
    half4 decal = tex2D(_DecalTex, IN.uv_DecalTex);
    c.rgb = lerp (c.rgb, decal.rgb, decal.a);
    o.Albedo = c.rgb* _MainColor.rgb;

    o.Alpha = c.a * tex2D(_AlphaMap, IN.uv_AlphaMap).r;
    o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));

    }
    ENDCG
    }
    FallBack "Diffuse"
    }

    any help would b much appreciated thanks....!:)
     
    Last edited: Aug 16, 2013
  2. Lulucifer

    Lulucifer

    Joined:
    Jul 8, 2012
    Posts:
    358
    In forawrd renderering path ,only direction light have shadow,
    In deferred rendering path,you can have varies shadows,
    but as afr as i know, transparent obj will be renderered after deferred rendering, in forward path,
    So....maybe that's the porblem
     
  3. AR_Rizvi

    AR_Rizvi

    Joined:
    Aug 12, 2013
    Posts:
    40
    Are u saying that transparent obj cant cast shadows because i have checked it in Both Deferred and Forward rendering path but no success
    Plz suggest me what else i can do
    Or may b thier is a problem with the shader that i am missng :(
     
  4. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    Yes, transparent objects do not cast shadows. They do not write to the ZBuffer and so cannot cast shadows.

    Only opaque and alpha test (rather than alpha blend - transparent - objects) objects can cast shadows.
     
  5. AR_Rizvi

    AR_Rizvi

    Joined:
    Aug 12, 2013
    Posts:
    40
    thanks so much
    i m trying things buts its going no where what can i do to make my objects receive and cast shadows
    what should i add in my shader to achieve what i want
    and also tell me that how can i use a cutout in this shader with my decal property
    plz help..... i am really confuse :confused:
     
    Last edited: Aug 16, 2013
  6. Lulucifer

    Lulucifer

    Joined:
    Jul 8, 2012
    Posts:
    358
    In surface shader, use fallback,that will make shadows for you,if unity supports that kind
     
  7. AR_Rizvi

    AR_Rizvi

    Joined:
    Aug 12, 2013
    Posts:
    40
    as far as i knw fallback is used when the shader didnt work properly and it falls back to the specified shader may b im wrong...
    so how can it help me with shadows
     
  8. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    The eventual fallback of the fallback will contain the shadow caster/collector passes for the mesh. So it's helpful for shadows.
     
  9. AR_Rizvi

    AR_Rizvi

    Joined:
    Aug 12, 2013
    Posts:
    40
    thank you fr ur suggestions i tested them but i am going no where with what i want nthing much happend
    can u guys plz show me what i can add or where i can add to make my object cast and recive shadows
    i tested fall backs but nothings happening i dnt understand what is the matter
    it dsnt work at all :(
     
  10. cician

    cician

    Joined:
    Dec 10, 2012
    Posts:
    233
    Unfortunately, as far as I know, semi-transparent objects cannot cast or receive shadows properly. You can cast a shadow that is cutout or opaque, but not translucent.
    Receiving shadows requires writing to z buffer which breaks among other things rendering of overlapping semitransparent objects.
    Built-in transparent shaders don't cast or receive shadows as well.
    While not 100% Unity specific it would be nice if it was written somewhere in the docs so newcomers don't get biten by the fact (like I did).

    The fallback directive other than the fallback itself makes your shader inherit shadow caster and collector passes from the fallback one.
    Other ways than the fallback are addshadow directive, defining the passes manually or referencing passes from other shaders with UsePass.

    If you're OK with cutout shadows you can add this:
    Code (csharp):
    1. _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
    and this inside the SubShader block:
    Code (csharp):
    1. UsePass "Transparent/Cutout/VertexLit/SHADOWCASTER"
    Hope that helps.
     
  11. AR_Rizvi

    AR_Rizvi

    Joined:
    Aug 12, 2013
    Posts:
    40
    thank you cician
    but i want to blend in 2 alphas with a decal texture as shader state so when i m using the cout out property i have to use

    #pragma surface surf Lambert alphatest:_Cutoff
    insted of
    #pragma surface surf Lambert alpha

    so after doing it i can not blend the two alphas together because they tend to cutoff as stated in #pragma
    i can do that nd can make it recive/cast shadows but i want to do it in this perticular shader
    i understand that taranspernt object dsnt support shadows
    and that usepass trick dsnt work at all may b i m putting it in a wrong place
    but as u said i put it in a subshader block of my above given shader and it dsnt work
    so thank you for your ideas guys cheers
     
    Last edited: Aug 17, 2013
  12. Lulucifer

    Lulucifer

    Joined:
    Jul 8, 2012
    Posts:
    358
    I know these
    u : you
    m : am
    insted : instead
    ......
    But i dont know 'nd' stands for ...?
     
  13. AR_Rizvi

    AR_Rizvi

    Joined:
    Aug 12, 2013
    Posts:
    40
    nd Stands for And