Пример #1
0
        /// <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);
            }
        }
        /// <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();
        }
Пример #3
0
    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 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;
 }