Hololens开发笔记之Gesture⼿势识别(Manipulation⼿势控制
物体平移)
Manipulationgesture:保持点击⼿势,在3D世界中绝对运动
当你想要全息图像1:1响应⽤户⼿部移动时,操纵⼿势能被⽤于移动、缩放或旋转全息图像。如此的⼀个⽤处是使得⽤户可以在世界中绘制
图像或作画。使⽤所有的⼿势时,操纵⼿势的初始⽬标应该通过凝视来选中。⼀旦点击⼿势开始,通过⼿部移动的任何对对象的操作都能够
被处理,在⽤户操作全息图像时,从⽽使得⽤户得以⾃由地四处张望。
1、修改,添加Presd,Relead处理函数,⽤于识别物体被点
击和被释放的事件
完整代码如下:
//Copyright(c)htsrerved.
//ENSEintheprojectrootforliceninformation.
c;
usingUnityEngine;
;
{
///
///HandsManagerdeterminesifthehandiscurrentlydetectedornot.
///
publicpartialclassHandsManager:Singleton
{
///
///HandDetectedtracksthehanddetectedstate.
///Returnstrueifthelistoftrackedhandsisnotempty.
///
publicboolHandDetected
{
get{>0;}
}
privateHashSet
publicGameObjectFocudGameObject{get;privatet;}
voidAwake()
{
//识别到来源
Detected+=InteractionManager_SourceDetected;
//来源丢失
Lost+=InteractionManager_SourceLost;
//来源被按下
Presd+=InteractionManager_SourcePresd;
//被释放
Relead+=InteractionManager_SourceRelead;
FocudGameObject=null;
}
privatevoidInteractionManager_SourceDetected(InteractionSourceStatestate)
{
//Checktoethatthesourceisahand.
if(!=)
{
return;
}
();
}
privatevoidInteractionManager_SourceLost(InteractionSourceStatestate)
{
//Checktoethatthesourceisahand.
if(!=)
{
return;
}
if(ns())
{
();
}
FocudGameObject=null;
}
//⼿势释放时,将被关注的物体置空
privatevoidInteractionManager_SourceRelead(InteractionSourceStatestate)
{
FocudGameObject=null;
}
//识别到⼿指按下时,将凝视射线关注的物体置为当前⼿势操作的对象
privatevoidInteractionManager_SourcePresd(InteractionSourceStatestate)
{
if(dObject!=null)
{
FocudGameObject=dObject;
}
}
voidOnDestroy()
{
Detected-=InteractionManager_SourceDetected;
Lost-=InteractionManager_SourceLost;
Relead-=InteractionManager_SourceRelead;
Presd-=InteractionManager_SourcePresd;
}
}
}
2、修改,订阅Manipulationgesture事件
//Copyright(c)htsrerved.
//ENSEintheprojectrootforliceninformation.
usingSystem;
usingUnityEngine;
;
{
///
///GestureManagercreatesagesturerecognizerandsignsupforatapgesture.
///Whenatapgestureisdetected,GestureManagerusGazeManagertofindthegameobject.
///GestureManagerthenndsamessagetothatgameobject.
///
[RequireComponent(typeof(GazeManager))]
publicpartialclassGestureManager:Singleton
{
///
///Keytopressintheeditortolectthecurrentlygazedhologram
///
publicKeyCodeEditorSelectKey=;
///
///Tolectevenwhenahologramisnotbeinggazedat,
///ttheoverridefocudobject.
///Ifitsnull,thenthegazedatobjectwillbelected.
///
publicGameObjectOverrideFocudObject
{
get;t;
}
publicboolIsManipulating{get;privatet;}
publicVector3ManipulationPosition{get;privatet;}
///
///Getsthecurrentlyfocudobject,ornullifnone.
///
publicGameObjectFocudObject
{
get{returnfocudObject;}
}
privateGestureRecognizergestureRecognizer;
privateGameObjectfocudObject;
voidStart()
{
//fortappedevents.
gestureRecognizer=newGestureRecognizer();
ognizableGestures(
|Tap
|lationTranslate);
Event+=GestureRecognizer_TappedEvent;
//订阅Manipulationgesture事件
lationStartedEvent+=GestureRecognizer_ManipulationStartedEvent;
lationUpdatedEvent+=GestureRecognizer_ManipulationUpdatedEvent;
lationCompletedEvent+=GestureRecognizer_ManipulationCompletedEvent;
lationCanceledEvent+=GestureRecognizer_ManipulationCanceledEvent;
//Startlookingforgestures.
apturingGestures();
}
privatevoidGestureRecognizer_ManipulationStartedEvent(InteractionSourceKindsource,Vector3cumulativeDelta,RayheadRay)
{
//当被关注的对象⾮空时,设置初始位置
if(dGameObject!=null)
{
IsManipulating=true;
ManipulationPosition=cumulativeDelta;
ssageUpwards("PerformManipulationStart",cumulativeDelta);
}
}
privatevoidGestureRecognizer_ManipulationUpdatedEvent(InteractionSourceKindsource,Vector3cumulativeDelta,RayheadRay)
{
//当被关注的对象⾮空时,更新新的位置
if(dGameObject!=null)
{
IsManipulating=true;
ManipulationPosition=cumulativeDelta;
ssageUpwards("PerformManipulationUpdate",cumulativeDelta);
}
}
privatevoidGestureRecognizer_ManipulationCompletedEvent(InteractionSourceKindsource,Vector3cumulativeDelta,RayheadRay)
{
IsManipulating=fal;
}
privatevoidGestureRecognizer_ManipulationCanceledEvent(InteractionSourceKindsource,Vector3cumulativeDelta,RayheadRay)
{
IsManipulating=fal;
}
privatevoidOnTap()
{
if(focudObject!=null)
{
ssage("OnTap");
}
}
privatevoidOnDoubleTap()
{
if(focudObject!=null)
{
ssage("OnDoubleTap");
}
}
privatevoidGestureRecognizer_TappedEvent(InteractionSourceKindsource,inttapCount,RayheadRay)
{
if(tapCount==1)
{
OnTap();
}
el
{
OnDoubleTap();
}
}
voidLateUpdate()
{
GameObjectoldFocudObject=focudObject;
if(&&
OverrideFocudObject==null&&
er!=null)
{
//Ifgazehitsahologram,tthefocudobjecttothatgameobject.
//Alsoifthecallerhasnotdecidedtooverridethefocudobject.
focudObject=ject;
}
el
{
//Ifourgazedoesn'thitahologram,tthefocudobjecttonulloroverridefocudobject.
focudObject=OverrideFocudObject;
}
//if(focudObject!=oldFocudObject)
//{
////Ifthecurrentlyfocudobjectdoesn'tmatchtheoldfocudobject,cancelthecurrentgesture.
////topreventapplyinggesturesfromonehologramtoanother.
//Gestures();
//apturingGestures();
//}
}
voidOnDestroy()
{
pturingGestures();
//取消订阅
Event-=GestureRecognizer_TappedEvent;
lationStartedEvent-=GestureRecognizer_ManipulationStartedEvent;
lationUpdatedEvent-=GestureRecognizer_ManipulationUpdatedEvent;
lationCompletedEvent-=GestureRecognizer_ManipulationCompletedEvent;
lationCanceledEvent-=GestureRecognizer_ManipulationCanceledEvent;
}
}
}
3、添加ManipulationTranslate脚本,计算相对位移,然后进⾏更新物体的新的位置。
usingUnityEngine;
tions;
;
publicclassManipulationTranslate:MonoBehaviour{
privateVector3manipulationPreviousPosition;
//Uthisforinitialization
voidStart(){
}
//Updateiscalledonceperframe
voidUpdate(){
}
voidPerformManipulationStart(Vector3position)
{
//设置初始位置
manipulationPreviousPosition=position;
}
voidPerformManipulationUpdate(Vector3position)
{
if(pulating)
{
//计算相对位移,然后更新物体的位置
Vector3moveVector=;
moveVector=position-manipulationPreviousPosition;
manipulationPreviousPosition=position;
on+=2f*moveVector;
}
}
4、运⾏测试
将凝视射线投射到对象上,举起右⼿使⾷指和拇指碰触在⼀起,然后进⾏左右移动,可以看到物体的位置将移动。
本文发布于:2022-12-31 18:21:34,感谢您对本站的认可!
本文链接:http://www.wtabcd.cn/fanwen/fan/90/67057.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |