Esempio n. 1
0
    /// <summary>
    /// Perform collision detection and sort the list of contacts.
    /// </summary>
    public void update()
    {
        ZSLinearShape shape = _stylusSelector.activeStylus as ZSLinearShape;
        //float maxLength = (shape != null) ? _core.GetWorldScale() * shape._defaultLength : Mathf.Infinity;
        float maxLength = (shape != null) ? _core.GetViewerScale() * shape._defaultLength : Mathf.Infinity;

        float distance   = Vector3.Distance(_stylusSelector.transform.position, _stylusSelector.activeStylus.hotSpot);
        float castLength = (Mathf.Approximately(distance, 0.0f)) ? maxLength : distance;

        //TODO: Sweeps/casts don't notice initial contacts.  And OnCollisionEnter doesn't work between a plain Collider and a Rigidbody.
        _hitInfos = Physics.RaycastAll(_stylusSelector.transform.position, _stylusSelector.transform.forward, castLength, _layerMask);
        Array.Sort <RaycastHit> (_hitInfos, this);
    }
Esempio n. 2
0
    /// <summary>
    /// This function is called by ZSCore after each input update.
    /// </summary>
    private void OnCoreUpdated(ZSCore sender)
    {
        if (sender != _zsCore)
        {
            return;
        }

        //Update stylus button states.
        for (int i = 0; i < numButtons; ++i)
        {
            _wasStylusButtonPressed [i] = _isStylusButtonPressed [i];
            _wasMouseButtonPressed [i]  = _isMouseButtonPressed [i];

            //Have to combine mouse state down here so asynchronous clients see it at the right time.
            _isStylusButtonPressed [i] = !_zsCore.IsMouseEmulationEnabled() && _zsCore.IsTrackerTargetButtonPressed(ZSCore.TrackerTargetType.Primary, i);
            _isMouseButtonPressed [i]  = Input.GetMouseButton(i);
        }

        bool useMousePointer = false;

        if (_useTracking && !_zsCore.IsMouseEmulationEnabled())
        {
            Matrix4x4 pose = _zsCore.GetTrackerTargetWorldPose(_targetType);
            if (pose != _previousStylusPose)
            {
                _timeSinceStylusMoved = 0f;
            }
            else
            {
                _timeSinceStylusMoved += Time.deltaTime;
            }

            useMousePointer = _timeSinceStylusMoved > StylusTimeout;
            if (!useMousePointer)
            {
                _previousStylusPose  = pose;
                transform.localScale = ZSStylusSelector.GetScale(pose);
                transform.rotation   = ZSStylusSelector.GetRotation(pose);
                transform.position   = ZSStylusSelector.GetPosition(pose);

                transform.localScale = _zsCore.GetViewerScale() * Vector3.one;
            }
        }

        //Simulate the stylus based on mouse input.

        Vector3 dMousePosition = Input.mousePosition - _lastMousePosition;

        dMousePosition [2] = WheelSensitivity * Input.GetAxis("Mouse ScrollWheel");
        if (Input.GetKey(KeyCode.LeftBracket))
        {
            dMousePosition [2] -= WheelSensitivity * Time.deltaTime;
        }
        if (Input.GetKey(KeyCode.RightBracket))
        {
            dMousePosition [2] += WheelSensitivity * Time.deltaTime;
        }

        Camera mainCamera = (_zsCore.IsStereoEnabled()) ? _centerCamera : _zsCore.CurrentCamera.camera;

        if (useMousePointer && mainCamera != null && mainCamera.enabled)
        {
            if (stylusSimulatorMode == StylusSimulatorMode.Projection || stylusSimulatorMode == StylusSimulatorMode.Position)
            {
                //Only update the wheel total if we aren't rotating.  Avoids extra Z translation artifact.
                _mouseWheel += dMousePosition [2];
                Ray     ray      = mainCamera.ScreenPointToRay(Input.mousePosition);
                Vector3 rayPoint = ray.GetPoint(0.1f + 0.5f * _mouseWheel * mainCamera.transform.localScale.magnitude);
                transform.position = rayPoint;

                if (stylusSimulatorMode == StylusSimulatorMode.Projection)
                {
                    transform.rotation = Quaternion.LookRotation(ray.GetPoint(1.0f) - mainCamera.transform.position, mainCamera.transform.up);
                }
            }
            else if (stylusSimulatorMode == StylusSimulatorMode.Rotation)
            {
                Vector3 euler = transform.localRotation.eulerAngles;
                euler += new Vector3(-0.1f * dMousePosition.y, 0.1f * dMousePosition.x, -1000.0f * dMousePosition.z);
                var oldHoverPoint = transform.TransformPoint(_localHoverPoint);
                transform.localRotation  = Quaternion.Euler(euler);
                transform.localPosition += oldHoverPoint - transform.TransformPoint(_localHoverPoint);
            }
        }

        //Make the hovered object the closest one to the tip.

        if (_useCollision)
        {
            _hoverQueue.update();
            RaycastHit hit = _hoverQueue.getFirst();
            if (hit.collider != null)
            {
                HoverPoint  = hit.point;
                HoverObject = (hit.collider.gameObject.layer == uiLayer) ?
                              hit.collider.gameObject :
                              objectResolver(hit.collider.gameObject);
            }
            else
            {
                ZSLinearShape linearStylus = activeStylus as ZSLinearShape;
                if (linearStylus != null && linearStylus._tip != null && linearStylus._tip.activeSelf)
                {
                    HoverPoint = linearStylus._tip.transform.position;
                }
                else
                {
                    HoverPoint = activeStylus.hotSpot;
                }
                HoverObject = null;
            }
        }
        else
        {
            HoverPoint = transform.TransformPoint(_localHoverPoint);
        }

        if (_HoverObject != null)
        {
            activeStylus.OnHoverStay(_HoverObject, HoverPoint);
        }

        //Update the set of selected objects based on clicking.

        if (GetButtonDown(SelectButton))
        {
            if (!Input.GetKey(KeyCode.LeftControl))
            {
                selectedObjects.Clear();
            }

            if (_HoverObject != null)
            {
                if (_HoverObject.layer == uiLayer)
                {
                    _wasHoveredObjectSelected = false;
                }
                else
                {
                    _wasHoveredObjectSelected = selectedObjects.Contains(_HoverObject);
                    if (!selectedObjects.Contains(_HoverObject))
                    {
                        selectedObjects.Add(_HoverObject);
                    }
                }
            }

            _buttonDownPosition = transform.position;
        }

        if (GetButtonUp(SelectButton))
        {
            bool wasDrag = (transform.position - _buttonDownPosition).magnitude > minDragDistance;
            if (_HoverObject != null && _wasHoveredObjectSelected && !wasDrag && Input.GetKey(KeyCode.LeftControl))
            {
                selectedObjects.Remove(_HoverObject);
            }
        }

        // Send messages to objects whose selection state changed.

        foreach (GameObject selectedObject in _oldSelectedObjects)
        {
            if (selectedObject != null && !selectedObjects.Contains(selectedObject))
            {
                activeStylus.OnDeselected(selectedObject);
                selectedObject.SendMessage("OnDeselected", SendMessageOptions.DontRequireReceiver);
            }
        }

        GameObject[] tmpSelectedObjects = new GameObject[selectedObjects.Count];
        selectedObjects.CopyTo(tmpSelectedObjects);                          // So objects can de-select themselves.
        foreach (GameObject selectedObject in tmpSelectedObjects)
        {
            if (selectedObject != null && !_oldSelectedObjects.Contains(selectedObject))
            {
                selectedObject.SendMessage("OnSelected", SendMessageOptions.DontRequireReceiver);
                activeStylus.OnSelected(selectedObject, HoverPoint);
            }
        }

        _oldSelectedObjects.Clear();
        foreach (GameObject selectedObject in selectedObjects)
        {
            if (selectedObject != null)
            {
                _oldSelectedObjects.Add(selectedObject);
            }
        }

        _lastMousePosition = Input.mousePosition;

        activeStylus.Tool.OnStylus();
    }