ShaderGraph Instancing

Hello guys,

i’am asking for help, because i don’t know why it’s not working?

My ShaderGraph Setup:
boolean keyword (PROCEDURAL_INSTANCING_ON) multiCompile, global

One CustomFunction as string

#pragma instancing_options procedural:ConfigureProcedural
Out = In;

a hlsl Script for the ShaderGraph to get the StructuredBuffer Data

#if defined(UNITY_ANY_INSTANCING_ENABLED)
StructuredBuffer<float3> _Positions;
#endif

float3 position;

#if UNITY_ANY_INSTANCING_ENABLED
void ConfigureProcedural() {
    position = _Positions[unity_InstanceID];
}
#endif

void ShaderGraphFunction_float(out float3 Out) {
    Out = position;
}

void ShaderGraphFunction_half(out half3 Out) {
    Out = position;
}

And here a picture of the shadergraph node setup:

I tested it with a SurfaceShader, it works fine, but the ShaderGraph renders/shows no objects.

Is there a mistake at some point?

Hope someone see it with differend eyes.

Thank you all :slight_smile:

position = _Positions[unity_InstanceID];
You should not just store a uniform variable’s value back from the structure buffer…

Try this approach instead :

#ifndef PROCEDURL_INSTANCE_INCLUDED
#define PROCEDURL_INSTANCE_INCLUDED

struct positionBuffer {
    float4 Position;
};

StructuredBuffer<positionBuffer> _PerInstanceData;

// use #pragma instancing_options procedural:vertInstancingSetup to setup unity_InstanceID & related macro, what vertInstancingSetup do are actually not important 
#if UNITY_ANY_INSTANCING_ENABLED
    void vertInstancingSetup() {
    }
#endif

// Shader Graph Functions
void GetInstancingPos_float(in float3 PositionWS, out float3 Out)
{
    Out = 0;
    #if UNITY_ANY_INSTANCING_ENABLED
    Out = PositionWS + _PerInstanceData[unity_InstanceID].Position.xyz;
    #endif
}

#endif

Instead of store back the strcuture buffer’s value you can just add the value and output the result using OUT keyword.

Also the global keyword
8492804--1129994--upload_2022-10-6_12-14-42.png

I’m able to use DrawMeshInstancedIndirect api via this approach