InfraredDemo.cs 66 KB

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