SpotsItem.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. using LitJson;
  2. using Newtonsoft.Json;
  3. using SC.XR.Unity.Module_InputSystem;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. using Vuforia;
  8. using XRTool.Util;
  9. public class SpotsItem
  10. {
  11. public List<sspos> position;
  12. public string id;
  13. public string name;
  14. public string triggerImageMark;
  15. public Vector3 pos;
  16. public Vector3 eul;
  17. public string updateTime;
  18. public string triggerImage;
  19. public int triggerImageId;
  20. public UserSceneItem usi;
  21. //景点中的模型
  22. public Dictionary<string, ModelList> modelList = new Dictionary<string, ModelList>();
  23. public GameObject VuforiaItem;
  24. GameObject _spotsObj;
  25. public GameObject SpotsObj
  26. {
  27. get
  28. {
  29. if (_spotsObj == null)
  30. {
  31. VuforiaItem = new GameObject("Vufroia--"+name);
  32. VuforiaItem.transform.parent = usi.SceneModel.transform;
  33. VuforiaItem.transform.localPosition = Vector3.zero;
  34. VuforiaItem.transform.localEulerAngles = Vector3.zero;
  35. VuforiaItem.SetActive(false);
  36. _spotsObj = new GameObject("景点--"+name);
  37. _spotsObj.transform.parent = VuforiaItem.transform;
  38. }
  39. return _spotsObj;
  40. }
  41. }
  42. public ObserverBehaviour obVuforia;
  43. public bool isInitModel;
  44. public void Clear()
  45. {
  46. Debug.Log("Clear SpotsList modelList");
  47. foreach (var i in modelList.Values)
  48. {
  49. i.Clear();
  50. }
  51. // Debug.Log("Clear SpotsList obVuforia");
  52. // GameManager.Instance.destroyGo(this.obVuforia.gameObject);
  53. //GameManager.Instance.destroyGo(_spotsObj);
  54. // _spotsObj = null;
  55. //Debug.Log("Clear SpotsList obVuforia VuforiaItem");
  56. //GameManager.Instance.destroyGo(VuforiaItem);
  57. }
  58. public void setObVuforia(ObserverBehaviour obVuforia)
  59. {
  60. this.obVuforia = obVuforia;
  61. Debug.Log("添加Vuforia监听" + obVuforia.TargetName+" 景点===》"+name);
  62. this.obVuforia.OnTargetStatusChanged += OnTargetStatusChanged;
  63. obVuforia.gameObject.name = "ImageTarget_" + obVuforia.TargetName;
  64. obVuforia.gameObject.AddComponent<TurnOffBehaviour>();
  65. if (obVuforia.gameObject.GetComponent<DefaultObserverEventHandler>())
  66. {
  67. GameObject.Destroy(obVuforia.gameObject.GetComponent<DefaultObserverEventHandler>());
  68. }
  69. obVuforia.gameObject.AddComponent<DefaultObserverEventHandler>();
  70. DefaultObserverEventHandler imageaction = obVuforia.gameObject.GetComponent<DefaultObserverEventHandler>();
  71. obj = new GameObject("Cube");
  72. obj.transform.parent = imageaction.transform;
  73. obj.transform.localEulerAngles = new Vector3(90, 0, 0);
  74. obj.transform.localPosition = Vector3.zero;
  75. }
  76. GameObject obj;
  77. void OnTargetStatusChanged(ObserverBehaviour behaviour, TargetStatus status)
  78. {
  79. if (status.Status == Status.TRACKED)
  80. {
  81. Debug.Log("OnTargetStatusChanged 发现 " + behaviour.TargetName + "status ==>" + status.Status.ToString());
  82. if(obVuforia.TargetName == behaviour.TargetName)
  83. {
  84. foreach (var item in usi.SpotsList.Keys)
  85. {
  86. if (usi.SpotsList[item].obVuforia.TargetName == behaviour.TargetName)
  87. {
  88. if (usi.SpotsList[item].VuforiaItem.activeSelf&& usi.SpotsList[item].id!=id )
  89. {
  90. usi.SpotsList[item].VuforiaItem.SetActive(false);
  91. }
  92. else if(usi.SpotsList[item].id == id)
  93. {
  94. VuforiaItem.SetActive(true);
  95. }
  96. }
  97. else
  98. {
  99. usi.SpotsList[item].VuforiaItem.SetActive(false);
  100. }
  101. }
  102. Debug.Log("OnTargetStatusChanged 发现 VuforiaItem " + VuforiaItem.name);
  103. TimerMgr.Instance.CreateTimer(()=> {
  104. VuforiaItem.transform.position = obj.transform.position;
  105. VuforiaItem.transform.eulerAngles = obj.transform.eulerAngles;
  106. VuforiaItem.transform.eulerAngles = new Vector3(0, VuforiaItem.transform.eulerAngles.y, 0);
  107. Debug.Log("OnTargetStatusChanged 发现 " + obj.transform.position + "status ==>" + obj.transform.eulerAngles);
  108. }, 0.1f,10);
  109. foreach (var i in modelList.Values)
  110. {
  111. Debug.Log("initModelObj===> " + i.id);
  112. i._model = SpotsObj;
  113. i.initModelObj();
  114. }
  115. isInitModel = true;
  116. /*
  117. if (!isInitModel)
  118. {
  119. Dictionary<string, ModelList> NewmodelList = new Dictionary<string, ModelList>();
  120. foreach (var mlkeys in modelList.Keys)
  121. {
  122. ModelList ml = modelList[mlkeys];
  123. ml._model = GameObject.Instantiate<GameObject>(ml.Model);
  124. JsonData msg = UserSceneManager.smsg;
  125. for (int i = 0; i < msg["listSpoit"].Count; i++)
  126. {
  127. Debug.Log("DGJ2 ==New1==> " + this.id + "_" + msg["listSpoit"][i]["id"].ToString());
  128. if (this.id == msg["listSpoit"][i]["id"].ToString())
  129. {
  130. for (int j = 0; j < msg["listSpoit"][i]["material"].Count; j++)
  131. {
  132. if (msg["listSpoit"][i]["material"][j]["materialList"] != null && msg["listSpoit"][i]["material"][j]["materialList"].IsArray)
  133. {
  134. for (int z = 0; z < msg["listSpoit"][i]["material"][j]["materialList"].Count; z++)
  135. {
  136. Debug.Log("DGJ2 ==New1==> " + this.triggerImage + "_" + msg["listSpoit"][i]["material"][j]["materialList"][z]["id"].ToJson() + "_" + (msg["listSpoit"][i]["material"][j]["materialList"][z]["objectTransform"].ToJson()));
  137. ml.materialList[z].objectTransform = JsonConvert.DeserializeObject<ObjectTransform>(msg["listSpoit"][i]["material"][j]["materialList"][z]["objectTransform"].ToJson());
  138. //modellist.materialList[z].initModelObj();
  139. Debug.Log("DGJ2 ==New1==> " + ml.materialList[z].id + "_" + msg["listSpoit"][i]["material"][j]["materialList"][z]["id"].ToJson());
  140. ml.materialList[z].name = this.triggerImage;
  141. // GameManager.Instance.StartCoroutine(modellist.materialList[z].initFrist());
  142. }
  143. }
  144. }
  145. }
  146. }
  147. NewmodelList.Add(mlkeys, ml);
  148. for (int i = 0; i < ml.materialList.Count; i++)
  149. {
  150. if (ml._model.transform.GetChild(0).GetComponent<YangShiManager>())
  151. {
  152. if (ml.materialList[i]._model)
  153. {
  154. ml.materialList[i]._model = ml._model.transform.GetChild(0).GetComponent<YangShiManager>().yangshiList[i].useObj;
  155. ml.materialList[i].prefabModel = ml.materialList[i]._model;
  156. // ml.materialList[i].initFrist();
  157. }
  158. else
  159. {
  160. ml.materialList[i]._model = ml._model.transform.GetChild(0).GetComponent<YangShiManager>().yangshiText.gameObject;
  161. ml.materialList[i].prefabModel = ml.materialList[i]._model;
  162. // ml.materialList[i].initFrist();
  163. }
  164. }
  165. else
  166. {
  167. ml.materialList[i]._model = ml._model.transform.GetChild(i).gameObject;
  168. ml.materialList[i].prefabModel = ml._model.transform.GetChild(i).gameObject;
  169. // ml.materialList[i].initFrist();
  170. }
  171. }
  172. ml.Model.SetActive(true);
  173. ml.Model.transform.parent = _spotsObj.transform;
  174. GameObject modelList2 = ml.Model;
  175. BoundingBox bb = modelList2.GetComponent<BoundingBox>();
  176. if (bb)
  177. {
  178. GameObject.Destroy(bb);
  179. }
  180. NearInterationGrabbable ng = modelList2.GetComponent<NearInterationGrabbable>();
  181. if (ng)
  182. {
  183. GameObject.Destroy(ng);
  184. }
  185. ManipulationHandler mh = modelList2.GetComponent<ManipulationHandler>();
  186. if (mh)
  187. {
  188. GameObject.Destroy(mh);
  189. }
  190. BoxCollider bc = modelList2.GetComponent<BoxCollider>();
  191. if (!bc)
  192. {
  193. bc = modelList2.AddComponent<BoxCollider>();
  194. bc.size = new Vector3(bc.size.x, bc.size.y, 0.01f);
  195. }
  196. bb = modelList2.AddComponent<BoundingBox>();
  197. bb.FlattenAxis = BoundingBox.FlattenModeType.FlattenZ;
  198. bb.ActiveHandle = BoundingBox.HandleType.Rotation | BoundingBox.HandleType.Scale;
  199. ng = modelList2.AddComponent<NearInterationGrabbable>();
  200. mh = modelList2.AddComponent<ManipulationHandler>();
  201. TimerMgr.Instance.CreateTimer(() =>
  202. {
  203. bb.enabled = GameManager.m_IsStartEditorzu;
  204. if (modelList2.transform.Find("BoundingBox") && modelList2.transform.Find("BoundingBox").gameObject != null)
  205. modelList2.transform.Find("BoundingBox").gameObject.SetActive(GameManager.m_IsStartEditorzu);
  206. if (bc)
  207. {
  208. bc.enabled = GameManager.m_IsStartEditorzu;
  209. }
  210. if (mh)
  211. mh.enabled = GameManager.m_IsStartEditorzu;
  212. if (ng)
  213. ng.enabled = GameManager.m_IsStartEditorzu;
  214. }, 1, -1);
  215. ml.initModelObj();
  216. }
  217. modelList = NewmodelList;
  218. isInitModel = true;
  219. }*/
  220. }
  221. UserSceneUI.Instance.obj.GetComponent<UserChooseScene>().st.SetActive(false);
  222. SendNetConnectPointTrigger sendNet = new SendNetConnectPointTrigger();
  223. sendNet.sn = SendSN.GetSN();
  224. //sendNet.sn = API_GSXR_Slam.SlamManager.plugin.SN;
  225. sendNet.projectId = int.Parse(usi.id);
  226. sendNet.id =int.Parse(id);
  227. string jsonData = JsonMapper.ToJson(sendNet);
  228. Debug.Log(jsonData);
  229. HttpTool.Instance.Post(HttpAction.sn_viewpoint, jsonData, (string msg)=> { });
  230. }
  231. else
  232. {
  233. Debug.Log("OnTargetStatusChanged 消失 " + behaviour.TargetName + "status ==>" + status.Status.ToString());
  234. // if (obVuforia.TargetName == behaviour.TargetName)
  235. // SpotsObj.SetActive(false);
  236. }
  237. }
  238. public void initSpoitTfInfo(JsonData data)
  239. {
  240. if (data["nowScale"] != null && (data["nowScale"]["x"].ToString() != "0" || data["nowScale"]["y"].ToString() != "0" || data["nowScale"]["z"].ToString() != "0"))
  241. {
  242. SpotsObj.transform.localScale = new Vector3(float.Parse(data["nowScale"]["x"].ToString()), float.Parse(data["nowScale"]["y"].ToString()), float.Parse(data["nowScale"]["z"].ToString()));
  243. }
  244. else
  245. {
  246. SpotsObj.transform.localScale = Vector3.one;
  247. }
  248. if (data["nowPos"] != null && (data["nowPos"]["x"].ToString() != "0" || data["nowPos"]["y"].ToString() != "0" || data["nowPos"]["z"].ToString() != "0"))
  249. {
  250. SpotsObj.transform.localPosition = new Vector3(float.Parse(data["nowPos"]["x"].ToString()), float.Parse(data["nowPos"]["y"].ToString()), float.Parse(data["nowPos"]["z"].ToString()));
  251. }
  252. else
  253. {
  254. SpotsObj.transform.localPosition = Vector3.zero;
  255. }
  256. if (data["nowRot"] != null && (data["nowRot"]["x"].ToString() != "0" || data["nowRot"]["y"].ToString() != "0" || data["nowRot"]["z"].ToString() != "0"))
  257. {
  258. SpotsObj.transform.localEulerAngles = new Vector3(float.Parse(data["nowRot"]["x"].ToString()), float.Parse(data["nowRot"]["y"].ToString()), float.Parse(data["nowRot"]["z"].ToString()));
  259. }
  260. else
  261. {
  262. SpotsObj.transform.localEulerAngles = Vector3.zero;
  263. }
  264. }
  265. public void init()
  266. {
  267. VuforiaItem.transform.parent = usi.SceneModel.transform;
  268. VuforiaItem.transform.localPosition = Vector3.zero;
  269. VuforiaItem.transform.localEulerAngles = Vector3.zero;
  270. VuforiaItem.SetActive(false);
  271. }
  272. public void show()
  273. {
  274. foreach(var v in modelList.Values)
  275. {
  276. v.init(true);
  277. }
  278. }
  279. }