Issues with shadows when using GPU instancing

I have a vertex shader that works great without GPU instancing, but when I use the shader with it, the shadows don’t show up. The instanced objects don’t cast or recieve any shadows. No errors or warnings show up.
I would also like for it to not cast shadows if it is possible.

I would be greatful for any help and hints.

Shader "Unlit/test"
{
SubShader
{
Pass
{
Tags {"LightMode" = "ForwardBase"}
CGPROGRAM
#pragma vertex VSMain
#pragma fragment PSMain
#pragma multi_compile_instancing
#pragma multi_compile_fwdbase
#include "UnityCG.cginc"
#include "AutoLight.cginc"

struct GrassData
{
float4 position;
float3 scale;
float rot;
};
StructuredBuffer<GrassData> Positions;
struct SHADERDATA
{
float4 position : SV_POSITION;
float4 _ShadowCoord : TEXCOORD1;
float3 normal : NORMAL;
};
float bias(float x, float bias)
{
float k = pow(bias, 3);
return (x * k) / (x * k - x + 1);
}
SHADERDATA VSMain(float4 vertex : POSITION, float2 uv : TEXCOORD0, float3 normal : NORMAL, uint instanceID : SV_INSTANCEID)
{
SHADERDATA vs;
vs.position = UnityObjectToClipPos(vertex + Positions[instanceID].position);
vs._ShadowCoord = ComputeScreenPos(vs.position);
vs.normal = normal;
return vs;
}
float4 PSMain(SHADERDATA ps) : SV_TARGET
{
// basic lighting
float3 lightDir = _WorldSpaceLightPos0.xyz;
float ndotl = dot(lightDir, ps.normal);
float darknessCorrection = .15;
float lightLevel = clamp(bias(SHADOW_ATTENUATION(ps), 2) * ndotl, 0, 1);
float lightLevelCorrected = (lightLevel * (1 - darknessCorrection) + darknessCorrection);
// bias function for shadow artifact correction
float3 col = float3(1,1,1) * lightLevelCorrected;
return float4(col,1);
}
ENDCG
}
Pass
{
Tags{ "LightMode" = "ShadowCaster" }
CGPROGRAM
#pragma vertex VSMain
#pragma fragment PSMain
#pragma multi_compile_shadowcaster
#pragma multi_compile_instancing
#include "UnityPBSLighting.cginc"
#include "UnityCG.cginc"
#include "AutoLight.cginc"
struct GrassData
{
float4 position;
float3 scale;
float rot;
};
StructuredBuffer<GrassData> Positions;
float4 VSMain(float4 vertex : POSITION, uint instanceID : SV_INSTANCEID) : SV_POSITION
{
float4 baseClipPos = UnityObjectToClipPos(vertex + Positions[instanceID].position);
// more shadow artifact correction
return UnityApplyLinearShadowBias(baseClipPos);
}
float4 PSMain(float4 vertex : SV_POSITION) : SV_TARGET
{
return 0;
}
ENDCG
}
}
}
1 Like

bump, having the same issue

I would suggest taking a look into a shader that works with GPU instancing and check how it’s done. There’s a bunch of macros that need to be used to set things up correctly.