Search Unity

[UNSOLVED] Custom shadow-mapping like shader, no idea where to start

Discussion in 'Shaders' started by Murgilod, Oct 10, 2016.

  1. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    10,138
    So I realise that this is a somewhat advanced topic for me to be delving into at my skill level, but it's key that I get this working for my game since it's a pretty big part of the gameplay itself.

    What I'm trying to do is make it so places that aren't visible by any security cameras are displayed in red. It was pointed out to me that what I needed was basically equivalent to shadow mapping, which eventually lead me to this page here which describes just that, but written in OpenGL. In theory, I understand all of this, but putting that theory into practice is a whole other thing. I'll break down a few parts of what I really need help with.

    First up:


    Is there a shorthand for this in Unity? Or am I going to have to manually code the matrix by hand? This isn't a complicated thing, but having a macro would be nice.

    Next up:
    How do I account for multiple camera angles? I thought I could simply use a for-loop that takes the amount of cameras in the scene and add the camera maps from each together, but apparently you can't write shaders with variable length for-loops since they have to be unrollable at runtime?

    Also:
    Is this going to be accomplishable entirely in shader or will I have to pass some things from script as well? I know scripting just fine, but shaders are kinda beyond me when they get more complicated than "render an object entirely in one colour."
     
  2. tsangwailam

    tsangwailam

    Joined:
    Jul 30, 2013
    Posts:
    280
    Or you may just simple use a projector with a red color shader?
     
  3. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    10,138
    That would only work if I were using a single camera, but I need to account for multiple cameras in pretty much every scenario.

    edit: Also, it seems like this would only work for illuminating the areas visible by cameras in red, not the areas not-visible. This is what I'm trying to accomplish:

     
    Last edited: Oct 10, 2016
  4. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    10,138
    Anyone? I'm kinda fumbling around in the dark here and the only thing I've found for Unity is this:



    It covers how to get it working for one camera, but I'm stuck on getting it to work for multiple ones.
     
  5. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    10,138
    One final bump.
     
  6. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,342
    If you're targeting DX11 you can do dynamically sized loops, though the arrays you use will still need to be static lengths. For DX9 generally you would either change the material's shader based on the number you need or just always iterate over a fixed number. With Unity you can do the "multiple shader" style with keywords and #pragma multi_compile, which internally generates multiple shader variants which you can switch between dynamically.

    The other option is projectors, as mentioned before. There are some examples on the forum for using projectors that take a depth map like that above example video so they're properly occluded. For each security camera you would just need one more projector (and depth texture assigned to that projector's material) for each.

    It's not going The be the fastest way to do it from a rendering perspective, but it's not far off of how Unity's own lighting system works.