2024-01-27 15:39:10 +00:00
|
|
|
using System.Linq;
|
2024-01-27 10:26:34 +00:00
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.UI;
|
|
|
|
|
|
|
|
public class CameraManager : MonoBehaviour
|
|
|
|
{
|
2024-01-27 15:39:10 +00:00
|
|
|
public WebCamDevice wDevice;
|
2024-01-27 12:39:18 +00:00
|
|
|
public Button freezeButton;
|
|
|
|
public Button resumeButton;
|
2024-01-27 23:04:18 +00:00
|
|
|
public RawImage photoBox;
|
2024-01-27 16:54:03 +00:00
|
|
|
private Texture2D photo;
|
2024-01-28 01:59:30 +00:00
|
|
|
private int numberOfWebcams;
|
2024-01-28 20:53:27 +00:00
|
|
|
private WebCamTexture wTexture;
|
2024-01-27 11:31:58 +00:00
|
|
|
|
2024-01-27 10:26:34 +00:00
|
|
|
// Start is called before the first frame update
|
2024-01-27 17:09:40 +00:00
|
|
|
void OnEnable()
|
2024-01-27 10:26:34 +00:00
|
|
|
{
|
2024-01-27 12:39:18 +00:00
|
|
|
WebcamResume();
|
2024-01-27 10:26:34 +00:00
|
|
|
}
|
|
|
|
|
2024-01-27 17:09:40 +00:00
|
|
|
void OnDisable()
|
|
|
|
{
|
|
|
|
wTexture = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Start()
|
|
|
|
{
|
|
|
|
wDevice = WebCamTexture.devices.FirstOrDefault(x => x.isFrontFacing == false);
|
2024-01-28 01:59:30 +00:00
|
|
|
numberOfWebcams = WebCamTexture.devices.Count();
|
2024-01-27 17:09:40 +00:00
|
|
|
}
|
|
|
|
|
2024-01-27 10:26:34 +00:00
|
|
|
// Update is called once per frame
|
|
|
|
void Update()
|
|
|
|
{
|
2024-01-28 01:48:34 +00:00
|
|
|
Texture2D texture = GetPhoto();
|
|
|
|
|
|
|
|
if (texture)
|
2024-01-27 23:21:38 +00:00
|
|
|
{
|
2024-01-28 01:48:34 +00:00
|
|
|
photoBox.texture = texture;
|
2024-01-27 23:21:38 +00:00
|
|
|
}
|
2024-01-28 01:48:34 +00:00
|
|
|
|
|
|
|
Resources.UnloadUnusedAssets();
|
2024-01-27 10:26:34 +00:00
|
|
|
}
|
2024-01-27 12:39:18 +00:00
|
|
|
|
|
|
|
public void WebcamResume()
|
|
|
|
{
|
2024-01-28 20:53:27 +00:00
|
|
|
if (wTexture == null)
|
|
|
|
wTexture = new WebCamTexture(wDevice.name);
|
|
|
|
|
2024-01-27 15:39:10 +00:00
|
|
|
wTexture.Play();
|
2024-01-27 16:54:03 +00:00
|
|
|
photo = null;
|
2024-02-04 11:04:24 +00:00
|
|
|
photoBox.texture = null;
|
2024-01-27 12:39:18 +00:00
|
|
|
freezeButton.gameObject.SetActive(true);
|
|
|
|
resumeButton.gameObject.SetActive(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void WebcamChange()
|
|
|
|
{
|
2024-01-27 15:39:10 +00:00
|
|
|
foreach (WebCamDevice loopDevice in WebCamTexture.devices)
|
2024-01-27 12:39:18 +00:00
|
|
|
{
|
2024-01-27 15:39:10 +00:00
|
|
|
if ((wDevice.isFrontFacing && !loopDevice.isFrontFacing) || (!wDevice.isFrontFacing && loopDevice.isFrontFacing))
|
2024-01-27 12:39:18 +00:00
|
|
|
{
|
2024-01-27 15:39:10 +00:00
|
|
|
wDevice = loopDevice;
|
|
|
|
wTexture = new WebCamTexture(wDevice.name);
|
2024-01-27 12:39:18 +00:00
|
|
|
WebcamResume();
|
2024-01-27 15:39:10 +00:00
|
|
|
break;
|
2024-01-27 12:39:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void WebcamStop()
|
|
|
|
{
|
2024-01-27 16:54:03 +00:00
|
|
|
photo = GetPhoto();
|
2024-01-27 15:39:10 +00:00
|
|
|
wTexture.Stop();
|
2024-01-27 12:39:18 +00:00
|
|
|
freezeButton.gameObject.SetActive(false);
|
|
|
|
resumeButton.gameObject.SetActive(true);
|
|
|
|
}
|
2024-01-27 15:39:10 +00:00
|
|
|
|
|
|
|
Texture2D RotateTexture(Texture2D originalTexture, bool clockwise)
|
|
|
|
{
|
|
|
|
Color32[] original = originalTexture.GetPixels32();
|
|
|
|
Color32[] rotated = new Color32[original.Length];
|
|
|
|
int w = originalTexture.width;
|
|
|
|
int h = originalTexture.height;
|
|
|
|
|
|
|
|
int iRotated, iOriginal;
|
|
|
|
|
|
|
|
for (int j = 0; j < h; ++j)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < w; ++i)
|
|
|
|
{
|
|
|
|
iRotated = (i + 1) * h - j - 1;
|
|
|
|
iOriginal = clockwise ? original.Length - 1 - (j * w + i) : j * w + i;
|
|
|
|
rotated[iRotated] = original[iOriginal];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Texture2D rotatedTexture = new(h, w);
|
|
|
|
rotatedTexture.SetPixels32(rotated);
|
|
|
|
|
|
|
|
return rotatedTexture;
|
|
|
|
}
|
|
|
|
|
|
|
|
Texture2D CropTexture(WebCamTexture originalTexture)
|
|
|
|
{
|
|
|
|
var x = 0;
|
|
|
|
var y = 0;
|
|
|
|
var size = originalTexture.height;
|
|
|
|
|
|
|
|
if (originalTexture.height > originalTexture.width)
|
|
|
|
{
|
|
|
|
size = originalTexture.width;
|
|
|
|
y = (originalTexture.height - originalTexture.width) / 2;
|
|
|
|
}
|
|
|
|
else if (originalTexture.height < originalTexture.width)
|
|
|
|
{
|
|
|
|
size = originalTexture.height;
|
|
|
|
x = (originalTexture.width - originalTexture.height) / 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
Texture2D croppedTexture = new(size, size);
|
|
|
|
croppedTexture.SetPixels(originalTexture.GetPixels(x, y, size, size));
|
|
|
|
|
|
|
|
return croppedTexture;
|
|
|
|
}
|
2024-01-27 16:54:03 +00:00
|
|
|
|
2024-01-27 17:09:40 +00:00
|
|
|
public Texture2D GetPhoto()
|
2024-01-27 16:54:03 +00:00
|
|
|
{
|
2024-01-27 23:21:38 +00:00
|
|
|
if (photo)
|
|
|
|
{
|
2024-01-27 17:09:40 +00:00
|
|
|
return photo;
|
|
|
|
}
|
|
|
|
|
2024-01-28 01:59:30 +00:00
|
|
|
if (!wTexture || numberOfWebcams < 1)
|
2024-01-27 23:21:38 +00:00
|
|
|
{
|
2024-01-27 17:09:40 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2024-01-27 16:54:03 +00:00
|
|
|
Texture2D cropped = CropTexture(wTexture);
|
|
|
|
Texture2D rotated = RotateTexture(cropped, !wDevice.isFrontFacing);
|
|
|
|
rotated.Apply();
|
|
|
|
|
|
|
|
return rotated;
|
|
|
|
}
|
2024-01-27 10:26:34 +00:00
|
|
|
}
|