t1 / TFDContents / Assets / KinectDemos / OverlayDemo / Scripts / HandOverlayer.cs @ 3
이력 | 보기 | 이력해설 | 다운로드 (4.92 KB)
1 | 3 | KTH | using UnityEngine; |
---|---|---|---|
2 | using System.Collections; |
||
3 | //using Windows.Kinect; |
||
4 | |||
5 | |||
6 | public class HandOverlayer : MonoBehaviour |
||
7 | { |
||
8 | [Tooltip("GUI-texture used to display the color camera feed on the scene background.")] |
||
9 | public GUITexture backgroundImage; |
||
10 | |||
11 | [Tooltip("Index of the player, tracked by this component. 0 means the 1st player, 1 - the 2nd one, 2 - the 3rd one, etc.")] |
||
12 | public int playerIndex = 0; |
||
13 | |||
14 | [Tooltip("Whether the player is left handed or not.")] |
||
15 | public bool isLeftHanded = false; |
||
16 | |||
17 | [Tooltip("Hand-cursor texture for the hand-grip state.")] |
||
18 | public Texture gripHandTexture; |
||
19 | [Tooltip("Hand-cursor texture for the hand-release state.")] |
||
20 | public Texture releaseHandTexture; |
||
21 | [Tooltip("Hand-cursor texture for the non-tracked state.")] |
||
22 | public Texture normalHandTexture; |
||
23 | |||
24 | [Tooltip("Smooth factor for cursor movement.")] |
||
25 | public float smoothFactor = 10f; |
||
26 | |||
27 | // current cursor position |
||
28 | private Vector2 cursorPos = Vector2.zero; |
||
29 | |||
30 | // last hand event (grip or release) |
||
31 | private InteractionManager.HandEventType lastHandEvent = InteractionManager.HandEventType.None; |
||
32 | |||
33 | |||
34 | /// <summary> |
||
35 | /// Gets the cursor position. |
||
36 | /// </summary> |
||
37 | /// <returns>The cursor position.</returns> |
||
38 | public Vector2 GetCursorPos() |
||
39 | { |
||
40 | return cursorPos; |
||
41 | } |
||
42 | |||
43 | |||
44 | /// <summary> |
||
45 | /// Gets the last hand event of the active hand (right or left). |
||
46 | /// </summary> |
||
47 | /// <returns>The last hand event.</returns> |
||
48 | public InteractionManager.HandEventType GetLastHandEvent() |
||
49 | { |
||
50 | return lastHandEvent; |
||
51 | } |
||
52 | |||
53 | |||
54 | // ----- end of public functions ----- |
||
55 | |||
56 | |||
57 | void Update () |
||
58 | { |
||
59 | KinectManager manager = KinectManager.Instance; |
||
60 | if(manager && manager.IsInitialized()) |
||
61 | { |
||
62 | //backgroundImage.renderer.material.mainTexture = manager.GetUsersClrTex(); |
||
63 | if(backgroundImage && (backgroundImage.texture == null)) |
||
64 | { |
||
65 | backgroundImage.texture = manager.GetUsersClrTex(); |
||
66 | } |
||
67 | |||
68 | // overlay the joint |
||
69 | int iJointIndex = !isLeftHanded ? (int)KinectInterop.JointType.HandRight : (int)KinectInterop.JointType.HandLeft; |
||
70 | |||
71 | if(manager.IsUserDetected(playerIndex)) |
||
72 | { |
||
73 | long userId = manager.GetUserIdByIndex(playerIndex); |
||
74 | |||
75 | if(manager.IsJointTracked(userId, iJointIndex)) |
||
76 | { |
||
77 | Vector3 posJointRaw = manager.GetJointKinectPosition(userId, iJointIndex); |
||
78 | //Vector3 posJoint = manager.GetJointPosColorOverlay(userId, iJointIndex, Camera.main, Camera.main.pixelRect); |
||
79 | |||
80 | if(posJointRaw != Vector3.zero) |
||
81 | { |
||
82 | Vector2 posDepth = manager.MapSpacePointToDepthCoords(posJointRaw); |
||
83 | ushort depthValue = manager.GetDepthForPixel((int)posDepth.x, (int)posDepth.y); |
||
84 | |||
85 | if(posDepth != Vector2.zero && depthValue > 0) |
||
86 | { |
||
87 | // depth pos to color pos |
||
88 | Vector2 posColor = manager.MapDepthPointToColorCoords(posDepth, depthValue); |
||
89 | |||
90 | if(!float.IsInfinity(posColor.x) && !float.IsInfinity(posColor.y)) |
||
91 | { |
||
92 | // get the color image x-offset and width (use the portrait background, if available) |
||
93 | float colorWidth = manager.GetColorImageWidth(); |
||
94 | float colorOfsX = 0f; |
||
95 | |||
96 | PortraitBackground portraitBack = PortraitBackground.Instance; |
||
97 | if(portraitBack && portraitBack.enabled) |
||
98 | { |
||
99 | colorWidth = manager.GetColorImageHeight() * manager.GetColorImageHeight() / manager.GetColorImageWidth(); |
||
100 | colorOfsX = (manager.GetColorImageWidth() - colorWidth) / 2f; |
||
101 | } |
||
102 | |||
103 | float xScaled = (posColor.x - colorOfsX) / colorWidth; |
||
104 | float yScaled = posColor.y / manager.GetColorImageHeight(); |
||
105 | |||
106 | cursorPos = Vector2.Lerp(cursorPos, new Vector2(xScaled, 1f - yScaled), smoothFactor * Time.deltaTime); |
||
107 | } |
||
108 | } |
||
109 | } |
||
110 | } |
||
111 | |||
112 | } |
||
113 | |||
114 | } |
||
115 | } |
||
116 | |||
117 | void OnGUI() |
||
118 | { |
||
119 | InteractionManager intManager = InteractionManager.Instance; |
||
120 | Texture texture = null; |
||
121 | |||
122 | if(intManager && intManager.IsInteractionInited()) |
||
123 | { |
||
124 | if(isLeftHanded) |
||
125 | { |
||
126 | lastHandEvent = intManager.GetLastLeftHandEvent(); |
||
127 | |||
128 | if(lastHandEvent == InteractionManager.HandEventType.Grip) |
||
129 | texture = gripHandTexture; |
||
130 | else if(lastHandEvent == InteractionManager.HandEventType.Release) |
||
131 | texture = releaseHandTexture; |
||
132 | } |
||
133 | else |
||
134 | { |
||
135 | lastHandEvent = intManager.GetLastRightHandEvent(); |
||
136 | |||
137 | if(lastHandEvent == InteractionManager.HandEventType.Grip) |
||
138 | texture = gripHandTexture; |
||
139 | else if(lastHandEvent == InteractionManager.HandEventType.Release) |
||
140 | texture = releaseHandTexture; |
||
141 | } |
||
142 | } |
||
143 | |||
144 | if(texture == null) |
||
145 | { |
||
146 | texture = normalHandTexture; |
||
147 | } |
||
148 | |||
149 | if((cursorPos != Vector2.zero) && (texture != null)) |
||
150 | { |
||
151 | //handCursor.transform.position = cursorScreenPos; // Vector3.Lerp(handCursor.transform.position, cursorScreenPos, 3 * Time.deltaTime); |
||
152 | Rect rectTexture = new Rect(cursorPos.x * Screen.width - texture.width / 2, (1f - cursorPos.y) * Screen.height - texture.height / 2, |
||
153 | texture.width, texture.height); |
||
154 | GUI.DrawTexture(rectTexture, texture); |
||
155 | } |
||
156 | } |
||
157 | |||
158 | |||
159 | } |