- Home /
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?
You can try create integer field in shader and from the unity script override it on start from c# script.
It is just some idea, maybe there is simpler way idk ;)
Found this also: https://forum.unity.com/threads/instance-id-in-shader.501821/
Answer by OutOfNothing · Nov 16, 2021 at 03:42 PM
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.
Thank you !!!
Why do you assign the Buffer via a MaterialPropertyBlock and not directly to the Material ?
Answer by owen_proto · Feb 29, 2020 at 01:51 PM
I'm also interested in this. Is it possible with a custom node, maybe?
Answer by DongJooByun · Mar 20, 2020 at 06:56 PM
Any update for this?
If unity has a plan to add this feature, I think it would be a separate node in the shadergraph, like a "instanceId" or "unity_InstanceID" node.
But, I have an opinion about the na$$anonymous$$g of this attribute because I was confused when I started using unity_InstanceID. Correct me if I'm wrong. As I know, the "instance id" is used for representing individual instance among multiple instances. The id of what instance the instances are just "id". Let's say that we have 1,000 particles or points, and we have a box, a sphere and a tetrahedron. When we make the points instance boxes, spheres and tetrahedrons randomly, the instance ids are
0 : box 1 : sphere 2 : tetrahedron
and the ids of particles are
0, 1, 2, ... , 997, 998, 999.
But, I think we've used "instanceId(unity_InstanceID)" like "id" in unity.
All in all, if I have the right understanding about ids, the new node in the shadergraph, should be named as "id". And "instanceId" should be used for instances in the multi-instancing system.
Your answer
![](https://koobas.hobune.stream/wayback/20220612221935im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
water foam on the horizontal axis 0 Answers
URP - Grayscale to black and white 0 Answers
Unable to alter color properties in shader graphs 4 Answers
Shader Graph transparency issue 0 Answers