t1 / TFDContents / Assets / KinectDemos / GesturesDemo / Scripts / ModelPresentationScript.cs @ 3
이력 | 보기 | 이력해설 | 다운로드 (2.12 KB)
1 |
using UnityEngine; |
---|---|
2 |
using System.Collections; |
3 |
using System.Collections.Generic; |
4 |
|
5 |
public class ModelPresentationScript : MonoBehaviour |
6 |
{ |
7 |
[Tooltip("Camera used for screen-to-world calculations. This is usually the main camera.")] |
8 |
public Camera screenCamera; |
9 |
|
10 |
[Tooltip("Speed of spinning, when presentation slides change.")] |
11 |
public float spinSpeed = 10; |
12 |
|
13 |
// reference to the gesture listener |
14 |
private ModelGestureListener gestureListener; |
15 |
|
16 |
// model's initial rotation |
17 |
private Quaternion initialRotation; |
18 |
|
19 |
|
20 |
void Start() |
21 |
{ |
22 |
// hide mouse cursor |
23 |
//Cursor.visible = false; |
24 |
|
25 |
// by default set the main-camera to be screen-camera |
26 |
if (screenCamera == null) |
27 |
{ |
28 |
screenCamera = Camera.main; |
29 |
} |
30 |
|
31 |
// get model initial rotation |
32 |
initialRotation = screenCamera ? Quaternion.Inverse(screenCamera.transform.rotation) * transform.rotation : transform.rotation; |
33 |
|
34 |
// get the gestures listener |
35 |
gestureListener = ModelGestureListener.Instance; |
36 |
} |
37 |
|
38 |
void Update() |
39 |
{ |
40 |
// dont run Update() if there is no gesture listener |
41 |
if(!gestureListener) |
42 |
return; |
43 |
|
44 |
if(gestureListener.IsZoomingIn() || gestureListener.IsZoomingOut()) |
45 |
{ |
46 |
// zoom the model |
47 |
float zoomFactor = gestureListener.GetZoomFactor(); |
48 |
|
49 |
Vector3 newLocalScale = new Vector3(zoomFactor, zoomFactor, zoomFactor); |
50 |
transform.localScale = Vector3.Lerp(transform.localScale, newLocalScale, spinSpeed * Time.deltaTime); |
51 |
} |
52 |
|
53 |
if(gestureListener.IsTurningWheel()) |
54 |
{ |
55 |
// rotate the model |
56 |
float turnAngle = Mathf.Clamp(gestureListener.GetWheelAngle(), -30f, 30f); |
57 |
float updateAngle = Mathf.Lerp(0, turnAngle, spinSpeed * Time.deltaTime); |
58 |
|
59 |
if(screenCamera) |
60 |
transform.RotateAround(transform.position, screenCamera.transform.TransformDirection(Vector3.up), updateAngle); |
61 |
else |
62 |
transform.Rotate(Vector3.up * turnAngle, Space.World); |
63 |
} |
64 |
|
65 |
if(gestureListener.IsRaiseHand()) |
66 |
{ |
67 |
// reset the model |
68 |
Vector3 newLocalScale = Vector3.one; |
69 |
transform.localScale = newLocalScale; |
70 |
|
71 |
transform.rotation = screenCamera ? screenCamera.transform.rotation * initialRotation : initialRotation; |
72 |
} |
73 |
|
74 |
} |
75 |
|
76 |
} |