diff --git a/Assets/Resources/UI/GuideUI.prefab b/Assets/Resources/UI/GuideUI.prefab index e51ee4d..a966322 100644 --- a/Assets/Resources/UI/GuideUI.prefab +++ b/Assets/Resources/UI/GuideUI.prefab @@ -255,6 +255,8 @@ RectTransform: - {fileID: 8475079236000353301} - {fileID: 6229047881894855180} - {fileID: 4277339089744270226} + - {fileID: 4318354961022875112} + - {fileID: 3096906925272982799} m_Father: {fileID: 475269767089658797} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_AnchorMin: {x: 0, y: 0} @@ -1395,6 +1397,78 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: m_ShowMaskGraphic: 1 +--- !u!1 &5049731194724459156 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 4318354961022875112} + - component: {fileID: 9105578097159592020} + - component: {fileID: 7455986887067286179} + m_Layer: 5 + m_Name: Displayer + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!224 &4318354961022875112 +RectTransform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 5049731194724459156} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 38199016232350786} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} + m_AnchorMin: {x: 0, y: 0} + m_AnchorMax: {x: 1, y: 1} + m_AnchoredPosition: {x: -270, y: 0} + m_SizeDelta: {x: -640, y: -100} + m_Pivot: {x: 0.5, y: 0.5} +--- !u!222 &9105578097159592020 +CanvasRenderer: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 5049731194724459156} + m_CullTransparentMesh: 1 +--- !u!114 &7455986887067286179 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 5049731194724459156} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 1344c3c82d62a2a41a3576d8abb8e3ea, type: 3} + m_Name: + m_EditorClassIdentifier: + m_Material: {fileID: 0} + m_Color: {r: 1, g: 1, b: 1, a: 1} + m_RaycastTarget: 1 + m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0} + m_Maskable: 1 + m_OnCullStateChanged: + m_PersistentCalls: + m_Calls: [] + m_Texture: {fileID: 0} + m_UVRect: + serializedVersion: 2 + x: 0 + y: 0 + width: 1 + height: 1 --- !u!1 &5105019391767359508 GameObject: m_ObjectHideFlags: 0 @@ -1908,6 +1982,114 @@ MonoBehaviour: m_hasFontAssetChanged: 0 m_baseMaterial: {fileID: 0} m_maskOffset: {x: 0, y: 0, z: 0, w: 0} +--- !u!1 &7940173089482779789 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 3096906925272982799} + - component: {fileID: 7626762285874418130} + - component: {fileID: 6854932209812094026} + m_Layer: 5 + m_Name: CaptureManager + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!224 &3096906925272982799 +RectTransform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 7940173089482779789} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 38199016232350786} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} + 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: 100, y: 100} + m_Pivot: {x: 0.5, y: 0.5} +--- !u!114 &7626762285874418130 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 7940173089482779789} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: c96807d839ba7554d92494fd6d25bb28, type: 3} + m_Name: + m_EditorClassIdentifier: + _webCamTextureToMatHelper: {fileID: 0} + SamplingRate: 0.3 + TargetDisplayer: {fileID: 7455986887067286179} +--- !u!114 &6854932209812094026 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 7940173089482779789} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: df35b0c19ca97734e87299a664cea35f, type: 3} + m_Name: + m_EditorClassIdentifier: + _requestedDeviceName: + _requestedWidth: 960 + _requestedHeight: 720 + _requestedIsFrontFacing: 0 + _requestedFPS: 30 + _rotate90Degree: 0 + _flipVertical: 0 + _flipHorizontal: 0 + _outputColorFormat: 4 + _timeoutFrameCount: 1500 + onInitialized: + m_PersistentCalls: + m_Calls: + - m_Target: {fileID: 7626762285874418130} + m_TargetAssemblyTypeName: Yoga.MotionCaptureManager, Assembly-CSharp + m_MethodName: OnWebCamTextureToMatHelperInitialized + m_Mode: 1 + m_Arguments: + m_ObjectArgument: {fileID: 0} + m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine + m_IntArgument: 0 + m_FloatArgument: 0 + m_StringArgument: + m_BoolArgument: 0 + m_CallState: 2 + onDisposed: + m_PersistentCalls: + m_Calls: + - m_Target: {fileID: 7626762285874418130} + m_TargetAssemblyTypeName: Yoga.MotionCaptureManager, Assembly-CSharp + m_MethodName: OnWebCamTextureToMatHelperDisposed + m_Mode: 1 + m_Arguments: + m_ObjectArgument: {fileID: 0} + m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine + m_IntArgument: 0 + m_FloatArgument: 0 + m_StringArgument: + m_BoolArgument: 0 + m_CallState: 2 + onErrorOccurred: + m_PersistentCalls: + m_Calls: [] + avoidAndroidFrontCameraLowLightIssue: 0 --- !u!1 &8393834018379886065 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Scenes/YogaMain.unity b/Assets/Scenes/YogaMain.unity index 0f9740f..be6dac6 100644 --- a/Assets/Scenes/YogaMain.unity +++ b/Assets/Scenes/YogaMain.unity @@ -290,6 +290,50 @@ RectTransform: m_CorrespondingSourceObject: {fileID: 8805156796672989800, guid: 85c5c8dc06b4f49419cf4b1751725a67, type: 3} m_PrefabInstance: {fileID: 168110290} m_PrefabAsset: {fileID: 0} +--- !u!1 &280144576 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 280144578} + - component: {fileID: 280144577} + m_Layer: 0 + m_Name: YogaMainManager + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!114 &280144577 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 280144576} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: b55f4807e5753354ca51edb2a942ff20, type: 3} + m_Name: + m_EditorClassIdentifier: +--- !u!4 &280144578 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 280144576} + serializedVersion: 2 + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 1042.905, y: 591.9674, z: 1279.6279} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 0} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &519578969 GameObject: m_ObjectHideFlags: 0 @@ -817,195 +861,6 @@ RectTransform: m_AnchoredPosition: {x: 0, y: 0} m_SizeDelta: {x: 0, y: 0} m_Pivot: {x: 0, y: 0} ---- !u!1 &1603288915 -GameObject: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - serializedVersion: 6 - m_Component: - - component: {fileID: 1603288919} - - component: {fileID: 1603288918} - - component: {fileID: 1603288916} - - component: {fileID: 1603288917} - - component: {fileID: 1603288921} - - component: {fileID: 1603288922} - m_Layer: 0 - m_Name: Capture - m_TagString: Untagged - m_Icon: {fileID: 0} - m_NavMeshLayer: 0 - m_StaticEditorFlags: 0 - m_IsActive: 1 ---- !u!64 &1603288916 -MeshCollider: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 1603288915} - m_Material: {fileID: 0} - m_IncludeLayers: - serializedVersion: 2 - m_Bits: 0 - m_ExcludeLayers: - serializedVersion: 2 - m_Bits: 0 - m_LayerOverridePriority: 0 - m_IsTrigger: 0 - m_ProvidesContacts: 0 - m_Enabled: 1 - serializedVersion: 5 - m_Convex: 0 - m_CookingOptions: 30 - m_Mesh: {fileID: 10210, guid: 0000000000000000e000000000000000, type: 0} ---- !u!23 &1603288917 -MeshRenderer: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 1603288915} - m_Enabled: 1 - m_CastShadows: 0 - m_ReceiveShadows: 0 - m_DynamicOccludee: 1 - m_StaticShadowCaster: 0 - m_MotionVectors: 1 - m_LightProbeUsage: 0 - m_ReflectionProbeUsage: 1 - m_RayTracingMode: 2 - m_RayTraceProcedural: 0 - m_RenderingLayerMask: 1 - m_RendererPriority: 0 - m_Materials: - - {fileID: 2100000, guid: 7b4f42e90cf460d47a091713acf8db47, type: 2} - m_StaticBatchInfo: - firstSubMesh: 0 - subMeshCount: 0 - m_StaticBatchRoot: {fileID: 0} - m_ProbeAnchor: {fileID: 0} - m_LightProbeVolumeOverride: {fileID: 0} - m_ScaleInLightmap: 1 - m_ReceiveGI: 1 - m_PreserveUVs: 0 - m_IgnoreNormalsForChartDetection: 0 - m_ImportantGI: 0 - m_StitchLightmapSeams: 1 - m_SelectedEditorRenderState: 3 - m_MinimumChartSize: 4 - m_AutoUVMaxDistance: 0.5 - m_AutoUVMaxAngle: 89 - m_LightmapParameters: {fileID: 0} - m_SortingLayerID: 0 - m_SortingLayer: 0 - m_SortingOrder: 0 - m_AdditionalVertexStreams: {fileID: 0} ---- !u!33 &1603288918 -MeshFilter: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 1603288915} - m_Mesh: {fileID: 10210, guid: 0000000000000000e000000000000000, type: 0} ---- !u!4 &1603288919 -Transform: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 1603288915} - serializedVersion: 2 - m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: -30, y: 0, z: 80} - m_LocalScale: {x: 128, y: 96, z: 0} - m_ConstrainProportionsScale: 0 - m_Children: [] - m_Father: {fileID: 0} - m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} ---- !u!114 &1603288921 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 1603288915} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: df35b0c19ca97734e87299a664cea35f, type: 3} - m_Name: - m_EditorClassIdentifier: - _requestedDeviceName: 1 - _requestedWidth: 1280 - _requestedHeight: 960 - _requestedIsFrontFacing: 0 - _requestedFPS: 30 - _rotate90Degree: 0 - _flipVertical: 0 - _flipHorizontal: 0 - _outputColorFormat: 2 - _timeoutFrameCount: 300 - onInitialized: - m_PersistentCalls: - m_Calls: - - m_Target: {fileID: 1603288922} - m_TargetAssemblyTypeName: Yoga.MotionCaptureManager, Assembly-CSharp - m_MethodName: OnWebCamTextureToMatHelperInitialized - m_Mode: 1 - m_Arguments: - m_ObjectArgument: {fileID: 0} - m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine - m_IntArgument: 0 - m_FloatArgument: 0 - m_StringArgument: - m_BoolArgument: 0 - m_CallState: 2 - onDisposed: - m_PersistentCalls: - m_Calls: - - m_Target: {fileID: 1603288922} - m_TargetAssemblyTypeName: Yoga.MotionCaptureManager, Assembly-CSharp - m_MethodName: OnWebCamTextureToMatHelperDisposed - m_Mode: 1 - m_Arguments: - m_ObjectArgument: {fileID: 0} - m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine - m_IntArgument: 0 - m_FloatArgument: 0 - m_StringArgument: - m_BoolArgument: 0 - m_CallState: 2 - onErrorOccurred: - m_PersistentCalls: - m_Calls: - - m_Target: {fileID: 1603288922} - m_TargetAssemblyTypeName: Yoga.MotionCaptureManager, Assembly-CSharp - m_MethodName: OnWebCamTextureToMatHelperErrorOccurred - m_Mode: 0 - m_Arguments: - m_ObjectArgument: {fileID: 0} - m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine - m_IntArgument: 0 - m_FloatArgument: 0 - m_StringArgument: - m_BoolArgument: 0 - m_CallState: 2 - avoidAndroidFrontCameraLowLightIssue: 1 ---- !u!114 &1603288922 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 1603288915} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: c96807d839ba7554d92494fd6d25bb28, type: 3} - m_Name: - m_EditorClassIdentifier: - SamplingRate: 0.1 --- !u!1 &1680658391 GameObject: m_ObjectHideFlags: 0 @@ -7387,7 +7242,7 @@ SceneRoots: - {fileID: 963194228} - {fileID: 1190353384} - {fileID: 1521186386} - - {fileID: 1603288919} - {fileID: 1763781738} - {fileID: 51299363} - {fileID: 2046868293} + - {fileID: 280144578} diff --git a/Assets/Scripts/PoseCheck/EstimateModel/CVEstimator.cs b/Assets/Scripts/PoseCheck/EstimateModel/CVEstimator.cs index c20d798..45ad2f6 100644 --- a/Assets/Scripts/PoseCheck/EstimateModel/CVEstimator.cs +++ b/Assets/Scripts/PoseCheck/EstimateModel/CVEstimator.cs @@ -90,11 +90,11 @@ public class CVEstimator : Singleton Mat bgrMat = new Mat(); while (IsRunning) { - Mat rgbaMat = MotionCaptureManager.Instance.RgbaMat; + Mat rgbaMat = YogaManager.Instance.RgbaMat; if (rgbaMat == null) { - Debug.Log("WebCamTexture is null. "); - Debug.Log("Re-Estimation."); + //Debug.Log("WebCamTexture is null. "); + //Debug.Log("Re-Estimation."); continue; //重新检测 } Imgproc.cvtColor(rgbaMat, bgrMat, Imgproc.COLOR_RGBA2BGR); @@ -106,7 +106,7 @@ public class CVEstimator : Singleton } //更新关键点位检测结果 - MotionCaptureManager.Instance.CurrPersonPoints = points; + YogaManager.Instance.CurrPersonPoints = points; //等待到下一个更新周期 if (DateTime.Now - startTime > _refreshRate) diff --git a/Assets/Scripts/PoseCheck/EstimateModel/OpenPoseEsimater.cs b/Assets/Scripts/PoseCheck/EstimateModel/OpenPoseEsimater.cs index 7b1ba5c..c5f656d 100644 --- a/Assets/Scripts/PoseCheck/EstimateModel/OpenPoseEsimater.cs +++ b/Assets/Scripts/PoseCheck/EstimateModel/OpenPoseEsimater.cs @@ -108,7 +108,7 @@ public class OpenPoseEsimater : Estimator } //更新人物框检测结果 - MotionCaptureManager.Instance.VoloResult = voloResultBox; + YogaManager.Instance.VoloResult = voloResultBox; points = null; @@ -164,12 +164,12 @@ public class OpenPoseEsimater : Estimator public override void DebugPrint(Mat image, bool isRGB = false) { - if (MotionCaptureManager.Instance.VoloResult.Count > 2) + if (YogaManager.Instance.VoloResult.Count > 2) return; - var box = MotionCaptureManager.Instance.VoloResult[0]; - var conf = MotionCaptureManager.Instance.VoloResult[1]; - var cls = MotionCaptureManager.Instance.VoloResult[2]; + var box = YogaManager.Instance.VoloResult[0]; + var conf = YogaManager.Instance.VoloResult[1]; + var cls = YogaManager.Instance.VoloResult[2]; float left = box[0]; float top = box[1]; diff --git a/Assets/Scripts/PoseCheck/MotionCaptureManager.cs b/Assets/Scripts/PoseCheck/MotionCaptureManager.cs index d0cfb3c..11af6dd 100644 --- a/Assets/Scripts/PoseCheck/MotionCaptureManager.cs +++ b/Assets/Scripts/PoseCheck/MotionCaptureManager.cs @@ -1,30 +1,24 @@ using OpenCVForUnity.CoreModule; -using OpenCVForUnity.DnnModule; using OpenCVForUnity.ImgprocModule; using OpenCVForUnity.UnityUtils; using OpenCVForUnity.UnityUtils.Helper; -using OpenCVForUnityExample; -//using Serenegiant.UVC; +using Serenegiant.UVC; using System; using System.Collections; using System.Collections.Generic; using System.Linq; -using System.Threading.Tasks; using UnityEngine; -using UnityEngine.Video; +using UnityEngine.UI; namespace Yoga { - public class MotionCaptureManager : MonoSingleton + public class MotionCaptureManager : CameraCaptureManagerBase, IUVCDrawer { - private WebCamTextureToMatHelper _webCamTextureToMatHelper; - //private UVCManager _uvcManager; + public WebCamTextureToMatHelper _webCamTextureToMatHelper; private Texture2D texture; public float SamplingRate = 0.3f; - - private List _voloResult = new List(); - private List _currPersonPoints = new List(); + public RawImage TargetDisplayer; private Mat _bgrMat; @@ -32,21 +26,7 @@ namespace Yoga public bool IsOnCamCapture { get => _isOnCamCapture; internal set => _isOnCamCapture = value; } public WebCamTextureToMatHelper WebCamTextureToMatHelper => _webCamTextureToMatHelper; - public List CurrPersonPoints { get => _currPersonPoints; set => _currPersonPoints = value; } - public List VoloResult { get => _voloResult; set => _voloResult = value; } private bool _isCorrectAction = false; - private Mat _rgbaMat; - public Mat RgbaMat - { - get - { - if (_rgbaMat == null && _webCamTextureToMatHelper != null) - _rgbaMat = _webCamTextureToMatHelper.GetMat(); - return _rgbaMat; - } - - set => _rgbaMat = value; - } private void OnEnable() { @@ -58,6 +38,7 @@ namespace Yoga EventManager.Instance.AddEventListener(YogaEventType.ChangeCaptureCameraDevice, ChangeCaptureCameraDevice); } + private void OnDisable() { EventManager.Instance.RemoveEventListener(YogaEventType.StartMotionCapture, OnStartMotionCapture); @@ -68,12 +49,10 @@ namespace Yoga EventManager.Instance.RemoveEventListener(YogaEventType.ChangeCaptureCameraDevice, ChangeCaptureCameraDevice); } - public override void Init() + public void Awake() { - base.Init(); - - _webCamTextureToMatHelper = gameObject.GetComponentInChildren(); - //_uvcManager = gameObject.GetComponentInChildren(); + if (_webCamTextureToMatHelper == null) + _webCamTextureToMatHelper = transform.GetComponent(); #if UNITY_ANDROID && !UNITY_EDITOR // Avoids the front camera low light issue that occurs in only some Android devices (e.g. Google Pixel, Pixel2). @@ -82,18 +61,116 @@ namespace Yoga _webCamTextureToMatHelper.Initialize(); - UIManager.Instance.LoadReset(); - - if (YogaManager.Instance.Action == null) - YogaManager.Instance.InitData(); - - UIManager.Instance.ShowPanel(false, YogaManager.Instance.Action); - Utils.setDebugMode(true); //打印日志 + GlobalData.Instance.CameraDeviceType = CameraDeviceType.USB; //test + + var devices = UVCManager.Instance.GetAttachedDevices(); CVEstimator.Instance.Init();//初始化姿态检测 } + #region UVC + public UVCFilter[] UVCFilters; + private Texture SavedTexture; + public bool OnUVCAttachEvent(UVCManager manager, UVCDevice device) + { + var result = !device.IsRicoh || device.IsTHETA; + + result &= UVCFilter.Match(device, UVCFilters); + + return result; + } + + public void OnUVCDetachEvent(UVCManager manager, UVCDevice device) + { + Debug.Log("OnUVCDetachEvent " + device); + } + + public bool CanDraw(UVCManager manager, UVCDevice device) + { + return UVCFilter.Match(device, UVCFilters); + } + + public void OnUVCStartEvent(UVCManager manager, UVCDevice device, Texture tex) + { + HandleOnStartPreview(tex); + } + + public void OnUVCStopEvent(UVCManager manager, UVCDevice device) + { + HandleOnStopPreview(); + } + + private void HandleOnStartPreview(Texture tex) + { + SavedTexture = TargetDisplayer.texture; + TargetDisplayer.texture = texture; + } + private void HandleOnStopPreview() + { + RestoreTexture(); + } + + private void RestoreTexture() + { + try + { + transform.GetComponent().material.mainTexture = SavedTexture; + } + catch (Exception e) + { + Debug.LogException(e); + } + SavedTexture = null; + } + #endregion + + + public static Texture2D HorizontalFlipTexture(Texture2D texture) + { + //得到图片的宽高 + int width = texture.width; + int height = texture.height; + + Texture2D flipTexture = new Texture2D(width, height); + + for (int i = 0; i < width; i++) + { + flipTexture.SetPixels(i, 0, 1, height, texture.GetPixels(width - i - 1, 0, 1, height)); + } + flipTexture.Apply(); + + return flipTexture; + } + + private Mat GetMat() + { + Mat img = null; + if (GlobalData.Instance.CameraDeviceType == CameraDeviceType.USB && + UVCManager.Instance != null && + UVCManager.Instance.GetAttachedDevices() != null && UVCManager.Instance.GetAttachedDevices().Count > 0) + { + var devices = UVCManager.Instance.GetAttachedDevices(); + var formatRefMat = _webCamTextureToMatHelper.GetMat(); + var picCaptured = devices.FirstOrDefault().previewTexture; + Texture2D tmpTex = new Texture2D(picCaptured.width, picCaptured.height); + Utils.textureToTexture2D(picCaptured, tmpTex); + tmpTex = HorizontalFlipTexture(tmpTex);//picCaptured texture水平反转 + img = new Mat(formatRefMat.rows(), formatRefMat.cols(), formatRefMat.type()); + Utils.texture2DToMat(tmpTex, img); + } + else + { + if (_webCamTextureToMatHelper.IsPlaying() && _webCamTextureToMatHelper.DidUpdateThisFrame()) + { + img = _webCamTextureToMatHelper.GetMat(); + Imgproc.cvtColor(img, img, Imgproc.COLOR_BGR2RGB); + } + } + + return img; + } + private void Update() { if (!_isOnCamCapture) @@ -101,66 +178,46 @@ namespace Yoga return; } - if (GlobalData.Instance.CameraDeviceType == CameraDeviceType.USB) - { - //if (_uvcManager != null) - //{ - // var devices = _uvcManager.GetAttachedDevices(); - // if (devices != null && devices.Count > 0) - // { - // Utils.textureToTexture2D(devices.FirstOrDefault().previewTexture, texture); - // //Mat img = RgbaMat.clone(); - // //Utils.texture2DToMat(texture, img); - // //YogaManager.Instance.CurrentEstimator.Check(ref img); - // //Utils.matToTexture2D(img, texture); - - // return; - // } - //} - } - if (!transform.gameObject.activeSelf) transform.gameObject.SetActive(true); - if (_webCamTextureToMatHelper.IsPlaying() && _webCamTextureToMatHelper.DidUpdateThisFrame()) + Mat img = GetMat(); + + if (img == null) + return; + + + YogaManager.Instance.RgbaMat = img.clone(); + YogaManager.Instance.CurrentEstimator.Check(ref img);//检测模型,将错误信息打印在图片上 + + if (YogaManager.Instance.VoloResult.Count >= 2) + YogaManager.Instance.CurrentEstimator.DebugPrint(img); + + if (YogaManager.Instance.CurrPersonPoints != null && YogaManager.Instance.CurrPersonPoints.Count > 0) { - Mat img = _webCamTextureToMatHelper.GetMat(); - Imgproc.cvtColor(img, img, Imgproc.COLOR_BGR2RGB); - _rgbaMat = img.clone(); + List points = YogaManager.Instance.CurrPersonPoints; - YogaManager.Instance.CurrentEstimator.Check(ref img);//检测模型,将错误信息打印在图片上 - - - if (_voloResult.Count >= 2) - YogaManager.Instance.CurrentEstimator.DebugPrint(img);//DebugPrintObjectLayout(img, _voloResult[0], _voloResult[1], _voloResult[2]); - - if (_currPersonPoints != null && _currPersonPoints.Count > 0) + for (int i = 0; i < YogaConfig.POSE_PAIRS.GetLength(0); i++) { - List points = _currPersonPoints; + string partFrom = YogaConfig.POSE_PAIRS[i, 0]; + string partTo = YogaConfig.POSE_PAIRS[i, 1]; - for (int i = 0; i < YogaConfig.POSE_PAIRS.GetLength(0); i++) + int idFrom = YogaConfig.BODY_PARTS[partFrom]; + int idTo = YogaConfig.BODY_PARTS[partTo]; + + if (points[idFrom] == new Point(-1, -1) || points[idTo] == new Point(-1, -1)) + continue; + + if (points[idFrom] != null && points[idTo] != null) { - string partFrom = YogaConfig.POSE_PAIRS[i, 0]; - string partTo = YogaConfig.POSE_PAIRS[i, 1]; - - int idFrom = YogaConfig.BODY_PARTS[partFrom]; - int idTo = YogaConfig.BODY_PARTS[partTo]; - - if (points[idFrom] == new Point(-1, -1) || points[idTo] == new Point(-1, -1)) - continue; - - if (points[idFrom] != null && points[idTo] != null) - { - Imgproc.line(img, points[idFrom], points[idTo], new Scalar(0, 255, 0), 3); - Imgproc.ellipse(img, points[idFrom], new Size(3, 3), 0, 0, 360, new Scalar(0, 0, 255), Core.FILLED); - Imgproc.ellipse(img, points[idTo], new Size(3, 3), 0, 0, 360, new Scalar(0, 0, 255), Core.FILLED); - } + Imgproc.line(img, points[idFrom], points[idTo], new Scalar(0, 255, 0), 3); + Imgproc.ellipse(img, points[idFrom], new Size(3, 3), 0, 0, 360, new Scalar(0, 0, 255), Core.FILLED); + Imgproc.ellipse(img, points[idTo], new Size(3, 3), 0, 0, 360, new Scalar(0, 0, 255), Core.FILLED); } } - - //#endif - Utils.matToTexture2D(img, texture); } + + Utils.matToTexture2D(img, texture); } #region Event Func @@ -182,9 +239,9 @@ namespace Yoga var startTime = DateTime.Now; while (true) { - if (_currPersonPoints != null && _currPersonPoints.Count != 0) + if (YogaManager.Instance.CurrPersonPoints != null && YogaManager.Instance.CurrPersonPoints.Count != 0) { - YogaManager.Instance.Points = _currPersonPoints; + YogaManager.Instance.Points = YogaManager.Instance.CurrPersonPoints; break; } @@ -212,9 +269,9 @@ namespace Yoga while (true) { - if (YogaManager.Instance.ActionCheckPoints(_currPersonPoints)) + if (YogaManager.Instance.ActionCheckPoints(YogaManager.Instance.CurrPersonPoints)) { - _isCorrectAction = (_isCorrectAction || YogaManager.Instance.IsCorrectAction(_currPersonPoints, actionType)); + _isCorrectAction = (_isCorrectAction || YogaManager.Instance.IsCorrectAction(YogaManager.Instance.CurrPersonPoints, actionType)); break; } if (startTime.AddMilliseconds(100) < DateTime.Now) @@ -226,7 +283,6 @@ namespace Yoga } } - private void ChangeCaptureCameraDevice() { var deviceName = WebCamTexture.devices[GlobalData.Instance.CameraIndex].name; @@ -273,7 +329,8 @@ namespace Yoga texture = new Texture2D(webCamTextureMat.cols(), webCamTextureMat.rows(), TextureFormat.RGB24, false); Utils.matToTexture2D(webCamTextureMat, texture); - this.gameObject.GetComponent().material.mainTexture = texture; + //gameObject.GetComponent().material.mainTexture = texture; + TargetDisplayer.texture = texture; gameObject.transform.localScale = new Vector3(webCamTextureMat.cols() / 10, webCamTextureMat.rows() / 10, 1); Debug.Log("Screen.width " + Screen.width + " Screen.height " + Screen.height + " Screen.orientation " + Screen.orientation); @@ -330,4 +387,8 @@ namespace Yoga _isCorrectAction = false;//重置 } } + + public abstract class CameraCaptureManagerBase : MonoBehaviour + { + } } diff --git a/Assets/Scripts/UI/YogaMainManager.cs b/Assets/Scripts/UI/YogaMainManager.cs new file mode 100644 index 0000000..c923432 --- /dev/null +++ b/Assets/Scripts/UI/YogaMainManager.cs @@ -0,0 +1,16 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class YogaMainManager : MonoBehaviour +{ + private void Awake() + { + UIManager.Instance.LoadReset(); + + if (YogaManager.Instance.Action == null) + YogaManager.Instance.InitData(); + + UIManager.Instance.ShowPanel(false, YogaManager.Instance.Action); + } +} diff --git a/Assets/Scripts/UI/YogaMainManager.cs.meta b/Assets/Scripts/UI/YogaMainManager.cs.meta new file mode 100644 index 0000000..11ff4f5 --- /dev/null +++ b/Assets/Scripts/UI/YogaMainManager.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: b55f4807e5753354ca51edb2a942ff20 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/YogaManager.cs b/Assets/Scripts/YogaManager.cs index e4c8096..c9fe203 100644 --- a/Assets/Scripts/YogaManager.cs +++ b/Assets/Scripts/YogaManager.cs @@ -16,6 +16,21 @@ using Yoga; public class YogaManager : MonoSingleton { private List _points = new List(); + private Mat _rgbaMat; + public Mat RgbaMat + { + get + { + return _rgbaMat; + } + + set => _rgbaMat = value; + } + + private List _currPersonPoints = new List(); + public List CurrPersonPoints { get => _currPersonPoints; set => _currPersonPoints = value; } + private List _voloResult = new List(); + public List VoloResult { get => _voloResult; set => _voloResult = value; } public List Points { get => _points; set => _points = value; } @@ -116,7 +131,7 @@ public class YogaManager : MonoSingleton { if (points == null || points.Count == 0) { - Debug.Log("ActionCheckPoints points is null"); + //Debug.Log("ActionCheckPoints points is null"); return false; } diff --git a/Assets/UVC4UnityAndroidPlugin/Samples/Scripts/CubeHandler.cs b/Assets/UVC4UnityAndroidPlugin/Samples/Scripts/CubeHandler.cs index b3f0cad..55030b0 100644 --- a/Assets/UVC4UnityAndroidPlugin/Samples/Scripts/CubeHandler.cs +++ b/Assets/UVC4UnityAndroidPlugin/Samples/Scripts/CubeHandler.cs @@ -68,7 +68,7 @@ public class CubeHandler : MonoBehaviour, public void OnPointerClick(PointerEventData eventData) { #if (!NDEBUG && DEBUG && ENABLE_LOG) - Console.WriteLine("OnClick:"); + Debug.Log("OnClick:"); #endif } } diff --git a/Assets/UVC4UnityAndroidPlugin/Samples/Scripts/TouchEventManager.cs b/Assets/UVC4UnityAndroidPlugin/Samples/Scripts/TouchEventManager.cs index 5f4b60b..b3511e7 100644 --- a/Assets/UVC4UnityAndroidPlugin/Samples/Scripts/TouchEventManager.cs +++ b/Assets/UVC4UnityAndroidPlugin/Samples/Scripts/TouchEventManager.cs @@ -82,7 +82,7 @@ namespace Serenegiant { { // タッチしたとき touchEvent.state = TouchState.Began; #if (!NDEBUG && DEBUG && ENABLE_LOG) - Console.WriteLine("タッチした:"); + Debug.Log("タッチした:"); #endif } @@ -90,7 +90,7 @@ namespace Serenegiant { { // 離したとき touchEvent.state = TouchState.Ended; #if (!NDEBUG && DEBUG && ENABLE_LOG) - Console.WriteLine("タッチした:"); + Debug.Log("タッチした:"); #endif } @@ -98,7 +98,7 @@ namespace Serenegiant { { // 押し続けているとき touchEvent.state = TouchState.Moved; #if (!NDEBUG && DEBUG && ENABLE_LOG) - Console.WriteLine("押し続けている"); + Debug.Log("押し続けている"); #endif } diff --git a/Assets/UVC4UnityAndroidPlugin/Scripts/AndroidUtils.cs b/Assets/UVC4UnityAndroidPlugin/Scripts/AndroidUtils.cs index 9e9358f..f40e8f8 100644 --- a/Assets/UVC4UnityAndroidPlugin/Scripts/AndroidUtils.cs +++ b/Assets/UVC4UnityAndroidPlugin/Scripts/AndroidUtils.cs @@ -61,7 +61,7 @@ namespace Serenegiant void Awake() { #if (!NDEBUG && DEBUG && ENABLE_LOG) - Console.WriteLine($"{TAG}Awake:"); + Debug.Log($"{TAG}Awake:"); #endif #if UNITY_ANDROID Input.backButtonLeavesApp = true; // 端末のバックキーでアプリを終了できるようにする @@ -78,7 +78,7 @@ namespace Serenegiant public void OnStartEvent() { #if (!NDEBUG && DEBUG && ENABLE_LOG) - Console.WriteLine($"{TAG}OnStartEvent:"); + Debug.Log($"{TAG}OnStartEvent:"); #endif } @@ -88,7 +88,7 @@ namespace Serenegiant public void OnResumeEvent() { #if (!NDEBUG && DEBUG && ENABLE_LOG) - Console.WriteLine($"{TAG}OnResumeEvent:"); + Debug.Log($"{TAG}OnResumeEvent:"); #endif LifecycleEvent?.Invoke(true); } @@ -99,7 +99,7 @@ namespace Serenegiant public void OnPauseEvent() { #if (!NDEBUG && DEBUG && ENABLE_LOG) - Console.WriteLine($"{TAG}OnPauseEvent:"); + Debug.Log($"{TAG}OnPauseEvent:"); #endif LifecycleEvent?.Invoke(false); } @@ -110,7 +110,7 @@ namespace Serenegiant public void OnStopEvent() { #if (!NDEBUG && DEBUG && ENABLE_LOG) - Console.WriteLine($"{TAG}OnStopEvent:"); + Debug.Log($"{TAG}OnStopEvent:"); #endif } @@ -120,7 +120,7 @@ namespace Serenegiant public void OnPermissionGrant() { #if (!NDEBUG && DEBUG && ENABLE_LOG) - Console.WriteLine($"{TAG}OnPermissionGrant:"); + Debug.Log($"{TAG}OnPermissionGrant:"); #endif grantResult = PermissionGrantResult.PERMISSION_GRANT; isPermissionRequesting = false; @@ -132,7 +132,7 @@ namespace Serenegiant public void OnPermissionDeny() { #if (!NDEBUG && DEBUG && ENABLE_LOG) - Console.WriteLine($"{TAG}OnPermissionDeny:"); + Debug.Log($"{TAG}OnPermissionDeny:"); #endif grantResult = PermissionGrantResult.PERMISSION_DENY; isPermissionRequesting = false; @@ -144,7 +144,7 @@ namespace Serenegiant public void OnPermissionDenyAndNeverAskAgain() { #if (!NDEBUG && DEBUG && ENABLE_LOG) - Console.WriteLine($"{TAG}OnPermissionDenyAndNeverAskAgain:"); + Debug.Log($"{TAG}OnPermissionDenyAndNeverAskAgain:"); #endif grantResult = PermissionGrantResult.PERMISSION_DENY_AND_NEVER_ASK_AGAIN; isPermissionRequesting = false; @@ -158,7 +158,7 @@ namespace Serenegiant private void Initialize() { #if (!NDEBUG && DEBUG && ENABLE_LOG) - Console.WriteLine($"{TAG}Initialize:{gameObject.name}"); + Debug.Log($"{TAG}Initialize:{gameObject.name}"); #endif using (AndroidJavaClass clazz = new AndroidJavaClass(FQCN_PLUGIN)) { @@ -204,7 +204,7 @@ namespace Serenegiant public static IEnumerator RequestPermission(string permission, OnPermission callback) { #if (!NDEBUG && DEBUG && ENABLE_LOG) - Console.WriteLine($"{TAG}GrantPermission:{permission}"); + Debug.Log($"{TAG}GrantPermission:{permission}"); #endif if (!HasPermission(permission)) { @@ -245,7 +245,7 @@ namespace Serenegiant public static IEnumerator GrantPermission(string permission, OnPermission callback) { #if (!NDEBUG && DEBUG && ENABLE_LOG) - Console.WriteLine($"{TAG}GrantPermission:{permission}"); + Debug.Log($"{TAG}GrantPermission:{permission}"); #endif if (!HasPermission(permission)) { @@ -284,7 +284,7 @@ namespace Serenegiant public static IEnumerator GrantCameraPermission(OnPermission callback) { #if (!NDEBUG && DEBUG && ENABLE_LOG) - Console.WriteLine($"{TAG}GrantCameraPermission:"); + Debug.Log($"{TAG}GrantCameraPermission:"); #endif if (CheckAndroidVersion(23)) { diff --git a/Assets/UVC4UnityAndroidPlugin/Scripts/UVCDrawer.cs b/Assets/UVC4UnityAndroidPlugin/Scripts/UVCDrawer.cs index c4312b5..d5a9596 100644 --- a/Assets/UVC4UnityAndroidPlugin/Scripts/UVCDrawer.cs +++ b/Assets/UVC4UnityAndroidPlugin/Scripts/UVCDrawer.cs @@ -67,7 +67,7 @@ namespace Serenegiant.UVC void Start() { #if (!NDEBUG && DEBUG && ENABLE_LOG) - Console.WriteLine($"{TAG}Start:"); + Debug.Log($"{TAG}Start:"); #endif UpdateTarget(); @@ -91,7 +91,7 @@ namespace Serenegiant.UVC public bool OnUVCAttachEvent(UVCManager manager, UVCDevice device) { #if (!NDEBUG && DEBUG && ENABLE_LOG) - Console.WriteLine($"{TAG}OnUVCAttachEvent:{device}"); + Debug.Log($"{TAG}OnUVCAttachEvent:{device}"); #endif // XXX 今の実装では基本的に全てのUVC機器を受け入れる // ただしTHETA SとTHETA VとTHETA Z1は映像を取得できないインターフェースがあるのでオミットする @@ -112,7 +112,7 @@ namespace Serenegiant.UVC public void OnUVCDetachEvent(UVCManager manager, UVCDevice device) { #if (!NDEBUG && DEBUG && ENABLE_LOG) - Console.WriteLine($"{TAG}OnUVCDetachEvent:{device}"); + Debug.Log($"{TAG}OnUVCDetachEvent:{device}"); #endif } @@ -126,26 +126,26 @@ namespace Serenegiant.UVC // public SupportedFormats.Size OnUVCSelectSize(UVCManager manager, UVCDevice device, SupportedFormats formats) // { //#if (!NDEBUG && DEBUG && ENABLE_LOG) -// Console.WriteLine($"{TAG}OnUVCSelectSize:{device}"); +// Debug.Log($"{TAG}OnUVCSelectSize:{device}"); //#endif // if (device.IsTHETA_V || device.IsTHETA_Z1) // { //#if (!NDEBUG && DEBUG && ENABLE_LOG) -// Console.WriteLine($"{TAG}OnUVCSelectSize:THETA V/Z1"); +// Debug.Log($"{TAG}OnUVCSelectSize:THETA V/Z1"); //#endif // return FindSize(formats, 3840, 1920); // } // else if (device.IsTHETA_S) // { //#if (!NDEBUG && DEBUG && ENABLE_LOG) -// Console.WriteLine($"{TAG}OnUVCSelectSize:THETA S"); +// Debug.Log($"{TAG}OnUVCSelectSize:THETA S"); //#endif // return FindSize(formats, 1920, 1080); // } // else // { //#if (!NDEBUG && DEBUG && ENABLE_LOG) -// Console.WriteLine($"{TAG}OnUVCSelectSize:other UVC device,{device}"); +// Debug.Log($"{TAG}OnUVCSelectSize:other UVC device,{device}"); //#endif // return formats.Find(DefaultWidth, DefaultHeight); // } @@ -172,7 +172,7 @@ namespace Serenegiant.UVC public void OnUVCStartEvent(UVCManager manager, UVCDevice device, Texture tex) { #if (!NDEBUG && DEBUG && ENABLE_LOG) - Console.WriteLine($"{TAG}OnUVCStartEvent:{device}"); + Debug.Log($"{TAG}OnUVCStartEvent:{device}"); #endif HandleOnStartPreview(tex); } @@ -186,7 +186,7 @@ namespace Serenegiant.UVC public void OnUVCStopEvent(UVCManager manager, UVCDevice device) { #if (!NDEBUG && DEBUG && ENABLE_LOG) - Console.WriteLine($"{TAG}OnUVCStopEvent:{device}"); + Debug.Log($"{TAG}OnUVCStopEvent:{device}"); #endif HandleOnStopPreview(); } @@ -214,7 +214,7 @@ namespace Serenegiant.UVC found = true; } #if (!NDEBUG && DEBUG && ENABLE_LOG) - Console.WriteLine($"{TAG}UpdateTarget:material={material}"); + Debug.Log($"{TAG}UpdateTarget:material={material}"); #endif } i++; @@ -313,7 +313,7 @@ namespace Serenegiant.UVC } catch { - Console.WriteLine($"{TAG}RestoreTexture:Exception cought"); + Debug.Log($"{TAG}RestoreTexture:Exception cought"); } SavedTextures[i] = null; quaternions[i] = Quaternion.identity; @@ -335,7 +335,7 @@ namespace Serenegiant.UVC private void HandleOnStartPreview(Texture tex) { #if (!NDEBUG && DEBUG && ENABLE_LOG) - Console.WriteLine($"{TAG}HandleOnStartPreview:({tex})"); + Debug.Log($"{TAG}HandleOnStartPreview:({tex})"); #endif int i = 0; foreach (var target in TargetMaterials) @@ -343,7 +343,7 @@ namespace Serenegiant.UVC if (target is Material) { #if (!NDEBUG && DEBUG && ENABLE_LOG) - Console.WriteLine($"{TAG}HandleOnStartPreview:assign Texture to Material({target})"); + Debug.Log($"{TAG}HandleOnStartPreview:assign Texture to Material({target})"); #endif SavedTextures[i++] = (target as Material).mainTexture; (target as Material).mainTexture = tex; @@ -351,7 +351,7 @@ namespace Serenegiant.UVC else if (target is RawImage) { #if (!NDEBUG && DEBUG && ENABLE_LOG) - Console.WriteLine($"{TAG}HandleOnStartPreview:assign Texture to RawImage({target})"); + Debug.Log($"{TAG}HandleOnStartPreview:assign Texture to RawImage({target})"); #endif SavedTextures[i++] = (target as RawImage).texture; (target as RawImage).texture = tex; @@ -365,12 +365,12 @@ namespace Serenegiant.UVC private void HandleOnStopPreview() { #if (!NDEBUG && DEBUG && ENABLE_LOG) - Console.WriteLine($"{TAG}HandleOnStopPreview:"); + Debug.Log($"{TAG}HandleOnStopPreview:"); #endif // 描画先のテクスチャをもとに戻す RestoreTexture(); #if (!NDEBUG && DEBUG && ENABLE_LOG) - Console.WriteLine($"{TAG}HandleOnStopPreview:finished"); + Debug.Log($"{TAG}HandleOnStopPreview:finished"); #endif } diff --git a/Assets/UVC4UnityAndroidPlugin/Scripts/UVCFilter.cs b/Assets/UVC4UnityAndroidPlugin/Scripts/UVCFilter.cs index 1b4f4e6..cc89231 100644 --- a/Assets/UVC4UnityAndroidPlugin/Scripts/UVCFilter.cs +++ b/Assets/UVC4UnityAndroidPlugin/Scripts/UVCFilter.cs @@ -122,7 +122,7 @@ namespace Serenegiant.UVC } #if (!NDEBUG && DEBUG && ENABLE_LOG) - Console.WriteLine($"{TAG}Match({device}):result={result}"); + Debug.Log($"{TAG}Match({device}):result={result}"); #endif return result; } diff --git a/Assets/UVC4UnityAndroidPlugin/Scripts/UVCManager.cs b/Assets/UVC4UnityAndroidPlugin/Scripts/UVCManager.cs index a3266d2..ec60401 100644 --- a/Assets/UVC4UnityAndroidPlugin/Scripts/UVCManager.cs +++ b/Assets/UVC4UnityAndroidPlugin/Scripts/UVCManager.cs @@ -13,142 +13,144 @@ using UnityEngine; #if UNITY_ANDROID && UNITY_2018_3_OR_NEWER using UnityEngine.Android; +using Yoga; #endif namespace Serenegiant.UVC { [RequireComponent(typeof(AndroidUtils))] - public class UVCManager : MonoBehaviour - { - private const string TAG = "UVCManager#"; - private const string FQCN_DETECTOR = "com.serenegiant.usb.DeviceDetectorFragment"; + public class UVCManager : MonoSingleton + { + private const string TAG = "UVCManager#"; + private const string FQCN_DETECTOR = "com.serenegiant.usb.DeviceDetectorFragment"; private const int FRAME_TYPE_MJPEG = 0x000007; private const int FRAME_TYPE_H264 = 0x000014; private const int FRAME_TYPE_H264_FRAME = 0x030011; - - /** + + /** * IUVCSelectorがセットされていないとき * またはIUVCSelectorが解像度選択時にnullを * 返したときのデフォルトの解像度(幅) */ - public Int32 DefaultWidth = 1280; - /** + public Int32 DefaultWidth = 1280; + /** * IUVCSelectorがセットされていないとき * またはIUVCSelectorが解像度選択時にnullを * 返したときのデフォルトの解像度(高さ) */ - public Int32 DefaultHeight = 720; - /** + public Int32 DefaultHeight = 720; + /** * UVC機器とのネゴシエーション時に * H.264を優先してネゴシエーションするかどうか * Android実機のみ有効 * true: H.264 > MJPEG > YUV * false: MJPEG > H.264 > YUV */ - public bool PreferH264 = false; + public bool PreferH264 = false; /** * シーンレンダリングの前にUVC機器映像のテクスチャへのレンダリング要求を行うかどうか */ public bool RenderBeforeSceneRendering = false; - - /** + + /** * UVC関係のイベンドハンドラー */ - [SerializeField, ComponentRestriction(typeof(IUVCDrawer))] - public Component[] UVCDrawers; + [SerializeField, ComponentRestriction(typeof(IUVCDrawer))] + public Component[] UVCDrawers; - /** + /** * 使用中のカメラ情報を保持するホルダークラス */ - public class CameraInfo - { - internal readonly UVCDevice device; - internal Texture previewTexture; + public class CameraInfo + { + internal readonly UVCDevice device; + internal Texture previewTexture; internal int frameType; - internal Int32 activeId; - private Int32 currentWidth; - private Int32 currentHeight; + internal Int32 activeId; + private Int32 currentWidth; + private Int32 currentHeight; private bool isRenderBeforeSceneRendering; private bool isRendering; internal CameraInfo(UVCDevice device) - { - this.device = device; - } + { + this.device = device; + } - /** + /** * 機器idを取得 */ - public Int32 Id{ - get { return device.id; } - } - - /** + public Int32 Id + { + get { return device.id; } + } + + /** * 機器名を取得 */ - public string DeviceName - { - get { return device.name; } - } + public string DeviceName + { + get { return device.name; } + } - /** + /** * ベンダーIDを取得 */ - public int Vid - { - get { return device.vid; } - } + public int Vid + { + get { return device.vid; } + } - /** + /** * プロダクトIDを取得 */ - public int Pid - { - get { return device.pid; } - } + public int Pid + { + get { return device.pid; } + } - /** + /** * 映像取得中かどうか */ - public bool IsPreviewing - { - get { return (activeId != 0) && (previewTexture != null); } - } + public bool IsPreviewing + { + get { return (activeId != 0) && (previewTexture != null); } + } - /** + /** * 現在の解像度(幅) * プレビュー中でなければ0 */ - public Int32 CurrentWidth - { - get { return currentWidth; } - } + public Int32 CurrentWidth + { + get { return currentWidth; } + } - /** + /** * 現在の解像度(高さ) * プレビュー中でなければ0 */ - public Int32 CurrentHeight - { - get { return currentHeight; } - } + public Int32 CurrentHeight + { + get { return currentHeight; } + } - /** + /** * 現在の解像度を変更 * @param width * @param height */ - internal void SetSize(Int32 width, Int32 height) - { - currentWidth = width; - currentHeight = height; - } + internal void SetSize(Int32 width, Int32 height) + { + currentWidth = width; + currentHeight = height; + } - public override string ToString() - { - return $"{base.ToString()}({currentWidth}x{currentHeight},id={Id},activeId={activeId},IsPreviewing={IsPreviewing})"; - } + public override string ToString() + { + return $"{base.ToString()}({currentWidth}x{currentHeight},id={Id},activeId={activeId},IsPreviewing={IsPreviewing})"; + } /** * UVC機器からの映像のレンダリングを開始 @@ -162,7 +164,8 @@ namespace Serenegiant.UVC if (renderBeforeSceneRendering) { return manager.StartCoroutine(OnRenderBeforeSceneRendering()); - } else + } + else { return manager.StartCoroutine(OnRender()); } @@ -194,15 +197,15 @@ namespace Serenegiant.UVC * シーンレンダリングの前にUVC機器からの映像をテクスチャへレンダリング要求する */ private IEnumerator OnRenderBeforeSceneRendering() - { - var renderEventFunc = GetRenderEventFunc(); - for (; activeId != 0;) - { - yield return null; - GL.IssuePluginEvent(renderEventFunc, activeId); - } - yield break; - } + { + var renderEventFunc = GetRenderEventFunc(); + for (; activeId != 0;) + { + yield return null; + GL.IssuePluginEvent(renderEventFunc, activeId); + } + yield break; + } /** * レンダーイベント処理用 @@ -226,79 +229,80 @@ namespace Serenegiant.UVC * メインスレッド上で実行するためのSynchronizationContextインスタンス */ private SynchronizationContext mainContext; - /** + /** * 端末に接続されたUVC機器の状態が変化した時のイベントコールバックを受け取るデリゲーター */ - private OnDeviceChangedCallbackManager.OnDeviceChangedFunc callback; - /** + private OnDeviceChangedCallbackManager.OnDeviceChangedFunc callback; + /** * 端末に接続されたUVC機器リスト */ - private List attachedDevices = new List(); - /** + private List attachedDevices = new List(); + /** * 映像取得中のUVC機器のマップ * 機器識別用のid - CameraInfoペアを保持する */ - private Dictionary cameraInfos = new Dictionary(); + private Dictionary cameraInfos = new Dictionary(); //-------------------------------------------------------------------------------- // UnityEngineからの呼び出し //-------------------------------------------------------------------------------- // Start is called before the first frame update IEnumerator Start() - { + { + DontDestroyOnLoad(gameObject); #if (!NDEBUG && DEBUG && ENABLE_LOG) - Console.WriteLine($"{TAG}Start:"); + Debug.Log($"{TAG}Start:"); #endif - mainContext = SynchronizationContext.Current; + mainContext = SynchronizationContext.Current; callback = OnDeviceChangedCallbackManager.Add(this); - - yield return Initialize(); - } + + yield return Initialize(); + } #if (!NDEBUG && DEBUG && ENABLE_LOG) - void OnApplicationFocus() - { - Console.WriteLine($"{TAG}OnApplicationFocus:"); - } + void OnApplicationFocus() + { + Debug.Log($"{TAG}OnApplicationFocus:"); + } #endif #if (!NDEBUG && DEBUG && ENABLE_LOG) - void OnApplicationPause(bool pauseStatus) - { - Console.WriteLine($"{TAG}OnApplicationPause:{pauseStatus}"); - } + void OnApplicationPause(bool pauseStatus) + { + Debug.Log($"{TAG}OnApplicationPause:{pauseStatus}"); + } #endif #if (!NDEBUG && DEBUG && ENABLE_LOG) - void OnApplicationQuits() - { - Console.WriteLine($"{TAG}OnApplicationQuits:"); - } + void OnApplicationQuits() + { + Debug.Log($"{TAG}OnApplicationQuits:"); + } #endif - void OnDestroy() - { + void OnDestroy() + { #if (!NDEBUG && DEBUG && ENABLE_LOG) - Console.WriteLine($"{TAG}OnDestroy:"); + Debug.Log($"{TAG}OnDestroy:"); #endif - StopAll(); + StopAll(); OnDeviceChangedCallbackManager.Remove(this); - } + } - //-------------------------------------------------------------------------------- - // UVC機器接続状態が変化したときのプラグインからのコールバック関数 - //-------------------------------------------------------------------------------- + //-------------------------------------------------------------------------------- + // UVC機器接続状態が変化したときのプラグインからのコールバック関数 + //-------------------------------------------------------------------------------- public void OnDeviceChanged(IntPtr devicePtr, bool attached) { var id = UVCDevice.GetId(devicePtr); #if (!NDEBUG && DEBUG && ENABLE_LOG) - Console.WriteLine($"{TAG}OnDeviceChangedInternal:id={id},attached={attached}"); + Debug.Log($"{TAG}OnDeviceChangedInternal:id={id},attached={attached}"); #endif if (attached) { UVCDevice device = new UVCDevice(devicePtr); #if (!NDEBUG && DEBUG && ENABLE_LOG) - Console.WriteLine($"{TAG}OnDeviceChangedInternal:device={device.ToString()}"); + Debug.Log($"{TAG}OnDeviceChangedInternal:device={device.ToString()}"); #endif if (HandleOnAttachEvent(device)) { @@ -320,103 +324,104 @@ namespace Serenegiant.UVC } } } - + //================================================================================ /** * 接続中のUVC機器一覧を取得 * @return 接続中のUVC機器一覧List */ public List GetAttachedDevices() - { - var result = new List(cameraInfos.Count); + { + var result = new List(cameraInfos.Count); - foreach (var info in cameraInfos.Values) - { - result.Add(info); - } + foreach (var info in cameraInfos.Values) + { + result.Add(info); + } - return result; - } + return result; + } -// /** -// * 対応解像度を取得 -// * @param camera 対応解像度を取得するUVC機器を指定 -// * @return 対応解像度 既にカメラが取り外されている/closeしているのであればnull -// */ -// public SupportedFormats GetSupportedVideoSize(CameraInfo camera) -// { -// var info = (camera != null) ? Get(camera.DeviceName) : null; -// if ((info != null) && info.IsOpen) -// { -// return GetSupportedVideoSize(info.DeviceName); -// } -// else -// { -// return null; -// } -// } + // /** + // * 対応解像度を取得 + // * @param camera 対応解像度を取得するUVC機器を指定 + // * @return 対応解像度 既にカメラが取り外されている/closeしているのであればnull + // */ + // public SupportedFormats GetSupportedVideoSize(CameraInfo camera) + // { + // var info = (camera != null) ? Get(camera.DeviceName) : null; + // if ((info != null) && info.IsOpen) + // { + // return GetSupportedVideoSize(info.DeviceName); + // } + // else + // { + // return null; + // } + // } -// /** -// * 解像度を変更 -// * @param 解像度を変更するUVC機器を指定 -// * @param 変更する解像度を指定, nullならデフォルトに戻す -// * @param 解像度が変更されたかどうか -// */ -// public bool SetVideoSize(CameraInfo camera, SupportedFormats.Size size) -// { -// var info = (camera != null) ? Get(camera.DeviceName) : null; -// var width = size != null ? size.Width : DefaultWidth; -// var height = size != null ? size.Height : DefaultHeight; -// if ((info != null) && info.IsPreviewing) -// { -// if ((width != info.CurrentWidth) || (height != info.CurrentHeight)) -// { // 解像度が変更になるとき -// StopPreview(info.DeviceName); -// StartPreview(info.DeviceName, width, height); -// return true; -// } -// } -// return false; -// } + // /** + // * 解像度を変更 + // * @param 解像度を変更するUVC機器を指定 + // * @param 変更する解像度を指定, nullならデフォルトに戻す + // * @param 解像度が変更されたかどうか + // */ + // public bool SetVideoSize(CameraInfo camera, SupportedFormats.Size size) + // { + // var info = (camera != null) ? Get(camera.DeviceName) : null; + // var width = size != null ? size.Width : DefaultWidth; + // var height = size != null ? size.Height : DefaultHeight; + // if ((info != null) && info.IsPreviewing) + // { + // if ((width != info.CurrentWidth) || (height != info.CurrentHeight)) + // { // 解像度が変更になるとき + // StopPreview(info.DeviceName); + // StartPreview(info.DeviceName, width, height); + // return true; + // } + // } + // return false; + // } - private void StartPreview(UVCDevice device) - { - var info = CreateIfNotExist(device); - if ((info != null) && !info.IsPreviewing) { + private void StartPreview(UVCDevice device) + { + var info = CreateIfNotExist(device); + if ((info != null) && !info.IsPreviewing) + { - int width = DefaultWidth; - int height = DefaultHeight; + int width = DefaultWidth; + int height = DefaultHeight; -// var supportedVideoSize = GetSupportedVideoSize(deviceName); -// if (supportedVideoSize == null) -// { -// throw new ArgumentException("fauled to get supported video size"); -// } + // var supportedVideoSize = GetSupportedVideoSize(deviceName); + // if (supportedVideoSize == null) + // { + // throw new ArgumentException("fauled to get supported video size"); + // } -// // 解像度の選択処理 -// if ((UVCDrawers != null) && (UVCDrawers.Length > 0)) -// { -// foreach (var drawer in UVCDrawers) -// { -// if ((drawer is IUVCDrawer) && ((drawer as IUVCDrawer).CanDraw(this, info.device))) -// { -// var size = (drawer as IUVCDrawer).OnUVCSelectSize(this, info.device, supportedVideoSize); -//#if (!NDEBUG && DEBUG && ENABLE_LOG) -// Console.WriteLine($"{TAG}StartPreview:selected={size}"); -//#endif -// if (size != null) -// { // 一番最初に見つかった描画可能なIUVCDrawersがnull以外を返せばそれを使う -// width = size.Width; -// height = size.Height; -// break; -// } -// } -// } -// } + // // 解像度の選択処理 + // if ((UVCDrawers != null) && (UVCDrawers.Length > 0)) + // { + // foreach (var drawer in UVCDrawers) + // { + // if ((drawer is IUVCDrawer) && ((drawer as IUVCDrawer).CanDraw(this, info.device))) + // { + // var size = (drawer as IUVCDrawer).OnUVCSelectSize(this, info.device, supportedVideoSize); + //#if (!NDEBUG && DEBUG && ENABLE_LOG) + // Debug.Log($"{TAG}StartPreview:selected={size}"); + //#endif + // if (size != null) + // { // 一番最初に見つかった描画可能なIUVCDrawersがnull以外を返せばそれを使う + // width = size.Width; + // height = size.Height; + // break; + // } + // } + // } + // } - // FIXME 対応解像度の確認処理 + // FIXME 対応解像度の確認処理 #if (!NDEBUG && DEBUG && ENABLE_LOG) - Console.WriteLine($"{TAG}StartPreview:({width}x{height}),id={device.id}"); + Debug.Log($"{TAG}StartPreview:({width}x{height}),id={device.id}"); #endif int[] frameTypes = { PreferH264 ? FRAME_TYPE_H264 : FRAME_TYPE_MJPEG, @@ -430,278 +435,290 @@ namespace Serenegiant.UVC break; } } - - info.SetSize(width, height); - info.activeId = device.id; - mainContext.Post(__ => - { // テクスチャの生成はメインスレッドで行わないといけない + + info.SetSize(width, height); + info.activeId = device.id; + mainContext.Post(__ => + { // テクスチャの生成はメインスレッドで行わないといけない #if (!NDEBUG && DEBUG && ENABLE_LOG) - Console.WriteLine($"{TAG}映像受け取り用テクスチャ生成:({width}x{height})"); + Debug.Log($"{TAG}映像受け取り用テクスチャ生成:({width}x{height})"); #endif - Texture2D tex = new Texture2D( - width, height, - TextureFormat.ARGB32, - false, /* mipmap */ - true /* linear */); - tex.filterMode = FilterMode.Point; - tex.Apply(); - info.previewTexture = tex; - var nativeTexPtr = info.previewTexture.GetNativeTexturePtr(); - Start(device.id, nativeTexPtr.ToInt32()); - HandleOnStartPreviewEvent(info); - info.StartRender(this, RenderBeforeSceneRendering); - }, null); - } - } + Texture2D tex = new Texture2D( + width, height, + TextureFormat.ARGB32, + false, /* mipmap */ + true /* linear */); + tex.filterMode = FilterMode.Point; + tex.Apply(); + info.previewTexture = tex; + var nativeTexPtr = info.previewTexture.GetNativeTexturePtr(); + Start(device.id, nativeTexPtr.ToInt32()); + HandleOnStartPreviewEvent(info); + info.StartRender(this, RenderBeforeSceneRendering); + }, null); + } + } - private void StopPreview(UVCDevice device) { - var info = Get(device); - if ((info != null) && info.IsPreviewing) - { - mainContext.Post(__ => - { - HandleOnStopPreviewEvent(info); - Stop(device.id); - info.StopRender(this); - info.SetSize(0, 0); - info.activeId = 0; - }, null); - } - } + private void StopPreview(UVCDevice device) + { + var info = Get(device); + if ((info != null) && info.IsPreviewing) + { + mainContext.Post(__ => + { + HandleOnStopPreviewEvent(info); + Stop(device.id); + info.StopRender(this); + info.SetSize(0, 0); + info.activeId = 0; + }, null); + } + } - private void StopAll() { - List values = new List(cameraInfos.Values); - foreach (var info in values) - { - StopPreview(info.device); - } - } + public void StopAll() + { + List values = new List(cameraInfos.Values); + foreach (var info in values) + { + StopPreview(info.device); + } + } - //-------------------------------------------------------------------------------- - /** + //-------------------------------------------------------------------------------- + /** * UVC機器が接続されたときの処理の実体 * @param info * @return true: 接続されたUVC機器を使用する, false: 接続されたUVC機器を使用しない */ - private bool HandleOnAttachEvent(UVCDevice device/*NonNull*/) - { - if ((UVCDrawers == null) || (UVCDrawers.Length == 0)) - { // IUVCDrawerが割り当てられていないときはtrue(接続されたUVC機器を使用する)を返す - return true; - } - else - { - bool hasDrawer = false; - foreach (var drawer in UVCDrawers) - { - if (drawer is IUVCDrawer) - { - hasDrawer = true; - if ((drawer as IUVCDrawer).OnUVCAttachEvent(this, device)) - { // どれか1つのIUVCDrawerがtrueを返せばtrue(接続されたUVC機器を使用する)を返す - return true; - } - } - } - // IUVCDrawerが割り当てられていないときはtrue(接続されたUVC機器を使用する)を返す - return !hasDrawer; - } - } + private bool HandleOnAttachEvent(UVCDevice device/*NonNull*/) + { + if ((UVCDrawers == null) || (UVCDrawers.Length == 0)) + { // IUVCDrawerが割り当てられていないときはtrue(接続されたUVC機器を使用する)を返す + return true; + } + else + { + bool hasDrawer = false; + Debug.LogWarning("UVCDrawers: " + UVCDrawers.Length); + foreach (var drawer in UVCDrawers) + { + if (drawer is IUVCDrawer) + { + hasDrawer = true; + if ((drawer as IUVCDrawer).OnUVCAttachEvent(this, device)) + { // どれか1つのIUVCDrawerがtrueを返せばtrue(接続されたUVC機器を使用する)を返す + return true; + } + } + } + // IUVCDrawerが割り当てられていないときはtrue(接続されたUVC機器を使用する)を返す + return !hasDrawer; + } + } - /** + /** * UVC機器が取り外されたときの処理の実体 * @param info */ - private void HandleOnDetachEvent(UVCDevice device/*NonNull*/) - { - if ((UVCDrawers != null) && (UVCDrawers.Length > 0)) - { - foreach (var drawer in UVCDrawers) - { - if (drawer is IUVCDrawer) - { - (drawer as IUVCDrawer).OnUVCDetachEvent(this, device); - } - } - } - } + private void HandleOnDetachEvent(UVCDevice device/*NonNull*/) + { + if ((UVCDrawers != null) && (UVCDrawers.Length > 0)) + { + foreach (var drawer in UVCDrawers) + { + if (drawer is IUVCDrawer) + { + (drawer as IUVCDrawer).OnUVCDetachEvent(this, device); + } + } + } + } - /** + /** * UVC機器からの映像取得を開始した * @param args UVC機器の識別文字列 */ - void HandleOnStartPreviewEvent(CameraInfo info) - { + void HandleOnStartPreviewEvent(CameraInfo info) + { #if (!NDEBUG && DEBUG && ENABLE_LOG) - Console.WriteLine($"{TAG}HandleOnStartPreviewEvent:({info})"); + Debug.Log($"{TAG}HandleOnStartPreviewEvent:({info})"); #endif - if ((info != null) && info.IsPreviewing && (UVCDrawers != null)) - { - foreach (var drawer in UVCDrawers) - { - if ((drawer is IUVCDrawer) && (drawer as IUVCDrawer).CanDraw(this, info.device)) - { - (drawer as IUVCDrawer).OnUVCStartEvent(this, info.device, info.previewTexture); - } - } - } else { + if ((info != null) && info.IsPreviewing && (UVCDrawers != null)) + { + foreach (var drawer in UVCDrawers) + { + if ((drawer is IUVCDrawer) && (drawer as IUVCDrawer).CanDraw(this, info.device)) + { + (drawer as IUVCDrawer).OnUVCStartEvent(this, info.device, info.previewTexture); + } + } + } + else + { #if (!NDEBUG && DEBUG && ENABLE_LOG) - Console.WriteLine($"{TAG}HandleOnStartPreviewEvent:No UVCDrawers"); + Debug.Log($"{TAG}HandleOnStartPreviewEvent:No UVCDrawers"); #endif - } - } + } + } - /** + /** * UVC機器からの映像取得を終了した * @param args UVC機器の識別文字列 */ - void HandleOnStopPreviewEvent(CameraInfo info) - { + void HandleOnStopPreviewEvent(CameraInfo info) + { #if (!NDEBUG && DEBUG && ENABLE_LOG) - Console.WriteLine($"{TAG}HandleOnStopPreviewEvent:({info})"); + Debug.Log($"{TAG}HandleOnStopPreviewEvent:({info})"); #endif - if (UVCDrawers != null) - { - foreach (var drawer in UVCDrawers) - { - if ((drawer is IUVCDrawer) && (drawer as IUVCDrawer).CanDraw(this, info.device)) - { - (drawer as IUVCDrawer).OnUVCStopEvent(this, info.device); - } - } - } - } + if (UVCDrawers != null) + { + foreach (var drawer in UVCDrawers) + { + if ((drawer is IUVCDrawer) && (drawer as IUVCDrawer).CanDraw(this, info.device)) + { + (drawer as IUVCDrawer).OnUVCStopEvent(this, info.device); + } + } + } + } - //-------------------------------------------------------------------------------- - /** + //-------------------------------------------------------------------------------- + /** * 指定したUVC識別文字列に対応するCameraInfoを取得する * まだ登録させていなければ新規作成する * @param deviceName UVC機器識別文字列 * @param CameraInfoを返す */ - /*NonNull*/ - private CameraInfo CreateIfNotExist(UVCDevice device) - { - if (!cameraInfos.ContainsKey(device.id)) - { - cameraInfos[device.id] = new CameraInfo(device); - } - return cameraInfos[device.id]; - } + /*NonNull*/ + private CameraInfo CreateIfNotExist(UVCDevice device) + { + if (!cameraInfos.ContainsKey(device.id)) + { + cameraInfos[device.id] = new CameraInfo(device); + } + return cameraInfos[device.id]; + } - /** + /** * 指定したUVC識別文字列に対応するCameraInfoを取得する * @param deviceName UVC機器識別文字列 * @param 登録してあればCameraInfoを返す、登録されていなければnull */ - /*Nullable*/ - private CameraInfo Get(UVCDevice device) - { - return cameraInfos.ContainsKey(device.id) ? cameraInfos[device.id] : null; - } + /*Nullable*/ + private CameraInfo Get(UVCDevice device) + { + return cameraInfos.ContainsKey(device.id) ? cameraInfos[device.id] : null; + } - //-------------------------------------------------------------------------------- - /** + //-------------------------------------------------------------------------------- + /** * プラグインを初期化 * パーミッションの確認を行って取得できれば実際のプラグイン初期化処理#InitPluginを呼び出す */ - private IEnumerator Initialize() - { + private IEnumerator Initialize() + { #if (!NDEBUG && DEBUG && ENABLE_LOG) - Console.WriteLine($"{TAG}Initialize:"); + Debug.Log($"{TAG}Initialize:"); #endif - if (AndroidUtils.CheckAndroidVersion(28)) - { - yield return AndroidUtils.GrantCameraPermission((string permission, AndroidUtils.PermissionGrantResult result) => - { + if (AndroidUtils.CheckAndroidVersion(28)) + { + yield return AndroidUtils.GrantCameraPermission((string permission, AndroidUtils.PermissionGrantResult result) => + { #if (!NDEBUG && DEBUG && ENABLE_LOG) - Console.WriteLine($"{TAG}OnPermission:{permission}={result}"); + Debug.Log($"{TAG}OnPermission:{permission}={result}"); #endif - switch (result) - { - case AndroidUtils.PermissionGrantResult.PERMISSION_GRANT: - InitPlugin(); - break; - case AndroidUtils.PermissionGrantResult.PERMISSION_DENY: - if (AndroidUtils.ShouldShowRequestPermissionRationale(AndroidUtils.PERMISSION_CAMERA)) - { - // パーミッションを取得できなかった - // FIXME 説明用のダイアログ等を表示しないといけない - } - break; - case AndroidUtils.PermissionGrantResult.PERMISSION_DENY_AND_NEVER_ASK_AGAIN: - break; - } - }); - } - else - { - InitPlugin(); - } + switch (result) + { + case AndroidUtils.PermissionGrantResult.PERMISSION_GRANT: + InitPlugin(); + break; + case AndroidUtils.PermissionGrantResult.PERMISSION_DENY: + if (AndroidUtils.ShouldShowRequestPermissionRationale(AndroidUtils.PERMISSION_CAMERA)) + { + // パーミッションを取得できなかった + // FIXME 説明用のダイアログ等を表示しないといけない + } + break; + case AndroidUtils.PermissionGrantResult.PERMISSION_DENY_AND_NEVER_ASK_AGAIN: + break; + } + }); + } + else + { + InitPlugin(); + } - yield break; - } + yield break; + } - /** + /** * プラグインを初期化 * uvc-plugin-unityへの処理要求 */ - private void InitPlugin() - { + private void InitPlugin() + { #if (!NDEBUG && DEBUG && ENABLE_LOG) - Console.WriteLine($"{TAG}InitPlugin:"); + Debug.Log($"{TAG}InitPlugin:"); #endif - // IUVCDrawersが割り当てられているかどうかをチェック - var hasDrawer = false; - if ((UVCDrawers != null) && (UVCDrawers.Length > 0)) - { - foreach (var drawer in UVCDrawers) - { - if (drawer is IUVCDrawer) - { - hasDrawer = true; - break; - } - } - } - if (!hasDrawer) - { // インスペクタでIUVCDrawerが設定されていないときは - // このスクリプトがaddされているゲームオブジェクトからの取得を試みる + // IUVCDrawersが割り当てられているかどうかをチェック + var hasDrawer = false; + if ((UVCDrawers != null) && (UVCDrawers.Length > 0)) + { + foreach (var drawer in UVCDrawers) + { + if (drawer is IUVCDrawer) + { + hasDrawer = true; + break; + } + } + } + if (!hasDrawer) + { // インスペクタでIUVCDrawerが設定されていないときは + // このスクリプトがaddされているゲームオブジェクトからの取得を試みる #if (!NDEBUG && DEBUG && ENABLE_LOG) - Console.WriteLine($"{TAG}InitPlugin:has no IUVCDrawer, try to get from gameObject"); + Debug.Log($"{TAG}InitPlugin:has no IUVCDrawer, try to get from gameObject"); #endif - var drawers = GetComponents(typeof(IUVCDrawer)); - if ((drawers != null) && (drawers.Length > 0)) - { - UVCDrawers = new Component[drawers.Length]; - int i = 0; - foreach (var drawer in drawers) - { - UVCDrawers[i++] = drawer; - } - } - } + var objs = FindObjectsByType(FindObjectsSortMode.None); + List drawers = new List(); + foreach (var obj in objs) + { + drawers.AddRange(obj.GetComponents(typeof(IUVCDrawer))); + + } + Debug.LogWarning("drawers: " + drawers.Count); + if ((drawers != null) && (drawers.Count > 0)) + { + UVCDrawers = new Component[drawers.Count]; + int i = 0; + foreach (var drawer in drawers) + { + UVCDrawers[i++] = drawer; + } + } + } #if (!NDEBUG && DEBUG && ENABLE_LOG) - Console.WriteLine($"{TAG}InitPlugin:num drawers={UVCDrawers.Length}"); + Debug.Log($"{TAG}InitPlugin:num drawers={UVCDrawers.Length}"); #endif - // aandusbのDeviceDetectorを読み込み要求 - using (AndroidJavaClass clazz = new AndroidJavaClass(FQCN_DETECTOR)) - { - clazz.CallStatic("initUVCDeviceDetector", - AndroidUtils.GetCurrentActivity()); - } - } + // aandusbのDeviceDetectorを読み込み要求 + using (AndroidJavaClass clazz = new AndroidJavaClass(FQCN_DETECTOR)) + { + clazz.CallStatic("initUVCDeviceDetector", + AndroidUtils.GetCurrentActivity()); + } + } //-------------------------------------------------------------------------------- // ネイティブプラグイン関係の定義・宣言 //-------------------------------------------------------------------------------- - /** + /** * プラグインでのレンダーイベント取得用native(c/c++)関数 */ - [DllImport("unityuvcplugin")] - private static extern IntPtr GetRenderEventFunc(); + [DllImport("unityuvcplugin")] + private static extern IntPtr GetRenderEventFunc(); /** * 初期設定 */ @@ -710,19 +727,19 @@ namespace Serenegiant.UVC /** * 映像取得開始 */ - [DllImport("unityuvcplugin", EntryPoint ="Start")] - private static extern Int32 Start(Int32 deviceId, Int32 tex); - /** + [DllImport("unityuvcplugin", EntryPoint = "Start")] + private static extern Int32 Start(Int32 deviceId, Int32 tex); + /** * 映像取得終了 */ - [DllImport("unityuvcplugin", EntryPoint ="Stop")] - private static extern Int32 Stop(Int32 deviceId); - /** + [DllImport("unityuvcplugin", EntryPoint = "Stop")] + private static extern Int32 Stop(Int32 deviceId); + /** * 映像サイズ設定 */ - [DllImport("unityuvcplugin")] - private static extern Int32 Resize(Int32 deviceId, Int32 frameType, Int32 width, Int32 height); - } // UVCManager + [DllImport("unityuvcplugin")] + private static extern Int32 Resize(Int32 deviceId, Int32 frameType, Int32 width, Int32 height); + } // UVCManager /** * IL2Cppだとc/c++からのコールバックにつかうデリゲーターをマーシャリングできないので @@ -748,7 +765,7 @@ namespace Serenegiant.UVC private static extern IntPtr Unregister(Int32 id); private static Dictionary sManagers = new Dictionary(); - + /** * 指定したUVCManagerを接続機器変化コールバックに追加 */ @@ -756,7 +773,7 @@ namespace Serenegiant.UVC { Int32 id = manager.GetHashCode(); OnDeviceChangedFunc callback = new OnDeviceChangedFunc(OnDeviceChanged); - sManagers.Add(id, manager); + sManagers[id] = manager; Register(id, callback); return callback; } @@ -777,6 +794,7 @@ namespace Serenegiant.UVC var manager = sManagers.ContainsKey(id) ? sManagers[id] : null; if (manager != null) { + Debug.Log($"OnDeviceChanged{id} attached:{attached}"); manager.OnDeviceChanged(devicePtr, attached); } }