// Update is called once per frame void Update() { Debug.DrawRay(m_transform.position, m_transform.position + m_transform.forward * 30); Ray eyeTracker = new Ray(m_transform.position, m_transform.forward); if (Physics.Raycast(eyeTracker, out m_hit, 30, m_layerMask)) { VRInteractiveItem item = m_hit.collider.GetComponent <VRInteractiveItem>(); m_vrInput.m_isGazeOnAnItem = true; m_currentInteractiveItem = item; if (item && item != m_lastInteractiveItem) { item.Over(); } if (item != m_lastInteractiveItem) { DesactivateLastItem(); } m_lastInteractiveItem = item; } else { DesactivateLastItem(); m_vrInput.m_isGazeOnAnItem = false; m_currentInteractiveItem = null; } }
private void EyeRaycast() { // Show the debug ray if required if (m_ShowDebugRay) { Debug.DrawRay(m_Camera.position, m_Camera.forward * m_DebugRayLength, Color.blue, m_DebugRayDuration); } // Create a ray that points forwards from the camera. Ray ray = new Ray(m_Camera.position, m_Camera.forward); RaycastHit hit; // Do the raycast forweards to see if we hit an interactive item if (Physics.Raycast(ray, out hit, m_RayLength, ~m_ExclusionLayers)) { VRInteractiveItem interactible = hit.collider.GetComponent <VRInteractiveItem>(); //attempt to get the VRInteractiveItem on the hit object m_CurrentInteractible = interactible; // If we hit an interactive item and it's not the same as the last interactive item, then call Over if (interactible && interactible != m_LastInteractible) { interactible.Over(); } // Deactive the last interactive item if (interactible != m_LastInteractible) { DeactiveLastInteractible(); } m_LastInteractible = interactible; // Something was hit, set at the hit position. if (m_Reticle) { m_Reticle.SetPosition(hit); } if (OnRaycasthit != null) { OnRaycasthit(hit); } } else { // Nothing was hit, deactive the last interactive item. DeactiveLastInteractible(); m_CurrentInteractible = null; // Position the reticle at default distance. if (m_Reticle) { m_Reticle.SetPosition(); } } }
// Update is called once per frame void Update() { //射线检测 Ray camera_ray = new Ray(m_Camera.position, m_Camera.forward); RaycastHit camera_hit; bool hitsomthing = Physics.Raycast(camera_ray, out camera_hit, Mathf.Infinity, m_ExclusionLayers); if (hitsomthing) { VRInteractiveItem interactible = camera_hit.collider.GetComponent <VRInteractiveItem>(); m_CurrentInteractible = interactible; // If we hit an interactive item and it's not the same as the last interactive item, then call Over if (interactible && interactible != m_LastInteractible) { interactible.Over(); } // Deactive the last interactive item if (interactible != m_LastInteractible) { DeactiveLastInteractible(); } m_LastInteractible = interactible; if (OnRaycasthit != null) { OnRaycasthit(camera_hit); } } else { DeactiveLastInteractible(); m_CurrentInteractible = null; } if (m_ShowDebugRay) { Debug.DrawRay(m_Camera.position, m_Camera.forward * 100, Color.blue); } }
private void EyeRaycast() { if (_ShowDebugRay) { Debug.DrawRay(Camera.main.transform.position, Camera.main.transform.forward * _RayLength, Color.blue); } Ray ray = new Ray(Camera.main.transform.position, Camera.main.transform.forward); RaycastHit hit; if (Physics.Raycast(ray, out hit, _RayLength, _detectLayerMask)) { _hitInfo.hit = hit; _hitInfo.hitTarget = hit.collider.gameObject; _hitInfo.hitPosition = hit.point; EventManager.instance.DispatchEvent(EventManager.instance, EventConfig.RETICLE_SETPOSITION, _hitInfo); VRInteractiveItem interactible = hit.collider.GetComponent <VRInteractiveItem>(); _CurrentInteractible = interactible; if (interactible && interactible != _LastInteractible) { interactible.Over(); } if (interactible != _LastInteractible) { DeactiveLastInteractible(); } _LastInteractible = interactible; } else { DeactiveLastInteractible(); _CurrentInteractible = null; EventManager.instance.DispatchEvent(EventManager.instance, EventConfig.RETICLE_SETPOSITION, null); } }
private void Raycast() { // Show the debug ray if required if (showDebugRay) { Debug.DrawRay(transform.position, forwardVector * rayLength, Color.blue); } // Create a ray that points forwards from the camera. Ray ray = new Ray(transform.position, forwardVector); RaycastHit hit; // Do the raycast forweards to see if we hit an interactive item if (Physics.Raycast(ray, out hit, rayLength, interactableObjectLayer)) //TODO exclode unused layers (~usedlayer) { VRInteractiveItem interactible = hit.collider.GetComponent <VRInteractiveItem>(); //attempt to get the VRInteractiveItem on the hit object currentInteractable = interactible; // If we hit an interactive item and it's not the same as the last interactive item, then call Over if (interactible && interactible != lastInteractable) { interactible.Over(); } // Deactive the last interactive item if (interactible != lastInteractable) { DeactiveLastInteractible(); } lastInteractable = interactible; // Something was hit, set at the hit position. //TODO more functions like click and so on } else { // Nothing was hit, deactive the last interactive item. DeactiveLastInteractible(); currentInteractable = null; } }
void Update() { Point3D gazeVec = GazeFrameCache.Instance.GetLastGazeVectorAvg(); Ray ray = new Ray(); if (Point3D.Zero != gazeVec) { ray = new Ray(_Camera.transform.position, _Camera.transform.rotation * gazeVec.ToVec3()); } else { // If no gaze vec, define ray based on 2D gaze coordinates Point2D gaze; if (!_UsingSmooth) { gaze = GazeFrameCache.Instance.GetLastRawGazeCoordinates(); } else { gaze = GazeFrameCache.Instance.GetLastSmoothedGazeCoordinates(); } if (Point2D.Zero != gaze) { Vector3 worldGaze = _Camera.ScreenToWorldPoint(new Vector3(gaze.X, _Camera.pixelHeight - gaze.Y, 10f)); Vector3 rayVec = worldGaze - _Camera.transform.position; rayVec.Normalize(); gazeVec = rayVec.ToPoint3D(); ray = new Ray(_Camera.transform.position, rayVec); } } if (Point3D.Zero != gazeVec) { Vector3 rayDirection = gazeVec.ToVec3(); // Show the debug ray if required if (_ShowDebugRay) { Debug.DrawRay(_Camera.transform.position, _Camera.transform.rotation * rayDirection * _DebugRayLength, Color.blue, _DebugRayDuration); } RaycastHit hit; // Do the raycast forweards to see if we hit an interactive item if (Physics.Raycast(ray, out hit, _RayLength, ~_ExclusionLayers)) { VRInteractiveItem interactible = hit.collider.GetComponent <VRInteractiveItem>(); //attempt to get the VRInteractiveItem on the hit object if (null != interactible && interactible.enabled && !_isToggleObject && Input.GetMouseButtonDown(0)) { UpdateToggle(); CurrentInteractible = interactible; SetObjectPosition(hit); // If we hit an interactive item and it's not the same as the last interactive item, then call Over if (interactible && interactible != _LastInteractible) { interactible.Over(); } // Deactive the last interactive item if (interactible != _LastInteractible) { DeactiveLastInteractible(); } _LastInteractible = interactible; } else if (_isToggleObject && Input.GetMouseButtonDown(0)) { // hit VRInteractiveItem is disabled, deactive the last interactive item. UpdateToggle(); SetObjectPosition(hit); DeactiveLastInteractible(); CurrentInteractible = null; } } else if (_isToggleObject && Input.GetMouseButtonDown(0)) { // Nothing was hit, deactive the last interactive item. SetObjectPosition(hit); UpdateToggle(); DeactiveLastInteractible(); CurrentInteractible = null; } // handle reticle position if (IsControllingReticle && _Reticle.enabled) { _Reticle.SetPosition(hit); SetObjectPosition(hit); } // If optional reticle set, position it if (null != _ReticleOptional && _ReticleOptional.gameObject.activeInHierarchy && _ReticleOptional.enabled) { _ReticleOptional.SetPosition(hit); SetObjectPosition(hit); } } else { // No gaze data, deactive the last interactive item. DeactiveLastInteractible(); CurrentInteractible = null; if (IsControllingReticle && _Reticle.enabled) { _Reticle.Hide(); } } }
private void HandRaycast() { // get raycast position and direction isLeftHandInteracting = (intetactionListener && leftWristTrans) ? intetactionListener.IsLeftHandInteracting() : false; isRightHandInteracting = (intetactionListener && rightWristTrans) ? intetactionListener.IsRightHandInteracting() : false; bool isInteracting = isLeftHandInteracting || isRightHandInteracting; isShooting = isLeftHandInteracting ? (makeFistToShoot ? (intetactionListener.GetLeftHandEvent() == InteractionManager.HandEventType.Grip) : !makeFistToShoot) : isRightHandInteracting ? (makeFistToShoot ? (intetactionListener.GetRightHandEvent() == InteractionManager.HandEventType.Grip) : !makeFistToShoot) : false; if (raycastSource) { raycastPos = raycastSource.position; raycastDir = raycastSource.forward; isInteracting = true; // eye interaction - always true if (!makeFistToShoot) { isShooting = true; } } else if (isInteracting) { // left or right arm raycastPos = isLeftHandInteracting ? leftWristTrans.position : rightWristTrans.position; raycastDir = isLeftHandInteracting ? (leftWristTrans.position - leftElbowTrans.position).normalized : (rightWristTrans.position - rightElbowTrans.position).normalized; } // // Show the debug ray if required // if (m_ShowDebugRay) // { // Debug.DrawRay(raycastPos, raycastDir * m_DebugRayLength, Color.blue, m_DebugRayDuration); // } if (infoText) { KinectDataClient dataClient = KinectDataClient.Instance; bool dataClientConnected = dataClient ? dataClient.IsConnected : false; if (dataClientConnected && intetactionListener) { if (isShooting) { if (raycastSource && !makeFistToShoot) { string sMessage = "Eyes shooting"; infoText.text = sMessage; } else if (isLeftHandInteracting || isRightHandInteracting) { string sMessage = (isLeftHandInteracting ? "Left" : "Right") + " hand shooting"; infoText.text = sMessage; } } else if (isLeftHandInteracting || isRightHandInteracting) { string sMessage = (isLeftHandInteracting ? "Left" : "Right") + (!raycastSource ? " hand pointing" : " hand ready"); infoText.text = sMessage; } else { string sMessage = !raycastSource ? "Point at snowflakes with your hand" : "Look at the snowflakes"; sMessage += (makeFistToShoot ? "\nClose your hand to shoot them" : "\nto shoot them"); infoText.text = sMessage; } } } // Create a ray that points forwards from the camera. bool raySuccess = false; if (isInteracting) { Ray ray = new Ray(raycastPos, raycastDir); //raySuccess = Physics.Raycast (ray, out raycastHit, m_RayLength, ~m_ExclusionLayers); raySuccess = Physics.SphereCast(ray, 0.5f, out raycastHit, m_RayLength, ~m_ExclusionLayers); } // Do the raycast forweards to see if we hit an interactive item if (raySuccess) { VRInteractiveItem interactible = raycastHit.collider.GetComponent <VRInteractiveItem>(); //attempt to get the VRInteractiveItem on the hit object m_CurrentInteractible = interactible; // If we hit an interactive item and it's not the same as the last interactive item, then call Over if (interactible && interactible != m_LastInteractible) { interactible.Over(); } // Deactive the last interactive item if (interactible != m_LastInteractible) { DeactiveLastInteractible(); if (interactible && isShooting) { // instantiate the laser beam if (laserPrefab) { laser = Instantiate(laserPrefab) as LineRenderer; laser.transform.parent = transform; Vector3 laserStartPos = isLeftHandInteracting ? leftWristTrans.position : rightWristTrans.position; if (raycastSource && !makeFistToShoot) { laserStartPos = raycastPos; } laser.SetPosition(0, laserStartPos); laser.SetPosition(1, raycastHit.point); } interactible.Click(); } } m_LastInteractible = interactible; // Something was hit, set at the hit position. if (m_Reticle) { m_Reticle.SetPosition(raycastHit); } if (OnRaycasthit != null) { OnRaycasthit(raycastHit); } } else { // Nothing was hit, deactive the last interactive item. DeactiveLastInteractible(); m_CurrentInteractible = null; // Position the reticle at default distance. if (m_Reticle) { m_Reticle.SetPosition(raycastPos, raycastDir); } } }
private void EyeRaycast() { Debug.Log("eye1"); // Show the debug ray if required if (m_ShowDebugRay) { Debug.DrawRay(m_Camera.position, m_Camera.forward * m_DebugRayLength, Color.blue, m_DebugRayDuration); } // Create a ray that points forwards from the camera. Ray ray = new Ray(m_Camera.position, m_Camera.forward); RaycastHit hit; Vector3 worldStartPoint = Vector3.zero; Vector3 worldEndPoint = Vector3.zero; if (m_LineRenderer != null) { m_LineRenderer.enabled = ControllerIsConnected && ShowLineRenderer; } Debug.Log("eye2"); if (ControllerIsConnected && m_TrackingSpace != null) { Matrix4x4 localToWorld = m_TrackingSpace.localToWorldMatrix; Quaternion orientation = OVRInput.GetLocalControllerRotation(Controller); Vector3 localStartPoint = OVRInput.GetLocalControllerPosition(Controller); Vector3 localEndPoint = localStartPoint + ((orientation * Vector3.forward) * 500.0f); worldStartPoint = localToWorld.MultiplyPoint(localStartPoint); worldEndPoint = localToWorld.MultiplyPoint(localEndPoint); Debug.Log("Update worldEndPoint"); // Create new ray ray = new Ray(worldStartPoint, worldEndPoint - worldStartPoint); } Debug.Log("eye3"); // Update selected object if (m_SelectedInteractible && m_SelectedInteractible.menuItem == false) { m_SelectedInteractible.gameObject.transform.position = worldStartPoint + ray.direction; m_SelectedInteractible.gameObject.transform.rotation = OVRInput.GetLocalControllerRotation(OVRInput.Controller.RTrackedRemote); } // Put selected object back if (OVRInput.GetUp(OVRInput.Button.PrimaryIndexTrigger) && m_SelectedInteractible && m_SelectedInteractible.menuItem == false) { m_SelectedInteractible.transform.position = m_SelectedInteractibleOriginalPosition; m_SelectedInteractible = null; } Debug.Log("eye4"); // Do the raycast forweards to see if we hit an interactive item if (Physics.Raycast(ray, out hit, m_RayLength, ~m_ExclusionLayers)) { VRInteractiveItem interactible = hit.collider.GetComponent <VRInteractiveItem>(); //attempt to get the VRInteractiveItem on the hit object if (interactible) { if (m_SelectedInteractible == null && OVRInput.GetUp(OVRInput.Button.PrimaryIndexTrigger)) { m_SelectedInteractible = interactible; m_SelectedInteractibleOriginalPosition = m_SelectedInteractible.gameObject.transform.position; Material material = new Material(Shader.Find("Diffuse")); material.mainTexture = m_SelectedInteractible.TheoryTexture; m_SelectedInteractible.TheoryScreen.GetComponent <Renderer>().material = material; } worldEndPoint = hit.point; Debug.Log("Update worldEndPoint hit"); } m_CurrentInteractible = interactible; Debug.Log("eye5"); // If we hit an interactive item and it's not the same as the last interactive item, then call Over if (interactible && interactible != m_LastInteractible) { interactible.Over(); } // Deactive the last interactive item if (interactible != m_LastInteractible) { DeactiveLastInteractible(); } m_LastInteractible = interactible; // Something was hit, set at the hit position. if (m_Reticle) { m_Reticle.SetPosition(hit); } Debug.Log("eye6"); if (OnRaycasthit != null) { OnRaycasthit(hit); } } else { // Nothing was hit, deactive the last interactive item. DeactiveLastInteractible(); m_CurrentInteractible = null; // Position the reticle at default distance. if (m_Reticle) { m_Reticle.SetPosition(ray.origin, ray.direction); } } if (ControllerIsConnected && m_LineRenderer != null) { Debug.Log("eye" + worldEndPoint.x + " " + worldEndPoint.y + " " + worldEndPoint.z); m_LineRenderer.SetPosition(0, worldStartPoint); m_LineRenderer.SetPosition(1, worldEndPoint); } Debug.Log("eye7"); }