Custom Shadowmapping

Hi guys, I would like to share my shadowmapping shader:

  • Support hardshadow, softshadow percentage-closer filtering(PCF) 2x2, PCF 4 Dithered Samples,
    PCF 4x4 ( require opengles 3.0).
  • My demo use 256x256 texture size (ARGB8, 16 bit depth) for shadow texture lookup.
  • Reference doc: http://http.developer.nvidia.com/GPUGems/gpugems_ch11.html.
    Demo scene:
  • Any suggestions or optimization tips are welcome.
    Thanks you :).

2320211–156431–Shadowmapping.unitypackage (17.2 KB)

4 Likes

Heh. You may want to look at my source over here:

I’ve implemented custom shadowmapping with only point lights =)

Nice, I will check it out :).

Thanks for this. I was looking into how to do this before, but I don’t know much about shaders and just got lost in the standard shader. But the default Unity shadows can look pretty ugly, so I’ll definitely look into this.

Hey kokichi88,
Currently I am working on my bachelor thesis and I found your shader online. It is exactly the one I need and I want to ask you if I can use it, if I reference you as the source. Also I have a few questions about parts of the code that I don`t understand. I would be happy if you could tell me something about your code.

  1. the shader which renders the shadow map returns transparent and color values, if I output the shadow map on an rawimage. Why is that ? And why isn`t it working on android at all (everything transparent, but shadows are working). Is there I way to get the “normal” shadow map with the typical gray color?

  2. I tried to create a shader on runtime, but I cant compile it, because unity doesn`t support that. You heard something about a workaround?

1 Like

Hi oxol,
You feel free to use my code :).

  1. the shader which renders the shadow map returns transparent and color values, if I output the shadow map on an rawimage. Why is that ? And why isn`t it working on android at all (everything transparent, but shadows are working). → sorry I dont get your points.
    Is there I way to get the “normal” shadow map with the typical gray color? → You may see weird color because depth value is float point(32bit) encoded into RGBA(8bit). You can get gray color if you dont encode it and use RGBA(32bit each channel) as depth texture.
  2. I tried to create a shader on runtime, but I cant compile it, because unity doesn`t support that. You heard something about a workaround? → Maybe it’s possible in some platforms but I dont recommend. It doesnt make sense and of course not optimization.

Hey kokichi88,

  1. Yes you have understand me very well. No idea why its happening, but it doenst matter anymore, since I managed to get my “normal” looking depth texture and I output that one (which works fine).
  2. I would make sense for my bachelor thesis, but I figured out, that unity doesn`t support that, so nethermind.

Another question: Could you give me a hint, how to get the shadow mapping techniques running with different kind of light sources. Actually it works finde with directional light, but I would like to also use it with spot lights.

Thx for your help :slight_smile:

You can follow this tutorial to create your custom point light shadow mapping OpenGL Step by Step - OpenGL Development

mhm sadly that tutorial is no help. I have no expirience in OpenGL and trying to do implement custom spot lights in Unity. Shouldn`t the shader code for the technique still be the same, and only the code für creating the shadow map should change ? I can see shadows if I set the projection to perspective and move the shadow map camera close to an object, but they are on a completly wrong position.