Snaparazzi/Assets/Scripts/CameraManager.cs

108 lines
3.0 KiB
C#
Raw Normal View History

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 WebCamTexture wTexture;
public WebCamDevice wDevice;
2024-01-27 12:39:18 +00:00
public Button freezeButton;
public Button resumeButton;
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 11:31:58 +00:00
void Start()
2024-01-27 10:26:34 +00:00
{
2024-01-27 15:39:10 +00:00
wDevice = WebCamTexture.devices.First(x => x.isFrontFacing == false);
wTexture = new WebCamTexture(wDevice.name);
2024-01-27 12:39:18 +00:00
WebcamResume();
2024-01-27 10:26:34 +00:00
}
// Update is called once per frame
void Update()
{
2024-01-27 15:39:10 +00:00
Texture2D cropped = CropTexture(wTexture);
Texture2D rotated = RotateTexture(cropped, !wDevice.isFrontFacing);
2024-01-27 14:22:45 +00:00
2024-01-27 15:39:10 +00:00
gameObject.GetComponent<RawImage>().texture = rotated;
gameObject.GetComponent<RawImage>().material.mainTexture = rotated;
2024-01-27 10:26:34 +00:00
}
2024-01-27 12:39:18 +00:00
public void WebcamResume()
{
2024-01-27 15:39:10 +00:00
wTexture.Play();
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 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);
rotatedTexture.Apply();
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));
croppedTexture.Apply();
return croppedTexture;
}
2024-01-27 10:26:34 +00:00
}