CameraManager.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using System;
  2. using UnityEngine;
  3. namespace COM3D2.MeidoPhotoStudio.Plugin
  4. {
  5. using Input = InputManager;
  6. using UInput = Input;
  7. public class CameraManager : IManager
  8. {
  9. public const string header = "CAMERA";
  10. private static readonly CameraMain mainCamera = CameraUtility.MainCamera;
  11. private static readonly UltimateOrbitCamera ultimateOrbitCamera = CameraUtility.UOCamera;
  12. private float defaultCameraMoveSpeed;
  13. private float defaultCameraZoomSpeed;
  14. private const float cameraFastMoveSpeed = 0.1f;
  15. private const float cameraFastZoomSpeed = 3f;
  16. private CameraInfo tempCameraInfo = new CameraInfo();
  17. private const KeyCode AlphaOne = KeyCode.Alpha1;
  18. public int CameraCount => cameraInfos.Length;
  19. public EventHandler CameraChange;
  20. private int currentCameraIndex;
  21. public int CurrentCameraIndex
  22. {
  23. get => currentCameraIndex;
  24. set
  25. {
  26. cameraInfos[currentCameraIndex].UpdateInfo(mainCamera);
  27. currentCameraIndex = value;
  28. LoadCameraInfo(cameraInfos[currentCameraIndex]);
  29. }
  30. }
  31. private CameraInfo[] cameraInfos;
  32. static CameraManager()
  33. {
  34. Input.Register(MpsKey.CameraLayer, KeyCode.Q, "Camera control layer");
  35. Input.Register(MpsKey.CameraSave, KeyCode.S, "Save camera transform");
  36. Input.Register(MpsKey.CameraLoad, KeyCode.A, "Load camera transform");
  37. Input.Register(MpsKey.CameraReset, KeyCode.R, "Reset camera transform");
  38. }
  39. public CameraManager()
  40. {
  41. cameraInfos = new CameraInfo[5];
  42. for (var i = 0; i < cameraInfos.Length; i++) cameraInfos[i] = new CameraInfo();
  43. Activate();
  44. }
  45. public void Activate()
  46. {
  47. ultimateOrbitCamera.enabled = true;
  48. defaultCameraMoveSpeed = ultimateOrbitCamera.moveSpeed;
  49. defaultCameraZoomSpeed = ultimateOrbitCamera.zoomSpeed;
  50. if (!MeidoPhotoStudio.EditMode) ResetCamera();
  51. currentCameraIndex = 0;
  52. tempCameraInfo.Reset();
  53. for (var i = 0; i < CameraCount; i++) cameraInfos[i].Reset();
  54. mainCamera.ForceCalcNearClip();
  55. }
  56. public void Deactivate()
  57. {
  58. mainCamera.camera.backgroundColor = Color.black;
  59. ultimateOrbitCamera.moveSpeed = defaultCameraMoveSpeed;
  60. ultimateOrbitCamera.zoomSpeed = defaultCameraZoomSpeed;
  61. if (MeidoPhotoStudio.EditMode) return;
  62. mainCamera.Reset(CameraMain.CameraType.Target, true);
  63. mainCamera.SetTargetPos(new Vector3(0.5609447f, 1.380762f, -1.382336f));
  64. mainCamera.SetDistance(1.6f);
  65. mainCamera.SetAroundAngle(new Vector2(245.5691f, 6.273283f));
  66. mainCamera.ResetCalcNearClip();
  67. }
  68. public void Update()
  69. {
  70. if (Input.GetKey(MpsKey.CameraLayer))
  71. {
  72. if (Input.GetKeyDown(MpsKey.CameraSave)) SaveTempCamera();
  73. else if (Input.GetKeyDown(MpsKey.CameraLoad)) LoadCameraInfo(tempCameraInfo);
  74. else if (Input.GetKeyDown(MpsKey.CameraReset)) ResetCamera();
  75. for (var i = 0; i < CameraCount; i++)
  76. {
  77. if (i != CurrentCameraIndex && UInput.GetKeyDown(AlphaOne + i)) CurrentCameraIndex = i;
  78. }
  79. }
  80. var shift = Input.Shift;
  81. ultimateOrbitCamera.moveSpeed = shift ? cameraFastMoveSpeed : defaultCameraMoveSpeed;
  82. ultimateOrbitCamera.zoomSpeed = shift ? cameraFastZoomSpeed : defaultCameraZoomSpeed;
  83. }
  84. private void SaveTempCamera()
  85. {
  86. tempCameraInfo.UpdateInfo(mainCamera);
  87. CameraUtility.StopAll();
  88. }
  89. public void LoadCameraInfo(CameraInfo info)
  90. {
  91. info.Apply(mainCamera);
  92. CameraUtility.StopAll();
  93. CameraChange?.Invoke(this, EventArgs.Empty);
  94. }
  95. private void ResetCamera()
  96. {
  97. mainCamera.Reset(CameraMain.CameraType.Target, true);
  98. mainCamera.SetTargetPos(new Vector3(0f, 0.9f, 0f));
  99. mainCamera.SetDistance(3f);
  100. CameraChange?.Invoke(this, EventArgs.Empty);
  101. }
  102. }
  103. public class CameraInfo
  104. {
  105. public Vector3 TargetPos { get; set; }
  106. public Quaternion Angle { get; set; }
  107. public float Distance { get; set; }
  108. public float FOV { get; set; }
  109. public CameraInfo() => Reset();
  110. public static CameraInfo FromCamera(CameraMain mainCamera)
  111. {
  112. var info = new CameraInfo();
  113. info.UpdateInfo(mainCamera);
  114. return info;
  115. }
  116. public void Reset()
  117. {
  118. TargetPos = new Vector3(0f, 0.9f, 0f);
  119. Angle = Quaternion.Euler(10f, 180f, 0f);
  120. Distance = 3f;
  121. FOV = 35f;
  122. }
  123. public void UpdateInfo(CameraMain camera)
  124. {
  125. TargetPos = camera.GetTargetPos();
  126. Angle = camera.transform.rotation;
  127. Distance = camera.GetDistance();
  128. FOV = camera.camera.fieldOfView;
  129. }
  130. public void Apply(CameraMain camera)
  131. {
  132. camera.SetTargetPos(TargetPos);
  133. camera.SetDistance(Distance);
  134. camera.transform.rotation = Angle;
  135. camera.camera.fieldOfView = FOV;
  136. }
  137. }
  138. }