Vuforia开发技巧二-如何实现交互
来源:互联网 发布:js 人脉关系图 编辑:程序博客网 时间:2024/05/01 00:00
原文链接:http://www.arvrschool.com/read.php?tid=22
ARVR技术交流群:129340649
模型交互主要还是以下这些交互方式:
主要的交互方式包括:1、模型的旋转、平移和缩放
这些需要配合触屏来操作。
2、模型的选定-射线法
模型选定之后显示高亮边框
3、模型动画的操作
使用代码,控制模型坐标等
使用Unity3d动画系统来完成
4、音频、文字等
使用Unity3D自带的组件完成
NGUI
5、其他
填色板等
这里先介绍最基本的交互方式:模型的旋转、平移和缩放
如果在PC上,使用鼠标操作。
如果在移动终端上,需要结合触屏来操作。
先来分析下旋转平移的原理,主要涉及到模型和摄像头的投影矩阵。
因此,一个模型的旋转、平移和缩放主要受这两者的投影矩阵对应关系影响。改变其中一个,都会使模型发生变化。
在一般的场景中,可以改变两者,而且改变摄像头的位置是比较常用的方式,因为对于缩放比较容易,网上一大推教程,可以去看看。
但是对AR场景,由于ARCamera是不能移动的,所以只能通过改变模型本身的坐标系。
请看下面的代码:
复制代码
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using
UnityEngine;
using
System.Collections;
public
class
Move : MonoBehaviour
{
public
Transform target;
float
distance = 30f;
float
xSpeed = 150f;
float
ySpeed = 150f;
float
yMinLimit = -180f;
float
yMaxLimit = 180f;
float
x = 0f;
float
y = 0f;
Vector2 oldPosition1;
Vector2 oldPosition2;
private
bool
flag_Roable =
true
;
//自动旋转标志
private
System.DateTime oldTime;
private
System.DateTime nowTime;
// Use this for initialization
void
Start()
{
transform.eulerAngles =
new
Vector3 (0,-90,0);
Vector3 angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
if
(rigidbody)
{
rigidbody.freezeRotation =
true
;
}
oldTime = System.DateTime.Now;
}
// Update is called once per frame
void
Update()
{
nowTime = System.DateTime.Now;
System.TimeSpan ts1 =
new
System.TimeSpan(oldTime.Ticks);
System.TimeSpan ts2 =
new
System.TimeSpan(nowTime.Ticks);
System.TimeSpan ts = ts2.Subtract(ts1).Duration();
if
(ts.Seconds > 8 && !Input.anyKey)
{
flag_Roable =
true
;
oldTime = System.DateTime.Now;
}
if
(Input.anyKey)
{
/*
if (Input.touchCount == 1)
{
if (Input.GetTouch(0).phase == TouchPhase.Moved)
{
//x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
//y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
x = Input.GetAxis("Mouse X") * xSpeed ;
y = Input.GetAxis("Mouse Y") * ySpeed ;
transform.Rotate(Vector3.up * -x * Time.deltaTime, Space.World);
transform.Rotate(Vector3.right * y * Time.deltaTime, Space.World);
}
}*/
if
(Input.touchCount > 1)
{
if
(Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(1).phase == TouchPhase.Moved)
{
Vector2 tempPosition1 = Input.GetTouch(0).position;
Vector2 tempPosition2 = Input.GetTouch(1).position;
if
(isEnlarge(oldPosition1, oldPosition2, tempPosition1, tempPosition2))
{
float
oldScale = transform.localScale.x;
float
newScale = oldScale * 1.025f;
transform.localScale =
new
Vector3(newScale, newScale, newScale);
}
else
{
float
oldScale = transform.localScale.x;
float
newScale = oldScale / 1.025f;
transform.localScale =
new
Vector3(newScale, newScale, newScale);
}
//备份上一次触摸点的位置,用于对比
oldPosition1 = tempPosition1;
oldPosition2 = tempPosition2;
}
}
}
}
bool
isEnlarge(Vector2 oP1, Vector2 oP2, Vector2 nP1, Vector2 nP2)
{
//函数传入上一次触摸两点的位置与本次触摸两点的位置计算出用户的手势
var leng1 = Mathf.Sqrt((oP1.x - oP2.x) * (oP1.x - oP2.x) + (oP1.y - oP2.y) * (oP1.y - oP2.y));
var leng2 = Mathf.Sqrt((nP1.x - nP2.x) * (nP1.x - nP2.x) + (nP1.y - nP2.y) * (nP1.y - nP2.y));
if
(leng1 < leng2)
{
//放大手势
return
true
;
}
else
{
//缩小手势
return
false
;
}
}
}
1 0
- Vuforia开发技巧二-如何实现交互
- (Unity3d)Vuforia开发基础五-模型交互
- Vuforia开发基础(五):模型交互
- Vuforia自学笔记(二)开发vuforia应用
- Vuforia开发技巧三-如何调整VideoPlayback中video的尺寸
- Vuforia开发技巧-用手指拖拽Augmented模型
- Vuforia SDK---- AR开发vuforia 相机自动对焦代码实现
- Vuforia SDK---- AR开发vuforia实现拍照截屏功能
- Vuforia SDK---- AR开发vuforia 相机自动对焦代码实现
- Vuforia开发入门二-资源介绍
- Vuforia开发基础二-官方demo解析
- Vuforia开发入门(二):资源介绍
- Vuforia开发基础(二):官方demo解析
- Vuforia开发基础二-官方demo解析
- 利用Vuforia开发一个AR卡牌对战(二):主要代码实现
- 【Vuforia】基于Vuforia增强现实开发(二)之模型移动
- 【Vuforia】Vuforia实现自动对焦
- vuforia结合unity3d开发AR的androidAPP总结(二)
- 回溯法经典案例----N皇后
- 2015 Multi-University Training Contest 2 1002 Buildings
- Linux下Arduino IDE无法选择串口的问题
- jQuery-mobile 学习笔记之三(事件监听)
- wpf 关于ContentControl一些记录
- Vuforia开发技巧二-如何实现交互
- 哈希表
- Python 设置系统默认编码
- 阿牛的EOF牛肉串-记忆化搜索或动态规划
- Intent对象
- 质数(素数)判定算法
- Vuforia开发技巧一-提高Target的识别率(第二部分)
- Leetcode 2
- 快排