Пример #1
0
        private MLResult RaycastInternal(QueryParams query, OnRaycastResultDelegate callback)
        {
            if (query == null || callback == null)
            {
                MLPluginLog.ErrorFormat("MLRaycast.Raycast failed. Reason: Invalid input parameters.");
                return(MLResult.Create(MLResult.Code.InvalidParam));
            }

            this.currentQuery.Position              = MLConvert.FromUnity(query.Position);
            this.currentQuery.Direction             = MLConvert.FromUnity(query.Direction, true, false);
            this.currentQuery.UpVector              = MLConvert.FromUnity(query.UpVector, true, false);
            this.currentQuery.Width                 = query.Width;
            this.currentQuery.Height                = query.Height;
            this.currentQuery.HorizontalFovDegrees  = query.HorizontalFovDegrees;
            this.currentQuery.CollideWithUnobserved = query.CollideWithUnobserved;

            ulong queryHandle = MagicLeapNativeBindings.InvalidHandle;

            MLResult.Code resultCode = NativeBindings.MLRaycastRequest(this.trackerHandle, ref this.currentQuery, ref queryHandle);
            if (resultCode != MLResult.Code.Ok)
            {
                MLResult result = MLResult.Create(resultCode);
                MLPluginLog.ErrorFormat("MLRaycast.Raycast failed to request a new ray cast. Reason: {0}", result);
                return(result);
            }

            this.pendingQueries.Add(queryHandle, callback);
            return(MLResult.Create(MLResult.Code.Ok));
        }
Пример #2
0
        /// <summary>
        /// Requests a ray cast with the given query parameters.
        /// </summary>
        /// <param name="query">Query parameters describing ray being cast.</param>
        /// <param name="callback">Delegate which will be called when the result of the ray cast is ready.</param>
        /// <returns>
        /// MLResult.Result will be <c>MLResult.Code.Ok</c> if successful.
        /// MLResult.Result will be <c>MLResult.Code.InvalidParam</c> if failed due to invalid input parameter.
        /// MLResult.Result will be <c>MLResult.Code.UnspecifiedFailure</c> if failed due to internal error.
        /// </returns>
        public static MLResult Raycast(QueryParams query, OnRaycastResultDelegate callback)
        {
            try
            {
                if (MLRaycast.IsValidInstance())
                {
                    if (query == null || callback == null)
                    {
                        MLPluginLog.ErrorFormat("MLRaycast.Raycast failed. Reason: Invalid input parameters.");
                        return(MLResult.Create(MLResult.Code.InvalidParam));
                    }

                    _instance.currentQuery.Position              = MLConvert.FromUnity(query.Position);
                    _instance.currentQuery.Direction             = MLConvert.FromUnity(query.Direction, true, false);
                    _instance.currentQuery.UpVector              = MLConvert.FromUnity(query.UpVector, true, false);
                    _instance.currentQuery.Width                 = query.Width;
                    _instance.currentQuery.Height                = query.Height;
                    _instance.currentQuery.HorizontalFovDegrees  = query.HorizontalFovDegrees;
                    _instance.currentQuery.CollideWithUnobserved = query.CollideWithUnobserved;

                    ulong         queryHandle = MagicLeapNativeBindings.InvalidHandle;
                    MLResult.Code resultCode  = NativeBindings.MLRaycastRequest(_instance.trackerHandle, ref _instance.currentQuery, ref queryHandle);
                    if (resultCode != MLResult.Code.Ok)
                    {
                        MLResult result = MLResult.Create(resultCode);
                        MLPluginLog.ErrorFormat("MLRaycast.Raycast failed to request a new ray cast. Reason: {0}", result);
                        return(result);
                    }

                    _instance.pendingQueries.Add(queryHandle, callback);
                    return(MLResult.Create(MLResult.Code.Ok));
                }
                else
                {
                    MLPluginLog.ErrorFormat("MLRaycast.Raycast failed. Reason: No Instance for MLRaycast");
                    return(MLResult.Create(MLResult.Code.UnspecifiedFailure, "MLRaycast.Raycast failed. Reason: No Instance for MLRaycast"));
                }
            }
            catch (System.EntryPointNotFoundException)
            {
                MLPluginLog.Error("MLRaycast.Raycast failed. Reason: API symbols not found");
                return(MLResult.Create(MLResult.Code.UnspecifiedFailure, "MLRaycast.Raycast failed. Reason: API symbols not found"));
            }
        }
