/// <summary>
        /// Parses the networkConfig string for the clients TCP info and room name to send to the client
        /// Retrieves the room from TangoDatabase based on it's name
        /// Establishes a TCP connection with the client and sends the byte data for the room
        /// </summary>
        /// <param name="networkConfig"></param>
        void SendTangoMeshThread(string networkConfig)
        {
            var    networkConfigArray = networkConfig.Split('~');
            string name = networkConfigArray[2];

            UnityEngine.Debug.Log("Sending Tango Mesh Master Client " + name);

            TcpClient client = new TcpClient();

            client.Connect(IPAddress.Parse(networkConfigArray[0]), System.Int32.Parse(networkConfigArray[1]));

            using (NetworkStream stream = client.GetStream())
            {
                TangoDatabase.TangoRoom T = TangoDatabase.GetRoomByName(name);

                //write the parent id
                var parentData = BitConverter.GetBytes(T.parentID);
                stream.Write(parentData, 0, parentData.Length);

                var data = T._meshes;
                stream.Write(data, 0, data.Length);
                UnityEngine.Debug.Log("Mesh sent: mesh size = " + data.Length);
            }
            client.Close();

            Thread.CurrentThread.Join();
        }
 public override void SendTangoMesh(int id)
 {
     if (TangoDatabase.GetMeshAsBytes() != null)
     {
         photonView.RPC("ReceiveTangoMesh", PhotonPlayer.Find(id), GetLocalIpAddress() + ":" + Port);
     }
 }
Beispiel #3
0
        public override void RecieveTangoRoomInfo(string data)
        {
            var dataArray = data.Split('~'); //parse data

            UnityEngine.Debug.Log("RecievedTangoInfo " + dataArray.Length);
            List <String> RoomNames = new List <string>();

            if (dataArray.Length > 1)
            {
                for (int i = 0; i < dataArray.Length; i += 2)
                {
                    TangoDatabase.TangoData T = new TangoDatabase.TangoData();
                    T.name = dataArray[i];
                    RoomNames.Add(T.name);
                    T.size = System.Int32.Parse(dataArray[i + 1]);

                    UnityEngine.Debug.Log(i + " " + T.name + " " + T.size);
                    if (!TangoDatabase.LookUpName(T.name))
                    {
                        TangoRoomStack.Push(T); //if room does not already exist, add to stack
                    }
                }
            }
            else
            {
                RoomNames.Add(" ");
            }

            TangoDatabase.CompareList(RoomNames); //delete any rooms missing
        }
        public override void ReceiveTangoMesh(int id)
        {
            // Setup TCPListener to wait and receive mesh
            this.DeleteLocalMesh();
            TcpListener receiveTcpListener = new TcpListener(IPAddress.Any, Port + 1);

            receiveTcpListener.Start();
            new Thread(() =>
            {
                var client = receiveTcpListener.AcceptTcpClient();
                using (var stream = client.GetStream())
                {
                    byte[] data = new byte[1024];

                    Debug.Log("Start receiving mesh");
                    using (MemoryStream ms = new MemoryStream())
                    {
                        int numBytesRead;
                        while ((numBytesRead = stream.Read(data, 0, data.Length)) > 0)
                        {
                            ms.Write(data, 0, numBytesRead);
                        }
                        Debug.Log("finish receiving mesh: size = " + ms.Length);
                        client.Close();
                        TangoDatabase.UpdateMesh(ms.ToArray());
                    }
                }
                client.Close();
                receiveTcpListener.Stop();
                photonView.RPC("ReceiveTangoMesh", PhotonTargets.Others, GetLocalIpAddress() + ":" + Port);
            }).Start();

            photonView.RPC("SendTangoMesh", PhotonPlayer.Find(id), GetLocalIpAddress() + ":" + (Port + 1));
        }
        /// <summary>
        /// Establishes a TCPListener to recieve a connection from the client
        /// Reads the byte stream and updates the mesh in the TangoDatabase.cs
        /// Terminates the Thread once finished
        /// </summary>
        /// <param name="id"></param>
        /// <param name="size"></param>
        void RecieveTangoMeshThread(int id, int size)
        {
            TcpListener receiveTcpListener = new TcpListener(IPAddress.Any, Port + 1);

            receiveTcpListener.Start();
            var client = receiveTcpListener.AcceptTcpClient();

            using (var stream = client.GetStream())
            {
                byte[] data = new byte[size];

                Debug.Log("Start receiving mesh");
                using (MemoryStream ms = new MemoryStream())
                {
                    int numBytesRead;
                    while ((numBytesRead = stream.Read(data, 0, data.Length)) > 0)
                    {
                        ms.Write(data, 0, numBytesRead);
                    }
                    Debug.Log("finish receiving mesh: size = " + ms.Length);
                    client.Close();
                    TangoDatabase.UpdateMesh(ms.ToArray(), id);
                }
            }
            client.Close();
            receiveTcpListener.Stop();

            Thread.CurrentThread.Join();
        }
