There is a very simple way of adding this functionality in the official Optitrack Untiy Plugin (I’m referring to version 1.0.1 here, but I expect you will get this done in future releases in a similar way):
In Assets/OptiTrack/Scripts/OptitrackStreamingClient.cs, there is a method called OnNatNetFrameReceived, where you get NatNetClient.NativeFrameReceivedEventArgs eventArgs as an argument. The class already implements doing most of the marshalling for you, although it is not used by the plugin (yet?), giving you an sFrameOfMocapData struct, which contains the fields OtherMarkersCount and OtherMarkers. I added a list of OptitrackMarkerState to the OptitrackStreamingClient class like this
private List<OptitrackMarkerState> m_latestOtherMarkerStates = new List<OptitrackMarkerState>();
and added this to the OnNatNetFrameReceived method, just above the code where updating of rigid bodies is done:
sFrameOfMocapData frame = eventArgs.MarshaledFrame;
//Debug.Log( "labeled: " + frame.LabeledMarkerCount + ", other: " + frame.OtherMarkerCount + ", sets: " + frame.MarkerSetCount );
float[ ] vec = new float[3 * frame.OtherMarkerCount];
System.Runtime.InteropServices.Marshal.Copy( frame.OtherMarkers, vec, 0, vec.Length );
m_latestOtherMarkerStates.Clear();
for( int i = 0; i < frame.OtherMarkerCount; i++ )
{
OptitrackMarkerState state = new OptitrackMarkerState();
state.Position = new Vector3( -vec[i * 3 + 0], vec[i * 3 + 1], vec[i * 3 + 2] );
//TODO: where do i get the size from?
state.Size = 0.01f;
m_latestOtherMarkerStates.Add( state );
//Debug.Log( “marker #” + i + “: [” + vec[i * 3 + 0] + ", " + vec[i * 3 + 1] + ", " + vec[i * 3 + 2] );
}
Works like a charm for me. You can also go ahead and implement an editor script so you have the markers drawn in the Unity scene view, use OptitrackRigidBodyEditor.cs as a reference, it isn’t too hard.