BowCamera.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. JCUnityLib.TouchChecker touchChecker = new JCUnityLib.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. if (CommonConfig.SpecialVersion1) {
  68. if (UnityEngine.SceneManagement.SceneManager.GetActiveScene().name.Equals("GameChallenge")) {
  69. this.cameraComp.fieldOfView = UserSettings.ins.bowRotateConvert.fieldOfView;
  70. }
  71. }
  72. RecordDefaultCameraFieldOfView();
  73. if (UserSettings.ins.bowCameraFixed && !CommonConfig.isReleaseVersion)
  74. {
  75. bowCameraFixed = new BowCameraFixed(this);
  76. }
  77. }
  78. void Start()
  79. {
  80. touchChecker.onMoved += delegate (Touch t, bool isOnUI)
  81. {
  82. if (banLogic) return;
  83. if (isOnUI) return;
  84. //触摸控制镜头和拉弓射箭
  85. this.localEulerAngles.x = Mathf.Clamp(this.localEulerAngles.x - t.deltaPosition.y * Time.deltaTime * 5, limitRangeRotateX[0], limitRangeRotateX[1]);
  86. this.localEulerAngles.y = Mathf.Clamp(this.localEulerAngles.y + t.deltaPosition.x * Time.deltaTime * 5, limitRangeRotateY[0], limitRangeRotateY[1]);
  87. this.transform.localEulerAngles = this.localEulerAngles;
  88. };
  89. touchChecker.onEnded += delegate (Touch t, bool isOnUI)
  90. {
  91. if (banLogic) return;
  92. if (!isOnUI) armBow.ADS_fire();
  93. };
  94. if (InfraredManager.ConnetDevicesSingle.ins)
  95. InfraredManager.ConnetDevicesSingle.ins.posAction += UpdatePosition;
  96. }
  97. void OnDestroy()
  98. {
  99. if (_ins == this) _ins = null;
  100. if (InfraredManager.ConnetDevicesSingle.ins)
  101. InfraredManager.ConnetDevicesSingle.ins.posAction -= UpdatePosition;
  102. }
  103. void UpdatePosition(Vector3 pos) {
  104. //红外demo传递数据操控
  105. if (InfraredManager.ConnetDevicesSingle.ins && Camera.main) {
  106. // 制作一条射线
  107. Ray ray = Camera.main.ScreenPointToRay(pos); // 将屏幕点转换为射线
  108. //print(Input.mousePosition);
  109. // 人物旋转
  110. transform.LookAt(ray.GetPoint(1000));
  111. }
  112. }
  113. void Update()
  114. {
  115. //更新相机视野
  116. if (cameraComp && !banCameraFieldOfView)
  117. {
  118. cameraComp.fieldOfView = cameraFieldOfView;
  119. }
  120. if (banLogic) return;
  121. //满足以下条件则阻止控制输入
  122. if (GameMgr.ins.gameOver)
  123. {
  124. return;
  125. }
  126. if (GameMgr.debugInEditor)
  127. {
  128. //需要-镜头看向九轴姿态虚拟节点
  129. // 制作一条射线
  130. //Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); // 将屏幕点转换为射线
  131. //print(Input.mousePosition);
  132. //// 人物旋转
  133. //transform.LookAt(ray.GetPoint(1000));
  134. //Quaternion localRotation = Quaternion.FromToRotation(Vector3.up, ray.GetPoint(1000).normalized);
  135. //CameraToLook.ins.point.transform.Rotate(0f, 10, 0f, Space.Self);
  136. //needLookAtPoint = true;
  137. if (InfraredManager.ConnetDevicesSingle.ins)
  138. {
  139. return;
  140. }
  141. else {
  142. //鼠标控制镜头和拉弓射箭
  143. this.localEulerAngles.x = Mathf.Clamp(this.localEulerAngles.x - 2f * Input.GetAxis("Mouse Y"), limitRangeRotateX[0], limitRangeRotateX[1]);
  144. this.localEulerAngles.y = Mathf.Clamp(this.localEulerAngles.y + 2f * Input.GetAxis("Mouse X"), limitRangeRotateY[0], limitRangeRotateY[1]);
  145. this.transform.localEulerAngles = this.localEulerAngles;
  146. if (EventSystem.current.IsPointerOverGameObject()) return;
  147. }
  148. }
  149. else if (isTouchMode)
  150. {
  151. touchChecker.Update();
  152. }
  153. else
  154. {
  155. if (SB_EventSystem.ins && SB_EventSystem.ins.simulateMouseIsAwaked) return;
  156. //需要-镜头看向九轴姿态虚拟节点
  157. needLookAtPoint = true;
  158. }
  159. }
  160. //需要-镜头看向九轴姿态虚拟节点?
  161. bool needLookAtPoint = false;
  162. //镜头旋转转换比值
  163. float[] _bowRotateConvertRate = null;
  164. float bowRotateConvertRate
  165. {
  166. get
  167. {
  168. if (_bowRotateConvertRate == null)
  169. {
  170. _bowRotateConvertRate = new float[] { UserSettings.ins.bowRotateConvert.GetRate() };
  171. }
  172. return _bowRotateConvertRate[0];
  173. }
  174. }
  175. void LateUpdate()
  176. {
  177. if (needLookAtPoint)
  178. {
  179. needLookAtPoint = false;
  180. //镜头看向九轴姿态虚拟节点
  181. this.transform.LookAt(CameraToLook.ins.point);
  182. // if (BowQuatDebug.ins) BowQuatDebug.ins.ShowRealBowQuat(this.transform.localEulerAngles);
  183. if (!CommonConfig.isReleaseVersion)
  184. {
  185. //镜头旋转比值转换
  186. Vector3 localAngles = this.transform.localEulerAngles;
  187. localAngles.x = Mathf.Clamp((localAngles.x > 180 ? localAngles.x - 360 : localAngles.x) * bowRotateConvertRate,
  188. limitRangeRotateX[0], limitRangeRotateX[1]);
  189. localAngles.y = Mathf.Clamp((localAngles.y > 180 ? localAngles.y - 360 : localAngles.y) * bowRotateConvertRate,
  190. limitRangeRotateY[0], limitRangeRotateY[1]);
  191. if (bowCameraFixed != null)
  192. {
  193. localAngles = bowCameraFixed.LimitBowAngle(localAngles);
  194. }
  195. this.transform.localEulerAngles = localAngles;
  196. // if (BowQuatDebug.ins) BowQuatDebug.ins.ShowGameBowQuat(this.transform.localEulerAngles);
  197. }
  198. }
  199. onAfterLateUpdate?.Invoke();
  200. }
  201. [NonSerialized] public Action onAfterLateUpdate;
  202. //---------------相机视野的相关操作---------------------
  203. //视野值记录
  204. float cameraFieldOfView = 60;
  205. [NonSerialized] public float defaultCameraFieldOfView = 60;
  206. private void RecordDefaultCameraFieldOfView()
  207. {
  208. defaultCameraFieldOfView = cameraComp.fieldOfView;
  209. cameraFieldOfView = defaultCameraFieldOfView;
  210. }
  211. //禁止相机视野改变
  212. [NonSerialized] public bool banCameraFieldOfView = false;
  213. public void SetCameraFieldOfView(float value)
  214. {
  215. cameraComp.fieldOfView = value;
  216. }
  217. public void SetCameraFieldOfViewRecord(float value)
  218. {
  219. cameraFieldOfView = value;
  220. }
  221. //拉弓时的相机视野值变化
  222. public void updateFollowPullBow()
  223. {
  224. // if (cameraFieldOfView > 40) {
  225. // cameraFieldOfView -= 20 * Time.deltaTime;
  226. // } else {
  227. // cameraFieldOfView = 40;
  228. // }
  229. }
  230. //松开拉弓时的相机视野值变化
  231. public void updateGiveUpPullBow()
  232. {
  233. // if (cameraFieldOfView < 60) {
  234. // cameraFieldOfView += 20 * Time.deltaTime;
  235. // } else {
  236. // cameraFieldOfView = 60;
  237. // }
  238. }
  239. // 2022-04-28
  240. // ------ 添加固定镜头选项后,新增的API ------
  241. bool isArrowFollowing = false;
  242. public void SetArrowFollowing(bool value)
  243. {
  244. isArrowFollowing = value;
  245. cameraComp.enabled = !isArrowFollowing;
  246. AutoSwitchCamera();
  247. }
  248. bool isScaleAimDisplaying = false;
  249. public void SetScaleAimDisplaying(bool value)
  250. {
  251. isScaleAimDisplaying = value;
  252. AutoSwitchCamera();
  253. }
  254. void AutoSwitchCamera()
  255. {
  256. if (bowCameraFixed == null)
  257. {
  258. cameraComp.enabled = !isArrowFollowing;
  259. }
  260. else
  261. {
  262. bowCameraFixed.gameObject.SetActive(!isScaleAimDisplaying && !isArrowFollowing);
  263. cameraComp.enabled = isScaleAimDisplaying && !isArrowFollowing;
  264. }
  265. }
  266. public Camera GetRenderCamera()
  267. {
  268. if (bowCameraFixed == null)
  269. {
  270. return cameraComp;
  271. }
  272. else
  273. {
  274. if (bowCameraFixed.gameObject.activeSelf)
  275. {
  276. return bowCameraFixed.camera;
  277. }
  278. else
  279. {
  280. return cameraComp;
  281. }
  282. }
  283. }
  284. public BowCameraFixed bowCameraFixed = null;
  285. public class BowCameraFixed
  286. {
  287. public GameObject gameObject;
  288. public Transform transform;
  289. public Camera camera;
  290. private UnityStandardAssets.ImageEffects.Blur blur;
  291. //bowCameraComponent
  292. BowCamera bowCamera;
  293. private Camera targetCamera;
  294. private UnityStandardAssets.ImageEffects.Blur targetBlur;
  295. public BowCameraFixed(BowCamera bowCamera)
  296. {
  297. this.bowCamera = bowCamera;
  298. gameObject = new GameObject("BowCameraFixed");
  299. transform = gameObject.transform;
  300. //复制Camera组件
  301. targetCamera = bowCamera.cameraComp;
  302. camera = gameObject.AddComponent<Camera>();
  303. camera.tag = "MainCamera";
  304. camera.clearFlags = targetCamera.clearFlags;
  305. camera.backgroundColor = targetCamera.backgroundColor;
  306. camera.cullingMask = targetCamera.cullingMask;
  307. camera.fieldOfView = targetCamera.fieldOfView;
  308. camera.nearClipPlane = targetCamera.nearClipPlane;
  309. camera.depth = targetCamera.depth + 0.1f;
  310. camera.renderingPath = targetCamera.renderingPath;
  311. //复制Blur组件
  312. targetBlur = bowCamera.GetComponent<UnityStandardAssets.ImageEffects.Blur>();
  313. if (targetBlur)
  314. {
  315. blur = gameObject.AddComponent<UnityStandardAssets.ImageEffects.Blur>();
  316. blur.enabled = targetBlur.enabled;
  317. blur.blurShader = targetBlur.blurShader;
  318. blur.blurSpread = targetBlur.blurSpread;
  319. blur.iterations = targetBlur.iterations;
  320. }
  321. //设置Transform属性
  322. transform.parent = bowCamera.transform.parent;
  323. transform.localPosition = bowCamera.transform.localPosition;
  324. transform.localScale = bowCamera.transform.localScale;
  325. transform.localRotation = Quaternion.identity;
  326. //监听和驱动LateUpdate
  327. bowCamera.onAfterLateUpdate += LateUpdate;
  328. //切换镜头
  329. bowCamera.AutoSwitchCamera();
  330. InitForLimitBound();
  331. }
  332. void LateUpdate()
  333. {
  334. if (gameObject.activeSelf)
  335. {
  336. CrossHair.ins.UpdatePostionWhenFixedCamera();
  337. }
  338. if (blur)
  339. {
  340. blur.enabled = targetBlur.enabled;
  341. if (blur.enabled)
  342. {
  343. blur.blurShader = targetBlur.blurShader;
  344. blur.blurSpread = targetBlur.blurSpread;
  345. blur.iterations = targetBlur.iterations;
  346. }
  347. }
  348. }
  349. //边界限制
  350. float[] rangeRotateY = { -80, 80 };
  351. float rangeRotateX = 25;
  352. Vector3 vecF;
  353. Vector3 vecU;
  354. public int outBoundIndex = -1; //-1为未出界
  355. void InitForLimitBound()
  356. {
  357. for (int i = (int)rangeRotateY[0]; i < 0; i++)
  358. {
  359. Vector3 pos = transform.position + Quaternion.AngleAxis(i, Vector3.up) * transform.forward;
  360. pos = camera.WorldToViewportPoint(pos);
  361. if (pos.x >= 0)
  362. {
  363. rangeRotateY[0] = i;
  364. rangeRotateY[1] = -i - 4;
  365. break;
  366. }
  367. }
  368. rangeRotateX = camera.fieldOfView / 2;
  369. vecF = Quaternion.AngleAxis(rangeRotateX, Vector3.right) * Vector3.forward;
  370. vecU = Quaternion.AngleAxis(rangeRotateX, Vector3.right) * Vector3.up;
  371. }
  372. public Vector3 LimitBowAngle(Vector3 outAngle)
  373. {
  374. float angleY = outAngle.y;
  375. float angleX = outAngle.x;
  376. outAngle.y = Mathf.Clamp(angleY, rangeRotateY[0], rangeRotateY[1]);
  377. Vector3 vec = Quaternion.AngleAxis(outAngle.y, vecU) * vecF;
  378. float rx = (float)(Math.Asin(vec.y) / Math.PI * 180) * (angleX < 0 ? 1 : -1);
  379. if (angleY < rangeRotateY[0]) outBoundIndex = 0;
  380. else if (angleY > rangeRotateY[1]) outBoundIndex = 1;
  381. else if (angleX < -rangeRotateX) outBoundIndex = 2;
  382. else if (angleX > rangeRotateX) outBoundIndex = 3;
  383. else outBoundIndex = -1;
  384. if (Mathf.Abs(angleX) > Mathf.Abs(rx))
  385. {
  386. outAngle.x = rx;
  387. }
  388. //因为Vector3是struct,传递给函数是值传递而非引用传递
  389. return outAngle;
  390. }
  391. }
  392. }