Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 abbsimoga · Sep 13, 2021 at 10:24 AM · shadowsvertexshadowmap

Per vertex shadow mapping

How do i make this shader have shadows casted per vertex instead of per pixel?

 Shader "Unlit/per vertex shader"
 {
     Properties
     {
         _MainTex ("Texture", 2D) = "white" {}
     }
     SubShader
     {
         Pass
         {
             Tags {
                 "RenderType" = "Opaque"
                 "LightMode" = "ForwardAdd"
                 "PassFlags" = "OnlyDirectional"
             }
 
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
 
             #include "UnityCG.cginc"
             #include "Lighting.cginc"
             #pragma multi_compile_fwdbase nolightmap nodirlightmap nodynlightmap novertexlight
             #include "AutoLight.cginc"
 
             struct v2f
             {
                 SHADOW_COORDS(1)
                 fixed3 diff : COLOR0;
                 fixed3 ambient : COLOR1;
                 float4 pos : SV_POSITION;
                 float3 worldPos : TEXCOORD2;
             };
 
             sampler2D _MainTex;
             float4 _MainTex_ST;
 
             sampler2D _LightTexture0;
             float4x4 unity_WorldToLight;
 
             v2f vert (appdata_base v)
             {
                 v2f o;
                 o.pos = UnityObjectToClipPos(v.vertex);
                 o.worldPos = mul (unity_ObjectToWorld, v.vertex);
                 half3 worldNormal = UnityObjectToWorldNormal(v.normal);
                 half nl = max(0, dot(worldNormal, _WorldSpaceLightPos0.xyz));
                 o.diff = nl * _LightColor0.rgb;
                 o.ambient = ShadeSH9(half4(worldNormal,1));
                 TRANSFER_SHADOW(o)
 
                 return o;
             }
 
             fixed4 frag (v2f i) : SV_Target
             {
                 fixed shadow = SHADOW_ATTENUATION(i);
                 float2 uvCookie = mul(unity_WorldToLight, float4(i.worldPos, 1)).xy;
                 float attenuation = tex2D(_LightTexture0, uvCookie).w;
                 fixed3 lighting = i.diff * shadow * attenuation + i.ambient;
 
                 return float4(lighting,1);
             }
             ENDCG
         }
         UsePass "Legacy Shaders/VertexLit/SHADOWCASTER"
     }
 }

Current Shader Results: alt text

Expected Shader Results: (Cant draw for shit so wont bother, but i want each vertex shown here in green mesh outline to have the same shadow) alt text

currentshaderresults.png (83.0 kB)
expectedshaderresults.png (71.6 kB)
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

2 Replies

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

Answer by Namey5 · Sep 14, 2021 at 03:10 AM

Unfortunately this isn't straightforward as Unity's directional shadows are rendered into a screenspace texture first, then resampled in the fragment shader. As such, just shifting the sampling into the vertex shader would kind of work (I did it for a forum post a while back) but would look pretty horrible as it would change based on your view. However, in your case because you are only supporting a single directional light, we can copy its shadowmap in a script and just sample it directly in the vertex shader ourselves.

To start off, let's expose that light's shadowmap to all shaders:

 using UnityEngine;
 using UnityEngine.Rendering;
 
 // Run in edit mode so we can see in the scene view
 [RequireComponent (typeof (Light)), ExecuteInEditMode]
 public class CopyShadowmap : MonoBehaviour
 {
     private Light m_Light;
     private CommandBuffer m_Buffer;
 
     private void OnEnable ()
     {
         m_Light = GetComponent<Light>();
 
         // Only want to support directional lights
         if (m_Light.type == LightType.Directional)
         {
             // Create a new command buffer
             m_Buffer = new CommandBuffer () { name = "Copy Shadowmap" };
             // Just expose the shadowmap to all shaders globally
             m_Buffer.SetGlobalTexture ("_DirectionalShadowmap", BuiltinRenderTextureType.CurrentActive);
 
             // Add just after the shadowmap is rendered so it is set to BuiltinRenderTextureType.CurrentActive
             m_Light.AddCommandBuffer (LightEvent.AfterShadowMap, m_Buffer);
         }
     }
 
     private void OnDisable ()
     {
         // Cleanup light & buffer
         if (m_Buffer != null)
         {
             if (m_Light != null)
             {
                 m_Light.RemoveCommandBuffer (LightEvent.AfterShadowMap, m_Buffer);
             }
 
             m_Buffer.Release ();
             m_Buffer = null;
         }
     }
 }

Just make sure to attach this to your desired directional light.

Then in the shader, we need to sample our own shadowmap copy directly. Basically what I've written here is how the shadows would be sampled internally in that screenspace pass:

 Shader "Unlit/per vertex shader"
 {
     Properties
     {
         _MainTex ("Texture", 2D) = "white" {}
     }
 
     SubShader
     {
         Pass
         {
             Tags 
             {
                 "RenderType" = "Opaque"
                 "LightMode" = "ForwardBase"
                 "PassFlags" = "OnlyDirectional"
             }
 
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
 
             #pragma multi_compile_fwdbase nolightmap nodirlightmap nodynlightmap novertexlight
 
             #include "UnityCG.cginc"
             #include "Lighting.cginc"
             #include "AutoLight.cginc"
 
             struct v2f
             {
                 float4 pos : SV_POSITION;
                 half3 diff : TEXCOORD0;
                 half3 ambient : TEXCOORD1;
                 float3 worldPos : TEXCOORD2;
             };
 
             sampler2D _MainTex;
             float4 _MainTex_ST;
 
             // Declare the shadowmap
             UNITY_DECLARE_SHADOWMAP(_DirectionalShadowmap);
 
             sampler2D _LightTexture0;
             float4x4 unity_WorldToLight;
 
             v2f vert (appdata_base v)
             {
                 v2f o;
                 o.pos = UnityObjectToClipPos (v.vertex);
                 o.worldPos = mul (unity_ObjectToWorld, v.vertex);
                 half3 worldNormal = UnityObjectToWorldNormal (v.normal);
                 o.ambient = ShadeSH9 (half4 (worldNormal, 1));
 
                 // Support cascaded shadows
                 float4 shadowCoords0 = mul (unity_WorldToShadow[0], float4 (o.worldPos, 1));
                 float4 shadowCoords1 = mul (unity_WorldToShadow[1], float4 (o.worldPos, 1));
                 float4 shadowCoords2 = mul (unity_WorldToShadow[2], float4 (o.worldPos, 1));
                 float4 shadowCoords3 = mul (unity_WorldToShadow[3], float4 (o.worldPos, 1));
 
                 // Find which cascaded shadow coords to use based on our distance to the camera
                 float dist = distance (o.worldPos, _WorldSpaceCameraPos.xyz);
                 float4 zNear = dist >= _LightSplitsNear;
                 float4 zFar = dist < _LightSplitsFar;
                 float4 weights = zNear * zFar;
                 float4 shadowCoords = shadowCoords0 * weights.x + shadowCoords1 * weights.y + shadowCoords2 * weights.z + shadowCoords3 * weights.w;
 
                 // Sample the shadowmap
                 half shadow = UNITY_SAMPLE_SHADOW (_DirectionalShadowmap, shadowCoords);
 
                 half nl = max (0, dot (worldNormal, _WorldSpaceLightPos0.xyz));
                 o.diff = (nl * shadow) * _LightColor0.rgb;
                 return o;
             }
 
             half4 frag (v2f i) : SV_Target
             {
                 float2 uvCookie = mul (unity_WorldToLight, float4 (i.worldPos, 1)).xy;
                 float attenuation = tex2D (_LightTexture0, uvCookie).w;
                 half3 lighting = i.diff * attenuation + i.ambient;
 
                 return float4 (lighting, 1);
             }
             ENDCG
         }
         UsePass "Legacy Shaders/VertexLit/SHADOWCASTER"
     }
 }
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
avatar image
0

Answer by abbsimoga · Sep 22, 2021 at 05:38 PM

@Namey5 Thanks for your respons (got a bit to type so cant leave a comment)

