Occlusion culling

Hello @caroline

is there any simple way in metal to enable occlusion culling for wireframe?? if I take Model chapter, there the model seems to be rendered with just visible faces,but I might be wrong. when textures are used… ofc only those “visible” ones are visible. but how shall I achieve that render of model ( can be just 2 quads, 2 triangles, respects visibility? e.g. if part of a triangle is covered by another triangle, that the non-visible part is hidden, even in wireframe?

thx a lot

Apple has a fragment visibility buffer sample: Apple Developer Documentation

But it doesn’t help with wireframe.

I think the way to do it would be not to use the encoder line fill, but to do your own wireframe in the fragment shader using barycentric coordinates.

So you can do occlusion culling, and only render those visible fragments, and when you call the fragment shader on the visible fragment, you choose whether to discard that fragment depending on the barycentric coordinate.



If you take Chapter 9’s sample, and replace the fragment shader with this:

fragment float4 fragment_main(
  constant Params &params [[buffer(ParamsBuffer)]],
  VertexOut in [[stage_in]],
  float3 bary [[barycentric_coord]])
  if (bary.x < 0.01 || bary.y < 0.01 || bary.z < 0.01) {
    return 0;
  return 1;

And also add this before the draw call in Renderer:


You get:

That obviously is just a starting point, as the fragment shader isn’t being occluded, and the lines are variable size.

But if you play around with the sizing and stencil tests or occlusion, you should be able to get what you want.

This shows how to fix the variable sizing:

Chapter 15 talks about stencil tests.

1 Like