PlayCast.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. using LitJson;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Runtime.InteropServices;
  6. using UnityEngine;
  7. using UnityEngine.UI;
  8. //using Vuforia;
  9. public class PlayCast : MonoBehaviour
  10. {
  11. public GameObject rawImage;
  12. [DllImport("NativeCameraPlugin")]
  13. public static extern void SetTextureFromUnity(System.IntPtr texture);
  14. [DllImport("NativeCameraPlugin")]
  15. private static extern IntPtr GetRenderEventFunc();
  16. private AndroidJavaClass jc;
  17. public static PlayCast Instance;
  18. private AndroidJavaObject CurrentActivity;
  19. public GameObject bgChar;
  20. private bool isRePos = false;
  21. // Start is called before the first frame update
  22. void Start()
  23. {
  24. // rimg.texture = rt;
  25. // return;
  26. Instance = this;
  27. Application.targetFrameRate = 30;
  28. jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
  29. CurrentActivity = jc.GetStatic<AndroidJavaObject>("currentActivity");
  30. // tempVuf.position = Vector3.zero;
  31. // tempVuf.eulerAngles = Vector3.zero;
  32. rimg.rectTransform.sizeDelta = new Vector2(Screen.width, Screen.width * 0.5625f);
  33. mr3.rectTransform.sizeDelta = new Vector2(Screen.width, Screen.width * 0.75f);
  34. for (int i = 0; i < delayInt; i++)
  35. {
  36. text2d.Add(new Texture2D(1920, 1080));
  37. }
  38. }
  39. public Text text;
  40. public static int fps;
  41. public static int fps2;
  42. // public RenderTexture renderTexture;
  43. public int TextureID;
  44. SDKCallbackListener aaa;
  45. // WebSocketClient wc;
  46. public TClient tcp;
  47. public void init(string ip)
  48. {
  49. // rimg.texture = rt;
  50. // return;
  51. isChoose = false;
  52. if (tcp != null)
  53. tcp.OnMessaged -= scoketMessage;
  54. CurrentActivity.Call("closeTou");
  55. Invoke("initStart", 1f);
  56. TClient.Instance.InitSocket(ip);
  57. }
  58. void initStart()
  59. {
  60. // rimg.texture = rt;
  61. // return;
  62. //if(wc==null)
  63. // {
  64. aaa = new SDKCallbackListener();
  65. CurrentActivity.Call("initSDK2", aaa);
  66. // wc = WebSocketClient.webSockets["castScreen"];
  67. tcp.OnMessaged += scoketMessage;
  68. JsonData initTcp = new JsonData();
  69. initTcp["action"] = "init";
  70. initTcp["data"] = "init";
  71. TClient.Instance.SocketSend(initTcp.ToJson());
  72. // }
  73. // Invoke("StartCast", 1f);
  74. }
  75. JsonData sendInfo = new JsonData();
  76. public void scoketMessage(string msg)
  77. {
  78. Debug.Log(msg + "scoketMessage接收到的信息");
  79. CurrentActivity.Call("onMessage", msg);
  80. try
  81. {
  82. JsonData datamsg = JsonMapper.ToObject(msg);
  83. {
  84. if (datamsg["type"].ToString() == "RePos")
  85. {
  86. sendInfo = datamsg["data"];
  87. isRePos = true;
  88. // RePos(sendInfo);
  89. }
  90. else if (datamsg["type"].ToString() == "init")
  91. {
  92. CurrentActivity.Call("InitTouPin", "test3");
  93. Invoke("StartCast", 0.2f);
  94. }
  95. }
  96. }
  97. catch
  98. {
  99. }
  100. }
  101. public RawImage rimg;
  102. private IEnumerator CallPluginAtEndOfFrames()
  103. {
  104. while (true)
  105. {
  106. if (TextureID != 0)
  107. {
  108. // float t = Time.time;
  109. GL.IssuePluginEvent(GetRenderEventFunc(), TextureID);
  110. // UnityEngine.Debug.Log(string.Format("totalUnity消耗时间: {0} ms", Time.time - t));
  111. }
  112. // skip one frame
  113. yield return new WaitForSeconds(0.013f);
  114. }
  115. }
  116. Texture2D tex;
  117. public void StartCast()
  118. {
  119. StopCoroutine("CallPluginAtEndOfFrames");
  120. StartCoroutine("CallPluginAtEndOfFrames");
  121. // StopCoroutine(CalculateFramesPerSecond());
  122. // StartCoroutine(CalculateFramesPerSecond());
  123. int width = 1280;
  124. int height = 720;
  125. tex = new Texture2D(width, height, TextureFormat.RGBA32, false);
  126. rimg.texture = tex;
  127. TextureID = tex.GetNativeTexturePtr().ToInt32();
  128. SetTextureFromUnity(tex.GetNativeTexturePtr());
  129. CurrentActivity.Call("jieshou", "0001", tex.GetNativeTexturePtr().ToInt32(), isdiyi);
  130. // bool focusModeSet = CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
  131. isChoose = true;
  132. }
  133. public void closeSocket()
  134. {
  135. if (!bgChar.activeSelf)//如果连接成功,返回列表才断开连接
  136. {
  137. isChoose = false;
  138. CurrentActivity.Call("closeTou");
  139. TClient.Instance.SocketQuit();
  140. }
  141. }
  142. public RenderTexture rt;
  143. bool isdiyi;
  144. public void qiehuan()
  145. {
  146. isChoose = true;
  147. isdiyi = !isdiyi;
  148. CurrentActivity.Call("changeShijiao", isdiyi);
  149. rawImage.SetActive(isdiyi);
  150. }
  151. bool isChoose = false;
  152. public Transform tf;
  153. // Update is called once per frame
  154. public Camera ca2;
  155. JsonData data = new JsonData();
  156. JsonData data2 = new JsonData();
  157. JsonData datapos = new JsonData();
  158. JsonData datapos2 = new JsonData();
  159. // private float _framesPerSecond;
  160. //int FrameCount = 0;
  161. //private IEnumerator CalculateFramesPerSecond()//FPS
  162. //{
  163. // int lastFrameCount = 0;
  164. // while (true)
  165. // {
  166. // yield return new WaitForSecondsRealtime(1f);
  167. // var elapsedFrames = FrameCount - lastFrameCount;
  168. // _framesPerSecond = elapsedFrames / 1f;
  169. // // Debug.Log("FPS====>"+ _framesPerSecond.ToString());
  170. // lastFrameCount = FrameCount;
  171. // }
  172. //}
  173. public Texture2D textBG;
  174. private int nowFps;
  175. private int nowFps2;
  176. private float timefloat;
  177. // PIXEL_FORMAT mPixelFormat = PIXEL_FORMAT.RGB888;
  178. public static Dictionary<string, Texture2D> imageList = new Dictionary<string, Texture2D>();
  179. private int nowIndex = -1;
  180. List<Texture2D> text2d = new List<Texture2D>();
  181. void Update()
  182. {
  183. if (aaa != null)
  184. {
  185. if (aaa.data != null && aaa.data != "")
  186. {
  187. JsonData datalis = JsonMapper.ToObject(aaa.data);
  188. if (datalis["action"].ToString() == "changeResolution")
  189. {
  190. int width = int.Parse(datalis["data"]["w"].ToString());
  191. int height = int.Parse(datalis["data"]["h"].ToString());
  192. tex = new Texture2D(width, height, TextureFormat.RGBA32, false);
  193. TextureID = tex.GetNativeTexturePtr().ToInt32();
  194. CurrentActivity.Call("changeTexturID", tex.GetNativeTexturePtr().ToInt32());
  195. rimg.texture = tex;
  196. }
  197. aaa.data = "";
  198. }
  199. if (isChoose)
  200. {
  201. if (isdiyi)
  202. {
  203. data["action"] = "firstView";
  204. data["data"] = data2;
  205. data2["info"] = "";
  206. TClient.Instance.SocketSend(data.ToJson());
  207. }
  208. else if (!isdiyi)
  209. {
  210. if (isRePos)
  211. {
  212. isRePos = false;
  213. RePos(sendInfo);
  214. }
  215. //temptf.position = new Vector3(tf.position.x-tempVuf.position.x, tf.position.y - tempVuf.position.y, tf.position.z - tempVuf.position.z);
  216. //temptf.position = tf.position - tempVuf.position;
  217. //temptf.eulerAngles = new Vector3(tf.eulerAngles.x-tempVuf.eulerAngles.x, tf.eulerAngles.y - tempVuf.eulerAngles.y, tf.eulerAngles.z - tempVuf.eulerAngles.z);
  218. //temptf.eulerAngles = tf.eulerAngles - tempVuf.eulerAngles;
  219. data["action"] = "thirdView";
  220. data["data"] = data2;
  221. data2["info"] = tf.position.x + "_" + tf.position.y + "_" + tf.position.z + "_" + tf.eulerAngles.x + "_" + tf.eulerAngles.y + "_" + tf.eulerAngles.z;
  222. if (nowIndex >= 0&& text2d[nowIndex]!=null)
  223. {
  224. mr3.texture = text2d[nowIndex];
  225. nowIndex++;
  226. if (nowIndex >= text2d.Count)
  227. nowIndex = 0;
  228. }
  229. // qT2.Add(t2);
  230. //FrameCount++;
  231. // data2["Fps"] = string.Format("{0:F2}", _framesPerSecond);
  232. TClient.Instance.SocketSend(data.ToJson());
  233. }
  234. }
  235. else
  236. {
  237. mr3.texture = textBG;
  238. rimg.texture = textBG;
  239. }
  240. }
  241. }
  242. private int delayInt = 15;
  243. public static int isUpdate = 0;
  244. List<Texture2D> qT2 = new List<Texture2D>();
  245. public Transform ArParent;
  246. private Transform tempVuf;
  247. private Transform temptf;
  248. public RawImage mr3;
  249. public void Relocation()
  250. {
  251. datapos["action"] = "Relocation";
  252. datapos["data"] = datapos2;
  253. datapos2["info"] = "";
  254. TClient.Instance.SocketSend(datapos.ToJson());
  255. }
  256. private void RePos(JsonData sendInfo)
  257. {
  258. if (sendInfo["method"].ToString() == "Relocation")
  259. {
  260. //tempVuf.position = tf.position;
  261. // tempVuf.eulerAngles = tf.eulerAngles;
  262. string[] strs = sendInfo["pose"].ToString().Split('_');
  263. ArParent.position = new Vector3(float.Parse(strs[0]), float.Parse(strs[1]), float.Parse(strs[2]));
  264. ArParent.eulerAngles = new Vector3(float.Parse(strs[3]), float.Parse(strs[4]), float.Parse(strs[5]));
  265. }
  266. }
  267. }