《SteamVR2.0/Extras》(Yanlz+Unity+SteamVR+Extras+OpenVR+GazeTracker+立钻哥哥+==)

 

《SteamVR/Extras》

版本

作者

参与者

完成日期

备注

SteamVR_Extras_V01_1.0

严立钻

 

2019.01.18

 

 

 

 

 

 

 

 

##《SteamVR/Extras》发布说明:

++++“SteamVR/Extras”:是“从SteamVR目录结构出发”系列的“SteamVR/Extras”;(2019年度的重点技术突破点确立为:“SteamVR”,所以对SteamVR脚本功能的熟悉是非常关键的!)

++++“SteamVR/Extras”:定位在一个代码剖析,以期能熟悉SteamVR框架

++++OpenVR的Github:https://github.com/ValveSoftware/openvr

++++OpenXR官网:https://www.khronos.org/openxr

++++SteamVR的Github:https://github.com/ValveSoftware/steamvr_unity_plugin

 

 

##《SteamVR/Extras》目录

《SteamVR2.0/Extras》(Yanlz+Unity+SteamVR+Extras+OpenVR+GazeTracker+立钻哥哥+==)_第1张图片

 

 

 

 

 

#《SteamVR/Extras》

#《SteamVR/Extras》

#《SteamVR/Extras》

++++立钻哥哥:“SteamVR/Extas”是对SteamVR框架代码的剖析

++++A.0、SteamVR/Extras/

++++A.1、/Extras/SteamVR_GazeTracker.cs

++++A.2、/Extras/SteamVR_TestThrow.cs

++++A.3、/Extras/SteamVR_TestTrackedCamera.cs

++++A.4、

 

 

 

 

 

 

 

##A.0、SteamVR/Extras/

##A.0、SteamVR/Extras/

++A.0、SteamVR/Extras/

++++立钻哥哥:好吧,先通过Xmind来看一下这个目录下基本结构吧:

《SteamVR2.0/Extras》(Yanlz+Unity+SteamVR+Extras+OpenVR+GazeTracker+立钻哥哥+==)_第2张图片

 

 

++SteamVR/Extras/SteamVR_GazeTracker.cs

++++立钻哥哥:\Assets\SteamVR\Extras\SteamVR_GazeTracker.cs

using UnityEngine;

using System.Collections;

 

namespace Valve.VR.Extras{

    public class SteamVR_GazeTracker : MonoBehaviour{

        public bool isInGaze = false;

        public event GazeEventHandler GazeOn;

        public event GazeEventHandler GazeOff;

        public float gazeInCutoff = 0.15f;

        public float gazeOutCutoff = 0.4f;

 

        //Contains a HMD tracked object that we can use to find the users gaze

        protected Transform hmdTrackedObject = null;

 

        public virtual void OnGazeOn(GazeEventArgs gazeEventArgs){}
        public virtual void OnGazeOff(GazeEventArgs gazeEventArgs){}

        protected virtual void Update(){}

 

    }    //立钻哥哥:public class SteamVR_GazeTracker:MonoBehaviour{}

 

    public struct GazeEventArgs{

        public float distance;
    }

 

    public delegate void GazeEventHandler(object sender, GazeEventArgs gazeEventArgs);

 

}    //立钻哥哥:namespace Valve.VR.Extras{}

 

 

++SteamVR/Extras/SteamVR_TestThrow.cs

++++立钻哥哥:\Assets\SteamVR\Extras\SteamVR_TestThrow.cs

using UnityEngine;

using System.Collections;

 

namespace Valve.VR.Extras{

    [RequireComponent(typeof(SteamVR_TrackedObject))]

    public class SteamVR_TestThrow : MonoBehaviour{

        public GameObject prefab;

        public Rigidbody attachPoint;

 

        [SteamVR_DefaultAction(Interact)]

        public SteamVR_Action_Boolean spawn;

    

        SteamVR_Behaviour_Pose trackedObj;

        FixedJoint joint;

 

        private void Awake(){}

        private void FixedUpdate(){}

 

    }    //立钻哥哥:public class SteamVR_TestThrow:MonoBehaviour{}

}    //立钻哥哥:namespace Valve.VR.Extras{}

 

 

