- Home /
Prime31 - Augmented Reality - Camera "Still" Photo Help
Hello, hoping someone could help me understand how using Texture2D with the Augmented Reality plugin works a bit better.
What I'm trying to do is use the AR plugin to take a frame from the camera and use that to texture a simple plane mesh with a diffuse shader.
I've made sure that the texture I've already assigned is set to 'Advanced' and the "Read/Write Enabled" option is checked. My texture format is RGBA32.
I looked at the sample code provided here (and the Prime31 doc):
http://forum.unity3d.com/threads/58692-Prime31-Unity-Plugins-Officially-Live!/page25
But this pertains to video textures, which isn't what I'm trying to do, so it is a bit confusing.
My Scene has one square plane in it, which is a two triangles with their normals facing the camera, UV Mapped tiled (1, 1) The existing material as I mentioned is a RGBA32 image using a diffuse shader.
Here's the code for my simple scrape-a-frame-from-the-camera function:
(I check for camera availability on the device in the Start section)
private var buttonMaterial : Material;
private var isCameraAvailable : boolean = false;
private var cameraCapture : Texture2D;
private var buttonTexture : Texture2D;
function takePhoto(squareName : String)
{
if(isCameraAvailable)
{
cameraCapture = ARBinding.startCameraCapture(false, ARCapturePreset.Size192x144);
cameraCapture.name = squareName + "_CameraCapture";
if(cameraCapture)
{
if(buttonTexture) // Destroy and null any pre-existing button Texture2D
{
Destroy(buttonTexture);
buttonTexture = null;
}
// Create new button texture
buttonTexture = new Texture2D(cameraCapture.width, cameraCapture.height, TextureFormat.ARGB32, false);
buttonTexture.name = squareName + "_ButtonTexture";
// Find existing material on our object with a mesh renderer named < squareName >
buttonMaterial = GameObject.FindWithTag(squareName).renderer.material;
// Scale the existing material to fit our camera capture
ARBinding.updateMaterialUVScaleForTexture(buttonMaterial, cameraCapture);
// Create temporary image to impart pixels from camera captured frame
var img : Color[] = new Color[cameraCapture.width * cameraCapture.height];
// Set pixels from img to button texture
buttonTexture.SetPixels(img, 0);
// Apply changes to texture
buttonTexture.Apply();
// Find our object and apply our new texture to it
GameObject.FindWithTag(squareName).renderer.material.mainTexture = buttonTexture;
// Stop camera capture
ARBinding.stopCameraCapture();
}
}
}
function Start () {
isCameraAvailable = ARBinding.isCaptureAvailable(); // Set boolean if camera is available
}
So, if you've read through all that - thank you - and here's what happens with the above code. The takePhoto function fires, I have Debug.Log statements throughout this thing to see where I'm at.
Every part of this function gets evaluated and run. The square however only shows the original placeholder 128 x 128 texture getting scaled up to match the capture, then all I see is black. I've tried different capture resolutions higher than 192x144, with no success.
I'm at the point where I'm pondering if I have to write out the capture to an actual image file then read it back in, but that would be more involved than this example.
Could someone please point me in the right direction here? I'm not afraid of doing my own work, just need some guidance.
Thanks in advance.
Answer by TraderTim · Apr 17, 2012 at 06:27 PM
Just thought I'd update this. The Prime31 guys told me that the AR Plugin isn't suitable for this kind of application, the Etcetera plugin would be the proper way to go.
So if you're banging your head against a problem like this, now you know what to do :)
Dear Trader, thanks a million for that info. In fact, did you get it to work in the end?
I'm having a ridiculously hard time doing what you would think is trivial, using the iPhone camera. Thanks for any info!
Your answer
Follow this Question
Related Questions
Solutions for iOs camera body tracking? 0 Answers
ARkit ARImageAnchorRemovedEvent not working 0 Answers
I cannot log into Facebook when logged in via the settings screen of iOS 0 Answers
ARFoundation body recognition doesn't work 0 Answers
ARFoundation: screen tearing on iOS, but not On Android 3 Answers