(Question)Triangle culling

is possible to implement triangle culling in monogame? how it work?

Ya sure cpu side if your sending in non instanced triangles.
I don’t think you can do it on the gpu as the vertice shader acts on individual vertices not full triangles.

Cpu side (for any above shown plane of the polyhedron frustrum as shown in your picture).
Define a surface of a side of the culling frustrum as it is a plane by creating a ray extending from it.

Take any point on one of the planes and create a surface normal extending out from it.
The point can be a corner vertice of the shown frustrum in your above illustration.
The surface normal can be found by the cross product of 3 of the corner vertices of a side.
(Sn)
Select one of the frustrum points to be the point of the ray extending from the surface.
Well denote this as the surface vertice.
(Sv)
Well denote each triangle vertice as
Tv0, Tv1, Tv2
Create a plane to vertice testing normal for each by.
Rn0 = Tv0 - Sv; Rn1 = Tv1 - Sv; Rn2 = Tv2 - Sv;
(pretty sure non of these normals need to be unit length (.normalized()) we just need the sign)
Next well test by Dot product of the original planes ray or its surface normal
Sn
Against the plane to surface testing normals we just found for each triangle vertice.
Rn0, Rn1, Rn2.
Using a Vector3. dot() product.
We find the resulting sign of the result of each test if all 3 are negative or positive depending on the direction of the surface normal sn we created
(the order we chose to pass the variables we cross product to find sn previously)
Then we have 3 dot results corresponding to a full triangle we can test for culling.

if(Vector3.Dot(Sn, Rn0) < 0 && Vector3.Dot(Sn, Rn1) < 0 && Vector3.Dot(Sn, Rn2) < 0)
{
// The triangle is completely outside the frustrum so don’t draw it.
}
else
{
// The triangle is at least partially within the frustrum so draw the triangle
}

but do it on cpu is too exp, wat if we can calculate the vertices normal direction is shader?

That would be great maybe one of the other guys here that are better with hlsl knows a way to achieve this.

However i am under the impression you can’t do that without a geometry shader on the gpu. Because you need to test all 3 vertices at once.

You can cull per pixel of course but that isn’t culling entire triangles.
Though that might actually be more useful.

You would have to simply test and not send each triangle that was to be culled cpu side i think without a geometry shader.
Or do something with bsp trees to make it faster but i think just culling each position in the vertice shader against 4 rays passed into the shader corresponding to each side of the frustrum would be faster and better looking . The steps are the same as previously described but you create a dot from sn to each vertice in the vertice shader though thats a lot more total operations the gpu is parallel processing all of them at once so.

thanks, i think i got wat u mean. discard in pixel shader doesn’t help in performance.

Without geometry shaders we cannot alter geometry in a shader (delete nor create) only move vertices :frowning:

What do we want? Compute APIs!

1 Like