Hi, I am trying for a while now to use DrawMeshInstancedIndirect to render object with textureArray on Android device. I am unable to get the textureID in structuredBuffer to work properly.
Here is a small snippet of code I use to send the buffer:
m_positionBuffer = new ComputeBuffer(instance.Count, sizeof(float) * 3);
m_textureBuffer = new ComputeBuffer(instance.Count, sizeof(float));
Vector3[] positions = new Vector3[instance.Count];
float[] textures = new float[instance.Count];
// in a for loop
positions[index] = new Vector3(pair.Value.Pos.x, pair.Value.Pos.y, pair.Value.Pos.z);
textures[index] = 1.0;
m_positionBuffer.SetData(positions);
m_textureBuffer.SetData(textures);
instanceMaterial.SetBuffer("positionBuffer", m_positionBuffer);
instanceMaterial.SetBuffer("textureBuffer", m_textureBuffer);
//later on update
Graphics.DrawMeshInstancedIndirect(instanceMesh, subMeshIndex, instanceMaterial, new Bounds(Vector3.zero, new Vector3(100.0f, 100.0f, 100.0f)), m_argsBuffer);
Here is a version of the shader that work fine when using a predefined textureId:
Shader"Custom/GPUInstanced" {
Properties {
_MainTexArray("Base Color Array", 2DArray) = "" {}
_TextureIdx("Texture Idx", float) = 0.0
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf Standard addshadow fullforwardshadows
#pragma multi_compile_instancing
#pragma instancing_options procedural:setup
#ifdef UNITY_PROCEDURAL_INSTANCING_ENABLED
StructuredBuffer<float3> positionBuffer;
#endif
sampler2D _MainTex;
UNITY_DECLARE_TEX2DARRAY(_MainTexArray);
UNITY_INSTANCING_BUFFER_START(Props)
UNITY_DEFINE_INSTANCED_PROP(float, _TextureIdx)
UNITY_INSTANCING_BUFFER_END(Props)
struct Input
{
float2 uv_MainTexArray;
};
void setup()
{
#ifdef UNITY_PROCEDURAL_INSTANCING_ENABLED
float3 data = positionBuffer[unity_InstanceID];
unity_ObjectToWorld._11_21_31_41 = float4(1.0f, 0, 0, 0);
unity_ObjectToWorld._12_22_32_42 = float4(0, 1.0f, 0, 0);
unity_ObjectToWorld._13_23_33_43 = float4(0, 0, 1.0f, 0);
unity_ObjectToWorld._14_24_34_44 = float4(data.xyz, 1);
unity_WorldToObject = unity_ObjectToWorld;
unity_WorldToObject._14_24_34 *= -1;
unity_WorldToObject._11_22_33 = 1.0f / unity_WorldToObject._11_22_33;
#endif
void surf(Input IN, inout SurfaceOutputStandard o)
{
fixed4 c = UNITY_SAMPLE_TEX2DARRAY(
_MainTexArray,
float3(IN.uv_MainTexArray, UNITY_ACCESS_INSTANCED_PROP(Props, _TextureIdx))
);
o.Albedo = c.rgb;
o.Alpha = 1.0f;
}
ENDCG
}
FallBack "Diffuse"
}
The moment I am trying to sample the textureId buffer then the object stop rendering:
Shader"Custom/GPUInstanced" {
Properties {
_MainTexArray("Base Color Array", 2DArray) = "" {}
_TextureIdx("Texture Idx", float) = 0.0
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf Standard addshadow fullforwardshadows
#pragma multi_compile_instancing
#pragma instancing_options procedural:setup
#ifdef UNITY_PROCEDURAL_INSTANCING_ENABLED
StructuredBuffer<float3> positionBuffer;
StructuredBuffer<float> textureBuffer;
#endif
sampler2D _MainTex;
UNITY_DECLARE_TEX2DARRAY(_MainTexArray);
UNITY_INSTANCING_BUFFER_START(Props)
UNITY_DEFINE_INSTANCED_PROP(float, _TextureIdx)
UNITY_INSTANCING_BUFFER_END(Props)
struct Input
{
float2 uv_MainTexArray;
};
void setup()
{
#ifdef UNITY_PROCEDURAL_INSTANCING_ENABLED
float3 data = positionBuffer[unity_InstanceID];
unity_ObjectToWorld._11_21_31_41 = float4(1.0f, 0, 0, 0);
unity_ObjectToWorld._12_22_32_42 = float4(0, 1.0f, 0, 0);
unity_ObjectToWorld._13_23_33_43 = float4(0, 0, 1.0f, 0);
unity_ObjectToWorld._14_24_34_44 = float4(data.xyz, 1);
unity_WorldToObject = unity_ObjectToWorld;
unity_WorldToObject._14_24_34 *= -1;
unity_WorldToObject._11_22_33 = 1.0f / unity_WorldToObject._11_22_33;
#endif
void surf(Input IN, inout SurfaceOutputStandard o)
{
float texture_array_id = 0.0f;
#ifdef UNITY_PROCEDURAL_INSTANCING_ENABLED
texture_array_id = textureBuffer[unity_InstanceID];
#endif
fixed4 c = UNITY_SAMPLE_TEX2DARRAY(
_MainTexArray,
float3(IN.uv_MainTexArray, texture_array_id)
);
o.Albedo = c.rgb;
o.Alpha = 1.0f;
}
ENDCG
}
FallBack "Diffuse"
}
We are using Unity 2019.4.22 Any help would be very appreciated.