Example #1
0
        /// <summary>
        /// Sends frame buffer.
        /// </summary>
        private void SendFrame()
        {
            foreach (var peer in remotePeersData)
            {
                int            peerId   = peer.Key;
                RemotePeerData peerData = peer.Value;
                if (peerData.LeftRenderTexture && (!peerData.IsStereo.Value || peerData.IsNew))
                {
                    Plugin.SendFrame(
                        peerId,
                        peerData.IsStereo.Value,
                        peerData.LeftRenderTexture,
                        peerData.RightRenderTexture,
                        peerData.LastTimestamp);

                    peerData.IsNew = false;
                }
            }
        }
Example #2
0
        /// <summary>
        /// Handles input data and updates local transformations
        /// </summary>
        /// <param name="data">input data</param>
        private void OnDataChannelMessage(int peerId, string data)
        {
            try
            {
                JSONNode node        = SimpleJSON.JSON.Parse(data);
                string   messageType = node["type"];
                string   camera      = "";

                // Retrieves remote peer data from dictionary, creates new if needed.
                RemotePeerData peerData = default(RemotePeerData);
                if (!remotePeersData.TryGetValue(peerId, out peerData))
                {
                    peerData           = new RemotePeerData();
                    peerData.startTick = Environment.TickCount;
                    remotePeersData.Add(peerId, peerData);
                }

                switch (messageType)
                {
                case "stereo-rendering":
                    // note: 1 represents true, 0 represents false
                    int isStereo;

                    // if it's not a valid bool, don't continue
                    if (!int.TryParse(node["body"].Value, out isStereo))
                    {
                        break;
                    }

                    peerData.IsStereo = isStereo == 1;

                    // Enables fps limiter in non-stereo mode.
                    if (!peerData.IsStereo.Value)
                    {
                        Application.targetFrameRate = 60;
                    }

                    peerData.EyeVector    = DefaultEyeVector;
                    peerData.LookAtVector = DefaultLookAtVector;
                    peerData.UpVector     = DefaultUpVector;
                    break;

                case "camera-transform-lookat":
                    camera = node["body"];
                    if (camera != null && camera.Length > 0)
                    {
                        string[] sp = camera.Split(new char[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries);

                        Vector3 loc = new Vector3();
                        loc.x = float.Parse(sp[0]);
                        loc.y = float.Parse(sp[1]);
                        loc.z = float.Parse(sp[2]);
                        peerData.EyeVector = loc;

                        Vector3 look = new Vector3();
                        look.x = float.Parse(sp[3]);
                        look.y = float.Parse(sp[4]);
                        look.z = float.Parse(sp[5]);
                        peerData.LookAtVector = look;

                        Vector3 u = new Vector3();
                        u.x = float.Parse(sp[6]);
                        u.y = float.Parse(sp[7]);
                        u.z = float.Parse(sp[8]);
                        peerData.UpVector = u;

                        peerData.IsNew = true;
                    }

                    break;

                case "camera-transform-stereo":
                    camera = node["body"];
                    if (!peerData.IsNew && camera != null && camera.Length > 0)
                    {
                        string[]  coords = camera.Split(',');
                        int       index  = 0;
                        Matrix4x4 leftProjectionMatrix  = Matrix4x4.identity;
                        Matrix4x4 leftViewMatrix        = Matrix4x4.identity;
                        Matrix4x4 rightProjectionMatrix = Matrix4x4.identity;
                        Matrix4x4 rightViewMatrix       = Matrix4x4.identity;
                        for (int i = 0; i < 4; i++)
                        {
                            for (int j = 0; j < 4; j++)
                            {
                                rightViewMatrix[i, j]       = float.Parse(coords[48 + index]);
                                rightProjectionMatrix[i, j] = float.Parse(coords[32 + index]);
                                leftViewMatrix[i, j]        = float.Parse(coords[16 + index]);
                                leftProjectionMatrix[i, j]  = float.Parse(coords[index++]);
                            }
                        }

                        peerData.stereoLeftProjectionMatrix  = leftProjectionMatrix;
                        peerData.stereoLeftViewMatrix        = leftViewMatrix;
                        peerData.stereoRightProjectionMatrix = rightProjectionMatrix;
                        peerData.stereoRightViewMatrix       = rightViewMatrix;
                        peerData.IsNew = true;
                    }

                    break;

                case "camera-transform-stereo-prediction":
                    camera = node["body"];
                    if (!peerData.IsNew && camera != null && camera.Length > 0)
                    {
                        string[] coords = camera.Split(',');

                        // Parse the prediction timestamp from the message body.
                        long timestamp = long.Parse(coords[64]);

                        if (timestamp != 0 && timestamp != peerData.LastTimestamp)
                        {
                            peerData.LastTimestamp = timestamp;

                            int       index = 0;
                            Matrix4x4 leftProjectionMatrix  = Matrix4x4.identity;
                            Matrix4x4 leftViewMatrix        = Matrix4x4.identity;
                            Matrix4x4 rightProjectionMatrix = Matrix4x4.identity;
                            Matrix4x4 rightViewMatrix       = Matrix4x4.identity;
                            for (int i = 0; i < 4; i++)
                            {
                                for (int j = 0; j < 4; j++)
                                {
                                    rightViewMatrix[i, j]       = float.Parse(coords[48 + index]);
                                    rightProjectionMatrix[i, j] = float.Parse(coords[32 + index]);
                                    leftViewMatrix[i, j]        = float.Parse(coords[16 + index]);
                                    leftProjectionMatrix[i, j]  = float.Parse(coords[index++]);
                                }
                            }

                            peerData.stereoLeftProjectionMatrix  = leftProjectionMatrix;
                            peerData.stereoLeftViewMatrix        = leftViewMatrix;
                            peerData.stereoRightProjectionMatrix = rightProjectionMatrix;
                            peerData.stereoRightViewMatrix       = rightViewMatrix;
                            peerData.IsNew = true;
                        }
                    }

                    break;
                }
            }
            catch (Exception ex)
            {
                Debug.LogWarning("DataChannelMessage threw " + ex.Message + "\r\n" + ex.StackTrace);
            }
        }