Per Instance Id For Static Mesh Instances

Hi,

I’d like to access the unique instance Id of each static mesh instance… is there a way to do this? This would be similar to the value passed in via the SV_InstanceId semantic.

Is there a way to do this? I’ve tried adding a custom HLSL node to my function, but it doesn’t look like the instance id is avaliable in the generated function.

Cheers!
Andy

Hello, hidsmurf2000,

You can retrieve the index of a given static mesh by using the “Hit Item” output pin (int) from a hit result. Then you can use that to get the particular properties of that instance (such as its transform) after you have casted to the InstancedStaticMesh class from the hit actor.

See this tutorial for details: UE4 - Interact with Foliage at Run-time (Cut down trees, remove foliage, etc...) - YouTube

Also, if you are talking about accessing the index from within the material, there is a material node that will get a random number out of the instance index and that will help you to randomize your looks a bit.

See this answer: https://answers.unrealengine.com/questions/280115/is-it-possible-to-use-instanced-material-on-instan.html

As for passing parameters to the material instance of a particular instanced mesh, remember that they are all the same draw call and are sharing the same material, so you would be affecting them all.

Thanks Diego, yes I’m after an instance id in the material. The random number isn’t quite enough for me, as I need to specifically lookup data on a per-instance basis - for example storing the color of each instance in a texture and then looking up a color per-instance (which cannot be random).

As far as I know there is no way to do this yet. If they are offering us a node to get a random number seeded by the instance index, I don’t see why we shouldn’t be able to access the instance index directly. It is stored on the gpu, right? Wouldn’t need to be additional draw calls? But the node isn’t there.

Depending on your use case, you could maybe hack something out using worldPosition, and using only it’s x,y components so you know where you are in a plane.

If your instances possible positions can be divided into a grid, and that grid’s measures are known, then you could know if your world position is in a given repetition of that distance, so in a given grid number.

Come to think of it, wouldn’t the actor position node give you the information necessary to do this check too?

Is there a solution to this problem? I have the same problem.

I am looking for a solution too… I cannot use position or any other property because they’re all changing on a per-instance basis. I think the solution I will use will be to assign a random number to the per instance custom data. That randomly assigned number will be a make-shift ‘id’.

Check this: