t1 / TFDContents / Assets / KinectDemos / BackgroundRemovalDemo / Scripts / WebcamSource.cs @ 3
이력 | 보기 | 이력해설 | 다운로드 (1.73 KB)
1 | 3 | KTH | using UnityEngine; |
---|---|---|---|
2 | using System.Collections; |
||
3 | using System.Text; |
||
4 | |||
5 | public class WebcamSource : MonoBehaviour |
||
6 | { |
||
7 | [Tooltip("Selected web-camera name. If left empty, the first available web camera will be selected.")] |
||
8 | public string webcamName; |
||
9 | |||
10 | [Tooltip("Whether the webcam image needs to be flipped horizontally.")] |
||
11 | public bool flipHorizontally = true; |
||
12 | |||
13 | |||
14 | // the web-camera texture |
||
15 | private WebCamTexture webcamTex; |
||
16 | private bool bTexAspectSet = false; |
||
17 | |||
18 | void Start () |
||
19 | { |
||
20 | if(string.IsNullOrEmpty(webcamName)) |
||
21 | { |
||
22 | // get available webcams |
||
23 | WebCamDevice[] devices = WebCamTexture.devices; |
||
24 | |||
25 | if(devices != null && devices.Length > 0) |
||
26 | { |
||
27 | // print available webcams |
||
28 | StringBuilder sbWebcams = new StringBuilder(); |
||
29 | sbWebcams.Append("Available webcams:").AppendLine(); |
||
30 | |||
31 | foreach(WebCamDevice device in devices) |
||
32 | { |
||
33 | sbWebcams.Append(device.name).AppendLine(); |
||
34 | } |
||
35 | |||
36 | Debug.Log(sbWebcams.ToString()); |
||
37 | |||
38 | // get the 1st webcam name |
||
39 | webcamName = devices[0].name; |
||
40 | } |
||
41 | } |
||
42 | |||
43 | // create the texture |
||
44 | if(!string.IsNullOrEmpty(webcamName)) |
||
45 | { |
||
46 | webcamTex = new WebCamTexture(webcamName.Trim()); |
||
47 | } |
||
48 | |||
49 | // set the texture |
||
50 | Renderer renderer = GetComponent<Renderer>(); |
||
51 | if(renderer) |
||
52 | { |
||
53 | renderer.material.mainTexture = webcamTex; |
||
54 | } |
||
55 | |||
56 | if(webcamTex && !string.IsNullOrEmpty(webcamTex.deviceName)) |
||
57 | { |
||
58 | webcamTex.Play(); |
||
59 | } |
||
60 | } |
||
61 | |||
62 | void Update() |
||
63 | { |
||
64 | if(!bTexAspectSet) |
||
65 | { |
||
66 | Vector3 localScale = transform.localScale; |
||
67 | localScale.x = (float)webcamTex.width * localScale.y / (float)webcamTex.height; |
||
68 | localScale.x = (!flipHorizontally ? localScale.x : -localScale.x); |
||
69 | transform.localScale = localScale; |
||
70 | |||
71 | bTexAspectSet = true; |
||
72 | } |
||
73 | } |
||
74 | |||
75 | } |