[URP] ComputeBuffers + DrawMeshInstancedIndirect + Lit Shader

Hello,

I’ve been experimenting with the function DrawMeshInstancedIndirect along with Compute Buffers (to update the instanced positions with a flocking algorithm). I can display all the mesh instances with a basic colour but when I want to be able to use the built in URP Lit Shader on the generated Instances it is becoming complicated to say the least.

I’ve been trying to create a custom Lit Shader by duplicating and modifying the LitForwardPass.hlsl but I’m not sure if I will be able to actually access the unity_InstanceID necessary to get the proper mesh instance from the buffer:

Varyings LitPassVertex(Attributes input)
{
Varyings output = (Varyings)0;

UNITY_SETUP_INSTANCE_ID(input);
UNITY_TRANSFER_INSTANCE_ID(input, output);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);

#if SHADER_TARGET >= 45
Boid data = boidsBuffer[???];
#else
Boid data = 0;
#endif

Not sure how to actually access the id at ???.
The Boid data is made like this :

struct Boid
{
float4 position;
float3 direction;
float noise_offset;
};

StructuredBuffer boidsBuffer;

Any idea / suggestion?

I’m happy to share more of the code if need be.

Thanks
Romain

1 Like

I’m having this exact issue myself. I tried disabling SRP batching but am still totally unable to find the instance ID in my shader when drawing with DrawMeshInstancedIndirect.

As an update, I was able to get access to the instance ID by including it in the input struct for the vertex shader with the SV_InstanceID semantic. However no instancing keywords are enabled so you will have to set your own keywords if you want dedicated instancing variants of your shader. Credit goes to the post here unity_InstanceID always 0 in URP?

1 Like

Hello,

Yes I’ve been able to do this on an unlit shader. My issues here is to make it work with the Lit URP Standard Shader which is more complicated.