Пример #3
0
        /// <summary>
        /// Requests a ray cast with the given query parameters.
        /// </summary>
        /// <param name="query">Query parameters describing ray being cast.</param>
        /// <param name="callback">Delegate which will be called when the result of the ray cast is ready.</param>
        /// <returns>
        /// MLResult.Result will be <c>MLResult.Code.Ok</c> if successful.
        /// MLResult.Result will be <c>MLResult.Code.InvalidParam</c> if failed due to invalid input parameter.
        /// MLResult.Result will be <c>MLResult.Code.UnspecifiedFailure</c> if failed due to internal error.
        /// </returns>
        public static MLResult Raycast(QueryParams query, OnRaycastResultDelegate callback)
        {
            try
            {
                if (MLRaycast.IsValidInstance())
                {
                    if (query == null || callback == null)
                    {
                        MLPluginLog.ErrorFormat("MLRaycast.Raycast failed. Reason: Invalid input parameters.");
                        return(MLResult.Create(MLResult.Code.InvalidParam));
                    }

                    bool RequestRaycast()
                    {
                        if (MLRaycast.IsValidInstance())
                        {
                            NativeBindings.MLRaycastQueryNative queryNative = new NativeBindings.MLRaycastQueryNative()
                            {
                                Position              = MLConvert.FromUnity(query.Position),
                                Direction             = MLConvert.FromUnity(query.Direction, true, false),
                                UpVector              = MLConvert.FromUnity(query.UpVector, true, false),
                                Width                 = query.Width,
                                Height                = query.Height,
                                HorizontalFovDegrees  = query.HorizontalFovDegrees,
                                CollideWithUnobserved = query.CollideWithUnobserved,
                            };

                            ulong requestHandle = MagicLeapNativeBindings.InvalidHandle;

                            MLResult.Code resultCode = NativeBindings.MLRaycastRequest(_instance.trackerHandle, ref queryNative, ref requestHandle);

                            if (resultCode != MLResult.Code.Ok)
                            {
                                MLPluginLog.ErrorFormat("MLRaycast.Raycast failed to request a new ray cast. Reason: {0}", MLResult.CodeToString(resultCode));
                                return(true);
                            }

                            if (requestHandle == MagicLeapNativeBindings.InvalidHandle)
                            {
                                MLPluginLog.Error("MLRaycast.Raycast failed to request a new ray cast. Reason: Request handle is invalid.");
                                return(true);
                            }

                            bool GetRaycastResults()
                            {
                                if (MLRaycast.IsValidInstance())
                                {
                                    NativeBindings.MLRaycastResultNative raycastResult = NativeBindings.MLRaycastResultNative.Create();

                                    resultCode = NativeBindings.MLRaycastGetResult(_instance.trackerHandle, requestHandle, ref raycastResult);
                                    if (resultCode == MLResult.Code.Pending)
                                    {
                                        return(false);
                                    }

                                    if (resultCode == MLResult.Code.Ok)
                                    {
                                        // Check if there is a valid hit result.
                                        bool didHit = raycastResult.State != ResultState.RequestFailed && raycastResult.State != ResultState.NoCollision;

                                        MLThreadDispatch.ScheduleMain(() =>
                                        {
                                            if (MLRaycast.IsValidInstance())
                                            {
                                                callback(
                                                    raycastResult.State,
                                                    didHit ? MLConvert.ToUnity(raycastResult.Hitpoint) : Vector3.zero,
                                                    didHit ? MLConvert.ToUnity(raycastResult.Normal, true, false) : Vector3.zero,
                                                    raycastResult.Confidence);
                                            }
                                            else
                                            {
                                                MLPluginLog.ErrorFormat("MLRaycast.Raycast failed. Reason: No Instance for MLRaycast");
                                            }
                                        });
                                    }
                                    else
                                    {
                                        MLPluginLog.ErrorFormat("MLRaycast.Raycast failed to get raycast result. Reason: {0}", MLResult.CodeToString(resultCode));
                                    }
                                }
                                else
                                {
                                    MLPluginLog.ErrorFormat("MLRaycast.Raycast failed. Reason: No Instance for MLRaycast");
                                }

                                return(true);
                            }

                            MLThreadDispatch.ScheduleWork(GetRaycastResults);
                        }
                        else
                        {
                            MLPluginLog.ErrorFormat("MLRaycast.Raycast failed. Reason: No Instance for MLRaycast");
                        }

                        return(true);
                    }

                    MLThreadDispatch.ScheduleWork(RequestRaycast);

                    return(MLResult.Create(MLResult.Code.Ok));
                }
                else
                {
                    MLPluginLog.ErrorFormat("MLRaycast.Raycast failed. Reason: No Instance for MLRaycast");
                    return(MLResult.Create(MLResult.Code.UnspecifiedFailure, "MLRaycast.Raycast failed. Reason: No Instance for MLRaycast"));
                }
            }
            catch (System.EntryPointNotFoundException)
            {
                MLPluginLog.Error("MLRaycast.Raycast failed. Reason: API symbols not found");
                return(MLResult.Create(MLResult.Code.UnspecifiedFailure, "MLRaycast.Raycast failed. Reason: API symbols not found"));
            }
        }
Пример #4
0
 /// <summary>
 /// Requests a ray cast with the given query parameters.
 /// </summary>
 /// <param name="query">Query parameters describing ray being cast.</param>
 /// <param name="callback">Delegate which will be called when the result of the ray cast is ready.</param>
 /// <returns>
 /// MLResult.Result will be <c>MLResult.Code.Ok</c> if successful.
 /// MLResult.Result will be <c>MLResult.Code.InvalidParam</c> if failed due to invalid input parameter.
 /// MLResult.Result will be <c>MLResult.Code.UnspecifiedFailure</c> if failed due to internal error.
 /// </returns>
 public static MLResult Raycast(QueryParams query, OnRaycastResultDelegate callback)
 {
     return(Instance.RaycastInternal(query, callback));
 }