InfraredDemo.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using InfraredManager;
  6. using ZIM;
  7. using System.Linq;
  8. using Serenegiant.UVC;
  9. using System;
  10. public class InfraredDemo : MonoBehaviour
  11. {
  12. public static bool DebugInEditor = true;
  13. public static InfraredDemo _ins;
  14. [SerializeField]
  15. RectTransform canvasRectTransform;
  16. public static void Create()
  17. {
  18. if (_ins) return;
  19. GameObject o = Instantiate(Resources.Load<GameObject>("InfraredDemo"));
  20. DontDestroyOnLoad(o);
  21. _ins = o.GetComponent<InfraredDemo>();
  22. //直接初始化一次
  23. _ins.InitInfraredCamera();
  24. }
  25. void Start()
  26. {
  27. if (Application.platform != RuntimePlatform.WindowsEditor) DebugInEditor = false;
  28. SetVisiable(false);
  29. InitDebugScreenPoint();
  30. gameObject.AddComponent<FPSTester>();
  31. //直接初始化一次
  32. InitInfraredCamera();
  33. //如果本地有记录,初始化一次
  34. initScreenLocateManual();
  35. }
  36. void Update()
  37. {
  38. UpdateInfraredCamera();
  39. UpdateDebugScreenPoint();
  40. }
  41. public void OnClick_See()
  42. {
  43. SetVisiable(!_visiable);
  44. }
  45. bool _visiable;
  46. void SetVisiable(bool value)
  47. {
  48. _visiable = value;
  49. transform.Find("Background").gameObject.SetActive(value);
  50. transform.Find("InfraredCamera").gameObject.SetActive(value);
  51. transform.Find("BtnSee").GetComponentInChildren<Text>().text = value ? "隐藏界面" : "调试红外";
  52. //if (value) InitInfraredCamera();
  53. }
  54. Text _spText;
  55. Vector2 _screenPoint;
  56. void InitDebugScreenPoint()
  57. {
  58. _spText = transform.Find("SPText").GetComponent<Text>();
  59. //_spText.gameObject.SetActive(DebugInEditor);
  60. _spText.gameObject.SetActive(false);
  61. }
  62. void UpdateDebugScreenPoint()
  63. {
  64. if (!DebugInEditor) return;
  65. if (infraredCameraHelper == null) return;
  66. _screenPoint.x = Mathf.Clamp(
  67. _screenPoint.x + Input.GetAxis("Horizontal") * Screen.width / 400f,
  68. 0, Screen.width);
  69. _screenPoint.y = Mathf.Clamp(
  70. _screenPoint.y + Input.GetAxis("Vertical") * Screen.height / 400f,
  71. 0, Screen.height);
  72. infraredCameraHelper.InvokeOnPositionUpdate(_screenPoint);
  73. _spText.text = _screenPoint.ToString();
  74. }
  75. #region 红外摄像
  76. [SerializeField] RawImage _cameraRender;
  77. [SerializeField] MaintainAspectRatio _MaintainAspectRatio;
  78. [SerializeField] List<RectTransform> _crosshairsInCamera;
  79. //[SerializeField] Slider _sliderBrightness;
  80. //[SerializeField] Slider _sliderSaturation;
  81. //[SerializeField] Slider _sliderContrast;
  82. [SerializeField] Slider _sliderShakeFilter;
  83. [SerializeField] Button _btnReset;
  84. [SerializeField] Button _btnScreenLocateManual;
  85. [SerializeField] Button _btnScreenLocateManualAuto;
  86. [SerializeField] Slider _sliderCapture;
  87. [SerializeField] Slider _sliderDelay;
  88. [SerializeField] Dropdown _dropdownResolution;
  89. [SerializeField] Dropdown _dropdownResolution2;
  90. [SerializeField] Slider _sliderLineWidth;
  91. //红外线阈值
  92. [SerializeField] Slider _infraredFilter;
  93. //调试UVC参数
  94. [SerializeField] GameObject _cameraParameterPanel;
  95. //Dictionary<string, ParamFloatValue> dUVCParameters = new Dictionary<string, ParamFloatValue>();
  96. //public ParamFloatValue brightness = new ParamFloatValue("ic_brightness", 1.0f);
  97. //public ParamFloatValue saturation = new ParamFloatValue("ic_saturation", 1.0f);
  98. //public ParamFloatValue contrast = new ParamFloatValue("ic_contrast", 1.0f);
  99. public ParamFloatValue shakeFilterValue = new ParamFloatValue("ic_shakeFilterValue2", 6.0f);
  100. public ParamFloatValue resoution = new ParamFloatValue("ic_resoution2", 2);
  101. public ParamFloatValue resoutionNew = new ParamFloatValue("ic_resoutionNew", 0);
  102. //线段宽度阈值
  103. public ParamFloatValue lineWidth = new ParamFloatValue("ic_lineWidth", 20.0f);
  104. public ParamFloatValue captureValue = new ParamFloatValue("ic_captureValue", 30.0f);
  105. public ParamFloatValue delayValue = new ParamFloatValue("ic_delayValue", 30.0f);
  106. //亮度过滤阈值
  107. public ParamFloatValue infraredFileterValue = new ParamFloatValue("ic_infraredFileterValue", 0.8f);
  108. //十字准心
  109. public ParamFloatValue crosshairValue = new ParamFloatValue("ic_crosshairValue",0);
  110. public static InfraredCameraHelper infraredCameraHelper;
  111. public static bool running { get => infraredCameraHelper != null; }
  112. private bool _inited;
  113. #region 参数控制
  114. public GameObject togglePrefab; // 拖入一个Toggle预设体
  115. public GameObject sliderPrefab; // 拖入一个Slider预设体
  116. public GameObject spawnPoint;
  117. //自动曝光
  118. bool bAutoAE = false;
  119. Toggle CTRLAEToggle;
  120. Slider CTRLAEABSSlider;
  121. string[] sliderNameArray = new string[]{
  122. "自动曝光模式",
  123. "曝光时间(绝对)",
  124. "亮度", //
  125. "对比度",
  126. "色调",
  127. "饱和度",
  128. "锐度",
  129. "伽玛",
  130. //"白平衡温度",
  131. //"白平衡分量",
  132. "背光补偿",
  133. "增益" };
  134. string[] sliderStrArray = new string[]{
  135. "CTRL_AE",
  136. "CTRL_AE_ABS",
  137. "PU_BRIGHTNESS",
  138. "PU_CONTRAST",
  139. "PU_HUE",
  140. "PU_SATURATION",
  141. "PU_SHARPNESS",
  142. "PU_GAMMA",
  143. //"PU_WB_TEMP",
  144. //"PU_WB_COMPO",
  145. "PU_BACKLIGHT",
  146. "PU_GAIN" };
  147. UVCManager.CameraInfo currentCameraInfo;
  148. //初始化相机参数
  149. public void initSlider(UVCManager.CameraInfo cameraInfo)
  150. {
  151. if (currentCameraInfo != null) return;
  152. currentCameraInfo = cameraInfo;
  153. for (int i = 0; i < sliderStrArray.Length; i++)
  154. {
  155. string typeStr = sliderStrArray[i];
  156. UInt64 _VALUE = cameraInfo.GetTypeByName(typeStr);
  157. //不支持的,跳过
  158. bool bContains = cameraInfo.ContainsKey(_VALUE);
  159. if (!bContains) continue;
  160. if (typeStr == "CTRL_AE")
  161. {
  162. //曝光Toggle
  163. GameObject toggleObject = Instantiate(togglePrefab, spawnPoint.transform);
  164. toggleObject.SetActive(true);
  165. toggleObject.name = typeStr;
  166. Text labelObj = toggleObject.transform.Find("Label").GetComponent<Text>();
  167. labelObj.text = sliderNameArray[i];
  168. Toggle toggle = toggleObject.GetComponent<Toggle>();
  169. CTRLAEToggle = toggle;
  170. UVCCtrlInfo _AEInfo = cameraInfo.GetInfo(_VALUE);
  171. Debug.Log("UVCCtrlInfo:" + _AEInfo.ToString());
  172. int _currentValue = cameraInfo.GetValue(_VALUE);
  173. bAutoAE = _currentValue == 8 ? true : false; //如果当前值是 8,则当前摄像机是自动曝光模式
  174. toggle.isOn = bAutoAE;
  175. //cameraInfo.SetValue(_VALUE, _currentValue!=8? 8:1);
  176. toggle.onValueChanged.AddListener((bool bValue) =>
  177. {
  178. //Debug.Log("Toggle value changed to: " + bValue + " from " + _VALUE);
  179. if (typeStr == "CTRL_AE")
  180. {
  181. //开关控制是否自动曝光
  182. bAutoAE = bValue;
  183. if (CTRLAEABSSlider) CTRLAEABSSlider.interactable = !bAutoAE;
  184. int _value = bValue ? 8 : 1;
  185. //Debug.Log("_value " + _value);
  186. cameraInfo.SetValue(_VALUE, _value);
  187. }
  188. });
  189. }
  190. else
  191. {
  192. //其余使用slider
  193. GameObject sliderObject = Instantiate(sliderPrefab, spawnPoint.transform);
  194. sliderObject.SetActive(true);
  195. sliderObject.name = typeStr;
  196. Slider slider = sliderObject.GetComponent<Slider>();
  197. Text textObj = sliderObject.transform.Find("text").GetComponent<Text>();
  198. Text titleTextObj = sliderObject.transform.Find("title").GetComponent<Text>();
  199. titleTextObj.text = sliderNameArray[i];//类型名字
  200. if (slider != null)
  201. {
  202. UVCCtrlInfo _UVCCtrlInfo = cameraInfo.GetInfo(_VALUE);
  203. slider.minValue = _UVCCtrlInfo.min;
  204. slider.maxValue = _UVCCtrlInfo.max;
  205. slider.wholeNumbers = true;
  206. //记录一个typeStr类型的数据存储操作对象
  207. ParamFloatValue paramFloatValue = new ParamFloatValue("ic_uvc_" + typeStr, _UVCCtrlInfo.def);
  208. //dUVCParameters.Add(typeStr, paramFloatValue);
  209. //获取当前值
  210. int _currentValue = cameraInfo.GetValue(_VALUE);
  211. //int _saveValue = (int)dUVCParameters.GetValueOrDefault(typeStr).Get();
  212. slider.value = _currentValue;
  213. textObj.text = _currentValue + "";
  214. //存储初始值,设置一次到UVC参数
  215. Debug.Log(_UVCCtrlInfo.ToString());
  216. Debug.Log("dUVCParameters2:" + _currentValue + " == " + _UVCCtrlInfo.def);
  217. //如果是曝光slider
  218. if (typeStr == "CTRL_AE_ABS")
  219. {
  220. CTRLAEABSSlider = slider;
  221. slider.interactable = !bAutoAE;
  222. //if (!bAutoAE) {
  223. // cameraInfo.SetValue(_VALUE, (int)paramFloatValue.Get());
  224. //}
  225. }
  226. //else{
  227. // if (_currentValue != _saveValue) cameraInfo.SetValue(_VALUE, (int)paramFloatValue.Get());
  228. //}
  229. slider.onValueChanged.AddListener((newValue) =>
  230. {
  231. var _value = Mathf.FloorToInt(newValue);
  232. textObj.text = _value + "";
  233. //Debug.Log("Slider value changed to: " + newValue + " from " + _VALUE);
  234. cameraInfo.SetValue(_VALUE, _value);
  235. //存储值
  236. //dUVCParameters.GetValueOrDefault(typeStr).Set(_value);
  237. });
  238. }
  239. }
  240. }
  241. }
  242. public void OpenUVCPanel()
  243. {
  244. _cameraParameterPanel.SetActive(true);
  245. }
  246. public void CloseUVCPanel()
  247. {
  248. _cameraParameterPanel.SetActive(false);
  249. }
  250. public void onResetUVCData()
  251. {
  252. if (currentCameraInfo == null) return;
  253. for (int i = 0; i < sliderStrArray.Length; i++)
  254. {
  255. string typeStr = sliderStrArray[i];
  256. UInt64 _VALUE = currentCameraInfo.GetTypeByName(typeStr);
  257. bool bContains = currentCameraInfo.ContainsKey(_VALUE);
  258. if (!bContains) continue;
  259. if (typeStr == "CTRL_AE")
  260. {
  261. //toggle值不进行重置
  262. Debug.Log("CTRL_AE 不需要重置");
  263. }
  264. else if (typeStr == "CTRL_AE_ABS")
  265. {
  266. if (!bAutoAE)
  267. {
  268. //如果是手动曝光,重置值
  269. Transform trans = _cameraParameterPanel.transform.Find(typeStr);
  270. Slider slider = trans.GetComponent<Slider>();
  271. Text textObj = trans.Find("text").GetComponent<Text>();
  272. UVCCtrlInfo _AEInfo = currentCameraInfo.GetInfo(_VALUE);
  273. //获取当前值
  274. int _currentValue = currentCameraInfo.GetValue(_VALUE);
  275. Debug.Log("CTRL_AE_ABS:" + _currentValue + " = " + _AEInfo.def + " = " + ",bAutoAE:" + bAutoAE);
  276. textObj.text = _AEInfo.def + "";
  277. slider.value = _AEInfo.def;
  278. if (_AEInfo.def != _currentValue) currentCameraInfo.SetValue(_VALUE, _AEInfo.def);
  279. }
  280. }
  281. else
  282. {
  283. Transform trans = _cameraParameterPanel.transform.Find(typeStr);
  284. Slider slider = trans.GetComponent<Slider>();
  285. Text textObj = trans.Find("text").GetComponent<Text>();
  286. UVCCtrlInfo _UVCCtrlInfo = currentCameraInfo.GetInfo(_VALUE);
  287. textObj.text = _UVCCtrlInfo.def + "";
  288. slider.value = _UVCCtrlInfo.def;
  289. //获取当前值
  290. int _currentValue = currentCameraInfo.GetValue(_VALUE);
  291. //重置存储值
  292. //dUVCParameters.GetValueOrDefault(typeStr).Set(_UVCCtrlInfo.def);
  293. //存储初始值,设置一次到UVC参数
  294. if (_UVCCtrlInfo.def != _currentValue) currentCameraInfo.SetValue(_VALUE, _UVCCtrlInfo.def);
  295. }
  296. }
  297. }
  298. #endregion
  299. void InitInfraredCamera()
  300. {
  301. if (_inited) return;
  302. _inited = true;
  303. //SDK创建
  304. if (infraredCameraHelper == null)
  305. {
  306. infraredCameraHelper = InfraredCameraHelper.GetInstance();
  307. infraredCameraHelper.Create();
  308. infraredCameraHelper.OnPositionUpdate += (Vector2 point) =>
  309. {
  310. //跑九轴时候,不处理这里位置
  311. if (AimHandler.ins && AimHandler.ins.bRuning9Axis()) return;
  312. if (Camera.main == null) return;
  313. Ray ray = Camera.main.ScreenPointToRay(point);
  314. Vector3 rayEndPoint = ray.GetPoint(200);
  315. Quaternion quat = Quaternion.LookRotation(rayEndPoint - Camera.main.transform.position);
  316. // 挑战场景中其相机的父级有旋转,需要换算
  317. if (UnityEngine.SceneManagement.SceneManager.GetActiveScene().name == "GameChallenge")
  318. {
  319. quat = Quaternion.AngleAxis(-180, Vector3.up) * quat;
  320. }
  321. if (CameraToLook.ins != null) CameraToLook.ins.localRotation = quat;
  322. if (SB_EventSystem.ins) SB_EventSystem.ins.MoveSimulateMouseByInfrared(point);
  323. };
  324. infraredCameraHelper.OnUVCIsReady += (UVCManager.CameraInfo camera) =>
  325. {
  326. //生成控制摄像机的参数滑条
  327. Debug.Log("初始化摄像机!");
  328. initSlider(camera);
  329. //_cameraRender.texture = ScreenLocate.Main.getUVCTexture; //infraredCameraHelper.GetCameraTexture();
  330. //_cameraRender.SetNativeSize();
  331. };
  332. //InfraredCameraHelper.InfraredCameraHelperRawImageList.Add(_cameraRender);
  333. }
  334. //参数面板
  335. SetShakeFilterValue(shakeFilterValue.Get());
  336. _sliderShakeFilter.onValueChanged.AddListener(SetShakeFilterValue);
  337. //功能按钮
  338. _btnReset.onClick.AddListener(OnClick_Reset);
  339. _btnScreenLocateManual.onClick.AddListener(OnClick_ScreenLocateManual);
  340. _btnScreenLocateManualAuto.onClick.AddListener(OnClick_ScreenLocateManualAuto);
  341. SetCaptureValue(captureValue.Get());
  342. _sliderCapture.onValueChanged.AddListener(SetCaptureValue);
  343. SetDelayValue(delayValue.Get());
  344. _sliderDelay.onValueChanged.AddListener(SetDelayValue);
  345. //分辨率
  346. SetResolution((int)resoution.Get());
  347. _dropdownResolution.onValueChanged.AddListener(v =>
  348. {
  349. SetResolution(v);
  350. StartCoroutine(RestartOrKillApp());
  351. });
  352. SetResolutionNew((int)resoutionNew.Get());
  353. _dropdownResolution2.onValueChanged.AddListener(v =>
  354. {
  355. SetResolutionNew(v);
  356. });
  357. //绘制线段
  358. SetLineWidth(lineWidth.Get());
  359. _sliderLineWidth.onValueChanged.AddListener(SetLineWidth);
  360. //检测红外亮度阈值
  361. SetInfraredFilterValue(infraredFileterValue.Get());
  362. _infraredFilter.onValueChanged.AddListener(SetInfraredFilterValue);
  363. }
  364. public int getCrosshairValue() {
  365. return (int)crosshairValue.Get();
  366. }
  367. public void setCrosshairValue(bool bshow) {
  368. crosshairValue.Set(bshow?1:0);
  369. }
  370. public void onStartPreview()
  371. {
  372. infraredCameraHelper.onStartPreview();
  373. }
  374. public void onStopPreview()
  375. {
  376. infraredCameraHelper.onStopPreview();
  377. }
  378. //获取并且初始化一次数据
  379. void initScreenLocateManual()
  380. {
  381. ScreenLocate.GetScreenLocateVectorList();
  382. infraredCameraHelper.QuitScreenLocateManual(ScreenLocate.quadUnityVectorList);
  383. ScreenLocate.Main.SyncInfraredDemo();
  384. }
  385. IEnumerator RestartOrKillApp()
  386. {
  387. yield return new WaitForSeconds(0.3f);
  388. if (Application.isEditor) yield break;
  389. if (Application.platform == RuntimePlatform.Android)
  390. {
  391. using (var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
  392. {
  393. const int kIntent_FLAG_ACTIVITY_CLEAR_TASK = 0x00008000;
  394. const int kIntent_FLAG_ACTIVITY_NEW_TASK = 0x10000000;
  395. var currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
  396. var pm = currentActivity.Call<AndroidJavaObject>("getPackageManager");
  397. var intent = pm.Call<AndroidJavaObject>("getLaunchIntentForPackage", Application.identifier);
  398. intent.Call<AndroidJavaObject>("setFlags", kIntent_FLAG_ACTIVITY_NEW_TASK | kIntent_FLAG_ACTIVITY_CLEAR_TASK);
  399. currentActivity.Call("startActivity", intent);
  400. currentActivity.Call("finish");
  401. var process = new AndroidJavaClass("android.os.Process");
  402. int pid = process.CallStatic<int>("myPid");
  403. process.CallStatic("killProcess", pid);
  404. }
  405. }
  406. else Application.Quit();
  407. }
  408. void UpdateInfraredCamera()
  409. {
  410. if (!_visiable) return;
  411. if (!_inited) return;
  412. if (infraredCameraHelper == null) return;
  413. //渲染相机画面
  414. //_cameraRender.texture = infraredCameraHelper.GetCameraTexture();
  415. //_cameraRender.material = infraredCameraHelper.GetCameraMaterial();
  416. if (ScreenLocate.Main.getUVCTexture)
  417. {
  418. _cameraRender.texture = ScreenLocate.Main.getUVCTexture;
  419. _cameraRender.SetNativeSize();
  420. _MaintainAspectRatio.AdjustSize();
  421. }
  422. //在相机画面显示准心
  423. if (ScreenLocate.Main)
  424. {
  425. var _sl = ScreenLocate.Main;
  426. var buffer = _sl.infraredSpotBuffer;
  427. if (buffer != null)
  428. {
  429. for (int i = 0; i < buffer.Length; i++)
  430. {
  431. if (buffer[i].CameraLocation != null)
  432. {
  433. // 检测到光点
  434. var pos = buffer[i].CameraLocation.Value.pixelToLocalPosition_AnchorCenter(_sl.mUVCCameraInfo.Size, _cameraRender.rectTransform.rect);
  435. _crosshairsInCamera[i].gameObject.SetActive(true);
  436. _crosshairsInCamera[i].anchoredPosition = pos;
  437. }
  438. else
  439. _crosshairsInCamera[i].gameObject.SetActive(false);
  440. }
  441. }
  442. }
  443. }
  444. public void SetLocatePointsToCameraRender(List<Vector2> points, float w, float h)
  445. {
  446. Transform pointsTF2 = _cameraRender.transform.Find("Points");
  447. if (pointsTF2.childCount == points.Count)
  448. {
  449. Vector2 texSize = new Vector2(w, h);
  450. for (int i = 0; i < pointsTF2.childCount; i++)
  451. {
  452. Transform pointTF = pointsTF2.GetChild(i);
  453. Vector2 pos = points[i];
  454. pointTF.localPosition = pos.pixelToLocalPosition_AnchorCenter(texSize, _cameraRender.rectTransform.rect);
  455. pointTF.gameObject.SetActive(true);
  456. }
  457. }
  458. }
  459. void SetShakeFilterValue(float v)
  460. {
  461. shakeFilterValue.Set(v);
  462. _sliderShakeFilter.SetValueWithoutNotify(shakeFilterValue.Get());
  463. _sliderShakeFilter.transform.Find("Value").GetComponent<Text>().text = shakeFilterValue.Get().ToString("f1");
  464. infraredCameraHelper.SetShakeFilterValue(shakeFilterValue.Get());
  465. }
  466. public void resolutionRestartApp(int index)
  467. {
  468. SetResolution(index);
  469. StartCoroutine(RestartOrKillApp());
  470. }
  471. //SetResolution((int)resoution.Get());
  472. public void SetResolutionGuider(int optionIndex)
  473. {
  474. resoution.Set(optionIndex);
  475. switch (optionIndex)
  476. {
  477. case 0:
  478. infraredCameraHelper.SetCameraResolution(1280, 720);
  479. break;
  480. case 1:
  481. infraredCameraHelper.SetCameraResolution(640, 360);
  482. break;
  483. case 2:
  484. infraredCameraHelper.SetCameraResolution(320, 240);
  485. break;
  486. }
  487. }
  488. public void SetResolution(int optionIndex)
  489. {
  490. resoution.Set(optionIndex);
  491. _dropdownResolution.SetValueWithoutNotify(optionIndex);
  492. switch (optionIndex)
  493. {
  494. case 0:
  495. infraredCameraHelper.SetCameraResolution(1280, 720);
  496. break;
  497. case 1:
  498. infraredCameraHelper.SetCameraResolution(640, 360);
  499. break;
  500. case 2:
  501. infraredCameraHelper.SetCameraResolution(320, 240);
  502. break;
  503. }
  504. }
  505. public int ResolutionIndex => _dropdownResolution2.value;
  506. public void SetResolutionNew(int optionIndex)
  507. {
  508. resoutionNew.Set(optionIndex);
  509. _dropdownResolution2.SetValueWithoutNotify(optionIndex);
  510. switch (optionIndex)
  511. {
  512. case 0:
  513. infraredCameraHelper.SetCameraResolutionNew(1920, 1080);
  514. break;
  515. case 1:
  516. infraredCameraHelper.SetCameraResolutionNew(1280, 800);
  517. break;
  518. case 2:
  519. infraredCameraHelper.SetCameraResolutionNew(1280, 720);
  520. break;
  521. case 3:
  522. infraredCameraHelper.SetCameraResolutionNew(1024, 768);
  523. break;
  524. case 4:
  525. infraredCameraHelper.SetCameraResolutionNew(960, 540);
  526. break;
  527. case 5:
  528. infraredCameraHelper.SetCameraResolutionNew(800, 600);
  529. break;
  530. case 6:
  531. infraredCameraHelper.SetCameraResolutionNew(848, 480);
  532. break;
  533. case 7:
  534. infraredCameraHelper.SetCameraResolutionNew(640, 480);
  535. break;
  536. case 8:
  537. infraredCameraHelper.SetCameraResolutionNew(640, 360);
  538. break;
  539. case 9:
  540. infraredCameraHelper.SetCameraResolutionNew(352, 288);
  541. break;
  542. case 10:
  543. infraredCameraHelper.SetCameraResolutionNew(320, 240);
  544. break;
  545. case 11:
  546. //不支持这种分辨率设置
  547. infraredCameraHelper.SetCameraResolutionNew(1280, 960);
  548. break;
  549. }
  550. }
  551. void OnClick_Reset()
  552. {
  553. //SetBrightness(1);
  554. //SetSaturation(1);
  555. //SetContrast(1);
  556. SetShakeFilterValue(6);
  557. SetLineWidth(10);
  558. }
  559. void OnClick_ScreenLocateManual()
  560. {
  561. ViewManager2.ShowView(ViewManager2.Path_InfraredScreenPositioningView);
  562. FindObjectOfType<InfraredScreenPositioningView>().enterFromInfraredDemo = true;
  563. }
  564. void OnClick_ScreenLocateManualAuto()
  565. {
  566. infraredCameraHelper.EnterScreenLocateManualAuto();
  567. }
  568. void SetCaptureValue(float v)
  569. {
  570. captureValue.Set(v);
  571. _sliderCapture.SetValueWithoutNotify(captureValue.Get());
  572. _sliderCapture.transform.Find("Value").GetComponent<Text>().text = captureValue.Get().ToString("f1");
  573. infraredCameraHelper.SetCapture((int)captureValue.Get());
  574. }
  575. void SetDelayValue(float v)
  576. {
  577. delayValue.Set(v);
  578. _sliderDelay.SetValueWithoutNotify(delayValue.Get());
  579. _sliderDelay.transform.Find("Value").GetComponent<Text>().text = delayValue.Get().ToString("f1");
  580. infraredCameraHelper.SetDelay((int)delayValue.Get());
  581. }
  582. #endregion
  583. #region 相机感光度
  584. public void onSetSliderValue(Slider _slider)
  585. {
  586. if (running)
  587. {
  588. string typeStr = "PU_BRIGHTNESS";
  589. UInt64 _VALUE = currentCameraInfo.GetTypeByName(typeStr);
  590. UVCCtrlInfo _UVCCtrlInfo = currentCameraInfo.GetInfo(_VALUE);
  591. float v = currentCameraInfo.GetValue(_VALUE); // infraredCameraHelper.GetBrightness();
  592. //float v2 = (v / _UVCCtrlInfo.max) * 10;
  593. // 目标区间 [0, 10] 的边界值
  594. double targetMin = 0.0;
  595. double targetMax = 10.0;
  596. double originalMin = _UVCCtrlInfo.min;
  597. double originalMax = _UVCCtrlInfo.max;
  598. // 计算转换后的值
  599. double v2 = MapValue(v, originalMin, originalMax, targetMin, targetMax);
  600. Debug.Log("获取相机的感光度:" + _VALUE + " = " + v + " = " + v2);
  601. _slider.SetValueWithoutNotify((float)v2);
  602. }
  603. else _slider.SetValueWithoutNotify(5);
  604. }
  605. public void onSliderEvent(float value)
  606. {
  607. Debug.Log(value);
  608. //修改亮度时,调试界面的亮度也应该一起修改
  609. if (running)
  610. {
  611. //Slider slider = transform.Find("InfraredCamera/Layout/SliderBrightness")
  612. // .GetComponent<Slider>();
  613. string typeStr = "PU_BRIGHTNESS";
  614. UInt64 _VALUE = currentCameraInfo.GetTypeByName(typeStr);
  615. Transform trans = _cameraParameterPanel.transform.Find(typeStr);
  616. Slider slider = trans.GetComponent<Slider>();
  617. Text textObj = trans.Find("text").GetComponent<Text>();
  618. UVCCtrlInfo _UVCCtrlInfo = currentCameraInfo.GetInfo(_VALUE);
  619. int _currentUVCValue = currentCameraInfo.GetValue(_VALUE);
  620. //value 0 ~ 10
  621. // 原始区间和目标区间的边界值
  622. double originalMin = 0.0;
  623. double originalMax = 10.0;
  624. double targetMin = _UVCCtrlInfo.min;
  625. double targetMax = _UVCCtrlInfo.max;
  626. // 计算转换后的值
  627. double result = MapValue(value, originalMin, originalMax, targetMin, targetMax);
  628. int _current = (int)(result);
  629. Debug.Log("_current:" + value + " , result:" + result);
  630. textObj.text = _current + "";
  631. slider.value = _current;
  632. //dUVCParameters.GetValueOrDefault(typeStr).Set(_current);
  633. //存储初始值,设置一次到UVC参数
  634. if (_currentUVCValue != _current) currentCameraInfo.SetValue(_VALUE, _current);
  635. }
  636. }
  637. double MapValue(double value, double originalMin, double originalMax, double targetMin, double targetMax)
  638. {
  639. // 线性插值公式
  640. return targetMin + (value - originalMin) * (targetMax - targetMin) / (originalMax - originalMin);
  641. }
  642. #endregion
  643. #region 绘制线段部分
  644. public void SetLineWidth(float v)
  645. {
  646. lineWidth.Set(v);
  647. _sliderLineWidth.SetValueWithoutNotify(lineWidth.Get());
  648. _sliderLineWidth.transform.Find("Value").GetComponent<Text>().text = lineWidth.Get().ToString("f1");
  649. }
  650. #endregion
  651. #region 亮度检测部分
  652. public void SetInfraredFilterValue(float v)
  653. {
  654. infraredFileterValue.Set(v);
  655. _infraredFilter.SetValueWithoutNotify(infraredFileterValue.Get());
  656. _infraredFilter.transform.Find("Value").GetComponent<Text>().text = infraredFileterValue.Get().ToString("f1");
  657. infraredCameraHelper.SetInfraredLocateBrightnessThreshold(infraredFileterValue.Get());
  658. }
  659. #endregion
  660. public void resetInfraredPlayerPrefs()
  661. {
  662. //测试用
  663. PlayerPrefs.DeleteKey("entry-guider-infrared-" + LoginMgr.myUserInfo.id);
  664. PlayerPrefs.DeleteKey("hideInfraredBowAndArrow");
  665. }
  666. }
  667. public class ParamFloatValue
  668. {
  669. private string _saveKey;
  670. private float _valueDefault;
  671. private bool _valueLoaded;
  672. private float _value;
  673. public ParamFloatValue(string saveKey, float valueDefault)
  674. {
  675. _saveKey = saveKey;
  676. _valueDefault = valueDefault;
  677. }
  678. public float Get()
  679. {
  680. if (!_valueLoaded) _value = PlayerPrefs.GetFloat(_saveKey, _valueDefault);
  681. return _value;
  682. }
  683. public void Set(float value)
  684. {
  685. _value = value;
  686. PlayerPrefs.SetFloat(_saveKey, _value);
  687. PlayerPrefs.Save();
  688. }
  689. }