Beispiel #6
0
        private void UpdateMesh()
        {
            var tangoApplication =
                GameObject.Find("Tango Manager")
                .GetComponent <TangoApplication>();
            List <Vector3> vertices  = new List <Vector3>();
            List <Vector3> normals   = new List <Vector3>();
            List <Color32> colors    = new List <Color32>();
            List <int>     triangles = new List <int>();

            tangoApplication.Tango3DRExtractWholeMesh(vertices, normals, colors,
                                                      triangles);
            Mesh mesh = new Mesh();

            mesh.vertices  = vertices.ToArray();
            mesh.normals   = normals.ToArray();
            mesh.colors32  = colors.ToArray();
            mesh.triangles = triangles.ToArray();
            List <Mesh> meshList = new List <Mesh>();

            meshList.Add(mesh);

            TangoDatabase.UpdateMesh(meshList);
            Debug.Log("Mesh Updated");
        }
        /// <summary>
        /// Sends a list of all Tango Rooms currently being stored in the TangoDatabase.cs to all Players
        /// </summary>
        public void SendTangoRoomInfoAll()
        {
            string data = "";

            if (TangoDatabase.Rooms.Count > 0)
            {
                data = TangoDatabase.GetAllRooms();
            }

            photonView.RPC("RecieveTangoRoomInfo", PhotonTargets.All, data);
        }
        public override void SendTangoRoomInfo(int id)
        {
            string data = "";

            if (TangoDatabase.Rooms.Count > 0)
            {
                data = TangoDatabase.GetAllRooms();
            }

            photonView.RPC("RecieveTangoRoomInfo", PhotonPlayer.Find(id), data);
        }
        /// <summary>
        /// Gets all information from the Tango Dynamic Mesh and modifies it based on marker information.
        /// </summary>
        private void UpdateMesh()
        {
            //create lists and populate them with dynamic mesh info
            var tangoApplication =
                GameObject.Find("Tango Manager")
                .GetComponent <TangoApplication>();
            List <Vector3> vertices  = new List <Vector3>();
            List <Vector3> normals   = new List <Vector3>();
            List <Color32> colors    = new List <Color32>();
            List <int>     triangles = new List <int>();

            tangoApplication.Tango3DRExtractWholeMesh(vertices, normals, colors,
                                                      triangles);

            //get current marker tranform information and apply it to every vert
            Vector3    V;
            Quaternion Q;
            Transform  T = GameObject.Find("Dynamic_GameObjects").transform;

            V = T.transform.position;
            Q = T.transform.rotation;

            float   angle;
            Vector3 axis;

            Q.ToAngleAxis(out angle, out axis);
            Q = Quaternion.AngleAxis(-angle, axis);

            for (int i = 0; i < vertices.Count; i++)
            {
                vertices[i] -= V;
                vertices[i]  = Q * vertices[i]; //inverse Q
            }

            //write the info to the mesh
            Mesh mesh = new Mesh();

            mesh.vertices  = vertices.ToArray();
            mesh.normals   = normals.ToArray();
            mesh.colors32  = colors.ToArray();
            mesh.triangles = triangles.ToArray();
            List <Mesh> meshList = new List <Mesh>();

            meshList.Add(mesh);

            //update mesh with info
            TangoDatabase.UpdateMesh(meshList);
            Debug.Log("Mesh Updated");
        }
Beispiel #10
0
        /// <summary>
        /// Listens for the TCP connection from the master client and recieves the room mesh
        /// </summary>
        /// <param name="T"></param>
        void RecieveTangoMeshThread(TangoDatabase.TangoData T)
        {
            TcpListener receiveTcpListener = new TcpListener(IPAddress.Any, (Port + 1));

            try
            {
                receiveTcpListener.Start();
            }
            catch (SocketException)
            {
                UnityEngine.Debug.Log("Error");
                _ThreadFinished = true;
                Thread.CurrentThread.Abort();
            }

            var client = receiveTcpListener.AcceptTcpClient();

            using (var stream = client.GetStream())
            {
                byte[] parentData = new byte[4];
                byte[] data       = new byte[T.size];

                UnityEngine.Debug.Log("Start receiving mesh " + data.Length);
                using (MemoryStream ms = new MemoryStream())
                {
                    //Get the parent of the mesh
                    stream.Read(parentData, 0, 4);
                    int parentID = BitConverter.ToInt32(parentData, 0);

                    int numBytesRead;
                    while ((numBytesRead = stream.Read(data, 0, data.Length)) > 0)
                    {
                        ms.Write(data, 0, numBytesRead);
                    }
                    UnityEngine.Debug.Log("finish receiving mesh: size = " + ms.Length);
                    TangoDatabase.UpdateMesh(ms.ToArray(), T.name, parentID);
                }
            }

            client.Close();

            receiveTcpListener.Stop();

            _ThreadFinished = true;

            UnityEngine.Debug.Log("Join Thread");
            Thread.CurrentThread.Join();
        }
Beispiel #11
0
        /// <summary>
        /// Creates a thread to recieve a TangoMesh based on the mesh currently at the top of the stack
        /// Sends a RPC call to the masterclient with the name of the room
        /// </summary>
        public virtual void ReceiveTangoMeshStack()
        {
            TangoDatabase.TangoData T = TangoRoomStack.Pop();

            if (!TangoDatabase.LookUpName(T.name))
            {
                _ThreadFinished = false;
                Thread Th = new Thread(() => RecieveTangoMeshThread(T));
                Th.IsBackground = true;
                Th.Start();
                threads.Add(Th);


                photonView.RPC("SendTangoMeshByName", PhotonTargets.MasterClient, GetLocalIpAddress() + "~" + (Port + 1) + "~" + T.name);
            }
        }
        /// <summary>
        /// Called once per frame
        /// When a new mesh is recieved, display it
        /// When L is pressed, load and send a saved Room Mesh file (used for testing without HoloLens)
        /// </summary>
        public override void Update()
        {
            // ERROR TESTING - REMOVE THIS METHOD - NOTHING SPECIAL HAPPENS IN IT ANYMORE
            base.Update();


            //    //AssetBundle object instantiation for testing purposes
            //    if (Input.GetKeyDown("i"))
            //    {
            //        int id1 = PhotonNetwork.AllocateViewID();

            //        photonView.RPC("SpawnNetworkObject", PhotonTargets.AllBuffered, transform.position, transform.rotation, id1, "Cube");
            //    }
            //}

            if (TangoDatabase.LastUpdate != DateTime.MinValue && DateTime.Compare(_lastUpdate, TangoDatabase.LastUpdate) < 0)
            {
                if (TangoDatabase.GetMeshAsBytes() != null)
                {
                    //    Create a material to apply to the mesh
                    Material meshMaterial = new Material(Shader.Find("Diffuse"));

                    //    grab the meshes in the database
                    IEnumerable <Mesh> temp = new List <Mesh>(TangoDatabase.GetMeshAsList());

                    foreach (var mesh in temp)
                    {
                        //        for each mesh in the database, create a game object to represent
                        //        and display the mesh in the scene
                        GameObject obj1 = new GameObject("tangomesh");

                        //        add a mesh filter to the object and assign it the mesh
                        MeshFilter filter = obj1.AddComponent <MeshFilter>();
                        filter.mesh = mesh;

                        //        add a mesh rendererer and add a material to it
                        MeshRenderer rend1 = obj1.AddComponent <MeshRenderer>();
                        rend1.material = meshMaterial;

                        rend1.material.shader = Shader.Find("Custom/UnlitVertexColor");
                    }
                }
                _lastUpdate = TangoDatabase.LastUpdate;
            }
        }
        public override void SendTangoMesh(string networkConfig)
        {
            var networkConfigArray = networkConfig.Split(':');

            TcpClient client = new TcpClient();

            client.Connect(IPAddress.Parse(networkConfigArray[0]), Int32.Parse(networkConfigArray[1]));
            new Thread(() =>
            {
                using (NetworkStream stream = client.GetStream())
                {
                    var data = TangoDatabase.GetMeshAsBytes();
                    stream.Write(data, 0, data.Length);
                    Debug.Log("Mesh sent: mesh size = " + data.Length);
                }
                client.Close();
            }).Start();
        }
        public override void ReceiveTangoMesh(string networkConfig)
        {
            this.DeleteLocalMesh();
            var networkConfigArray = networkConfig.Split(':');

            TcpClient client = new TcpClient();

            client.Connect(IPAddress.Parse(networkConfigArray[0]), Int32.Parse(networkConfigArray[1]));

            using (var stream = client.GetStream())
            {
                byte[] data = new byte[1024];

                Debug.Log("Start receiving mesh.");
                using (MemoryStream ms = new MemoryStream())
                {
                    int numBytesRead;
                    while ((numBytesRead = stream.Read(data, 0, data.Length)) > 0)
                    {
                        ms.Write(data, 0, numBytesRead);
                    }
                    Debug.Log("Finish receiving mesh: size = " + ms.Length);
                    client.Close();

                    //DONE RECIEVING MESH FROM THE MASTER SERVER, NOW UPDATE IT

                    TangoDatabase.UpdateMesh(ms.ToArray());
                    Debug.Log("You updated the meshes in the database");
                }
            }

            client.Close();


            //CREATE AND DRAW THEM MESHES------------------------------------------------------
            Debug.Log("Checking for them meshes in ze database");

            //goes into the if statement if the database is not NULL
            if (TangoDatabase.GetMeshAsList() != null)
            {
                //Create a material to apply to the mesh
                Material meshMaterial = new Material(Shader.Find("Diffuse"));

                //grab the meshes in the database
                IEnumerable <Mesh> temp = new List <Mesh>(TangoDatabase.GetMeshAsList());

                foreach (var mesh in temp)
                {
                    //for each mesh in the database, create a game object to represent
                    //and display the mesh in the scene
                    GameObject obj1 = new GameObject("mesh");

                    //add a mesh filter to the object and assign it the mesh
                    MeshFilter filter = obj1.AddComponent <MeshFilter>();
                    filter.mesh = mesh;

                    //add a mesh rendererer and add a material to it
                    MeshRenderer rend1 = obj1.AddComponent <MeshRenderer>();
                    rend1.material = meshMaterial;
                }
            }
            else
            {
                UnityEngine.Debug.Log("YO... your mesh is empty...");
            }
            //END OF CREATING AND DRAWING THE MEESHES------------------------------------------
        }
        /// <summary>
        /// When a TangoRoom is delete, remove it from the list in TangoDatabase.cs
        /// </summary>
        private void OnDestroy()
        {
            TangoDatabase.TangoRoom T = TangoDatabase.GetRoomByName(this.gameObject.name);

            TangoDatabase.DeleteRoom(T);
        }
        public override void SendTangoMesh()
        {
            UpdateMesh();

            photonView.RPC("ReceiveTangoMesh", PhotonTargets.MasterClient, PhotonNetwork.player.ID, TangoDatabase.GetMeshAsBytes().Length);
        }
        private float updateTime        = 5f;                  //time tracker to send a new room list to all clients
        #endregion

        /// <summary>
        /// Called once per frame
        /// When a new mesh is recieved, display it
        /// When L is pressed, load and send a saved Room Mesh file (used for testing without HoloLens)
        /// </summary>
        public override void Update()
        {
            base.Update();

            //    //AssetBundle object instantiation for testing purposes
            //    if (Input.GetKeyDown("i"))
            //    {
            //        int id1 = PhotonNetwork.AllocateViewID();

            //        photonView.RPC("SpawnNetworkObject", PhotonTargets.AllBuffered, transform.position, transform.rotation, id1, "Cube");
            //    }
            //}

            //Check to see if a new Tango Room Mesh has been recieved and create a gameobject and render the mesh in gamespace
            if (TangoDatabase.LastUpdate != DateTime.MinValue && DateTime.Compare(_lastUpdate, TangoDatabase.LastUpdate) < 0)
            {
                for (int i = 0; i < TangoDatabase.Rooms.Count; i++)
                {
                    TangoDatabase.TangoRoom T = TangoDatabase.GetRoom(i);

                    if (TangoDatabase.ID < T.ID)
                    {
                        TangoDatabase.ID = T.ID;
                        //    Create a material to apply to the mesh
                        Material meshMaterial = new Material(Shader.Find("Diffuse"));

                        //    grab the meshes in the database
                        IEnumerable <Mesh> temp = new List <Mesh>(TangoDatabase.GetMeshAsList(T));

                        foreach (var mesh in temp)
                        {
                            //        for each mesh in the database, create a game object to represent
                            //        and display the mesh in the scene
                            GameObject obj1 = new GameObject(T.name);

                            //        add a mesh filter to the object and assign it the mesh
                            MeshFilter filter = obj1.AddComponent <MeshFilter>();
                            filter.mesh = mesh;

                            //        add a mesh rendererer and add a material to it
                            MeshRenderer rend1 = obj1.AddComponent <MeshRenderer>();
                            rend1.material = meshMaterial;

                            rend1.material.shader = Shader.Find("Custom/UnlitVertexColor");

                            obj1.tag = "Room";
                            obj1.AddComponent <TangoRoom>();

                            PhotonView parentView = PhotonView.Find(T.parentID);
                            if (parentView != null)
                            {
                                obj1.transform.SetParent(parentView.transform, false);
                            }
                        }
                    }
                }

                foreach (PhotonPlayer p in PhotonNetwork.otherPlayers)
                {
                    SendTangoRoomInfo(p.ID);
                }

                _lastUpdate = TangoDatabase.LastUpdate;
            }

            //Check if the Thread has stopped and join if so
            Thread[] TL = threads.ToArray();
            foreach (Thread T in TL)
            {
                if (T.ThreadState == ThreadState.Stopped)
                {
                    T.Join();
                    //threads.Remove(T);
                }
            }

            //decrement updateTime which controls the TangoRoomInfo sending between clients
            updateTime -= Time.deltaTime;
            if (updateTime <= 0)
            {
                SendTangoRoomInfoAll();
                updateTime = 5f;
            }
        }
