void RefreshTracking() { RefreshTrackingList(); if (indicatorPrefab == null) { return; } realTrackDistance = trackStartDistance / 1000f; int colliderMask = 0x1 << 11; // Find hand float minimalDistance = 9999f; int minimalObject = -1; Collider[] lcolliders = new Collider[1]; Collider[] rcolliders = new Collider[1]; if (handLT != null) { lcolliders = Physics.OverlapSphere(handLT.position, realTrackDistance, colliderMask); for (int i = 0; i < lcolliders.Length; ++i) { // Check if it already has an indicator if (lcolliders[i].tag == "InteractableObj") { if (m_trackingObjects.ContainsKey(lcolliders[i])) { m_trackingObjects[lcolliders[i]].RefreshActiveTime(); } else { BindIndicatorToObject(lcolliders[i]); } } // Single Sound test. /* * float minDistance = Vector3.SqrMagnitude(lcolliders[i].transform.position - handLT.position); * if (minDistance < minimalDistance) { * minimalDistance = minDistance; * minimalObject = -(i + 1); // To avoid 0 * }*/ // cylinder sound test Vector3 v = handLT.position - Camera.main.transform.position; Vector3 u = lcolliders[i].transform.position - Camera.main.transform.position; float ulength = u.magnitude; float uvdot = Vector3.Dot(u, v); // if the hand is behind camera or the object is behind the camera, ignore it if (uvdot > 0 && Vector3.Dot(v, Camera.main.transform.forward) > 0) { float cos_theta = uvdot / (v.magnitude * ulength); // use cos^2 + sin^2 = 1 and use ucos <= R is equal to u^2 cos^2 <= R^2. // so that we avoid using arccos and sin to calculate triangular functions. float sin_theta2 = 1.0f - cos_theta * cos_theta; if (ulength * ulength * sin_theta2 <= soundCylinderRadius * soundCylinderRadius && (u - v).magnitude < minimalDistance) { minimalDistance = (u - v).magnitude; minimalObject = -(i + 1); } } } } if (handRT != null) { rcolliders = Physics.OverlapSphere(handRT.position, realTrackDistance, colliderMask); for (int i = 0; i < rcolliders.Length; ++i) { // Check if it already has an indicator if (rcolliders[i].tag == "InteractableObj") { if (m_trackingObjects.ContainsKey(rcolliders[i])) { m_trackingObjects[rcolliders[i]].RefreshActiveTime(); } else { BindIndicatorToObject(rcolliders[i]); } } // Single Sound Test /* * float minDistance = Vector3.SqrMagnitude(rcolliders[i].transform.position - handRT.position); * if (minDistance < minimalDistance) { * minimalDistance = minDistance; * minimalObject = i + 1; // To avoid 0 * }*/ // cylinder sound test Vector3 v = handRT.position - Camera.main.transform.position; Vector3 u = rcolliders[i].transform.position - Camera.main.transform.position; float ulength = u.magnitude; float uvdot = Vector3.Dot(u, v); // if the hand is behind camera or the object is behind the camera, ignore it if (uvdot > 0 && Vector3.Dot(v, Camera.main.transform.forward) > 0) { float cos_theta = uvdot / (v.magnitude * ulength); // use cos^2 + sin^2 = 1 and use ucos <= R is equal to u^2 cos^2 <= R^2. // so that we avoid using arccos and sin to calculate triangular functions. float sin_theta2 = 1.0f - cos_theta * cos_theta; if (ulength * ulength * sin_theta2 <= soundCylinderRadius * soundCylinderRadius && (u - v).magnitude < minimalDistance) { minimalDistance = (u - v).magnitude; minimalObject = (i + 1); } } } } if (m_testSingleSound && minimalDistance != 9999f) { //Debug.Log("Single sound enabled"); IDistanceIndicator newIndicator = null; if (minimalObject < 0) { newIndicator = m_trackingObjects[lcolliders[-minimalObject - 1]]; } else { newIndicator = m_trackingObjects[rcolliders[minimalObject - 1]]; } if (m_lastSingleSound != null && m_lastSingleSound != newIndicator) { setSoundPlayForIndicator(m_lastSingleSound, false); } m_lastSingleSound = newIndicator; setSoundPlayForIndicator(newIndicator, true); // Debug.Log("New Indicator:" + newIndicator.transform.parent.name); /* * if (newIndicator is DistanceIndicator) { * DistanceIndicator newSLIndicator = (DistanceIndicator)newIndicator; * if (m_lastSingleSound != null && m_lastSingleSound != newIndicator) { * m_lastSingleSound.notifySound.gameObject.SetActive(false); * m_lastSingleSound = newSLIndicator; * m_lastSingleSound.notifySound.gameObject.SetActive(true); * } * else if (m_lastSingleSound == null) { * m_lastSingleSound = newSLIndicator; * m_lastSingleSound.notifySound.gameObject.SetActive(true); * } * }*/ } else if (m_lastSingleSound != null) { setSoundPlayForIndicator(m_lastSingleSound, false); m_lastSingleSound = null; } // Text Update if (screenText != null) { float thresholdDis = 0.3f * realTrackDistance; if (m_trackingObjects.Count == 0) { screenText.setText("Move Forward"); } else if (minimalDistance >= thresholdDis * thresholdDis) { // In this way because minimalDistance is sqrt magnitude screenText.setText("Keep Going"); } else { screenText.setText(""); } } }