2,在unity工程中导入相应的package
Vive-SRanipal-Unity-Plugin.unitypackage
https://download.csdn.net/download/moonlightpeng/11237918
3, 标定
方案1 在没有运行工程时,直接在头盔中通过手柄来标定。
方案2 用unitypackage里面的标定功能。
4, 在工程中用红色的小球表示眼睛的注视点,把自动的紫色的射线不显示。
直接把Line Renderer不要勾选。
//========= Copyright 2018, HTC Corporation. All rights reserved. =========== using UnityEngine; using UnityEngine.Assertions; namespace ViveSR { namespace anipal { namespace Eye { public class SRanipal_GazeRaySample : MonoBehaviour { private GameObject gazePointer; public int LengthOfRay = 25; [SerializeField] private LineRenderer GazeRayRenderer; private void Start() { gazePointer = new GameObject(); gazePointer = GameObject.Find("GazePointer"); if (gazePointer) { gazePointer.GetComponent<MeshRenderer>().enabled = false; } else { Debug.LogError("cannot find object of gazePointer"); } if (!SRanipal_Eye_Framework.Instance.EnableEye) { enabled = false; return; } Assert.IsNotNull(GazeRayRenderer); } private void Update() { if (SRanipal_Eye_Framework.Status != SRanipal_Eye_Framework.FrameworkStatus.WORKING && SRanipal_Eye_Framework.Status != SRanipal_Eye_Framework.FrameworkStatus.NOT_SUPPORT) return; Vector3 GazeOriginCombinedLocal, GazeDirectionCombinedLocal; if (SRanipal_Eye.GetGazeRay(GazeIndex.COMBINE, out GazeOriginCombinedLocal, out GazeDirectionCombinedLocal)) { } else if (SRanipal_Eye.GetGazeRay(GazeIndex.LEFT, out GazeOriginCombinedLocal, out GazeDirectionCombinedLocal)) { } else if (SRanipal_Eye.GetGazeRay(GazeIndex.RIGHT, out GazeOriginCombinedLocal, out GazeDirectionCombinedLocal)) { } else return; Vector3 GazeDirectionCombined = Camera.main.transform.TransformDirection(GazeDirectionCombinedLocal); GazeRayRenderer.SetPosition(0, Camera.main.transform.position - Camera.main.transform.up * 0.05f); GazeRayRenderer.SetPosition(1, Camera.main.transform.position + GazeDirectionCombined * LengthOfRay); Ray ray = new Ray(Camera.main.transform.position - Camera.main.transform.up * 0.05f, GazeDirectionCombined); Debug.DrawLine(Camera.main.transform.position - Camera.main.transform.up * 0.05f, Camera.main.transform.position + GazeDirectionCombined * LengthOfRay, Color.red); //这个就是绘制出的射线了,包含发射位置,发射距离和射线的颜色; RaycastHit hitInfo; if (Physics.Raycast(ray, out hitInfo, 100)) { DrawLineInGame(hitInfo.point); GameObject hitme = hitInfo.collider.gameObject; } else { gazePointer.GetComponent<MeshRenderer>().enabled = false; } } void DrawLineInGame(Vector3 point) { //render the position of sphere gazePointer.GetComponent<MeshRenderer>().enabled = true; gazePointer.transform.position = point; } } } } }
文章来源: https://blog.csdn.net/moonlightpeng/article/details/91572812