How to do frustum culling with instancedMesh?

Turns out it wasn’t that hard:

import { Sphere } from 'three';

export interface CullableObject {
  boundingSphere?: Sphere;
  cullable?: boolean;
}

const _sphere = new Sphere();

// Patch Frustum to allow culling at object level.
const prevImpl = Frustum.prototype.intersectsObject;
Frustum.prototype.intersectsObject = function (object: Object3D) {
  const cullable = object as unknown as CullableObject;
  if (cullable.boundingSphere) {
    _sphere.copy( cullable.boundingSphere ).applyMatrix4( object.matrixWorld );
    return this.intersectsSphere(_sphere);
  }
  return prevImpl.call(this, object);
};
4 Likes