Log in or register to post comments

How to put a Vuforia image (from webcam) on a plane

August 16, 2017 - 1:21pm #1

I am trying to show both the augmented and non-augmented camera feed in my app. I have the augmented feed on one side of the screen and another camera displaying on the other side of the screen. I am trying to get the image created by this example onto a plane so that I can point a camera at it. I attached the script to a plane and added a few lines so that the OnTrackablesUpdated() method looks like this:

void OnTrackablesUpdated()
{
  if (mFormatRegistered)
  {
   if (mAccessCameraImage)
   {
    Vuforia.Image image = CameraDevice.Instance.GetCameraImage(mPixelFormat);

    if (image != null)
    {
     Debug.Log(
      "\nImage Format: " + image.PixelFormat +
      "\nImage Size:   " + image.Width + "x" + image.Height +
      "\nBuffer Size:  " + image.BufferWidth + "x" + image.BufferHeight +
      "\nImage Stride: " + image.Stride + "\n"
     );

     byte[] pixels = image.Pixels;

     if (pixels != null && pixels.Length > 0)
     {
      Debug.Log(
       "\nImage pixels: " +
       pixels[0] + ", " +
       pixels[1] + ", " +
       pixels[2] + ", ...\n"
      );
     }

     Texture2D texture = new Texture2D (10, 10);
     image.CopyToTexture (texture);
     this.GetComponent<Renderer> ().material.mainTexture = texture;
    }
   }
  }
}

The plane still shows all gray. Any ideas what I'm doing wrong? Is there a standard way to display to non-augmented image that is different from what I am doing?

Also, the plane shows up in the augmented image when the target is recognized even though the plane is not a child of the target. I assume this means I am doing something fundamentally wrong. Is there a way to add objects to the scene without them showing up when targets are recognized?

Thank you in advance for any help.

How to put a Vuforia image (from webcam) on a plane

August 17, 2017 - 8:50am #4

That's correct, my apologies. I'll update my code block to be accurate.

-Vuforia Support

How to put a Vuforia image (from webcam) on a plane

August 17, 2017 - 6:49am #3

That is great, thank you! To document for anyone having the same problem, it should be texture.Apply(), not image.Apply(). I ended up using GUI.DrawTexture and removed the plane.

How to put a Vuforia image (from webcam) on a plane

August 16, 2017 - 4:39pm #2

Hello topaz123,

There is a known issue with the CopyToTexture function. The work around for this issue is to immediately after the function, call Texture2D's Apply function.

For example, in your code do this:

Texture2D texture = new Texture2D (10, 10);
image.CopyToTexture (texture);
texture.Apply();
this.GetComponent<Renderer> ().material.mainTexture = texture;

You can also get an OpenGL texture. For more information on that, see this document: https://library.vuforia.com/content/vuforia-library/en/articles/Solution/Working-with-the-Camera.html#How-To-Access-the-Camera-Image-in-Unity

Thanks,

-Vuforia Support

Log in or register to post comments