CameraManager.cs 5.9 KB

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