117 lines
3.7 KiB
C#
Raw Normal View History

2020-10-27 22:59:19 +00:00
using OWML.Common;
using QSB.Utility;
using UnityEngine;
2020-11-07 18:26:49 +00:00
using UnityEngine.PostProcessing;
2020-10-27 22:59:19 +00:00
namespace QSB.Instruments.QSBCamera
{
2020-12-02 21:23:01 +00:00
public class CameraManager : MonoBehaviour
{
public static CameraManager Instance;
2020-12-14 21:41:56 +01:00
2020-12-02 21:23:01 +00:00
public bool IsSetUp { get; private set; }
public CameraMode Mode { get; private set; }
2020-10-27 22:59:19 +00:00
2020-12-14 21:41:56 +01:00
private GameObject _cameraBase;
private GameObject _cameraObj;
private Camera _camera;
private OWCamera _owCamera;
public void Start()
2020-12-02 21:23:01 +00:00
{
Instance = this;
SetupCamera();
}
2020-10-27 22:59:19 +00:00
2020-12-02 21:23:01 +00:00
private void SetupCamera()
{
2020-12-14 21:41:56 +01:00
_cameraBase = new GameObject();
_cameraBase.SetActive(false);
_cameraBase.AddComponent<Transform>();
_cameraBase.transform.parent = Locator.GetPlayerTransform();
_cameraBase.transform.localPosition = Vector3.zero;
_cameraBase.transform.localRotation = Quaternion.Euler(0, 0, 0);
2020-10-27 22:59:19 +00:00
2020-12-14 21:41:56 +01:00
_cameraObj = new GameObject();
_cameraObj.transform.parent = _cameraBase.transform;
_cameraObj.transform.localPosition = new Vector3(0, 0, -5f);
_cameraObj.transform.localRotation = Quaternion.Euler(0, 0, 0);
_camera = _cameraObj.AddComponent<Camera>();
_camera.cullingMask = Locator.GetPlayerCamera().mainCamera.cullingMask & ~(1 << 27) | (1 << 22);
_camera.clearFlags = CameraClearFlags.Color;
_camera.backgroundColor = Color.black;
_camera.fieldOfView = 90f;
_camera.nearClipPlane = 0.1f;
_camera.farClipPlane = 40000f;
_camera.depth = 0f;
_camera.enabled = false;
_owCamera = _cameraObj.AddComponent<OWCamera>();
_owCamera.renderSkybox = true;
2020-11-01 11:52:31 +00:00
2020-12-14 21:41:56 +01:00
_cameraBase.AddComponent<CameraController>().CameraObject = _cameraObj;
2020-10-27 22:59:19 +00:00
2020-12-14 21:41:56 +01:00
var screenGrab = _cameraObj.AddComponent<FlashbackScreenGrabImageEffect>();
2020-12-02 21:23:01 +00:00
screenGrab._downsampleShader = Locator.GetPlayerCamera().gameObject.GetComponent<FlashbackScreenGrabImageEffect>()._downsampleShader;
2020-10-27 22:59:19 +00:00
2020-12-14 21:41:56 +01:00
var fogImage = _cameraObj.AddComponent<PlanetaryFogImageEffect>();
2020-12-02 21:23:01 +00:00
fogImage.fogShader = Locator.GetPlayerCamera().gameObject.GetComponent<PlanetaryFogImageEffect>().fogShader;
2020-10-27 22:59:19 +00:00
2020-12-14 21:41:56 +01:00
_cameraBase.SetActive(true);
2020-10-27 22:59:19 +00:00
2020-12-02 21:23:01 +00:00
IsSetUp = true;
}
2020-10-27 22:59:19 +00:00
2020-12-02 21:23:01 +00:00
public void SwitchTo3rdPerson()
{
if (!IsSetUp)
{
DebugLog.ToConsole("Warning - Camera not set up!", MessageType.Warning);
OWInput.ChangeInputMode(InputMode.None);
Mode = CameraMode.ThirdPerson;
return;
}
if (Mode == CameraMode.ThirdPerson)
{
DebugLog.ToConsole("Warning - Already in 3rd person!", MessageType.Warning);
return;
}
if (OWInput.GetInputMode() != InputMode.Character)
{
DebugLog.ToConsole("Warning - Cannot change to 3rd person while not in Character inputmode!", MessageType.Warning);
return;
}
OWInput.ChangeInputMode(InputMode.None);
2020-12-14 21:41:56 +01:00
GlobalMessenger<OWCamera>.FireEvent("SwitchActiveCamera", _owCamera);
2020-12-02 21:23:01 +00:00
Locator.GetPlayerCamera().mainCamera.enabled = false;
2020-12-14 21:41:56 +01:00
if (_cameraObj.GetComponent<PostProcessingBehaviour>() == null)
2020-12-02 21:23:01 +00:00
{
2020-12-14 21:41:56 +01:00
var postProcessing = _cameraObj.AddComponent<PostProcessingBehaviour>();
2020-12-02 21:23:01 +00:00
postProcessing.profile = Locator.GetPlayerCamera().gameObject.GetComponent<PostProcessingBehaviour>().profile;
}
2020-12-14 21:41:56 +01:00
_camera.enabled = true;
2020-12-02 21:23:01 +00:00
Mode = CameraMode.ThirdPerson;
}
2020-10-27 22:59:19 +00:00
2020-12-02 21:23:01 +00:00
public void SwitchTo1stPerson()
{
if (!IsSetUp)
{
DebugLog.ToConsole("Warning - Camera not set up!", MessageType.Warning);
OWInput.ChangeInputMode(InputMode.Character);
Mode = CameraMode.FirstPerson;
return;
}
if (Mode == CameraMode.FirstPerson)
{
DebugLog.ToConsole("Warning - Already in 1st person!", MessageType.Warning);
return;
}
OWInput.ChangeInputMode(InputMode.Character);
GlobalMessenger<OWCamera>.FireEvent("SwitchActiveCamera", Locator.GetPlayerCamera());
Locator.GetActiveCamera().mainCamera.enabled = true;
2020-12-14 21:41:56 +01:00
_camera.enabled = false;
2020-12-02 21:23:01 +00:00
Mode = CameraMode.FirstPerson;
}
}
2020-12-03 08:28:05 +00:00
}