PXR_ControllerLoader.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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.Collections;
  12. using System.IO;
  13. using LitJson;
  14. using UnityEngine;
  15. namespace Unity.XR.PXR
  16. {
  17. public class PXR_ControllerLoader : MonoBehaviour
  18. {
  19. [SerializeField]
  20. private PXR_Input.Controller hand;
  21. public GameObject neo3L;
  22. public GameObject neo3R;
  23. public GameObject PICO_4L;
  24. public GameObject PICO_4R;
  25. public GameObject G3;
  26. public Material legacyMaterial;
  27. private Texture2D modelTexture2D;
  28. private int controllerType = -1;
  29. private JsonData curControllerData = null;
  30. private int systemOrLocal = 0;
  31. private bool loadModelSuccess = false;
  32. private string modelName = "";
  33. private string texFormat = "";
  34. private string prePath = "";
  35. private string modelFilePath = "/system/media/pxrRes/controller/";
  36. private bool leftControllerState = false;
  37. private bool rightControllerState = false;
  38. private enum ControllerSimulationType
  39. {
  40. None,
  41. Neo3,
  42. PICO4,
  43. G3
  44. }
  45. #if UNITY_EDITOR
  46. [SerializeField]
  47. private ControllerSimulationType controllerSimulation = ControllerSimulationType.None;
  48. #endif
  49. public PXR_ControllerLoader(PXR_Input.Controller controller)
  50. {
  51. hand = controller;
  52. }
  53. void Awake()
  54. {
  55. #if UNITY_EDITOR
  56. switch (controllerSimulation)
  57. {
  58. case ControllerSimulationType.Neo3:
  59. {
  60. Instantiate(hand == PXR_Input.Controller.LeftController ? neo3L : neo3R, transform, false);
  61. break; ;
  62. }
  63. case ControllerSimulationType.PICO4:
  64. {
  65. Instantiate(hand == PXR_Input.Controller.LeftController ? PICO_4L : PICO_4R, transform, false);
  66. break; ;
  67. }
  68. case ControllerSimulationType.G3:
  69. {
  70. Instantiate(G3, transform, false);
  71. break; ;
  72. }
  73. }
  74. #endif
  75. }
  76. void Start()
  77. {
  78. controllerType = PXR_Plugin.Controller.UPxr_GetControllerType();
  79. #if UNITY_ANDROID && !UNITY_EDITOR
  80. LoadResFromJson();
  81. #endif
  82. leftControllerState = PXR_Input.IsControllerConnected(PXR_Input.Controller.LeftController);
  83. rightControllerState = PXR_Input.IsControllerConnected(PXR_Input.Controller.RightController);
  84. if (hand == PXR_Input.Controller.LeftController)
  85. RefreshController(PXR_Input.Controller.LeftController);
  86. if (hand == PXR_Input.Controller.RightController)
  87. RefreshController(PXR_Input.Controller.RightController);
  88. }
  89. void Update()
  90. {
  91. if (hand == PXR_Input.Controller.LeftController)
  92. {
  93. if (PXR_Input.IsControllerConnected(PXR_Input.Controller.LeftController))
  94. {
  95. if (!leftControllerState)
  96. {
  97. controllerType = PXR_Plugin.Controller.UPxr_GetControllerType();
  98. RefreshController(PXR_Input.Controller.LeftController);
  99. leftControllerState = true;
  100. }
  101. }
  102. else
  103. {
  104. if (leftControllerState)
  105. {
  106. DestroyLocalController();
  107. leftControllerState = false;
  108. }
  109. }
  110. }
  111. if (hand == PXR_Input.Controller.RightController)
  112. {
  113. if (PXR_Input.IsControllerConnected(PXR_Input.Controller.RightController))
  114. {
  115. if (!rightControllerState)
  116. {
  117. controllerType = PXR_Plugin.Controller.UPxr_GetControllerType();
  118. RefreshController(PXR_Input.Controller.RightController);
  119. rightControllerState = true;
  120. }
  121. }
  122. else
  123. {
  124. if (rightControllerState)
  125. {
  126. DestroyLocalController();
  127. rightControllerState = false;
  128. }
  129. }
  130. }
  131. }
  132. private void RefreshController(PXR_Input.Controller hand)
  133. {
  134. if (PXR_Input.IsControllerConnected(hand))
  135. {
  136. if (systemOrLocal == 0)
  137. {
  138. LoadControllerFromPrefab(hand);
  139. if (!loadModelSuccess)
  140. {
  141. LoadControllerFromSystem((int)hand);
  142. }
  143. }
  144. else
  145. {
  146. var isControllerExist = false;
  147. foreach (Transform t in transform)
  148. {
  149. if (t.name == modelName)
  150. {
  151. isControllerExist = true;
  152. }
  153. }
  154. if (!isControllerExist)
  155. {
  156. LoadControllerFromSystem((int)hand);
  157. if (!loadModelSuccess)
  158. {
  159. LoadControllerFromPrefab(hand);
  160. }
  161. }
  162. else
  163. {
  164. var currentController = transform.Find(modelName);
  165. currentController.gameObject.SetActive(true);
  166. }
  167. }
  168. }
  169. }
  170. private void LoadResFromJson()
  171. {
  172. string json = PXR_Plugin.System.UPxr_GetObjectOrArray("config.controller", (int)ResUtilsType.TypeObjectArray);
  173. if (json != null)
  174. {
  175. JsonData jdata = JsonMapper.ToObject(json);
  176. if (controllerType > 0)
  177. {
  178. if (jdata.Count >= controllerType)
  179. {
  180. curControllerData = jdata[controllerType - 1];
  181. if (curControllerData != null)
  182. {
  183. modelFilePath = (string)curControllerData["base_path"];
  184. modelName = (string)curControllerData["model_name"] + "_sys";
  185. }
  186. }
  187. }
  188. }
  189. else
  190. {
  191. Debug.LogError("PXRLog LoadJsonFromSystem Error");
  192. }
  193. }
  194. private void DestroyLocalController()
  195. {
  196. foreach (Transform t in transform)
  197. {
  198. Destroy(modelTexture2D);
  199. Destroy(t.gameObject);
  200. Resources.UnloadUnusedAssets();
  201. loadModelSuccess = false;
  202. }
  203. }
  204. private void LoadControllerFromPrefab(PXR_Input.Controller hand)
  205. {
  206. switch (controllerType)
  207. {
  208. case 5:
  209. Instantiate(hand == PXR_Input.Controller.LeftController ? neo3L : neo3R, transform, false);
  210. loadModelSuccess = true;
  211. break;
  212. case 6:
  213. Instantiate(hand == PXR_Input.Controller.LeftController ? PICO_4L : PICO_4R, transform, false);
  214. loadModelSuccess = true;
  215. break;
  216. case 7:
  217. Instantiate(G3, transform, false);
  218. loadModelSuccess = true;
  219. break;
  220. default:
  221. loadModelSuccess = false;
  222. break;
  223. }
  224. }
  225. private void LoadControllerFromSystem(int id)
  226. {
  227. var sysControllerName = controllerType.ToString() + id.ToString() + ".obj";
  228. var fullFilePath = modelFilePath + sysControllerName;
  229. if (!File.Exists(fullFilePath))
  230. {
  231. Debug.Log("PXRLog Load Obj From Prefab");
  232. }
  233. else
  234. {
  235. GameObject go = new GameObject
  236. {
  237. name = modelName
  238. };
  239. MeshFilter meshFilter = go.AddComponent<MeshFilter>();
  240. meshFilter.mesh = PXR_ObjImporter.Instance.ImportFile(fullFilePath);
  241. go.transform.SetParent(transform);
  242. go.transform.localPosition = Vector3.zero;
  243. MeshRenderer meshRenderer = go.AddComponent<MeshRenderer>();
  244. meshRenderer.material = legacyMaterial;
  245. LoadTexture(meshRenderer, controllerType.ToString() + id.ToString(), false);
  246. go.transform.localRotation = Quaternion.Euler(new Vector3(0, 180, 0));
  247. go.transform.localScale = new Vector3(-0.01f, 0.01f, 0.01f);
  248. loadModelSuccess = true;
  249. }
  250. }
  251. private void LoadTexture(MeshRenderer mr,string controllerName, bool fromRes)
  252. {
  253. if (fromRes)
  254. {
  255. texFormat = "";
  256. prePath = controllerName;
  257. }
  258. else
  259. {
  260. texFormat = "." + (string)curControllerData["tex_format"];
  261. prePath = modelFilePath + controllerName;
  262. }
  263. var texturePath = prePath + "_idle" + texFormat;
  264. mr.material.SetTexture("_MainTex", LoadOneTexture(texturePath, fromRes));
  265. }
  266. private Texture2D LoadOneTexture(string filepath, bool fromRes)
  267. {
  268. if (fromRes)
  269. {
  270. return Resources.Load<Texture2D>(filepath);
  271. }
  272. else
  273. {
  274. int tW = (int)curControllerData["tex_width"];
  275. int tH = (int)curControllerData["tex_height"];
  276. modelTexture2D = new Texture2D(tW, tH);
  277. modelTexture2D.LoadImage(ReadPNG(filepath));
  278. return modelTexture2D;
  279. }
  280. }
  281. private byte[] ReadPNG(string path)
  282. {
  283. FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);
  284. fileStream.Seek(0, SeekOrigin.Begin);
  285. byte[] binary = new byte[fileStream.Length];
  286. fileStream.Read(binary, 0, (int)fileStream.Length);
  287. fileStream.Close();
  288. fileStream.Dispose();
  289. return binary;
  290. }
  291. }
  292. }
粤ICP备19079148号