Thank you for replaying

I have already copyed the vertex buffer into a native array.

I now have found a way to solve my problem pretty fast. The only problem is that it uses meshes, so I can't use this for big vertex buffers (maximum 65534 faces)

Here is what I do:

1.Create a temporary mesh with my vertexbuffer (I use PositionOnly for speed reasons)

Code:

Dim TempMesh As New Mesh(TargetBufferVertexCount / 3, TargetBufferVertexCount, MeshFlags.Managed, VertexFormats.Position, d3dDevice)
Dim gs As GraphicsStream = TempMesh.VertexBuffer.Lock(0, 0, LockFlags.None)
For i As Long = 0 To TargetBufferVertexCount - 1
gs.Write(New CustomVertex.PositionOnly(TargetVerts(i).Position))
Next

2.Build a simple index buffer with index1=1, index2=2, ....

Code:

Dim indices(TargetBufferVertexCount) As Long
For i As Long = 0 To TargetBufferVertexCount - 1
indices(i) = i
Next
Dim ib As IndexBuffer = TempMesh.IndexBuffer
ib.SetData(indices, 0, LockFlags.None)

3.Calculate the adjacency (the neighbors for each face)

Code:

Dim Adjacency(TargetBufferVertexCount * 9) As Integer
TempMesh.GenerateAdjacency(0, Adjacency)

4. Dispose the mesh

5.Make a function that gets all neighbours and calls itself again for every new neighbour

Code:

GetNeighbours(0, Adjacency, SelectedTriangles)

0 is the triangle to start

Adjacency is the adjacency array

SelectedTriangles is the array we want to fill with all connected triangles

Here is the body of the function:

Code:

Private Sub GetNeighbours(ByVal TriangleIndex As Integer, ByVal Adjacency() As Integer, ByVal SelectedTriangles As ArrayList)
Dim Triangle As Integer = Adjacency(TriangleIndex * 3)
If Not SelectedTriangles.Contains(Triangle) Then
SelectedTriangles.Add(Triangle)
GetNeighbours(Triangle, Adjacency, SelectedTriangles)
End If
Triangle = Adjacency(TriangleIndex * 3 + 1)
If Not SelectedTriangles.Contains(Triangle) Then
SelectedTriangles.Add(Triangle)
GetNeighbours(Triangle, Adjacency, SelectedTriangles)
End If
Triangle = Adjacency(TriangleIndex * 3 + 2)
If Not SelectedTriangles.Contains(Triangle) Then
SelectedTriangles.Add(Triangle)
GetNeighbours(Triangle, Adjacency, SelectedTriangles)
End If
End Sub

This function checks each of the three neighbour-triangles if they have already been added. If not, the function is repeated for this neighbour.

Everyone can use this code at own will, but if you can still speed it up, please post your ideas. I would be happy about it.