t1 / TFDContents / Assets / KinectDemos / MovieSequenceDemo / Scripts / UserMovieSequence.cs @ 3
이력 | 보기 | 이력해설 | 다운로드 (2.42 KB)
1 |
using UnityEngine; |
---|---|
2 |
using System; |
3 |
|
4 |
class UserMovieSequence : MonoBehaviour |
5 |
{ |
6 |
[Tooltip("Index of the player, tracked by this component. 0 means the 1st player, 1 - the 2nd one, 2 - the 3rd one, etc.")] |
7 |
public int playerIndex = 0; |
8 |
|
9 |
[Tooltip("How far left or right from the camera may be the user, in meters.")] |
10 |
public float limitLeftRight = 1.2f; |
11 |
|
12 |
[Tooltip("GUI texture to display the movie frames.")] |
13 |
public GUITexture movieGuiTexture = null; |
14 |
|
15 |
[Tooltip("Seuqence of frames in the movie (left to right).")] |
16 |
public Texture[] frameTextures = null; |
17 |
|
18 |
[Tooltip("Smooth factor used for frame interpolation.")] |
19 |
public float smoothFactor = 10f; |
20 |
|
21 |
[Tooltip("Current frame number.")] |
22 |
public int currentFrame = 0; |
23 |
|
24 |
[Tooltip("GUI-Text to display status messages.")] |
25 |
public GUIText statusText = null; |
26 |
|
27 |
|
28 |
private KinectManager kinectManager; |
29 |
private int numberOfFrames; |
30 |
private float fCurrentFrame; |
31 |
|
32 |
|
33 |
|
34 |
void Start() |
35 |
{ |
36 |
kinectManager = KinectManager.Instance; |
37 |
numberOfFrames = frameTextures != null ? frameTextures.Length : 0; |
38 |
fCurrentFrame = 0f; |
39 |
} |
40 |
|
41 |
void Update() |
42 |
{ |
43 |
if (kinectManager && kinectManager.IsInitialized()) |
44 |
{ |
45 |
long userId = kinectManager.GetUserIdByIndex(playerIndex); |
46 |
|
47 |
if (kinectManager.IsUserTracked (userId) && kinectManager.IsJointTracked (userId, (int)KinectInterop.JointType.SpineBase)) |
48 |
{ |
49 |
Vector3 userPos = kinectManager.GetJointPosition (userId, (int)KinectInterop.JointType.SpineBase); |
50 |
|
51 |
if (userPos.x >= -limitLeftRight && userPos.x <= limitLeftRight) |
52 |
{ |
53 |
// calculate the relative position in the movie |
54 |
float relPos = (userPos.x + limitLeftRight) / (2f * limitLeftRight); |
55 |
fCurrentFrame = (fCurrentFrame != 0f) ? Mathf.Lerp (fCurrentFrame, relPos * (numberOfFrames - 1), smoothFactor * Time.deltaTime) : (relPos * (numberOfFrames - 1)); |
56 |
|
57 |
// current frame index |
58 |
currentFrame = Mathf.RoundToInt(fCurrentFrame); |
59 |
|
60 |
if (statusText) |
61 |
{ |
62 |
statusText.text = string.Format ("X-Pos: {0:F2}, RelPos: {1:F3}, Frame: {2}", userPos.x, relPos, currentFrame); |
63 |
} |
64 |
} |
65 |
} |
66 |
// else |
67 |
// { |
68 |
// fCurrentFrame = 0f; |
69 |
// } |
70 |
|
71 |
// display the frame with 'currentFrame' index |
72 |
if(frameTextures != null && currentFrame >= 0 && currentFrame < frameTextures.Length) |
73 |
{ |
74 |
Texture tex = frameTextures[currentFrame]; |
75 |
|
76 |
if (movieGuiTexture) |
77 |
{ |
78 |
movieGuiTexture.texture = tex; |
79 |
} |
80 |
} |
81 |
|
82 |
} |
83 |
} |
84 |
|
85 |
} |