Search Unity

An object above camera shader.

Discussion in 'Shaders' started by FoxyShadow, Jan 16, 2018.

  1. FoxyShadow

    FoxyShadow

    Joined:
    Aug 14, 2016
    Posts:
    41
    Is it possible to render an object above shader I'm using on camera?
    In my scenario I have a shader I'm using on camera to create a night effect on scene. But then I need to add an object which has light. So I'm trying to create an object with another shader which creates some light around it. The problem is that I want to apply night effect before light effect. I'm trying to understand shaders just for a couple days so my knowledge is still low about it.
     
  2. LennartJohansen

    LennartJohansen

    Joined:
    Dec 1, 2014
    Posts:
    2,394
    Have you tried to use 2 cameras? Render the 2nd camera on top of the first, not clearing background?
     
  3. FoxyShadow

    FoxyShadow

    Joined:
    Aug 14, 2016
    Posts:
    41
    I didn't even know it is possible. I'll dig into it. Thanks!
     
  4. FoxyShadow

    FoxyShadow

    Joined:
    Aug 14, 2016
    Posts:
    41
    Can't figure out how to do that. Could you help me? I made a simple situation. My game is 2D. Background has Z = 0. Then I put a camera Z = -10, depth = -1 with a shader. Then I put a sprite Z = -15 with a specific shader effect (in test it just inverts color under it). Then I put a MainCamera Z = -20, depth = 0.
    MainCamera renders background and a sprite with effect corectly, but there is no shader effect from the first camera. What am I missing?... :)
     
  5. FoxyShadow

    FoxyShadow

    Joined:
    Aug 14, 2016
    Posts:
    41
    So.. is there a way how to do that?.. Because I still cannot get a result I want to achieve...
     
  6. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    I'm not 100% sure I understand exactly what you're attempting. However I think the issue you're having at the moment is caused by the fact you're not using layers and layer masks on the cameras.

    To ensure the cameras are rendering what you want you need to set your "lit" objects to a unique layer, disable they layer for the first camera, and have that be the only layer visible to the second. Then you can just have the second camera be a child of the first with the same position as they'll each only render the objects you want.

    However I suspect that's not actually what you want. It sounds more like you want to apply an image effect to areas that aren't affected by a light in the scene. In that case you could have a single sprite that is half in the light and half out, in which case the two camera setup isn't going to work.

    To do the above you're going to have to do things a little differently, with some amount of custom shader and image effect work. There are some threads on the forum on the topic of selectively applying image effects that go into this more.
     
  7. FoxyShadow

    FoxyShadow

    Joined:
    Aug 14, 2016
    Posts:
    41
    Seems like I resolved my problem. I just needed to set ClearFlags to DepthOnly value. But thanks anyway! An advice about two cameras worked. Thanks a lot! :)