-
Notifications
You must be signed in to change notification settings - Fork 5
/
CaptureButton.cs
82 lines (70 loc) · 2.16 KB
/
CaptureButton.cs
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
using System;
using System.Collections;
using System.Runtime.InteropServices;
using UnityEngine;
public class CaptureButton : MonoBehaviour
{
DepthCapture capture_;
public GameObject quad;
// Use this for initialization
IEnumerator Start()
{
Debug.Log("Start");
if (Application.platform == RuntimePlatform.IPhonePlayer)
{
capture_ = new DepthCapture();
yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
capture_.Configure();
capture_.Start();
}
}
// Update is called once per frame
void Update()
{
}
public void OnClick()
{
Debug.Log("OnClick");
if (Application.platform == RuntimePlatform.IPhonePlayer)
{
int width = 0, height = 0;
float[] pixels = null;
capture_.AcquireNextFrame((pVideoData, videoWidth, videoHeight, pDepthData, depthWidth, depthHeight) =>
{
width = depthWidth;
height = depthHeight;
pixels = new float[width * height];
Marshal.Copy(pDepthData, pixels, 0, width * height);
});
var texture = new Texture2D(width, height);
for (var y = 0; y < (int)height; y++)
{
for (var x = 0; x < (int)width; x++)
{
var v = pixels[y * width + x];
Color color;
if (float.IsNaN(v))
{
color = new Color(0f, 1f, 0f);
}
else
{
color = new Color(v, v, v);
}
texture.SetPixel(x, y, color);
}
}
quad.GetComponent<Renderer>().material.mainTexture = texture;
texture.Apply();
}
}
void OnDestroy()
{
Debug.Log("OnDestroy");
if (Application.platform == RuntimePlatform.IPhonePlayer)
{
capture_.Stop();
capture_.Dispose();
}
}
}