forked from homuler/MediaPipeUnityPlugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebCamScreenController.cs
More file actions
95 lines (77 loc) · 2.73 KB
/
Copy pathWebCamScreenController.cs
File metadata and controls
95 lines (77 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using System;
using UnityEngine;
public class WebCamScreenController : MonoBehaviour {
[SerializeField] readonly int Width = 640;
[SerializeField] readonly int Height = 480;
[SerializeField] readonly int FPS = 30;
[SerializeField] readonly float FocalLengthPx = 2.0f; /// TODO: calculate it from webCamDevice info if possible.
private const int TEXTURE_SIZE_THRESHOLD = 50;
private WebCamDevice webCamDevice;
private WebCamTexture webCamTexture;
private Texture2D outputTexture;
private Color32[] pixelData;
public bool isPlaying {
get { return isWebCamTextureInitialized && webCamTexture.isPlaying; }
}
private bool isWebCamTextureInitialized {
get {
// Some cameras may take time to be initialized, so check the texture size.
return webCamTexture != null && webCamTexture.width > TEXTURE_SIZE_THRESHOLD;
}
}
/// TODO: use Coroutine and call InitScreen here
public void ResetScreen(WebCamDevice? device) {
if (isPlaying) {
webCamTexture.Stop();
webCamTexture = null;
}
if (device is WebCamDevice deviceValue) {
webCamDevice = deviceValue;
} else {
return;
}
/// TODO: call Application.RequestUserAuthorization
webCamTexture = new WebCamTexture(webCamDevice.name, Width, Height, FPS);
WebCamTextureFramePool.Instance.SetDimension(Width, Height);
try {
webCamTexture.Play();
Debug.Log($"WebCamTexture Graphics Format: {webCamTexture.graphicsFormat}");
} catch (Exception e) {
Debug.LogWarning(e.ToString());
return;
}
}
public void InitScreen() {
Renderer renderer = GetComponent<Renderer>();
outputTexture = new Texture2D(webCamTexture.width, webCamTexture.height, TextureFormat.RGBA32, false);
renderer.material.mainTexture = outputTexture;
pixelData = new Color32[webCamTexture.width * webCamTexture.height];
}
public float GetFocalLengthPx() {
return isPlaying ? FocalLengthPx : 0;
}
public Color32[] GetPixels32() {
return isPlaying ? webCamTexture.GetPixels32(pixelData) : null;
}
public IntPtr GetNativeTexturePtr() {
return webCamTexture.GetNativeTexturePtr();
}
public Texture2D GetScreen() {
return outputTexture;
}
public void DrawScreen(Color32[] colors) {
// TODO: size assertion
outputTexture.SetPixels32(colors);
outputTexture.Apply();
}
public void DrawScreen(TextureFrame src) {
// TODO: size assertion
src.CopyTexture(outputTexture);
}
public TextureFramePool.TextureFrameRequest RequestNextFrame() {
return WebCamTextureFramePool.Instance.RequestNextTextureFrame((TextureFrame textureFrame) => {
textureFrame.CopyTextureFrom(webCamTexture);
});
}
private class WebCamTextureFramePool : TextureFramePool {}
}