Exemple #1
0
    private void BuildMesh()
    {
        Debug.Log("Received mesh!");
        Debug.LogFormat("Compressed serialized mesh size: {0} KB", m_incomingCompressedMesh.LongLength / 1000);

        byte[] serializedMesh = CLZF2.Decompress(m_incomingCompressedMesh);
        Debug.LogFormat("Serialized mesh size: {0} KB", serializedMesh.LongLength / 1000);

        List <Mesh> receivedMesh = (List <Mesh>)SimpleMeshSerializer.Deserialize(serializedMesh);

        meshFilter.mesh = receivedMesh[0];
    }
Exemple #2
0
    private void SendMesh(Mesh m)
    {
        Debug.Log("Sending mesh...");

        byte[] serializedMesh = SimpleMeshSerializer.Serialize(new Mesh[] { mesh });
        Debug.LogFormat("Serialized mesh size: {0} KB", serializedMesh.LongLength / 1000);

        byte[] compressedSerializedMesh = CLZF2.Compress(serializedMesh);
        Debug.LogFormat("Compressed serialized mesh size: {0} KB", compressedSerializedMesh.LongLength / 1000);

        SendMeshHeader(compressedSerializedMesh);
        StartCoroutine(SendMeshChunks(compressedSerializedMesh));
    }
