Created
December 18, 2011 14:09
-
-
Save AngryAnt/1493513 to your computer and use it in GitHub Desktop.
An example of a four-split camera setup where one camera can be brought to front. UNTESTED.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
enum CameraIdentity | |
{ | |
None, | |
One, | |
Two, | |
Three, | |
Four | |
} | |
public SurveillanceCameraController : MonoBehaviour | |
{ | |
const float kPadding = 10.0f; | |
public Camera cameraOne, cameraTwo, cameraThree, cameraFour; | |
public float blendSpeed = 1.0f; | |
public CameraIdentity activeCamera = CameraIdentity.None; | |
CameraIdentity targetCamera; | |
Rect GetViewportRect (int x, int y) | |
{ | |
x = Mathf.Clamp (x, 0, 1); | |
y = Mathf.Clamp (y, 0, 1); | |
return new Rect ( | |
kPadding + (Screen.width * 0.5f + kPadding) * x, | |
kPadding + (Screen.height * 0.5f + kPadding) * y, | |
Screen.width * 0.5f - kPadding * 2.0f, | |
Screen.height * 0.5f - kPadding * 2.0f | |
); | |
} | |
Rect GetViewportRect () | |
{ | |
return new Rect ( | |
kPadding, | |
kPadding, | |
Screen.width - 2.0f * kPadding, | |
Screen.height - 2.0f * kPadding | |
); | |
} | |
IEnumerator Start () | |
{ | |
while (Application.isPlaying) | |
{ | |
targetCamera = activeCamera; | |
cameraOne.depth = targetCamera == CameraIdentity.One ? 1 : 0; | |
cameraTwo.depth = targetCamera == CameraIdentity.Two ? 1 : 0; | |
cameraThree.depth = targetCamera == CameraIdentity.Three ? 1 : 0; | |
cameraFour.depth = targetCamera == CameraIdentity.Four ? 1 : 0; | |
while ( | |
Application.isPlaying && !( | |
BlendViewport (cameraOne, targetCamera == CameraIdentity.One ? GetViewportRect () : GetViewportRect (0, 0)) && | |
BlendViewport (cameraTwo, targetCamera == CameraIdentity.Two ? GetViewportRect () : GetViewportRect (1, 0)) && | |
BlendViewport (cameraThree, targetCamera == CameraIdentity.Three ? GetViewportRect () : GetViewportRect (0, 1)) && | |
BlendViewport (cameraFour, targetCamera == CameraIdentity.Four ? GetViewportRect () : GetViewportRect (1, 1)) | |
) | |
) | |
{ | |
yield return null; | |
} | |
} | |
} | |
bool BlendViewport (Camera camera, Rect targetViewport) | |
{ | |
Rect pixelRect = camera.pixelRect; | |
camera.pixelRect = new Rect ( | |
Mathf.Lerp (pixelRect.x, targetViewport.x, blendSpeed * Time.deltaTime), | |
Mathf.Lerp (pixelRect.y, targetViewport.y, blendSpeed * Time.deltaTime), | |
Mathf.Lerp (pixelRect.width, targetViewport.width, blendSpeed * Time.deltaTime), | |
Mathf.Lerp (pixelRect.height, targetViewport.height, blendSpeed * Time.deltaTime) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment