protected void _RaySelection() { // Ray picking Vector3 rayOrigin = transform.position; Vector3 rayDirection = transform.TransformDirection(Vector3.forward); VRSelection newSelection = null; foreach (RaycastHit raycastHit in Physics.RaycastAll(rayOrigin, rayDirection, m_Wand.GetDefaultRayLength())) { if (newSelection != null && raycastHit.distance >= newSelection.SelectionDistance) { continue; } GameObject objectHit = raycastHit.collider.gameObject; if (objectHit.name != "VRWand") { // Ignore GameObject without the VRActor component if (objectHit.GetComponent <VRActor>() == null) { continue; } VRWebView webView = objectHit.GetComponent <VRWebView>(); VRRaycastHit completeHit = null; if (webView != null) { completeHit = webView.RaycastMesh(rayOrigin, rayDirection); } else { completeHit = new VRRaycastHit(raycastHit); } if (completeHit != null) { // Special case : pass through transparent pixels of web views. if (webView != null) { if (!webView.GetComponent <Renderer>().enabled || webView.IsPixelEmpty(completeHit.textureCoord)) { continue; } } // Create selection if it does not exist if (newSelection == null) { newSelection = new VRSelection(); } newSelection.SourceWand = m_Wand; newSelection.SelectedObject = objectHit; newSelection.TextureCoordinate = completeHit.textureCoord; newSelection.SelectionDistance = completeHit.distance; newSelection.SelectionContact = completeHit.point; newSelection.SelectionNormal = completeHit.normal; } } } m_LastSelection = m_SelectionMgr.GetSelection(); m_SelectionMgr.SetSelection(newSelection); }
private void _RaySelection() { // Ray picking RaycastHit[] hits; Vector3 dir = transform.localToWorldMatrix * Vector3.forward; hits = Physics.RaycastAll(transform.position, dir, m_Wand.GetDefaultRayLength()); bool foundActor = false; int currentHitId = 0; int foundHitId = 0; float distance = Mathf.Infinity; while (currentHitId < hits.Length) { RaycastHit hit = hits[currentHitId]; if (hit.distance < distance && hit.collider.name != "VRWand") { if (hit.collider.GetComponent <VRActor>() == null) { currentHitId++; continue; } // Pass through empty/transparent GUI pixels VRWebView webView = hit.collider.GetComponent <VRWebView>(); if (webView != null) { if (!webView.GetComponent <Renderer>().enabled || webView.IsPixelEmpty(hit.textureCoord)) { currentHitId++; continue; } } foundActor = true; foundHitId = currentHitId; distance = hit.distance; } currentHitId++; } m_LastSelection = m_SelectionMgr.GetSelection(); // If something found, select if (foundActor) { RaycastHit selectionHit = hits[foundHitId]; VRSelection newSelection = new VRSelection(); newSelection.SourceWand = m_Wand; newSelection.SelectedObject = selectionHit.collider.gameObject; newSelection.TextureCoordinate = selectionHit.textureCoord; newSelection.SelectionDistance = selectionHit.distance; newSelection.SelectionContact = selectionHit.point; newSelection.SelectionNormal = selectionHit.normal; m_SelectionMgr.SetSelection(newSelection); } else { m_SelectionMgr.SetSelection(null); } }