No Shadow on my custom shader

Hi, I’m just having trouble having shadow with my custom shader.

My renderer has cast and receive shadow enabled and my shader use a
#pragma surf lambert light model. And when I use a classic diffuse shader on my mesh, I can see the shadows, Is there any others reasons than the ones in the Troubleshooting problem in UNITY for my shadows to not appear?? :slight_smile:

Thanks!

btw : these are the reasons I checked out on the manual and I my shader is clear on every issue…

Shadows

I see no shadows at all!
Shadows are a Unity Pro only feature, so without Unity Pro you won't get shadows. Simpler shadow methods, like using a Projector, are still possible of course.
Shadows also require certain graphics hardware support. See Shadows page for details.
Check if shadows are not completely disabled in Quality Settings.
Shadows are currently not supported for Android and iOS mobile platforms.
Some of my objects do not cast or receive shadows
First, the Renderer has to have Receive Shadows on to have shadows on itself; and Cast Shadows on to cast shadows on other objects (both are on by default).

Next, only opaque objects cast and receive shadows; that means if you use built-in Transparent or Particle shaders then you'll get no shadows. In most cases it's possible to Transparent Cutout shaders (for objects like fences, vegetation etc.). If you use custom written Shaders, they have to be pixel-lit and use Geometry render queue. Objects using VertexLit shaders do not receive shadows either (but can cast shadows just fine).

Finally, only Pixel lights cast shadows. If you want to make sure that some light always casts shadows, no matter how many other lights are in the scene, set it to Force Pixel render mode (see Light documentation).

Forget to add something : I’m using a directionnal Light in order to cast my capricious shadow

so to check and be sure:

  1. You are on Desktop with desktop target
  2. you are on Unity Pro
  3. Quality settings allow shadows
  4. You have pixel lights (important!)
  5. you have realtime lights (not bake only)
  6. your shader is a surface shader, vert - frag combinations won’t get shadow code added you would have to
  7. and you have objects that send and receive shadows
  8. your light is set to cast shadows (default is disabled)
  9. the light is a directional one cause otherwise you will not get realtime shadows unless you are in deferred rendering or unless you used the pragma for the full foward shadows to enable shadows also from point lights + more than 1 directional
1 Like

Thanks!

I’ve checked every point you told and met everything except the Desktop target thing, because I’m building a webApp (although, it should at least work on the editor). However, I’m pretty sure it has something to do with the shader itself because when I use a simple Diffuse shader I got my shader received by my mesh!

And my shader is a surface shader

#pragma surface surf Lambert
#pragma target 3.0

maybe it has something to do with the Alpha (even if it’s put to 1 in my case) or the ZWrite?

even with alpha it should cast shadows just not with your alpha form but with the underlying mesh (as the quoted part mentioned, only cutout cuts into the shadow too)

and yupp web is theoretically no reason for it not to work at least in the editor.when I say desktop I include web basically (non desktop then would be mobile or console :))

Thanks for your help “Dreamora”. Actually, I managed to obtain my shadows.

In fact, my fallback shader was also a custom shader and no “shadow pass” was performed. I’ve just added

Fallback "VertexLit"

at the end of it and that’s it!

Hey dreamora, how do you sample a shadow map from a fragment shader this way?
I tried tapping _ShadowMapTexture but this texture is pure white.

Literally 10 posts down, there’s this thread with the answer.

http://forum.unity3d.com/threads/108612-Adding-shadows-to-custom-shader-(vert-frag)