How to get InstanceId in Shadergraph?

I want to work with instancing using Shadergraph but I can’t find a simple way to get the InstanceID in the graph. I believe this would have many uses especially considering ECS uses drawmeshinstanced.

Is there a way to get the InstanceID in Shadergraph without a sloppy workaround?

A bit of a Necro, but some updates here for anyone else finding this off google:

Not sure when it was added, but at least in Unity 2021.2, InstanceID is now exposed in the shader graph as a standard node.

You can then add a custom function node which takes in InstanceID, and retrieves variables from a ComputeBuffer assigned to your MaterialPropertiesBlock.

For example, create a hlsl file with the following code, and call getMcguffin via the custom function node.

#ifndef WHATEVER_INCLUDE_NAME_YOU_LIKE
#define WHATEVER_INCLUDE_NAME_YOU_LIKE

CBUFFER_START(UnityPerMaterial)
StructuredBuffer<float4> _WhateverBufferName;
CBUFFER_END

void getMcguffin_float(in float InstanceID, out float4 mcguffin) {
	mcguffin = _WhateverBufferName[InstanceID];
}
#endif

Then pass in the InstanceID on the graph to the custom function node, and take your property out as output.

On the C# side, declaring and attaching the compute buffer to the MaterialPropertyBlock is standard fair, which you can google the syntax for relatively easily.

I’m also interested in this. Is it possible with a custom node, maybe?

Any update for this?

i do not know why。 access InstanceID is wrong in shadergraph use my hlsl。i set computebuff。this is float3 type。it is world position。 i call Graphics.RenderMeshPrimitives render 8 instance。but only 4 instance view in unity。

Here’s a tutorial on how to get a specific item on an array for a given instance Id