void OnVREvent(VREvent e)
        {
            // only respond to tracker move events
            if ((e.ContainsStringField("EventType")) &&
                (e.GetStringData("EventType") == "TrackerMove"))
            {
                string trackerName = e.Name.Remove(e.Name.IndexOf("_Move"), 5);
                if (!trackersToIgnore.Contains(trackerName))
                {
                    float[]    data = e.GetFloatArrayData("Transform");
                    Matrix4x4  m    = VRConvert.ToMatrix4x4(data);
                    Vector3    pos  = m.GetTranslation();
                    Quaternion rot  = m.GetRotation();

                    if (!cursors.ContainsKey(trackerName))
                    {
                        GameObject newCursorObj = Instantiate(cursorPrefab);
                        TextMesh   label        = newCursorObj.GetComponentInChildren <TextMesh>();
                        if (label != null)
                        {
                            label.text = trackerName;
                        }
                        cursors[trackerName] = newCursorObj;
                    }
                    GameObject cursorObj = cursors[trackerName];
                    cursorObj.transform.position = pos;
                    cursorObj.transform.rotation = rot;
                }
            }
        }
        // Update is called once per frame
        void Update()
        {
            if (threadingMode == ThreadingMode.SINGLE_THREADED)
            {
                // update the trackers only once per frame
                if (lastTrackerUpdateFrame != Time.frameCount)
                {
                    UpdateTrackers();
                    lastTrackerUpdateFrame = Time.frameCount;
                }

                trackerData = (TrackerData)Marshal.PtrToStructure(trackerDataPointer, typeof(TrackerData));
            }
            else
            {
                // update the trackers only once per frame
                if (lastTrackerUpdateFrame != Time.frameCount)
                {
                    UpdateTrackersMultiThreaded();
                    lastTrackerUpdateFrame = Time.frameCount;
                }

                LockTrackerData(trackerDataPointer);
                trackerData = (TrackerData)Marshal.PtrToStructure(trackerDataPointer, typeof(TrackerData));
                UnlockTrackerData(trackerDataPointer);
            }

            Vector3 transformPosition = transform.position;
            Quaternion transformRotation = transform.rotation;

            this.updateTransformFromTrackerData(ref transformPosition, ref transformRotation);

            // if a custom origin has been specified
            // then update the transform coordinate space
            if (origin != null)
            {
                if (trackingMode != TrackingMode.POSITION_ONLY)
                    transformRotation = origin.rotation * transformRotation;
                else
                    transformRotation = origin.rotation;

                if (trackingMode != TrackingMode.ORIENTATION_ONLY)
                    transformPosition = origin.position + origin.rotation * transformPosition;
                else
                    transformPosition = origin.position;
            }

            Matrix4x4 m3 = Matrix4x4.TRS(transformPosition, transformRotation, Vector3.one);
            float[] d3 = VRConvert.ToFloatArray(m3);
            VREvent e = new VREvent(eventName);
            e.AddData("EventType", "TrackerMove");
            e.AddData("Transform", d3);
            pendingEvents.Add(e);

            if (applyUpdatesToGameObject)
            {
                transform.position = transformPosition;
                transform.rotation = transformRotation;
            }
        }
        private void AddHeadTrackerEvent(ref List <VREvent> eventList)
        {
            if (Input.GetKey("up"))
            {
                headTrackerPos += 0.1f * Camera.main.transform.forward;
            }
            else if (Input.GetKey("down"))
            {
                headTrackerPos -= 0.1f * Camera.main.transform.forward;
            }
            else if (Input.GetKey("left"))
            {
                headTrackerRot *= Quaternion.AngleAxis(-1.0f, new Vector3(0f, 1f, 0f));
            }
            else if (Input.GetKey("right"))
            {
                headTrackerRot *= Quaternion.AngleAxis(1.0f, new Vector3(0f, 1f, 0f));
            }

            Matrix4x4 m3 = Matrix4x4.TRS(headTrackerPos, headTrackerRot, Vector3.one);

            float[] d3 = VRConvert.ToFloatArray(m3);
            VREvent e  = new VREvent(fakeHeadTrackerEvent);

            e.AddData("EventType", "TrackerMove");
            e.AddData("Transform", d3);
            eventList.Add(e);
        }
Exemple #4
0
    // Called whenever an ImageUpdate event is received via MinVR
    void OnImageUpdate(MinVR.VREvent e)
    {
        // access the image data buffer from the event
        float[] buffer = e.GetFloatArrayData("Buffer");

        // copy data into an array of colors
        int w = 100;
        int h = 100;

        Color[] colors = new Color[w * h];
        int     bindex = 0;
        int     cindex = 0;

        for (int y = 0; y < h; y++)
        {
            for (int x = 0; x < w; x++)
            {
                colors[cindex] = new Color(buffer[bindex + 0], buffer[bindex + 1], buffer[bindex + 2], 1.0f);
                cindex++;
                bindex += 3;
            }
        }

        tex.SetPixels(colors);
        tex.Apply();

        // connect texture to material of GameObject this script is attached to
        GetComponent <Renderer>().material.mainTexture = tex;
    }
Exemple #5
0
    void OnVREvent(VREvent e)
    {
        if (e.Name == headTrackingEvent) {
            Matrix4x4 m = VRConvert.ToMatrix4x4(e.DataIndex.GetValueAsDoubleArray(matrix4x4DataField));
            transform.position = m.GetTranslation();
            transform.rotation = m.GetRotation();
            //Debug.Log("Head Position = " + m.GetTranslation().ToString());
            //Debug.Log("Head Rotation = " + m.GetRotation().ToString());

            Vector3 offsetInLocalCoords = new Vector3(0.0f, 0.0f, Camera.main.nearClipPlane + 0.001f);
            Vector3 offsetInWorldCoords = transform.TransformVector(offsetInLocalCoords);
            transform.position += offsetInWorldCoords;
        }
    }
Exemple #6
0
    void OnButtonDown()
    {
        buttonPressed = true;
        if (selected == 0)
        {
            titleBoxObj.GetComponent <Renderer>().material.color = pressColor;
        }
        else if (selected > 0)
        {
            labelBoxes[selected - 1].GetComponent <Renderer>().material.color = pressColor;

            string        eName = eventsToGenerate[selected - 1];
            MinVR.VREvent e     = new MinVR.VREvent(eName);
            e.AddData("EventType", "ButtonDown");
            MinVR.VRMain.Instance.QueueEvent(e);
        }
    }
Exemple #7
0
        private void AddHeadTrackerEvent(ref List <VREvent> eventList)
        {
            // if (Input.GetKey(KeyCode.LeftShift))
            // {
            //     if (Input.GetKey(KeyCode.DownArrow))
            //     {
            //         headTrackerRot *= Quaternion.AngleAxis(-1.0f, new Vector3(1f, 0f, 0f));
            //     }
            //     else if (Input.GetKey(KeyCode.UpArrow))
            //     {
            //         headTrackerRot *= Quaternion.AngleAxis(1.0f, new Vector3(1f, 0f, 0f));
            //     }
            // }
            // else
            if (Input.GetKey(KeyCode.UpArrow))
            {
                headTrackerPos += 0.1f * Camera.main.transform.forward;
            }
            else if (Input.GetKey(KeyCode.DownArrow))
            {
                headTrackerPos -= 0.1f * Camera.main.transform.forward;
            }
            else if (Input.GetKey(KeyCode.LeftArrow))
            {
                headTrackerRot *= Quaternion.AngleAxis(-1.0f, new Vector3(0f, 1f, 0f));
            }
            else if (Input.GetKey(KeyCode.RightArrow))
            {
                headTrackerRot *= Quaternion.AngleAxis(1.0f, new Vector3(0f, 1f, 0f));
            }

            headTrackerRot.Normalize();

            Matrix4x4 m3 = Matrix4x4.TRS(headTrackerPos, headTrackerRot, Vector3.one);

            float[] d3 = VRConvert.ToFloatArray(m3);
            VREvent e  = new VREvent(fakeHeadTrackerEvent);

            e.AddData("EventType", "TrackerMove");
            e.AddData("Transform", d3);
            eventList.Add(e);
        }
Exemple #8
0
        void WaitForAndReceiveInputEvents(ref List <VREvent> inputEvents)
        {
            // 1. receive 1-byte message header
            WaitForAndReceiveMessageHeader(INPUT_EVENTS_MSG);

            // 2. receive int that tells us the size of the data portion of the message in bytes
            Int32 dataSize = ReadInt32();

            // 3. receive dataSize bytes, then decode these as InputEvents
            byte[] buf2   = new byte[dataSize + 1];
            int    status = ReceiveAll(ref buf2, dataSize);

            if (status == -1)
            {
                Console.WriteLine("WaitForAndReceiveInputEvents error reading data");
                return;
            }
            buf2[dataSize] = 0;

            // buf2 is the XML string that contains all the events.
            string xmlEventList = System.Text.Encoding.UTF8.GetString(buf2);
            //Debug.Log(xmlEventList);

            Dictionary <string, string> props = new Dictionary <string, string>();
            string xmlEvents = string.Empty;
            string leftover  = string.Empty;
            bool   success   = XMLUtils.GetXMLField(xmlEventList, "VREventList", ref props, ref xmlEvents, ref leftover);

            if (!success)
            {
                Debug.Log("Error decoding VREventList");
                return;
            }
            int nEvents = Convert.ToInt32(props["num"]);

            for (int i = 0; i < nEvents; i++)
            {
                // Create the next VREvent from the XML description
                VREvent e = new VREvent(ref xmlEvents);
                inputEvents.Add(e);
            }
        }
Exemple #9
0
        private void ProcessCallbacks(VREvent e)
        {
            // These "unfiltered" callbacks are called for every event
            for (int ucb = 0; ucb < genericCallbacksUnfiltered.Count; ucb++)
            {
                genericCallbacksUnfiltered[ucb].Invoke(e);
            }


            // These callbacks are passed the enitre event data structure, which can really contain anything.
            if (genericCallbacks.ContainsKey(e.Name))
            {
                for (int cb = 0; cb < genericCallbacks[e.Name].Count; cb++)
                {
                    genericCallbacks[e.Name][cb].Invoke(e);
                }
            }


            // For these callbacks, we know the type of data, so we pull it out of the dataindex here to
            // make it a bit easier for the user.

            if ((analogCallbacks.ContainsKey(e.Name)) &&
                (e.ContainsStringField("EventType")) &&
                (e.GetStringData("EventType") == "AnalogUpdate"))
            {
                for (int cb = 0; cb < analogCallbacks[e.Name].Count; cb++)
                {
                    analogCallbacks[e.Name][cb].Invoke(e.GetFloatData("AnalogValue"));
                }
            }

            if ((buttonDownCallbacks.ContainsKey(e.Name)) &&
                (e.ContainsStringField("EventType")) &&
                ((e.GetStringData("EventType") == "ButtonDown") || (e.GetStringData("EventType") == "ButtonTouch")))
            {
                for (int cb = 0; cb < buttonDownCallbacks[e.Name].Count; cb++)
                {
                    buttonDownCallbacks[e.Name][cb].Invoke();
                }
            }

            if ((buttonUpCallbacks.ContainsKey(e.Name)) &&
                (e.ContainsStringField("EventType")) &&
                ((e.GetStringData("EventType") == "ButtonUp") || (e.GetStringData("EventType") == "ButtonUntouch")))
            {
                for (int cb = 0; cb < buttonUpCallbacks[e.Name].Count; cb++)
                {
                    buttonUpCallbacks[e.Name][cb].Invoke();
                }
            }

            if ((cursorCallbacks.ContainsKey(e.Name)) &&
                (e.ContainsStringField("EventType")) &&
                (e.GetStringData("EventType") == "CursorMove"))
            {
                float[] pos  = e.GetFloatArrayData("Position");
                float[] npos = e.GetFloatArrayData("NormalizedPosition");
                for (int cb = 0; cb < cursorCallbacks[e.Name].Count; cb++)
                {
                    cursorCallbacks[e.Name][cb].Invoke(new Vector3(pos[0], pos[1], pos[2]), new Vector3(npos[0], npos[1], npos[2]));
                }
            }

            if ((trackerCallbacks.ContainsKey(e.Name)) &&
                (e.ContainsStringField("EventType")) &&
                (e.GetStringData("EventType") == "TrackerMove"))
            {
                float[]    data = e.GetFloatArrayData("Transform");
                Matrix4x4  m    = VRConvert.ToMatrix4x4(data);
                Vector3    pos  = m.GetTranslation();
                Quaternion rot  = m.GetRotation();
                for (int cb = 0; cb < trackerCallbacks[e.Name].Count; cb++)
                {
                    trackerCallbacks[e.Name][cb].Invoke(pos, rot);
                }
            }
        }
Exemple #10
0
        public void AddUnityInputEvents(ref List <VREvent> eventList)
        {
            // Convery Unity Mouse Button events to VREvent ButtonUp/Down events
            if (vrDevice.unityMouseBtnsToVREvents)
            {
                if (Input.GetMouseButtonDown(0))
                {
                    VREvent e = new VREvent("MouseBtnLeft_Down");
                    e.AddData("EventType", "ButtonDown");
                    eventList.Add(e);
                }
                if (Input.GetMouseButtonDown(2))
                {
                    VREvent e = new VREvent("MouseBtnMiddle_Down");
                    e.AddData("EventType", "ButtonDown");
                    eventList.Add(e);
                }
                if (Input.GetMouseButtonDown(1))
                {
                    VREvent e = new VREvent("MouseBtnRight_Down");
                    e.AddData("EventType", "ButtonDown");
                    eventList.Add(e);
                }
                if (Input.GetMouseButtonUp(0))
                {
                    VREvent e = new VREvent("MouseBtnLeft_Up");
                    e.AddData("EventType", "ButtonUp");
                    eventList.Add(e);
                }
                if (Input.GetMouseButtonUp(2))
                {
                    VREvent e = new VREvent("MouseBtnMiddle_Up");
                    e.AddData("EventType", "ButtonUp");
                    eventList.Add(e);
                }
                if (Input.GetMouseButtonUp(1))
                {
                    VREvent e = new VREvent("MouseBtnRight_Up");
                    e.AddData("EventType", "ButtonUp");
                    eventList.Add(e);
                }
            }


            // Convert Unity mouse move events to VREvent Cursor events
            if (vrDevice.unityMouseMovesToVREvents)
            {
                Vector3 pos = Input.mousePosition;
                if (pos != lastMousePos)
                {
                    Vector3 npos = new Vector3(pos[0] / Screen.width, pos[1] / Screen.height, 0.0f);
                    VREvent e    = new VREvent("Mouse_Move");
                    e.AddData("EventType", "CursorMove");
                    e.AddData("Position", VRConvert.ToFloatArray(pos));
                    e.AddData("NormalizedPosition", VRConvert.ToFloatArray(npos));
                    eventList.Add(e);
                    lastMousePos = pos;
                }
            }


            // Convert Unity key up/down events to VREvent ButtonUp/Down events
            for (int i = 0; i < vrDevice.unityKeysToVREvents.Count; i++)
            {
                if (Input.GetKeyDown(vrDevice.unityKeysToVREvents[i]))
                {
                    VREvent e = new VREvent("Kbd" + vrDevice.unityKeysToVREvents[i] + "_Down");
                    e.AddData("EventType", "ButtonDown");
                    eventList.Add(e);
                }
                if (Input.GetKeyDown(vrDevice.unityKeysToVREvents[i]))
                {
                    VREvent e = new VREvent("Kbd" + vrDevice.unityKeysToVREvents[i] + "_Up");
                    e.AddData("EventType", "ButtonUp");
                    eventList.Add(e);
                }
            }

            // Convert Unity Axis values to VREvent AnalogUpdate events
            for (int i = 0; i < vrDevice.unityAxesToVREvents.Count; i++)
            {
                float current = Input.GetAxis(vrDevice.unityAxesToVREvents[i]);
                bool  update  = false;
                if (axesStates.ContainsKey(vrDevice.unityAxesToVREvents[i]))
                {
                    float last = axesStates[vrDevice.unityAxesToVREvents[i]];
                    if (current != last)
                    {
                        axesStates[vrDevice.unityAxesToVREvents[i]] = current;
                        update = true;
                    }
                }
                else
                {
                    axesStates.Add(vrDevice.unityAxesToVREvents[i], current);
                    update = true;
                }
                if (update)
                {
                    VREvent e = new VREvent(vrDevice.unityAxesToVREvents[i] + "_Update");
                    e.AddData("EventType", "AnalogUpdate");
                    e.AddData("AnalogValue", current);
                    eventList.Add(e);
                }
            }


            // TODO: Convert other Unity inputs as needed (touch, accelerometer, etc.)
        }
Exemple #11
0
 // As an alternative to making your class a virtual input device that gets polled each frame for
 // new events by implementing the "EventGenerator" interface, if you're class only occasionally
 // generates a new event, you might prefer to just queue the event directly with VRMain by calling
 // this function.  The event will be processed the following frame.
 public void QueueEvent(VREvent e)
 {
     _queuedEvents.Add(e);
 }
Exemple #12
0
        // Update is called once per frame
        void Update()
        {
            // update the trackers only once per frame
            if (lastButtonUpdateFrame != Time.frameCount)
            {
                UpdateButtons();
                lastButtonUpdateFrame = Time.frameCount;
            }
            buttonData = (ButtonData)Marshal.PtrToStructure(buttonDataPointer, typeof(ButtonData));

            if (buttonData.state != lastButtonState)
            {
                lastButtonState = buttonData.state;
                VREvent e = new VREvent(eventName);
                if (buttonData.state == 0)
                {
                    e.AddData("EventType", "ButtonUp");
                }
                else if (buttonData.state == 1)
                {
                    e.AddData("EventType", "ButtonDown");
                }
                e.AddData("ButtonState", buttonData.state);
                pendingEvents.Add(e);
            }

            if (applyUpdatesToGameObject)
            {
                if (buttonData.state > 0)
                {
                    float speedThisFrame = speed * Time.deltaTime;

                    if (movementType == MovementType.TRANSLATE)
                    {
                        Vector3 position = transform.localPosition;
                        if (axis == Axis.X)
                        {
                            position.x += speedThisFrame;
                        }
                        else if (axis == Axis.Y)
                        {
                            position.y += speedThisFrame;
                        }
                        else if (axis == Axis.Z)
                        {
                            position.z += speedThisFrame;
                        }
                        else
                        {
                            position.x += speedThisFrame;
                            position.y += speedThisFrame;
                            position.z += speedThisFrame;
                        }

                        transform.localPosition = position;
                    }
                    else if (movementType == MovementType.ROTATE)
                    {
                        Quaternion newRotation = Quaternion.identity;
                        Vector3    newAngles   = newRotation.eulerAngles;

                        if (axis == Axis.X)
                        {
                            newAngles.x += speedThisFrame;
                        }
                        else if (axis == Axis.Y)
                        {
                            newAngles.y += speedThisFrame;
                        }
                        else if (axis == Axis.Z)
                        {
                            newAngles.z += speedThisFrame;
                        }
                        else
                        {
                            newAngles.x += speedThisFrame;
                            newAngles.y += speedThisFrame;
                            newAngles.z += speedThisFrame;
                        }

                        if (newAngles.x > 360)
                        {
                            newAngles.x %= 360;
                        }

                        if (newAngles.y > 360)
                        {
                            newAngles.y %= 360;
                        }

                        if (newAngles.z > 360)
                        {
                            newAngles.z %= 360;
                        }

                        newRotation.eulerAngles = newAngles;


                        Quaternion rotation = transform.localRotation;
                        rotation = rotation * newRotation;
                        transform.localRotation = rotation;
                    }
                    else
                    {
                        Vector3 scale = transform.localScale;
                        if (axis == Axis.X)
                        {
                            scale.x += speedThisFrame;
                        }
                        else if (axis == Axis.Y)
                        {
                            scale.y += speedThisFrame;
                        }
                        else if (axis == Axis.Z)
                        {
                            scale.z += speedThisFrame;
                        }
                        else
                        {
                            scale.x += speedThisFrame;
                            scale.y += speedThisFrame;
                            scale.z += speedThisFrame;
                        }

                        transform.localScale = scale;
                    }
                }
            }
        }
Exemple #13
0
    // This function gets called every time a new VREvent is generated.  Typically, VREvents will come
    // from the MinVR server, which polls trackers, buttons, and other input devices for input.  When
    // debugging on your laptop, you can also generate 'fake' VREvents using the VRMain script.
    void OnVREvent(VREvent e)
    {
        if (e.Name == "Brush_Move") {	// for faster user 2
            Matrix4x4 m = VRConvert.ToMatrix4x4 (e.DataIndex.GetValueAsDoubleArray ("Transform"));
            brushPos = m.GetTranslation ();
            brushRot = m.GetRotation ();
            brushCursor.transform.position = brushPos;
            brushCursor.transform.rotation = brushRot;
        } else if (e.Name == "Hand_Move") {
            Matrix4x4 m = VRConvert.ToMatrix4x4 (e.DataIndex.GetValueAsDoubleArray ("Transform"));
            handPos = m.GetTranslation ();
            handRot = m.GetRotation ();
            handCursor.transform.position = handPos;
            handCursor.transform.rotation = handRot;
        } else if (e.Name == "user_header_1" && user_id) { // for cave painting system user 1
            Matrix4x4 m = VRConvert.ToMatrix4x4 (e.DataIndex.GetValueAsDoubleArray ("Transform"));
            headPos = m.GetTranslation ();
            headRot = m.GetRotation ();
            headObj.transform.position = headPos;
            headObj.transform.rotation = headRot;
        } else if (e.Name == "user_header_2" && !user_id) { // for cave painting system user 2
            Matrix4x4 m = VRConvert.ToMatrix4x4 (e.DataIndex.GetValueAsDoubleArray ("Transform"));
            handPos = m.GetTranslation ();
            handRot = m.GetRotation ();
            headObj.transform.position = headPos;
            headObj.transform.rotation = headRot;
        } else if (e.Name == "Head_Move") {
            Matrix4x4 m = VRConvert.ToMatrix4x4 (e.DataIndex.GetValueAsDoubleArray ("Transform"));
            headPos = m.GetTranslation ();
            handRot = m.GetRotation ();
        } else if (e.Name == "Mouse_Down") {	// TODO : useless for now
            Debug.Log ("mouse down");
        } else if (e.Name == "Mouse_Up") {
            Debug.Log ("mouse up");
        } else if (e.Name == "stylus1_btn0_down") {        // user 2 start to draw
            startDrawing = true;
            Debug.Log ("Red User 2 drawing down");

        } else if (e.Name == "stylus1_btn0_up") {
            startDrawing = false;
            Debug.Log ("Red User 2 drawing up");

        } else if (e.Name == "stylus1_btn1_down") {    // controll what user 2 see
            // TODO : manipulate User 2 camera
            Debug.Log ("Red User 2 control down");

        } else if (e.Name == "stylus1_btn1_up") {
            // TODO : .... I don't know for now
            Debug.Log ("Red User 2 control up");

        } else if (e.Name == "stylus1_btn1_down") {    // user 1 start to draw
            startDrawing = true;
            Debug.Log ("Red User 1 control down");

        } else if (e.Name == "stylus1_btn1_up") {
            startDrawing = false;
            Debug.Log ("Red User 1 control up");

        } else if (e.Name == "stylus1_btn1_down") {    // controll what user 1 see
            // TODO : manipulate User 1 camera
            Debug.Log ("Red User 1 control down");

        } else if (e.Name == "stylus1_btn1_up") {
            // TODO : .... I don't know for now
            Debug.Log ("Red User 1 control up");

        }
    }
Exemple #14
0
        public static VREvent FromBinary(BinaryReader br)
        {
            string  eName = br.ReadString();
            VREvent e     = new VREvent(eName);

            int nByteFields = br.ReadInt32();

            for (int i = 0; i < nByteFields; i++)
            {
                string fName = br.ReadString();
                byte   data  = br.ReadByte();
                e.AddData(fName, data);
            }

            int nByteArrayFields = br.ReadInt32();

            for (int i = 0; i < nByteArrayFields; i++)
            {
                string fName    = br.ReadString();
                int    nEntries = br.ReadInt32();
                byte[] data     = new byte[nEntries];
                for (int j = 0; j < nEntries; j++)
                {
                    data[j] = br.ReadByte();
                }
                e.AddData(fName, data);
            }


            int nIntFields = br.ReadInt32();

            for (int i = 0; i < nIntFields; i++)
            {
                string fName = br.ReadString();
                int    data  = br.ReadInt32();
                e.AddData(fName, data);
            }

            int nIntArrayFields = br.ReadInt32();

            for (int i = 0; i < nIntArrayFields; i++)
            {
                string fName    = br.ReadString();
                int    nEntries = br.ReadInt32();
                int[]  data     = new int[nEntries];
                for (int j = 0; j < nEntries; j++)
                {
                    data[j] = br.ReadInt32();
                }
                e.AddData(fName, data);
            }


            int nFloatFields = br.ReadInt32();

            for (int i = 0; i < nFloatFields; i++)
            {
                string fName = br.ReadString();
                float  data  = br.ReadSingle();
                e.AddData(fName, data);
            }

            int nFloatArrayFields = br.ReadInt32();

            for (int i = 0; i < nFloatArrayFields; i++)
            {
                string  fName    = br.ReadString();
                int     nEntries = br.ReadInt32();
                float[] data     = new float[nEntries];
                for (int j = 0; j < nEntries; j++)
                {
                    data[j] = br.ReadSingle();
                }
                e.AddData(fName, data);
            }


            int nStringFields = br.ReadInt32();

            for (int i = 0; i < nStringFields; i++)
            {
                string fName = br.ReadString();
                string data  = br.ReadString();
                e.AddData(fName, data);
            }

            int nStringArrayFields = br.ReadInt32();

            for (int i = 0; i < nStringArrayFields; i++)
            {
                string   fName    = br.ReadString();
                int      nEntries = br.ReadInt32();
                string[] data     = new string[nEntries];
                for (int j = 0; j < nEntries; j++)
                {
                    data[j] = br.ReadString();
                }
                e.AddData(fName, data);
            }

            return(e);
        }
Exemple #15
0
        public static void ReceiveEventData(ref TcpClient client, ref List <VREvent> inputEvents)
        {
            // Debug.Log("WaitForAndReceiveInputEvents");

            // 1. receive 1-byte message header
            ReceiveOneByteMessage(ref client, VRNet.INPUT_EVENTS_MSG);

            // 2. receive event data
            try {
                int    dataSize = ReadInt32(ref client);
                byte[] bytes    = new byte[dataSize];
                int    status   = ReceiveAll(ref client, ref bytes, dataSize);
                if (status == -1)
                {
                    Console.WriteLine("ReceiveEventData error reading data");
                    return;
                }

                using (MemoryStream ms = new MemoryStream(bytes)) {
                    using (BinaryReader br = new BinaryReader(ms)) {
                        int nEvents = br.ReadInt32();
                        for (int i = 0; i < nEvents; i++)
                        {
                            inputEvents.Add(VREvent.FromBinary(br));
                        }
                    }
                }
            }
            catch (Exception e) {
                Debug.Log("Exception: " + e);
                Console.WriteLine("Exception: {0}", e);
                BrokenConnectionError();
            }


            /**
             * // 2. receive int that tells us the size of the data portion of the message in bytes
             * Int32 dataSize = ReadInt32(ref client);
             *
             * // 3. receive dataSize bytes, then decode these as InputEvents
             * byte[] buf2 = new byte[dataSize + 1];
             * int status = ReceiveAll(ref client, ref buf2, dataSize);
             * if (status == -1) {
             *  Console.WriteLine("ReceiveEventData error reading data");
             *  return;
             * }
             * buf2[dataSize] = 0;
             *
             * // buf2 is the XML string that contains all the events.
             * string serializedQueue = System.Text.Encoding.UTF8.GetString(buf2);
             * //Debug.Log("Queue = " + serializedQueue);
             *
             * // Extract the VRDataQueue object
             * Dictionary<string, string> queueProps = new Dictionary<string, string>();
             * string queueContent = string.Empty;
             * string queueLeftover = string.Empty;
             * bool queueSuccess = XMLUtils.GetXMLField(serializedQueue, "VRDataQueue", ref queueProps, ref queueContent, ref queueLeftover);
             * if (!queueSuccess) {
             *  Debug.Log("Error decoding VRDataQueue");
             *  return;
             * }
             *
             * // The queue contents are VRDataItems, extract each one
             * int nItems = Convert.ToInt32(queueProps["num"]);
             *
             * //Debug.Log("Num = " + nItems);
             * //Debug.Log(queueContent);
             *
             * for (int i = 0; i < nItems; i++) {
             *  Dictionary<string, string> itemProps = new Dictionary<string, string>();
             *  string itemContent = string.Empty;
             *  string itemLeftover = string.Empty;
             *  bool itemSuccess = XMLUtils.GetXMLField(queueContent, "VRDataQueueItem", ref itemProps, ref itemContent, ref itemLeftover);
             *  if (!itemSuccess) {
             *      Debug.Log("Error decoding VRDataQueueItem #" + i);
             *      return;
             *  }
             *
             *  // Create a new VREvent from the content of this item
             *  //Debug.Log("Item Content = " + itemContent);
             *  VREvent e = new VREvent(ref itemContent);
             *  inputEvents.Add(e);
             *
             *  // Update the content to point to the next item if there is one
             *  queueContent = itemLeftover;
             * }
             **/
        }
Exemple #16
0
        // Update is called once per frame
        void Update()
        {
            // update the trackers only once per frame
            if (lastAnalogUpdateFrame != Time.frameCount)
            {
                UpdateAnalogs();
                lastAnalogUpdateFrame = Time.frameCount;
            }
            analogData = (AnalogData)Marshal.PtrToStructure(analogDataPointer, typeof(AnalogData));

            if (!Mathf.Approximately((float)analogData.state, lastAnalogState))
            {
                lastAnalogState = (float)analogData.state;

                VREvent e = new VREvent(eventName);
                e.AddData("EventType", "AnalogUpdate");
                e.AddData("AnalogValue", (float)analogData.state);
                pendingEvents.Add(e);

                if (applyUpdatesToGameObject)
                {
                    float speedThisFrame = speed * Time.deltaTime * (float)analogData.state;

                    if (movementType == MovementType.TRANSLATE)
                    {
                        Vector3 position = transform.localPosition;
                        if (axis == Axis.X)
                        {
                            position.x += speedThisFrame;
                        }
                        else if (axis == Axis.Y)
                        {
                            position.y += speedThisFrame;
                        }
                        else if (axis == Axis.Z)
                        {
                            position.z += speedThisFrame;
                        }
                        else
                        {
                            position.x += speedThisFrame;
                            position.y += speedThisFrame;
                            position.z += speedThisFrame;
                        }

                        transform.localPosition = position;
                    }
                    else if (movementType == MovementType.ROTATE)
                    {
                        Quaternion newRotation = Quaternion.identity;
                        Vector3    newAngles   = newRotation.eulerAngles;

                        if (axis == Axis.X)
                        {
                            newAngles.x += speedThisFrame;
                        }
                        else if (axis == Axis.Y)
                        {
                            newAngles.y += speedThisFrame;
                        }
                        else if (axis == Axis.Z)
                        {
                            newAngles.z += speedThisFrame;
                        }
                        else
                        {
                            newAngles.x += speedThisFrame;
                            newAngles.y += speedThisFrame;
                            newAngles.z += speedThisFrame;
                        }

                        if (newAngles.x > 360)
                        {
                            newAngles.x %= 360;
                        }

                        if (newAngles.y > 360)
                        {
                            newAngles.y %= 360;
                        }

                        if (newAngles.z > 360)
                        {
                            newAngles.z %= 360;
                        }

                        newRotation.eulerAngles = newAngles;


                        Quaternion rotation = transform.localRotation;
                        rotation = rotation * newRotation;
                        transform.localRotation = rotation;
                    }
                    else if (movementType == MovementType.SCALE)
                    {
                        Vector3 scale = transform.localScale;
                        if (axis == Axis.X)
                        {
                            scale.x += speedThisFrame;
                        }
                        else if (axis == Axis.Y)
                        {
                            scale.y += speedThisFrame;
                        }
                        else if (axis == Axis.Z)
                        {
                            scale.z += speedThisFrame;
                        }
                        else
                        {
                            scale.x += speedThisFrame;
                            scale.y += speedThisFrame;
                            scale.z += speedThisFrame;
                        }

                        transform.localScale = scale;
                    }
                }
            }
        }
Exemple #17
0
        void WaitForAndReceiveInputEvents(ref List<VREvent> inputEvents)
        {
            // 1. receive 1-byte message header
            WaitForAndReceiveMessageHeader(INPUT_EVENTS_MSG);

            // 2. receive int that tells us the size of the data portion of the message in bytes
            Int32 dataSize = ReadInt32();

            // 3. receive dataSize bytes, then decode these as InputEvents
            byte[] buf2 = new byte[dataSize+1];
            int status = ReceiveAll(ref buf2, dataSize);
            if (status == -1) {
            Console.WriteLine("WaitForAndReceiveInputEvents error reading data");
            return;
            }
            buf2[dataSize] = 0;

            // buf2 is the XML string that contains all the events.
            string xmlEventList = System.Text.Encoding.UTF8.GetString(buf2);
            //Debug.Log(xmlEventList);

            Dictionary<string, string> props = new Dictionary<string, string>();
            string xmlEvents = string.Empty;
            string leftover = string.Empty;
            bool success = XMLUtils.GetXMLField(xmlEventList, "VREventList", ref props, ref xmlEvents, ref leftover);
            if (!success) {
            Debug.Log("Error decoding VREventList");
            return;
            }
            int nEvents = Convert.ToInt32(props["num"]);
            for (int i=0; i<nEvents; i++) {
            // Create the next VREvent from the XML description
            VREvent e = new VREvent(ref xmlEvents);
            inputEvents.Add(e);
            }
        }
Exemple #18
0
 void AddInputEvent(VREvent e)
 {
     inputEvents.Add(e);
 }
Exemple #19
0
        private void AddTrackerEvents(ref List <VREvent> eventList)
        {
            float x = Input.mousePosition.x;
            float y = Input.mousePosition.y;

            // first time through
            if (float.IsNaN(lastx))
            {
                lastx = x;
                lasty = y;
                return;
            }

            if (Input.GetKeyDown("1"))
            {
                curTracker = 0;
            }
            else if (Input.GetKeyDown("2"))
            {
                curTracker = 1;
            }

            if (Input.GetKey("x"))
            {
                float angle = 0.1f * (x - lastx);
                if (curTracker == 0)
                {
                    tracker1Rot = Quaternion.AngleAxis(angle, new Vector3(1f, 0f, 0f)) * tracker1Rot;
                }
                else if (curTracker == 1)
                {
                    tracker2Rot = Quaternion.AngleAxis(angle, new Vector3(1f, 0f, 0f)) * tracker2Rot;
                }
            }
            else if (Input.GetKey("y"))
            {
                float angle = 0.1f * (x - lastx);
                if (curTracker == 0)
                {
                    tracker1Rot = Quaternion.AngleAxis(angle, new Vector3(0f, 1f, 0f)) * tracker1Rot;
                }
                else if (curTracker == 1)
                {
                    tracker2Rot = Quaternion.AngleAxis(angle, new Vector3(0f, 1f, 0f)) * tracker2Rot;
                }
            }
            else if (Input.GetKey("z"))
            {
                float angle = 0.1f * (x - lastx);
                if (curTracker == 0)
                {
                    tracker1Rot = Quaternion.AngleAxis(angle, new Vector3(0f, 0f, 1f)) * tracker1Rot;
                }
                else if (curTracker == 1)
                {
                    tracker2Rot = Quaternion.AngleAxis(angle, new Vector3(0f, 0f, 1f)) * tracker2Rot;
                }
            }
            else if (Input.GetKey("left shift"))
            {
                float depth = 0.005f * (y - lasty);
                if (curTracker == 0)
                {
                    tracker1Pos += depth * Camera.main.transform.forward;
                }
                else if (curTracker == 1)
                {
                    tracker2Pos += depth * Camera.main.transform.forward;
                }
            }
            else
            {
                Ray   ray  = Camera.main.ScreenPointToRay(new Vector3(x, y, 0f));
                Plane p    = new Plane();
                float dist = 0.0f;
                if (curTracker == 0)
                {
                    p.SetNormalAndPosition(-Camera.main.transform.forward, tracker1Pos);
                    if (p.Raycast(ray, out dist))
                    {
                        tracker1Pos = ray.GetPoint(dist);
                    }
                }
                else if (curTracker == 1)
                {
                    p.SetNormalAndPosition(-Camera.main.transform.forward, tracker2Pos);
                    if (p.Raycast(ray, out dist))
                    {
                        tracker2Pos = ray.GetPoint(dist);
                    }
                }
            }

            // for fake traker 1
            Matrix4x4 m1 = Matrix4x4.TRS(tracker1Pos, tracker1Rot, Vector3.one);

            float[] d1 = VRConvert.ToFloatArray(m1);
            VREvent e1 = new VREvent(fakeTracker1Event);

            e1.AddData("EventType", "TrackerMove");
            e1.AddData("Transform", d1);
            eventList.Add(e1);

            // for fake traker 2
            Matrix4x4 m2 = Matrix4x4.TRS(tracker2Pos, tracker2Rot, Vector3.one);

            float[] d2 = VRConvert.ToFloatArray(m2);
            VREvent e2 = new VREvent(fakeTracker2Event);

            e2.AddData("EventType", "TrackerMove");
            e2.AddData("Transform", d2);
            eventList.Add(e2);

            //
            this.lastx = x;
            this.lasty = y;
        }
        void OnVREvent(VREvent e)
        {
            if (e.Name == user1HeadTrackingEvent)
            {
                Matrix4x4 m = VRConvert.ToMatrix4x4(e.DataIndex.GetValueAsDoubleArray(matrix4x4DataField));

                if (user_id == 1)
                {
                    transform.position = m.GetTranslation();
                    transform.rotation = m.GetRotation();
                }
                //Debug.Log("Head Position = " + m.GetTranslation().ToString());
                //Debug.Log("Head Rotation = " + m.GetRotation().ToString());

                //------------------ Avatar 1 movement ------------------------
                // position
                Vector3 pos = m.GetTranslation();
                user1Avatar.transform.position = new Vector3(pos.x, user1Avatar.transform.position.y, pos.z);


                // rotation
                Quaternion rot = m.GetRotation();
                // 3rd column of a rotation matrix is the z-axis
                Vector3 z = new Vector3(m.GetColumn(2).x, m.GetColumn(2).y, m.GetColumn(2).z);
                // z-axis of the tracker projected onto a horizontal plane
                Vector3 zHorizontal = (z - Vector3.Dot(z, Vector3.up) * Vector3.up).normalized;

                // align the z axis of the avatar with zHorizontal
                Quaternion newRot = Quaternion.FromToRotation(new Vector3(0, 0, 1), zHorizontal);
                user1Avatar.transform.rotation = newRot;

                //------------------ Menu movement ------------------------
                if (user_id == 1)
                {
                    /*
                     * GameObject terrain = GameObject.Find ("Terrain");
                     * MenuContainer.transform.position = new Vector3 (pos.x, terrain.transform.position.y, pos.z);
                     * MenuContainer.transform.rotation = newRot;
                     */

                    Text hPos = GameObject.Find("MinVRUnityClient/VRCameraPair/HeadPosition").GetComponent <Text> ();
                    Text mPos = GameObject.Find("MinVRUnityClient/VRCameraPair/MenuPosition").GetComponent <Text> ();
                    Text bPos = GameObject.Find("MinVRUnityClient/VRCameraPair/BrushPosition").GetComponent <Text> ();

                    //MenuContainer.transform.position = new Vector3 (pos.x, pos.y - 50, pos.z);
                    //MenuContainer.transform.rotation = newRot;

                    hPos.text = "H pos: " + pos.ToString();
                    mPos.text = "m Pos: " + MenuContainer.transform.position.ToString();
                    bPos.text = "b Pos: " + brush1.transform.position.ToString();
                }
            }
            else if (e.Name == user2HeadTrackingEvent)
            {
                Matrix4x4 m = VRConvert.ToMatrix4x4(e.DataIndex.GetValueAsDoubleArray(matrix4x4DataField));

                if (user_id == 2)
                {
                    transform.position = m.GetTranslation();
                    transform.rotation = m.GetRotation();
                }
                //Debug.Log("Head Position = " + m.GetTranslation().ToString());
                //Debug.Log("Head Rotation = " + m.GetRotation().ToString());

                //------------------ Avatar 2 movement ------------------------
                // position
                Vector3 pos = m.GetTranslation();
                user2Avatar.transform.position = new Vector3(pos.x, user2Avatar.transform.position.y, pos.z);

                // rotation
                Quaternion rot = m.GetRotation();
                // 3rd column of a rotation matrix is the z-axis
                Vector3 z = new Vector3(m.GetColumn(2).x, m.GetColumn(2).y, m.GetColumn(2).z);
                // z-axis of the tracker projected onto a horizontal plane
                Vector3 zHorizontal = (z - Vector3.Dot(z, Vector3.up) * Vector3.up).normalized;

                // align the z axis of the avatar with zHorizontal
                Quaternion newRot = Quaternion.FromToRotation(new Vector3(0, 0, 1), zHorizontal);
                user2Avatar.transform.rotation = newRot;

                //------------------ Menu movement ------------------------
                if (user_id == 2)
                {
                    Text hPos = GameObject.Find("MinVRUnityClient/VRCameraPair/HeadPosition").GetComponent <Text> ();
                    Text mPos = GameObject.Find("MinVRUnityClient/VRCameraPair/MenuPosition").GetComponent <Text> ();
                    Text bPos = GameObject.Find("MinVRUnityClient/VRCameraPair/BrushPosition").GetComponent <Text> ();

                    //MenuContainer.transform.position = new Vector3 (pos.x, pos.y - 50, pos.z);
                    //MenuContainer.transform.rotation = newRot;

                    hPos.text = "H pos: " + pos.ToString();
                    mPos.text = "m Pos: " + MenuContainer.transform.position.ToString();
                    bPos.text = "b Pos: " + brush2.transform.position.ToString();
                }
            }
        }