Created
May 19, 2014 17:50
-
-
Save tiagosr/43b06604c53ccb327083 to your computer and use it in GitHub Desktop.
A Camera component for Unity which can blend between two positions (for now only the same type of projection will work, no mixing between ortho and non-ortho cameras)
This file contains 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; | |
[RequireComponent(typeof(Camera))] | |
public class MixableCamera : MonoBehaviour { | |
public Camera fromCamera, toCamera; | |
public float mixPosition = 0f; | |
public float mixSpeed = 0f; | |
void OnPreCull() { | |
// OnPreCull is only called for components attached to cameras | |
if((fromCamera != null) && (toCamera != null)) { | |
transform.position = Vector3.Lerp(fromCamera.transform.position, toCamera.transform.position, mixPosition); | |
transform.rotation = Quaternion.Lerp(fromCamera.transform.rotation, toCamera.transform.rotation, mixPosition); | |
//transform.localScale = Vector3.Lerp(fromCamera.transform.localScale, toCamera.transform.localScale, mixPosition); | |
camera.farClipPlane = Mathf.Lerp(fromCamera.farClipPlane, toCamera.farClipPlane, mixPosition); | |
camera.nearClipPlane = Mathf.Lerp(fromCamera.nearClipPlane, toCamera.nearClipPlane, mixPosition); | |
if(toCamera.isOrthoGraphic) { | |
camera.orthographicSize = Mathf.Lerp(fromCamera.orthographicSize, toCamera.orthographicSize, mixPosition); | |
} else { | |
camera.fieldOfView = Mathf.Lerp(fromCamera.fieldOfView, toCamera.fieldOfView, mixPosition); | |
} | |
camera.rect = RectAddons.Lerp(fromCamera.rect, toCamera.rect, mixPosition); | |
//camera.pixelRect = RectAddons.Lerp(fromCamera.pixelRect, toCamera.pixelRect, mixPosition); | |
} else if(fromCamera != null) { | |
transform.position = fromCamera.transform.position; | |
transform.rotation = fromCamera.transform.rotation; | |
camera.CopyFrom(fromCamera); | |
} else if(toCamera != null) { | |
transform.position = toCamera.transform.position; | |
transform.rotation = toCamera.transform.rotation; | |
camera.CopyFrom(toCamera); | |
} | |
mixPosition = Mathf.Clamp(mixPosition + mixSpeed * Time.deltaTime, 0f, 1f); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment