Urko Sanchez Ortiz
Urko Sanchez Ortiz

Reputation: 311

image problems with webcamtexture

I have a little problem in my Unity work, I'm trying to get the images from a Webcam texture, and I'll keep it in a byte []. the problem comes when I show the image, which is not seen correctly, it looks like some type of grid mesh. I think the problem is in the for I use to pass the pixels. I hope you can help me.enter image description here

    void Update()
{

    //texto.text = texto.text +  width +" / "+ height + " / " + ini.ToString()+ " mal: "+ testo().ToString()+ " SizeIMG: "+ imgData.Length + " NUEVO: "+ nuevo +"\n";

    imgData = null;
    imgData = new byte[width * height * 3];
    resultado = null;
    resultado = new byte[width * height * 3];

    color = webcam.GetPixels32();
    for (int i = 0; i < color.Length; i += 3)
    {
        imgData[(i * 3)] = color[i].r;
        imgData[(i * 3) + 1] = color[i].g;
        imgData[(i * 3) + 2] = color[i].b;

    }
    color = null;

    //video(imgData, resultado);

    //ProcessFrame(imgData, resultado, 0, 0, 0,0, nuevo);
    nuevo = false;

    textura2 = new Texture2D(width, height, TextureFormat.RGB24, false);
    textura2.LoadRawTextureData(imgData);
    textura2.Apply();

    //Left IMAGE
    renderer.material.mainTexture = textura2;
    textura2 = null;

    RightImage.GetComponent<Renderer>().material.mainTexture = webcam;

    if (kont == 30)
    {
        texto.text = "";
        kont = 0;
    }
    kont++;

    Resources.UnloadUnusedAssets();
}

Upvotes: 1

Views: 903

Answers (1)

Jared Lovin
Jared Lovin

Reputation: 551

I think the issue is how your indexing into your imgData array. You're incrementing i by three in addition to multiplying by 3.

int bytesPerPixel = 3;
const int indexR = 0;
const int indexG = 1;
const int indexB = 2;
for(var i = 0; i < color.Length; i++)
{
    imgData[(i * bytesPerPixel) + indexR] = color[i].r;
    imgData[(i * bytesPerPixel) + indexG] = color[i].g;
    imgData[(i * bytesPerPixel) + indexB] = color[i].b;
}

Upvotes: 1

Related Questions