t1 / TFDContents / Assets / KinectDemos / GesturesDemo / Scripts / CubePresentationScript.cs @ 3
이력 | 보기 | 이력해설 | 다운로드 (6.23 KB)
1 | 3 | KTH | using UnityEngine; |
---|---|---|---|
2 | using System.Collections; |
||
3 | using System.Collections.Generic; |
||
4 | |||
5 | public class CubePresentationScript : MonoBehaviour |
||
6 | { |
||
7 | [Tooltip("Camera used for screen-to-world calculations. This is usually the main camera.")] |
||
8 | public Camera screenCamera; |
||
9 | |||
10 | [Tooltip("Whether the presentation slides can be changed with gestures (SwipeLeft & SwipeRight).")] |
||
11 | public bool slideChangeWithGestures = true; |
||
12 | [Tooltip("Whether the presentation slides can be changed with keys (PgDown & PgUp).")] |
||
13 | public bool slideChangeWithKeys = true; |
||
14 | [Tooltip("Speed of spinning, when presentation slides change.")] |
||
15 | public int spinSpeed = 5; |
||
16 | |||
17 | [Tooltip("List of the presentation slides.")] |
||
18 | public List<Texture> slideTextures; |
||
19 | [Tooltip("List of the side planes, comprising the presentation cube.")] |
||
20 | public List<GameObject> cubeSides; |
||
21 | |||
22 | |||
23 | //private int maxSides = 0; |
||
24 | private int maxTextures = 0; |
||
25 | private int side = 0; |
||
26 | private int tex = 0; |
||
27 | private bool isSpinning = false; |
||
28 | |||
29 | private int[] hsides = { 0, 1, 2, 3 }; // left, front, right, back |
||
30 | private int[] vsides = { 4, 1, 5, 3}; // up, front, down, back |
||
31 | |||
32 | private CubeGestureListener gestureListener; |
||
33 | private Quaternion initialRotation; |
||
34 | private int stepsToGo = 0; |
||
35 | |||
36 | private float rotationStep; |
||
37 | private Vector3 rotationAxis; |
||
38 | |||
39 | |||
40 | void Start() |
||
41 | { |
||
42 | // hide mouse cursor |
||
43 | //Cursor.visible = false; |
||
44 | |||
45 | // by default set the main-camera to be screen-camera |
||
46 | if (screenCamera == null) |
||
47 | { |
||
48 | screenCamera = Camera.main; |
||
49 | } |
||
50 | |||
51 | // calculate max slides and textures |
||
52 | //maxSides = cubeSides.Count; |
||
53 | maxTextures = slideTextures.Count; |
||
54 | |||
55 | initialRotation = screenCamera ? Quaternion.Inverse(screenCamera.transform.rotation) * transform.rotation : transform.rotation; |
||
56 | isSpinning = false; |
||
57 | |||
58 | tex = 0; |
||
59 | side = hsides[1]; |
||
60 | |||
61 | if(side < cubeSides.Count && cubeSides[side] && cubeSides[side].GetComponent<Renderer>()) |
||
62 | { |
||
63 | cubeSides[side].GetComponent<Renderer>().material.mainTexture = slideTextures[tex]; |
||
64 | } |
||
65 | |||
66 | // get the gestures listener |
||
67 | gestureListener = CubeGestureListener.Instance; |
||
68 | } |
||
69 | |||
70 | void Update() |
||
71 | { |
||
72 | // dont run Update() if there is no gesture listener |
||
73 | if(!gestureListener) |
||
74 | return; |
||
75 | |||
76 | if(!isSpinning) |
||
77 | { |
||
78 | if(slideChangeWithKeys) |
||
79 | { |
||
80 | if(Input.GetKeyDown(KeyCode.PageDown)) |
||
81 | RotateLeft(); |
||
82 | else if(Input.GetKeyDown(KeyCode.PageUp)) |
||
83 | RotateRight(); |
||
84 | } |
||
85 | |||
86 | if(slideChangeWithGestures && gestureListener) |
||
87 | { |
||
88 | if(gestureListener.IsSwipeLeft()) |
||
89 | RotateLeft(); |
||
90 | else if(gestureListener.IsSwipeRight()) |
||
91 | RotateRight(); |
||
92 | else if(gestureListener.IsSwipeUp()) |
||
93 | RotateUp(); |
||
94 | } |
||
95 | } |
||
96 | else |
||
97 | { |
||
98 | // spin the presentation |
||
99 | if(stepsToGo > 0) |
||
100 | { |
||
101 | //if(Time.realtimeSinceStartup >= nextStepTime) |
||
102 | { |
||
103 | if(screenCamera) |
||
104 | transform.RotateAround(transform.position, screenCamera.transform.TransformDirection(rotationAxis), rotationStep); |
||
105 | else |
||
106 | transform.Rotate(rotationAxis * rotationStep, Space.World); |
||
107 | |||
108 | stepsToGo--; |
||
109 | //nextStepTime = Time.realtimeSinceStartup + Time.deltaTime; |
||
110 | } |
||
111 | } |
||
112 | else |
||
113 | { |
||
114 | Quaternion cubeRotation = Quaternion.Euler(rotationAxis * rotationStep * 90f / spinSpeed) * initialRotation; |
||
115 | transform.rotation = screenCamera ? screenCamera.transform.rotation * cubeRotation : cubeRotation; |
||
116 | isSpinning = false; |
||
117 | } |
||
118 | } |
||
119 | } |
||
120 | |||
121 | // rotates cube left |
||
122 | private void RotateLeft() |
||
123 | { |
||
124 | // set the next texture slide |
||
125 | tex = (tex + 1) % maxTextures; |
||
126 | |||
127 | // rotate hsides left |
||
128 | SetSides(ref hsides, hsides[1], hsides[2], hsides[3], hsides[0]); |
||
129 | SetSides(ref vsides, -1, hsides[1], -1, hsides[3]); |
||
130 | side = hsides[1]; |
||
131 | |||
132 | // set the slide on the selected side |
||
133 | if(side < cubeSides.Count && cubeSides[side] && cubeSides[side].GetComponent<Renderer>()) |
||
134 | { |
||
135 | cubeSides[side].GetComponent<Renderer>().material.mainTexture = slideTextures[tex]; |
||
136 | } |
||
137 | |||
138 | // rotate the presentation |
||
139 | isSpinning = true; |
||
140 | initialRotation = screenCamera ? Quaternion.Inverse(screenCamera.transform.rotation) * transform.rotation : transform.rotation; |
||
141 | |||
142 | rotationStep = spinSpeed; // new Vector3(0, spinSpeed, 0); |
||
143 | rotationAxis = Vector3.up; |
||
144 | |||
145 | stepsToGo = 90 / spinSpeed; |
||
146 | //nextStepTime = 0f; |
||
147 | } |
||
148 | |||
149 | // rotates cube right |
||
150 | private void RotateRight() |
||
151 | { |
||
152 | // set the previous texture slide |
||
153 | if(tex <= 0) |
||
154 | tex = maxTextures - 1; |
||
155 | else |
||
156 | tex -= 1; |
||
157 | |||
158 | // rotate hsides right |
||
159 | SetSides(ref hsides, hsides[3], hsides[0], hsides[1], hsides[2]); |
||
160 | SetSides(ref vsides, -1, hsides[1], -1, hsides[3]); |
||
161 | side = hsides[1]; |
||
162 | |||
163 | // set the slide on the selected side |
||
164 | if(side < cubeSides.Count && cubeSides[side] && cubeSides[side].GetComponent<Renderer>()) |
||
165 | { |
||
166 | cubeSides[side].GetComponent<Renderer>().material.mainTexture = slideTextures[tex]; |
||
167 | } |
||
168 | |||
169 | // rotate the presentation |
||
170 | isSpinning = true; |
||
171 | initialRotation = screenCamera ? Quaternion.Inverse(screenCamera.transform.rotation) * transform.rotation : transform.rotation; |
||
172 | |||
173 | rotationStep = -spinSpeed; // new Vector3(0, -spinSpeed, 0); |
||
174 | rotationAxis = Vector3.up; |
||
175 | |||
176 | stepsToGo = 90 / spinSpeed; |
||
177 | //nextStepTime = 0f; |
||
178 | } |
||
179 | |||
180 | // rotates cube up |
||
181 | private void RotateUp() |
||
182 | { |
||
183 | // set the next texture slide |
||
184 | tex = (tex + 1) % maxTextures; |
||
185 | |||
186 | // rotate vsides up |
||
187 | SetSides(ref vsides, vsides[1], vsides[2], vsides[3], vsides[0]); |
||
188 | SetSides(ref hsides, -1, vsides[1], -1, vsides[3]); |
||
189 | side = hsides[1]; |
||
190 | |||
191 | // set the slide on the selected side |
||
192 | if(side < cubeSides.Count && cubeSides[side] && cubeSides[side].GetComponent<Renderer>()) |
||
193 | { |
||
194 | cubeSides[side].GetComponent<Renderer>().material.mainTexture = slideTextures[tex]; |
||
195 | } |
||
196 | |||
197 | // rotate the presentation |
||
198 | isSpinning = true; |
||
199 | initialRotation = screenCamera ? Quaternion.Inverse(screenCamera.transform.rotation) * transform.rotation : transform.rotation; |
||
200 | |||
201 | rotationStep = spinSpeed; // new Vector3(spinSpeed, 0, 0); |
||
202 | rotationAxis = Vector3.right; |
||
203 | |||
204 | stepsToGo = 90 / spinSpeed; |
||
205 | //nextStepTime = 0f; |
||
206 | } |
||
207 | |||
208 | // sets values of sides' array |
||
209 | private void SetSides(ref int[] sides, int v0, int v1, int v2, int v3) |
||
210 | { |
||
211 | if(v0 >= 0) |
||
212 | { |
||
213 | sides[0] = v0; |
||
214 | } |
||
215 | |||
216 | if(v1 >= 0) |
||
217 | { |
||
218 | sides[1] = v1; |
||
219 | } |
||
220 | |||
221 | if(v2 >= 0) |
||
222 | { |
||
223 | sides[2] = v2; |
||
224 | } |
||
225 | |||
226 | if(v3 >= 0) |
||
227 | { |
||
228 | sides[3] = v3; |
||
229 | } |
||
230 | } |
||
231 | |||
232 | } |