From 95665b2aab409eea3ceb2aeae932151f6c67a254 Mon Sep 17 00:00:00 2001 From: Michel Roux Date: Sat, 27 Jan 2024 15:22:45 +0100 Subject: [PATCH] feat: webcam crop --- Assets/Scenes/CameraView.unity | 8 +++++--- Assets/Scripts/CameraManager.cs | 24 ++++++++++++++++++++++-- ProjectSettings/ProjectSettings.asset | 2 +- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/Assets/Scenes/CameraView.unity b/Assets/Scenes/CameraView.unity index e51e2a5..e846b17 100644 --- a/Assets/Scenes/CameraView.unity +++ b/Assets/Scenes/CameraView.unity @@ -625,10 +625,10 @@ RectTransform: m_Children: [] m_Father: {fileID: 1908036955} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} - m_AnchorMin: {x: 0, y: 0} - m_AnchorMax: {x: 1, y: 1} + m_AnchorMin: {x: 0.5, y: 0.5} + m_AnchorMax: {x: 0.5, y: 0.5} m_AnchoredPosition: {x: 0, y: 0} - m_SizeDelta: {x: 0, y: 0} + m_SizeDelta: {x: 512, y: 512} m_Pivot: {x: 0.5, y: 0.5} --- !u!114 &1128585036 MonoBehaviour: @@ -643,6 +643,8 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: webCamTexture: {fileID: 0} + freezeButton: {fileID: 1776746473} + resumeButton: {fileID: 1872873213} --- !u!114 &1128585037 MonoBehaviour: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/CameraManager.cs b/Assets/Scripts/CameraManager.cs index 4fbfd69..c1144a7 100644 --- a/Assets/Scripts/CameraManager.cs +++ b/Assets/Scripts/CameraManager.cs @@ -11,14 +11,34 @@ public class CameraManager : MonoBehaviour void Start() { webCamTexture = new WebCamTexture(); + //gameObject.transform.rotation = transform.rotation * Quaternion.AngleAxis(webCamTexture.videoRotationAngle, Vector3.up); WebcamResume(); } // Update is called once per frame void Update() { - gameObject.GetComponent().texture = webCamTexture; - gameObject.GetComponent().material.mainTexture = webCamTexture; + var x = 0; + var y = 0; + var size = webCamTexture.height; + + 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; } public void WebcamResume() diff --git a/ProjectSettings/ProjectSettings.asset b/ProjectSettings/ProjectSettings.asset index a9cc00e..e379946 100644 --- a/ProjectSettings/ProjectSettings.asset +++ b/ProjectSettings/ProjectSettings.asset @@ -8,7 +8,7 @@ PlayerSettings: AndroidProfiler: 0 AndroidFilterTouchesWhenObscured: 0 AndroidEnableSustainedPerformanceMode: 0 - defaultScreenOrientation: 4 + defaultScreenOrientation: 0 targetDevice: 2 useOnDemandResources: 0 accelerometerFrequency: 60