t1 / TFDContents / Assets / KinectDemos / FaceTrackingDemo / Scripts / SetBackgroundImage.cs @ 3
이력 | 보기 | 이력해설 | 다운로드 (2.1 KB)
1 | 3 | KTH | using UnityEngine; |
---|---|---|---|
2 | using System.Collections; |
||
3 | |||
4 | public class SetBackgroundImage : MonoBehaviour |
||
5 | { |
||
6 | [Tooltip("GUI-texture used to display the color camera feed on the scene background.")] |
||
7 | public GUITexture backgroundImage; |
||
8 | |||
9 | [Tooltip("Camera that will be set-up to display 3D-models in the Kinect FOV.")] |
||
10 | public Camera foregroundCamera; |
||
11 | |||
12 | [Tooltip("Use this setting to minimize the offset between the background image and model overlay.")] |
||
13 | [Range(-0.1f, 0.1f)] |
||
14 | public float adjustedCameraOffset = 0f; |
||
15 | |||
16 | |||
17 | // variable to track the current camera offset |
||
18 | private float currentCameraOffset = 0f; |
||
19 | // initial camera position |
||
20 | private Vector3 initialCameraPos = Vector3.zero; |
||
21 | |||
22 | |||
23 | void Start() |
||
24 | { |
||
25 | KinectManager manager = KinectManager.Instance; |
||
26 | |||
27 | if(manager && manager.IsInitialized()) |
||
28 | { |
||
29 | KinectInterop.SensorData sensorData = manager.GetSensorData(); |
||
30 | |||
31 | if(foregroundCamera != null && sensorData != null && sensorData.sensorInterface != null) |
||
32 | { |
||
33 | foregroundCamera.fieldOfView = sensorData.colorCameraFOV; |
||
34 | |||
35 | initialCameraPos = foregroundCamera.transform.position; |
||
36 | Vector3 fgCameraPos = initialCameraPos; |
||
37 | |||
38 | fgCameraPos.x += sensorData.faceOverlayOffset + adjustedCameraOffset; |
||
39 | foregroundCamera.transform.position = fgCameraPos; |
||
40 | currentCameraOffset = adjustedCameraOffset; |
||
41 | } |
||
42 | } |
||
43 | } |
||
44 | |||
45 | void Update() |
||
46 | { |
||
47 | KinectManager manager = KinectManager.Instance; |
||
48 | if(manager && manager.IsInitialized()) |
||
49 | { |
||
50 | if(backgroundImage && (backgroundImage.texture == null)) |
||
51 | { |
||
52 | backgroundImage.texture = manager.GetUsersClrTex(); |
||
53 | } |
||
54 | |||
55 | if(currentCameraOffset != adjustedCameraOffset) |
||
56 | { |
||
57 | // update the camera automatically, according to the current sensor height and angle |
||
58 | KinectInterop.SensorData sensorData = manager.GetSensorData(); |
||
59 | |||
60 | if(foregroundCamera != null && sensorData != null) |
||
61 | { |
||
62 | Vector3 fgCameraPos = initialCameraPos; |
||
63 | fgCameraPos.x += sensorData.faceOverlayOffset + adjustedCameraOffset; |
||
64 | foregroundCamera.transform.position = fgCameraPos; |
||
65 | currentCameraOffset = adjustedCameraOffset; |
||
66 | } |
||
67 | } |
||
68 | } |
||
69 | } |
||
70 | |||
71 | } |