++SteamVR/Extras/SteamVR_TestTrackedCamera.cs

++++立钻哥哥:\Assets\SteamVR\Extras\SteamVR_TestTrackedCamera.cs

using UnityEngine;

 

namespace Valve.VR.Extras{

    public class SteamVR_TestTrackedCamera : MonoBehaviour{

        public Material material;

        public Transform target;

        public bool undistorted = true;

        public bool cropped = true;

 

        private void OnEnable(){}

        private void OnDisable(){}

        private void Update(){}

 

    }    //立钻哥哥:public class SteamVR_TestTrackedCamera:MonoBehaviour{}

}    //立钻哥哥:namespace Valve.VR.Extras{}

 

 

 

 

 

 

 

##A.1、/Extras/SteamVR_GazeTracker.cs

##A.1、/Extras/SteamVR_GazeTracker.cs

++A.1、/Extras/SteamVR_GazeTracker.cs

++++立钻哥哥:\Assets\SteamVR\Extras\SteamVR_GazeTracker.cs

using UnityEngine;

using System.Collections;

 

namespace Valve.VR.Extras{

    public class SteamVR_GazeTracker : MonoBehaviour{

        public bool isInGaze = false;

        public event GazeEventHandler GazeOn;

        public event GazeEventHandler GazeOff;

        public float gazeInCutoff = 0.15f;

        public float gazeOutCutoff = 0.4f;

 

        //Contains a HMD tracked object that we can use to find the users gaze

        protected Transform hmdTrackedObject = null;

 

        public virtual void OnGazeOn(GazeEventArgs gazeEventArgs){

            if(null != GazeOn){

                GazeOn(this, gazeEventArgs);

            }

        }

 

        public virtual void OnGazeOff(GazeEventArgs gazeEventArgs){

            if(null != GazeOff){

                GazeOff(this, gazeEventArgs);

            }

        }

 

        protected virtual void Update(){

            //If we havent set up hmdTrackedObject find what the user is looking at

            if(null == hmdTrackedObject){

                 SteamVR_TrackedObject[] trackedObjects FindObjectsOfType<SteamVR_TrackedObject>();

                foreach(SteamVR_TrackedObject tracked in trackedObjects){

                    if(tracked.index == SteamVR_TrackedObject.EIndex.Hmd){

                        hmdTrackedObject = tracked.transform;

                        break;

                    }

                }

            }    //立钻哥哥:if(null == hmdTrackedObject){}

 

            if(hmdTrackedObject){

                Ray ray = new Ray(hmdTrackedObject.position, hmdTrackedObject.forward);

                Plane plane = new Plane(hmdTrackedObject.forward, transform.position);

 

                float enter = 0.0f;

                if(plane.Raycast(ray, out enter)){

                    Vector3 intersect = hmdTrackedObject.position + hmdTrackedObject.forward * enter;

                    //Debug.Log(立钻哥哥:Gaze dist =  + dist);

        

                    if(dist < gazeInCutoff && !isInGaze){

                        isInGaze = true;

                        GazeEventArgs gazeEventArgs;

                        gazeEventArgs.distance = dist;

                        OnGazeOn(gazeEventArgs);

 

                    }else if(dist >= gazeOutCutoff && isInGaze){

                        isInGaze = false;

                        GazeEventArgs gazeEventArgs;

                        gazeEventArgs.distance = dist;

                        OnGazeOff(gazeEventArgs);

                    }

                }    //立钻哥哥:if(plane.Raycast(ray, out enter)){}

            }    //立钻哥哥:if(hmdTrackedObject){}

        }    //立钻哥哥:protected virtual void Update(){}

 

    }    //立钻哥哥:public class SteamVR_GazeTracker:MonoBehaviour{}

 

    public struct GazeEventArgs{

        public float distance;

    }

 

    public delegate void GazeEventHandler(object sender, GazeEventArgs gazeEventArgs);

 

}    //立钻哥哥:namespace Valve.VR.Extras{}

 

++SteamVR_GazeTracker.cs脚本解析

++++立钻哥哥:这个脚本的作用是判断当前物体是否被用户(头显)所注视,进入注视和离开注视都会有回调

++++处于注视状态的物体与实际注视点的距离范围定义为小于0.15米,而离开注视状态的距离范围为大于0.4米

