- Home /
Question by
mfahadminhas · Aug 06, 2016 at 07:54 PM ·
cameratexturetexture2dvuforiaconversion
How to convert Camera frame to Texture2D in unity c# script
Hi,
I am developing an AR application and I need to convert the camera frame/image into texture2D. What is the proper procedure to convert it maintaining the good results?
Below is my code and I am not getting good results out of it.
'img' is the current camera frame.
'boxMesh' is the MeshRenderer of my 3D object.
Texture2D caption = new Texture2D(img.Width, img.Height, TextureFormat.ARGB32, false);
byte[] pixels = img.Pixels;
int numPixels = img.Width * img.Height;
Color[] colors = new Color[numPixels];
for (int i = 0; i < numPixels; i++)
{
colors[i].r = pixels[3*i] / 255.0f;
colors[i].g = pixels[3*i + 1] / 255.0f;
colors[i].b = pixels[3*i + 2] / 255.0f;
colors[i].a = 1.0f;
}
caption.SetPixels(colors);
caption.Apply();
boxMesh.material.mainTexture = caption;
Comment