Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How to clear AR camera texture

Discussion in 'AR' started by kevinetourneau, Jul 29, 2021.

  1. kevinetourneau

    kevinetourneau

    Joined:
    Oct 3, 2017
    Posts:
    9
    Hi,

    In our app we got 3D viewer and AR viewer in the same unity scene.

    When we first start an AR session, we see a white AR texture before the camera stream.
    To go back to the 3D viewer we destroy the AR session and stop everything related to AR.
    When we start another AR session, we see the previous AR camera texture before the camera stream.

    How to correcty clear (or reset) the AR camera texture between two AR session ?
     
  2. TreyK-47

    TreyK-47

    Unity Technologies

    Joined:
    Oct 22, 2019
    Posts:
    1,816
    KyryloKuzyk likes this.
  3. kevinetourneau

    kevinetourneau

    Joined:
    Oct 3, 2017
    Posts:
    9
    Thank you @TreyK-47 for your response.

    First invocation of ARCameraManager.frameReceived event does not solve the problem.
    I have to wait 30 frames to not see the previous camera background image (temporary solution).
    Is this normal? How to make it more robust?

    Code (CSharp):
    1. int _nbFrames = 0;
    2. ARSession arSession;
    3. ARSessionOrigin arSessionOrigin;
    4.  
    5. public void StartARSession() {
    6.     arSession = GameObject.Instantiate<ARSession> (arSessionPrefb);
    7.     arSessionOrigin = GameObject.Instantiate<ARSessionOrigin> (arSessionOriginPrefab);
    8.     ...
    9.     _nbFrames = 0;
    10.     arCameraManager.frameReceived += OnFrameReceived;
    11. }
    12.  
    13. public void StopARSession() {
    14.     GameObject.DestroyImmediate (arSession.gameObject);
    15.     GameObject.DestroyImmediate (arSessionOrigin.gameObject);
    16.     ...
    17.     arCameraBackground.enabled = false;
    18.     arCameraManager.enabled = false;
    19. }
    20.  
    21. void OnFrameReceived (ARCameraFrameEventArgs args) {
    22.     _nbFrames++;
    23.     if (_nbFrames > 30) {
    24.         _nbFrames = 0;
    25.         arCameraManager.frameReceived -= OnFrameReceived;
    26.         arCameraBackground.enabled = true;
    27.     }
    28. }
     
  4. todds_unity

    todds_unity

    Joined:
    Aug 1, 2018
    Posts:
    324
    You are not following the appropriate usage of the ARSession or ARSession origin. Destroying the ARSession and/or ARSessionOrigin game objects does nothing to the global AR session. Instead you should be disabling the ARSession and then calling Reset on it. Please refer to the SimpleAR sample.

    https://github.com/Unity-Technologies/arfoundation-samples#simplear