Search Unity

Shader request: invisible, but occluding and receives shadows?

Discussion in 'Shaders' started by StarManta, Dec 3, 2014.

  1. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Hi all, I have a need for a shader to be used in augmented reality applications that occludes what's behind, is transparent (so that the video feed "behind" the scene shows through), BUT can receive real-time shadows to be drawn overtop of the video feed. Is this kind of thing possible in Unity? Or am I going to be stuck with using blob-shadows for these things?
     
    Last edited: Dec 8, 2014
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    I'm still looking for a shader like this. Anyone have any ideas?
     
  3. Trisibo

    Trisibo

    Joined:
    Nov 1, 2010
    Posts:
    245
    So, you will render the geometry and the video will be the background?

    If so, take a look at this quick shader, which is veeeery limited (it won't look too nice with other than a plane as the occluding object, one directional light pointing almost directly to the plane, and also the shadows will be completely black). Depending on what you exactly need it may or may not work for you, or may be an starting point to do something better. Basically, it's a surface shader with a white color, and a blending operation which draws the minimum color between the object and the background (which most of the time will be the background):

    Code (CSharp):
    1. Shader "Custom/Occluder"
    2. {
    3.     SubShader
    4.     {
    5.         Tags
    6.         {
    7.             "Queue" = "Background+1"
    8.         }
    9.  
    10.  
    11.         BlendOp Min
    12.         Blend One One
    13.    
    14.  
    15.     CGPROGRAM
    16.         #pragma surface surf Lambert
    17.    
    18.    
    19.         struct Input
    20.         {
    21.             float Nothing;
    22.         };
    23.    
    24.    
    25.         void surf(Input IN, inout SurfaceOutput o)
    26.         {
    27.             o.Albedo = half4(1, 1, 1, 1);
    28.         }
    29.     ENDCG
    30.     }
    31.  
    32.  
    33.     Fallback "VertexLit"
    34. }
     
    Last edited: Dec 12, 2014
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Thanks, that is pretty much what I was looking for! There were some side effects, but they are ones that in hindsight I should have seen coming... I may be able to compensate for them using layer masks.
     
  5. GGeff

    GGeff

    Joined:
    Nov 11, 2014
    Posts:
    40
    Would there be any harm in doing:

    o.Albedo=half4(65520, 65520, 65520, 65520);

    instead, so that it was sure to work with HDR as well?
     
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    I tried it, but it made all the shadows stop working. I don't anticipate it really needing to work with HDR anyhow, TBH.