Search Unity

Matte Shadow

Discussion in 'Shaders' started by ern, Oct 29, 2008.

  1. ern

    ern

    Joined:
    Oct 29, 2008
    Posts:
    3
    Hi,
    Is it possible to make a matte shadow effect, like 3DS Max's "matte shadow" material.

    I want to create realtime shadows on a background image/movie.


    Regards
    ern
     
  2. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
  3. ern

    ern

    Joined:
    Oct 29, 2008
    Posts:
    3
    @bigkahuna,
    thanks for your reply, I don't think "blob shadows" apply to what im trying to do, which is this...

    I have a background picture of a room(foto)and i want my furniture(the furniture can be moved around.) to cast realtime shadows on to the floor.

    If it was a max rendering, I would make a 3D floor and apply a "matte shadow" material. And then the 3D floor will recive shadows and project them onto the background picture, the 3D floor element itself is invisible.

    Or like in this movie, the ground plane has a matte material that allows it to be invisible, and yet receive shadows…this way it looks like the
    background 2D movie receives shadows from the 3D elements:http://www.youtube.com/watch?v=P5IDEe2UQfM

    Regards
    ern
     
  4. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    In Unity there are only three ways to make shadows (that I know of at least):

    1. Baked shadows and added to a lightmap shader (not move-able).

    2. Blob shadows (which I already discussed).

    3. Real-time light based shadows (Unity Pro only feature):
    http://unity3d.com/support/documentation/Manual/Shadows.html
     
  5. ern

    ern

    Joined:
    Oct 29, 2008
    Posts:
    3
    It is not as much the shadows, as it is the object the shadows hits.
    that object must be invisible, and only show shadows in areas where shadows are applied in realtime.

    so the question is Can such a shader be made???

    ern
     
  6. amano

    amano

    Joined:
    Apr 9, 2009
    Posts:
    1
    Hi, it may be a little too late, but I jsut see your post. I found what you search for to "emulate" the Matte Shadow effect of 3DS Max. It's the DephtMask shader in the ShaderLab part of the wiki. You can follow these links:

    http://www.unifycommunity.com/wiki/index.php?title=DepthMask
    http://forum.unity3d.com/viewtopic.php?t=16305&highlight=depth+mask

    I don't know if the shadows are casted with this shader, since I'm using Indie version, but I think that would help many people like us who are familiar with the Matte Shadow effect :wink:
     
  7. the_gnoblin

    the_gnoblin

    Joined:
    Jan 10, 2009
    Posts:
    722
    I didn't get the idea how to use depth shader to emulate matte shadow effect, so I'm also interested in a way to do it :)
     
  8. Kragh

    Kragh

    Joined:
    Jan 22, 2008
    Posts:
    657
    I tried to make a Matte Shadow shader, as I can't see how the depth mask (as someone proposed) is a solution to this.
    It's working as the one in 3ds max. It's probably buggy in some situations. I didn't get it to work witout making a horrible outline on other objects recieving shadows, when they are drawn against the matte shadow object.
    If you remove "Recieve shadows" from the objects which will cast shadows on the Matte shadow plane, the ugly outline disappears. My knowledge of the actual workings of this stuff is still rather limitied.
    The strength of the shadow is controlled by the darkness of the main color of the shader.

    This works fine enough for many situations, I guess :)
     

    Attached Files:

    zbalai likes this.
  9. wimeck

    wimeck

    Joined:
    May 26, 2008
    Posts:
    50
    Nice Kragh, was looking for this, thanks!
     
  10. jcarpay

    jcarpay

    Joined:
    Aug 15, 2008
    Posts:
    561
    Nice shader Kragh! It would be great to have it working with Unity3. Anyone?
     
  11. the_gnoblin

    the_gnoblin

    Joined:
    Jan 10, 2009
    Posts:
    722
  12. kraemology

    kraemology

    Joined:
    Oct 20, 2010
    Posts:
    20
    Thanks Kragh, work just as I had hoped; however, is there possibly a way to control the hard/softness of the shadows? I opened up the shader, but as an artist, I couldn't make heads or tails of it. Either way it still rocks. Thanks again
     
  13. Kragh

    Kragh

    Joined:
    Jan 22, 2008
    Posts:
    657
    Does this even work in U3? I'm sure it doesn't :)
    I haven't checked this thread for ages, thought it would never become active...
    I could probably throw one together working a lot more reliable than this (and for U3), as my knowledge of the stuff has increased since. As for this shader, you would control the softness by changing the lights shadow type to soft. But if I remember correctly, it presented some crappy outlining effect of the shadow, so I don't think is useful to you.
     
  14. kraemology

    kraemology

    Joined:
    Oct 20, 2010
    Posts:
    20
    Actually, I'm currently using v2.6. Sorry I forgot to specify that in my previous post. I took your advice and disabled the "receive shadows," which does eliminate the weird outline, but it also disables any and all casted shadows on the object itself (as I'm sure you are probably already aware). Anyways, at the moment I'm trying to find away around this, but have yet to succeed. At the moment I'm experimenting with the "cutout" transparency shader but it appears that when something is transparent it does not receive shadows.
     
  15. pinkhair

    pinkhair

    Joined:
    Dec 17, 2010
    Posts:
    141
    Can you texture your floor to match the background in max?

    Or you can feed screen position into the uv input of your texture to make a a texture that is locked to your camera view. Not sure how to handle the shading.
     

    Attached Files:

    Last edited: Mar 24, 2011
  16. gungnir

    gungnir

    Joined:
    Nov 27, 2009
    Posts:
    16
    Hi, If you are willing, I'd like to ask if you could make a version that works in U3. Shaders in Unity really blow my mind :eek:
     
  17. Kragh

    Kragh

    Joined:
    Jan 22, 2008
    Posts:
    657
    EDIT: Doesn't work, really. Need to have another object below it, which receives shadows. Or it won't work. Really buggy solution, don't use it for now!! But I may get back to it, if I find the time...


    The surface system REALLy hate me trying to do this, so this was the best I could come up with (without going deeper into the shadow caster functions directly). There's a lot of stuff going on behind the scene in surface shaders.
    If I do not set the alpha to 0 (or just don't set it, as I did in this), it is not possible to do anything with the alpha part of the lighting function, at least not using the "atten" property, which contain shadow information. But if set to 0, the Albedo part will be ignored.

    So, this works, somewhat. But the shadow can't be tinted using the color property. Hope this will be enough for your needs, I can't use more time on it at the moment.
    Beware the mainTex is not used for anything. It's only there because removing it from the input struct, gave problems. Can't have no inputs!!! But what to input? So I just left it there... Somebody else can clean it up, if needed :)
    Code (csharp):
    1. Here was a shader that didn't work yet, sorry
     
    Last edited: Dec 13, 2011
  18. Kragh

    Kragh

    Joined:
    Jan 22, 2008
    Posts:
    657
    Ok, so this is just crappy. But I can't leave you with that last post ;) This works, if you live with its limits. This can't be done without going deeper into how shadows work in the shadow caster functions, which are built in.
    This will receive shadows, but it will double the shadow intensity, if the is an object below which also receives shadows. Very funny, have no idea what goes on :) But go ahead, if it works for some purposes,

    Code (csharp):
    1. Shader "FX/Matte Shadow" {
    2.  
    3. Properties {
    4.  
    5.     _Color ("Main Color", Color) = (1,1,1,1)
    6.  
    7.     _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    8.  
    9.     _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
    10.  
    11. }
    12.  
    13.  
    14.  
    15. SubShader {
    16.  
    17.     Tags {"Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout"}
    18.  
    19.     LOD 200
    20.  
    21.     Blend Zero SrcColor
    22.  
    23.    
    24.  
    25. CGPROGRAM
    26.  
    27. #pragma surface surf ShadowOnly alphatest:_Cutoff
    28.  
    29.  
    30.  
    31.  
    32.  
    33. fixed4 _Color;
    34.  
    35.  
    36.  
    37. struct Input {
    38.  
    39.     float2 uv_MainTex;
    40.  
    41. };
    42.  
    43.  
    44.  
    45. inline fixed4 LightingShadowOnly (SurfaceOutput s, fixed3 lightDir, fixed atten)
    46.  
    47. {
    48.  
    49.    
    50.  
    51.     fixed4 c;
    52.  
    53.    
    54.  
    55.     c.rgb = s.Albedo*atten;
    56.  
    57.     c.a = s.Alpha;
    58.  
    59.     return c;
    60.  
    61. }
    62.  
    63.  
    64.  
    65.  
    66.  
    67. void surf (Input IN, inout SurfaceOutput o) {
    68.  
    69.     fixed4 c = _Color;
    70.  
    71.     o.Albedo = c.rgb;
    72.  
    73.     o.Alpha = 1;
    74.  
    75. }
    76.  
    77. ENDCG
    78.  
    79. }
    80.  
    81.  
    82.  
    83. Fallback "Transparent/Cutout/VertexLit"
    84.  
    85. }
    86.  
     
    WalterKavalri and vexe like this.
  19. gungnir

    gungnir

    Joined:
    Nov 27, 2009
    Posts:
    16
    Hi,

    Thanks for doing this. However, I can't get this to display correctly, It renders the shadow and everything else is the colour of the skybox. It seems to not take underlying objects into account. Any idea?

    -Gungnir
     
  20. Kragh

    Kragh

    Joined:
    Jan 22, 2008
    Posts:
    657
    Do you have a screen shot of it?
     
  21. gungnir

    gungnir

    Joined:
    Nov 27, 2009
    Posts:
    16
    Sent you a pm.
     
  22. Cameron_SM

    Cameron_SM

    Joined:
    Jun 1, 2009
    Posts:
    915
    You could write a shader to render the shadow map yourself into a rendertexture then use projective texturing onto your geometry (or use a projector).

    Admittedly a fairly complicated way to do it, but at least then you're not fighting engine code you can't change.
     
  23. kyy921

    kyy921

    Joined:
    Aug 6, 2009
    Posts:
    4
    have problem , the light can be got, but the shadow didnt ,
     
  24. Oraito

    Oraito

    Joined:
    Apr 29, 2013
    Posts:
    1
    Kragh, your shader helped me a LOT!
    Just had to revive this super old topic to say thanks.
     
  25. nm8shun

    nm8shun

    Joined:
    Jul 14, 2007
    Posts:
    476
    I know this is old, but Kragh, thanks! Great stuff.
     
  26. monkeyscience

    monkeyscience

    Joined:
    Dec 13, 2011
    Posts:
    705
    Yeah, this is very cool. I've modified it a bit to for shadow color and intensity settings:

    Code (csharp):
    1.  
    2. Shader "FX/Matte Shadow" {
    3. Properties {
    4.     _Color ("Shadow Color", Color) = (1,1,1,1)
    5.     _ShadowInt ("Shadow Intensity", Range(0,1)) = 1.0
    6.     _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
    7. }
    8.  
    9.  
    10. SubShader {
    11.     Tags {
    12.         "Queue"="AlphaTest"
    13.         "IgnoreProjector"="True"
    14.         "RenderType"="TransparentCutout"
    15.     }
    16.     LOD 200
    17.     ZWrite off
    18.     Blend zero SrcColor
    19.  
    20. CGPROGRAM
    21. #pragma surface surf ShadowOnly alphatest:_Cutoff
    22.  
    23. fixed4 _Color;
    24. float _ShadowInt;
    25.  
    26. struct Input {
    27.     float2 uv_MainTex;
    28. };
    29.  
    30. inline fixed4 LightingShadowOnly (SurfaceOutput s, fixed3 lightDir, fixed atten)
    31. {
    32.     fixed4 c;
    33.     c.rgb = lerp(s.Albedo, float3(1.0,1.0,1.0), atten);
    34.     c.a = 1.0-atten;
    35.     return c;
    36. }
    37.  
    38.  
    39. void surf (Input IN, inout SurfaceOutput o) {
    40.     o.Albedo = lerp(float3(1.0,1.0,1.0), _Color.rgb, _ShadowInt);
    41.     o.Alpha = 1.0;
    42. }
    43. ENDCG
    44. }
    45.  
    46. Fallback "Transparent/Cutout/VertexLit"
    47. }
    48.  
    Couple things maybe someone could help me with:

    I can't get the alpha cutoff slider to do anything. Does alpha testing apply in some strange place between the surface function and light passes? Or did I just break something?

    And It doesn't work with deferred lighting, instead it cuts a hole clear through to the background color/skybox.

     
    Fonserbc, kyewong and Abaobao like this.
  27. Rich0

    Rich0

    Joined:
    Aug 9, 2013
    Posts:
    50
    Monkeyscience, it's awesome that you're looking at this! I've just tried your code.

    I'm looking for the exact effect you're getting with the deferred lighting (I simply wan't to cast a shadow from a directional light onto a rendersettings.skybox), however I can't reproduce it in the game view. It works in scene view in forward rendering.

    Deferred (the plane is not transparent in game view):

    $Screen Shot 2013-08-10 at 6.11.24 AM.png

    Forward (the plane is invisible in game view, and the shadow does not appear)

    $Screen Shot 2013-08-10 at 6.14.17 AM.png


    Any ideas?
     
    vexe likes this.
  28. monkeyscience

    monkeyscience

    Joined:
    Dec 13, 2011
    Posts:
    705
    Interesting that it is different in play and editor modes. A draw order problem with background shaders? I'll look into it.
     
  29. monkeyscience

    monkeyscience

    Joined:
    Dec 13, 2011
    Posts:
    705
    Looks like there are two problems:
    - ZWrite should remain enabled in the shadow catching shader
    - There is some draw order bug in how PlayerSettings skyboxes are being drawn

    I've put the exact same shader into PlayerSettings and onto a big sphere acting as skydome geometry. With ZWrite on, the shadow shader works against the skydome. Otherwise the player view is broken and editor view works o_O.

    ZWrite also cuts into the PlayerSettings skybox which only poses further questions.



    Here is the source for the sky dome shader. If you don't have Skyshop installed, feel free to axe the skymatrix and fromRGBM calls. All this is is the built-in skybox shader with v.vertex.xyz used as the cubemap lookup instead of texcoords.

    Code (csharp):
    1.  
    2. // Marmoset Skyshop
    3. // Copyright 2013 Marmoset LLC
    4. // http://marmoset.co
    5.  
    6. Shader "Marmoset/Skydome IBL" {
    7. Properties {
    8.     _SkyCubeIBL ("Custom Sky Cube", Cube) = "white" {}
    9. }
    10.  
    11. SubShader {
    12.     Tags { "Queue"="Background" "RenderType"="Background" }
    13.     Cull Off ZWrite Off Fog { Mode Off }
    14.  
    15.     Pass {
    16.         CGPROGRAM
    17.         #pragma vertex vert
    18.         #pragma fragment frag
    19.         #pragma fragmentoption ARB_precision_hint_fastest
    20.         //gamma-correct sampling permutations
    21.         #pragma multi_compile MARMO_LINEAR MARMO_GAMMA
    22.                
    23.         #define MARMO_HQ
    24.        
    25.         #ifndef SHADER_API_GLES
    26.             #define MARMO_SKY_ROTATION
    27.         #endif
    28.        
    29.         #include "UnityCG.cginc"
    30.         #include "../MarmosetCore.cginc"
    31.  
    32.         samplerCUBE _SkyCubeIBL;
    33.         half4       _ExposureIBL;
    34.         float4x4    _SkyMatrix;
    35.        
    36.         struct appdata_t {
    37.             float4 vertex : POSITION;
    38.             float3 texcoord : TEXCOORD0;
    39.         };
    40.  
    41.         struct v2f {
    42.             float4 vertex : POSITION;
    43.             float3 texcoord : TEXCOORD0;
    44.         };
    45.  
    46.         v2f vert (appdata_t v)
    47.         {
    48.             v2f o;
    49.             o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
    50.             #ifdef MARMO_SKY_ROTATION
    51.             o.texcoord = mulVec3(_SkyMatrix, v.vertex.xyz);
    52.             #else
    53.             o.texcoord = v.vertex.xyz;
    54.             #endif
    55.             return o;
    56.         }
    57.  
    58.         half4 frag (v2f i) : COLOR
    59.         {
    60.             half4 col = texCUBE(_SkyCubeIBL, i.texcoord);
    61.             col.rgb = fromRGBM(col)*_ExposureIBL.z;
    62.             col.a = 1.0;
    63.             return col;
    64.         }
    65.         ENDCG
    66.     }
    67. }  
    68.  
    69.  
    70. SubShader {
    71.     Tags { "Queue"="Background" "RenderType"="Background" }
    72.     Cull Off ZWrite Off Fog { Mode Off }
    73.     Color [_Tint]
    74.     Pass {
    75.         SetTexture [_Tex] { combine texture +- primary, texture * primary }
    76.     }
    77. }
    78.  
    79. Fallback Off
    80.  
    81. }
    82.  
     
  30. Jamda

    Jamda

    Joined:
    Apr 3, 2013
    Posts:
    6
    Thanks so much!!! The matte shadow is beautiful and works perfectly for most applications. It is not compatible with android devices.
     
    Last edited: Oct 25, 2013
  31. Partomo

    Partomo

    Joined:
    Oct 7, 2013
    Posts:
    17
    Hi,

    I'm very green to Unity so please excuse my newbie questions.

    This shader is very interesting to me as I'm trying to create shadows in an augmented reality scene (Vuforia). Basically, I'm trying to make my virtual objects throw shadows on my physical table.

    I've created a new plane game object, I've created a shader based on the code above, I've created a new material. I attached the shader to the material and the material to the plane but I still see no shadows. Also, the plane itself remains white. I was expecting the plane to go transparent with grey shadows on top.

    What am I doing wrong?

    Any help greatly appreciated.


    Tom

    $Screen Shot 2013-11-15 at 2.35.55 AM.png
     
  32. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    I'll revive it one more time... ;-)
    is it possible to get this shader working for other lights then the directional light? for realtime shadows cast by a spotlight in deferred rendering?
    if some shader wizard says it possible I'll gladly go and learn how to do it.
     
  33. Abaobao

    Abaobao

    Joined:
    Oct 8, 2013
    Posts:
    7
    Amazing Shader!!
     
  34. kunimura

    kunimura

    Joined:
    Jan 22, 2015
    Posts:
    3
    Script in here can not be used in Android. Someone help me.
     
  35. kyewong

    kyewong

    Joined:
    Sep 11, 2014
    Posts:
    22
    I've tried it. It works indded!
    Thank you very much!

     
    Carterryan1990 likes this.
  36. aure

    aure

    Joined:
    May 28, 2013
    Posts:
    4
    Thank you very much! Works like a charm!
     
  37. barjed

    barjed

    Joined:
    May 9, 2013
    Posts:
    70
    A quick question - is there a way to avoid the intense white effect when a point light is held close to the invisible surface?
     
  38. alexdeloy

    alexdeloy

    Joined:
    Jan 15, 2009
    Posts:
    7
    As this thread looks promising:

    Has anyone used the shader in Unity5 yet? I get only a bright Quad without any lighting.

    I'm looking for something similar but the shader didn't work for me.
    Can anyone nudge me in the right direction for a shader that paints a shadow on a transparent plane without a skybox but a solid color (from the camera settings) behind?
     
  39. barjed

    barjed

    Joined:
    May 9, 2013
    Posts:
    70
    Here is the issue from my previous post:



    The point light casts a strong, white effect on the invisible surface and it doesn't react to color changes.

    Is there any way to avoid it?
     
  40. scandell

    scandell

    Joined:
    Mar 4, 2015
    Posts:
    4
    This shader has saved my hide. Thank you all for contributing! I would like to request an additional feature. I have spend three hours already trying to implement it myself...but i end up with a syntax error that I can't for the life of me fix. If there is a shader guru out there that can help. I would be much obliged.

    I need the shader to support an additional directional light. I believe using multiple passes is the way to go...and using these tags: (I am using monkeyscience's shader up in post #24)


    Pass {
    Tags {"LightMode" = "ForwardBase"}

    }
    Pass {
    Tags {"LightMode" = "ForwardAdd"}

    }

    I even found a great youtube video on the subject of "multiple light shaders" that should help me...but again...i can't get past the syntax problems.



    Help me obi-wan-kenobi...
     
  41. Deleted User

    Deleted User

    Guest

    Hi all, The above shader works fine but it doesn't work in andriod. Can someone help me make it work on android. Would really appreciate that. Thanks.
     
  42. AwDogsGo2Heaven

    AwDogsGo2Heaven

    Joined:
    Jan 17, 2014
    Posts:
    102
    Same issue in IOS, anyone have any ideas?
     
  43. davidosullivan

    davidosullivan

    Joined:
    Jun 9, 2015
    Posts:
    387
    Me too, I'm making an augmented reality scene and want a transparent plane under my scene that will overlay the shadows onto the video layer- and my target devices are Android/iOS...
     
  44. HowardKudan

    HowardKudan

    Joined:
    Nov 12, 2015
    Posts:
    7
    maybe you could try it with our AR plugin?

     
  45. davidosullivan

    davidosullivan

    Joined:
    Jun 9, 2015
    Posts:
    387
    @HowardKudan I am super excited to try your AR plugin out, I see you guys are based in Bristol, and it'd be great for us to use a UK based AR provider. Thus far we have been planning everything assuming we would be using Vuforia, since there were literally NO other options available- so the market is wide open for you guys.

    For our particular project, tracking stability is absolutely key, we'd rather use image marker tracking over markerless tracking, if that gives more stability, put it that way. And we have tried wikitude, aurasma etc etc and the only thing that came close to Metaio (which was where we started before they got bought up) was Vuforia- hence here using Unity.

    So how do you feel your tracking compares to Metaio/Vuforia? (Feel free to start a private chat since I know wwe are off topic here)
     
  46. HowardKudan

    HowardKudan

    Joined:
    Nov 12, 2015
    Posts:
    7
    Hello @davidosullivan ,

    Of course my opinion on the tracking could be a little biased, I believe it to be best in class, but you needn't take my word for it, we received 2 comments this week from experienced AR developers who are trying out our SDK:

    “Just checked your demos on iPhone and I have to admit it's very impressive. It is way more stable and robust than ARToolkit, recognition is immediate.”

    “the tracking quality is impressive - better than metaio's one”

    But we are not resting on our laurels, constant improvements are being made to the tracking:

     
  47. zbalai

    zbalai

    Joined:
    May 28, 2012
    Posts:
    21
    Thank you, this version works for me for Vuforia.

     
  48. lambofett

    lambofett

    Joined:
    Oct 29, 2015
    Posts:
    4
    Just found this. Thanks for all the hard work people, your amazing!
     
  49. andersemil

    andersemil

    Joined:
    Feb 2, 2015
    Posts:
    112
    Doesn't seem to work in Unity 5 :( Anyone knows why? Or have a link to something which does?
     
  50. joaobsneto

    joaobsneto

    Joined:
    Dec 10, 2009
    Posts:
    152
    I found this, it working for me: https://github.com/keijiro/ShadowDrawer
    I'm using in a sprite based game, and I have to change the RenderQueue value of my Sprite Materials to set the rendering order of the behind the shadow receiver.