t1 / TFDContents / Assets / KinectDemos / VisualizerDemo / Scripts / MousePointOverlayer.cs @ 3
이력 | 보기 | 이력해설 | 다운로드 (3.36 KB)
1 | 3 | KTH | using UnityEngine; |
---|---|---|---|
2 | using System.Collections; |
||
3 | |||
4 | public class MousePointOverlayer : MonoBehaviour |
||
5 | { |
||
6 | [Tooltip("Whether to wait for mouse click or not.")] |
||
7 | public bool waitForClick = true; |
||
8 | |||
9 | [Tooltip("Game object used to overlay the mouse pointed position.")] |
||
10 | public Transform overlayObject; |
||
11 | |||
12 | [Tooltip("Camera that may be used to overlay the mesh over the color background.")] |
||
13 | public Camera foregroundCamera; |
||
14 | |||
15 | [Tooltip("GUI-Text used to display information messages.")] |
||
16 | public GUIText infoText; |
||
17 | |||
18 | |||
19 | // reference to the singleton instance of KM |
||
20 | private KinectManager kinectManager; |
||
21 | |||
22 | // sensor color-image width & height |
||
23 | private int colorWidth = 0; |
||
24 | private int colorHeight = 0; |
||
25 | |||
26 | // last mouse position |
||
27 | private Vector2 lastMousePos; |
||
28 | // screen rectangle |
||
29 | private Rect backgroundRect; |
||
30 | |||
31 | |||
32 | void Start () |
||
33 | { |
||
34 | // by default set the main camera as foreground-camera |
||
35 | if (foregroundCamera == null) |
||
36 | { |
||
37 | foregroundCamera = Camera.main; |
||
38 | } |
||
39 | |||
40 | // get the singleton instance of KM |
||
41 | kinectManager = KinectManager.Instance; |
||
42 | |||
43 | // get color-image resolution |
||
44 | if (kinectManager && kinectManager.IsInitialized()) |
||
45 | { |
||
46 | colorWidth = kinectManager.GetColorImageWidth(); |
||
47 | colorHeight = kinectManager.GetColorImageHeight(); |
||
48 | } |
||
49 | |||
50 | // estimate the background rectangle |
||
51 | backgroundRect = foregroundCamera ? foregroundCamera.pixelRect : new Rect(0, 0, Screen.width, Screen.height); |
||
52 | } |
||
53 | |||
54 | void Update () |
||
55 | { |
||
56 | // get mouse button state and position |
||
57 | bool bMouseClicked = waitForClick ? Input.GetMouseButtonDown(0) : true; |
||
58 | Vector2 mousePos = Input.mousePosition; |
||
59 | |||
60 | if (kinectManager && kinectManager.IsInitialized() && overlayObject && |
||
61 | bMouseClicked && mousePos.x >= 0 && mousePos.y >= 0 && mousePos.x < Screen.width && mousePos.y < Screen.height |
||
62 | && mousePos != lastMousePos) |
||
63 | { |
||
64 | lastMousePos = mousePos; |
||
65 | |||
66 | // screen position |
||
67 | Vector2 screenPos = mousePos; |
||
68 | |||
69 | // update the background rectangle with the portrait background, if available |
||
70 | PortraitBackground portraitBack = PortraitBackground.Instance; |
||
71 | if(portraitBack && portraitBack.enabled) |
||
72 | { |
||
73 | backgroundRect = portraitBack.GetBackgroundRect(); |
||
74 | } |
||
75 | |||
76 | // convert to color image rectangle |
||
77 | float colorX = (screenPos.x - backgroundRect.x) * (float)colorWidth / backgroundRect.width; |
||
78 | float colorY = (backgroundRect.y + (backgroundRect.height - screenPos.y)) * (float)colorHeight / backgroundRect.height; |
||
79 | Vector2 colorPos = new Vector2(colorX, colorY); |
||
80 | |||
81 | // get the respective depth image pos |
||
82 | Vector2 depthPos = kinectManager.MapColorPointToDepthCoords(colorPos, true); |
||
83 | |||
84 | if (depthPos != Vector2.zero) |
||
85 | { |
||
86 | // get the depth in mm |
||
87 | ushort depthValue = kinectManager.GetDepthForPixel((int)depthPos.x, (int)depthPos.y); |
||
88 | |||
89 | // get the space position in world coordinates |
||
90 | Vector3 worldPos = foregroundCamera ? foregroundCamera.ScreenToWorldPoint(new Vector3(screenPos.x, screenPos.y, (float)depthValue / 1000f)) : |
||
91 | kinectManager.MapDepthPointToSpaceCoords(depthPos, depthValue, true); |
||
92 | |||
93 | // set the overlay object's position |
||
94 | if (!float.IsNaN(worldPos.x) && !float.IsNaN(worldPos.y) && !float.IsNaN(worldPos.z)) |
||
95 | { |
||
96 | overlayObject.position = worldPos; |
||
97 | |||
98 | if (infoText) |
||
99 | { |
||
100 | infoText.text = string.Format("Pos: ({0:F2}, {1:F2}, {2:F2})", worldPos.x, worldPos.y, worldPos.z); |
||
101 | } |
||
102 | } |
||
103 | } |
||
104 | |||
105 | } |
||
106 | } |
||
107 | |||
108 | } |