t1 / TFDContents / Assets / KinectDemos / OverlayDemo / Scripts / JointOverlayer.cs @ 3
이력 | 보기 | 이력해설 | 다운로드 (3.24 KB)
1 | 3 | KTH | using UnityEngine; |
---|---|---|---|
2 | using System.Collections; |
||
3 | using System; |
||
4 | //using Windows.Kinect; |
||
5 | |||
6 | |||
7 | public class JointOverlayer : MonoBehaviour |
||
8 | { |
||
9 | [Tooltip("GUI-texture used to display the color camera feed on the scene background.")] |
||
10 | public GUITexture backgroundImage; |
||
11 | |||
12 | [Tooltip("Camera that will be used to overlay the 3D-objects over the background.")] |
||
13 | public Camera foregroundCamera; |
||
14 | |||
15 | [Tooltip("Index of the player, tracked by this component. 0 means the 1st player, 1 - the 2nd one, 2 - the 3rd one, etc.")] |
||
16 | public int playerIndex = 0; |
||
17 | |||
18 | [Tooltip("Kinect joint that is going to be overlayed.")] |
||
19 | public KinectInterop.JointType trackedJoint = KinectInterop.JointType.HandRight; |
||
20 | |||
21 | [Tooltip("Game object used to overlay the joint.")] |
||
22 | public Transform overlayObject; |
||
23 | |||
24 | [Tooltip("Smoothing factor used for joint rotation.")] |
||
25 | public float smoothFactor = 10f; |
||
26 | |||
27 | //public GUIText debugText; |
||
28 | |||
29 | [NonSerialized] |
||
30 | public Quaternion initialRotation = Quaternion.identity; |
||
31 | private bool objFlipped = false; |
||
32 | |||
33 | |||
34 | public void Start() |
||
35 | { |
||
36 | if (!foregroundCamera) |
||
37 | { |
||
38 | // by default - the main camera |
||
39 | foregroundCamera = Camera.main; |
||
40 | } |
||
41 | |||
42 | if(overlayObject) |
||
43 | { |
||
44 | // always mirrored |
||
45 | initialRotation = overlayObject.rotation; // Quaternion.Euler(new Vector3(0f, 180f, 0f)); |
||
46 | |||
47 | Vector3 vForward = foregroundCamera ? foregroundCamera.transform.forward : Vector3.forward; |
||
48 | objFlipped = (Vector3.Dot(overlayObject.forward, vForward) < 0); |
||
49 | |||
50 | overlayObject.rotation = Quaternion.identity; |
||
51 | } |
||
52 | } |
||
53 | |||
54 | void Update () |
||
55 | { |
||
56 | KinectManager manager = KinectManager.Instance; |
||
57 | |||
58 | if(manager && manager.IsInitialized() && foregroundCamera) |
||
59 | { |
||
60 | //backgroundImage.renderer.material.mainTexture = manager.GetUsersClrTex(); |
||
61 | if(backgroundImage && (backgroundImage.texture == null)) |
||
62 | { |
||
63 | backgroundImage.texture = manager.GetUsersClrTex(); |
||
64 | } |
||
65 | |||
66 | // get the background rectangle (use the portrait background, if available) |
||
67 | Rect backgroundRect = foregroundCamera.pixelRect; |
||
68 | PortraitBackground portraitBack = PortraitBackground.Instance; |
||
69 | |||
70 | if(portraitBack && portraitBack.enabled) |
||
71 | { |
||
72 | backgroundRect = portraitBack.GetBackgroundRect(); |
||
73 | } |
||
74 | |||
75 | // overlay the joint |
||
76 | long userId = manager.GetUserIdByIndex(playerIndex); |
||
77 | |||
78 | int iJointIndex = (int)trackedJoint; |
||
79 | if (manager.IsJointTracked (userId, iJointIndex)) |
||
80 | { |
||
81 | Vector3 posJoint = manager.GetJointPosColorOverlay(userId, iJointIndex, foregroundCamera, backgroundRect); |
||
82 | |||
83 | if (posJoint != Vector3.zero) |
||
84 | { |
||
85 | // if(debugText) |
||
86 | // { |
||
87 | // debugText.text = string.Format("{0} - {1}", trackedJoint, posJoint); |
||
88 | // } |
||
89 | |||
90 | if (overlayObject) |
||
91 | { |
||
92 | overlayObject.position = posJoint; |
||
93 | |||
94 | Quaternion rotJoint = manager.GetJointOrientation (userId, iJointIndex, !objFlipped); |
||
95 | rotJoint = initialRotation * rotJoint; |
||
96 | |||
97 | overlayObject.rotation = Quaternion.Slerp (overlayObject.rotation, rotJoint, smoothFactor * Time.deltaTime); |
||
98 | } |
||
99 | } |
||
100 | } |
||
101 | else |
||
102 | { |
||
103 | // make the overlay object invisible |
||
104 | if (overlayObject && overlayObject.position.z > 0f) |
||
105 | { |
||
106 | Vector3 posJoint = overlayObject.position; |
||
107 | posJoint.z = -10f; |
||
108 | overlayObject.position = posJoint; |
||
109 | } |
||
110 | } |
||
111 | |||
112 | } |
||
113 | } |
||
114 | } |