Exemple #3
0
    public void DisplayMeshes()
    {
        var meshes = SimpleMeshSerializer.Deserialize(_mesh);

        Debug.Log(meshes.Count());
        foreach (var mesh in meshes)
        {
            GameObject surface = AddSurfaceObject(mesh, 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;
        }
    }
Exemple #4
0
        public void SendSpatialMapping()
        {
            List <MeshFilter> meshes = SpatialMappingManager.Instance.GetMeshFilters();

            byte[] meshData = SimpleMeshSerializer.Serialize(meshes);

            //SpatialMappingManager.Instance.DrawVisualMeshes = true;

            LiveMessageResponseSpatialMapping msg = new LiveMessageResponseSpatialMapping();

            msg.mapData = meshData;

            Debug.Log("Spatial Mapping bytes len=" + meshData.Length);
            Debug.Log("Send Spatial Mapping!");

            syncClient.SendMessage(msg.Serialize());
        }
    public async void sendMesh(Mesh m, Vector3 location, Quaternion rotation)
    {
        if (!connected)
        {
            return;
        }


        try
        {
            //SendHeadsetLocation();
            List <Mesh> meshes = new List <Mesh>();
            meshes.Add(m);
            byte[] meshData = SimpleMeshSerializer.Serialize(meshes);
            byte[] bytes    = new byte[36];                                    // 4 bytes per float
            System.Buffer.BlockCopy(BitConverter.GetBytes(36 + meshData.Length), 0, bytes, 0, 4);
            System.Buffer.BlockCopy(BitConverter.GetBytes(1), 0, bytes, 4, 4); //type of packet
            System.Buffer.BlockCopy(BitConverter.GetBytes(location.x), 0, bytes, 8, 4);
            System.Buffer.BlockCopy(BitConverter.GetBytes(location.y), 0, bytes, 12, 4);
            System.Buffer.BlockCopy(BitConverter.GetBytes(location.z), 0, bytes, 16, 4);
            System.Buffer.BlockCopy(BitConverter.GetBytes(rotation.x), 0, bytes, 20, 4);
            System.Buffer.BlockCopy(BitConverter.GetBytes(rotation.y), 0, bytes, 24, 4);
            System.Buffer.BlockCopy(BitConverter.GetBytes(rotation.z), 0, bytes, 28, 4);
            System.Buffer.BlockCopy(BitConverter.GetBytes(rotation.w), 0, bytes, 32, 4);
            byte[] sendData = Combine(bytes, meshData);
            if (sendData.Length > 0)
            {
                enqueueOutgoing(sendData);
            }
            SendHeadsetLocation();
        }
        catch (Exception e)
        {
            Debug.Log(e.ToString());
            return;
        }
        //Debug.Log("Sent: " + sendData.Length + " bytes");
    }
Exemple #6
0
 public static void UpdateMesh(IEnumerable <Mesh> newMesh)
 {
     _mesh = SimpleMeshSerializer.Serialize(newMesh);
     _lastestMeshVersion++;
 }
Exemple #7
0
 public static IEnumerable <Mesh> GetMeshAsEnumerable()
 {
     return(SimpleMeshSerializer.Deserialize(_mesh));
 }
Exemple #8
0
    // 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);
    }
    /*
     * public void captureImageData()
     * {
     *
     *  Resolution cameraResolution = UnityEngine.XR.WSA.WebCam.PhotoCapture.SupportedResolutions.OrderByDescending((res) => res.width * res.height).First();
     *  targetTexture = new Texture2D(cameraResolution.width, cameraResolution.height);
     *
     *  // Create a PhotoCapture object
     *  UnityEngine.XR.WSA.WebCam.PhotoCapture.CreateAsync(false, delegate (UnityEngine.XR.WSA.WebCam.PhotoCapture captureObject) {
     *      photoCaptureObject = captureObject;
     *      UnityEngine.XR.WSA.WebCam.CameraParameters cameraParameters = new UnityEngine.XR.WSA.WebCam.CameraParameters();
     *      cameraParameters.hologramOpacity = 0.0f;
     *      cameraParameters.cameraResolutionWidth = cameraResolution.width;
     *      cameraParameters.cameraResolutionHeight = cameraResolution.height;
     *      cameraParameters.pixelFormat = UnityEngine.XR.WSA.WebCam.CapturePixelFormat.BGRA32;
     *
     *      // Activate the camera
     *      photoCaptureObject.StartPhotoModeAsync(cameraParameters, delegate (UnityEngine.XR.WSA.WebCam.PhotoCapture.PhotoCaptureResult result) {
     *          // Take a picture
     *          photoCaptureObject.TakePhotoAsync(OnCapturedPhotoToMemory);
     *      });
     *  });
     * }
     *
     * void OnCapturedPhotoToMemory(UnityEngine.XR.WSA.WebCam.PhotoCapture.PhotoCaptureResult result, UnityEngine.XR.WSA.WebCam.PhotoCaptureFrame photoCaptureFrame)
     * {
     *  // Copy the raw image data into the target texture
     *  photoCaptureFrame.UploadImageDataToTexture(targetTexture);
     *
     *  // Create a GameObject to which the texture can be applied
     *  GameObject quad = GameObject.CreatePrimitive(PrimitiveType.Quad);
     *  Renderer quadRenderer = quad.GetComponent<Renderer>() as Renderer;
     *  quadRenderer.material = new Material(Shader.Find("Custom/Unlit/UnlitTexture"));
     *
     *  quad.transform.parent = this.transform;
     *  quad.transform.localPosition = new Vector3(0.0f, 0.0f, 3.0f);
     *
     *  quadRenderer.material.SetTexture("_MainTex", targetTexture);
     *
     *  // Deactivate the camera
     *  photoCaptureObject.StopPhotoModeAsync(OnStoppedPhotoMode);
     * }
     *
     * void OnStoppedPhotoMode(UnityEngine.XR.WSA.WebCam.PhotoCapture.PhotoCaptureResult result)
     * {
     *  // Shutdown the photo capture resource
     *  photoCaptureObject.Dispose();
     *  photoCaptureObject = null;
     * }
     */
    public async void sendMesh(Mesh m, Vector3 location, Quaternion rotation)
    {
        if (!connected)
        {
            return;
        }
#if !UNITY_EDITOR
        try
        {
            List <Mesh> meshes = new List <Mesh>();
            meshes.Add(m);
            byte[] meshData = SimpleMeshSerializer.Serialize(meshes);
            //byte[] vectBytes = BitConverter.GetBytes(location);
            //byte[] quatBytes = BitConverter.GetBytes(rotation);

            byte[] bytes = new byte[4 + 12 + 16]; // 4 bytes per float
            System.Buffer.BlockCopy(BitConverter.GetBytes(32 + meshData.Length), 0, bytes, 0, 4);
            System.Buffer.BlockCopy(BitConverter.GetBytes(location.x), 0, bytes, 4, 4);
            System.Buffer.BlockCopy(BitConverter.GetBytes(location.y), 0, bytes, 8, 4);
            System.Buffer.BlockCopy(BitConverter.GetBytes(location.z), 0, bytes, 12, 4);
            System.Buffer.BlockCopy(BitConverter.GetBytes(rotation.x), 0, bytes, 16, 4);
            System.Buffer.BlockCopy(BitConverter.GetBytes(rotation.y), 0, bytes, 20, 4);
            System.Buffer.BlockCopy(BitConverter.GetBytes(rotation.z), 0, bytes, 24, 4);
            System.Buffer.BlockCopy(BitConverter.GetBytes(rotation.w), 0, bytes, 28, 4);


            //byte[] sendData = Compress(Combine(bytes, meshData));
            byte[] sendData = Combine(bytes, meshData);

            //byte[] sendData = Compress(Combine(bytes, SimpleMeshSerializer.Serialize(meshes)));
            //Byte[] sendData = Encoding.ASCII.GetBytes("WEEEEEE!");
            //udpClient.Send(sendData, sendData.Length);
            //safety catch for huge items

            //temp for testing
            //byte[] sendData = meshData;

            if (sendData.Length > 200000)
            {
                Debug.Log("Packet of length " + sendData.Length + " waiting to go out... But can't.. Because it is probably too huge...");
                return;
            }
            DataWriter writer = new DataWriter(outputStream);
            writer.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8;
            writer.ByteOrder       = Windows.Storage.Streams.ByteOrder.LittleEndian;
            //StreamWriter writer = new StreamWriter(outputStream);
            //writer.BaseStream.Write(sendData,0,sendData.Length);
            writer.WriteBytes(sendData);
            await writer.StoreAsync();

            await writer.FlushAsync();

            Debug.Log("Sent " + sendData.Length + " bytes.");
            writer.DetachStream();
            //writer.WriteBytes(sendData);
            //await writer.FlushAsync();
            //await writer.StoreAsync();
        }
        catch (Exception e)
        {
            Debug.Log(e.ToString());
            return;
        }
        //Debug.Log("Sent: " + sendData.Length + " bytes");
#endif
    }