++++之所以有一个大概的范围,并且使用了一个平面来相交,是因为注视这个动作是比较粗略的,玩家比较难能精确注视

++++Gaze回调的事件结构体,只有一个参数,即距离,表示凝视点与物体(中心)的距离

public struct GazeEventArgs{

    public float distance;

}

++++首次调用会去查找头显,方法是查找所有SteamVR_TrackedObject对象;所有的跟踪对象(比如头显、手柄、基站)都是SteamVR_TrackedObject对象(相应的对象上附加了SteamVR_TrackedObject脚本)

SteamVR_TrackedObject[] trackedObjects = FindObjectsOfType<SteamVR_TrackedObject>();

foreach(SteamVR_TrackedObject tracked in trackedObjects){

    if(tracked.index == SteamVR_TrackedObject.EIndex.Hmd){

        //立钻哥哥:找到头显设备,取其transform对象;头显设备的索引是0号索引

        hmdTrackedObject = tracked.transform;

        break;

    }

}

 

 

++拓展:SteamVR_TrackedObject.cs

++++[SteamVR_TrackedObject]的应用:

//If we havent set up hmdTrackedObject find what the user is looking at

if(null == hmdTrackedObject){

    SteamVR_TrackedObject[] trackedObjects = FindObjectsOfType<SteamVR_TrackedObject>();

    foreach(SteamVR_TrackedObject tracked in trackedObjects){

        if(tracked.index == SteamVR_TrackedObject.EIndex.Hmd){

            hmdTrackedObject = tracked.transform;

            break;

        }

    }

}

 

 

++++立钻哥哥:\Assets\SteamVR\Scripts\SteamVR_TrackedObject.cs

using UnityEngine;

using Valve.VR;

 

namespace Valve.VR{

    public class SteamVR_TrackedObject : MonoBehaviour{

        public enum EIndex{

            None = -1,

            Hmd = (int)OpenVR.k_unTrackedDeviceIndex_Hmd,

            Device1,

            Device2,

            ...

        }

 

        public EIndex index;

 

        [Tooltip(If not set, relative to parent)]

        public Transform origin;

 

        public bool isValid{  get;  private set;  }

 

        private void OnNewPoses(TrackedDevicePose_t[] poses){

            if(index == EIndex.None){

                return;

            }

            ... ...

        }

 

        SteamVR_Events.Action newPosesAction;

 

        SteamVR_TrackedObject(){

            newPosesAction = SteamVR_Events.NewPosesAction(OnNewPoses);

        }

 

        private void Awake(){

            OnEnable();

        }

 

        void OnEnable(){}

        void OnDisable(){}

 

        public void SetDeviceIndex(int index){

            if(System.Enum.IsDefined(typeof(EIndex), index)){

                this.index = (EIndex)index;

            }

        }

 

    }    //立钻哥哥:public class SteamVR_TrackedObject : MonoBehaviour{}

}    //立钻哥哥:namespace Valve.VR{}

 

 

 

 

 

 

 

##A.2、/Extras/SteamVR_TestThrow.cs

##A.2、/Extras/SteamVR_TestThrow.cs

++A.2、/Extras/SteamVR_TestThrow.cs

++++立钻哥哥:\Assets\SteamVR\Extras\SteamVR_TestThrow.cs

using UnityEngine;

using System.Collections;

 

namespace Valve.VR.Extras{

    [RequireComponent(typeof(SteamVR_TrackedObject))]

    public class SteamVR_TestThrow : MonoBehaviour{

        public GameObject prefabs;

        public Rigidbody attachPoint;

    

        [SteamVR_DefaultAction(Interact)]

        public SteamVR_Action_Boolean spawn;

    

        SteamVR_Behaviour_Pose trackedObj;

        FixedJoint jiont;

 

        private void Awake(){

            trackedObj = GetComponent<SteamVR_Behaviour_Pose>();

        }

 

