简体   繁体   English

在ARKit面部追踪和后置摄像头之间进行Unity切换?

[英]Unity Switch between ARKit Face Tracking and Rear Camera?

I am making an application in Unity for iOS using ARKit. 我正在使用ARKit在Unity for iOS中创建应用程序。 I would like to have a calibration scene first, which uses the ARKit face tracking tools to measure the distance from the phone to the face. 我想先创建一个校准场景,该场景使用ARKit人脸跟踪工具来测量手机到人脸的距离。 This information is fed to the next scene which then uses the rear camera for an AR game. 该信息被馈送到下一个场景,然后下一个场景将后置摄像头用于AR游戏。

I have successfully created both scenes separately, and added a GUI button which should switch them in a phone app. 我已经成功地分别创建了两个场景,并添加了一个GUI按钮,该按钮应在电话应用程序中进行切换。 However, when I built the app to test it, pressing the button causes the app to crash. 但是,当我构建应用程序进行测试时,按下按钮会导致应用程序崩溃。

Does anyone know whether this type of camera switching on iOS is even supported? 有谁知道iOS是否支持这种类型的相机切换? If so, any suggestions on how to make it work? 如果是这样,关于如何使其工作的任何建议? Do I need to change any configurations in the scenes? 我是否需要更改场景中的任何配置?

Thank a lot! 非常感谢!

EDIT: I think the problem is that the ARSession object (which has a different configuration for face tracking and back-camera AR) is not destroyed when the scene is changed. 编辑:我认为问题在于,更改场景时,不会破坏ARSession对象(它具有用于面部跟踪和后置摄像头AR的不同配置)。 I'm not sure how to wrangle Unity into destroying this session and starting a new one. 我不确定如何破坏Unity以破坏此会话并开始新的会话。

STACK TRACE (last part)] 堆栈跟踪(最后一部分)

enter code here
=================================================================
Main Thread Checker: UI API called on a background thread: -[UIWindow rootViewController]
PID: 433, TID: 61976, Thread name: UnityGfxDeviceWorker, Queue name: com.apple.root.default-qos.overcommit, QoS: 21
Backtrace:
4   FaceRCalibration                    0x00000001080ed07c _ZN7Vuforia4initEv + 356020
5   FaceRCalibration                    0x00000001080ed49c _ZN7Vuforia4initEv + 357076
6   FaceRCalibration                    0x0000000108239de0 _ZN7Vuforia4initEv + 1719320
7   FaceRCalibration                    0x0000000108222c34 _ZN7Vuforia4initEv + 1624684
8   FaceRCalibration                    0x000000010804f9d0 onSurfaceCreated + 32
9   FaceRCalibration                    0x000000010784221c _ZN9GfxDevice26InsertCustomMarkerCallbackEPFviEi + 48
10  FaceRCalibration                    0x0000000107c4252c _ZN15GfxDeviceWorker10RunCommandER20ThreadedStreamBuffer + 11056
11  FaceRCalibration                    0x00000001073591c8 GfxDeviceWorkerAutoreleasePoolProxy + 68
12  FaceRCalibration                    0x0000000107c47fe0 _ZN15GfxDeviceWorker6RunExtER20ThreadedStreamBuffer + 84
13  FaceRCalibration                    0x0000000107c47f80 _ZN15GfxDeviceWorker3RunEv + 140
14  FaceRCalibration                    0x0000000107c3f86c _ZN15GfxDeviceWorker18RunGfxDeviceWorkerEPv + 12
15  FaceRCalibration                    0x0000000107707e5c _ZN6Thread16RunThreadWrapperEPv + 64
16  libsystem_pthread.dylib             0x0000000182d85220 <redacted> + 272
17  libsystem_pthread.dylib             0x0000000182d85110 <redacted> + 0
18  libsystem_pthread.dylib             0x0000000182d83b10 thread_start + 4
2018-08-13 15:02:44.496950+0100 FaceRCalibration[433:61976] [reports] Main Thread Checker: UI API called on a background thread: -[UIWindow rootViewController]
PID: 433, TID: 61976, Thread name: UnityGfxDeviceWorker, Queue name: com.apple.root.default-qos.overcommit, QoS: 21
Backtrace:
4   FaceRCalibration                    0x00000001080ed07c _ZN7Vuforia4initEv + 356020
5   FaceRCalibration                    0x00000001080ed49c _ZN7Vuforia4initEv + 357076
6   FaceRCalibration                    0x0000000108239de0 _ZN7Vuforia4initEv + 1719320
7   FaceRCalibration                    0x0000000108222c34 _ZN7Vuforia4initEv + 1624684
8   FaceRCalibration                    0x000000010804f9d0 onSurfaceCreated + 32
9   FaceRCalibration                    0x000000010784221c _ZN9GfxDevice26InsertCustomMarkerCallbackEPFviEi + 48
10  FaceRCalibration                    0x0000000107c4252c _ZN15GfxDeviceWorker10RunCommandER20ThreadedStreamBuffer + 11056
11  FaceRCalibration                    0x00000001073591c8 GfxDeviceWorkerAutoreleasePoolProxy + 68
12  FaceRCalibration                    0x0000000107c47fe0 _ZN15GfxDeviceWorker6RunExtER20ThreadedStreamBuffer + 84
13  FaceRCalibration                    0x0000000107c47f80 _ZN15GfxDeviceWorker3RunEv + 140
14  FaceRCalibration                    0x0000000107c3f86c _ZN15GfxDeviceWorker18RunGfxDeviceWorkerEPv + 12
15  FaceRCalibration                    0x0000000107707e5c _ZN6Thread16RunThreadWrapperEPv + 64
16  libsystem_pthread.dylib             0x0000000182d85220 <redacted> + 272
17  libsystem_pthread.dylib             0x0000000182d85110 <redacted> + 0
18  libsystem_pthread.dylib             0x0000000182d83b10 thread_start + 4
2018-08-13 15:02:44.632260+0100 FaceRCalibration[433:61976] DEBUG/AR(433) UIView has CAMetalLayer layer class
2018-08-13 15:02:44.632402+0100 FaceRCalibration[433:61976] DEBUG/AR(433) UIView does not respond to selector renderFrameVuforia
2018-08-13 15:02:44.632479+0100 FaceRCalibration[433:61976] DEBUG/AR(433) Could not find a UIView with CAEAGLLayer or CAMetalLayer layer class that responds to selector renderFrameVuforia
2018-08-13 15:02:44.864480+0100 FaceRCalibration[433:61734] [Common] _BSMachError: port da03; (os/kern) invalid capability (0x14) "Unable to insert COPY_SEND"
2018-08-13 15:02:44.887877+0100 FaceRCalibration[433:61734] [Common] _BSMachError: port da03; (os/kern) invalid capability (0x14) "Unable to insert COPY_SEND"
-> applicationWillResignActive()
ar_session_interrupted
UnityEngine.DebugLogHandler:Internal_Log(LogType, String, Object)
UnityEngine.DebugLogHandler:LogFormat(LogType, Object, String, Object[])
UnityEngine.Logger:Log(LogType, Object)
UnityEngine.Debug:Log(Object)
UnityEngine.XR.iOS.UnityARSessionNativeInterface:_ar_session_interrupted()

(Filename: /Users/builduser/buildslave/unity/build/Runtime/Export/Debug.bindings.h Line: 43)

-> applicationDidEnterBackground()
2018-08-13 15:03:00.267669+0100 FaceRCalibration[433:62230] TIC Read Status [6:0x0]: 1:57
2018-08-13 15:03:00.267723+0100 FaceRCalibration[433:62230] TIC Read Status [6:0x0]: 1:57
2018-08-13 15:03:04.129255+0100 FaceRCalibration[433:62583] dnssd_clientstub read_all(33) DEFUNCT
-> applicationWillEnterForeground()
-> applicationDidBecomeActive()
ar_session_interruption_ended
UnityEngine.DebugLogHandler:Internal_Log(LogType, String, Object)
UnityEngine.DebugLogHandler:LogFormat(LogType, Object, String, Object[])
UnityEngine.Logger:Log(LogType, Object)
UnityEngine.Debug:Log(Object)
UnityEngine.XR.iOS.UnityARSessionNativeInterface:_ar_session_interruption_ended()

(Filename: /Users/builduser/buildslave/unity/build/Runtime/Export/Debug.bindings.h Line: 43)

Unloading 6 Unused Serialized files (Serialized files now loaded: 0)
UnloadTime: 4.989750 ms
System memory in use before: 26.3 MB.
System memory in use after: 26.4 MB.

Unloading 9 unused Assets to reduce memory usage. Loaded Objects now: 2123.
Total: 3.255875 ms (FindLiveObjects: 0.764083 ms CreateObjectMapping: 0.056708 ms MarkObjects: 2.368958 ms  DeleteObjects: 0.065208 ms)

Setting up 1 worker threads for Enlighten.
  Thread -> id: 16c77f000 -> priority: 1 
libc++abi.dylib: terminating with uncaught exception of type Il2CppExceptionWrapper
(lldb) 

I have managed to fix this problem. 我设法解决了这个问题。 You have to check null for anchorPrefab in 3 functions: FaceAdded, FaceUpdated, FaceRemoved and on the OnDestroy function, just release the event delegation from beginning and hide the anchorPrefab too. 您必须在3个函数中检查anchorPrefab的null:FaceAdded,FaceUpdated,FaceRemoved以及OnDestroy函数,只需从头开始释放事件委托并隐藏anchorPrefab。 Full source code is below. 完整的源代码如下。

[SerializeField]
private GameObject anchorPrefab;

private UnityARSessionNativeInterface m_session;

// Use this for initialization
void Start()
{
    m_session = new UnityARSessionNativeInterface();

    Application.targetFrameRate = 60;
    ARKitFaceTrackingConfiguration config = new ARKitFaceTrackingConfiguration();
    config.alignment = UnityARAlignment.UnityARAlignmentGravity;
    config.enableLightEstimation = true;

    if (config.IsSupported)
    {

        m_session.RunWithConfig(config);

        UnityARSessionNativeInterface.ARFaceAnchorAddedEvent += FaceAdded;
        UnityARSessionNativeInterface.ARFaceAnchorUpdatedEvent += FaceUpdated;
        UnityARSessionNativeInterface.ARFaceAnchorRemovedEvent += FaceRemoved;

    }

}

void FaceAdded(ARFaceAnchor anchorData)
{
    if (anchorPrefab != null)
    {
        anchorPrefab.transform.position = UnityARMatrixOps.GetPosition(anchorData.transform);
        anchorPrefab.transform.rotation = UnityARMatrixOps.GetRotation(anchorData.transform);
        anchorPrefab.SetActive(true);
    }
}

void FaceUpdated(ARFaceAnchor anchorData)
{
    if (anchorPrefab != null)
    {
        anchorPrefab.transform.position = UnityARMatrixOps.GetPosition(anchorData.transform);
        anchorPrefab.transform.rotation = UnityARMatrixOps.GetRotation(anchorData.transform);
    }
}

void FaceRemoved(ARFaceAnchor anchorData)
{
    if (anchorPrefab != null)
    {
        anchorPrefab.SetActive(false);
    }
}



// Update is called once per frame
void Update()
{

}

void OnDestroy()
{
    if (anchorPrefab != null)
    {
        anchorPrefab.SetActive(false);
    }
    UnityARSessionNativeInterface.ARFaceAnchorAddedEvent -= FaceAdded;
    UnityARSessionNativeInterface.ARFaceAnchorUpdatedEvent -= FaceUpdated;
    UnityARSessionNativeInterface.ARFaceAnchorRemovedEvent -= FaceRemoved;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM