Vuforia开发技巧-用手指拖拽Augmented模型

原文链接:http://www.arvrschool.com/read.php?tid=77&fid=21

AR/VR技术交流群 129340649


前面文章介绍了怎么对3D模型进行缩放、平移和旋转, 

(Unity3d)Vuforia开发基础五-模型交互  
这篇将会介绍如何通过手指拖拽平移模型。  
思路:  
1、通过绘制射线判断是否选中模型  
2、然后根据手指在屏幕上的移动位置变化来移动模型  
使用以下脚本,将脚本放到ARCamera下,确保模型具有mesh Collider 属性。
复制代码
1
2
3
4
5
6
7
8
9
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
37
38
39
40
41
42
43
44
using  UnityEngine;
using  System.Collections;
public  class  DragObject : MonoBehaviour {
    private  Transform pickedObject =  null ;
    private  Vector3 lastPlanePoint;
    // Use this for initialization
    void  Start () {
    }
    // Update is called once per frame
    void  Update () {
//创建一个平面
         Plane targetPlane =  new  Plane(transform.up, transform.position);
         foreach  (Touch touch  in  Input.touches) {
             //获取摄像头近平面到屏幕触摸点的射线
             Ray ray = Camera.main.ScreenPointToRay(touch.position);
             //获取射线沿着plane的距离
             float  dist = 0.0f;
             targetPlane.Raycast(ray,  out  dist);
             //获取沿着射线在距离dist位置的点
             Vector3 planePoint = ray.GetPoint(dist);
             //Debug.Log("Point=" + planePoint);
             //按下手指触碰屏幕
             if  (touch.phase == TouchPhase.Began) {
                  RaycastHit hit =  new  RaycastHit();
// 判断是否有碰撞到对象
                  if  (Physics.Raycast(ray,  out  hit, 1000)) {
                      pickedObject = hit.transform;
                      lastPlanePoint = planePoint;
                  else  {
                      pickedObject =  null ;
                  }
             //选中模型后拖拽
             else  if  (touch.phase == TouchPhase.Moved) {
                  if  (pickedObject !=  null ) {
                      pickedObject.position += planePoint - lastPlanePoint;
                      lastPlanePoint = planePoint;
                  }
             //释放
             else  if  (touch.phase == TouchPhase.Ended) {
                  pickedObject =  null ;
             }
         }
    }
}
通过前面教程中粗略计算手指在屏幕上的滑动位移也能够用来做类似的效果,只是精度相对来说差一些,需要手动调整。但是调整之后,效果还是挺好的 

复制代码
1
2
3
4
5
6
7
8
9
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using  UnityEngine;
using  System.Collections;
public  class  DragObject : MonoBehaviour
{
     private  Transform pickedObject =  null ;
     private  Vector3 lastPlanePoint;
  
     private  float  x;
     private  float  y;
     // 移动加权,使移动与手指移动同步
     private  float  xSpeed = 2;
     // Use this for initialization
     void  Start()
     {
     }
     // Update is called once per frame
     void  Update()
     {
         //创建一个平面
         Plane targetPlane =  new  Plane(transform.up, transform.position);
         Debug.Log( "position="  + transform.position);
         foreach  (Touch touch  in  Input.touches)
         {
             //获取摄像头近平面到屏幕触摸点的射线
             Ray ray = Camera.main.ScreenPointToRay(touch.position);
             //获取射线沿着plane的距离
             float  dist = 0.0f;
             targetPlane.Raycast(ray,  out  dist);
             //获取沿着射线在距离dist位置的点
             Vector3 planePoint = ray.GetPoint(dist);
             Debug.Log( "Point="  + planePoint);
             //按下手指触碰屏幕
             if  (touch.phase == TouchPhase.Began)
             {
                 RaycastHit hit =  new  RaycastHit();
                 // 判断是否有碰撞到对象
                 if  (Physics.Raycast(ray,  out  hit, 1000))
                 {
                     pickedObject = hit.transform;
                     lastPlanePoint = planePoint;
                 }
                 else
                 {
                     pickedObject =  null ;
                 }
  
             } //选中模型后拖拽
             else  if  (touch.phase == TouchPhase.Moved)
             {
                 if  (pickedObject !=  null )
                 {
                     // 设置移动位移
                     x = Input.GetAxis( "Mouse X" ) * xSpeed;
                     pickedObject.position +=  new  Vector3(x, 0, 0);
                     // 方法一
                     //pickedObject.position += planePoint - lastPlanePoint;
                     lastPlanePoint = planePoint;
                 }
                 //释放
             }
             else  if  (touch.phase == TouchPhase.Ended)
             {
                 pickedObject =  null ;
             }
         }
     }
}
建议使用第二种方式。第一种方式经过测试,不是很灵敏,可能还需要进一步调整。
  • 3
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值