Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by Feelnside · May 01, 2017 at 11:13 AM · shadershadersshadowsshader programmingshader writing

Shadow distance rendering in a custom shader

Hello everyone!

I have a custom shader with shadows. The main problem which I have a shadow distance rendering. Please take a look at the image below:

Alt text

As we can see, in the default mobile/diffuse shader shadows ends smoothly. Is it possible to achieve the same effect in the custom shader? Maybe someone is able to give me some help in this.

Here is the shader which I'm using:

 Shader "Test/Mobile Diffuse Colored"
 {
     SubShader
     {
         Pass
         {
             Tags {"LightMode"="ForwardBase"}
             CGPROGRAM
 
             #pragma vertex vert
             #pragma fragment frag
 
             #include "UnityCG.cginc"
             #include "Lighting.cginc"
 
             // compile shader into multiple variants, with and without shadows
             // (we don't care about any lightmaps yet, so skip these variants)
             #pragma multi_compile_fwdbase nolightmap nodirlightmap nodynlightmap novertexlight
             #pragma multi_compile_fog
             // shadow helper functions and macros
             #include "AutoLight.cginc"
 
             struct v2f
             {
                 SHADOW_COORDS(1) // put shadows data into TEXCOORD1
                 UNITY_FOG_COORDS(2)
                 fixed3 diff : COLOR0;
                 fixed3 color : TEXCOORD2;
                 fixed3 ambient : COLOR1;
                 fixed4 pos : SV_POSITION;
             };
 
             v2f vert (appdata_full v)
             {
                 v2f o;
 
                 o.pos = UnityObjectToClipPos(v.vertex);
 
                 o.color = v.color;
 
                 fixed3 worldNormal = UnityObjectToWorldNormal(v.normal);
                 fixed nl = max(0, dot(worldNormal, _WorldSpaceLightPos0.xyz));
                 o.diff = nl * _LightColor0.rgb;
                 o.ambient = ShadeSH9(fixed4(worldNormal,1));
 
                 // compute shadows data 
                 TRANSFER_SHADOW(o)
                 TRANSFER_VERTEX_TO_FRAGMENT(o);
                 UNITY_TRANSFER_FOG(o, o.pos);
                 return o;
             }
 
             fixed4 frag (v2f v) : COLOR  
             {
                 fixed4 col;
                 // compute shadow attenuation (1.0 = fully lit, 0.0 = fully shadowed)
                 fixed shadow = SHADOW_ATTENUATION(v);
 
                 // darken light's illumination with shadow, keep ambient intact
                 fixed3 lighting = v.diff * shadow + v.ambient;
                 col.rgb = lighting;
                 col.rgb *= v.color;
                 UNITY_APPLY_FOG(v.fogCoord, col);
                 return col;
             }
             ENDCG
         }
 
         // shadow casting support
        UsePass "VertexLit/SHADOWCASTER"
     }
 }

Thank you for any response.

Comment
Add comment
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

1 Reply

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

Answer by Namey5 · May 03, 2017 at 01:51 AM

Try using the following;

       Shader "Test/Mobile Diffuse Colored"
       {
           Properties {
               _Dist ("Fade Distance", Float) = 20.0
               _Fade ("Fade Intensity", Float) = 1.0
           }
      
           SubShader
           {
               Pass
               {
                   Tags {"LightMode"="ForwardBase"}
                   CGPROGRAM
       
                   #pragma vertex vert
                   #pragma fragment frag
       
                   #include "UnityCG.cginc"
                   #include "Lighting.cginc"
       
                   // compile shader into multiple variants, with and without shadows
                   // (we don't care about any lightmaps yet, so skip these variants)
                   #pragma multi_compile_fwdbase nolightmap nodirlightmap nodynlightmap novertexlight
                   #pragma multi_compile_fog
                   // shadow helper functions and macros
                   #include "AutoLight.cginc"
       
                   half _Dist;
                   half _Fade;
      
                   struct v2f
                   {
                       SHADOW_COORDS(1) // put shadows data into TEXCOORD1
                       UNITY_FOG_COORDS(2)
                       fixed3 diff : COLOR0;
                       fixed3 color : TEXCOORD2;
                       float3 worldPos : TEXCOORD3;
                       fixed3 ambient : COLOR1;
                       fixed4 pos : SV_POSITION;
                   };
       
                   v2f vert (appdata_full v)
                   {
                       v2f o;
       
                       o.pos = UnityObjectToClipPos(v.vertex);
       
                       o.color = v.color;
       
                       fixed3 worldNormal = UnityObjectToWorldNormal(v.normal);
                       fixed nl = max(0, dot(worldNormal, _WorldSpaceLightPos0.xyz));
                       o.diff = nl * _LightColor0.rgb;
                       o.ambient = ShadeSH9(fixed4(worldNormal,1));
       
                       o.worldPos = mul (unity_ObjectToWorld, v.vertex);
      
                       // compute shadows data 
                       TRANSFER_SHADOW(o)
                       TRANSFER_VERTEX_TO_FRAGMENT(o);
                       UNITY_TRANSFER_FOG(o, o.pos);
                       return o;
                   }
       
                   fixed4 frag (v2f v) : COLOR  
                   {
                       fixed4 col;
                       // compute shadow attenuation (1.0 = fully lit, 0.0 = fully shadowed)
                       fixed shadow = saturate (lerp (SHADOW_ATTENUATION(v), 1.0, (distance (v.worldPos.xyz, _WorldSpaceCameraPos.xyz) - _Dist) * _Fade));
       
                       // darken light's illumination with shadow, keep ambient intact
                       fixed3 lighting = v.diff * shadow + v.ambient;
                       col.rgb = lighting;
                       col.rgb *= v.color;
                       UNITY_APPLY_FOG(v.fogCoord, col);
                       return col;
                   }
                   ENDCG
               }
       
               // shadow casting support
              UsePass "VertexLit/SHADOWCASTER"
           }
       }

You do have to manually set the shadow fade distance property, although you can pass Unity's default value through a script, i.e. "Shader.SetGlobalFloat ("_Dist", ..."

Comment
Add comment · Show 4 · 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 Feelnside · May 03, 2017 at 04:52 AM 0
Share

Thank you for your resonse. I tried to check it and looks like it changes the shadow color. I tried to find out solutions but looks like it's a global problem. For example, there is: https://forum.unity3d.com/threads/getting-to-the-bottom-of-point-light-attenuation-squares.442757 a lot of things with the same problem and looks like it requires completely replacing Unity's lighting system which is unbelievable for me..

avatar image Namey5 Feelnside · May 03, 2017 at 05:19 AM 0
Share

Not necessary. I just made some weird choices in writing the original. The above has been edited and now works properly (actually checked it myself).

avatar image Feelnside Namey5 · May 03, 2017 at 06:01 AM 0
Share

Wow, it realy works! I spent so much time on this.. Thank you so much for your help!

Show more comments

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Shader: How to write a shader that respects sorting order first and then depth 1 Answer

Stretching when using world coordinates 2 Answers

Foliage shader advices ? 0 Answers

Shader Darkening the UI sprite behind it! 1 Answer

Making shader not ignore Light on transparent areas ? 0 Answers


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