// Creates a unique ID, adds that to the objects Dictionary in Global, and sends the object over the network // This method is called automatically from the Update loop for any object added to Global.newSpawns. You do // NOT need to call this method yourself. private void SendObject(GameObject obj) { // Generate and save a unique Object ID int objId = Global.AddObject(obj); // Create a buffer List <byte> messageBuffer = new List <byte>(); // Add our flag messageBuffer.AddRange(BitConverter.GetBytes((int)Global.NetFlag.OBJECT_CREATE_FLAG)); // Add our Position messageBuffer.AddRange(BitConverter.GetBytes(obj.transform.position.x)); messageBuffer.AddRange(BitConverter.GetBytes(obj.transform.position.y)); messageBuffer.AddRange(BitConverter.GetBytes(obj.transform.position.z)); // Add our Rotation messageBuffer.AddRange(BitConverter.GetBytes(obj.transform.rotation.eulerAngles.x)); messageBuffer.AddRange(BitConverter.GetBytes(obj.transform.rotation.eulerAngles.y)); messageBuffer.AddRange(BitConverter.GetBytes(obj.transform.rotation.eulerAngles.z)); // Get our ObjectType Global.ObjType objT = Global.ToEnum <Global.ObjType>(obj.name.Substring(0, obj.name.Length - 7)); // Name - "(Clone)" // Add our Object Type messageBuffer.AddRange(BitConverter.GetBytes((int)objT)); // Add our Object ID messageBuffer.AddRange(BitConverter.GetBytes(objId)); // Add our size int size = messageBuffer.Count + 4; messageBuffer.InsertRange(0, BitConverter.GetBytes(size)); //the byte[] to send byte[] theDataPackage = messageBuffer.ToArray(); // Set object name to be ID obj.name = objId.ToString(); SendPacket(theDataPackage); }
// This is the meat of DataRecieving. Takes the full package and interprets // the package by taking out the first 4 bytes which denotes the package flag. // Types of package flags can be found in the Global.NetFlag enum. The method // then correctly acts on the flag (such as creating a new object in the scene). Global.NetFlag interpretIncomingPackage(byte[] thePackage, int sizeOfPackage) { //a byte[] to hold the actual data byte[] theActualData = new byte[sizeOfPackage - sizeof(int)]; //a byte[] to hold the flag data byte[] flagData = new byte[sizeof(int)]; //GET THE FLAG OF THE PACKET Array.Copy(thePackage, flagData, sizeof(int)); //convert the flag data into the actual int flag int flagInt = BitConverter.ToInt32(flagData, 0); //convert the flagInt into the actual flag Global.NetFlag flag = (Global.NetFlag)flagInt; //GET THE ACTUAL DATA OF THE PACKET //copy only the actual data into the actual data byte array for (int i = 0; i < theActualData.Length; i++) { theActualData[i] = thePackage[i + 4]; //start at the place of the actual data and do not grab the flag data that was in the front of the package } //Do work based on the flag found if (flag == Global.NetFlag.MESH_FLAG) { //The packet was sending a mesh // Pass the data to the mesh serializer. List <Mesh> meshes = new List <Mesh>(SimpleMeshSerializer.Deserialize(theActualData)); globalMeshes.AddRange(meshes); // For each mesh, create a GameObject to render it. for (int index = 0; index < meshes.Count; index++) { GameObject surface = AddSurfaceObject(meshes[index], string.Format("Beamed-{0}", surfaceObjects.Count), transform); surface.transform.parent = SpatialMappingManager.Instance.transform; surface.GetComponent <MeshRenderer>().enabled = true; surface.GetComponent <MeshRenderer>().shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.On; } } else if (flag == Global.NetFlag.OBJECT_CREATE_FLAG) { //Receiving an object creation flag // Get the Position info float posX = BitConverter.ToSingle(theActualData, 0); float posY = BitConverter.ToSingle(theActualData, 4); float posZ = BitConverter.ToSingle(theActualData, 8); // Get the Rotation info float rotX = BitConverter.ToSingle(theActualData, 12); float rotY = BitConverter.ToSingle(theActualData, 16); float rotZ = BitConverter.ToSingle(theActualData, 20); // Create a new Position and Rotation for the object based on the passed info Quaternion newRotation = Quaternion.Euler(rotX, rotY, rotZ); Vector3 newPosition = new Vector3(posX, posY, posZ); // Determine the prefab of the object (See: Global.ObjType) Global.ObjType objType = (Global.ObjType)BitConverter.ToInt32(theActualData, 24); // Get the Object ID info int objId = BitConverter.ToInt32(theActualData, 28); // Create the correct prefab GameObject newObj = GameObject.Instantiate(Resources.Load("Prefabs/" + objType.ToString())) as GameObject; // Set the position and rotation newObj.transform.position = newPosition; newObj.transform.rotation = newRotation; // Set the name equal to the object ID newObj.name = objId.ToString(); // Add this to the hashmap Global.AddObject(objId, newObj); Debug.Log(objType.ToString() + " created"); } else if (flag == Global.NetFlag.STRING_FLAG) { //The packet was sending a string message //decode the message string something = Encoding.ASCII.GetString(theActualData); //print out the message UnityEngine.Debug.Log("Message: " + something); } else if (flag == Global.NetFlag.OBJECT_MOVE_FLAG) { //Receiving an object move flag float posX = BitConverter.ToSingle(theActualData, 0); float posY = BitConverter.ToSingle(theActualData, 4); float posZ = BitConverter.ToSingle(theActualData, 8); float rotX = BitConverter.ToSingle(theActualData, 12); float rotY = BitConverter.ToSingle(theActualData, 16); float rotZ = BitConverter.ToSingle(theActualData, 20); Quaternion newRotation = Quaternion.Euler(rotX, rotY, rotZ); Vector3 newPosition = new Vector3(posX, posY, posZ); int objId = BitConverter.ToInt32(theActualData, 24); GameObject newObj = Global.GetObject(objId); newObj.transform.position = newPosition; newObj.transform.rotation = newRotation; //Debug.Log(newObj.name + " Moved"); } else if (flag == Global.NetFlag.CAMERA_FLAG) { // The packet is sending the camera position // Set our camera position equal to the sent position float posX = BitConverter.ToSingle(theActualData, 0); float posY = BitConverter.ToSingle(theActualData, 4); float posZ = BitConverter.ToSingle(theActualData, 8); // Get the Rotation info float rotX = BitConverter.ToSingle(theActualData, 12); float rotY = BitConverter.ToSingle(theActualData, 16); float rotZ = BitConverter.ToSingle(theActualData, 20); // Create a new Position and Rotation for the object based on the passed info Quaternion newRotation = Quaternion.Euler(rotX, rotY, rotZ); Vector3 newPosition = new Vector3(posX, posY, posZ); Camera projCamera = GameObject.Find("KinectReference").GetComponent <Camera>(); Camera topCamera = GameObject.Find("Top-Down").GetComponent <Camera>(); projCamera.transform.position = newPosition; projCamera.transform.rotation = newRotation; Quaternion topRotation = Quaternion.Euler(90, rotY, 0); topCamera.transform.rotation = topRotation; } else if (flag == Global.NetFlag.HOLO_HEAD_CREATION_FLAG) { float posX = BitConverter.ToSingle(theActualData, 0); float posY = BitConverter.ToSingle(theActualData, 4); float posZ = BitConverter.ToSingle(theActualData, 8); float rotX = BitConverter.ToSingle(theActualData, 12); float rotY = BitConverter.ToSingle(theActualData, 16); float rotZ = BitConverter.ToSingle(theActualData, 20); Quaternion newRotation = Quaternion.Euler(rotX, rotY, rotZ); Vector3 newPosition = new Vector3(posX, posY, posZ); int objId = BitConverter.ToInt32(theActualData, 24); GameObject newObj = GameObject.Instantiate(Resources.Load("Prefabs/HoloHead")) as GameObject; newObj.transform.position = newPosition; newObj.transform.rotation = newRotation; newObj.name = objId.ToString(); Global.AddObject(objId, newObj); UnityEngine.Debug.Log("HoloHead Created!"); } else if (flag == Global.NetFlag.DELETE_FLAG) { int objId = BitConverter.ToInt32(theActualData, 0); Global.DeleteObject(objId); } else if (flag == Global.NetFlag.VIVE_CREATION_FLAG) { int hmdId = BitConverter.ToInt32(theActualData, 0); int leftCId = BitConverter.ToInt32(theActualData, 4); int rightCId = BitConverter.ToInt32(theActualData, 8); GameObject hmd = GameObject.Instantiate(Resources.Load("Prefabs/HMD")) as GameObject; GameObject leftC = GameObject.Instantiate(Resources.Load("Prefabs/Controller")) as GameObject; GameObject rightC = GameObject.Instantiate(Resources.Load("Prefabs/Controller")) as GameObject; Global.AddObject(hmdId, hmd); Global.AddObject(leftCId, leftC); Global.AddObject(rightCId, rightC); UnityEngine.Debug.Log("Vive Avatar Created!"); // Store information on this Vive machine for future lookup (to differentiate // between multiple Vive machines that coulc be connected in case one of them // disconnects ViveMachine currentlyConnectingViveMachine; currentlyConnectingViveMachine.connectionID = connectionId; currentlyConnectingViveMachine.headsetID = hmdId; currentlyConnectingViveMachine.leftControllerID = leftCId; currentlyConnectingViveMachine.rightControllerID = rightCId; ViveMachines.Add(currentlyConnectingViveMachine); } else if (flag == Global.NetFlag.VIVE_MOVE_FLAG) { // HMD float posX = BitConverter.ToSingle(theActualData, 0); float posY = BitConverter.ToSingle(theActualData, 4); float posZ = BitConverter.ToSingle(theActualData, 8); float rotX = BitConverter.ToSingle(theActualData, 12); float rotY = BitConverter.ToSingle(theActualData, 16); float rotZ = BitConverter.ToSingle(theActualData, 20); int hmdId = BitConverter.ToInt32(theActualData, 24); GameObject hmd = Global.GetObject(hmdId); hmd.transform.position = new Vector3(posX, posY, posZ); hmd.transform.rotation = Quaternion.Euler(rotX, rotY, rotZ); // Left Controller posX = BitConverter.ToSingle(theActualData, 28); posY = BitConverter.ToSingle(theActualData, 32); posZ = BitConverter.ToSingle(theActualData, 36); rotX = BitConverter.ToSingle(theActualData, 40); rotY = BitConverter.ToSingle(theActualData, 44); rotZ = BitConverter.ToSingle(theActualData, 48); int leftCId = BitConverter.ToInt32(theActualData, 52); GameObject leftC = Global.GetObject(leftCId); leftC.transform.position = new Vector3(posX, posY, posZ); leftC.transform.rotation = Quaternion.Euler(rotX, rotY, rotZ); // Right Controller posX = BitConverter.ToSingle(theActualData, 56); posY = BitConverter.ToSingle(theActualData, 60); posZ = BitConverter.ToSingle(theActualData, 64); rotX = BitConverter.ToSingle(theActualData, 68); rotY = BitConverter.ToSingle(theActualData, 72); rotZ = BitConverter.ToSingle(theActualData, 76); int rightCId = BitConverter.ToInt32(theActualData, 80); GameObject rightC = Global.GetObject(rightCId); rightC.transform.position = new Vector3(posX, posY, posZ); rightC.transform.rotation = Quaternion.Euler(rotX, rotY, rotZ); } else { //PACKAGE TYPE DID NOT HAVE A VALID FLAG IDENTIFIER IN THE FRONT OF PACKAGE UnityEngine.Debug.LogError("UNKNOWN PACKAGE RECIEVED"); } return(flag); }