Hello!

For context, I’m working with Sebastian Lague’s marching cubes (planet) project as a base. In it, he uses a RenderTexture with a volume depth to pass to various compute shaders using it as a 3d array of floats.

I’m modifying the project so that I can check every element within this 3d array of floats to see if its xyz index, when treated as a vector3, is inside a mesh in the Unity scene.

I’ve been able to get the mesh’s vertex buffer working using this code:

```
mesh.vertexBufferTarget |= GraphicsBuffer.Target.Raw;
var vertexBuffer = mesh.GetVertexBuffer(0);
MyDensityCompute.SetBuffer(0, "meshVertexBuffer", vertexBuffer);
MyDensityCompute.SetInt("vertexCount", mesh.vertexCount);
var locToWorldMatrix = theMeshGameObject.transform.localToWorldMatrix;
MyDensityCompute.SetMatrix("localToWorldMatrix", locToWorldMatrix);
```

```
ByteAddressBuffer meshVertexBuffer;
int vertexCount;
float4x4 localToWorldMatrix;
float3 load_vertex(int i) {
int i_location = i * 56;
float3 vert = float3(0.0f, 0.0f, 0.0f);
vert.x = asfloat(meshVertexBuffer.Load(i_location));
vert.y = asfloat(meshVertexBuffer.Load(i_location + 4));
vert.z = asfloat(meshVertexBuffer.Load(i_location + 8));
return mul(localToWorldMatrix, float4(vert, 1)).xyz;
}
```

where i is >= 0 and <= vertexCount

I’m struggling with figuring out how to use the mesh’s index buffer to access the indices of each triangle of the mesh. Here’s what I have so far:

```
mesh.indexBufferTarget |= GraphicsBuffer.Target.Raw;
mesh.indexBufferTarget |= GraphicsBuffer.Target.Index;
var indexBuffer = mesh.GetIndexBuffer();
MyDensityCompute.SetBuffer(0, "meshIndexBuffer", indexBuffer);
MyDensityCompute.SetInt("triangleCount", mesh.GetTriangles(0).Length);
```

```
ByteAddressBuffer meshIndexBuffer;
int triangleCount;
int3 getTriangleIndices(int idx){
int i_loc = idx * 16;
int id1 = asint(meshIndexBuffer.Load(i_loc));
int id2 = asint(meshIndexBuffer.Load(i_loc + 4));
int id3 = asint(meshIndexBuffer.Load(i_loc + 8));
return int3(id1, id2, id3);
}
```

where idx is >= 0 and <= triangleCount.

I then call load_vertex, passing the x, y, or z value from the in3 returned by getTriangleIndices, and Unity hard crashes.

Any direction on where to go from here would be greatly appreciated, thank you!