InfraredDemo.cs 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816
  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 SLAMUVC;
  9. using System;
  10. using SmartBowSDK;
  11. using ZIM.Unity;
  12. public class InfraredDemo : JCUnityLib.ViewBase
  13. {
  14. public static bool DebugInEditor = true;
  15. public static InfraredDemo _ins;
  16. [SerializeField]
  17. RectTransform canvasRectTransform;
  18. public Button mBtnSee;
  19. Reporter reporter;
  20. public Text FPSText;
  21. public static void Create()
  22. {
  23. if (_ins) return;
  24. GameObject o = Instantiate(Resources.Load<GameObject>("InfraredDemo"));
  25. DontDestroyOnLoad(o);
  26. _ins = o.GetComponent<InfraredDemo>();
  27. //添加一个父物体
  28. o.transform.SetParent(ViewMgr.Instance.transform.Find("1").transform);
  29. CanvasScaler canvasScaler = o.GetComponent<CanvasScaler>();
  30. if (canvasScaler != null)
  31. {
  32. Destroy(canvasScaler);
  33. }
  34. RectTransform rectTransform = o.GetComponent<RectTransform>();
  35. rectTransform.anchorMin = Vector2.zero; // 左下角对齐父级
  36. rectTransform.anchorMax = Vector2.one; // 右上角对齐父级
  37. rectTransform.offsetMin = Vector2.zero; // 移除左下角偏移
  38. rectTransform.offsetMax = Vector2.zero; // 移除右上角偏移
  39. rectTransform.localScale = Vector3.one; // 确保缩放为 1
  40. //直接初始化一次
  41. _ins.InitInfraredCamera();
  42. }
  43. void Start()
  44. {
  45. //生成一个控制台
  46. GameObject obj = UserPlayer.InitReturnLogReporter() as GameObject;
  47. if (obj != null) {
  48. reporter = obj.GetComponent<Reporter>();
  49. }
  50. if (Application.platform != RuntimePlatform.WindowsEditor) DebugInEditor = false;
  51. SetVisiable(false);
  52. InitDebugScreenPoint();
  53. // gameObject.AddComponent<FPSTester>();
  54. if (CommonConfig.StandaloneModeOrPlatformB)
  55. {
  56. mBtnSee.gameObject.SetActive(false);
  57. }
  58. else {
  59. mBtnSee.interactable = false;
  60. mBtnSee.transform.Find("Text").GetComponent<Text>().text = TextAutoLanguage2.GetTextByKey("WaitingForInitialization"); // "等待初始化..";
  61. }
  62. //直接初始化一次
  63. //InitInfraredCamera();
  64. Debug.Log("[InfraredDemo] Start Function!");
  65. initToggle();
  66. initExport();
  67. InitFullScreen();
  68. #if UNITY_STANDALONE_WIN
  69. _calibrationFixedText.transform.parent.gameObject.SetActive(false);
  70. #endif
  71. }
  72. void OnGUI()
  73. {
  74. ShowVersionCode();
  75. }
  76. private void ShowVersionCode()
  77. {
  78. GUIStyle labelFont = new GUIStyle();
  79. labelFont.normal.textColor = new Color(1, 0.6f, 0.6f);
  80. labelFont.fontSize = Mathf.CeilToInt(Screen.height * 0.03f);
  81. GUI.Label(new Rect(Screen.width / 100, Screen.height / 100, 100, 100), "game-20241204-1814-5-1", labelFont);
  82. }
  83. void Update()
  84. {
  85. UpdateInfraredCamera();
  86. //UpdateDebugScreenPoint();
  87. if(reporter!=null) FPSText.text = "FPS:" + reporter.fps;
  88. }
  89. public void OnClick_See()
  90. {
  91. SetVisiable(!_visiable);
  92. }
  93. //初始化一次 initScreenLocateManual
  94. bool hasCalled = false;
  95. bool _visiable;
  96. void SetVisiable(bool value)
  97. {
  98. _visiable = value;
  99. transform.Find("Background").gameObject.SetActive(value);
  100. transform.Find("InfraredCamera").gameObject.SetActive(value);
  101. //如果是b端,控制这里按钮显示
  102. if (CommonConfig.StandaloneModeOrPlatformB)
  103. {
  104. mBtnSee.gameObject.SetActive(value);
  105. }
  106. updateBtnSee();
  107. Action<Text> onApplyToNext = (t) => {
  108. updateBtnSee();
  109. };
  110. mBtnSee.GetComponentInChildren<TextAutoLanguage2>().onApplyToNext += onApplyToNext;
  111. //if (value) InitInfraredCamera();
  112. //刷新翻译的Dropdown
  113. if (value) {
  114. UpdateLanguage();
  115. updateDropdownResolution2();
  116. }
  117. //if (!hasCalled && value)
  118. //{
  119. // hasCalled = true;
  120. // //如果本地有记录,初始化一次
  121. // StartCoroutine(initScreenLocateManual());
  122. // Debug.Log("[InfraredDemo] SetVisiable Function!");
  123. //}
  124. }
  125. void updateBtnSee() {
  126. mBtnSee.GetComponentInChildren<Text>().text = _visiable ? TextAutoLanguage2.GetTextByKey("HideInterface") : TextAutoLanguage2.GetTextByKey("DebugInfrared");//"隐藏界面" : "调试红外";
  127. }
  128. Text _spText;
  129. Vector2 _screenPoint;
  130. void InitDebugScreenPoint()
  131. {
  132. _spText = transform.Find("SPText").GetComponent<Text>();
  133. //_spText.gameObject.SetActive(DebugInEditor);
  134. _spText.gameObject.SetActive(false);
  135. }
  136. void UpdateDebugScreenPoint()
  137. {
  138. if (!DebugInEditor) return;
  139. if (infraredCameraHelper == null) return;
  140. _screenPoint.x = Mathf.Clamp(
  141. _screenPoint.x + Input.GetAxis("Horizontal") * Screen.width / 400f,
  142. 0, Screen.width);
  143. _screenPoint.y = Mathf.Clamp(
  144. _screenPoint.y + Input.GetAxis("Vertical") * Screen.height / 400f,
  145. 0, Screen.height);
  146. infraredCameraHelper.InvokeOnPositionUpdate(_screenPoint);
  147. _spText.text = _screenPoint.ToString();
  148. }
  149. #region 红外摄像
  150. [SerializeField] RawImage _cameraRender;
  151. [SerializeField] RawImage _cameraRender2;
  152. /// <summary>
  153. /// 测试texture
  154. /// </summary>
  155. public RawImage MyCameraRender2
  156. {
  157. get { return _cameraRender2; }
  158. set { _cameraRender2 = value; }
  159. }
  160. //[SerializeField] MaintainAspectRatio _MaintainAspectRatio;
  161. [SerializeField] List<RectTransform> _crosshairsInCamera;
  162. [SerializeField] Slider _sliderShakeFilter;
  163. [SerializeField] Button _btnReset;
  164. [SerializeField] Button _btnScreenLocateManual;
  165. [SerializeField] Button _btnScreenLocateManualAuto;
  166. [SerializeField] Slider _sliderCapture;
  167. [SerializeField] Slider _sliderDelay;
  168. [SerializeField] Dropdown _dropdownResolution;
  169. [SerializeField] Dropdown _dropdownResolution2;
  170. [SerializeField] Slider _sliderLineWidth;
  171. [SerializeField] Slider _fanWidth;
  172. //红外线阈值
  173. [SerializeField] Slider _infraredFilter;
  174. //调试UVC参数
  175. [SerializeField] GameObject _cameraParameterPanel;
  176. [SerializeField] Button _btnAdjusting;
  177. //野鸭选择
  178. [SerializeField] Dropdown _duckLevelDropdown;
  179. //Dictionary<string, ParamFloatValue> dUVCParameters = new Dictionary<string, ParamFloatValue>();
  180. //public ParamFloatValue brightness = new ParamFloatValue("ic_brightness", 1.0f);
  181. //public ParamFloatValue saturation = new ParamFloatValue("ic_saturation", 1.0f);
  182. //public ParamFloatValue contrast = new ParamFloatValue("ic_contrast", 1.0f);
  183. #if UNITY_ANDROID
  184. //初始化纹理-1280*720,index = 0
  185. // public ParamFloatValue resoution = new ParamFloatValue("ic_resoution2", 0);
  186. //摄像机分辨率 -320*240,index = 10
  187. public ParamFloatValue resoutionNew = new ParamFloatValue("ic_resoutionNew", 10);
  188. //摄像机分辨率 -只处理高低分辨率情况
  189. public ParamFloatValue resoution = new ParamFloatValue("ic_resoution", 0);
  190. //string[] resolutions = { "高分辨率", "低分辨率" };
  191. #endif
  192. //抖动过滤值 - 6.0
  193. public ParamFloatValue shakeFilterValue = new ParamFloatValue("ic_shakeFilterValue2", 6.0f);
  194. //亮度过滤阈值 - 0.8
  195. public ParamFloatValue infraredFileterValue = new ParamFloatValue("ic_infraredFileterValue", 0.8f);
  196. //线段宽度阈值
  197. public ParamFloatValue lineWidth = new ParamFloatValue("ic_lineWidth", 4.0f);
  198. //扇形宽度
  199. public ParamFloatValue fanWidth = new ParamFloatValue("ic_fanWidth", 90.0f);
  200. public ParamFloatValue captureValue = new ParamFloatValue("ic_captureValue", 30.0f);
  201. public ParamFloatValue delayValue = new ParamFloatValue("ic_delayValue", 30.0f);
  202. //红外准心,默认打开吧
  203. public ParamFloatValue crosshairValue = new ParamFloatValue("ic_crosshairValue", 1);
  204. //野鸭测试存储
  205. public ParamFloatValue duckHunterLevel = new ParamFloatValue("test_duckHunterLevelValue", 1);
  206. //存储滑块信息。camera
  207. public Dictionary<string ,ParamFloatValue> ParamFloatValueCameraSlider = new Dictionary<string, ParamFloatValue>();
  208. public static InfraredCameraHelper infraredCameraHelper;
  209. public static bool running { get => infraredCameraHelper != null; }
  210. private bool _inited;
  211. #region 参数控制
  212. public GameObject togglePrefab; // 拖入一个Toggle预设体
  213. public GameObject sliderPrefab; // 拖入一个Slider预设体
  214. public GameObject spawnPoint;
  215. //自动曝光
  216. bool bAutoAE = false;
  217. Toggle CTRLAEToggle;
  218. Slider CTRLAEABSSlider;
  219. string[] sliderNameArray = new string[]{
  220. "自动曝光模式",
  221. "曝光时间(绝对)",
  222. "亮度", //
  223. "对比度",
  224. "色调",
  225. "饱和度",
  226. "锐度",
  227. "伽玛",
  228. //"白平衡温度",
  229. //"白平衡分量",
  230. "背光补偿",
  231. "增益" };
  232. string[] sliderStrArray = new string[]{
  233. "CTRL_AE",
  234. "CTRL_AE_ABS",
  235. "PU_BRIGHTNESS",
  236. "PU_CONTRAST",
  237. "PU_HUE",
  238. "PU_SATURATION",
  239. "PU_SHARPNESS",
  240. "PU_GAMMA",
  241. //"PU_WB_TEMP",
  242. //"PU_WB_COMPO",
  243. "PU_BACKLIGHT",
  244. "PU_GAIN" };
  245. UVCManager.CameraInfo currentCameraInfo;
  246. //初始化相机参数
  247. public void initSlider(UVCManager.CameraInfo cameraInfo)
  248. {
  249. if (currentCameraInfo != null) return;
  250. currentCameraInfo = cameraInfo;
  251. string[] sliderNameArrayKey = new string[] {
  252. "AutoExposureMode", // "自动曝光模式"
  253. "ExposureTimeAbsolute", // "曝光时间(绝对)"
  254. "Brightness", // "亮度"
  255. "Contrast", // "对比度"
  256. "Hue", // "色调"
  257. "Saturation", // "饱和度"
  258. "Sharpness", // "锐度"
  259. "Gamma", // "伽玛"
  260. "BacklightCompensation", // "背光补偿"
  261. "Gain" // "增益"
  262. };
  263. for (int i = 0; i < sliderStrArray.Length; i++)
  264. {
  265. string typeStr = sliderStrArray[i];
  266. // UInt64 _VALUE = cameraInfo.GetTypeByName(typeStr);
  267. //不支持的,跳过
  268. bool bContains = cameraInfo.ContainsKey(typeStr);
  269. if (!bContains) continue;
  270. if (typeStr == "CTRL_AE")
  271. {
  272. //曝光Toggle
  273. GameObject toggleObject = Instantiate(togglePrefab, spawnPoint.transform);
  274. toggleObject.SetActive(true);
  275. toggleObject.name = typeStr;
  276. Text labelObj = toggleObject.transform.Find("Label").GetComponent<Text>();
  277. //labelObj.text = sliderNameArray[i];
  278. labelObj.gameObject.AddComponent<TextAutoLanguage2>().SetTextKey(sliderNameArrayKey[i]);
  279. Toggle toggle = toggleObject.GetComponent<Toggle>();
  280. CTRLAEToggle = toggle;
  281. UVCCtrlInfo _AEInfo = cameraInfo.GetInfo(typeStr);
  282. Debug.Log("UVCCtrlInfo:" + _AEInfo.ToString());
  283. int _currentValue = cameraInfo.GetValue(typeStr);
  284. bAutoAE = _currentValue == 8 ? true : false; //如果当前值是 8,则当前摄像机是自动曝光模式
  285. toggle.isOn = bAutoAE;
  286. //cameraInfo.SetValue(_VALUE, _currentValue!=8? 8:1);
  287. toggle.onValueChanged.AddListener((bool bValue) =>
  288. {
  289. //Debug.Log("Toggle value changed to: " + bValue + " from " + _VALUE);
  290. if (typeStr == "CTRL_AE")
  291. {
  292. //开关控制是否自动曝光
  293. bAutoAE = bValue;
  294. if (CTRLAEABSSlider) CTRLAEABSSlider.interactable = !bAutoAE;
  295. int _value = bValue ? 8 : 1;
  296. //Debug.Log("_value " + _value);
  297. cameraInfo.SetValue(typeStr, _value);
  298. }
  299. });
  300. }
  301. else
  302. {
  303. //其余使用slider
  304. GameObject sliderObject = Instantiate(sliderPrefab, spawnPoint.transform);
  305. sliderObject.SetActive(true);
  306. sliderObject.name = typeStr;
  307. Slider slider = sliderObject.GetComponent<Slider>();
  308. Text textObj = sliderObject.transform.Find("text").GetComponent<Text>();
  309. Text titleTextObj = sliderObject.transform.Find("title").GetComponent<Text>();
  310. //titleTextObj.text = sliderNameArray[i];//类型名字
  311. titleTextObj.gameObject.AddComponent<TextAutoLanguage2>().SetTextKey(sliderNameArrayKey[i]);
  312. if (slider != null)
  313. {
  314. UVCCtrlInfo _UVCCtrlInfo = cameraInfo.GetInfo(typeStr);
  315. slider.minValue = _UVCCtrlInfo.min;
  316. slider.maxValue = _UVCCtrlInfo.max;
  317. slider.wholeNumbers = true;
  318. int _defValue = _UVCCtrlInfo.def;
  319. //指定默认值
  320. //5、UVC亮度 - 50
  321. //6、UVC对比度 - 50
  322. if (typeStr == "PU_BRIGHTNESS") {
  323. _defValue = 50;
  324. } else if (typeStr == "PU_CONTRAST") {
  325. _defValue = 50;
  326. }
  327. //记录一个typeStr类型的数据存储操作对象
  328. ParamFloatValue paramFloatValue = new ParamFloatValue("ic_uvc_" + typeStr, _defValue);
  329. ParamFloatValueCameraSlider.Add(typeStr, paramFloatValue);
  330. if (typeStr == "PU_BRIGHTNESS" || typeStr == "PU_CONTRAST")
  331. {
  332. //获取设置默认存储的值
  333. int _Value = (int)paramFloatValue.Get();
  334. cameraInfo.SetValue(typeStr, _Value);
  335. Debug.Log("[InfraredDemo] UVC 设置" + typeStr + "默认值:" + _Value);
  336. slider.value = _Value;
  337. textObj.text = _Value + "";
  338. }
  339. else {
  340. //获取设置当前值
  341. int _currentValue = cameraInfo.GetValue(typeStr);
  342. slider.value = _currentValue;
  343. textObj.text = _currentValue + "";
  344. }
  345. //如果是曝光slider
  346. if (typeStr == "CTRL_AE_ABS")
  347. {
  348. CTRLAEABSSlider = slider;
  349. slider.interactable = !bAutoAE;
  350. }
  351. slider.onValueChanged.AddListener((newValue) =>
  352. {
  353. var _value = Mathf.FloorToInt(newValue);
  354. textObj.text = _value + "";
  355. //Debug.Log("Slider value changed to: " + newValue + " from " + _VALUE);
  356. cameraInfo.SetValue(typeStr, _value);
  357. paramFloatValue.Set(_value);
  358. });
  359. }
  360. }
  361. }
  362. }
  363. public void OpenUVCPanel()
  364. {
  365. _cameraParameterPanel.SetActive(true);
  366. }
  367. public void CloseUVCPanel()
  368. {
  369. _cameraParameterPanel.SetActive(false);
  370. }
  371. public void onResetUVCData()
  372. {
  373. #if UNITY_ANDROID
  374. resetUVCData();
  375. #endif
  376. #if UNITY_STANDALONE_WIN || UNITY_EDITOR
  377. resetPCData();
  378. #endif
  379. }
  380. void resetUVCData() {
  381. if (currentCameraInfo == null) return;
  382. for (int i = 0; i < sliderStrArray.Length; i++)
  383. {
  384. string typeStr = sliderStrArray[i];
  385. // UInt64 _VALUE = currentCameraInfo.GetTypeByName(typeStr);
  386. bool bContains = currentCameraInfo.ContainsKey(typeStr);
  387. if (!bContains) continue;
  388. if (typeStr == "CTRL_AE")
  389. {
  390. //toggle值不进行重置
  391. Debug.Log("CTRL_AE 不需要重置");
  392. }
  393. else if (typeStr == "CTRL_AE_ABS")
  394. {
  395. if (!bAutoAE)
  396. {
  397. //如果是手动曝光,重置值
  398. Transform trans = _cameraParameterPanel.transform.Find(typeStr);
  399. Slider slider = trans.GetComponent<Slider>();
  400. Text textObj = trans.Find("text").GetComponent<Text>();
  401. UVCCtrlInfo _AEInfo = currentCameraInfo.GetInfo(typeStr);
  402. //获取当前值
  403. int _currentValue = currentCameraInfo.GetValue(typeStr);
  404. Debug.Log("CTRL_AE_ABS:" + _currentValue + " = " + _AEInfo.def + " = " + ",bAutoAE:" + bAutoAE);
  405. textObj.text = _AEInfo.def + "";
  406. slider.value = _AEInfo.def;
  407. if (_AEInfo.def != _currentValue) currentCameraInfo.SetValue(typeStr, _AEInfo.def);
  408. }
  409. }
  410. else
  411. {
  412. Transform trans = _cameraParameterPanel.transform.Find(typeStr);
  413. Slider slider = trans.GetComponent<Slider>();
  414. Text textObj = trans.Find("text").GetComponent<Text>();
  415. UVCCtrlInfo _UVCCtrlInfo = currentCameraInfo.GetInfo(typeStr);
  416. ParamFloatValue paramFloatValue = ParamFloatValueCameraSlider.GetValueOrDefault(typeStr);
  417. //重置的值
  418. int _defValue = _UVCCtrlInfo.def;
  419. if (typeStr == "PU_BRIGHTNESS")
  420. {
  421. _defValue = (int)paramFloatValue.GetDefault();
  422. }
  423. else if (typeStr == "PU_CONTRAST")
  424. {
  425. _defValue = (int)paramFloatValue.GetDefault();
  426. }
  427. textObj.text = _defValue + "";
  428. slider.value = _defValue;
  429. //获取当前值
  430. int _currentValue = currentCameraInfo.GetValue(typeStr);
  431. //重置存储值
  432. paramFloatValue.Reset();
  433. //存储初始值,设置一次到UVC参数
  434. if (_defValue != _currentValue) currentCameraInfo.SetValue(typeStr, _defValue);
  435. }
  436. }
  437. }
  438. //windows 相机
  439. string[] sliderNameArrayPC = new string[]{
  440. "亮度",
  441. "对比度",};
  442. string[] sliderNameArrayPCKey = new string[]{
  443. "Brightness",
  444. "Contrast",};
  445. string[] sliderStrArrayPC = new string[]{
  446. "PU_BRIGHTNESS",
  447. "PU_CONTRAST"};
  448. //记录一个typeStr类型的数据存储操作对象
  449. ParamFloatValue pcBRIGHTNESS = new ParamFloatValue("ic_pc_PU_BRIGHTNESS", 0);
  450. ParamFloatValue pcCONTRAST = new ParamFloatValue("ic_pc_PU_CONTRAST", 0);
  451. public void initSliderPC()
  452. {
  453. for (int i = 0; i < sliderStrArrayPC.Length; i++)
  454. {
  455. string typeStr = sliderStrArrayPC[i];
  456. GameObject sliderObject = Instantiate(sliderPrefab, spawnPoint.transform);
  457. sliderObject.SetActive(true);
  458. sliderObject.name = typeStr;
  459. Slider slider = sliderObject.GetComponent<Slider>();
  460. Text textObj = sliderObject.transform.Find("text").GetComponent<Text>();
  461. Text titleTextObj = sliderObject.transform.Find("title").GetComponent<Text>();
  462. //titleTextObj.text = TextAutoLanguage2.GetTextByKey(sliderNameArrayPCKey[i]);// sliderNameArrayPC[i];//类型名字
  463. titleTextObj.gameObject.AddComponent<TextAutoLanguage2>().SetTextKey(sliderNameArrayPCKey[i]);
  464. if (slider != null)
  465. {
  466. slider.minValue = -1;
  467. slider.maxValue = 1;
  468. //记录一个typeStr类型的数据存储操作对象
  469. if (typeStr == "PU_BRIGHTNESS")
  470. {
  471. //获取设置默认存储的值
  472. float _Value = pcBRIGHTNESS.Get();
  473. infraredCameraHelper.SetBrightness(_Value);
  474. Debug.Log("[InfraredDemo] PC 设置 PU_BRIGHTNESS:" + typeStr + "默认值:" + _Value);
  475. slider.value = _Value;
  476. textObj.text = _Value + "";
  477. slider.onValueChanged.AddListener((newValue) =>
  478. {
  479. var _value = newValue;
  480. textObj.text = _value + "";
  481. infraredCameraHelper.SetBrightness(_value);
  482. pcBRIGHTNESS.Set(_value);
  483. });
  484. }
  485. else if (typeStr == "PU_CONTRAST")
  486. {
  487. //获取设置默认存储的值
  488. float _Value = pcCONTRAST.Get();
  489. infraredCameraHelper.SetContrast(_Value);
  490. Debug.Log("[InfraredDemo] PC 设置 PU_CONTRAST:" + typeStr + "默认值:" + _Value);
  491. slider.value = _Value;
  492. textObj.text = _Value + "";
  493. slider.onValueChanged.AddListener((newValue) =>
  494. {
  495. var _value = newValue;
  496. textObj.text = _value + "";
  497. infraredCameraHelper.SetContrast(_value);
  498. pcCONTRAST.Set(_value);
  499. });
  500. }
  501. }
  502. }
  503. }
  504. void resetPCData() {
  505. for (int i = 0; i < sliderStrArrayPC.Length; i++)
  506. {
  507. string typeStr = sliderStrArrayPC[i];
  508. Transform trans = _cameraParameterPanel.transform.Find(typeStr);
  509. Slider slider = trans.GetComponent<Slider>();
  510. Text textObj = trans.Find("text").GetComponent<Text>();
  511. //重置的值
  512. int _defValue = 0;
  513. if (typeStr == "PU_BRIGHTNESS")
  514. {
  515. _defValue = 0;
  516. infraredCameraHelper.SetBrightness(_defValue);
  517. }
  518. else if (typeStr == "PU_CONTRAST")
  519. {
  520. _defValue = 0;
  521. infraredCameraHelper.SetContrast(_defValue);
  522. }
  523. textObj.text = _defValue + "";
  524. slider.value = _defValue;
  525. }
  526. }
  527. #endregion
  528. void InitInfraredCamera()
  529. {
  530. if (_inited) return;
  531. _inited = true;
  532. //SDK创建
  533. if (infraredCameraHelper == null)
  534. {
  535. infraredCameraHelper = InfraredCameraHelper.GetInstance();
  536. #if UNITY_STANDALONE_WIN || UNITY_EDITOR
  537. infraredCameraHelper.CreateToPc();
  538. #endif
  539. #if UNITY_ANDROID
  540. //开始时候设置一个默认分辨率
  541. Debug.Log("开始时候的分辨率下标:" + resoution.Get());
  542. Vector2 resoutionVec2 = SetDefByIndex((int)resoution.Get());
  543. infraredCameraHelper.Create((int)resoutionVec2.x, (int)resoutionVec2.y);
  544. #endif
  545. infraredCameraHelper.OnPositionUpdate += (Vector2 point) =>
  546. {
  547. //跑九轴时候,不处理这里位置
  548. if (AimHandler.ins && AimHandler.ins.bRuning9Axis()) return;
  549. if (Camera.main == null) return;
  550. Ray ray = Camera.main.ScreenPointToRay(point);
  551. Vector3 rayEndPoint = ray.GetPoint(200);
  552. Quaternion quat = Quaternion.LookRotation(rayEndPoint - Camera.main.transform.position);
  553. // 挑战场景中其相机的父级有旋转,需要换算
  554. if (UnityEngine.SceneManagement.SceneManager.GetActiveScene().name == "GameChallenge")
  555. {
  556. quat = Quaternion.AngleAxis(-180, Vector3.up) * quat;
  557. }
  558. if (CameraToLook.ins != null) CameraToLook.ins.localRotation = quat;
  559. if (SB_EventSystem.ins) SB_EventSystem.ins.MoveSimulateMouseByInfrared(point);
  560. //移动目标游戏
  561. if (GeneratingTarget.gm != null) GeneratingTarget.gm.shootingEvent.OnPositionUpdate(point);
  562. };
  563. infraredCameraHelper.OnUVCIsReady += (UVCManager.CameraInfo camera) =>
  564. {
  565. //生成控制摄像机的参数滑条
  566. Debug.Log("初始化摄像机!");
  567. //可以操作按钮
  568. mBtnSee.interactable = true;
  569. //mBtnSee.transform.Find("Text").GetComponent<Text>().text = "调试红外";
  570. updateBtnSee();
  571. //延迟重新设置一次分辨率
  572. StartCoroutine(delayInitOhterInfo(camera));
  573. };
  574. //屏幕变化时候
  575. infraredCameraHelper.OnUVCPosUpdate += (list) =>
  576. {
  577. Debug.Log("OnUVCPosUpdate list.Count" + list.Count);
  578. SetLocatePointsToCameraRender(list, 1, 1);
  579. //InfraredScreenPositioningView 页面
  580. if (list.Count == 4)
  581. {
  582. FindObjectOfType<InfraredScreenPositioningView>()?.SyncScreenPosition();
  583. }
  584. else {
  585. FindObjectOfType<InfraredScreenPositioningView>()?.onReset();
  586. }
  587. };
  588. }
  589. }
  590. private void updateDropdownResolution2()
  591. {
  592. string[] resolutions = {
  593. TextAutoLanguage2.GetTextByKey("HighResolution"),
  594. TextAutoLanguage2.GetTextByKey("LowResolution")
  595. };
  596. // 清除默认选项
  597. _dropdownResolution2.ClearOptions();
  598. // 将分辨率字符串数组转换为 Dropdown 选项
  599. _dropdownResolution2.AddOptions(new List<string>(resolutions));
  600. }
  601. /// <summary>
  602. /// 初始化一些游戏配置
  603. /// </summary>
  604. /// <returns></returns>
  605. IEnumerator delayInitOhterInfo(UVCManager.CameraInfo camera) {
  606. //yield return new WaitForSeconds(1f);
  607. yield return new WaitForEndOfFrame();
  608. #if UNITY_ANDROID
  609. initSlider(camera);
  610. camera.GetResolutionsStrs();
  611. // 默认设置
  612. updateDropdownResolution2();
  613. int index = (int)resoution.Get();
  614. _dropdownResolution2.value = index;
  615. _dropdownResolution2.RefreshShownValue();
  616. // 处理 Dropdown 的选择变化
  617. _dropdownResolution2.onValueChanged.AddListener(index => { OnResolutionChanged(index); });
  618. #endif
  619. #if UNITY_STANDALONE_WIN || UNITY_EDITOR
  620. initSliderPC();
  621. //隐藏不需要的设置
  622. _btnAdjusting.transform.Find("Text").GetComponent<Text>().text = "调整PC参数";
  623. _dropdownResolution.gameObject.SetActive(false);
  624. _dropdownResolution2.gameObject.SetActive(false);
  625. #endif
  626. //震动阈值
  627. SetShakeFilterValue(shakeFilterValue.Get());
  628. _sliderShakeFilter.onValueChanged.AddListener(SetShakeFilterValue);
  629. //功能按钮
  630. //重置触发测试
  631. _btnReset.onClick.AddListener(OnClick_Reset);
  632. _btnScreenLocateManual.onClick.AddListener(OnClick_ScreenLocateManual);
  633. _btnScreenLocateManualAuto.onClick.AddListener(OnClick_ScreenLocateManualAuto);
  634. SetCaptureValue(captureValue.Get());
  635. _sliderCapture.onValueChanged.AddListener(SetCaptureValue);
  636. SetDelayValue(delayValue.Get());
  637. _sliderDelay.onValueChanged.AddListener(SetDelayValue);
  638. //绘制线段
  639. SetLineWidth(lineWidth.Get());
  640. _sliderLineWidth.onValueChanged.AddListener(SetLineWidth);
  641. SetFanWidth(fanWidth.Get());
  642. _fanWidth.onValueChanged.AddListener(SetFanWidth);
  643. //检测红外亮度阈值
  644. SetInfraredFilterValue(infraredFileterValue.Get());
  645. _infraredFilter.onValueChanged.AddListener(SetInfraredFilterValue);
  646. //野鸭设置初始关卡
  647. int defLevel = (int)duckHunterLevel.Get() - 1 < 0 ? 0 : (int)duckHunterLevel.Get() - 1;
  648. _duckLevelDropdown.SetValueWithoutNotify(defLevel);
  649. _duckLevelDropdown.onValueChanged.AddListener(SetDuckLevelTest);
  650. }
  651. /// <summary>
  652. /// 初始化时候获取设置和存储来控制显示准心
  653. /// </summary>
  654. /// <returns></returns>
  655. public bool bInitCrosshairShow() {
  656. return UserSettings.ins.openCrossHair && getCrosshairValue() == 1 ? true : false;
  657. }
  658. /// <summary>
  659. /// 获取弓箭准心存储值
  660. /// </summary>
  661. /// <returns></returns>
  662. public int getCrosshairValue() {
  663. return (int)crosshairValue.Get();
  664. }
  665. public void setCrosshairValue(bool bshow) {
  666. crosshairValue.Set(bshow ? 1 : 0);
  667. }
  668. public void onStartPreview()
  669. {
  670. infraredCameraHelper.onStartPreview();
  671. }
  672. public void onStopPreview()
  673. {
  674. infraredCameraHelper.onStopPreview();
  675. }
  676. /// <summary>
  677. /// 获取并且初始化一次记录点的数据
  678. /// </summary>
  679. /// <returns></returns>
  680. IEnumerator initScreenLocateManual()
  681. {
  682. yield return new WaitForSeconds(1f);
  683. infraredCameraHelper.InitScreenLocateManual();
  684. }
  685. IEnumerator RestartOrKillApp()
  686. {
  687. yield return new WaitForSeconds(0.3f);
  688. if (Application.isEditor) yield break;
  689. if (Application.platform == RuntimePlatform.Android)
  690. {
  691. using (var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
  692. {
  693. const int kIntent_FLAG_ACTIVITY_CLEAR_TASK = 0x00008000;
  694. const int kIntent_FLAG_ACTIVITY_NEW_TASK = 0x10000000;
  695. var currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
  696. var pm = currentActivity.Call<AndroidJavaObject>("getPackageManager");
  697. var intent = pm.Call<AndroidJavaObject>("getLaunchIntentForPackage", Application.identifier);
  698. intent.Call<AndroidJavaObject>("setFlags", kIntent_FLAG_ACTIVITY_NEW_TASK | kIntent_FLAG_ACTIVITY_CLEAR_TASK);
  699. currentActivity.Call("startActivity", intent);
  700. currentActivity.Call("finish");
  701. var process = new AndroidJavaClass("android.os.Process");
  702. int pid = process.CallStatic<int>("myPid");
  703. process.CallStatic("killProcess", pid);
  704. }
  705. }
  706. else {
  707. // 获取当前应用程序的可执行文件路径
  708. string executablePath = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
  709. // 重启应用程序
  710. System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo
  711. {
  712. FileName = executablePath,
  713. UseShellExecute = false
  714. };
  715. System.Diagnostics.Process.Start(startInfo);
  716. // 退出当前应用程序
  717. Application.Quit();
  718. };
  719. }
  720. void UpdateInfraredCamera()
  721. {
  722. if (!_visiable) return;
  723. if (!_inited) return;
  724. if (infraredCameraHelper == null) return;
  725. //渲染相机画面
  726. //_cameraRender.texture = infraredCameraHelper.GetCameraTexture();
  727. //_cameraRender.material = infraredCameraHelper.GetCameraMaterial();
  728. if (ScreenLocate.Main.getUVCTexture)
  729. {
  730. if (isFullscreen || ScreenLocate.Main.OutputTextures[4] == null)
  731. {
  732. if (_cameraRender.texture == null || ScreenLocate.Main.getUVCTexture.GetNativeTexturePtr() != _cameraRender.texture.GetNativeTexturePtr())
  733. _cameraRender.texture = ScreenLocate.Main.getUVCTexture;
  734. //ScreenLocate.Main.OutputTextures[4] == null 并且 摄像机缩小的情况下。显示适配线段
  735. if (!isFullscreen) SetAllToggle(true);
  736. }
  737. else if (ScreenLocate.Main.OutputTextures[4] != null)
  738. {
  739. //缩小的情况下。显示黑白色差的合成图
  740. if (_cameraRender.texture == null || ScreenLocate.Main.OutputTextures[4] != null && ScreenLocate.Main.OutputTextures[4].GetNativeTexturePtr() != _cameraRender.texture.GetNativeTexturePtr())
  741. _cameraRender.texture = ScreenLocate.Main.OutputTextures[4];
  742. //底图缩小的情况下。不显示适配线段,并且隐藏points
  743. if (!isFullscreen) {
  744. if (showPoints) {
  745. Transform pointsTF2 = _cameraRender.transform.Find("Points");
  746. for (int i = 0; i < pointsTF2.childCount; i++)
  747. {
  748. Transform pointTF = pointsTF2.GetChild(i);
  749. pointTF.gameObject.SetActive(false);
  750. showPoints = false;
  751. }
  752. }
  753. SetAllToggle(false);
  754. }
  755. }
  756. //_cameraRender.SetNativeSize();
  757. // _MaintainAspectRatio.AdjustSize();
  758. }
  759. //在相机画面显示准心
  760. if (ScreenLocate.Main)
  761. {
  762. var _sl = ScreenLocate.Main;
  763. var buffer = _sl.infraredSpotBuffer;
  764. if (buffer != null)
  765. {
  766. for (int i = 0; i < buffer.Length; i++)
  767. {
  768. if (buffer[i].CameraLocation != null)
  769. {
  770. //添加一个偏移量,使得最后输出的准心是指向正中心
  771. Vector2 newPoint2 = _sl.GetOffsetCameraLocation(buffer[i].CameraLocation.Value);
  772. // 检测到光点
  773. var pos = newPoint2.pixelToLocalPosition_AnchorCenter(_sl.mUVCCameraInfo.Size, _cameraRender.rectTransform.rect);
  774. _crosshairsInCamera[i].gameObject.SetActive(true);
  775. _crosshairsInCamera[i].anchoredPosition = pos;
  776. }
  777. else
  778. _crosshairsInCamera[i].gameObject.SetActive(false);
  779. }
  780. }
  781. //渲染固定摄像机分辨率
  782. SetCalibrationFixedText(_sl.mUVCCameraInfo.CurrentCalibrationResolution);
  783. //渲染摄像机大小
  784. if (_sl.mUVCCameraInfo != null) SetCameraSizeText(_sl.mUVCCameraInfo.Size);
  785. //渲染摄像机识别点位置
  786. SetQuadUnityVectorListText(_sl.PrintVector2List(ScreenLocate.quadUnityVectorList));
  787. }
  788. }
  789. bool showPoints = false;
  790. public void SetLocatePointsToCameraRender(List<Vector2> points, float w, float h)
  791. {
  792. Transform pointsTF2 = _cameraRender.transform.Find("Points");
  793. if (pointsTF2.childCount == points.Count)
  794. {
  795. Vector2 texSize = new Vector2(w, h);
  796. for (int i = 0; i < pointsTF2.childCount; i++)
  797. {
  798. Transform pointTF = pointsTF2.GetChild(i);
  799. Vector2 pos = points[i];
  800. pointTF.localPosition = pos.pixelToLocalPosition_AnchorCenter(texSize, _cameraRender.rectTransform.rect);
  801. pointTF.gameObject.SetActive(true);
  802. showPoints = true;
  803. }
  804. }
  805. else {
  806. for (int i = 0; i < pointsTF2.childCount; i++)
  807. {
  808. Transform pointTF = pointsTF2.GetChild(i);
  809. pointTF.gameObject.SetActive(false);
  810. showPoints = false;
  811. }
  812. }
  813. //绘制数据
  814. DrawTestLine();
  815. }
  816. #region 测试数据绘制
  817. [SerializeField] private Toggle curToggle;
  818. [SerializeField] private Toggle autoToggle; // 控制 AutoUILineGenerator 的 Toggle
  819. [SerializeField] private Toggle semiAutoToggle; // 控制 SemiAutoUILineGenerator 的 Toggle
  820. [SerializeField] private Toggle manualToggle; // 控制 ManualUILineGenerator 的 Toggle
  821. [SerializeField] LineGenerator CurUILineGenerator;//当前结果屏幕线条
  822. [SerializeField] LineGenerator AutoUILineGenerator;//第一个结果屏幕线条
  823. [SerializeField] LineGenerator SemiAutoUILineGenerator;//第二个结果屏幕线条
  824. [SerializeField] LineGenerator ManualUILineGenerator;//第三个结果屏幕线条
  825. [SerializeField] private Dropdown dropdown; // 引用场景中的 Dropdown 组件
  826. [SerializeField] RawImage _cameraRenderTest5;
  827. [SerializeField] RawImage _cameraRenderTest6;
  828. void DrawTestLine()
  829. {
  830. Vector2 texSize = ScreenLocate.Main.getUVCCameraInfoSize;
  831. // 判断 texSize 是否为有效值
  832. if (texSize == Vector2.zero)
  833. {
  834. Debug.LogWarning("Texture size is zero, cannot proceed with line drawing.");
  835. return;
  836. }
  837. // 设置三个屏幕四边形和线条生成器
  838. QuadrilateralInCamera[] screenQuads = new QuadrilateralInCamera[] {
  839. ScreenLocate.Main.ScreenIdentification?.QuadAuto,
  840. ScreenLocate.Main.ScreenIdentification?.QuadSemiAuto,
  841. ScreenLocate.Main.ScreenIdentification?.QuadManual
  842. };
  843. LineGenerator[] lineGenerators = new LineGenerator[] {
  844. AutoUILineGenerator,
  845. SemiAutoUILineGenerator,
  846. ManualUILineGenerator
  847. };
  848. for (int i = 0; i < screenQuads.Length; i++)
  849. {
  850. if (screenQuads[i] == null)
  851. {
  852. Debug.LogWarning($"Screen quadrilateral {i} is null, skipping this line.");
  853. continue;
  854. }
  855. if (lineGenerators[i] == null)
  856. {
  857. Debug.LogWarning($"Line generator {i} is null, skipping this line.");
  858. continue;
  859. }
  860. lineGenerators[i].Points = ConvertQuadToPoints(screenQuads[i], texSize);
  861. }
  862. // 清除旧选项并填充新的选项
  863. dropdown.ClearOptions();
  864. List<string> options = new List<string>();
  865. //for (int i = 0; i < screenQuads.Length; i++)
  866. //{
  867. // options.Add($"Quad {i + 1}");
  868. //}
  869. //options.Add("自动识别");
  870. //options.Add("半自动识别");
  871. //options.Add("手动识别");
  872. options.Add(TextAutoLanguage2.GetTextByKey("AutoRecognition")); // "自动识别"
  873. options.Add(TextAutoLanguage2.GetTextByKey("SemiAutoRecognition")); // "半自动识别"
  874. options.Add(TextAutoLanguage2.GetTextByKey("ManualRecognition")); // "手动识别"
  875. dropdown.AddOptions(options);
  876. // 添加监听器来处理选择事件
  877. dropdown.onValueChanged.AddListener((index) => {
  878. OnDropdownValueChanged(index, screenQuads);
  879. });
  880. if (screenQuads[0] != null || screenQuads[1] != null)
  881. {
  882. Texture texImage6 = ScreenLocate.Main.OutputTextures[5];
  883. if (texImage6 != null) _cameraRenderTest6.texture = texImage6;
  884. }
  885. if (ScreenLocate.quadUnityVectorList.Count == 4) {
  886. //绘制白色线段
  887. var lo = new Vector2(-0.5f, -0.5f);
  888. CurUILineGenerator.Points = new Vector2[4] {
  889. 2 * (ScreenLocate.quadUnityVectorList[0] + lo),
  890. 2 * (ScreenLocate.quadUnityVectorList[1] + lo),
  891. 2 * (ScreenLocate.quadUnityVectorList[3] + lo),
  892. 2 * (ScreenLocate.quadUnityVectorList[2] + lo) };
  893. }
  894. }
  895. // 当下拉框选项更改时调用的方法
  896. private void OnDropdownValueChanged(int index, QuadrilateralInCamera[] screenQuads)
  897. {
  898. if (index >= 0 && index < screenQuads.Length)
  899. {
  900. if (screenQuads[index] != null)
  901. {
  902. QuadrilateralInCamera selectedQuad = screenQuads[index];
  903. Debug.Log($"Selected Quad {index + 1}: {selectedQuad}");
  904. CurUILineGenerator.Points = ConvertQuadToPoints(selectedQuad, ScreenLocate.Main.getUVCCameraInfoSize);
  905. }
  906. else
  907. {
  908. Debug.LogWarning($"Selected Quad {index + 1} is null.");
  909. }
  910. }
  911. else
  912. {
  913. Debug.LogWarning("所选索引超出范围!");
  914. }
  915. }
  916. /// <summary>
  917. /// 提取转换四边形的方法
  918. /// </summary>
  919. /// <param name="quad"></param>
  920. /// <param name="texSize"></param>
  921. /// <returns></returns>
  922. public Vector2[] ConvertQuadToPoints(QuadrilateralInCamera quad, Vector2 texSize)
  923. {
  924. if (quad == null || quad.Quad == null || quad.Quad.Count < 4)
  925. {
  926. Debug.LogWarning("Invalid quadrilateral data provided.");
  927. return new Vector2[0]; // 返回空数组以避免异常
  928. }
  929. Vector2 offset = new Vector2(-0.5f, -0.5f); // 假设 'lo' 是某个已定义的偏移量
  930. return new Vector2[4] {
  931. 2 * (new Vector2(quad.Quad[0].x / texSize.x, quad.Quad[0].y / texSize.y) + offset),
  932. 2 * (new Vector2(quad.Quad[1].x / texSize.x, quad.Quad[1].y / texSize.y) + offset),
  933. 2 * (new Vector2(quad.Quad[3].x / texSize.x, quad.Quad[3].y / texSize.y) + offset),
  934. 2 * (new Vector2(quad.Quad[2].x / texSize.x, quad.Quad[2].y / texSize.y) + offset)
  935. };
  936. }
  937. public Vector2[] ConvertPointsToCoordinates(Vector2[] points, Vector2 texSize, Vector2 pivot)
  938. {
  939. if (points == null || points.Length < 1)
  940. {
  941. Debug.LogWarning("Invalid point data provided.");
  942. return new Vector2[0]; // 返回空数组以避免异常
  943. }
  944. Vector2 offset = new Vector2(-0.5f, -0.5f); // 将坐标从中心对齐到左下角对齐
  945. List<Vector2> transformedPoints = new List<Vector2>();
  946. // 遍历所有传入的点进行转换
  947. foreach (var point in points)
  948. {
  949. // 转换每个点,考虑 texSize 和 pivot 偏移
  950. Vector2 transformedPoint = 2 * ((point / texSize) + offset + pivot);
  951. transformedPoints.Add(transformedPoint);
  952. }
  953. return transformedPoints.ToArray();
  954. }
  955. void initToggle()
  956. {
  957. // 监听 Toggle 值的变化
  958. if (curToggle != null)
  959. curToggle.onValueChanged.AddListener(isOn => ToggleLineGenerator(CurUILineGenerator, isOn));
  960. // 监听 Toggle 值的变化
  961. if (autoToggle != null)
  962. autoToggle.onValueChanged.AddListener(isOn => ToggleLineGenerator(AutoUILineGenerator, isOn));
  963. if (semiAutoToggle != null)
  964. semiAutoToggle.onValueChanged.AddListener(isOn => ToggleLineGenerator(SemiAutoUILineGenerator, isOn));
  965. if (manualToggle != null)
  966. manualToggle.onValueChanged.AddListener(isOn => ToggleLineGenerator(ManualUILineGenerator, isOn));
  967. }
  968. // 控制 LineGenerator 的启用/禁用
  969. private void ToggleLineGenerator(LineGenerator lineGenerator, bool isOn)
  970. {
  971. if (lineGenerator != null && lineGenerator.enabled != isOn)
  972. {
  973. lineGenerator.enabled = isOn;
  974. Debug.Log($"{lineGenerator.name} is now {(isOn ? "enabled" : "disabled")}");
  975. }
  976. }
  977. /// <summary>
  978. /// 设置全部显示
  979. /// </summary>
  980. /// <param name="value"></param>
  981. private void SetAllToggle(bool value) {
  982. ToggleLineGenerator(CurUILineGenerator, value);
  983. ToggleLineGenerator(AutoUILineGenerator, value);
  984. ToggleLineGenerator(SemiAutoUILineGenerator, value);
  985. ToggleLineGenerator(ManualUILineGenerator, value);
  986. }
  987. /// <summary>
  988. /// 放大时候同步一次toggle
  989. /// </summary>
  990. private void SyncAllToggle()
  991. {
  992. ToggleLineGenerator(CurUILineGenerator, curToggle.isOn);
  993. ToggleLineGenerator(AutoUILineGenerator, autoToggle.isOn);
  994. ToggleLineGenerator(SemiAutoUILineGenerator, semiAutoToggle.isOn);
  995. ToggleLineGenerator(ManualUILineGenerator, manualToggle.isOn);
  996. }
  997. // 下拉菜单用于显示纹理名称
  998. public Dropdown textureDropdown;
  999. // 按钮用于导出选中的纹理
  1000. public Button exportSelectedButton;
  1001. // 按钮用于导出所有纹理
  1002. public Button exportAllButton;
  1003. // 定义纹理名称
  1004. string[] textureNames;
  1005. /// <summary>
  1006. /// 更新一下菜单
  1007. /// </summary>
  1008. public void UpdateLanguage() {
  1009. string[] _textureNames = {
  1010. TextAutoLanguage2.GetTextByKey("ScreenAOriginal"), // "A屏幕原图"
  1011. TextAutoLanguage2.GetTextByKey("SemiAutoSegmentB"), // "B半自动识别线段"
  1012. TextAutoLanguage2.GetTextByKey("BlackWhiteContrastC"), // "C黑白色差"
  1013. TextAutoLanguage2.GetTextByKey("RecognitionResultD"), // "D识别结果"
  1014. TextAutoLanguage2.GetTextByKey("ColorContrastOverlayE"), // "E色差叠加识别结果"
  1015. TextAutoLanguage2.GetTextByKey("SemiAutoAlternativeF") // "F半自动备选线段"
  1016. };
  1017. textureNames = _textureNames;
  1018. // 设置下拉菜单的选项
  1019. textureDropdown.ClearOptions();
  1020. textureDropdown.AddOptions(new System.Collections.Generic.List<string>(textureNames));
  1021. }
  1022. void initExport() {
  1023. UpdateLanguage();
  1024. // 添加导出选中纹理的按钮点击事件
  1025. exportSelectedButton.onClick.AddListener(() => SaveTextureToLocal(textureDropdown.value));
  1026. // 添加导出所有纹理的按钮点击事件
  1027. exportAllButton.onClick.AddListener(SaveAllTexturesToLocal);
  1028. }
  1029. /// <summary>
  1030. /// 导出所有纹理
  1031. /// </summary>
  1032. public void SaveAllTexturesToLocal()
  1033. {
  1034. for (int i = 0; i < textureNames.Length; i++)
  1035. {
  1036. SaveTextureToLocal(i); // 调用之前定义的保存方法
  1037. }
  1038. }
  1039. /// <summary>
  1040. /// 导出图片
  1041. /// </summary>
  1042. /// <param name="index"></param>
  1043. public void SaveTextureToLocal(int index)
  1044. {
  1045. Texture2D[] outputTexture2D = ScreenLocate.Main.OutputTextures;
  1046. if (outputTexture2D == null || index >= outputTexture2D.Length || outputTexture2D[index] == null)
  1047. {
  1048. Debug.LogError("Invalid texture index or texture is null.index:"+ index);
  1049. return;
  1050. }
  1051. // 定义保存路径
  1052. string time = DateTime.Now.ToString("yyyyMMdd_HHmmss");
  1053. string baseName = $"{time}_Texture_{index}";
  1054. // 检查索引是否在命名数组内
  1055. string fileName = index < textureNames.Length ? textureNames[index] : $"Texture_{index}";
  1056. #if UNITY_ANDROID && !UNITY_EDITOR_WIN
  1057. string path = System.IO.Path.Combine("/storage/emulated/0/Download", $"{baseName}_{fileName}.png"); //public
  1058. #else
  1059. string path = System.IO.Path.Combine(Application.persistentDataPath, $"{baseName}_{fileName}.png");
  1060. #endif
  1061. // 编码纹理为 PNG 格式
  1062. byte[] bytes = outputTexture2D[index].EncodeToPNG();
  1063. // 将字节写入文件
  1064. System.IO.File.WriteAllBytes(path, bytes);
  1065. Debug.Log($"Texture saved as: {path}");
  1066. }
  1067. //下面是处理对象放大缩小
  1068. public RectTransform cameraObj1; // 拖拽你的 CameraSize1 对象到此字段
  1069. public Button toggleButton; // 缩小时使用的按钮
  1070. private Vector2 originalSize;
  1071. private Vector2 originalAnchoredPosition; // 使用 anchoredPosition 代替 localPosition
  1072. private Vector2 originalAnchorMin;
  1073. private Vector2 originalAnchorMax;
  1074. private int originalSiblingIndex; // 保存初始的层级索引
  1075. private bool isFullscreen = false;
  1076. /// <summary>
  1077. /// 测试效果屏幕数据初始化
  1078. /// </summary>
  1079. void InitFullScreen() {
  1080. if (cameraObj1 != null)
  1081. {
  1082. // 保存初始大小、位置和层级索引
  1083. originalSize = cameraObj1.sizeDelta;
  1084. originalAnchoredPosition = cameraObj1.anchoredPosition;
  1085. originalAnchorMin = cameraObj1.anchorMin; // 记录初始 anchorMin
  1086. originalAnchorMax = cameraObj1.anchorMax; // 记录初始 anchorMax
  1087. originalSiblingIndex = cameraObj1.GetSiblingIndex();
  1088. // 为 cameraObj1 添加点击事件
  1089. cameraObj1.GetComponent<Button>().onClick.AddListener(ToggleFullscreen);
  1090. toggleButton.transform.parent.gameObject.SetActive(false);
  1091. }
  1092. if (toggleButton != null)
  1093. {
  1094. // 为按钮添加点击事件以恢复大小
  1095. toggleButton.onClick.AddListener(RestoreSize);
  1096. }
  1097. }
  1098. void ToggleFullscreen()
  1099. {
  1100. if (cameraObj1 == null) return;
  1101. if (!isFullscreen)
  1102. {
  1103. // 设置锚点为全屏,调整位置和偏移量
  1104. cameraObj1.anchorMin = Vector2.zero;
  1105. cameraObj1.anchorMax = Vector2.one;
  1106. cameraObj1.offsetMin = Vector2.zero;
  1107. cameraObj1.offsetMax = Vector2.zero;
  1108. cameraObj1.anchoredPosition = Vector3.zero;
  1109. cameraObj1.SetSiblingIndex(cameraObj1.parent.childCount - 1); // 设置为最上层
  1110. //刷新一次点
  1111. SetLocatePointsToCameraRender(ScreenLocate.quadUnityVectorList, 1, 1);
  1112. toggleButton.transform.parent.gameObject.SetActive(true);
  1113. isFullscreen = true;
  1114. //放大情况根据toggle来显示
  1115. SyncAllToggle();
  1116. }
  1117. }
  1118. void RestoreSize()
  1119. {
  1120. if (cameraObj1 == null || !isFullscreen) return;
  1121. // 恢复锚点和偏移量
  1122. cameraObj1.anchorMin = originalAnchorMin; // 使用在 Start() 中记录的初始锚点
  1123. cameraObj1.anchorMax = originalAnchorMax; // 使用在 Start() 中记录的初始锚点
  1124. cameraObj1.sizeDelta = originalSize;
  1125. cameraObj1.anchoredPosition = originalAnchoredPosition; // 恢复为初始 anchoredPosition
  1126. cameraObj1.SetSiblingIndex(originalSiblingIndex); // 恢复到原本的层级索引
  1127. //刷新一次点
  1128. SetLocatePointsToCameraRender(ScreenLocate.quadUnityVectorList, 1, 1);
  1129. toggleButton.transform.parent.gameObject.SetActive(false);
  1130. isFullscreen = false;
  1131. }
  1132. #endregion
  1133. public void SetLocatePointsToCameraRenderTest(List<Vector2> points, float w, float h)
  1134. {
  1135. Transform pointsTF2 = _cameraRender.transform.Find("PointsTest");
  1136. if (pointsTF2.childCount == points.Count)
  1137. {
  1138. Vector2 texSize = new Vector2(w, h);
  1139. for (int i = 0; i < pointsTF2.childCount; i++)
  1140. {
  1141. Transform pointTF = pointsTF2.GetChild(i);
  1142. Vector2 pos = points[i];
  1143. pointTF.localPosition = pos.pixelToLocalPosition_AnchorCenter(texSize, _cameraRender.rectTransform.rect);
  1144. pointTF.gameObject.SetActive(true);
  1145. }
  1146. }
  1147. else
  1148. {
  1149. for (int i = 0; i < pointsTF2.childCount; i++)
  1150. {
  1151. Transform pointTF = pointsTF2.GetChild(i);
  1152. pointTF.gameObject.SetActive(false);
  1153. }
  1154. }
  1155. }
  1156. void SetShakeFilterValue(float v)
  1157. {
  1158. shakeFilterValue.Set(v);
  1159. _sliderShakeFilter.SetValueWithoutNotify(shakeFilterValue.Get());
  1160. _sliderShakeFilter.transform.Find("Value").GetComponent<Text>().text = shakeFilterValue.Get().ToString("f1");
  1161. infraredCameraHelper.SetShakeFilterValue(shakeFilterValue.Get());
  1162. }
  1163. /// <summary>
  1164. /// 重新设置分辨率
  1165. /// </summary>
  1166. /// <param name="index"></param>
  1167. public void resolutionRestartApp(int index)
  1168. {
  1169. #if UNITY_ANDROID
  1170. //StartCoroutine(RestartOrKillApp());
  1171. //获取第一个分辨率设置后设置size
  1172. switch (index)
  1173. {
  1174. case 0:
  1175. infraredCameraHelper.SetHighCameraResolution();
  1176. resoution.Set(index);
  1177. _dropdownResolution2.SetValueWithoutNotify(index);
  1178. break;
  1179. case 1:
  1180. infraredCameraHelper.SetLowCameraResolution();
  1181. resoution.Set(index);
  1182. _dropdownResolution2.SetValueWithoutNotify(index);
  1183. break;
  1184. }
  1185. #endif
  1186. }
  1187. /// <summary>
  1188. /// 初始化时候,设置的值
  1189. /// </summary>
  1190. /// <param name="index"></param>
  1191. public Vector2 SetDefByIndex(int index = 0)
  1192. {
  1193. //SetResolutionNew 里面对应的下标
  1194. Vector2 vec2 = new Vector2(320, 240);
  1195. switch (index)
  1196. {
  1197. case 0:
  1198. vec2 = new Vector2(320, 240);
  1199. break;
  1200. case 1:
  1201. vec2 = new Vector2(160, 120);
  1202. break;
  1203. }
  1204. return vec2;
  1205. }
  1206. /// <summary>
  1207. /// 当 Dropdown 选择变化时的处理方法
  1208. /// </summary>
  1209. /// <param name="OnResolutionChanged">320x240</param>
  1210. private void OnResolutionChanged(int index)
  1211. {
  1212. #if UNITY_ANDROID
  1213. Debug.Log("Selected Resolution: " + index);
  1214. // 将选定的分辨率转换为实际的宽度和高度
  1215. // 并且需要判断是否存在最低分辨率
  1216. if (index == 1 && currentCameraInfo.ContainsResulutionKey("160x120"))
  1217. {
  1218. // 在这里你可以根据实际需要设置屏幕分辨率或进行其他操作
  1219. infraredCameraHelper.SetLowCameraResolution();
  1220. // 保存选择的分辨率到 PlayerPrefs
  1221. resoution.Set(index);
  1222. }
  1223. else if(index == 0){
  1224. //如果是0就 使用高分辨率
  1225. infraredCameraHelper.SetHighCameraResolution();
  1226. // 保存选择的分辨率到 PlayerPrefs
  1227. resoution.Set(index);
  1228. }
  1229. #endif
  1230. }
  1231. /// <summary>
  1232. /// 野鸭关卡测试初始值
  1233. /// </summary>
  1234. /// <param name="optionIndex"></param>
  1235. public void SetDuckLevelTest(int optionIndex)
  1236. {
  1237. //选择下标+1
  1238. int v = optionIndex + 1;
  1239. duckHunterLevel.Set(v);
  1240. int defLevel = (int)duckHunterLevel.Get() - 1 < 0 ? 0 : (int)duckHunterLevel.Get() - 1;
  1241. _duckLevelDropdown.SetValueWithoutNotify(defLevel);
  1242. //_duckLevelDropdown.transform.Find("Value").GetComponent<Text>().text = defLevel+"";
  1243. }
  1244. void OnClick_Reset()
  1245. {
  1246. //SetBrightness(1);
  1247. //SetSaturation(1);
  1248. //SetContrast(1);
  1249. SetShakeFilterValue(6);
  1250. SetLineWidth(4);
  1251. SetFanWidth(90);
  1252. SetInfraredFilterValue(0.8f);
  1253. SetCaptureValue(30.0f);
  1254. SetDelayValue(30.0f);
  1255. //重置为第一关
  1256. SetDuckLevelTest(0);
  1257. }
  1258. public void OnClickClearAll() {
  1259. PlayerPrefs.DeleteAll();
  1260. // 保存更改
  1261. PlayerPrefs.Save();
  1262. StartCoroutine(RestartOrKillApp());
  1263. }
  1264. void OnClick_ScreenLocateManual()
  1265. {
  1266. bool bEnter = true;
  1267. if (BluetoothAim.ins && BluetoothAim.ins.status == BluetoothStatusEnum.ConnectSuccess)
  1268. {
  1269. if (BluetoothAim.ins.isMainConnectToInfraredDevice() || BluetoothAim.ins.isMainConnectToGun())
  1270. {
  1271. bEnter = true;
  1272. }
  1273. else
  1274. {
  1275. bEnter = false;
  1276. //只有红外设备才能进行屏幕识别
  1277. PopupMgr.ins.ShowTipTop(TextAutoLanguage2.GetTextByKey("Connect_Tip_Infrared"));
  1278. }
  1279. }
  1280. if (bEnter)
  1281. {
  1282. ViewManager2.ShowView(ViewManager2.Path_InfraredScreenPositioningView);
  1283. FindObjectOfType<InfraredScreenPositioningView>().enterFromInfraredDemo = true;
  1284. }
  1285. }
  1286. void OnClick_ScreenLocateManualAuto()
  1287. {
  1288. bool bEnter = true;
  1289. if (BluetoothAim.ins && BluetoothAim.ins.status == BluetoothStatusEnum.ConnectSuccess)
  1290. {
  1291. if (BluetoothAim.ins.isMainConnectToInfraredDevice() || BluetoothAim.ins.isMainConnectToGun())
  1292. {
  1293. bEnter = true;
  1294. }
  1295. else
  1296. {
  1297. bEnter = false;
  1298. //只有红外设备才能进行屏幕识别
  1299. PopupMgr.ins.ShowTipTop(TextAutoLanguage2.GetTextByKey("Connect_Tip_Infrared"));
  1300. }
  1301. }
  1302. if (bEnter) {
  1303. ResetCenterOffset();
  1304. ResetPositioningData();
  1305. infraredCameraHelper.EnterScreenLocateManualAuto();
  1306. }
  1307. }
  1308. public void OnClick_SetAdjustPointsOffset()
  1309. {
  1310. var _sl = ScreenLocate.Main;
  1311. var buffer = _sl.infraredSpotBuffer;
  1312. if (buffer != null)
  1313. {
  1314. for (int i = 0; i < buffer.Length; i++)
  1315. {
  1316. if (buffer[i].CameraLocation != null)
  1317. {
  1318. Debug.Log("CameraLocation:"+buffer[i].CameraLocation.Value);
  1319. var centerOffset = infraredCameraHelper.GetCenterOffset(buffer[i].CameraLocation.Value, "CameraLocation");
  1320. Debug.Log("CenterOffset: " + centerOffset);
  1321. var uvCenterOffset = infraredCameraHelper.GetCenterOffset(buffer[i].ScreenUV.Value, "ScreenUV");
  1322. Debug.Log("UvCenterOffset: " + uvCenterOffset);
  1323. //如果是新手教程场景里面的校准
  1324. InfraredGuider infraredGuiderObj = FindObjectOfType<InfraredGuider>();
  1325. if (infraredGuiderObj != null)
  1326. {
  1327. GameObject infraredGuiderGameObject = infraredGuiderObj.gameObject;
  1328. infraredGuiderGameObject.GetComponent<InfraredGuider>().SetTitleAfterCalibration();
  1329. }
  1330. }
  1331. }
  1332. }
  1333. }
  1334. /// <summary>
  1335. /// 重置中心点
  1336. /// </summary>
  1337. public void ResetCenterOffset() {
  1338. infraredCameraHelper.ResetCenterOffset();
  1339. }
  1340. /// <summary>
  1341. /// 撤销中心点
  1342. /// </summary>
  1343. public void RevokeCenterOffset()
  1344. {
  1345. infraredCameraHelper.RevokeCenterOffset();
  1346. }
  1347. /// <summary>
  1348. /// 重置定位数据
  1349. /// </summary>
  1350. public void ResetPositioningData(bool bSyncLocal = false) {
  1351. infraredCameraHelper.ClearInfraredPositioningData(bSyncLocal);
  1352. }
  1353. void SetCaptureValue(float v)
  1354. {
  1355. captureValue.Set(v);
  1356. _sliderCapture.SetValueWithoutNotify(captureValue.Get());
  1357. _sliderCapture.transform.Find("Value").GetComponent<Text>().text = captureValue.Get().ToString("f1");
  1358. infraredCameraHelper.SetCapture((int)captureValue.Get());
  1359. }
  1360. void SetDelayValue(float v)
  1361. {
  1362. delayValue.Set(v);
  1363. _sliderDelay.SetValueWithoutNotify(delayValue.Get());
  1364. _sliderDelay.transform.Find("Value").GetComponent<Text>().text = delayValue.Get().ToString("f1");
  1365. infraredCameraHelper.SetDelay((int)delayValue.Get());
  1366. }
  1367. #endregion
  1368. #region 相机感光度(默认修改对比度)
  1369. public void onSetSliderCustomValue(Slider _slider, float min = 0.0f, float max = 10.0f)
  1370. {
  1371. onSetSliderValue(_slider, "PU_CONTRAST", min, max);
  1372. }
  1373. public void onSliderCustomEvent(float value, float min = 0.0f, float max = 10.0f)
  1374. {
  1375. onSliderEvent(value, "PU_CONTRAST", min, max);
  1376. }
  1377. public void onSetSliderCustomBrightnessValue(Slider _slider, float min = 0.0f, float max = 10.0f)
  1378. {
  1379. onSetSliderValue(_slider, "PU_BRIGHTNESS", min, max);
  1380. }
  1381. public void onSliderCustomBrightnessEvent(float value, float min = 0.0f, float max = 10.0f)
  1382. {
  1383. onSliderEvent(value, "PU_BRIGHTNESS", min, max);
  1384. }
  1385. public void onSetSliderValue(Slider _slider,string typeStr = "PU_CONTRAST", float min = 0.0f, float max = 10.0f)
  1386. {
  1387. if (BluetoothWindows.IsWindows()) {
  1388. float v = typeStr == "PU_CONTRAST"?infraredCameraHelper.GetContrast(): infraredCameraHelper.GetBrightness();
  1389. // 目标区间 [0, 10] 的边界值
  1390. double targetMin = min;
  1391. double targetMax = max;
  1392. double originalMin = -1;
  1393. double originalMax = 1;
  1394. // 计算转换后的值
  1395. double v2 = MapValue(v, originalMin, originalMax, targetMin, targetMax);
  1396. Debug.Log("PC获取相机的感光度 "+ typeStr + " :" + v + " = " + v2);
  1397. _slider.SetValueWithoutNotify((float)v2);
  1398. return;
  1399. }
  1400. //功能也改为UVC的对比度
  1401. if (running)
  1402. {
  1403. //string typeStr = "PU_CONTRAST";//"PU_BRIGHTNESS";
  1404. //UInt64 _VALUE = currentCameraInfo.GetTypeByName(typeStr);
  1405. UVCCtrlInfo _UVCCtrlInfo = currentCameraInfo.GetInfo(typeStr);
  1406. float v = currentCameraInfo.GetValue(typeStr); // infraredCameraHelper.GetBrightness();
  1407. //float v2 = (v / _UVCCtrlInfo.max) * 10;
  1408. // 目标区间 [0, 10] 的边界值
  1409. double targetMin = min;
  1410. double targetMax = max;
  1411. double originalMin = _UVCCtrlInfo.min;
  1412. double originalMax = _UVCCtrlInfo.max;
  1413. // 计算转换后的值
  1414. double v2 = MapValue(v, originalMin, originalMax, targetMin, targetMax);
  1415. Debug.Log("获取相机的感光度" + typeStr + ": " + v + " = " + v2);
  1416. _slider.SetValueWithoutNotify((float)v2);
  1417. }
  1418. else _slider.SetValueWithoutNotify(5);
  1419. }
  1420. public void onSliderEvent(float value, string typeStr = "PU_CONTRAST", float min = 0.0f,float max = 10.0f)
  1421. {
  1422. // string typeStr = "PU_CONTRAST";// "PU_BRIGHTNESS";
  1423. //pc
  1424. if (BluetoothWindows.IsWindows())
  1425. {
  1426. Transform trans = _cameraParameterPanel.transform.Find(typeStr);
  1427. Slider slider = trans.GetComponent<Slider>();
  1428. Text textObj = trans.Find("text").GetComponent<Text>();
  1429. // 原始区间和目标区间的边界值
  1430. double originalMin = min;
  1431. double originalMax = max;
  1432. double targetMin = -1;
  1433. double targetMax = 1;
  1434. // 计算转换后的值
  1435. double result = MapValue(value, originalMin, originalMax, targetMin, targetMax);
  1436. float _current = (float)result;
  1437. //Debug.Log("_current:" + _current + " , result:" + result);
  1438. textObj.text = _current + "";
  1439. slider.value = _current;
  1440. if (typeStr == "PU_CONTRAST")
  1441. {
  1442. infraredCameraHelper.SetContrast(_current);
  1443. pcCONTRAST.Set(_current);
  1444. }
  1445. else {
  1446. infraredCameraHelper.SetBrightness(_current);
  1447. pcBRIGHTNESS.Set(_current);
  1448. }
  1449. return;
  1450. }
  1451. //修改亮度时,调试界面的亮度也应该一起修改
  1452. //功能也改为UVC的对比度
  1453. if (running)
  1454. {
  1455. //Slider slider = transform.Find("InfraredCamera/Layout/SliderBrightness")
  1456. // .GetComponent<Slider>();
  1457. // UInt64 _VALUE = currentCameraInfo.GetTypeByName(typeStr);
  1458. Transform trans = _cameraParameterPanel.transform.Find(typeStr);
  1459. Slider slider = trans.GetComponent<Slider>();
  1460. Text textObj = trans.Find("text").GetComponent<Text>();
  1461. UVCCtrlInfo _UVCCtrlInfo = currentCameraInfo.GetInfo(typeStr);
  1462. int _currentUVCValue = currentCameraInfo.GetValue(typeStr);
  1463. //value 0 ~ 10
  1464. // 原始区间和目标区间的边界值
  1465. double originalMin = min;
  1466. double originalMax = max;
  1467. double targetMin = _UVCCtrlInfo.min;
  1468. double targetMax = _UVCCtrlInfo.max;
  1469. // 计算转换后的值
  1470. double result = MapValue(value, originalMin, originalMax, targetMin, targetMax);
  1471. int _current = (int)(result);
  1472. Debug.Log("_current:" + value + " , result:" + result);
  1473. textObj.text = _current + "";
  1474. slider.value = _current;
  1475. //dUVCParameters.GetValueOrDefault(typeStr).Set(_current);
  1476. //存储初始值,设置一次到UVC参数
  1477. if (_currentUVCValue != _current) currentCameraInfo.SetValue(typeStr, _current);
  1478. }
  1479. }
  1480. public static double MapValue(double value, double originalMin, double originalMax, double targetMin, double targetMax)
  1481. {
  1482. // 线性插值公式
  1483. return targetMin + (value - originalMin) * (targetMax - targetMin) / (originalMax - originalMin);
  1484. }
  1485. #endregion
  1486. #region 绘制线段部分
  1487. public void SetLineWidth(float v)
  1488. {
  1489. lineWidth.Set(v);
  1490. _sliderLineWidth.SetValueWithoutNotify(lineWidth.Get());
  1491. _sliderLineWidth.transform.Find("Value").GetComponent<Text>().text = lineWidth.Get().ToString("f1");
  1492. }
  1493. public void SetFanWidth(float v)
  1494. {
  1495. fanWidth.Set(v);
  1496. _fanWidth.SetValueWithoutNotify(fanWidth.Get());
  1497. _fanWidth.transform.Find("Value").GetComponent<Text>().text = fanWidth.Get().ToString("f1");
  1498. }
  1499. #endregion
  1500. #region 亮度检测部分
  1501. public void SetInfraredFilterValue(float v)
  1502. {
  1503. infraredFileterValue.Set(v);
  1504. _infraredFilter.SetValueWithoutNotify(infraredFileterValue.Get());
  1505. _infraredFilter.transform.Find("Value").GetComponent<Text>().text = infraredFileterValue.Get().ToString("f1");
  1506. infraredCameraHelper.SetInfraredLocateBrightnessThreshold(infraredFileterValue.Get());
  1507. }
  1508. #endregion
  1509. public void resetInfraredPlayerPrefs()
  1510. {
  1511. //测试用
  1512. PlayerPrefs.DeleteKey("entry-guider-infrared-" + LoginMgr.myUserInfo.id);
  1513. PlayerPrefs.DeleteKey("hideInfraredBowAndArrow");
  1514. }
  1515. #region 显示Log部分信息
  1516. [SerializeField] Text _calibrationFixedText;
  1517. [SerializeField] Text _cameraSizeText;
  1518. [SerializeField] Text _quadUnityVectorListText;
  1519. /// <summary>
  1520. /// 校准时候的固定值分辨率
  1521. /// </summary>
  1522. /// <param name="v"></param>
  1523. public void SetCalibrationFixedText(Vector2 v)
  1524. {
  1525. _calibrationFixedText.text = v.x + "*" + v.y;
  1526. }
  1527. /// <summary>
  1528. /// 渲染摄像机当前分辨率
  1529. /// </summary>
  1530. /// <param name="v"></param>
  1531. public void SetCameraSizeText(Vector2 v)
  1532. {
  1533. _cameraSizeText.text = v.x + "*" + v.y;
  1534. }
  1535. /// <summary>
  1536. /// 渲染摄像机识别点
  1537. /// </summary>
  1538. /// <param name="v"></param>
  1539. public void SetQuadUnityVectorListText(string v)
  1540. {
  1541. _quadUnityVectorListText.text = v;
  1542. }
  1543. #endregion
  1544. }
  1545. public class ParamFloatValue
  1546. {
  1547. private string _saveKey;
  1548. private float _valueDefault;
  1549. private bool _valueLoaded;
  1550. private float _value;
  1551. public ParamFloatValue(string saveKey, float valueDefault)
  1552. {
  1553. _saveKey = saveKey;
  1554. _valueDefault = valueDefault;
  1555. }
  1556. public float Get()
  1557. {
  1558. if (!_valueLoaded) _value = PlayerPrefs.GetFloat(_saveKey, _valueDefault);
  1559. return _value;
  1560. }
  1561. public float GetDefault() {
  1562. return _valueDefault;
  1563. }
  1564. public string GetKey()
  1565. {
  1566. return _saveKey;
  1567. }
  1568. public void Set(float value)
  1569. {
  1570. _value = value;
  1571. PlayerPrefs.SetFloat(_saveKey, _value);
  1572. PlayerPrefs.Save();
  1573. }
  1574. /// <summary>
  1575. /// 重置成默认值
  1576. /// </summary>
  1577. public void Reset() {
  1578. PlayerPrefs.SetFloat(_saveKey, _valueDefault);
  1579. PlayerPrefs.Save();
  1580. }
  1581. }