- Home /
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:
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)
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"
}
}
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!
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);