From a07aa506b4c6c84353d89f807bdf535a345ab06d Mon Sep 17 00:00:00 2001 From: Michel Roux Date: Sat, 27 Jan 2024 16:39:10 +0100 Subject: [PATCH] feat: webcam working --- Assets/Scenes/CameraView.unity | 2 +- Assets/Scripts/CameraManager.cs | 94 +++++++++++++++++++-------- ProjectSettings/ProjectSettings.asset | 12 +++- 3 files changed, 76 insertions(+), 32 deletions(-) diff --git a/Assets/Scenes/CameraView.unity b/Assets/Scenes/CameraView.unity index e846b17..7775b84 100644 --- a/Assets/Scenes/CameraView.unity +++ b/Assets/Scenes/CameraView.unity @@ -642,7 +642,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: efbbc7abb0db8c7e0b29ec4bb986d783, type: 3} m_Name: m_EditorClassIdentifier: - webCamTexture: {fileID: 0} + texture: {fileID: 0} freezeButton: {fileID: 1776746473} resumeButton: {fileID: 1872873213} --- !u!114 &1128585037 diff --git a/Assets/Scripts/CameraManager.cs b/Assets/Scripts/CameraManager.cs index c1144a7..ac1db37 100644 --- a/Assets/Scripts/CameraManager.cs +++ b/Assets/Scripts/CameraManager.cs @@ -1,69 +1,107 @@ +using System.Linq; using UnityEngine; using UnityEngine.UI; public class CameraManager : MonoBehaviour { - public WebCamTexture webCamTexture; + public WebCamTexture wTexture; + public WebCamDevice wDevice; public Button freezeButton; public Button resumeButton; // Start is called before the first frame update void Start() { - webCamTexture = new WebCamTexture(); - //gameObject.transform.rotation = transform.rotation * Quaternion.AngleAxis(webCamTexture.videoRotationAngle, Vector3.up); + wDevice = WebCamTexture.devices.First(x => x.isFrontFacing == false); + wTexture = new WebCamTexture(wDevice.name); WebcamResume(); } // Update is called once per frame void Update() { - var x = 0; - var y = 0; - var size = webCamTexture.height; + Texture2D cropped = CropTexture(wTexture); + Texture2D rotated = RotateTexture(cropped, !wDevice.isFrontFacing); - if (webCamTexture.height > webCamTexture.width) - { - 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().texture = cropped; - gameObject.GetComponent().material.mainTexture = cropped; + gameObject.GetComponent().texture = rotated; + gameObject.GetComponent().material.mainTexture = rotated; } public void WebcamResume() { - webCamTexture.Play(); + wTexture.Play(); freezeButton.gameObject.SetActive(true); resumeButton.gameObject.SetActive(false); } 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(); + break; } } } public void WebcamStop() { - webCamTexture.Stop(); + wTexture.Stop(); freezeButton.gameObject.SetActive(false); 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; + } } diff --git a/ProjectSettings/ProjectSettings.asset b/ProjectSettings/ProjectSettings.asset index aa0a8ab..a1bcf2e 100644 --- a/ProjectSettings/ProjectSettings.asset +++ b/ProjectSettings/ProjectSettings.asset @@ -48,6 +48,7 @@ PlayerSettings: defaultScreenHeightWeb: 600 m_StereoRenderingPath: 0 m_ActiveColorSpace: 1 + unsupportedMSAAFallback: 0 m_SpriteBatchVertexThreshold: 300 m_MTRendering: 1 mipStripping: 0 @@ -75,6 +76,7 @@ PlayerSettings: androidMinimumWindowWidth: 400 androidMinimumWindowHeight: 300 androidFullscreenMode: 1 + androidAutoRotationBehavior: 1 defaultIsNativeResolution: 1 macRetinaSupport: 1 runInBackground: 1 @@ -86,6 +88,7 @@ PlayerSettings: hideHomeButton: 0 submitAnalytics: 1 usePlayerLog: 1 + dedicatedServerOptimizations: 0 bakeCollisionMeshes: 0 forceSingleInstance: 0 useFlipModelSwapchain: 1 @@ -125,6 +128,7 @@ PlayerSettings: switchNVNMaxPublicTextureIDCount: 0 switchNVNMaxPublicSamplerIDCount: 0 switchNVNGraphicsFirmwareMemory: 32 + switchMaxWorkerMultiple: 8 stadiaPresentMode: 0 stadiaTargetFramerate: 0 vulkanNumSwapchainBuffers: 3 @@ -133,6 +137,8 @@ PlayerSettings: vulkanEnableLateAcquireNextImage: 0 vulkanEnableCommandBufferRecycling: 1 loadStoreDebugModeEnabled: 0 + visionOSBundleVersion: 1.0 + tvOSBundleVersion: 1.0 bundleVersion: 0.1 preloadedAssets: [] metroInputSource: 0 @@ -145,6 +151,7 @@ PlayerSettings: isWsaHolographicRemotingEnabled: 0 enableFrameTimingStats: 0 enableOpenGLProfilerGPURecorders: 1 + allowHDRDisplaySupport: 0 useHDRDisplay: 0 hdrBitDepth: 0 m_ColorGamuts: 00000000 @@ -590,7 +597,7 @@ PlayerSettings: switchSocketConcurrencyLimit: 14 switchScreenResolutionBehavior: 2 switchUseCPUProfiler: 0 - switchUseGOLDLinker: 0 + switchEnableFileSystemTrace: 0 switchLTOSetting: 0 switchApplicationID: 0x01004b9000490000 switchNSODependencies: @@ -720,7 +727,6 @@ PlayerSettings: switchSocketBufferEfficiency: 4 switchSocketInitializeEnabled: 1 switchNetworkInterfaceManagerInitializeEnabled: 1 - switchPlayerConnectionEnabled: 1 switchUseNewStyleFilepaths: 1 switchUseLegacyFmodPriorities: 0 switchUseMicroSleepForYield: 1 @@ -834,7 +840,7 @@ PlayerSettings: additionalCompilerArguments: {} platformArchitecture: {} scriptingBackend: - Android: 1 + Android: 0 Standalone: 1 il2cppCompilerConfiguration: {} il2cppCodeGeneration: {}