To obtain the Magic Leap 2 camera feed as a preview in a Unity project

Give us as much detail as possible regarding the issue you're experiencing:

Unity Editor version: 2022.3.25f1
ML2 OS version: v1.5.0
Unity SDK version: v2.2.0
Host OS: (Windows/MacOS) : Windows

I've encountered difficulties recently while developing with Magic Leap 2. I want to obtain the camera feed and display it as a preview in a Unity project. I followed the MLCamera sample code from the online documentation, but I found it wasn't that straightforward. I implemented the sample code script and noticed that it required a renderer. So, I created a plane object and assigned the plane's mesh renderer to the script. I've gotten this far, but I'm not sure which object the sample code script needs to be placed on; it feels like I'm missing a step.

Additionally, I remember that I placed an XR rig as the character. I'm considering whether I need to add the sample code to the main camera component of the XR rig prefab, but I haven't succeeded yet. I'm not sure what other steps I'm missing. Any help would be greatly appreciated.

I complete the following step

  1. Create empty object and placed the sample code on it.
  2. create a plane object and assigned its mesh render to sample code script
  3. enable project setting -> magic leap -> permission -> android.permission.camera

What steps have you tried? What happened when you placed it on the camera? Have you tried placing the script on a new game object? What issues have you run into?

What steps have you tried?

  1. Create an empty GameObject and attach the ML camera sample code to it.
  2. Create a quad and assign its mesh renderer to the sample code script.

What happened when you placed it on the camera?
Nothing happened, but I found out using debug.log that it couldn't access my camera. I have enabled the camera permission.

Have you tried placing the script on a new game object?
I tried creating an empty object and attaching the script to it, and I also tried attaching it directly to the main camera of the XR rig. However, according to the log, it always fails at the step where it tries to access the camera.

What issues have you run into?
The program is stuck in this while loop:
while (!_cameraDeviceAvailable)
{
MLResult result = MLCamera.GetDeviceAvailabilityStatus(_identifier, out _cameraDeviceAvailable);
if (result.IsOk == false || _cameraDeviceAvailable == false)
{
// Wait until the camera device is available
yield return new WaitForSeconds(1.0f);
}
}
However, after building it on the Magic Leap, there is no display, and I found that my app did not request camera permissions.

Which script are you using? Does your script include a request for the Camera access? Since Camera is a dangerous permission, the permission must be declared and then requested at runtime .

I used the SimpleCamera script from this website to access the Magic Leap 2 camera.
Simple Camera Example

Thank you for your reminder. I found that the script does not include the request for camera access. I think this part is missing. I thought it was enough to just check the camera permission in the Magic Leap permissions. Thank you very much, I will try again.

1 Like

Hello kbabilinski,

I followed your advice and added more to the script. I successfully obtained the camera feed, but I found a problem: the preview image is mirrored from my actual head movement. I'd like to know if I need to modify this through my own code, as I couldn't find relevant information in the web documentation. Could you help clarify this for me?

Thank you!

You can do two things inside unity, either manipulate the bites of the image directly or rotate the rendered image (by rotating the object or changing the material properties)

Since rotating an image is not magically specific, I recommend looking at some other resources online on how to rotate bytes of a texture in Unity.

If you are using the example online and using an RGBA texture you would incorporate the flipping logic here :

// Image width and stride may differ due to padding bytes for memory alignment. Skip over padding bytes when accessing pixel data.
        if (imagePlane.Stride != actualWidth)
        {
            // Create a new array to store the pixel data without padding
            var newTextureChannel = new byte[actualWidth * imagePlane.Height];
            // Loop through each row of the image
            for (int i = 0; i < imagePlane.Height; i++)
            {
                // Copy the pixel data from the original array to the new array, skipping the padding bytes
                Buffer.BlockCopy(imagePlane.Data, (int)(i * imagePlane.Stride), newTextureChannel, i * actualWidth, actualWidth);
            }
            // Load the new array as the texture data
            _rawVideoTextureRGBA.LoadRawTextureData(newTextureChannel);
        }
        else // If the stride is equal to the width, no padding bytes are present
        {
            _rawVideoTextureRGBA.LoadRawTextureData(imagePlane.Data);
        }

Hello kbabilinski,

Thank you very much for the information you provided. It has been very useful to me. Thanks to you, I can finally move my project into the next stage of development.

Thank you once again.

1 Like

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.