Beispiel #18
0
        private static bool _ThreadFinished            = true;                                  //Check to see that a room request has finished
        #endregion

        //// Ensure not HoloLens

        public override void Update()
        {
            //    if (Database.LastUpdate != DateTime.MinValue && DateTime.Compare(_lastUpdate, Database.LastUpdate) < 0)
            //    {
            //        if (Database.GetMeshAsBytes() != null)
            //        {
            //            //Create a material to apply to the mesh
            //            Material meshMaterial = new Material(Shader.Find("Diffuse"));

            //            //grab the meshes in the database
            //            IEnumerable<Mesh> temp = new List<Mesh>(Database.GetMeshAsList());

            //            foreach (var mesh in temp)
            //            {
            //                //for each mesh in the database, create a game object to represent
            //                //and display the mesh in the scene
            //                GameObject obj1 = new GameObject("mesh");

            //                //add a mesh filter to the object and assign it the mesh
            //                MeshFilter filter = obj1.AddComponent<MeshFilter>();
            //                filter.mesh = mesh;

            //                //add a mesh rendererer and add a material to it
            //                MeshRenderer rend1 = obj1.AddComponent<MeshRenderer>();
            //                rend1.material = meshMaterial;
            //            }
            //        }
            //        _lastUpdate = Database.LastUpdate;
            //    }

            //    //Loading a mesh from a file for testing purposes.
            //    if (Input.GetKeyDown("l"))
            //    {
            //        //Database.UpdateMesh(MeshSaver.Load("RoomMesh"));
            //        var memoryStream = new MemoryStream(File.ReadAllBytes("RoomMesh"));
            //        this.DeleteLocalRoomModelInfo();
            //        Database.UpdateMesh(memoryStream.ToArray());
            //    }

            //    //Testcalls for the added functionality
            //    if (Input.GetKeyDown("s"))
            //    {
            //        this.SendMesh();
            //    }

            //    if (Input.GetKeyDown("d"))
            //    {
            //        this.DeleteRoomInfo();
            //    }

            //    if (Input.GetKeyDown("a"))
            //    {
            //        this.SendAddMesh();
            //    }

            //Check all current Rooms in the database and render any without a current gameobject and mesh renderer
            if (TangoDatabase.LastUpdate != DateTime.MinValue && DateTime.Compare(_lastUpdate, TangoDatabase.LastUpdate) < 0)
            {
                for (int i = 0; i < TangoDatabase.Rooms.Count; i++)
                {
                    TangoDatabase.TangoRoom T = TangoDatabase.GetRoom(i);

                    if (TangoDatabase.ID < T.ID)
                    {
                        TangoDatabase.ID = T.ID;
                        //    Create a material to apply to the mesh
                        Material meshMaterial = new Material(Shader.Find("Diffuse"));

                        //    grab the meshes in the database
                        IEnumerable <Mesh> temp = new List <Mesh>(TangoDatabase.GetMeshAsList(T));

                        foreach (var mesh in temp)
                        {
                            //        for each mesh in the database, create a game object to represent
                            //        and display the mesh in the scene
                            GameObject obj1 = new GameObject(T.name);

                            //        add a mesh filter to the object and assign it the mesh
                            MeshFilter filter = obj1.AddComponent <MeshFilter>();
                            filter.mesh = mesh;

                            //        add a mesh rendererer and add a material to it
                            MeshRenderer rend1 = obj1.AddComponent <MeshRenderer>();
                            rend1.material = meshMaterial;

                            rend1.material.shader = Shader.Find("Custom/UnlitVertexColor");

                            obj1.tag = "Room";
                            obj1.AddComponent <TangoRoom>();

                            Transform parentXform = PhotonView.Find(T.parentID).transform;
                            obj1.transform.SetParent(parentXform, false);
                        }
                    }
                }
            }

            //Check to see that no rooms are currently being recieved and then check if there is a room to request
            if (_ThreadFinished == true)
            {
                if (TangoRoomStack.Count > 0)
                {
                    ReceiveTangoMeshStack();
                }
            }

            //check to see if a thread has stopped and join it
            Thread[] TL = threads.ToArray();
            foreach (Thread T in TL)
            {
                if (T.ThreadState == ThreadState.Stopped)
                {
                    T.Join();
                    //threads.Remove(T);
                }
            }
        }