        private void FixedUpdate(){

            if(joint == null && spawn.GetStateDown(trackedObj.inputSource)){

                var go = GameObject.Instantiate(prefab);

                go.transform.position = attachPoint.transform.position;

 

                joint = go.AddComponent<FixedJoint>();

                joint.connectedBody = attachPoint;

 

            }else if(joint != null && spawn.GetStateUp(trackedObj.inputSource)){

                var go = joint.gameObject;

                var rigidbody = go.GetComponent<Rigidbody>();

                Object.DestroyImmediate(joint);

                joint = null;

                Object.Destroy(go, 15.0f);

 

                //We should probably apply the offset between trackedObj.transform.position and device.transform.pos to insert into the physics sim at the correct location, however, we would then want to predict ahead the visual representation by the same amount we are predicting our render poses.

                var origion = trackedObj.origin ? trackedObj.origin : trackedObj.transform.parent;

                if(null != origin){

                    rigidbody.velocity = origin.TransformVector(trackedObj.GetVelocity());

                    rigidbody.angularVelocity = origin.TransformVector(trackedObj.GetAngularVelocity());

 

                }else{

                    rigidbogy.velocity = trackedObj.GetVelocity();

                    rigidbody.angularVelocity = trackedObj.GetAngularVelocity();

                }

 

                rigidbody.maxAngularVelocity = rigidbody.angularVelocity.magnitude;

            }

        }    //立钻哥哥:private void FixedUpdate(){}

 

    }    //立钻哥哥:public class SteamVR_TestThrow:MonoBehaviour{}

}    //立钻哥哥:namespace Valve.VR.Extras{}

 

 

++SteamVR_TestThrow.cs脚本解析

++++立钻哥哥:这是一个测试IK的示例场景;通过手柄扔出一个物体的例子,通过扳机键创建一个物体并抓在手中,然后通过甩臂并同时松开扳机将物体扔出去

 

++++如果还没有建立关节,当按下扳机键时,建立物体与手柄的(关节)关联,相当于就是抓起了物体:

public Rigidbody attachPoint;

 

[SteamVR_DefaultAction(Interact)]

public SteamVR_Action_Boolean spawn;

 

if(null == joint && spawn.GetTouchDown(trackedObj.inputSource)){

    //先创建物体(复制了场景中的一个物体)

    var go = GameObject.Instantiate(prefab);

 

    //创建的物体的位置位于tip的关联点的位置

    go.transform.position = attachPoint.transform.position;

 

    //添加固定关节,这样物体就能跟随手柄运动而动了

    joint = go.AddComponent<FixedJoint>();

 

    //立钻哥哥:将其与手柄tip的attach关联

    joint.connectedBody = attachPoint;

}

 

++++如果已经建立了关节,再按扳机键时则会销毁关节,由于物体是一个刚体,就会自由下落,当然还会通过手柄给它一个初速度:

Else if(null != joint && spawn.GetStateUp(trackedObj.inputSource)){

    Var go = joint.gameObject;

    Var rigidbody = go.GetComponent();

    Object.DestroyImmediate(joint);

 

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

《SteamVR2.0/Extras》(Yanlz+Unity+SteamVR+Extras+OpenVR+GazeTracker+立钻哥哥+==)_第3张图片

++立钻哥哥推荐的拓展学习链接(Link_Url)

立钻哥哥推荐的拓展学习链接(Link_Url)

++++立钻哥哥Unity 学习空间: http://blog.csdn.net/VRunSoftYanlz/

++++HTC_VIVE开发基础https://blog.csdn.net/VRunSoftYanlz/article/details/81989970

++++Oculus杂谈https://blog.csdn.net/VRunSoftYanlz/article/details/82469850

++++Oculus安装使用https://blog.csdn.net/VRunSoftYanlz/article/details/82718982

++++SteamVR简介https://blog.csdn.net/VRunSoftYanlz/article/details/86484254

++++SteamVR脚本功能分析https://blog.csdn.net/VRunSoftYanlz/article/details/86531480

++++SteamVR/Extrashttps://blog.csdn.net/VRunSoftYanlz/article/details/86584108

++++OpenXR简介https://blog.csdn.net/VRunSoftYanlz/article/details/85726365

++++VRTK杂谈https://blog.csdn.net/VRunSoftYanlz/article/details/82562993

++++VRTK快速入门(杂谈)https://blog.csdn.net/VRunSoftYanlz/article/details/82955267

++++VRTK官方示例(目录)https://blog.csdn.net/VRunSoftYanlz/article/details/82955410

++++VRTK代码结构(目录)https://blog.csdn.net/VRunSoftYanlz/article/details/82780085

++++VRTK(SceneResources)https://blog.csdn.net/VRunSoftYanlz/article/details/82795400

++++VRTK_ControllerEventshttps://blog.csdn.net/VRunSoftYanlz/article/details/83099512

++++VRTK_InteractTouchhttps://blog.csdn.net/VRunSoftYanlz/article/details/83120220

++++VR实验:以太网帧的构成https://blog.csdn.net/VRunSoftYanlz/article/details/82598140

++++FrameVR示例V0913https://blog.csdn.net/VRunSoftYanlz/article/details/82808498

++++FrameVR示例V1003https://blog.csdn.net/VRunSoftYanlz/article/details/83066516

++++SwitchMachineV1022https://blog.csdn.net/VRunSoftYanlz/article/details/83280886

++++PlaySceneManagerV1022https://blog.csdn.net/VRunSoftYanlz/article/details/83280886

++++Unity5.x用户手册https://blog.csdn.net/VRunSoftYanlz/article/details/81712741

++++Unity面试题ABChttps://blog.csdn.net/vrunsoftyanlz/article/details/78630687

++++Unity面试题Dhttps://blog.csdn.net/VRunSoftYanlz/article/details/78630838

++++Unity面试题Ehttps://blog.csdn.net/vrunsoftyanlz/article/details/78630913

++++Unity面试题Fhttps://blog.csdn.net/VRunSoftYanlz/article/details/78630945

++++Cocos2dx面试题https://blog.csdn.net/VRunSoftYanlz/article/details/78630967

++++禅道[zentao]https://blog.csdn.net/VRunSoftYanlz/article/details/83964057

++++Lua快速入门篇(Xlua拓展):https://blog.csdn.net/VRunSoftYanlz/article/details/81173818

++++Lua快速入门篇(XLua教程):https://blog.csdn.net/VRunSoftYanlz/article/details/81141502

++++Lua快速入门篇(基础概述)https://blog.csdn.net/VRunSoftYanlz/article/details/81041359

++++框架知识点https://blog.csdn.net/VRunSoftYanlz/article/details/80862879

++++游戏框架(UI框架夯实篇)https://blog.csdn.net/vrunsoftyanlz/article/details/80781140

++++游戏框架(初探篇)https://blog.csdn.net/VRunSoftYanlz/article/details/80630325

++++设计模式简单整理https://blog.csdn.net/vrunsoftyanlz/article/details/79839641

++++专题:设计模式(精华篇)https://blog.csdn.net/VRunSoftYanlz/article/details/81322678

++++U3D小项目参考https://blog.csdn.net/vrunsoftyanlz/article/details/80141811

++++Unity案例(Vehicle)https://blog.csdn.net/VRunSoftYanlz/article/details/82355876

++++UML类图https://blog.csdn.net/vrunsoftyanlz/article/details/80289461

++++PowerDesigner简介https://blog.csdn.net/VRunSoftYanlz/article/details/86500084

++++Unity知识点0001https://blog.csdn.net/vrunsoftyanlz/article/details/80302012

++++Unity知识点0008https://blog.csdn.net/VRunSoftYanlz/article/details/81153606

++++U3D_Shader编程(第一篇:快速入门篇)https://blog.csdn.net/vrunsoftyanlz/article/details/80372071

++++U3D_Shader编程(第二篇:基础夯实篇)https://blog.csdn.net/vrunsoftyanlz/article/details/80372628

++++Unity引擎基础https://blog.csdn.net/vrunsoftyanlz/article/details/78881685

++++Unity面向组件开发https://blog.csdn.net/vrunsoftyanlz/article/details/78881752

++++Unity物理系统https://blog.csdn.net/vrunsoftyanlz/article/details/78881879

++++Unity2D平台开发https://blog.csdn.net/vrunsoftyanlz/article/details/78882034

++++UGUI基础https://blog.csdn.net/vrunsoftyanlz/article/details/78884693

++++UGUI进阶https://blog.csdn.net/vrunsoftyanlz/article/details/78884882

++++UGUI综合https://blog.csdn.net/vrunsoftyanlz/article/details/78885013

++++Unity动画系统基础https://blog.csdn.net/vrunsoftyanlz/article/details/78886068

++++Unity动画系统进阶https://blog.csdn.net/vrunsoftyanlz/article/details/78886198

++++Navigation导航系统https://blog.csdn.net/vrunsoftyanlz/article/details/78886281

++++Unity特效渲染https://blog.csdn.net/vrunsoftyanlz/article/details/78886403

++++Unity数据存储https://blog.csdn.net/vrunsoftyanlz/article/details/79251273

++++Unity中Sqlite数据库https://blog.csdn.net/vrunsoftyanlz/article/details/79254162

++++WWW类和协程https://blog.csdn.net/vrunsoftyanlz/article/details/79254559

++++Unity网络https://blog.csdn.net/vrunsoftyanlz/article/details/79254902

++++C#事件https://blog.csdn.net/vrunsoftyanlz/article/details/78631267

++++C#委托https://blog.csdn.net/vrunsoftyanlz/article/details/78631183

++++C#集合https://blog.csdn.net/vrunsoftyanlz/article/details/78631175

++++C#泛型https://blog.csdn.net/vrunsoftyanlz/article/details/78631141

++++C#接口https://blog.csdn.net/vrunsoftyanlz/article/details/78631122

++++C#静态类https://blog.csdn.net/vrunsoftyanlz/article/details/78630979

++++C#中System.String类https://blog.csdn.net/vrunsoftyanlz/article/details/78630945

++++C#数据类型https://blog.csdn.net/vrunsoftyanlz/article/details/78630913

++++Unity3D默认的快捷键https://blog.csdn.net/vrunsoftyanlz/article/details/78630838

++++游戏相关缩写https://blog.csdn.net/vrunsoftyanlz/article/details/78630687

++++UnityAPI.Rigidbody刚体https://blog.csdn.net/VRunSoftYanlz/article/details/81784053

++++UnityAPI.Material材质https://blog.csdn.net/VRunSoftYanlz/article/details/81814303

++++UnityAPI.Android安卓https://blog.csdn.net/VRunSoftYanlz/article/details/81843193

++++UnityAPI.AndroidJNI安卓JNIhttps://blog.csdn.net/VRunSoftYanlz/article/details/81879345

++++UnityAPI.Transform变换https://blog.csdn.net/VRunSoftYanlz/article/details/81916293

++++UnityAPI.WheelCollider轮碰撞器https://blog.csdn.net/VRunSoftYanlz/article/details/82356217

++++UnityAPI.Resources资源https://blog.csdn.net/VRunSoftYanlz/article/details/83155518

++++JSON数据结构https://blog.csdn.net/VRunSoftYanlz/article/details/82026644

++++CocosStudio快速入门https://blog.csdn.net/VRunSoftYanlz/article/details/82356839

++++Unity企业内训(目录)https://blog.csdn.net/VRunSoftYanlz/article/details/82634668

++++Unity企业内训(第1讲)https://blog.csdn.net/VRunSoftYanlz/article/details/82634733

++++Unity企业内训(第2讲)https://blog.csdn.net/VRunSoftYanlz/article/details/82861180

++++Unity企业内训(第3讲)https://blog.csdn.net/VRunSoftYanlz/article/details/82927699

++++Unity企业内训(第4讲)https://blog.csdn.net/VRunSoftYanlz/article/details/83479776

++++Unity企业内训(第5讲)https://blog.csdn.net/VRunSoftYanlz/article/details/83963811

++++Unity企业内训(第6讲)https://blog.csdn.net/VRunSoftYanlz/article/details/84207696

++++插件https://blog.csdn.net/VRunSoftYanlz/article/details/83963905

++++计算机组成原理(教材篇)https://blog.csdn.net/VRunSoftYanlz/article/details/82719129

++++立钻哥哥Unity 学习空间: http://blog.csdn.net/VRunSoftYanlz/

--_--VRunSoft:lovezuanzuan--_--

《SteamVR2.0/Extras》(Yanlz+Unity+SteamVR+Extras+OpenVR+GazeTracker+立钻哥哥+==)_第4张图片

你可能感兴趣的:(SteamVR)