Example #1
0
        public bool Connect(IMoCapClient_ConnectionInfo connectionInfo)
        {
            // extract filename from connection info
            dataStream = (connectionInfo is ConnectionInfo) ?
                         ((ConnectionInfo)connectionInfo).dataStream :
                         null;        // fallback > no stream

            // try connecting
            try
            {
                if ((dataStream != null) && dataStream.Open())
                {
                    GetSceneDescription();

                    Debug.Log("Reading from MOT file '" + dataStream.GetName() + "'.");

                    // immediately get first packet of frame data
                    GetFrameData();

                    streamingTimer = new Timer(new TimerCallback(StreamingTimerCallback));
                    streamingTimer.Change(0, 1000 / updateRate);
                }
            }
            catch (Exception e)
            {
                Debug.LogWarning("Could not read from MOT file '" + dataStream.GetName() + "' (" + e.Message + ").\n" + e.StackTrace);
                dataStream.Close();
                dataStream = null;
            }

            return(IsConnected());
        }
		public bool Connect(IMoCapClient_ConnectionInfo connectionInfo)
		{
			// extract filename from connection info
			dataStream = (connectionInfo is ConnectionInfo) ?
				((ConnectionInfo)connectionInfo).dataStream :
				null; // fallback > no stream

			// try connecting
			try
			{
				if ((dataStream != null) && dataStream.Open())
				{
					GetSceneDescription();

					Debug.Log("Reading from MOT file '" + dataStream.GetName() + "'.");
					// print list of actor and device names
					if (scene.actors.Length > 0)
					{
						string actorNames = "";
						foreach (Actor a in scene.actors)
						{
							if (actorNames.Length > 0) { actorNames += ", "; }
							actorNames += a.name;
						}
						Debug.Log("Actors (" + scene.actors.Length + "): " + actorNames);
					}
					if (scene.devices.Length > 0)
					{
						string deviceNames = "";
						foreach (Device d in scene.devices)
						{
							if (deviceNames.Length > 0) { deviceNames += ", "; }
							deviceNames += d.name;
						}
						Debug.Log("Devices (" + scene.devices.Length + "): " + deviceNames);
					}

					// immediately get first packet of frame data
					GetFrameData();

					NotifyListeners_Change();

					streamingTimer = new Timer(new TimerCallback(StreamingTimerCallback));
					streamingTimer.Change(0, 1000 / updateRate);
				}
			}
			catch (Exception e)
			{
				Debug.LogWarning("Could not read from MOT file '" + dataStream.GetName() + "' (" + e.Message + ").");
				dataStream.Close();
				dataStream = null;
			}

			return IsConnected();
		}
		public bool Connect(IMoCapClient_ConnectionInfo connectionInfo)
		{
			connected = VRDevice.isPresent;

			if (connected)
			{
				system = OpenVR.System;
				if (system == null)
				{
					connected = false;
					Debug.LogWarning("Could not find OpenVR System instance.");
				}
				compositor = OpenVR.Compositor;
				if (compositor == null)
				{
					connected = false;
					Debug.LogWarning("Could not find OpenVR Compositor instance.");
				}
			}

			if (connected)
			{
				poses     = new TrackedDevicePose_t[OpenVR.k_unMaxTrackedDeviceCount];
				gamePoses = new TrackedDevicePose_t[0];

				FindControllerIndices();
				scene.actors  = new Actor[controllerIndices.Length];
				scene.devices = new Device[controllerIndices.Length];
				states        = new VRControllerState_t[controllerIndices.Length];

				for (int idx = 0; idx < controllerIndices.Length; idx++)
				{
					string name = "Controller" + (idx + 1);

					Actor actor        = new Actor(scene, name, idx);
					actor.bones        = new Bone[1];
					actor.bones[0]     = new Bone(actor, "root", 0);
					scene.actors[idx]  = actor;

					Device device      = new Device(scene, name, idx);
					device.channels    = new Channel[7];
					device.channels[0] = new Channel(device, "button1");  // fire
					device.channels[1] = new Channel(device, "button2");  // menu
					device.channels[2] = new Channel(device, "button3");  // grip
					device.channels[3] = new Channel(device, "axis1");    // touchpad + press
					device.channels[4] = new Channel(device, "axis2");
					device.channels[5] = new Channel(device, "axis1raw"); // touchpad touch
					device.channels[6] = new Channel(device, "axis2raw");

					scene.devices[idx] = device;
				}
			}
			return connected;
		}
        public bool Connect(IMoCapClient_ConnectionInfo connectionInfo)
        {
            connected = XRDevice.isPresent;

            if (connected)
            {
                InputTracking.GetNodeStates(nodeStates);
                actors = new Dictionary <ulong, Actor>();

                // construct scene description
                scene.actors.Clear();
                foreach (XRNodeState state in nodeStates)
                {
                    CreateActor(state);
                }
            }
            return(connected);
        }
		public bool Connect(IMoCapClient_ConnectionInfo connectionInfo)
		{
			// successful every time
			connected = true;
			return true;
		}
        public bool Connect(IMoCapClient_ConnectionInfo connectionInfo)
        {
            connected = XRDevice.isPresent;

            if (connected)
            {
                try
                {
                    system = OpenVR.System;
                    if (system == null)
                    {
                        connected = false;
                        Debug.LogWarning("Could not find OpenVR System instance.");
                    }

                    compositor = OpenVR.Compositor;
                    if ((system != null) && (compositor == null))
                    {
                        connected = false;
                        Debug.LogWarning("Could not find OpenVR Compositor instance.");
                    }
                }
                catch (DllNotFoundException)
                {
                    // well, can't do anything about this
                    connected = false;
                }
            }

            if (connected)
            {
                // query refresh rate
                updateRate = XRDevice.refreshRate;
                if (updateRate == 0)
                {
                    updateRate = 60;
                }                                                         // fallback

                // allocate structures
                state     = new VRControllerState_t();
                poses     = new TrackedDevicePose_t[OpenVR.k_unMaxTrackedDeviceCount];
                gamePoses = new TrackedDevicePose_t[0];

                // find HMDs, trackers and controllers
                trackedDevices.Clear();
                int controllerCount = 0;
                int trackerCount    = 0;
                int hmdCount        = 0;
                int inputDeviceIdx  = 0;
                for (int index = 0; index < OpenVR.k_unMaxTrackedDeviceCount; index++)
                {
                    ETrackedDeviceClass deviceClass   = system.GetTrackedDeviceClass((uint)index);
                    TrackedDevice       trackedDevice = null;
                    if (deviceClass == ETrackedDeviceClass.Controller)
                    {
                        controllerCount++;
                        trackedDevice = new TrackedDevice("ViveController" + controllerCount);

                        // controller has 12 input channels and 1 output
                        Device dev = new Device(scene, trackedDevice.name, inputDeviceIdx);
                        dev.channels         = new Channel[13];
                        dev.channels[0]      = new Channel(dev, "button1");                      // fire
                        dev.channels[1]      = new Channel(dev, "button2");                      // menu
                        dev.channels[2]      = new Channel(dev, "button3");                      // grip
                        dev.channels[3]      = new Channel(dev, "button4");                      // touchpad press
                        dev.channels[4]      = new Channel(dev, "axis1");                        // touchpad + press
                        dev.channels[5]      = new Channel(dev, "axis2");
                        dev.channels[6]      = new Channel(dev, "axis1raw");                     // touchpad touch
                        dev.channels[7]      = new Channel(dev, "axis2raw");
                        dev.channels[8]      = new Channel(dev, "right");                        // touchpad as buttons
                        dev.channels[9]      = new Channel(dev, "left");
                        dev.channels[10]     = new Channel(dev, "up");
                        dev.channels[11]     = new Channel(dev, "down");
                        dev.channels[12]     = new Channel(dev, "rumble");                       // rumble output
                        trackedDevice.device = dev;
                    }
                    else if (deviceClass == ETrackedDeviceClass.GenericTracker)
                    {
                        trackerCount++;
                        trackedDevice = new TrackedDevice("ViveTracker" + trackerCount);

                        // tracker has 4 input channels and 1 output channel
                        Device dev = new Device(scene, trackedDevice.name, inputDeviceIdx);
                        dev.channels         = new Channel[5];
                        dev.channels[0]      = new Channel(dev, "input1");                    // pin 3: grip
                        dev.channels[1]      = new Channel(dev, "input2");                    // pin 4: trigger
                        dev.channels[2]      = new Channel(dev, "input3");                    // pin 5: touchpad press
                        dev.channels[3]      = new Channel(dev, "input4");                    // pin 6: menu
                        dev.channels[4]      = new Channel(dev, "output1");                   // pin 1: rumble output
                        trackedDevice.device = dev;
                    }
                    else if (deviceClass == ETrackedDeviceClass.HMD)
                    {
                        hmdCount++;
                        trackedDevice = new TrackedDevice("ViveHMD" + hmdCount);
                    }

                    if (trackedDevice != null)
                    {
                        trackedDevice.controllerIdx = index;
                        trackedDevice.deviceClass   = deviceClass;
                        trackedDevices.Add(trackedDevice);

                        if (trackedDevice.device != null)
                        {
                            inputDeviceIdx++;
                        }
                    }
                }

                // construct scene description
                scene.actors.Clear();
                scene.devices.Clear();
                foreach (TrackedDevice td in trackedDevices)
                {
                    Actor actor = new Actor(scene, td.name);
                    actor.bones    = new Bone[1];
                    actor.bones[0] = new Bone(actor, "root", 0);
                    scene.actors.Add(actor);

                    // is this an input device, too?
                    if (td.device != null)
                    {
                        scene.devices.Add(td.device);
                    }
                }
            }
            return(connected);
        }
Example #7
0
        public bool Connect(IMoCapClient_ConnectionInfo connectionInfo)
        {
            // extract server address from connection info
            IPAddress serverAddress = (connectionInfo is NatNetClient.ConnectionInfo) ?
                                      ((NatNetClient.ConnectionInfo)connectionInfo).serverAddress :
                                      IPAddress.Loopback; // fallback > localhost

            // try connecting
            try
            {
                IPEndPoint commandEndpoint = new IPEndPoint(serverAddress, PORT_COMMAND);

                packetOut = new NatNetPacket_Out(commandEndpoint);
                packetIn  = new NatNetPacket_In();

                commandClient = new UdpClient();                 // connect to server's command port

                if (PingServer())
                {
                    GetSceneDescription();

                    Debug.Log("Connected to NatNet server '" + serverInfo.serverName + "'.");

                    // immediately get first packet of frame data
                    GetFrameData();
                    connected        = true;
                    streamingEnabled = false;
                }
            }
            catch (Exception e)
            {
                Debug.LogWarning("Could not connect to NatNet server " + serverAddress + " (" + e.Message + ").");
            }

            if (connected)
            {
                // request streaming IP address
                try
                {
                    if (SendRequest("getDataStreamAddress") && (serverResponse.Length > 0))
                    {
                        multicastAddress = IPAddress.Parse(serverResponse);
                        Debug.Log("Data stream address: " + multicastAddress);

                        // Prepare multicast data reception
                        dataClient = new UdpClient();
                        dataClient.ExclusiveAddressUse = false;
                        dataClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
                        dataClient.Client.Bind(new IPEndPoint(IPAddress.Any, PORT_DATA));
                        dataClient.JoinMulticastGroup(multicastAddress);

                        dataClient.Client.ReceiveTimeout = 100;
                    }
                }
                catch (Exception e)
                {
                    Debug.LogWarning("Could not establish data streaming (" + e.Message + ").");
                    if (dataClient != null)
                    {
                        dataClient.Close();
                        dataClient = null;
                    }
                }

                // request framerate
                try
                {
                    if (SendRequest("getFramerate") && (serverResponse.Length > 0))
                    {
                        updateRate = float.Parse(serverResponse);
                    }
                }
                catch (Exception e)
                {
                    Debug.LogWarning("Could not establish framerate (" + e.Message + ").");
                }
            }

            return(connected);
        }
Example #8
0
 public bool Connect(IMoCapClient_ConnectionInfo connectionInfo)
 {
     // successful every time
     connected = true;
     return(connected);
 }
		public bool Connect(IMoCapClient_ConnectionInfo connectionInfo)
		{
			// extract server address from connection info
			IPAddress serverAddress = (connectionInfo is NatNetClient.ConnectionInfo) ?
				((NatNetClient.ConnectionInfo) connectionInfo).serverAddress :
				IPAddress.Loopback; // fallback > localhost
			
			// try connecting
			try
			{
				IPEndPoint commandEndpoint = new IPEndPoint(serverAddress, PORT_COMMAND);

				packetOut = new NatNetPacket_Out(commandEndpoint);
				packetIn  = new NatNetPacket_In();
				
				commandClient = new UdpClient(); // connect to server's command port

				if ( PingServer() )
				{
					GetSceneDescription();

					Debug.Log("Connected to NatNet server '" + serverInfo.serverName + "'.");
					// print list of actor and device names
					if (scene.actors.Length > 0)
					{
						string actorNames = "";
						foreach (Actor a in scene.actors)
						{
							if (actorNames.Length > 0) { actorNames += ", "; }
							actorNames += a.name;
						}
						Debug.Log("Actors (" + scene.actors.Length + "): " + actorNames);
					}
					if (scene.devices.Length > 0)
					{
						string deviceNames = "";
						foreach (Device d in scene.devices)
						{
							if (deviceNames.Length > 0) { deviceNames += ", "; }
							deviceNames += d.name;
						}
						Debug.Log("Devices (" + scene.devices.Length + "): " + deviceNames);
					}

					// immediately get first packet of frame data
					GetFrameData();
					connected = true;
					streamingEnabled = false;
				}
			}
			catch (Exception e)
			{
				Debug.LogWarning("Could not connect to NatNet server " + serverAddress + " (" + e.Message + ").");
			}

			// request streaming IP address
			if (connected)
			{
				try
				{
					if (SendRequest("getDataStreamAddress") && (serverResponse.Length > 0))
					{
						multicastAddress = IPAddress.Parse(serverResponse);
						Debug.Log("Data stream address: " + multicastAddress);

						// Prepare multicast data reception
						dataClient = new UdpClient();
						dataClient.ExclusiveAddressUse = false;
						dataClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
						dataClient.Client.Bind(new IPEndPoint(IPAddress.Any, PORT_DATA));
						dataClient.JoinMulticastGroup(multicastAddress);

						dataClient.Client.ReceiveTimeout = 100;
					}
				}
				catch (Exception e)
				{
					Debug.LogWarning("Could not establish data streaming (" + e.Message + ").");
					if (dataClient != null)
					{
						dataClient.Close();
						dataClient = null;
					}
				}
			}

			return connected;
		}