PXR_Manager.cs 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
  1. /*******************************************************************************
  2. Copyright © 2015-2022 PICO Technology Co., Ltd.All rights reserved.
  3. NOTICE:All information contained herein is, and remains the property of
  4. PICO Technology Co., Ltd. The intellectual and technical concepts
  5. contained herein are proprietary to PICO Technology Co., Ltd. and may be
  6. covered by patents, patents in process, and are protected by trade secret or
  7. copyright law. Dissemination of this information or reproduction of this
  8. material is strictly forbidden unless prior written permission is obtained from
  9. PICO Technology Co., Ltd.
  10. *******************************************************************************/
  11. using System;
  12. using System.Collections;
  13. using System.Collections.Generic;
  14. using UnityEngine;
  15. using UnityEngine.Rendering;
  16. using UnityEngine.XR;
  17. using UnityEngine.XR.Management;
  18. using UnityEngine.UI;
  19. using System.Collections.Generic;
  20. using System.Linq;
  21. namespace Unity.XR.PXR
  22. {
  23. public class PXR_Manager : MonoBehaviour
  24. {
  25. private const string TAG = "[PXR_Manager]";
  26. private static PXR_Manager instance = null;
  27. public static PXR_Manager Instance
  28. {
  29. get
  30. {
  31. if (instance == null)
  32. {
  33. instance = FindObjectOfType<PXR_Manager>();
  34. if (instance == null)
  35. {
  36. GameObject go = new GameObject("[PXR_Manager]");
  37. DontDestroyOnLoad(go);
  38. instance = go.AddComponent<PXR_Manager>();
  39. Debug.LogError("PXRLog instance is not initialized!");
  40. }
  41. }
  42. return instance;
  43. }
  44. }
  45. private float refreshRate = -1.0f;
  46. private Camera[] eyeCamera;
  47. private bool appSpaceWarp;
  48. private Transform m_AppSpaceTransform;
  49. private DepthTextureMode m_CachedDepthTextureMode;
  50. [HideInInspector]
  51. public bool screenFade;
  52. [HideInInspector]
  53. public bool eyeTracking;
  54. [HideInInspector]
  55. public FaceTrackingMode trackingMode = FaceTrackingMode.None;
  56. [HideInInspector]
  57. public SharpeningMode sharpeningMode = SharpeningMode.None;
  58. [HideInInspector]
  59. public SharpeningEnhance sharpeningEnhance = SharpeningEnhance.None;
  60. [HideInInspector]
  61. public bool faceTracking;
  62. [HideInInspector]
  63. public bool lipsyncTracking;
  64. [HideInInspector]
  65. public bool lateLatching;
  66. [HideInInspector]
  67. public bool latelatchingDebug;
  68. [HideInInspector]
  69. public bool bodyTracking;
  70. [HideInInspector]
  71. public FoveationLevel foveationLevel = FoveationLevel.None;
  72. [HideInInspector]
  73. public bool adaptiveResolution;
  74. [HideInInspector]
  75. public FoveationLevel eyeFoveationLevel = FoveationLevel.None;
  76. [HideInInspector]
  77. public FoveatedRenderingMode foveatedRenderingMode = FoveatedRenderingMode.FixedFoveatedRendering;
  78. //MRC
  79. #region MRCData
  80. [HideInInspector]
  81. public bool openMRC = true;
  82. [HideInInspector]
  83. public LayerMask foregroundLayerMask = -1;
  84. [HideInInspector]
  85. public LayerMask backLayerMask = -1;
  86. private static bool initMRCSucceed = false;
  87. private Texture[] swapChain = new Texture[2];
  88. private struct LayerTexture
  89. {
  90. public Texture[] swapChain;
  91. };
  92. private LayerTexture[] layerTexturesInfo;
  93. private bool createMRCOverlaySucceed = false;
  94. private int imageIndex;
  95. private UInt32 imageCounts = 0;
  96. private static CameraData xmlCamData;
  97. private bool mrcCamObjActived = false;
  98. private float[] cameraAttribute;
  99. private PxrLayerParam layerParam = new PxrLayerParam();
  100. [HideInInspector]
  101. public GameObject backgroundCamObj = null;
  102. [HideInInspector]
  103. public GameObject foregroundCamObj = null;
  104. [HideInInspector]
  105. public RenderTexture mrcBackgroundRT = null;
  106. [HideInInspector]
  107. public RenderTexture mrcForegroundRT = null;
  108. private Color foregroundColor = new Color(0, 1, 0, 1);
  109. private static float height;
  110. [SerializeField]
  111. [HideInInspector]
  112. public AdaptiveResolutionPowerSetting adaptiveResolutionPowerSetting = AdaptiveResolutionPowerSetting.BALANCED;
  113. [SerializeField]
  114. [HideInInspector]
  115. public float minEyeTextureScale = 0.7f;
  116. [SerializeField]
  117. [HideInInspector]
  118. public float maxEyeTextureScale = 1.26f;
  119. #endregion
  120. private bool isNeedResume = false;
  121. //Super Resolution
  122. [HideInInspector]
  123. public bool enableSuperResolution;
  124. public Action<float> DisplayRefreshRateChanged;
  125. [HideInInspector]
  126. public bool useRecommendedAntiAliasingLevel = true;
  127. private List<PxrEventDataBuffer> eventList = new List<PxrEventDataBuffer>();
  128. //New
  129. public static event Action<PxrEventAnchorEntityCreated> AnchorEntityCreated;
  130. public static event Action<PxrEventSpatialTrackingStateUpdate> SpatialTrackingStateUpdate;
  131. public static event Action<PxrEventAnchorEntityPersisted> AnchorEntityPersisted;
  132. public static event Action<PxrEventAnchorEntityUnPersisted> AnchorEntityUnPersisted;
  133. public static event Action<PxrEventAnchorEntityCleared> AnchorEntityCleared;
  134. public static event Action<PxrEventAnchorEntityLoaded> AnchorEntityLoaded;
  135. public static event Action<PxrEventSpatialSceneCaptured> SpatialSceneCaptured;
  136. public static event Action<PxrEventSemiAutoCandidatesUpdate> SemiAutoCandidatesUpdate;
  137. //Deprecate
  138. public static event Action<PxrEventSpatialAnchorSaveResult> SpatialAnchorSaveResult;
  139. public static event Action<PxrEventSpatialAnchorDeleteResult> SpatialAnchorDeleteResult;
  140. public static event Action<PxrEventSpatialAnchorLoadResults> SpatialAnchorLoadResults;
  141. public static event Action<PxrEventSpatialAnchorLoadResultsAvailable> SpatialAnchorLoadResultsAvailable;
  142. public static event Action<PxrEventSpatialAnchorLoadResultsComplete> SpatialAnchorLoadResultsComplete;
  143. public static event Action<PxrEventRoomSceneLoadResultsComplete> RoomSceneLoadResultsComplete;
  144. public static event Action<PxrEventRoomSceneDataSaveResult> RoomSceneDataSaveResult;
  145. public static event Action<PxrEventRoomSceneDataDeleteResult> RoomSceneDataDeleteResult;
  146. public static event Action<PxrEventRoomSceneDataUpdateResult> RoomSceneDataUpdateResult;
  147. public static event Action<PxrSemiAutoRoomCaptureCandidatesUpdate> SemiAutoRoomCaptureCandidatesUpdate;
  148. public static event Action<PxrEventSpatialTrackingStateInfo> SpatialTrackingStateInfo;
  149. void Awake()
  150. {
  151. //version log
  152. Debug.Log("PXRLog XR Platform----SDK Version:" + PXR_Plugin.System.UPxr_GetSDKVersion());
  153. //log level
  154. int logLevel = PXR_Plugin.System.UPxr_GetConfigInt(ConfigType.UnityLogLevel);
  155. Debug.Log("PXRLog XR Platform----SDK logLevel:" + logLevel);
  156. PLog.logLevel = (PLog.LogLevel)logLevel;
  157. eyeCamera = new Camera[3];
  158. Camera[] cam = gameObject.GetComponentsInChildren<Camera>();
  159. for (int i = 0; i < cam.Length; i++)
  160. {
  161. if (cam[i].stereoTargetEye == StereoTargetEyeMask.Both && cam[i] == Camera.main)
  162. {
  163. eyeCamera[0] = cam[i];
  164. }
  165. else if (cam[i].stereoTargetEye == StereoTargetEyeMask.Left)
  166. {
  167. eyeCamera[1] = cam[i];
  168. }
  169. else if (cam[i].stereoTargetEye == StereoTargetEyeMask.Right)
  170. {
  171. eyeCamera[2] = cam[i];
  172. }
  173. }
  174. PXR_Plugin.System.UPxr_EnableEyeTracking(eyeTracking);
  175. PXR_Plugin.System.UPxr_EnableFaceTracking(faceTracking);
  176. PXR_Plugin.System.UPxr_EnableLipSync(lipsyncTracking);
  177. StartCoroutine("SetFoveationLevel");
  178. if (GraphicsSettings.renderPipelineAsset == null || QualitySettings.renderPipeline == null)
  179. {
  180. int recommendedAntiAliasingLevel = PXR_Plugin.System.UPxr_GetConfigInt(ConfigType.AntiAliasingLevelRecommended);
  181. if (useRecommendedAntiAliasingLevel && QualitySettings.antiAliasing != recommendedAntiAliasingLevel)
  182. {
  183. QualitySettings.antiAliasing = recommendedAntiAliasingLevel;
  184. List<XRDisplaySubsystem> displaySubsystems = new List<XRDisplaySubsystem>();
  185. SubsystemManager.GetInstances(displaySubsystems);
  186. if (displaySubsystems.Count > 0)
  187. {
  188. displaySubsystems[0].SetMSAALevel(recommendedAntiAliasingLevel);
  189. }
  190. }
  191. }
  192. if (bodyTracking)
  193. {
  194. PXR_Plugin.Controller.UPxr_SetBodyTrackingMode(1);
  195. }
  196. Debug.LogFormat(TAG_MRC + "Awake openMRC = {0} ,MRCInitSucceed = {1}.", openMRC, initMRCSucceed);
  197. if (openMRC && initMRCSucceed == false)
  198. {
  199. MRCInitialize();
  200. }
  201. PXR_Plugin.System.UPxr_LogSdkApi("pico_msaa|" + QualitySettings.antiAliasing.ToString());
  202. }
  203. IEnumerator SetFoveationLevel()
  204. {
  205. int num = 3;
  206. bool result;
  207. do
  208. {
  209. if (FoveatedRenderingMode.EyeTrackedFoveatedRendering == foveatedRenderingMode)
  210. {
  211. result = PXR_FoveationRendering.SetFoveationLevel(eyeFoveationLevel, true);
  212. }
  213. else
  214. {
  215. result = PXR_FoveationRendering.SetFoveationLevel(foveationLevel, false);
  216. }
  217. PLog.i(TAG, "num = " + num + ", result = " + result);
  218. yield return new WaitForSeconds(1);
  219. } while (!result && num-- > 0);
  220. }
  221. void OnApplicationPause(bool pause)
  222. {
  223. if (!pause)
  224. {
  225. if (isNeedResume)
  226. {
  227. StartCoroutine("StartXR");
  228. isNeedResume = false;
  229. }
  230. }
  231. }
  232. private void OnApplicationQuit()
  233. {
  234. Debug.LogFormat(TAG_MRC + "OnApplicationQuit openMRC = {0} ,MRCInitSucceed = {1}.", openMRC, initMRCSucceed);
  235. if (openMRC && initMRCSucceed)
  236. {
  237. PXR_Plugin.Render.UPxr_DestroyLayer(LAYER_MRC);
  238. }
  239. }
  240. public IEnumerator StartXR()
  241. {
  242. yield return XRGeneralSettings.Instance.Manager.InitializeLoader();
  243. if (XRGeneralSettings.Instance.Manager.activeLoader == null)
  244. {
  245. Debug.LogError("PXRLog Initializing XR Failed. Check log for details.");
  246. }
  247. else
  248. {
  249. XRGeneralSettings.Instance.Manager.StartSubsystems();
  250. }
  251. }
  252. void StopXR()
  253. {
  254. XRGeneralSettings.Instance.Manager.StopSubsystems();
  255. XRGeneralSettings.Instance.Manager.DeinitializeLoader();
  256. }
  257. void Start()
  258. {
  259. #if UNITY_EDITOR
  260. Application.targetFrameRate = 72;
  261. #endif
  262. PXR_Plugin.Controller.UPxr_SetControllerDelay();
  263. if (adaptiveResolution)
  264. {
  265. XRSettings.eyeTextureResolutionScale = maxEyeTextureScale;
  266. }
  267. }
  268. void Update()
  269. {
  270. if (Math.Abs(refreshRate - PXR_Plugin.System.UPxr_RefreshRateChanged()) > 0.1f)
  271. {
  272. refreshRate = PXR_Plugin.System.UPxr_RefreshRateChanged();
  273. if (DisplayRefreshRateChanged != null)
  274. DisplayRefreshRateChanged(refreshRate);
  275. }
  276. //recenter callback
  277. if (PXR_Plugin.System.UPxr_GetHomeKey())
  278. {
  279. if (PXR_Plugin.System.RecenterSuccess != null)
  280. {
  281. PXR_Plugin.System.RecenterSuccess();
  282. }
  283. PXR_Plugin.System.UPxr_InitHomeKey();
  284. }
  285. UpdateMRCCam();
  286. //Adaptive Resolution
  287. if (adaptiveResolution)
  288. {
  289. UpdateAdaptiveResolution();
  290. }
  291. //pollEvent
  292. PollEvent();
  293. }
  294. void UpdateAdaptiveResolution()
  295. {
  296. float lastRenderScale = XRSettings.renderViewportScale;
  297. int newWidth = (int)((float)XRSettings.eyeTextureWidth * lastRenderScale);
  298. int success = PXR_Plugin.System.UPxr_UpdateAdaptiveResolution(ref newWidth, adaptiveResolutionPowerSetting);
  299. if (success == -1)
  300. return;
  301. float currRenderScale = (float)newWidth / (float)XRSettings.eyeTextureWidth;
  302. float minScale = minEyeTextureScale / maxEyeTextureScale;
  303. float newRenderScale = Mathf.Min(1.0f, Mathf.Max(currRenderScale, minScale));
  304. //Debug.Log(" RenderViewportScale: " + newRenderScale);
  305. UnityEngine.XR.XRSettings.renderViewportScale = newRenderScale;
  306. }
  307. void OnEnable()
  308. {
  309. if (PXR_OverLay.Instances.Count > 0)
  310. {
  311. if (Camera.main.gameObject.GetComponent<PXR_OverlayManager>() == null)
  312. {
  313. Camera.main.gameObject.AddComponent<PXR_OverlayManager>();
  314. }
  315. foreach (var layer in PXR_OverLay.Instances)
  316. {
  317. if (eyeCamera[0] != null && eyeCamera[0].enabled)
  318. {
  319. layer.RefreshCamera(eyeCamera[0], eyeCamera[0]);
  320. }
  321. else if (eyeCamera[1] != null && eyeCamera[1].enabled)
  322. {
  323. layer.RefreshCamera(eyeCamera[1], eyeCamera[2]);
  324. }
  325. }
  326. }
  327. if (openMRC)
  328. {
  329. if (GraphicsSettings.renderPipelineAsset != null)
  330. {
  331. RenderPipelineManager.beginFrameRendering += BeginRendering;
  332. RenderPipelineManager.endFrameRendering += EndRendering;
  333. }
  334. else
  335. {
  336. Camera.onPreRender += OnPreRenderCallBack;
  337. Camera.onPostRender += OnPostRenderCallBack;
  338. }
  339. PXR_Plugin.System.RecenterSuccess += CalibrationMRCCam;
  340. }
  341. PXR_Plugin.System.LoglevelChangedChanged += LoglevelChangedCallback;
  342. }
  343. private void LateUpdate()
  344. {
  345. if (appSpaceWarp && m_AppSpaceTransform != null)
  346. {
  347. PXR_Plugin.Render.UPxr_SetAppSpacePosition(m_AppSpaceTransform.position.x, m_AppSpaceTransform.position.y, m_AppSpaceTransform.position.z);
  348. PXR_Plugin.Render.UPxr_SetAppSpaceRotation(m_AppSpaceTransform.rotation.x, m_AppSpaceTransform.rotation.y, m_AppSpaceTransform.rotation.z, m_AppSpaceTransform.rotation.w);
  349. }
  350. }
  351. private void PollEvent()
  352. {
  353. eventList.Clear();
  354. bool ret = PXR_Plugin.MixedReality.UPxr_PollEventQueue(ref eventList);
  355. if (ret)
  356. {
  357. for (int i = 0; i < eventList.Count; i++)
  358. {
  359. Debug.Log("PXRLog PollEventOfMixedReality" + eventList[i].type);
  360. switch (eventList[i].type)
  361. {
  362. case PxrStructureType.AnchorEntityCreated:
  363. {
  364. if (AnchorEntityCreated != null)
  365. {
  366. PxrEventAnchorEntityCreated info = new PxrEventAnchorEntityCreated()
  367. {
  368. taskId = BitConverter.ToUInt64(eventList[i].data, 0),
  369. result = (PxrResult)BitConverter.ToInt32(eventList[i].data, 8),
  370. anchorHandle = BitConverter.ToUInt64(eventList[i].data, 16),
  371. };
  372. byte[] byteArray = new byte[16];
  373. var value0 = BitConverter.ToUInt64(eventList[i].data, 24);
  374. var value1 = BitConverter.ToUInt64(eventList[i].data, 32);
  375. BitConverter.GetBytes(value0).CopyTo(byteArray, 0);
  376. BitConverter.GetBytes(value1).CopyTo(byteArray, 8);
  377. info.uuid = new Guid(byteArray);
  378. AnchorEntityCreated(info);
  379. }
  380. }
  381. break;
  382. case PxrStructureType.AnchorEntityPersisted:
  383. {
  384. if (AnchorEntityPersisted != null)
  385. {
  386. PxrEventAnchorEntityPersisted info = new PxrEventAnchorEntityPersisted()
  387. {
  388. taskId = BitConverter.ToUInt64(eventList[i].data, 0),
  389. result = (PxrResult)BitConverter.ToInt32(eventList[i].data, 8),
  390. location = (PxrPersistLocation)BitConverter.ToInt32(eventList[i].data, 12)
  391. };
  392. AnchorEntityPersisted(info);
  393. }
  394. }
  395. break;
  396. case PxrStructureType.AnchorEntityUnPersisted:
  397. {
  398. if (AnchorEntityUnPersisted != null)
  399. {
  400. PxrEventAnchorEntityUnPersisted info = new PxrEventAnchorEntityUnPersisted()
  401. {
  402. taskId = BitConverter.ToUInt64(eventList[i].data, 0),
  403. result = (PxrResult)BitConverter.ToInt32(eventList[i].data, 8),
  404. location = (PxrPersistLocation)BitConverter.ToInt32(eventList[i].data, 12)
  405. };
  406. AnchorEntityUnPersisted(info);
  407. }
  408. }
  409. break;
  410. case PxrStructureType.AnchorEntityCleared:
  411. {
  412. if (AnchorEntityCleared != null)
  413. {
  414. PxrEventAnchorEntityCleared info = new PxrEventAnchorEntityCleared()
  415. {
  416. taskId = BitConverter.ToUInt64(eventList[i].data, 0),
  417. result = (PxrResult)BitConverter.ToInt32(eventList[i].data, 8),
  418. location = (PxrPersistLocation)BitConverter.ToInt32(eventList[i].data, 12)
  419. };
  420. AnchorEntityCleared(info);
  421. }
  422. }
  423. break;
  424. case PxrStructureType.AnchorEntityLoaded:
  425. {
  426. if (AnchorEntityLoaded != null)
  427. {
  428. PxrEventAnchorEntityLoaded info = new PxrEventAnchorEntityLoaded()
  429. {
  430. taskId = BitConverter.ToUInt64(eventList[i].data, 0),
  431. result = (PxrResult)BitConverter.ToInt32(eventList[i].data, 8),
  432. count = BitConverter.ToUInt32(eventList[i].data, 12),
  433. location = (PxrPersistLocation)BitConverter.ToInt32(eventList[i].data, 16)
  434. };
  435. AnchorEntityLoaded(info);
  436. }
  437. }
  438. break;
  439. case PxrStructureType.SpatialTrackingStateUpdate:
  440. {
  441. if (SpatialTrackingStateUpdate != null)
  442. {
  443. PxrEventSpatialTrackingStateUpdate info = new PxrEventSpatialTrackingStateUpdate()
  444. {
  445. state = (PxrSpatialTrackingState)BitConverter.ToInt32(eventList[i].data, 0),
  446. message = (PxrSpatialTrackingStateMessage)BitConverter.ToInt32(eventList[i].data, 4),
  447. };
  448. SpatialTrackingStateUpdate(info);
  449. }
  450. }
  451. break;
  452. case PxrStructureType.SpatialSceneCaptured:
  453. {
  454. if (SpatialSceneCaptured != null)
  455. {
  456. PxrEventSpatialSceneCaptured info = new PxrEventSpatialSceneCaptured()
  457. {
  458. taskId = BitConverter.ToUInt64(eventList[i].data, 0),
  459. result = (PxrResult)BitConverter.ToInt32(eventList[i].data, 8),
  460. status = (PxrSpatialSceneCaptureStatus)BitConverter.ToUInt32(eventList[i].data, 12),
  461. };
  462. SpatialSceneCaptured(info);
  463. }
  464. }
  465. break;
  466. case PxrStructureType.PXR_TYPE_EVENT_DATA_SEETHROUGH_STATE_CHANGED:
  467. {
  468. PXR_Plugin.System.SeethroughStateChangedChanged((int)BitConverter.ToInt32(eventList[i].data, 0));
  469. }
  470. break;
  471. case PxrStructureType.SemiAutoCandidatesUpdate:
  472. {
  473. if (SemiAutoCandidatesUpdate != null)
  474. {
  475. PxrEventSemiAutoCandidatesUpdate data = new PxrEventSemiAutoCandidatesUpdate()
  476. {
  477. state = BitConverter.ToUInt32(eventList[i].data, 0),
  478. count = BitConverter.ToUInt32(eventList[i].data, 4),
  479. };
  480. SemiAutoCandidatesUpdate(data);
  481. }
  482. }
  483. break;
  484. case PxrStructureType.SpatialAnchorSaveResult:
  485. {
  486. if (SpatialAnchorSaveResult != null)
  487. {
  488. PxrEventSpatialAnchorSaveResult result = new PxrEventSpatialAnchorSaveResult()
  489. {
  490. type = PxrStructureType.SpatialAnchorSaveResult,
  491. eventLevel = eventList[i].eventLevel,
  492. result = (PxrSpatialPersistenceResult)BitConverter.ToInt32(eventList[i].data, 0),
  493. asyncRequestId = BitConverter.ToUInt64(eventList[i].data, 8),
  494. uuid = new PxrSpatialInstanceUuid()
  495. {
  496. value0 = BitConverter.ToUInt64(eventList[i].data, 16),
  497. value1 = BitConverter.ToUInt64(eventList[i].data, 24),
  498. },
  499. handle = BitConverter.ToUInt64(eventList[i].data, 32),
  500. location = (PxrSpatialPersistenceLocation)BitConverter.ToInt32(eventList[i].data, 40)
  501. };
  502. SpatialAnchorSaveResult(result);
  503. }
  504. }
  505. break;
  506. case PxrStructureType.SpatialAnchorDeleteResult:
  507. {
  508. if (SpatialAnchorDeleteResult != null)
  509. {
  510. PxrEventSpatialAnchorDeleteResult result = new PxrEventSpatialAnchorDeleteResult()
  511. {
  512. type = PxrStructureType.SpatialAnchorDeleteResult,
  513. eventLevel = eventList[i].eventLevel,
  514. result = (PxrSpatialPersistenceResult)BitConverter.ToInt32(eventList[i].data, 0),
  515. asyncRequestId = BitConverter.ToUInt64(eventList[i].data, 8),
  516. uuid = new PxrSpatialInstanceUuid()
  517. {
  518. value0 = BitConverter.ToUInt64(eventList[i].data, 16),
  519. value1 = BitConverter.ToUInt64(eventList[i].data, 24),
  520. },
  521. location = (PxrSpatialPersistenceLocation)BitConverter.ToInt32(eventList[i].data, 32)
  522. };
  523. SpatialAnchorDeleteResult(result);
  524. }
  525. }
  526. break;
  527. case PxrStructureType.SpatialAnchorLoadResults:
  528. {
  529. if (SpatialAnchorLoadResults != null)
  530. {
  531. PxrEventSpatialAnchorLoadResults results = new PxrEventSpatialAnchorLoadResults()
  532. {
  533. type = PxrStructureType.SpatialAnchorLoadResults,
  534. eventLevel = eventList[i].eventLevel,
  535. result = (PxrSpatialPersistenceResult)BitConverter.ToInt32(eventList[i].data, 0),
  536. hasNext = BitConverter.ToBoolean(eventList[i].data, 4),
  537. asyncRequestId = BitConverter.ToUInt64(eventList[i].data, 8),
  538. numResults = BitConverter.ToUInt32(eventList[i].data, 16),
  539. loadResults = new PxrSpatialAnchorLoadResult[BitConverter.ToUInt32(eventList[i].data, 16)],
  540. };
  541. int offset = 24;
  542. for (int j = 0; j < results.numResults; j++)
  543. {
  544. results.loadResults[j] = new PxrSpatialAnchorLoadResult();
  545. results.loadResults[j].anchorHandle = BitConverter.ToUInt64(eventList[i].data, offset);
  546. offset += 8;
  547. results.loadResults[j].uuid = new PxrSpatialInstanceUuid();
  548. results.loadResults[j].uuid.value0 = BitConverter.ToUInt64(eventList[i].data, offset);
  549. offset += 8;
  550. results.loadResults[j].uuid.value1 = BitConverter.ToUInt64(eventList[i].data, offset);
  551. offset += 8;
  552. }
  553. SpatialAnchorLoadResults(results);
  554. }
  555. }
  556. break;
  557. case PxrStructureType.SpatialAnchorLoadResultsAvailable:
  558. {
  559. if (SpatialAnchorLoadResultsAvailable != null)
  560. {
  561. PxrEventSpatialAnchorLoadResultsAvailable data = new PxrEventSpatialAnchorLoadResultsAvailable()
  562. {
  563. type = PxrStructureType.SpatialAnchorLoadResultsAvailable,
  564. eventLevel = eventList[i].eventLevel,
  565. asyncRequestId = BitConverter.ToUInt64(eventList[i].data, 0),
  566. };
  567. SpatialAnchorLoadResultsAvailable(data);
  568. }
  569. }
  570. break;
  571. case PxrStructureType.SpatialAnchorLoadResultsComplete:
  572. {
  573. if (SpatialAnchorLoadResultsComplete != null)
  574. {
  575. PxrEventSpatialAnchorLoadResultsComplete data = new PxrEventSpatialAnchorLoadResultsComplete()
  576. {
  577. type = PxrStructureType.SpatialAnchorLoadResultsComplete,
  578. eventLevel = eventList[i].eventLevel,
  579. asyncRequestId = BitConverter.ToUInt64(eventList[i].data, 0),
  580. result = (PxrSpatialPersistenceResult)BitConverter.ToInt32(eventList[i].data, 8),
  581. };
  582. SpatialAnchorLoadResultsComplete(data);
  583. }
  584. }
  585. break;
  586. case PxrStructureType.RoomSceneDataSaveResult:
  587. {
  588. if (RoomSceneDataSaveResult != null)
  589. {
  590. PxrEventRoomSceneDataSaveResult data = new PxrEventRoomSceneDataSaveResult()
  591. {
  592. type = PxrStructureType.RoomSceneDataSaveResult,
  593. eventLevel = eventList[i].eventLevel,
  594. result = (PxrSpatialPersistenceResult)BitConverter.ToInt32(eventList[i].data, 0),
  595. asyncRequestId = BitConverter.ToUInt64(eventList[i].data, 8),
  596. handle = BitConverter.ToUInt64(eventList[i].data, 16),
  597. location = (PxrSpatialPersistenceLocation)BitConverter.ToInt32(eventList[i].data, 24),
  598. };
  599. RoomSceneDataSaveResult(data);
  600. }
  601. }
  602. break;
  603. case PxrStructureType.RoomSceneDataDeleteResult:
  604. {
  605. if (RoomSceneDataDeleteResult != null)
  606. {
  607. PxrEventRoomSceneDataDeleteResult data = new PxrEventRoomSceneDataDeleteResult()
  608. {
  609. type = PxrStructureType.RoomSceneDataDeleteResult,
  610. eventLevel = eventList[i].eventLevel,
  611. result = (PxrSpatialPersistenceResult)BitConverter.ToInt32(eventList[i].data, 0),
  612. asyncRequestId = BitConverter.ToUInt64(eventList[i].data, 8),
  613. handle = BitConverter.ToUInt64(eventList[i].data, 16),
  614. location = (PxrSpatialPersistenceLocation)BitConverter.ToInt32(eventList[i].data, 24),
  615. };
  616. RoomSceneDataDeleteResult(data);
  617. }
  618. }
  619. break;
  620. case PxrStructureType.RoomSceneLoadResultsComplete:
  621. {
  622. if (RoomSceneLoadResultsComplete != null)
  623. {
  624. PxrEventRoomSceneLoadResultsComplete data = new PxrEventRoomSceneLoadResultsComplete()
  625. {
  626. type = PxrStructureType.RoomSceneLoadResultsComplete,
  627. eventLevel = eventList[i].eventLevel,
  628. asyncRequestId = BitConverter.ToUInt64(eventList[i].data, 0),
  629. result = (PxrSpatialPersistenceResult)BitConverter.ToInt32(eventList[i].data, 8),
  630. };
  631. RoomSceneLoadResultsComplete(data);
  632. }
  633. }
  634. break;
  635. case PxrStructureType.RoomSceneDataUpdateResult:
  636. {
  637. if (RoomSceneDataUpdateResult != null)
  638. {
  639. PxrEventRoomSceneDataUpdateResult data = new PxrEventRoomSceneDataUpdateResult()
  640. {
  641. type = PxrStructureType.RoomSceneDataUpdateResult,
  642. level = eventList[i].eventLevel,
  643. anchorUuid = new PxrSpatialInstanceUuid()
  644. {
  645. value0 = BitConverter.ToUInt64(eventList[i].data, 0),
  646. value1 = BitConverter.ToUInt64(eventList[i].data, 8),
  647. },
  648. roomSceneDataHandle = BitConverter.ToUInt64(eventList[i].data, 16),
  649. result = BitConverter.ToUInt32(eventList[i].data, 24),
  650. dataLength = BitConverter.ToUInt32(eventList[i].data, 28),
  651. };
  652. data.roomSceneData = new byte[data.dataLength];
  653. Buffer.BlockCopy(eventList[i].data, 32, data.roomSceneData, 0, (int)data.dataLength);
  654. RoomSceneDataUpdateResult(data);
  655. }
  656. }
  657. break;
  658. case PxrStructureType.SemiAutoRoomCaptureCandidatesUpdate:
  659. {
  660. if (SemiAutoRoomCaptureCandidatesUpdate != null)
  661. {
  662. PxrSemiAutoRoomCaptureCandidatesUpdate data = new PxrSemiAutoRoomCaptureCandidatesUpdate()
  663. {
  664. type = PxrStructureType.SemiAutoRoomCaptureCandidatesUpdate,
  665. eventLevel = eventList[i].eventLevel,
  666. result = (PxrSpatialPersistenceResult)BitConverter.ToInt32(eventList[i].data, 0),
  667. };
  668. SemiAutoRoomCaptureCandidatesUpdate(data);
  669. }
  670. }
  671. break;
  672. case PxrStructureType.TrackingStateChanged:
  673. {
  674. if (SpatialTrackingStateInfo != null)
  675. {
  676. PxrEventSpatialTrackingStateInfo info = new PxrEventSpatialTrackingStateInfo()
  677. {
  678. type = PxrStructureType.TrackingStateChanged,
  679. eventLevel = eventList[i].eventLevel,
  680. stateInfo = new PxrSpatialTrackingStateInfo()
  681. {
  682. state = (PxrSpatialTrackingState)BitConverter.ToInt32(eventList[i].data, 0),
  683. message = (PxrSpatialTrackingStateMessage)BitConverter.ToInt32(eventList[i].data, 4),
  684. },
  685. };
  686. SpatialTrackingStateInfo(info);
  687. }
  688. }
  689. break;
  690. }
  691. }
  692. }
  693. }
  694. public void SetSpaceWarp(bool enabled)
  695. {
  696. for (int i = 0; i < 3; i++)
  697. {
  698. if (eyeCamera[i] != null && eyeCamera[i].enabled)
  699. {
  700. if (enabled)
  701. {
  702. m_CachedDepthTextureMode = eyeCamera[i].depthTextureMode;
  703. eyeCamera[i].depthTextureMode |= (DepthTextureMode.MotionVectors | DepthTextureMode.Depth);
  704. if (eyeCamera[i].transform.parent == null)
  705. {
  706. m_AppSpaceTransform.SetPositionAndRotation(Vector3.zero, Quaternion.identity);
  707. }
  708. else
  709. {
  710. m_AppSpaceTransform = eyeCamera[i].transform.parent;
  711. }
  712. }
  713. else
  714. {
  715. eyeCamera[i].depthTextureMode = m_CachedDepthTextureMode;
  716. m_AppSpaceTransform = null;
  717. }
  718. }
  719. }
  720. PXR_Plugin.Render.UPxr_SetSpaceWarp(enabled);
  721. appSpaceWarp = enabled;
  722. }
  723. void OnDisable()
  724. {
  725. StopAllCoroutines();
  726. if (openMRC)
  727. {
  728. if (GraphicsSettings.renderPipelineAsset != null)
  729. {
  730. RenderPipelineManager.beginFrameRendering -= BeginRendering;
  731. RenderPipelineManager.endFrameRendering -= EndRendering;
  732. }
  733. else
  734. {
  735. Camera.onPreRender -= OnPreRenderCallBack;
  736. Camera.onPostRender -= OnPostRenderCallBack;
  737. }
  738. PXR_Plugin.System.RecenterSuccess -= CalibrationMRCCam;
  739. }
  740. PXR_Plugin.System.LoglevelChangedChanged -= LoglevelChangedCallback;
  741. }
  742. void LoglevelChangedCallback(int value)
  743. {
  744. PLog.logLevel = (PLog.LogLevel)value;
  745. PLog.i(TAG, "LoglevelChangedCallback value " + value);
  746. }
  747. public Camera[] GetEyeCamera()
  748. {
  749. return eyeCamera;
  750. }
  751. #region MRC FUNC
  752. private const string TAG_MRC = "PXR MRC ";
  753. private const int LAYER_MRC = 99999;
  754. public void MRCInitialize()
  755. {
  756. xmlCamData = new CameraData();
  757. string path = Application.persistentDataPath + "/mrc.xml";
  758. cameraAttribute = PXR_Plugin.PlatformSetting.UPxr_MRCCalibration(path);
  759. PLog.i(TAG_MRC, "cameraDataLength: " + cameraAttribute.Length);
  760. for (int i = 0; i < cameraAttribute.Length; i++)
  761. {
  762. PLog.i(TAG_MRC, "cameraData: " + i.ToString() + ": " + cameraAttribute[i].ToString());
  763. }
  764. xmlCamData.imageW = cameraAttribute[0];
  765. xmlCamData.imageH = cameraAttribute[1];
  766. xmlCamData.yFov = cameraAttribute[2];
  767. xmlCamData.position.x = cameraAttribute[3];
  768. xmlCamData.position.y = cameraAttribute[4];
  769. xmlCamData.position.z = cameraAttribute[5];
  770. xmlCamData.orientation.x = cameraAttribute[6];
  771. xmlCamData.orientation.y = cameraAttribute[7];
  772. xmlCamData.orientation.z = cameraAttribute[8];
  773. xmlCamData.orientation.w = cameraAttribute[9];
  774. Invoke("Pxr_GetHeight", 0.5f);
  775. PXR_Plugin.System.UPxr_SetIsSupportMovingMrc(true);
  776. PxrPosef pose = new PxrPosef();
  777. pose.position = xmlCamData.position;
  778. pose.orientation = xmlCamData.orientation;
  779. PXR_Plugin.System.UPxr_SetMrcPose(ref pose);
  780. PXR_Plugin.System.UPxr_GetMrcPose(ref pose);
  781. xmlCamData.position = pose.position;
  782. xmlCamData.orientation = pose.orientation;
  783. mrcCamObjActived = false;
  784. PXR_Plugin.System.UPxr_SetMrcTextutrWidth((ulong)xmlCamData.imageW);
  785. PXR_Plugin.System.UPxr_SetMrcTextutrHeight((ulong)xmlCamData.imageH);
  786. if (xmlCamData.imageW <= 0 || xmlCamData.imageH <= 0)
  787. {
  788. initMRCSucceed = false;
  789. PLog.e(TAG_MRC, "Abnormal calibration data, so MRC init failed!");
  790. return;
  791. }
  792. layerParam.layerId = LAYER_MRC;
  793. layerParam.layerShape = PXR_OverLay.OverlayShape.Quad;
  794. layerParam.layerType = PXR_OverLay.OverlayType.Overlay;
  795. layerParam.layerLayout = PXR_OverLay.LayerLayout.Stereo;
  796. layerParam.format = (UInt64)RenderTextureFormat.Default;
  797. layerParam.width = (uint)xmlCamData.imageW;
  798. layerParam.height = (uint)xmlCamData.imageH;
  799. layerParam.sampleCount = 1;
  800. layerParam.faceCount = 1;
  801. layerParam.arraySize = 1;
  802. layerParam.mipmapCount = 0;
  803. layerParam.layerFlags = 0;
  804. PXR_Plugin.Render.UPxr_CreateLayerParam(layerParam);
  805. initMRCSucceed = true;
  806. PLog.i(TAG_MRC, "Init Succeed.");
  807. }
  808. private void BeginRendering(ScriptableRenderContext arg1, Camera[] arg2)
  809. {
  810. foreach (Camera cam in arg2)
  811. {
  812. if (cam != null && Camera.main == cam)
  813. {
  814. OnPreRenderCallBack(cam);
  815. }
  816. }
  817. }
  818. private void EndRendering(ScriptableRenderContext arg1, Camera[] arg2)
  819. {
  820. foreach (Camera cam in arg2)
  821. {
  822. if (cam != null && Camera.main == cam)
  823. {
  824. OnPostRenderCallBack(cam);
  825. }
  826. }
  827. }
  828. private void OnPreRenderCallBack(Camera cam)
  829. {
  830. if (!initMRCSucceed || createMRCOverlaySucceed) return;
  831. if (null == layerTexturesInfo)
  832. {
  833. layerTexturesInfo = new LayerTexture[2];
  834. }
  835. for (int i = 0; i < 2; i++)
  836. {
  837. int ret = PXR_Plugin.Render.UPxr_GetLayerImageCount(LAYER_MRC, (EyeType)i, ref imageCounts);
  838. if (ret != 0 || imageCounts < 1)
  839. {
  840. PLog.e(TAG_MRC, "UPxr_GetLayerImageCount failed, i:" + i);
  841. continue;
  842. }
  843. if (layerTexturesInfo[i].swapChain == null)
  844. {
  845. layerTexturesInfo[i].swapChain = new Texture[imageCounts];
  846. }
  847. for (int j = 0; j < imageCounts; j++)
  848. {
  849. IntPtr ptr = IntPtr.Zero;
  850. PXR_Plugin.Render.UPxr_GetLayerImagePtr(LAYER_MRC, (EyeType)i, j, ref ptr);
  851. if (IntPtr.Zero == ptr)
  852. {
  853. PLog.e(TAG_MRC, "UPxr_GetLayerImagePtr is Zero, i:" + i);
  854. continue;
  855. }
  856. Texture texture = Texture2D.CreateExternalTexture((int)xmlCamData.imageW, (int)xmlCamData.imageH, TextureFormat.RGBA32, false, true, ptr);
  857. if (null == texture)
  858. {
  859. PLog.e(TAG_MRC, "CreateExternalTexture texture null, i:" + i);
  860. continue;
  861. }
  862. layerTexturesInfo[i].swapChain[j] = texture;
  863. }
  864. createMRCOverlaySucceed = true;
  865. PLog.i(TAG_MRC, " UPxr_GetLayerImagePtr createMRCOverlaySucceed : true. i:" + i);
  866. }
  867. }
  868. public void OnPostRenderCallBack(Camera cam)
  869. {
  870. if (!initMRCSucceed || !createMRCOverlaySucceed || !PXR_Plugin.System.UPxr_GetMRCEnable()) return;
  871. if (cam == null || cam != Camera.main || cam.stereoActiveEye == Camera.MonoOrStereoscopicEye.Right) return;
  872. PXR_Plugin.Render.UPxr_GetLayerNextImageIndex(LAYER_MRC, ref imageIndex);
  873. for (int eyeId = 0; eyeId < 2; ++eyeId)
  874. {
  875. Texture dstT = layerTexturesInfo[eyeId].swapChain[imageIndex];
  876. if (dstT == null)
  877. {
  878. PLog.e(TAG_MRC, "dstT is null, eyeId:" + eyeId);
  879. continue;
  880. }
  881. RenderTexture rt = (0 == eyeId) ? mrcBackgroundRT : mrcForegroundRT;
  882. RenderTexture tempRT = null;
  883. if (!(QualitySettings.activeColorSpace == ColorSpace.Gamma && rt != null && rt.format == RenderTextureFormat.ARGB32))
  884. {
  885. RenderTextureDescriptor descriptor = new RenderTextureDescriptor((int)xmlCamData.imageW, (int)xmlCamData.imageH, RenderTextureFormat.ARGB32, 0);
  886. descriptor.msaaSamples = 1;
  887. descriptor.useMipMap = false;
  888. descriptor.autoGenerateMips = false;
  889. descriptor.sRGB = false;
  890. tempRT = RenderTexture.GetTemporary(descriptor);
  891. if (!tempRT.IsCreated())
  892. {
  893. tempRT.Create();
  894. }
  895. rt.DiscardContents();
  896. tempRT.DiscardContents();
  897. Graphics.Blit(rt, tempRT);
  898. Graphics.CopyTexture(tempRT, 0, 0, dstT, 0, 0);
  899. }
  900. else
  901. {
  902. Graphics.CopyTexture(rt, 0, 0, dstT, 0, 0);
  903. }
  904. if (tempRT != null)
  905. {
  906. RenderTexture.ReleaseTemporary(tempRT);
  907. }
  908. }
  909. PxrLayerQuad layerSubmit = new PxrLayerQuad();
  910. layerSubmit.header.layerId = LAYER_MRC;
  911. layerSubmit.header.layerFlags = (UInt32)PxrLayerSubmitFlagsEXT.PxrLayerFlagMRCComposition;
  912. layerSubmit.width = 1.0f;
  913. layerSubmit.height = 1.0f;
  914. layerSubmit.header.colorScaleX = 1.0f;
  915. layerSubmit.header.colorScaleY = 1.0f;
  916. layerSubmit.header.colorScaleZ = 1.0f;
  917. layerSubmit.header.colorScaleW = 1.0f;
  918. layerSubmit.pose.orientation.w = 1.0f;
  919. layerSubmit.header.headPose.orientation.x = 0;
  920. layerSubmit.header.headPose.orientation.y = 0;
  921. layerSubmit.header.headPose.orientation.z = 0;
  922. layerSubmit.header.headPose.orientation.w = 1;
  923. PXR_Plugin.Render.UPxr_SubmitLayerQuad(layerSubmit);
  924. }
  925. private void UpdateMRCCam()
  926. {
  927. if (!openMRC || !initMRCSucceed) return;
  928. if (!PXR_Plugin.System.UPxr_GetMRCEnable())
  929. {
  930. if (mrcCamObjActived)
  931. {
  932. mrcCamObjActived = false;
  933. backgroundCamObj.SetActive(false);
  934. foregroundCamObj.SetActive(false);
  935. }
  936. return;
  937. }
  938. if (null != Camera.main.transform && (null == backgroundCamObj || !mrcCamObjActived))
  939. {
  940. CreateMRCCam();
  941. }
  942. if (null != foregroundCamObj)
  943. {
  944. Vector3 cameraLookAt = Camera.main.transform.position - foregroundCamObj.transform.position;
  945. float distance = Vector3.Dot(cameraLookAt, foregroundCamObj.transform.forward);
  946. foregroundCamObj.GetComponent<Camera>().farClipPlane = Mathf.Max(foregroundCamObj.GetComponent<Camera>().nearClipPlane + 0.001f, distance);
  947. }
  948. CalibrationMRCCam();
  949. }
  950. public void CreateMRCCam()
  951. {
  952. if (backgroundCamObj == null)
  953. {
  954. backgroundCamObj = new GameObject("myBackgroundCamera");
  955. backgroundCamObj.transform.parent = Camera.main.transform.parent;
  956. backgroundCamObj.AddComponent<Camera>();
  957. PLog.i(TAG_MRC, "create background camera object.");
  958. }
  959. InitBackgroungCam(backgroundCamObj.GetComponent<Camera>());
  960. backgroundCamObj.SetActive(true);
  961. if (foregroundCamObj == null)
  962. {
  963. foregroundCamObj = new GameObject("myForegroundCamera");
  964. foregroundCamObj.transform.parent = Camera.main.transform.parent;
  965. foregroundCamObj.AddComponent<Camera>();
  966. PLog.i(TAG_MRC, "create foreground camera object.");
  967. }
  968. InitForegroundCam(foregroundCamObj.GetComponent<Camera>());
  969. foregroundCamObj.SetActive(true);
  970. mrcCamObjActived = true;
  971. PLog.i(TAG_MRC, "Camera Obj Actived. mrcCamObjActived : true.");
  972. }
  973. private void InitBackgroungCam(Camera camera)
  974. {
  975. camera.clearFlags = CameraClearFlags.Skybox;
  976. camera.stereoTargetEye = StereoTargetEyeMask.None;
  977. camera.transform.localScale = Vector3.one;
  978. #if UNITY_2021_3_OR_NEWER
  979. camera.transform.SetLocalPositionAndRotation(Vector3.zero,Quaternion.identity);
  980. #else
  981. camera.transform.localPosition = Vector3.zero;
  982. camera.transform.localEulerAngles = Vector3.zero;
  983. #endif
  984. camera.depth = 9999;
  985. camera.gameObject.layer = 0;
  986. camera.orthographic = false;
  987. camera.fieldOfView = xmlCamData.yFov;
  988. camera.aspect = xmlCamData.imageW / xmlCamData.imageH;
  989. camera.allowMSAA = true;
  990. camera.cullingMask = backLayerMask;
  991. if (mrcBackgroundRT == null)
  992. {
  993. mrcBackgroundRT = new RenderTexture((int)xmlCamData.imageW, (int)xmlCamData.imageH, 24, RenderTextureFormat.ARGB32);
  994. }
  995. mrcBackgroundRT.name = "backgroundMrcRenderTexture";
  996. camera.targetTexture = mrcBackgroundRT;
  997. PLog.i(TAG_MRC, "init background camera.");
  998. }
  999. void InitForegroundCam(Camera camera)
  1000. {
  1001. camera.backgroundColor = foregroundColor;
  1002. camera.clearFlags = CameraClearFlags.SolidColor;
  1003. camera.stereoTargetEye = StereoTargetEyeMask.None;
  1004. camera.transform.localScale = Vector3.one;
  1005. #if UNITY_2021_3_OR_NEWER
  1006. camera.transform.SetLocalPositionAndRotation(Vector3.zero, Quaternion.identity);
  1007. #else
  1008. camera.transform.localPosition = Vector3.zero;
  1009. camera.transform.localEulerAngles = Vector3.zero;
  1010. #endif
  1011. camera.depth = 10000;
  1012. camera.gameObject.layer = 0;
  1013. camera.orthographic = false;
  1014. camera.fieldOfView = xmlCamData.yFov;
  1015. camera.aspect = xmlCamData.imageW / xmlCamData.imageH;
  1016. camera.allowMSAA = true;
  1017. camera.cullingMask = foregroundLayerMask;
  1018. if (mrcForegroundRT == null)
  1019. {
  1020. mrcForegroundRT = new RenderTexture((int)xmlCamData.imageW, (int)xmlCamData.imageH, 24, RenderTextureFormat.ARGB32);
  1021. }
  1022. mrcForegroundRT.name = "foregroundMrcRenderTexture";
  1023. camera.targetTexture = mrcForegroundRT;
  1024. PLog.i(TAG_MRC, "init foreground camera.");
  1025. }
  1026. public void CalibrationMRCCam()
  1027. {
  1028. if (!PXR_Plugin.System.UPxr_GetMRCEnable() || null == backgroundCamObj || null == foregroundCamObj) return;
  1029. PxrPosef pose = new PxrPosef();
  1030. pose.orientation.x = 0;
  1031. pose.orientation.y = 0;
  1032. pose.orientation.z = 0;
  1033. pose.orientation.w = 0;
  1034. pose.position.x = 0;
  1035. pose.position.y = 0;
  1036. pose.position.z = 0;
  1037. PXR_Plugin.System.UPxr_GetMrcPose(ref pose);
  1038. backgroundCamObj.transform.localPosition = new Vector3(pose.position.x, pose.position.y + height, (-pose.position.z) * 1f);
  1039. foregroundCamObj.transform.localPosition = new Vector3(pose.position.x, pose.position.y + height, (-pose.position.z) * 1f);
  1040. Vector3 rototion = new Quaternion(pose.orientation.x, pose.orientation.y, pose.orientation.z, pose.orientation.w).eulerAngles;
  1041. backgroundCamObj.transform.localEulerAngles = new Vector3(-rototion.x, -rototion.y, -rototion.z);
  1042. foregroundCamObj.transform.localEulerAngles = new Vector3(-rototion.x, -rototion.y, -rototion.z);
  1043. }
  1044. public Vector3 UPxr_ToVector3(float[] translation)
  1045. {
  1046. Debug.Log("translation:" + new Vector3(translation[0], translation[1], -translation[2]).ToString());
  1047. return new Vector3(translation[0], translation[1] + height, (-translation[2]) * 1f);
  1048. }
  1049. public Vector3 UPxr_ToRotation(float[] rotation)
  1050. {
  1051. Quaternion quaternion = new Quaternion(rotation[0], rotation[1], rotation[2], rotation[3]);
  1052. Vector3 vector3 = quaternion.eulerAngles;
  1053. Debug.Log("rotation:" + vector3.ToString());
  1054. return new Vector3(-vector3.x, -vector3.y, -vector3.z);
  1055. }
  1056. public void Pxr_GetHeight()
  1057. {
  1058. height = Camera.main.transform.localPosition.y - PXR_Plugin.System.UPxr_GetMrcY();
  1059. PLog.i(TAG_MRC, "Pxr_GetMrcY+:" + PXR_Plugin.System.UPxr_GetMrcY().ToString());
  1060. }
  1061. #endregion
  1062. }
  1063. public struct CameraData
  1064. {
  1065. public string id;
  1066. public string name;
  1067. public float imageW;
  1068. public float imageH;
  1069. public float yFov;
  1070. public PxrVector3f position;
  1071. public PxrVector4f orientation;
  1072. }
  1073. }
粤ICP备19079148号