分享

【新提醒】【HTC Vive之Unity3d开发日记@5】

 雪柳花明 2016-07-12

目录:

开发是一场西游的旅行,我们都有共同心愿,然而这是一场艰难困苦的旅程,有的人有些浮躁,有的人想要放弃,也有的人有坚定的信仰...无论你是什么样的心态,历史的车轮滚滚,时间如长江东逝水,浪花淘尽英雄.我敢说,无论有多少人,无论路途有多困难,我们无法阻碍世界的进程,我们只是在经历这一场修行,仅此而已.
下面请允许我援引一小段方大同<悟空>中的歌词,提升下这篇帖子的B格(有形装B,并不致命 ^_^):
[AppleScript] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
From which ever page you were on
 This is a whole new chapter
 See I once was a King
 Now I serve my master
 My master is a master of self
 He taught me how to master myself
 My master is the servant of people
 Seeks to give strength to the feeble
 It’s gonna be a long long journey
 But I’ll protect him from evil
 Someday we’ll find the wisdom
 One day we’ll carry the light
 When we return to the kingdom
 Then we can end the fight
 Then all the people will know
 After laying eyes on the scroll
 Then all the people will know
 Then all the people will know
 Journey


说到这首歌,我在htc vive版的节奏大师中还上了全球排行榜,一个叫做"中环世纪"的帅哥便是在下,当然同事也常常来挑战,然而,作为一个王者,她们并不是在下的对手,期待大家来挑战我,下面是该应用的截图!


Ok,回归正题,今天要分享一个检测htc手柄滑动的脚本:
[C#] 纯文本查看 复制代码
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
/// <summary>
/// 滑动检测
/// </summary>
public class ViveSwipeDetector : NetworkBehaviour {
        [SerializeField]
        SteamVR_TrackedObject trackedObj;
        private const int mMessageWidth  = 200;
        private const int mMessageHeight = 64;
        //X轴
        private readonly Vector2 mXAxis = new Vector2(1, 0);
        //Y轴
        private readonly Vector2 mYAxis = new Vector2(0, 1);
        //是否跟踪滑动
        private bool trackingSwipe = false;
        //是否检查滑动
        private bool checkSwipe = false;
        //消息数组,可以考虑用枚举enum
        private readonly string [] mMessage = {
                "",
                "Swipe Left",//左滑
                "Swipe Right",//右滑
                "Swipe Top",//上滑
                "Swipe Bottom"//下滑
        };
        //消息索引
        private int mMessageIndex = 0;
        // 滑动的角度范围参考值
        private const float mAngleRange = 30;
        // 用户至少滑动多少像素才能被识别成滑动
        private const float mMinSwipeDist = 0.2f;
        //最小的滑动速度,低于该速度则无法识别为滑动
        private const float mMinVelocity  = 4.0f;
        //记录开始位置和结束位置
        private Vector2 mStartPosition;
        private Vector2 endPosition;
        //滑动开始时间
        private float mSwipeStartTime;
        void Update () {
                //获取手柄设备
                var device = SteamVR_Controller.Input((int)trackedObj.index);
                // GetTouchDown时可能会滑动
                if ((int)trackedObj.index != -1 && device.GetTouchDown (Valve.VR.EVRButtonId.k_EButton_Axis0)) {
                        trackingSwipe = true;
                        // 记录开始时间和位置
                        mStartPosition = new Vector2 (device.GetAxis (Valve.VR.EVRButtonId.k_EButton_Axis0).x,
                                device.GetAxis (Valve.VR.EVRButtonId.k_EButton_Axis0).y);
                        mSwipeStartTime = Time.time;
                }
                // GetTouchUp时可能会滑动
                else if (device.GetTouchUp (Valve.VR.EVRButtonId.k_EButton_Axis0)) {
                        trackingSwipe = false;
                        trackingSwipe = true;
                        checkSwipe = true;
                        Debug.Log ("追踪结束"+Time.time);
                }
                else if(trackingSwipe)
                {
                        //结束位置
                        endPosition= new Vector2 (device.GetAxis (Valve.VR.EVRButtonId.k_EButton_Axis0).x,
                                                      device.GetAxis (Valve.VR.EVRButtonId.k_EButton_Axis0).y);
                }
                if (checkSwipe) {
                        checkSwipe = false;
                        //滑动时间
                        float deltaTime = Time.time - mSwipeStartTime;
                        //滑动向量
                        Vector2 swipeVector = endPosition - mStartPosition;
                        //滑动速度
                        float velocity = swipeVector.magnitude/deltaTime;
                        Debug.Log("滑动的速度为:"+velocity+"--"+"滑动时间为:"+deltaTime);
            if (velocity > mMinVelocity &&
                                swipeVector.magnitude > mMinSwipeDist) {
                // 如果滑动满足检测要求
                
                swipeVector.Normalize();
                                //滑动的角度
                                float angleOfSwipe = Vector2.Dot(swipeVector, mXAxis);
                                angleOfSwipe = Mathf.Acos(angleOfSwipe) * Mathf.Rad2Deg;
                                // 检测是左滑还是右滑
                                if (angleOfSwipe < mAngleRange) {
                                        OnSwipeRight();
                                } else if ((180.0f - angleOfSwipe) < mAngleRange) {
                                        OnSwipeLeft();
                                } else {
                                        //检测上下滑动
                                        angleOfSwipe = Vector2.Dot(swipeVector, mYAxis);
                                        angleOfSwipe = Mathf.Acos(angleOfSwipe) * Mathf.Rad2Deg;
                                        if (angleOfSwipe < mAngleRange) {
                                                OnSwipeTop();
                                        } else if ((180.0f - angleOfSwipe) < mAngleRange) {
                                                OnSwipeBottom();
                                        } else {
                                                mMessageIndex = 0;
                                        }
                                }
                        }
                }
         
        }
        void OnGUI() {
                // 显示消息,方便在眼镜里面观测
                GUI.Label(new Rect((Screen.width-mMessageWidth)/2,
                        (Screen.height-mMessageHeight)/2,
                        mMessageWidth, mMessageHeight),
                        mMessage[mMessageIndex]);
        }
        private void OnSwipeLeft() {
                Debug.Log ("Swipe Left 左滑");
                mMessageIndex = 1;
        }
        private void OnSwipeRight() {
                Debug.Log ("Swipe right 右滑");
                mMessageIndex = 2;
        }
        private void OnSwipeTop() {
                Debug.Log ("Swipe Top 上滑");
                mMessageIndex = 3;
        }
        private void OnSwipeBottom() {
                Debug.Log ("Swipe Bottom 下滑");
                mMessageIndex = 4;
        }
}

大家如果觉得在自己的项目中可以用得上的话可以自行改进,本身是一个不太复杂的脚本哈.
还有就是,关于Htc Vive开发者联盟(非官方)的入盟方式,可能有些朋友不太理解,为什么进个组织要100蛮牛币呢?
其实,我们建立联盟,是希望这是一个有益开发者的地方,所以联盟是抱着一种宁缺毋滥的态度来招揽开发者的,在联盟中有非常明了的纪律,来确保联盟成员可以有一个好的交流环境,并且营造一种严肃且积极的开发氛围,力求团结每一个开发者,凝聚每一分力量和智慧,从而促进整个行业的发展!
所以,100只是一个数字,代表的是百分百的诚意和意志,如果是一个随随便便的联盟,大家可以找到一大堆的乌合之众,可能一天到晚就知道侃大山/瞎扯淡,这并不能提高你的开发水平,同时也浪费了你的开发时间.
Anyway,联盟诚邀广大的开发者朋友加入进来,团结一心,共同进步,For the glory of Alliance!
再多的艰难困苦,联盟和你一起度过,就像我心仪的女子就要远去,尽管我竭尽全力去挽留,然而,芳心已失,芳心已逝...在悄悄地流下几滴动情的眼泪,茫茫人海中,要遇到心仪之人,谈何容易,然造化弄人,非人力可抗拒,孤只能黯然销魂;此时我只能去想想联盟的未来,转移情思,可见联盟就是大家的希望,一切都是为了联盟的荣耀!
似乎已经满满的B格了,下面再附上联盟的密匙,愿君多采集!
The Lab渲染器(CloudHu译)

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多