RTG implements a class called RTCameraViewports which can be used to support multiple viewports in your application. The following sections will describe the funtions and properties of this class and how they can be used to implement multiple viewport support in your application.

Note

If you are only going to use a single camera for rendering, you don't need to touch the RTCameraViewports class. The information discussed in the following sections applies only when more than one camera must render the scene.

The Focus Camera

RTG uses the term focus camera to refer to a camera that is currently used to navigate the scene (i.e. the camera that responds to input).

Adding a Camera

Whenever you want to have multiple cameras rendering in different viewports, you need to use the following function call to register the camera with the RTCameraViewports class:

// Register the cameras with the viewport system
RTCameraViewports.Get.AddCamera(topLeftCamera);
RTCameraViewports.Get.AddCamera(topRightCamera);
RTCameraViewports.Get.AddCamera(bottomLeftCamera);
RTCameraViewports.Get.AddCamera(bottomRightCamera);

// Adjust the camera view rects. 
// Note: Normalized voordinates are used!
topLeftCamera.rect = new Rect(0.0f, 0.5f, 0.5f, 0.5f);      // Top left
topRightCamera.rect = new Rect(0.5f, 0.5f, 0.5f, 0.5f);     // Top right
bottomLeftCamera.rect = new Rect(0.0f, 0.0f, 0.5f, 0.5f);   // Bottom left
bottomRightCamera.rect = new Rect(0.5f, 0.0f, 0.5f, 0.5f);  // Bottom right

OR

// Add the camera and specify the normalized view rectangle at the same time
RTCameraViewports.Get.AddCamera(topLeftCamera, new Rect(0.0f, 0.5f, 0.5f, 0.5f));
RTCameraViewports.Get.AddCamera(topRightCamera, new Rect(0.5f, 0.5f, 0.5f, 0.5f));
RTCameraViewports.Get.AddCamera(bottomLeftCamera, new Rect(0.0f, 0.0f, 0.5f, 0.5f));
RTCameraViewports.Get.AddCamera(bottomRightCamera, new Rect(0.5f, 0.0f, 0.5f, 0.5f));

The AddCamera function will register the camera with the viewport system so that it can be marked as the focus camera later.

Removing a Camera

You can remove a camera from the viewport system by callingthe RemoveCamera function:

// Remove the top left camera
RTCameraViewports.Get.RemoveCamera(topLeftCamera);

Changing the Focus Camera

After you called AddCamera for each camera you plan to use, you can switch between different cameras by calling the SetFocusCamera function. The following code uses alpha-numeric keys to switch between different cameras:

if (Input.GetKeyDown(KeyCode.Alpha1)) RTCameraViewports.Get.SetFocusCamera(topLeftCamera);
else if (Input.GetKeyDown(KeyCode.Alpha2)) RTCameraViewports.Get.SetFocusCamera(topRightCamera);
else if (Input.GetKeyDown(KeyCode.Alpha3)) RTCameraViewports.Get.SetFocusCamera(bottomLeftCamera);
else if (Input.GetKeyDown(KeyCode.Alpha4)) RTCameraViewports.Get.SetFocusCamera(bottomRightCamera);

There is another version of this function which accepts a camera index instead of a camera reference:

RTCameraViewports.Get.SetFocusCamera(0);    // Top left camera
RTCameraViewports.Get.SetFocusCamera(1);    // Top right camera
RTCameraViewports.Get.SetFocusCamera(2);    // Bottom left camera
RTCameraViewports.Get.SetFocusCamera(3);    // Bottom right camera

Note

Do not make any assumtions about the indices. Cameras can be removed from the viewport system and in that case, indices may become invalid or they may reference the wrong camera. In the above example, if the top left camera were to be removed (index 0), calling SetFocusCamera(0) would activate the top right camera (new camera with index 0).

Creating Scene Gizmos

You might want to have a scene gizmo associated with each viewport. So you will need to write the following code for each camera/viewport that you intend to support:

RTGizmosEngine.Get.CreateSceneGizmo(topLeftCamera);
RTGizmosEngine.Get.CreateSceneGizmo(topRightCamera);
RTGizmosEngine.Get.CreateSceneGizmo(bottomLeftCamera);
RTGizmosEngine.Get.CreateSceneGizmo(bottomRightCamera);

RTCameraViewports Events

The RTCameraViewports class fires the following events:

  • CameraAdded - when you call one of the AdCamera functions;
  • CameraRemoved when a camera is removed via a call to RemoveCamera;
  • FocusCameraChanged - when you call one of the SetFocusCamera functions;

The event handlers must have the following signature:

private void OnCameraAdded(Camera camera);      
private void OnCameraRemoved(Camera camera);    
private void OnFocusCameraChanged(Camera oldFocusCam, Camera newFocusCam);

The handlers can be registered like so:

RTCameraViewports.Get.CameraAdded += OnCameraAdded;
RTCameraViewports.Get.CameraRemoved += OnCameraRemoved;
RTCameraViewports.Get.FocusCameraChanged += OnFocusCameraChanged;