feat: webcam working

This commit is contained in:
Michel Roux 2024-01-27 16:39:10 +01:00
parent 5a39339251
commit a07aa506b4
3 changed files with 76 additions and 32 deletions

View File

@ -642,7 +642,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: efbbc7abb0db8c7e0b29ec4bb986d783, type: 3} m_Script: {fileID: 11500000, guid: efbbc7abb0db8c7e0b29ec4bb986d783, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
webCamTexture: {fileID: 0} texture: {fileID: 0}
freezeButton: {fileID: 1776746473} freezeButton: {fileID: 1776746473}
resumeButton: {fileID: 1872873213} resumeButton: {fileID: 1872873213}
--- !u!114 &1128585037 --- !u!114 &1128585037

View File

@ -1,69 +1,107 @@
using System.Linq;
using UnityEngine; using UnityEngine;
using UnityEngine.UI; using UnityEngine.UI;
public class CameraManager : MonoBehaviour public class CameraManager : MonoBehaviour
{ {
public WebCamTexture webCamTexture; public WebCamTexture wTexture;
public WebCamDevice wDevice;
public Button freezeButton; public Button freezeButton;
public Button resumeButton; public Button resumeButton;
// Start is called before the first frame update // Start is called before the first frame update
void Start() void Start()
{ {
webCamTexture = new WebCamTexture(); wDevice = WebCamTexture.devices.First(x => x.isFrontFacing == false);
//gameObject.transform.rotation = transform.rotation * Quaternion.AngleAxis(webCamTexture.videoRotationAngle, Vector3.up); wTexture = new WebCamTexture(wDevice.name);
WebcamResume(); WebcamResume();
} }
// Update is called once per frame // Update is called once per frame
void Update() void Update()
{ {
var x = 0; Texture2D cropped = CropTexture(wTexture);
var y = 0; Texture2D rotated = RotateTexture(cropped, !wDevice.isFrontFacing);
var size = webCamTexture.height;
if (webCamTexture.height > webCamTexture.width) gameObject.GetComponent<RawImage>().texture = rotated;
{ gameObject.GetComponent<RawImage>().material.mainTexture = rotated;
size = webCamTexture.width;
y = (webCamTexture.height - webCamTexture.width) / 2;
}
else if (webCamTexture.height < webCamTexture.width)
{
size = webCamTexture.height;
x = (webCamTexture.width - webCamTexture.height) / 2;
}
Texture2D cropped = new(size, size);
cropped.SetPixels(webCamTexture.GetPixels(x, y, size, size));
cropped.Apply();
gameObject.GetComponent<RawImage>().texture = cropped;
gameObject.GetComponent<RawImage>().material.mainTexture = cropped;
} }
public void WebcamResume() public void WebcamResume()
{ {
webCamTexture.Play(); wTexture.Play();
freezeButton.gameObject.SetActive(true); freezeButton.gameObject.SetActive(true);
resumeButton.gameObject.SetActive(false); resumeButton.gameObject.SetActive(false);
} }
public void WebcamChange() public void WebcamChange()
{ {
foreach (WebCamDevice webCamDevice in WebCamTexture.devices) foreach (WebCamDevice loopDevice in WebCamTexture.devices)
{ {
if (webCamTexture.deviceName != webCamDevice.name) if ((wDevice.isFrontFacing && !loopDevice.isFrontFacing) || (!wDevice.isFrontFacing && loopDevice.isFrontFacing))
{ {
webCamTexture = new WebCamTexture(webCamDevice.name); wDevice = loopDevice;
wTexture = new WebCamTexture(wDevice.name);
WebcamResume(); WebcamResume();
break;
} }
} }
} }
public void WebcamStop() public void WebcamStop()
{ {
webCamTexture.Stop(); wTexture.Stop();
freezeButton.gameObject.SetActive(false); freezeButton.gameObject.SetActive(false);
resumeButton.gameObject.SetActive(true); resumeButton.gameObject.SetActive(true);
} }
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;
}
} }

View File

@ -48,6 +48,7 @@ PlayerSettings:
defaultScreenHeightWeb: 600 defaultScreenHeightWeb: 600
m_StereoRenderingPath: 0 m_StereoRenderingPath: 0
m_ActiveColorSpace: 1 m_ActiveColorSpace: 1
unsupportedMSAAFallback: 0
m_SpriteBatchVertexThreshold: 300 m_SpriteBatchVertexThreshold: 300
m_MTRendering: 1 m_MTRendering: 1
mipStripping: 0 mipStripping: 0
@ -75,6 +76,7 @@ PlayerSettings:
androidMinimumWindowWidth: 400 androidMinimumWindowWidth: 400
androidMinimumWindowHeight: 300 androidMinimumWindowHeight: 300
androidFullscreenMode: 1 androidFullscreenMode: 1
androidAutoRotationBehavior: 1
defaultIsNativeResolution: 1 defaultIsNativeResolution: 1
macRetinaSupport: 1 macRetinaSupport: 1
runInBackground: 1 runInBackground: 1
@ -86,6 +88,7 @@ PlayerSettings:
hideHomeButton: 0 hideHomeButton: 0
submitAnalytics: 1 submitAnalytics: 1
usePlayerLog: 1 usePlayerLog: 1
dedicatedServerOptimizations: 0
bakeCollisionMeshes: 0 bakeCollisionMeshes: 0
forceSingleInstance: 0 forceSingleInstance: 0
useFlipModelSwapchain: 1 useFlipModelSwapchain: 1
@ -125,6 +128,7 @@ PlayerSettings:
switchNVNMaxPublicTextureIDCount: 0 switchNVNMaxPublicTextureIDCount: 0
switchNVNMaxPublicSamplerIDCount: 0 switchNVNMaxPublicSamplerIDCount: 0
switchNVNGraphicsFirmwareMemory: 32 switchNVNGraphicsFirmwareMemory: 32
switchMaxWorkerMultiple: 8
stadiaPresentMode: 0 stadiaPresentMode: 0
stadiaTargetFramerate: 0 stadiaTargetFramerate: 0
vulkanNumSwapchainBuffers: 3 vulkanNumSwapchainBuffers: 3
@ -133,6 +137,8 @@ PlayerSettings:
vulkanEnableLateAcquireNextImage: 0 vulkanEnableLateAcquireNextImage: 0
vulkanEnableCommandBufferRecycling: 1 vulkanEnableCommandBufferRecycling: 1
loadStoreDebugModeEnabled: 0 loadStoreDebugModeEnabled: 0
visionOSBundleVersion: 1.0
tvOSBundleVersion: 1.0
bundleVersion: 0.1 bundleVersion: 0.1
preloadedAssets: [] preloadedAssets: []
metroInputSource: 0 metroInputSource: 0
@ -145,6 +151,7 @@ PlayerSettings:
isWsaHolographicRemotingEnabled: 0 isWsaHolographicRemotingEnabled: 0
enableFrameTimingStats: 0 enableFrameTimingStats: 0
enableOpenGLProfilerGPURecorders: 1 enableOpenGLProfilerGPURecorders: 1
allowHDRDisplaySupport: 0
useHDRDisplay: 0 useHDRDisplay: 0
hdrBitDepth: 0 hdrBitDepth: 0
m_ColorGamuts: 00000000 m_ColorGamuts: 00000000
@ -590,7 +597,7 @@ PlayerSettings:
switchSocketConcurrencyLimit: 14 switchSocketConcurrencyLimit: 14
switchScreenResolutionBehavior: 2 switchScreenResolutionBehavior: 2
switchUseCPUProfiler: 0 switchUseCPUProfiler: 0
switchUseGOLDLinker: 0 switchEnableFileSystemTrace: 0
switchLTOSetting: 0 switchLTOSetting: 0
switchApplicationID: 0x01004b9000490000 switchApplicationID: 0x01004b9000490000
switchNSODependencies: switchNSODependencies:
@ -720,7 +727,6 @@ PlayerSettings:
switchSocketBufferEfficiency: 4 switchSocketBufferEfficiency: 4
switchSocketInitializeEnabled: 1 switchSocketInitializeEnabled: 1
switchNetworkInterfaceManagerInitializeEnabled: 1 switchNetworkInterfaceManagerInitializeEnabled: 1
switchPlayerConnectionEnabled: 1
switchUseNewStyleFilepaths: 1 switchUseNewStyleFilepaths: 1
switchUseLegacyFmodPriorities: 0 switchUseLegacyFmodPriorities: 0
switchUseMicroSleepForYield: 1 switchUseMicroSleepForYield: 1
@ -834,7 +840,7 @@ PlayerSettings:
additionalCompilerArguments: {} additionalCompilerArguments: {}
platformArchitecture: {} platformArchitecture: {}
scriptingBackend: scriptingBackend:
Android: 1 Android: 0
Standalone: 1 Standalone: 1
il2cppCompilerConfiguration: {} il2cppCompilerConfiguration: {}
il2cppCodeGeneration: {} il2cppCodeGeneration: {}