Exemplo n.º 1
0
    /// <summary>
    /// Draws marker visualizations in the editor viewport for any selected OptitrackRigidBody component.
    /// </summary>
    void OnSceneGUI()
    {
        OptitrackRigidBody rb = target as OptitrackRigidBody;

        if (!rb || rb.StreamingClient == null)
        {
            return;
        }

        rb.StreamingClient._EnterFrameDataUpdateLock();

        try
        {
            OptitrackRigidBodyDefinition rbDef   = rb.StreamingClient.GetRigidBodyDefinitionById(rb.RigidBodyId);
            OptitrackRigidBodyState      rbState = rb.StreamingClient.GetLatestRigidBodyState(rb.RigidBodyId);

            if (rbDef != null && rbState != null)
            {
                for (int iMarker = 0; iMarker < rbDef.Markers.Count; ++iMarker)
                {
                    OptitrackRigidBodyDefinition.MarkerDefinition marker = rbDef.Markers[iMarker];

                    // Effectively treat the RB GameObject transform as a rigid transform by negating its local scale.
                    Vector3 markerPos = marker.Position;
                    markerPos.Scale(new Vector3(1.0f / rb.transform.localScale.x, 1.0f / rb.transform.localScale.y, 1.0f / rb.transform.localScale.z));
                    markerPos = rb.transform.TransformPoint(markerPos);

                    float     kMarkerSize     = 0.02f;
                    Matrix4x4 markerTransform = Matrix4x4.TRS(markerPos, Quaternion.identity, new Vector3(kMarkerSize, kMarkerSize, kMarkerSize));

                    for (int iPass = 0; iPass < m_markerMaterial.passCount; ++iPass)
                    {
                        if (m_markerMaterial.SetPass(iPass))
                        {
                            Graphics.DrawMeshNow(m_markerMesh, markerTransform);
                        }
                    }
                }
            }
        }
        finally
        {
            rb.StreamingClient._ExitFrameDataUpdateLock();
        }
    }
Exemplo n.º 2
0
    /// <summary>Request data descriptions from the host, then update our definitions.</summary>
    /// <exception cref="NatNetException">
    /// Thrown by <see cref="NatNetClient.GetDataDescriptions"/> if the request to the server fails.
    /// </exception>
    public void UpdateDefinitions()
    {
        // This may throw an exception if the server request times out or otherwise fails.
        m_dataDescs = m_client.GetDataDescriptions();

        m_rigidBodyDefinitions.Clear();
        m_skeletonDefinitions.Clear();

        // Translate rigid body definitions.
        for (int nativeRbDescIdx = 0; nativeRbDescIdx < m_dataDescs.RigidBodyDescriptions.Count; ++nativeRbDescIdx)
        {
            sRigidBodyDescription nativeRb = m_dataDescs.RigidBodyDescriptions[nativeRbDescIdx];

            OptitrackRigidBodyDefinition rbDef = new OptitrackRigidBodyDefinition {
                Id      = nativeRb.Id,
                Name    = nativeRb.Name,
                Markers = new List <OptitrackRigidBodyDefinition.MarkerDefinition>(nativeRb.MarkerCount),
            };

            // Populate nested marker definitions.
            for (int nativeMarkerIdx = 0; nativeMarkerIdx < nativeRb.MarkerCount; ++nativeMarkerIdx)
            {
                int    positionOffset = nativeMarkerIdx * Marshal.SizeOf(typeof(MarkerDataVector));
                IntPtr positionPtr    = new IntPtr(nativeRb.MarkerPositions.ToInt64() + positionOffset);

                int    labelOffset = nativeMarkerIdx * Marshal.SizeOf(typeof(Int32));
                IntPtr labelPtr    = new IntPtr(nativeRb.MarkerRequiredLabels.ToInt64() + labelOffset);

                MarkerDataVector nativePos =
                    (MarkerDataVector)Marshal.PtrToStructure(positionPtr, typeof(MarkerDataVector));

                Int32 nativeLabel = Marshal.ReadInt32(labelPtr);

                OptitrackRigidBodyDefinition.MarkerDefinition markerDef =
                    new OptitrackRigidBodyDefinition.MarkerDefinition {
                    Position      = new Vector3(nativePos.Values[0], nativePos.Values[1], nativePos.Values[2]),
                    RequiredLabel = nativeLabel,
                };

                rbDef.Markers.Add(markerDef);
            }

            m_rigidBodyDefinitions.Add(rbDef);
        }

        // Translate skeleton definitions.
        for (int nativeSkelDescIdx = 0; nativeSkelDescIdx < m_dataDescs.SkeletonDescriptions.Count; ++nativeSkelDescIdx)
        {
            sSkeletonDescription nativeSkel = m_dataDescs.SkeletonDescriptions[nativeSkelDescIdx];

            OptitrackSkeletonDefinition skelDef = new OptitrackSkeletonDefinition {
                Id    = nativeSkel.Id,
                Name  = nativeSkel.Name,
                Bones = new List <OptitrackSkeletonDefinition.BoneDefinition>(nativeSkel.RigidBodyCount),
            };

            // Populate nested bone definitions.
            for (int nativeBoneIdx = 0; nativeBoneIdx < nativeSkel.RigidBodyCount; ++nativeBoneIdx)
            {
                sRigidBodyDescription nativeBone = nativeSkel.RigidBodies[nativeBoneIdx];

                OptitrackSkeletonDefinition.BoneDefinition boneDef =
                    new OptitrackSkeletonDefinition.BoneDefinition {
                    Id       = nativeBone.Id,
                    ParentId = nativeBone.ParentId,
                    Name     = nativeBone.Name,
                    Offset   = new Vector3(-nativeBone.OffsetX, nativeBone.OffsetY, nativeBone.OffsetZ),
                };

                skelDef.Bones.Add(boneDef);
            }

            m_skeletonDefinitions.Add(skelDef);
        }
    }