I’ve been trying to get a Graphics.DrawMeshInstancedIndirect mesh to have a material that can receive lighting. To a degree I’ve gotten ambience working. Is this actually possible to have them individually receive shadows from the main light and shadow casters? Will this require a compute shader to accomplish or something?
Here’s the code I’ve got and I’ve included an unlisted yt video below of the problem…
I’m not going to include all of the code for using Graphics.DrawMeshInstancedIndirect here, but I will show the method call. I am making sure recieve shadows is set to true:
Graphics.DrawMeshInstancedIndirect(mesh, 0, material, bounds, argsBuffer, 0, null, UnityEngine.Rendering.ShadowCastingMode.On, true);
And I’ve tried basically for 16 hours straight right now going over shader after shader to try to get it working.
Shader "TestGrass/GrassF" {
Properties{
_Color("_Color", Color) = (0.26,0.78,0.36,1)
_Brightness("_Brightness", float) = 1.0
_MaxStrength("_MaxStrength", float) = 0.5
_MinStrength("_MinStrength", float) = 0.25
_StrengthScale("_StrengthScale", float) = 0.05
_Speed("_Speed", float) = 60
_Offset("_Offset", float) = 0.0
_Interval("_Interval", float) = 1.0
_HeightOffset("_HeightOffset", float) = 2.1
_Detail("_Detail", float) = 1.0
}
SubShader{
Tags { "RenderType" = "Opaque" "LightMode" = "ForwardBase" }
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fwdbase
#include "UnityCG.cginc"
#include "UnityLightingCommon.cginc"
#include "AutoLight.cginc"
float _Brightness;
float _MaxStrength;
float _MinStrength;
float _StrengthScale;
float _Speed;
float _Offset;
float _Interval;
float _HeightOffset;
float _Detail;
struct appdata_t {
float4 vertex : POSITION;
float4 color : COLOR;
float4 texcoord : TEXCOORD0;
};
struct v2f
{
float4 pos : SV_POSITION;
LIGHTING_COORDS(0, 1)
float4 color : COLOR;
};
struct MeshProperties {
float4x4 mat;
float4 color;
};
float rand(float2 p)
{
return frac(sin(dot(p, float2(12.9898, 78.233))) * 43758.5453);
}
float getWind(float2 vertex, float2 uv, float time) {
float detailOffset = rand(float2(1.0, 2.0));
float diff = pow(_MaxStrength - _MinStrength, 2.0);
float strength = clamp(_MinStrength + diff + sin(time / _Interval) * diff, _MinStrength, _MaxStrength) * _StrengthScale;
float wind = (sin(time) + cos(time * _Detail)) * strength * max(0.0, (1.0 - uv.y) - _HeightOffset);
return wind;
}
fixed4 _Color;
StructuredBuffer<MeshProperties> _Properties;
v2f vert(appdata_full v, uint instanceID: SV_InstanceID) {
float time = _Time * _Speed + _Offset;
v.vertex.x += getWind(v.vertex.xy, v.texcoord.xy * -1, time);
v2f o;
float4 pos = mul(_Properties[instanceID].mat, v.vertex);
o.pos = UnityObjectToClipPos(pos);
o.color = _Properties[instanceID].color;
half3 worldNormal = UnityObjectToWorldNormal(v.normal);
half nl = max(0, dot(worldNormal, _WorldSpaceLightPos0.xyz));
o.color = nl * _LightColor0;
o.color.rgb += ShadeSH9(half4(worldNormal, 1));
TRANSFER_VERTEX_TO_FRAGMENT(o);
return o;
}
fixed4 frag(v2f i) : SV_Target{
fixed4 col = _Color * _Brightness;
col *= i.color;
float attenuation = LIGHT_ATTENUATION(i);
col *= attenuation;
return col;
}
ENDCG
}
}
Fallback "VertexLit"
}