Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Santokes · Oct 27, 2011 at 02:55 AM · shadershadowmap

How do I sample a shadowmap in a custom shader?

Hi guys,

I just signed up with Unity 3 Pro and am writing custom shaders for my game.

Unity shadows work for stock shaders: you just have to set all shadow casters and receivers in your scene and shadows magically work. But how about custom shaders? I would like to tap the shadow map myself to apply shadows to receivers, does anyone know how to do this? I tried sampling _ShadowMapTexture but this texture is always white.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Santokes · Oct 31, 2011 at 09:17 PM 1
Share

Has anyone written a custom shader in Unity that receives shadows?

Surely someone must have..

3 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

Answer by Santokes · Nov 04, 2011 at 11:01 PM

For the sake of anyone else who is trying to write a fragment shader that receives shadows, I figured it out.

You must do these things:

  1. '#include "AutoLight.cginc"'

  2. '#include "Lighting.cginc"'

  3. Add "Tags {"LightMode" = "ForwardBase"}

  4. '#pragma multi_compile_fwdbase'

  5. Add the Unity macros to your VSOut struct, VS and PS: LIGHTING_COORDS, TRANSFER_VERTEX_TO_FRAGMENT, LIGHT_ATTENUATION.

None of this is documented in the Unity manual.

To access the primary directional light color, unity_LightColor[0] works as long as you don't add "Tags {"LightMode" = "ForwardBase"}". If you do add that line, then it doesn't work: use _LightColor0.rgb instead. Why? Who knows.. probably makes sense to someone with access to the Unity source code. Which means no one.

Good luck!

-Peter

 Shader "Custom/PeterShader2" {
 Properties
 {
     _MainTex ("Base (RGB)", 2D) = "white" {}
 }

 CGINCLUDE

 #include "UnityCG.cginc"
 #include "AutoLight.cginc"
 #include "Lighting.cginc"

 uniform sampler2D _MainTex;

 ENDCG

 SubShader
 {
     Tags { "RenderType"="Opaque" }
     LOD 200

     Pass
     {
         Lighting On

         Tags {"LightMode" = "ForwardBase"}

         CGPROGRAM

         #pragma vertex vert
         #pragma fragment frag
         #pragma multi_compile_fwdbase

         struct VSOut
         {
             float4 pos        : SV_POSITION;
             float2 uv        : TEXCOORD1;
             LIGHTING_COORDS(3,4)
         };

         VSOut vert(appdata_tan v)
         {
             VSOut o;
             o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
             o.uv = v.texcoord.xy;

             TRANSFER_VERTEX_TO_FRAGMENT(o);

             return o;
         }

         float4 frag(VSOut i) : COLOR
         {
             float3 lightColor = _LightColor0.rgb;
             float3 lightDir = _WorldSpaceLightPos0;
             float4 colorTex = tex2D(_MainTex, i.uv.xy * float2(25.0f));
             float  atten = LIGHT_ATTENUATION(i);
             float3 N = float3(0.0f, 1.0f, 0.0f);
             float  NL = saturate(dot(N, lightDir));

             float3 color = colorTex.rgb * lightColor * NL * atten;
             return float4(color, colorTex.a);
         }

         ENDCG
     }
 } 
 FallBack "Diffuse"
 }
Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image forestj · May 21, 2012 at 04:23 PM 0
Share

$$anonymous$$A$$anonymous$$E NOTE that you can't do this if Tags {"Queue"="Transparent"} !

avatar image temptest123 · May 31, 2013 at 11:26 AM 0
Share

This works but it seems to shadow also all pixels that are not facing the lightsource, additionally darkening those areas. Did you find a solution for this?

avatar image psantoki · Oct 18, 2013 at 09:18 PM 0
Share

Hi temptest123

You can use N*L to deter$$anonymous$$e which faces are front-facing and just reduce shadows this way.

 float NL = saturate(dot(N, lightDir));
 float shadow = LIGHT_ATTENUATION(i) * NL;


Now you have a term, shadow, which is equally dark for back faces and shadowed front faces, no double darkening.

avatar image ICEYHOTSTUNTA · Oct 25, 2013 at 01:40 AM 0
Share

Thanks for this, you're the man!

avatar image mikkel.gjoel · Nov 14, 2013 at 01:02 PM 0
Share

$$anonymous$$A$$anonymous$$E NOTE that you can't do this if Tags {"Queue"="Transparent"} !

You can, if you add

#pragma multi_compile_fwdadd_fullshadows

- equally undocumented :)

Show more comments
avatar image
2

Answer by mikkel.gjoel · Nov 14, 2013 at 01:11 PM

MAKE NOTE that you can't do this if Tags {"Queue"="Transparent"} !

You can, if you add

#pragma multi_compile_fwdadd_fullshadows

- equally undocumented :)

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image psantoki · Nov 18, 2013 at 07:15 PM 0
Share

Great find $$anonymous$$ikkel :)

avatar image Corngood · Nov 21, 2013 at 01:40 AM 0
Share

Thanks! I just hit this magic handling of Transparent within a week of your comment. Good ti$$anonymous$$g. :)

avatar image
0

Answer by Kushulain · Nov 22, 2013 at 06:26 PM

How do i detect if shadows are activated or compatible with current platform ? I would like to use another sub-shader if it's not the case. And can't find a right way to do that.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to correctly sample a shadow map from a render texture? 1 Answer

Custom shadow subshader 1 Answer

Getting depth value from Shadow map in shader 0 Answers

What's in unity_LightShadowBias? 0 Answers

Shadows being received by non-shadow collectors that are depth culled by a shadow collector. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges