예제 #1
0
        private MixedRealityPose GetPoseData(ref MlHandTracking.MLKeyPointState keyPoint, ref MlSnapshot.MLSnapshot snapshot)
        {
            var pose = poseIdentity;

            if (keyPoint.is_valid)
            {
                if (MlSnapshot.MLSnapshotGetTransform(snapshot, keyPoint.frame_id, ref tempTransform).IsOk)
                {
                    pose.Position = tempTransform.position;
                }
                else
                {
                    pose = poseIdentity;
                    Debug.LogError($"{nameof(MlSnapshot.MLSnapshotGetTransform)} Failed!");
                }
            }
            else
            {
                pose = poseIdentity;
            }

            return(pose);
        }
예제 #2
0
        private void UpdateControllerData(MlInput.MLInputControllerState inputState, MlController.MLControllerState controllerState)
        {
            var lastState = TrackingState;

            lastControllerPose = currentControllerPose;

            var bestStream = controllerState.stream[0];

            for (int i = 0; i < controllerState.stream.Length; i++)
            {
                if (!controllerState.stream[i].is_active)
                {
                    continue;
                }

                if (bestStream.mode <= controllerState.stream[i].mode)
                {
                    bestStream = controllerState.stream[i];
                }
            }

            if (!MlPerception.MLPerceptionGetSnapshot(out var snapshot).IsOk)
            {
                Debug.LogError("Failed to get perception snapshot!");
            }

            if (bestStream.is_active)
            {
                if (!MlSnapshot.MLSnapshotGetTransform(snapshot, bestStream.coord_frame_controller, ref controllerTransform).IsOk)
                {
                    Debug.LogError($"Failed to get snapshot transform for controller {controllerState.controller_id}:{bestStream}");
                }
            }

            if (!MlPerception.MLPerceptionReleaseSnapshot(snapshot).IsOk)
            {
                Debug.LogError("Failed to release perception snapshot!");
            }

            if (inputState.type == MlInput.MLInputControllerType.Device)
            {
                // The source is either a hand or a controller that supports pointing.
                // We can now check for position and rotation.
                IsPositionAvailable = bestStream.mode > MlController.MLControllerMode.Imu3Dof;

                if (IsPositionAvailable)
                {
                    IsPositionApproximate = controllerState.accuracy <= MlController.MLControllerCalibAccuracy.Medium;
                }
                else
                {
                    IsPositionApproximate = false;
                }

                IsRotationAvailable = bestStream.is_active;

                // Devices are considered tracked if we receive position OR rotation data from the sensors.
                TrackingState = (IsPositionAvailable || IsRotationAvailable) ? TrackingState.Tracked : TrackingState.NotTracked;
            }
            else
            {
                // The input source does not support tracking.
                TrackingState = TrackingState.NotApplicable;
            }

            currentControllerPose.Position = controllerTransform.position;
            currentControllerPose.Rotation = controllerTransform.rotation;

            // Raise input system events if it is enabled.
            if (lastState != TrackingState)
            {
                InputSystem?.RaiseSourceTrackingStateChanged(InputSource, this, TrackingState);
            }

            if (TrackingState == TrackingState.Tracked && lastControllerPose != currentControllerPose)
            {
                if (IsPositionAvailable && IsRotationAvailable)
                {
                    InputSystem?.RaiseSourcePoseChanged(InputSource, this, currentControllerPose);
                }
                else if (IsPositionAvailable && !IsRotationAvailable)
                {
                    InputSystem?.RaiseSourcePositionChanged(InputSource, this, currentControllerPose.Position);
                }
                else if (!IsPositionAvailable && IsRotationAvailable)
                {
                    InputSystem?.RaiseSourceRotationChanged(InputSource, this, currentControllerPose.Rotation);
                }
            }
        }