Custom Occlusion Frame

I am working on my side camera (2D ortho) and I want to occlude anything that is too far from the center of the camera. I had determined this should start at 24 units from the camera’s transform.position.

For a quick fix I made some quads and have them as a border that moves with the camera, but this does not cover an infinite space, but only as thick as I make it.

Is there someway to make a mask that hides everything outside that 24*24 area?
I am hoping I don’t have to have some crazy code that spawns in a bunch of quads based on what the resolution is, but maybe that is actually the most simple solution?

My purpose is to restrict the area of view to a maximum of 1536px*1536px.

Any help on the matter is appreciated.

After a few hours of thinking and coding, I was able to come up with a solution:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ScreenArea : MonoBehaviour {

    private static GameObject gameManager;
    private static GameManager gameManagerScript;
    private static float screenHeightOld;
    private static int screenSizeMinimum = 768;
 
    void Awake() {
        gameManager = GameObject.FindWithTag("GameManager");
        gameManagerScript = gameManager.GetComponent<GameManager>();
        EnforceScreenSizeLimits();
        ChangeCameraSize();
    }
 
    void Update() {
        if (screenHeightOld != Screen.height) {
            screenHeightOld = Screen.height;
            EnforceScreenSizeLimits();
            ChangeCameraSize();
        }
    }
 
    private void ChangeCameraSize() {
        float newSize = Screen.height / (gameManagerScript.pixelsPerUnit * 4);
        newSize /= (int)(newSize / gameManagerScript.pixelsPerUnit);
        gameManagerScript.mainCameraCamera.orthographicSize = newSize;
    }
 
    private void EnforceScreenSizeLimits() {
        if (Screen.height < screenSizeMinimum) {
            Debug.LogWarning("Screen height too small. Minimum size will be enforced.");
            if (Screen.fullScreen = false) {
                Screen.SetResolution(Screen.width, screenSizeMinimum, Screen.fullScreen);
            } else {
                foreach (Resolution resolution in Screen.resolutions) {
                    if (resolution.height > screenSizeMinimum) {
                        Screen.SetResolution(resolution.width, resolution.height, Screen.fullScreen, resolution.refreshRate);
                        break;
                    }
                }
            }
        }
        if (Screen.height < screenSizeMinimum) {    //Failed to set a new suitable resolution!
            Debug.LogError("Could not find a suitable new resolution!");
            Screen.SetResolution(screenSizeMinimum, screenSizeMinimum, false);
        }
        if (Screen.width > Screen.height * 2) {
            Debug.LogWarning("Screen aspect ratio too large. Maximum width for this height will be enforced.");
            if (Screen.fullScreen = false) {
                Screen.SetResolution(Screen.height * 2, Screen.height, Screen.fullScreen);
            } else {
                if (Screen.resolutions != null) {
                    foreach (Resolution resolution in Screen.resolutions) {
                        if (resolution.height > screenSizeMinimum) {
                            if (resolution.width <= resolution.height * 2) {
                                Screen.SetResolution(resolution.width, resolution.height, Screen.fullScreen, resolution.refreshRate);
                                break;
                            }
                        }
                    }
                } else {
                    Debug.LogError("Could not find a suitable new resolution!");
                    Screen.SetResolution(screenSizeMinimum, screenSizeMinimum, false);
                }
            }
        }
        if (Screen.width > Screen.height * 2) {    //Failed to set a new suitable resolution!
            Debug.LogError("Could not find a suitable new resolution!");
            Screen.SetResolution(screenSizeMinimum, screenSizeMinimum, false);
        }
    }
}

With this occlusion frames are optional, don’t need to extend infinitely and best of all this should support resolutions of 16K and even greater. :slight_smile:

It was ugly and had no check on the camera size being valid, so here is the fixed code because I know someone somewhere will find this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ScreenArea : MonoBehaviour {

   private static GameObject gameManager;
   private static GameManager gameManagerScript;
   private static float screenHeightOld;
   private static int screenSizeMinimum = 768;
  
   void Awake() {
       gameManager = GameObject.FindWithTag("GameManager");
       gameManagerScript = gameManager.GetComponent<GameManager>();
   }
  
   void FixedUpdate() {
       if (screenHeightOld != Screen.height) {
           screenHeightOld = Screen.height;
           EnforceScreenSizeLimits();
           ChangeCameraSize();
       }
   }
  
   private void EnforceScreenSizeLimits() {
       if (Screen.fullScreen) {
           if (Screen.resolutions != null) {
               if ((Screen.resolutions[Screen.resolutions.Length - 1].height < screenSizeMinimum) ||
                   (Screen.resolutions[Screen.resolutions.Length - 1].width > Screen.resolutions[Screen.resolutions.Length - 1].height * 2)) {
                   Debug.LogError("Monitor resolution too short or too wide!");
                   SetScreenToMinimumSize();
               } else {
                   Screen.SetResolution(Screen.resolutions[Screen.resolutions.Length - 1].width, Screen.resolutions[Screen.resolutions.Length - 1].height, true);
               }
           } else {
               Debug.LogError("Could not determine monitor resolution!");
               SetScreenToMinimumSize();
           }
       } else if ((Screen.height < screenSizeMinimum) || (Screen.width > Screen.height * 2)) {
           Debug.LogWarning("Screen too short or too wide!");
           SetScreenToMinimumSize();
       }
   }
  
   private static void SetScreenToMinimumSize() {
       Screen.SetResolution(screenSizeMinimum, screenSizeMinimum, false);
   }
  
   private void ChangeCameraSize() {
       float newSize = Screen.height / (gameManagerScript.pixelsPerUnit * 4);
       float _newSize = Mathf.Ceil(newSize / 24);
       newSize /= _newSize;
       gameManagerScript.mainCameraCamera.orthographicSize = newSize;
   }
}

Enjoy; who ever you are.