BowCamera.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.EventSystems;
  4. /* 弓的相机 */
  5. public class BowCamera : MonoBehaviour
  6. {
  7. //相机组件
  8. Camera _cameraComp;
  9. Camera cameraComp
  10. {
  11. get
  12. {
  13. if (!_cameraComp) _cameraComp = GetComponent<Camera>();
  14. return _cameraComp;
  15. }
  16. }
  17. //控制的手臂弓
  18. ArmBow _armBow;
  19. ArmBow armBow
  20. {
  21. get
  22. {
  23. if (!_armBow) _armBow = GetComponentInChildren<ArmBow>();
  24. return _armBow;
  25. }
  26. }
  27. //本地欧拉角记录值
  28. Vector3 localEulerAngles;
  29. //触摸检测器
  30. JC.Unity.TouchChecker touchChecker = new JC.Unity.TouchChecker();
  31. //触摸模式开关
  32. private static bool _isTouchMode = true;
  33. public static bool isTouchMode
  34. {
  35. get
  36. {
  37. if (CommonConfig.isReleaseVersion) return false;
  38. return _isTouchMode;
  39. }
  40. set
  41. {
  42. _isTouchMode = value;
  43. }
  44. }
  45. //左右转动范围限制
  46. float[] limitRangeRotateY = { -80, 80 };
  47. //上下转动范围限制
  48. float[] limitRangeRotateX = { -80, 80 };
  49. //禁止逻辑,只用于同步状态和渲染,目前用于联机
  50. [NonSerialized] public bool banLogic = false;
  51. private static BowCamera _ins;
  52. public static BowCamera ins
  53. {
  54. get
  55. {
  56. if (!_ins)
  57. {
  58. _ins = GameObject.FindObjectOfType<BowCamera>();
  59. }
  60. return _ins;
  61. }
  62. }
  63. void Awake()
  64. {
  65. _ins = this;
  66. localEulerAngles = transform.localEulerAngles;
  67. RecordDefaultCameraFieldOfView();
  68. if (UserSettings.ins.bowCameraFixed && !CommonConfig.isReleaseVersion)
  69. {
  70. bowCameraFixed = new BowCameraFixed(this);
  71. }
  72. }
  73. void Start()
  74. {
  75. touchChecker.onMoved += delegate (Touch t, bool isOnUI)
  76. {
  77. if (banLogic) return;
  78. if (isOnUI) return;
  79. //触摸控制镜头和拉弓射箭
  80. this.localEulerAngles.x = Mathf.Clamp(this.localEulerAngles.x - t.deltaPosition.y * Time.deltaTime * 5, limitRangeRotateX[0], limitRangeRotateX[1]);
  81. this.localEulerAngles.y = Mathf.Clamp(this.localEulerAngles.y + t.deltaPosition.x * Time.deltaTime * 5, limitRangeRotateY[0], limitRangeRotateY[1]);
  82. this.transform.localEulerAngles = this.localEulerAngles;
  83. };
  84. touchChecker.onEnded += delegate (Touch t, bool isOnUI)
  85. {
  86. if (banLogic) return;
  87. if (!isOnUI) armBow.ADS_fire();
  88. };
  89. }
  90. void Update()
  91. {
  92. //更新相机视野
  93. if (cameraComp && !banCameraFieldOfView)
  94. {
  95. cameraComp.fieldOfView = cameraFieldOfView;
  96. }
  97. if (banLogic) return;
  98. //满足以下条件则阻止控制输入
  99. if (GameMgr.ins.gameOver)
  100. {
  101. return;
  102. }
  103. if (GameMgr.debugInEditor)
  104. {
  105. //鼠标控制镜头和拉弓射箭
  106. this.localEulerAngles.x = Mathf.Clamp(this.localEulerAngles.x - 2f * Input.GetAxis("Mouse Y"), limitRangeRotateX[0], limitRangeRotateX[1]);
  107. this.localEulerAngles.y = Mathf.Clamp(this.localEulerAngles.y + 2f * Input.GetAxis("Mouse X"), limitRangeRotateY[0], limitRangeRotateY[1]);
  108. this.transform.localEulerAngles = this.localEulerAngles;
  109. if (EventSystem.current.IsPointerOverGameObject()) return;
  110. }
  111. else if (isTouchMode)
  112. {
  113. touchChecker.Update();
  114. }
  115. else
  116. {
  117. if (SB_EventSystem.ins && SB_EventSystem.ins.simulateMouseIsAwaked) return;
  118. //需要-镜头看向九轴姿态虚拟节点
  119. needLookAtPoint = true;
  120. }
  121. }
  122. //需要-镜头看向九轴姿态虚拟节点?
  123. bool needLookAtPoint = false;
  124. //镜头旋转转换比值
  125. float[] _bowRotateConvertRate = null;
  126. float bowRotateConvertRate
  127. {
  128. get
  129. {
  130. if (_bowRotateConvertRate == null)
  131. {
  132. _bowRotateConvertRate = new float[] { UserSettings.ins.bowRotateConvert.GetRate() };
  133. }
  134. return _bowRotateConvertRate[0];
  135. }
  136. }
  137. void LateUpdate()
  138. {
  139. #if UNITY_STANDALONE_WIN
  140. if (BleUDP.ins && BleUDP.ins.bluetoothStatusEnum == BluetoothStatusEnum.ConnectSuccess)
  141. {
  142. needLookAtPoint = true;
  143. }
  144. #endif
  145. if (needLookAtPoint)
  146. {
  147. needLookAtPoint = false;
  148. //镜头看向九轴姿态虚拟节点
  149. this.transform.LookAt(CameraToLook.ins.point);
  150. // if (BowQuatDebug.ins) BowQuatDebug.ins.ShowRealBowQuat(this.transform.localEulerAngles);
  151. if (!CommonConfig.isReleaseVersion)
  152. {
  153. //镜头旋转比值转换
  154. Vector3 localAngles = this.transform.localEulerAngles;
  155. localAngles.x = Mathf.Clamp((localAngles.x > 180 ? localAngles.x - 360 : localAngles.x) * bowRotateConvertRate,
  156. limitRangeRotateX[0], limitRangeRotateX[1]);
  157. localAngles.y = Mathf.Clamp((localAngles.y > 180 ? localAngles.y - 360 : localAngles.y) * bowRotateConvertRate,
  158. limitRangeRotateY[0], limitRangeRotateY[1]);
  159. if (bowCameraFixed != null)
  160. {
  161. localAngles = bowCameraFixed.LimitBowAngle(localAngles);
  162. }
  163. this.transform.localEulerAngles = localAngles;
  164. // if (BowQuatDebug.ins) BowQuatDebug.ins.ShowGameBowQuat(this.transform.localEulerAngles);
  165. }
  166. }
  167. onAfterLateUpdate?.Invoke();
  168. }
  169. Action onAfterLateUpdate;
  170. //---------------相机视野的相关操作---------------------
  171. //视野值记录
  172. float cameraFieldOfView = 60;
  173. [NonSerialized] public float defaultCameraFieldOfView = 60;
  174. private void RecordDefaultCameraFieldOfView()
  175. {
  176. defaultCameraFieldOfView = cameraComp.fieldOfView;
  177. cameraFieldOfView = defaultCameraFieldOfView;
  178. }
  179. //禁止相机视野改变
  180. [NonSerialized] public bool banCameraFieldOfView = false;
  181. public void SetCameraFieldOfView(float value)
  182. {
  183. cameraComp.fieldOfView = value;
  184. }
  185. public void SetCameraFieldOfViewRecord(float value)
  186. {
  187. cameraFieldOfView = value;
  188. }
  189. //拉弓时的相机视野值变化
  190. public void updateFollowPullBow()
  191. {
  192. // if (cameraFieldOfView > 40) {
  193. // cameraFieldOfView -= 20 * Time.deltaTime;
  194. // } else {
  195. // cameraFieldOfView = 40;
  196. // }
  197. }
  198. //松开拉弓时的相机视野值变化
  199. public void updateGiveUpPullBow()
  200. {
  201. // if (cameraFieldOfView < 60) {
  202. // cameraFieldOfView += 20 * Time.deltaTime;
  203. // } else {
  204. // cameraFieldOfView = 60;
  205. // }
  206. }
  207. // 2022-04-28
  208. // ------ 添加固定镜头选项后,新增的API ------
  209. bool isArrowFollowing = false;
  210. public void SetArrowFollowing(bool value)
  211. {
  212. isArrowFollowing = value;
  213. cameraComp.enabled = !isArrowFollowing;
  214. AutoSwitchCamera();
  215. }
  216. bool isScaleAimDisplaying = false;
  217. public void SetScaleAimDisplaying(bool value)
  218. {
  219. isScaleAimDisplaying = value;
  220. AutoSwitchCamera();
  221. }
  222. void AutoSwitchCamera()
  223. {
  224. if (bowCameraFixed == null)
  225. {
  226. cameraComp.enabled = !isArrowFollowing;
  227. }
  228. else
  229. {
  230. bowCameraFixed.gameObject.SetActive(!isScaleAimDisplaying && !isArrowFollowing);
  231. cameraComp.enabled = isScaleAimDisplaying && !isArrowFollowing;
  232. }
  233. }
  234. public Camera GetRenderCamera()
  235. {
  236. if (bowCameraFixed == null)
  237. {
  238. return cameraComp;
  239. }
  240. else
  241. {
  242. if (bowCameraFixed.gameObject.activeSelf)
  243. {
  244. return bowCameraFixed.camera;
  245. }
  246. else
  247. {
  248. return cameraComp;
  249. }
  250. }
  251. }
  252. public BowCameraFixed bowCameraFixed = null;
  253. public class BowCameraFixed
  254. {
  255. public GameObject gameObject;
  256. public Transform transform;
  257. public Camera camera;
  258. private UnityStandardAssets.ImageEffects.Blur blur;
  259. //bowCameraComponent
  260. BowCamera bowCamera;
  261. private Camera targetCamera;
  262. private UnityStandardAssets.ImageEffects.Blur targetBlur;
  263. public BowCameraFixed(BowCamera bowCamera)
  264. {
  265. this.bowCamera = bowCamera;
  266. gameObject = new GameObject("BowCameraFixed");
  267. transform = gameObject.transform;
  268. //复制Camera组件
  269. targetCamera = bowCamera.cameraComp;
  270. camera = gameObject.AddComponent<Camera>();
  271. camera.tag = "MainCamera";
  272. camera.clearFlags = targetCamera.clearFlags;
  273. camera.backgroundColor = targetCamera.backgroundColor;
  274. camera.cullingMask = targetCamera.cullingMask;
  275. camera.fieldOfView = targetCamera.fieldOfView;
  276. camera.nearClipPlane = targetCamera.nearClipPlane;
  277. camera.depth = targetCamera.depth + 0.1f;
  278. camera.renderingPath = targetCamera.renderingPath;
  279. //复制Blur组件
  280. targetBlur = bowCamera.GetComponent<UnityStandardAssets.ImageEffects.Blur>();
  281. if (targetBlur)
  282. {
  283. blur = gameObject.AddComponent<UnityStandardAssets.ImageEffects.Blur>();
  284. blur.enabled = targetBlur.enabled;
  285. blur.blurShader = targetBlur.blurShader;
  286. blur.blurSpread = targetBlur.blurSpread;
  287. blur.iterations = targetBlur.iterations;
  288. }
  289. //设置Transform属性
  290. transform.parent = bowCamera.transform.parent;
  291. transform.localPosition = bowCamera.transform.localPosition;
  292. transform.localScale = bowCamera.transform.localScale;
  293. transform.localRotation = Quaternion.identity;
  294. //监听和驱动LateUpdate
  295. bowCamera.onAfterLateUpdate += LateUpdate;
  296. //切换镜头
  297. bowCamera.AutoSwitchCamera();
  298. InitForLimitBound();
  299. }
  300. void LateUpdate()
  301. {
  302. if (gameObject.activeSelf)
  303. {
  304. CrossHair.ins.UpdatePostionWhenFixedCamera();
  305. }
  306. if (blur)
  307. {
  308. blur.enabled = targetBlur.enabled;
  309. if (blur.enabled)
  310. {
  311. blur.blurShader = targetBlur.blurShader;
  312. blur.blurSpread = targetBlur.blurSpread;
  313. blur.iterations = targetBlur.iterations;
  314. }
  315. }
  316. }
  317. //边界限制
  318. float[] rangeRotateY = { -80, 80 };
  319. float rangeRotateX = 25;
  320. Vector3 vecF;
  321. Vector3 vecU;
  322. public int outBoundIndex = -1; //-1为未出界
  323. void InitForLimitBound()
  324. {
  325. for (int i = (int)rangeRotateY[0]; i < 0; i++)
  326. {
  327. Vector3 pos = transform.position + Quaternion.AngleAxis(i, Vector3.up) * transform.forward;
  328. pos = camera.WorldToViewportPoint(pos);
  329. if (pos.x >= 0)
  330. {
  331. rangeRotateY[0] = i;
  332. rangeRotateY[1] = -i - 4;
  333. break;
  334. }
  335. }
  336. rangeRotateX = camera.fieldOfView / 2;
  337. vecF = Quaternion.AngleAxis(rangeRotateX, Vector3.right) * Vector3.forward;
  338. vecU = Quaternion.AngleAxis(rangeRotateX, Vector3.right) * Vector3.up;
  339. }
  340. public Vector3 LimitBowAngle(Vector3 outAngle)
  341. {
  342. float angleY = outAngle.y;
  343. float angleX = outAngle.x;
  344. outAngle.y = Mathf.Clamp(angleY, rangeRotateY[0], rangeRotateY[1]);
  345. Vector3 vec = Quaternion.AngleAxis(outAngle.y, vecU) * vecF;
  346. float rx = (float)(Math.Asin(vec.y) / Math.PI * 180) * (angleX < 0 ? 1 : -1);
  347. if (angleY < rangeRotateY[0]) outBoundIndex = 0;
  348. else if (angleY > rangeRotateY[1]) outBoundIndex = 1;
  349. else if (angleX < -rangeRotateX) outBoundIndex = 2;
  350. else if (angleX > rangeRotateX) outBoundIndex = 3;
  351. else outBoundIndex = -1;
  352. if (Mathf.Abs(angleX) > Mathf.Abs(rx))
  353. {
  354. outAngle.x = rx;
  355. }
  356. //因为Vector3是struct,传递给函数是值传递而非引用传递
  357. return outAngle;
  358. }
  359. }
  360. }