This document is about: QUANTUM 3
SWITCH TO

Frustum Prediction Culling

The snippet shows how to apply simple frustum culling to Quantum entities.

Culled entities in Quantum will not be updated when simulating predicted frames which is a performance improvement.

Read more about Profiling in Quantum.

C#

namespace Quantum
{
  using Photon.Deterministic;

  public class FrustumPredictionCulling : QuantumViewComponent<CameraContext>
  {
    public float ProximityRadiusOverhaul = 20;

    public override void OnActivate(Frame frame)
    {
      Game.Frames.Verified.Context.CullingCallback = Callback;
    }

    private bool Callback(FPVector3 position)
    {
      var unityPosition = position.ToUnityVector3();

      // use Camera.WorldToViewportPoint, check if X and Y are within 0…1 range AND check Z>0
      var normalizedPos = ViewContext.Camera.WorldToViewportPoint(unityPosition);

      var distance = (unityPosition - ViewContext.Camera.transform.position).sqrMagnitude;

      if (distance < ProximityRadiusOverhaul * ProximityRadiusOverhaul) {
        return false;
      }

      // check culling cases
      if (normalizedPos.z <= 0) 
      {
        return true;
      }

      if (normalizedPos.y < 0 || normalizedPos.y > 1) 
      {
        return true;
      }

      if (normalizedPos.x < 0 || normalizedPos.x > 1) 
      { 
        return true;
      }

      // within frustum unit
      return false;
    }
  }
}

Make the main game camera accessible via a view context script for example.

C#

namespace Quantum
{
  using UnityEngine;

  public class CameraContext : QuantumMonoBehaviour, IQuantumViewContext
  {
    public Camera Camera;
  }
}
Back to top