/// <summary> /// Aplies the correct object interactions based on the raycast results. /// </summary> /// <param name="state"> The state of the raycast result.</param> /// <param name="result"> Contains the info on the result of the raycast</param> public void OnRaycastHit(MLRaycast.ResultState state, RaycastHit result, float confidence) { // Detect if raycast hit same object, new object or none if (_lastHit != result.transform || _lastHit == null) { if (_lastHit != null) { if (_triggerPressed) { InteractionRelease(_lastPressed); _lastPressed = null; _triggerPreviousPressed = false; } InteractionEnd(_lastHit); } _lastHit = result.transform; if (_lastHit != null) { InteractionBegin(_lastHit); } else { return; } } HandleInput(); }
/// <summary> /// Callback handler called when raycast has a result. /// Updates the transform an color on the Hit Position and Normal from the assigned object. /// </summary> /// <param name="state"> The state of the raycast result.</param> /// <param name="mode">The mode that the raycast was in (physical, virtual, or combination).</param> /// <param name="ray">A ray that contains the used direction and origin for this raycast.</param> /// <param name="result">The hit results (point, normal, distance).</param> /// <param name="confidence">Confidence value of hit. 0 no hit, 1 sure hit.</param> public void OnRaycastHit(MLRaycast.ResultState state, MLRaycastBehavior.Mode mode, Ray ray, RaycastHit result, float confidence) { if (state != MLRaycast.ResultState.RequestFailed && state != MLRaycast.ResultState.NoCollision) { gameObject.SetActive(true); // Update the cursor position and normal. transform.position = result.point; transform.LookAt(result.normal + result.point, ray.direction); transform.localScale = Vector3.one; // Set the color to yellow if the hit is unobserved. _render.material.color = (state == MLRaycast.ResultState.HitObserved) ? _color : Color.yellow; if (_scaleWhenClose) { // Check the hit distance. if (result.distance < 1.0f) { // Apply a downward scale to the cursor. transform.localScale = new Vector3(result.distance, result.distance, result.distance); } } } else { gameObject.SetActive(false); } }
// Use a callback to know when to run the NormalMaker() coroutine. void HandleOnReceiveRaycast(MLRaycast.ResultState state, UnityEngine.Vector3 point, Vector3 normal, float confidence) { /* * if (state == MLRaycast.ResultState.) * { * // StartCoroutine(NormalMarker(point, normal)); * }*/ }
/// <summary> /// Sets _raycastResult based on results from callback function HandleOnReceiveRaycast. /// </summary> /// <param name="state"> The state of the raycast result.</param> /// <param name="point"> Position of the hit.</param> /// <param name="normal"> Normal of the surface hit.</param> /// <returns></returns> protected void SetWorldRaycastResult(MLRaycast.ResultState state, Vector3 point, Vector3 normal) { _raycastResult = new RaycastHit(); if (state != MLRaycast.ResultState.RequestFailed && state != MLRaycast.ResultState.NoCollision) { _raycastResult.point = point; _raycastResult.normal = normal; #if PLATFORM_LUMIN _raycastResult.distance = Vector3.Distance(_raycastParams.Position, point); #endif } }
/// <summary> /// Detects the type of surface the cursor is at. /// </summary> private void OnRaycastResult(MLRaycast.ResultState state, MLRaycastBehavior.Mode mode, Ray ray, RaycastHit hit, float confidence) { if (_firstPlacement == true) { return; } else { _isValid = true; float dot = Vector3.Dot(-Cursor.transform.forward, Vector3.up); //are we oriented like a table/floor or a wall? if (dot > .5f || dot < -.5f) { if (dot < 0) { _type = SurfaceType.Floor; } else if (dot > 0) { _type = SurfaceType.Ceiling; } } else { _type = SurfaceType.Wall; } } if (StatusLabel != null) { StatusLabel.text = "Valid Plane " + _type.ToString(); StatusLabel.color = Color.green; } if (StatusLabel != null) { StatusLabel.text = "Invalid Location"; StatusLabel.color = Color.red; } UpdatePad(); UpdateObject(); }
public void OnRaycastHit(MLRaycast.ResultState state, MLRaycastBehavior.Mode mode, Ray ray, RaycastHit result, float confidence) { if (confidence < 0.1f) { //no hit raycastHitRenderer.enabled = false; raycastHit = false; raycastLine.SetPosition(0, ray.origin); raycastLine.SetPosition(1, ray.origin + transform.forward); } else { if (Mathf.Abs(result.normal.y) > 0.05f) { //it is not a wall raycastHitRenderer.enabled = true; raycastHit = false; raycastLine.SetPosition(0, ray.origin); raycastLine.SetPosition(1, result.point); raycastHitObject.position = result.point; raycastHitObject.rotation = Quaternion.LookRotation(result.normal, Vector3.up); raycastHitRenderer.material.SetColor("_EmissionColor", Color.red); } else { //hit on wall raycastHitRenderer.enabled = true; raycastHit = true; raycastLine.SetPosition(0, ray.origin); raycastLine.SetPosition(1, result.point); raycastHitObject.position = result.point; raycastHitObject.rotation = Quaternion.LookRotation(result.normal, Vector3.up); raycastHitRenderer.material.SetColor("_EmissionColor", Color.green); } } }
/// <summary> /// Callback handler called when raycast call has a result. /// </summary> /// <param name="state"> The state of the raycast result.</param> /// <param name="point"> Position of the hit.</param> /// <param name="normal"> Normal of the surface hit.</param> /// <param name="confidence"> Confidence value on hit.</param> protected void HandleOnReceiveRaycast(MLRaycast.ResultState state, Vector3 point, Vector3 normal, float confidence) { if (state == MLRaycast.ResultState.HitObserved || state == MLRaycast.ResultState.HitUnobserved) { if (_modeOnWorldRaycast == Mode.World) { SetWorldRaycastResult(state, point, normal); OnRaycastResult?.Invoke(state, Mode.World, _ray, _raycastResult, confidence); } if (_modeOnWorldRaycast == Mode.Combination) { // If there was a hit on world raycast, change max distance to the hitpoint distance float maxDist = (_raycastResult.distance > 0.0f) ? (_raycastResult.distance + WorldRayProperties.bias) : virtualRayProperties.distance; CastVirtualRay(maxDist); } } _isReady = true; }
public void OnRaycastResult(MLRaycast.ResultState state, Vector3 hitpoint, Vector3 normal, float confidence) { if (MLRaycast.IsStarted) { switch (state) { case MLRaycast.ResultState.RequestFailed: // Do nothing break; case MLRaycast.ResultState.HitUnobserved: break; case MLRaycast.ResultState.NoCollision: // World Mesh miss mlRaycastHitNormal = -(transform.forward); mlRaycastHitPoint = transform.position + (transform.forward * 10.0f); this.actsAsControllerRay.startColor = Color.red; this.actsAsControllerRay.endColor = Color.red; break; case MLRaycast.ResultState.HitObserved: // World Mesh Hit this.actsAsControllerRay.startColor = Color.green; this.actsAsControllerRay.endColor = Color.green; mlRaycastHitNormal = normal; mlRaycastHitPoint = hitpoint; break; default: break; } queryParams.Position = transform.position; queryParams.UpVector = transform.up; queryParams.Direction = transform.forward; MLRaycast.Raycast(queryParams, OnRaycastResult); } }
/// <summary> /// Callback handler called when raycast has a result. /// Updates the confidence value to the new confidence value. /// </summary> /// <param name="state"> The state of the raycast result.</param> /// <param name="mode">The mode that the raycast was in (physical, virtual, or combination).</param> /// <param name="ray">A ray that contains the used direction and origin for this raycast.</param> /// <param name="result">The hit results (point, normal, distance).</param> /// <param name="confidence">Confidence value of hit. 0 no hit, 1 sure hit.</param> public void OnRaycastHit(MLRaycast.ResultState state, MLRaycastBehavior.Mode mode, Ray ray, RaycastHit result, float confidence) { _confidence = confidence; }