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 /
  • Help Room /
avatar image
0
Question by GoodKingJohn · May 06, 2019 at 01:34 AM · shadershadowsshader programminghlsl

Vert/frag shader not receiving shadow despite all relevant code being present

Hi there, I've been working on a cartoon shader for my game and have been trying to implement received shadows. I have included the necessary parts:

 #pragma mutli_compile_fwdbase // at the start of the pass
 #include "AutoLight.cginc" // at the start of the pass
 LIGHTING_COORDS(3,4) // within the v2f struct
 TRANSFER_VERTEX_TO_FRAGMENT(o) // in the v2f instance
 float shadow = LIGHT_ATTENUATION(i); // in the fragment shader
 Fallback "VertexLit" // after the final pass

However shadows still do not render on my objects. I am using multiple passes, one first to render a border around the object then one to render the object itself on top, and I have wondered if that is affecting it however I cannot figure out how. Here is my shader code (relevant lighting code is in the bottom pass and marked with -------):

 Shader "Custom/CelShader"
 {
     Properties
     {
         _MainTex ("Texture", 2D) = "white" {}
         _Color ("Colour", Color) = (1.0,1.0,1.0,1.0)
         [HDR]
         _AmbientColor("Ambient Color", Color) = (0.4,0.4,0.4,1)
         [HDR]
         _SpecularColor("Specular Color", Color) = (0.9,0.9,0.9,1)
         _Glossiness("Glossiness", Float) = 32
         [HDR]
         _RimColor("Rim Color", Color) = (1,1,1,1)
         _RimAmount("Rim Amount", Range(0, 1)) = 0.716
         _RimThreshold("Rim Threshold", Range(0,1)) = 0.1
         _OutlineAmount("Outline Amount", Float) = 0.01
         _Transparency("Transparency", Range(0,1)) = 1
         _OutlineByNormal("Outline By Normal", Range(0,1)) = 1
     }
     SubShader
     {
         
         Tags
         {
             "Queue"="Transparent"
             "RenderType"="Transparent"
             "LightMode"="ForwardBase"
             "PassFlags"="OnlyDirectional"
         }
         LOD 100
         Blend SrcAlpha OneMinusSrcAlpha
 
         Pass
         {
             ZWrite Off
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             
             #include "UnityCG.cginc"
 
             struct appdata
             {
                 float4 vertex : POSITION;
                 float2 uv : TEXCOORD0;
                 float3 normal : NORMAL;
             };
 
             struct v2f
             {
                 float4 vertex : SV_POSITION;
                 float3 worldNormal : NORMAL;
             };
 
             float _OutlineAmount;
             float _Transparency;
             float _OutlineByNormal;
 
             v2f vert (appdata v)
             {
                 v2f o;
                 o.worldNormal = UnityObjectToWorldNormal(v.normal);
                 o.vertex = UnityObjectToClipPos(_OutlineByNormal > 0.5 ? v.vertex + (v.normal * _OutlineAmount) : v.vertex * _OutlineAmount);
                 return o;
             }
             
             fixed4 frag (v2f i) : SV_Target
             {
                 fixed4 col = fixed4(0,0,0,_Transparency);
                 return col;
             }
             ENDCG
         }
 
         Pass
         {
             
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             // make fog work
             #pragma multi_compile_fog
             #pragma multi_compile_fwdbase // pragma HERE -------------------------------------------------------------------------------
             
             #include "UnityCG.cginc"
             #include "Lighting.cginc"
             #include "AutoLight.cginc" // INCLUDE HERE ----------------------------------------------------------------------------------
 
             struct appdata
             {
                 float4 vertex : POSITION;
                 float2 uv : TEXCOORD0;
                 float3 normal : NORMAL;
             };
 
             struct v2f
             {
                 float2 uv : TEXCOORD0;
                 float4 vertex : SV_POSITION;
                 float3 worldNormal : NORMAL;
                 float3 viewDir : TEXCOORD1;
                 UNITY_FOG_COORDS(2)
                 LIGHTING_COORDS(3,4) // LIGHTING COORDS HERE -------------------------------------------------------------------------
             };
 
             sampler2D _MainTex;
             float4 _MainTex_ST;
             fixed4 _Color;
             fixed4 _AmbientColor;
             float _Glossiness;
             float4 _SpecularColor;
             float4 _RimColor;
             float _RimAmount;
             float _RimThreshold;
             float _OutlineAmount;
             float _Transparency;
 
             v2f vert (appdata v)
             {
                 v2f o;
                 o.vertex = UnityObjectToClipPos(v.vertex);
                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                 o.worldNormal = UnityObjectToWorldNormal(v.normal);
                 o.viewDir = WorldSpaceViewDir(v.vertex);
                 UNITY_TRANSFER_FOG(o,o.vertex);
                 TRANSFER_VERTEX_TO_FRAGMENT(o) // TRANSFER DONE HERE ---------------------------------------------------------------------
                 return o;
             }
             
             fixed4 frag (v2f i) : SV_Target
             {
                 // sample the texture
                 fixed4 col = tex2D(_MainTex, i.uv);
                 float3 normal = normalize(i.worldNormal);
                 float NdotL = dot(_WorldSpaceLightPos0, normal);
                 //float lightIntensity = smoothstep(0, 0.01, NdotL);
                 float shadow = LIGHT_ATTENUATION(i);
                 float lightIntensity = (NdotL > 0.75 ? 1 : (NdotL > 0.25 ? 0.5 : 0)) * shadow;  // SHADOW APPLIED HERE -----------------------------------------------
                 float4 light = lightIntensity * _LightColor0;
                 float3 viewDir = normalize(i.viewDir);
 
                 float3 halfVector = normalize(_WorldSpaceLightPos0 + viewDir);
                 float NdotH = dot(normal, halfVector);
 
                 float specularIntensity = pow(NdotH * lightIntensity, _Glossiness * _Glossiness);
                 float specularIntensitySmooth = specularIntensity > 0.5 ? 1 : 0;
                 float4 specular = specularIntensitySmooth * _SpecularColor;
 
                 float4 rimDot = 1 - dot(viewDir, normal);
                 float rimIntensity = rimDot * pow(NdotL, _RimThreshold);
                  rimIntensity = smoothstep(_RimAmount - 0.01, _RimAmount + 0.01, rimIntensity);
                 float4 rim = rimIntensity * _RimColor;
 
                 float4 outlineDot = 1 - dot(viewDir, normal);
                 float outline = outlineDot > _OutlineAmount ? 1 : 0;
 
                 fixed4 finalCol = _Color * col * (_AmbientColor + light + specular + rim);
                 finalCol.a = _Transparency;
                 // apply fog
                 UNITY_APPLY_FOG(i.fogCoord, finalCol);
                 return finalCol;//outline > 0 ? fixed4(0,0,0,1) : finalCol;
             }
             ENDCG
         }
     }
     Fallback "VertexLit"
 }

Am I missing something obvious? Or is this due to some interaction I'm not aware of?

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 GoodKingJohn · May 07, 2019 at 07:05 PM

Found the problem: The shader is transparent, which means it cannot receive shadows, which apparently only opaque shaders can do.

Changing:

 Tags
          {
              "Queue"="Transparent"
              "RenderType"="Transparent"
              "LightMode"="ForwardBase"
              "PassFlags"="OnlyDirectional"
          }

To

 Tags
          {
              "RenderType"="Opaque"
              "LightMode"="ForwardBase"
              "PassFlags"="OnlyDirectional"
          }


Fixed the issue.

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

234 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Geometry shader double sided leaf, shadows projected on both sides. 0 Answers

How can I make this custom shader support shadows? 1 Answer

Add hard shadows to a Toon Shader 0 Answers

Is it possible to set the 'ForceNoShadowCasting' SubShader Tag via shader properties or a custom material editor? 1 Answer

Shader error when updating shader. 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