Search Unity

Making object (A) inside object (B) show through (B)... But ONLY through (B)

Discussion in 'Shaders' started by Cherno, May 20, 2015.

  1. Cherno

    Cherno

    Joined:
    Apr 7, 2013
    Posts:
    515
    I wonder if this is somehow possible? I have to spheres: A big red one, and a small yellow one. the first picture shows both next to each other.

    Now I want the yellow sphere to be inside the red sphere, but the yellow sphere should be completely visible. However, it should only be visible through the red sphere, and not through any other objects in front of it.

    To describe it the other way around: It want only the red sphere to NOT block the yellow sphere, everything else in front of it should block it as usual.

    I know of shaders that make objects appear through anything else, but can this be made selective, so it only shows through certain other shaders and/or objects?

    Note that for presentation purposes, I didn't actually move the yellow sphere inside the red sphere; it only sits in front of it, but you get the idea (I hope ;))



     
  2. Zicandar

    Zicandar

    Joined:
    Feb 10, 2014
    Posts:
    388
    In short, stencil buffer.
    Longer answer:
    This will NOT work for deferred objects in deferred mode!
    The trick is to First draw the yellow sphere, with a stencil tag that you then later use to make sure the RED sphere is NOT drawn where the yellow is :D
    While this might seem counter intuitive, when you think of it you'll (hopefully) see that the stencil tests can't modify depth tests, so we need the thing "behind" (or in this case inside) to be drawn first.
     
  3. echo4papa

    echo4papa

    Joined:
    Mar 26, 2015
    Posts:
    158
    Couldn't you just adjust the render queue order?

    Either in the shaders or in a script force the red sphere to render at 3000, the yellow at 3001 and everything else at 3002? Or numbers similar to that anyway.
     
  4. Cherno

    Cherno

    Joined:
    Apr 7, 2013
    Posts:
    515
    I don't know, could I? :) I never worked with render orders, I guess I'll have to do some research on the topic. Thanks for the suggestion.
     
  5. Zicandar

    Zicandar

    Joined:
    Feb 10, 2014
    Posts:
    388
    Possible, so both write z, but the second sphere doesn't test for it?
    Could work, but risky if anything else is drawn before them...