Skip to content

Instantly share code, notes, and snippets.

@slembcke
Created August 1, 2023 16:26
Show Gist options
  • Save slembcke/e77bec0c34fc773e22173557d474c06b to your computer and use it in GitHub Desktop.
Save slembcke/e77bec0c34fc773e22173557d474c06b to your computer and use it in GitHub Desktop.
Cull a bounding box (-1, 1) using the MVP transform.
// Check if a tile is visible onscreen using it's MVP transform.
static inline bool MapTileFrustumCull(const mat4 mvp){
// Clip space center and extents.
vec4 c = {mvp.m[12], mvp.m[13], mvp.m[14], mvp.m[15]};
vfloat ex = vabs(mvp.m[ 0]) + vabs(mvp.m[ 4]) + vabs(mvp.m[ 8]);
vfloat ey = vabs(mvp.m[ 1]) + vabs(mvp.m[ 5]) + vabs(mvp.m[ 9]);
vfloat ez = vabs(mvp.m[ 2]) + vabs(mvp.m[ 6]) + vabs(mvp.m[10]);
// Check the bounds against the clip space viewport using a conservative w-value.
vfloat w = vmax(0, c.w + vabs(mvp.m[ 3]) + vabs(mvp.m[ 7]) + vabs(mvp.m[11]));
return ((vabs(c.x) - ex < w) && (vabs(c.y) - ey < w) && (vabs(c.z) - ez < w));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment