Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Custom Shadowmapping

Discussion in 'General Graphics' started by kokichi88, Oct 1, 2015.

  1. kokichi88

    kokichi88

    Joined:
    Mar 3, 2014
    Posts:
    13
    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 :).
     

    Attached Files:

  2. Zuntatos

    Zuntatos

    Joined:
    Nov 18, 2012
    Posts:
    612
  3. kokichi88

    kokichi88

    Joined:
    Mar 3, 2014
    Posts:
    13
    Nice, I will check it out :).
     
  4. UziMonkey

    UziMonkey

    Joined:
    Nov 7, 2012
    Posts:
    206
    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.
     
  5. oxol1989

    oxol1989

    Joined:
    Dec 8, 2016
    Posts:
    3
    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?
     
    kokichi88 likes this.
  6. kokichi88

    kokichi88

    Joined:
    Mar 3, 2014
    Posts:
    13
    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.
     
  7. oxol1989

    oxol1989

    Joined:
    Dec 8, 2016
    Posts:
    3
    Hey kokichi88,
    1. Yes you have understand me very well. No idea why it`s happening, but it doens`t 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 :)
     
  8. kokichi88

    kokichi88

    Joined:
    Mar 3, 2014
    Posts:
    13
  9. oxol1989

    oxol1989

    Joined:
    Dec 8, 2016
    Posts:
    3
    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.