SpotsItem.cs 14 KB

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