First of i tried using the same vertexOutput with changed SV_POSITION in hope that the shadow coorinate would be the same for each vertex and therefore the whole face of the triangle. It did work, however as you predicted it resulted in inconsistencies blinking and leaving artifacts. Here is that shader:

 Shader "Custom/shadow testing shader"
 {
     Properties
     {
         _ShadowSoftness("Shadow Softness", Float) = 0.5
     }
 
     SubShader
     {
         Pass
         {
             Tags {
                 "RenderType" = "Opaque"
                 "LightMode" = "ForwardAdd"
                 "PassFlags" = "OnlyDirectional"
             }
             CGPROGRAM
             #include "UnityCG.cginc"
             #include "AutoLight.cginc"
             #include "Lighting.cginc"
             #pragma target 3.0
             #pragma vertex vert
             #pragma fragment frag
             #pragma geometry geo
             #pragma require geometry
             #pragma multi_compile_fwdbase nolightmap nodirlightmap nodynlightmap novertexlight
             
             sampler2D _LightTexture0;
             float4x4 unity_WorldToLight;
 
             float _ShadowSoftness;
 
             struct vertexInput
             {
                 float4 vertex : POSITION;
                 float3 normal : NORMAL;
                 float4 tangent : TANGENT;
             };
 
             struct vertexOutput
             {
                 float4 vertex : SV_POSITION;
                 float3 normal : NORMAL;
                 float4 tangent : TANGENT;
             };
 
             vertexInput vert(vertexInput v)
             {
                 return v;
             }
 
             vertexOutput tessVert(vertexInput v)
             {
                 vertexOutput o;
                 o.vertex = v.vertex; // Note that the vertex is NOT transformed to clip space here;
                 o.normal = v.normal;
                 o.tangent = v.tangent;
                 return o;
             }
 
             struct g2f
             {
                 float4 pos : SV_POSITION;
                 float3 color : COLOR0;
                 SHADOW_COORDS(0)
             };
 
             g2f VertexOutput(float3 pos, float directionalLightValue)
             {
                 g2f o;
                 o.pos = UnityObjectToClipPos(pos);
                 o.color = _LightColor0 * directionalLightValue;
                 TRANSFER_SHADOW(o)
                 return o;
             }
 
             [maxvertexcount(3)]
             void geo(triangle vertexOutput IN[3], inout TriangleStream<g2f> outStream)
             {
                 float3 flatNormal = normalize(cross(IN[1].vertex - IN[0].vertex, IN[2].vertex - IN[0].vertex));
                 float3 center = (IN[0].vertex + IN[1].vertex + IN[1].vertex) / 3;
 
                 float2 lightUVCookie = mul(unity_WorldToLight, float4(center, 1)).xy;
                 float lightMap = tex2Dlod(_LightTexture0, float4(lightUVCookie,0,0)).w;
                 float3 worldFlatNormal = UnityObjectToWorldNormal(flatNormal);
                 float directionalLightValue = max(0, dot(worldFlatNormal, _WorldSpaceLightPos0.xyz)) * lightMap;
 
                 // This results in inconsistant shadows:
                 // Create vertex output so that shadow sampeling are the same for the 3 vertices and therefore the whole face
                 g2f idealVertexOutput = VertexOutput(center, directionalLightValue);
 
                 // Only change SV_POSITION and append to outStream
                 idealVertexOutput.pos = UnityObjectToClipPos(IN[0].vertex);
                 outStream.Append(idealVertexOutput);
                 idealVertexOutput.pos = UnityObjectToClipPos(IN[1].vertex);
                 outStream.Append(idealVertexOutput);
                 idealVertexOutput.pos = UnityObjectToClipPos(IN[2].vertex);
                 outStream.Append(idealVertexOutput);
             }
 
             fixed4 frag(g2f i, fixed facing : VFACE) : SV_Target
             {
                 float shadow = SHADOW_ATTENUATION(i);
                 shadow = saturate(shadow + _ShadowSoftness);
                 return float4(i.color * shadow,1);
             }
             ENDCG
         }
         UsePass "Legacy Shaders/VertexLit/SHADOWCASTER"
     }
 }

Here is the results for the second option which worked! Although i would love to be able to support multiple lights with shadows in the future. Could you explain how rendering it to our own shadow map solves the issue of inconcistency. Is it the accuracy of floating points? Also did i miss somthing because the two spheres are constantly in full shadow with this change even though they are using the same exact material as the ground. And here is the shader (CopyShadowmap is the same as Namey5 described it):

 Shader "Custom/shadow testing shader 2"
 {
     Properties
     {
         _ShadowSoftness("Shadow Softness", Float) = 0.5
     }
 
     SubShader
     {
         Pass
         {
             Tags {
                 "RenderType" = "Opaque"
                 "LightMode" = "ForwardAdd"
                 "PassFlags" = "OnlyDirectional"
             }
             CGPROGRAM
             #include "UnityCG.cginc"
             #include "AutoLight.cginc"
             #include "Lighting.cginc"
             #pragma target 3.0
             #pragma vertex vert
             #pragma fragment frag
             #pragma geometry geo
             #pragma require geometry
             #pragma multi_compile_fwdbase nolightmap nodirlightmap nodynlightmap novertexlight
             
             sampler2D _LightTexture0;
             float4x4 unity_WorldToLight;
 
             UNITY_DECLARE_SHADOWMAP(_DirectionalShadowmap);
             float _ShadowSoftness;
 
             struct vertexInput
             {
                 float4 vertex : POSITION;
                 float3 normal : NORMAL;
                 float4 tangent : TANGENT;
             };
 
             struct vertexOutput
             {
                 float4 vertex : SV_POSITION;
                 float3 normal : NORMAL;
                 float4 tangent : TANGENT;
             };
 
             vertexInput vert(vertexInput v)
             {
                 return v;
             }
 
             vertexOutput tessVert(vertexInput v)
             {
                 vertexOutput o;
                 o.vertex = v.vertex; // Note that the vertex is NOT transformed to clip space here;
                 o.normal = v.normal;
                 o.tangent = v.tangent;
                 return o;
             }
 
             struct g2f
             {
                 float4 pos : SV_POSITION;
                 float3 color : COLOR0;
             };
 
             g2f VertexOutput(float3 pos, float directionalLightValue)
             {
                 g2f o;
                 o.pos = UnityObjectToClipPos(pos);
                 o.color = _LightColor0 * directionalLightValue;
                 return o;
             }
 
             [maxvertexcount(3)]
             void geo(triangle vertexOutput IN[3], inout TriangleStream<g2f> outStream)
             {
                 float3 flatNormal = normalize(cross(IN[1].vertex - IN[0].vertex, IN[2].vertex - IN[0].vertex));
                 float3 center = (IN[0].vertex + IN[1].vertex + IN[1].vertex) / 3;
 
                 float2 lightUVCookie = mul(unity_WorldToLight, float4(center, 1)).xy;
                 float lightMap = tex2Dlod(_LightTexture0, float4(lightUVCookie,0,0)).w;
                 float3 worldFlatNormal = UnityObjectToWorldNormal(flatNormal);
                 float directionalLightValue = max(0, dot(worldFlatNormal, _WorldSpaceLightPos0.xyz)) * lightMap;
 
                 // Support cascaded shadows
                 float4 shadowCoords0 = mul (unity_WorldToShadow[0], float4 (center, 1));
                 float4 shadowCoords1 = mul (unity_WorldToShadow[1], float4 (center, 1));
                 float4 shadowCoords2 = mul (unity_WorldToShadow[2], float4 (center, 1));
                 float4 shadowCoords3 = mul (unity_WorldToShadow[3], float4 (center, 1));
  
                 // Find which cascaded shadow coords to use based on our distance to the camera
                 float dist = distance (center, _WorldSpaceCameraPos.xyz);
                 float4 zNear = dist >= _LightSplitsNear;
                 float4 zFar = dist < _LightSplitsFar;
                 float4 weights = zNear * zFar;
                 float4 shadowCoords = shadowCoords0 * weights.x + shadowCoords1 * weights.y + shadowCoords2 * weights.z + shadowCoords3 * weights.w;
  
                 // Sample the shadowmap
                 float shadow = UNITY_SAMPLE_SHADOW (_DirectionalShadowmap, shadowCoords);
                 shadow = saturate(shadow + _ShadowSoftness);
 
                 // Create vertex output so that shadow sampeling are the same for the 3 vertices and therefore the whole face
                 g2f idealVertexOutput = VertexOutput(center, directionalLightValue * shadow);
 
                 // Only change SV_POSITION and append to outStream
                 idealVertexOutput.pos = UnityObjectToClipPos(IN[0].vertex);
                 outStream.Append(idealVertexOutput);
                 idealVertexOutput.pos = UnityObjectToClipPos(IN[1].vertex);
                 outStream.Append(idealVertexOutput);
                 idealVertexOutput.pos = UnityObjectToClipPos(IN[2].vertex);
                 outStream.Append(idealVertexOutput);
             }
 
             fixed4 frag(g2f i, fixed facing : VFACE) : SV_Target
             {
                 return float4(i.color,1);
             }
             ENDCG
         }
         UsePass "Legacy Shaders/VertexLit/SHADOWCASTER"
     }
 }

I am hesitant to leaving the issue solved since spheres are in full shadow and i would love to support multiple lights, i will keep looking into it.

Thanks again!

Comment
Add comment · Show 1 · 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 abbsimoga · Oct 03, 2021 at 07:49 AM 0
Share

The problem was that my center variable was in object space and not world space. This fixed the issue: fixed4 world_center = fixed4(mul (unity_ObjectToWorld, center).xyz, 1);

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

127 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

Related Questions

Is it possible for Shuriken Billboard particles to receive shadows from shadow maps? 0 Answers

How to prevent holes in shadows without generating Shadow Acne? 1 Answer

Using shadow texture to recieve shadows (on grass) 1 Answer

Close Fit, no Cascades directional shadow issue. 1 Answer

How to store directional light shadowmap? 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