/// <summary> /// Request the list of detected found objects. /// Callback will never be called while request is still pending. /// </summary> /// <param name="queryFilter">Filter used to customize query results.</param> /// <param name="callback"> /// Callback used to report query results. /// Callback MLResult code will never be <c>MLResult.Code.Pending</c>. /// </param> /// <returns> /// MLResult.Result inside callback will be <c>MLResult.Code.Ok</c> if successful. /// MLResult.Result inside callback will be <c>MLResult.Code.InvalidParam</c> if failed due to invalid input parameter. /// MLResult.Result inside callback will be <c>MLResult.Code.UnspecifiedFailure</c> if failed due to internal error. /// </returns> public static MLResult GetObjectsAsync(Query.Filter queryFilter, QueryResultsDelegate callback) { if (MLFoundObjects.IsValidInstance()) { // Don't allow null callbacks to be registered. if (callback == null) { MLPluginLog.Error("MLFoundObjects.GetObjects failed. Reason: Passed input callback is null."); return(MLResult.Create(MLResult.Code.InvalidParam)); } MLThreadDispatch.ScheduleWork(() => { _instance.BeginObjectQueryAsync(queryFilter, callback); return(true); }); return(MLResult.Create(MLResult.Code.Ok)); } else { MLPluginLog.ErrorFormat("MLFoundObjects.GetObjects failed. Reason: No Instance for MLFoundObjects"); return(MLResult.Create(MLResult.Code.UnspecifiedFailure, "MLFoundObjects.GetFoundObjects failed. Reason: No Instance for MLFoundObjects")); } }
/// <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")); } }
/// <summary> /// Begin querying for planes. /// </summary> /// <param name="queryParams">All values are required, omitting values may result in unexpected behavior.</param> /// <param name="callback">Callback used to report query results.</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> private MLResult BeginPlaneQuery(QueryParams queryParams, QueryResultsDelegate callback) { try { if (!NativeBindings.MLHandleIsValid(_instance.planesTracker)) { MLPluginLog.Error("MLPlanes.BeginPlaneQuery failed to request planes. Reason: Tracker handle is invalid"); return(MLResult.Create(MLResult.Code.InvalidParam)); } bool BeginQuery() { if (MLPlanes.IsValidInstance()) { NativeBindings.QueryParamsNative planeQuery = new NativeBindings.QueryParamsNative() { Data = queryParams }; ulong queryHandle = MagicLeapNativeBindings.InvalidHandle; MLResult.Code resultCode = NativeBindings.MLPlanesQueryBegin(_instance.planesTracker, ref planeQuery, ref queryHandle); if (resultCode != MLResult.Code.Ok) { MLPluginLog.ErrorFormat("MLPlanes.BeginPlaneQuery failed to request planes. Reason: {0}", MLResult.CodeToString(resultCode)); return(true); } // Create query object to prepresent this newly registered plane query. NativeBindings.Query query = new NativeBindings.Query((QueryResultsDelegate)callback, planeQuery.MaxResults, _instance.IsRequestingBoundaries(planeQuery.Flags)); bool GetPlanesResults() { if (MLPlanes.IsValidInstance()) { // Request the update. resultCode = NativeBindings.MLPlanesQueryGetResultsWithBoundaries(_instance.planesTracker, queryHandle, query.PlanesResultsUnmanaged, out uint numResults, ref query.PlaneBoundariesList); if (resultCode == MLResult.Code.Pending) { return(false); } if (resultCode == MLResult.Code.Ok) { query.ExtractPlanesFromQueryResults(numResults); resultCode = NativeBindings.MLPlanesReleaseBoundariesList(_instance.planesTracker, ref query.PlaneBoundariesList); if (resultCode == MLResult.Code.Ok) { query.Result = MLResult.Create(resultCode); if (query.Planes == null) { query.Planes = new Plane[] { }; } if (query.PlaneBoundaries == null) { query.PlaneBoundaries = new Boundaries[] { }; } MLThreadDispatch.ScheduleMain(() => { if (MLPlanes.IsValidInstance()) { callback(query.Result, query.Planes, query.PlaneBoundaries); } else { MLPluginLog.ErrorFormat("MLPlanes.BeginPlaneQuery failed. Reason: No Instance for MLPlanes"); } }); } else { MLPluginLog.ErrorFormat("MLPlanes.BeginPlaneQuery failed to release boundaries list. Reason: {0}", MLResult.CodeToString(resultCode)); } } else { MLPluginLog.ErrorFormat("MLPlanes.BeginPlaneQuery failed to query planes. Reason: {0}", MLResult.CodeToString(resultCode)); } } else { MLPluginLog.ErrorFormat("MLPlanes.BeginPlaneQuery failed. Reason: No Instance for MLPlanes"); } return(true); } MLThreadDispatch.ScheduleWork(GetPlanesResults); } else { MLPluginLog.ErrorFormat("MLPlanes.BeginPlaneQuery failed. Reason: No Instance for MLPlanes"); } return(true); } MLThreadDispatch.ScheduleWork(BeginQuery); return(MLResult.Create(MLResult.Code.Ok)); } catch (System.EntryPointNotFoundException) { MLPluginLog.Error("MLPlanes.BeginPlaneQuery failed. Reason: API symbols not found"); return(MLResult.Create(MLResult.Code.UnspecifiedFailure, "MLPlanes.BeginPlaneQuery failed. Reason: API symbols not found")); } }