Search Unity

Using WebCameraTexture with ARCamera causing issues

Discussion in 'AR' started by Deadshot1994, May 12, 2020.

  1. Deadshot1994

    Deadshot1994

    Joined:
    Sep 6, 2015
    Posts:
    25
    What I want to do in my AR App is have the option for the user to take a picture with his front-facing camera having a filter over it.

    I managed to get it working by using this code


    Code (CSharp):
    1. private bool camAvailable;
    2. private WebCamTexture frontCam;
    3. private Texture defaultBackground;
    4. string selectedDeviceName = "";
    5.  
    6. private Quaternion baseRotation;
    7.  
    8. public RawImage background;
    9. public AspectRatioFitter fit;
    10. // Start is called before the first frame update
    11.  
    12. void Start()
    13. {
    14.     defaultBackground = background.texture;
    15.     WebCamDevice[] devices = WebCamTexture.devices;
    16.     baseRotation = transform.rotation;
    17.  
    18.     if (devices.Length == 0)
    19.     {
    20.         camAvailable = false;
    21.         return;
    22.     }
    23.  
    24.     foreach (var camDevice in devices)
    25.     {
    26.         if (camDevice.isFrontFacing)
    27.         {
    28.             selectedDeviceName = camDevice.name;
    29.             frontCam = new WebCamTexture(selectedDeviceName, Screen.width, Screen.height);
    30.         }
    31.     }
    32.  
    33.  
    34.     if (frontCam == null)
    35.     {
    36.         Debug.Log("Unable to find front camera");
    37.         return;
    38.     }
    39.  
    40.     frontCam.Play();
    41.     background.texture = frontCam;
    42.  
    43.     camAvailable = true;
    44. }
    45.  
    46. // Update is called once per frame
    47. void Update()
    48. {
    49.     if (!camAvailable)
    50.     {
    51.         return;
    52.     }
    53.  
    54.     float ratio = (float)frontCam.width / (float)frontCam.height;
    55.     fit.aspectRatio = ratio;
    56.  
    57.     float scaleY = frontCam.videoVerticallyMirrored ? -1f : 1f;
    58.     background.rectTransform.localScale = new Vector3(1, scaleY, 1);
    59.  
    60.     int orient = -frontCam.videoRotationAngle;
    61.     background.rectTransform.localEulerAngles = new Vector3(0, 0, orient);
    62. }

    However, when I try to use it in conjunction with ARCore / ARKit the ARCamera goes completely black and doesn't seem to do anything.

    Is there a way to combine these two options or not?

    Any help is kindly appreciated.
     
  2. todds_unity

    todds_unity

    Joined:
    Aug 1, 2018
    Posts:
    324
    Both ARCore and ARKit require exclusive control of the camera when running. Thus, you cannot simultaneously WebCameraTexture with ARCore/ARKit actively running.