- Home /
URP Shader Graphs with instanced indirect - InstanceID node seems to give 0
Hello!
I'm trying to render meshes with shader graph shaders from a custom render pass using CommandBuffer.DrawMeshInstancedIndirect. I'm passing my positions to the shader via a StructuredBuffer defined in a custom function node. I index the buffer with the instance id and return the position, that gets added to the object space vertex position. This works great, but it renders all of my objects on the same position (the first one in the buffer).
I checked the draw command with renderdoc and it was indexing the buffer for every instance with 0 instead of the instance id.
I was able to make it work without shader graphs, using SV_InstanceID, but i would really love to get it working with shader graphs.
I also got multiple objects to render with DrawMeshInstanced, using shader graphs, but then i'm not able to use compute buffers to provide the count and I also have to make a long array of matrices. However the instance id worked that time.
I attached some pictures to hopefully make it more understandable. Any ideas what I'm doing wrong? (I selected instance 24 in the renderdoc picture)
Answer by Gergo4222 · Dec 18, 2021 at 07:08 PM
I managed to solve it using this code: https://gist.github.com/Cyanilux/4046e7bf3725b8f64761bf6cf54a16eb
I was missing this:
#pragma instancing_options procedural:setup
Answer by Llockham-Industries · Mar 11 at 11:18 PM
Hey @Gergo4222 , trying to achieve something similar.
How does one use #pragma instancing_options procedural:setup?
I continue to get the following error after following the comments in the link. "UNITY_INSTANCING_PROCEDURAL_FUNC must be defined."
Starting out super simple, & still struggling. Here is what I have thus far -
#ifndef EXPANSE_INCLUDED
#define EXPANSE_INCLUDED
StructuredBuffer<float4> _PositionBuffer;
#pragma instancing_options procedural:vertInstancingSetup
void GetPosition_float(float instanceID, out float4 position)
{
position = _PositionBuffer[instanceID];
}
#endif
& the barebones shader -
Any help appreciated, cheers.
Hi @Llockham-Industries !
The error is because pragmas must be defined directly in the shader graph, so no external files are allowed for that. You have to type them into a custom function node with the type set to string like this:
Also, you should probably set the built in unity_ObjectToWorld and unity_WorldToObject transformation matrices instead of adding to the vertex positions. This way you can set not only the position, but the rotation and scale as well. Here is how i did that:
#include "Assets/InstancedTerrain/AdditionalHLSL/MatrixOperations.hlsl"
#ifndef INSTANCEDTERRAIN_INCLUDED
#define INSTANCEDTERRAIN_INCLUDED
StructuredBuffer<float4x4> _TEMatrices;
StructuredBuffer<uint> _CullingResultIdxs;
void setMatrices(inout float4x4 objectToWorld, inout float4x4 worldToObject)
{
float4x4 matr = _TEMatrices[_CullingResultIdxs[unity_InstanceID]];
objectToWorld = mul(objectToWorld, matr);
// Inverse transform matrix
worldToObject = inverse(objectToWorld);
}
void setupInstancing()
{
setMatrices(unity_ObjectToWorld, unity_WorldToObject);
}
void FNodeDummy_float(in float3 IN, out float3 OUT)
{
OUT = IN;
}
#endif