Unity-Technologies / arfoundation-samples

Example content for Unity projects based on AR Foundation
Other
2.98k stars 1.11k forks source link

[Bug]AR Foundation TryAcquireLatestCPUImage Freeze #1066

Closed Bruce-yu199 closed 1 year ago

Bruce-yu199 commented 1 year ago

Describe the bug The function TryAcquireLatestCpuImage causes the preview to freeze on IOS. I have tried #1057 and #1010 but failed.

To Reproduce Put the following MonoBehaviour on a GameObject in the scene: `using UnityEngine; using UnityEngine.XR.ARFoundation; using UnityEngine.XR.ARSubsystems;

public class TestXRCpuImage : MonoBehaviour { private IEnumerator Start() { public ARCameraManager _cameraManager; _cameraManager.frameReceived += OnCameraFrameReceived ; while(true) { TryShowLatestCpuImage(); yield return new WaitForEndOfFrame(); } }

  private unsafe void TryShowLatestCpuImage()
 {
     if (_cameraManager.TryAcquireLatestCpuImage(out var image))
    {
       var conversionParams = new XRCpuImage.ConversionParams
     {
         inputRect = new RectInt(0, 0, image.width, image.height),
         outputDimensions = new Vector2Int(_width, _height),
         outputFormat = TextureFormat.RGBA32,
         transformation = XRCpuImage.Transformation.MirrorX
      };
     var _buffer = new NativeArray<byte>(_width * _height * 4, Allocator.Temp);
     var ptr = (IntPtr)NativeArrayUnsafeUtility.GetUnsafePtr(_buffer);
     image.Convert(conversionParams, ptr, _buffer.Length);
      image.Dispose();
      _outputTexture.LoadRawTextureData(_buffer);
      _outputTexture.Apply();
     _buffer.Dispose();
}
private void OnCameraFrameReceived (ARCameraFrameEventArgs args){
   Debug.Log("OnCameraFrameReceived has been called.");
}

}`

Then build the app for IOS.

Expected behavior The image is returned and nothing happens.

Actual behavior When i build it to my iPad and run it, The first frame is returned and then image in the screen freezes no matter what pose i do. No errors are logged to the console about this. The application itself isn't locking up since all other functionallities still work like expected, it's just the camera preview. I found that _cameraManager.TryAcquireLatestCpuImage(out var image) always gives me the same image(the first frame) which caused the screen freezing.

Smartphone (please complete the following information):

ankur-unity commented 1 year ago

@Bruce-yu199 - I noticed that you're using v4.2.6 of AR Foundation and ARKit packages. This is an older patch. Can you try using the latest v4.2.7 and check if you can still reproduce the issue? https://docs.unity3d.com/Packages/com.unity.xr.arkit@4.2/changelog/CHANGELOG.html#427---2022-10-31

andyb-unity commented 1 year ago

Closing this issue due to inactivity. Feel free to re-open if you have further questions. This issue should be resolved if you update to AR Foundation 4.2.7 or newer.