using data aquired by WebCamTexture.GetPixels32

Discussion in 'Scripting' started by MisterMe, Feb 4, 2012.

  1. MisterMe

    MisterMe

    New Member

    Joined:
    Feb 3, 2012
    Messages:
    3
    Hi there,

    I'm slowly learning to program, but have many (probably silly) questions :)... I'm playing with the new Unity 3.5 webcam possibilities, and have the following code (mostly from the 'scripting reference'):

    Code (csharp):
    1.  
    2. var webcamTexture : WebCamTexture;
    3. var data : Color32[];
    4.  
    5.  
    6. function Start () {
    7. // Start web cam feed
    8. webcamTexture = WebCamTexture(160, 120, 15);
    9. webcamTexture.Play();
    10. data = new Color32[webcamTexture.width * webcamTexture.height];
    11.  
    12.  
    13. var cube : GameObject = GameObject.CreatePrimitive(PrimitiveType.Cube);
    14. cube.transform.renderer.material.color = Color(data[0]);
    15. }
    16.  
    17. function Update () {
    18. webcamTexture.GetPixels32 (data);
    19. // Do processing of data here.
    20. print(data[0]);
    21. }
    22.  
    What I am trying to do here is... store the webcam data in an array (resolution 160x120), create a cube and give this the first color stored in the array. I can print the rgba data of this color, but don't know how to apply it to the cube. What I do on line 13 is obviously wrong... How do you do this?

    Thanks!
  2. Dreamora

    Dreamora

    Member

    Joined:
    Apr 5, 2008
    Messages:
    26,586
    add a texture to the cube and then use SetPixels32(data) and Apply on it.

    Or simply apply webcamTexture to the cube directly instaed of trying to GET SET Apply :)
  3. MisterMe

    MisterMe

    New Member

    Joined:
    Feb 3, 2012
    Messages:
    3
    Hi Dreamora,

    Thanks for your reply! I'm not sure if I understand your feedback correctly... I'm not trying to apply the whole webcam-image to the cube, but only the rgba value of one 'pixel' of it. For that I need WebCamTexture.GetPixels32 right, not just WebCamTexture? And do I need a texture? I just want to apply the rgba data to material.color.
    Maybe I should explain what I'm trying to achieve a bit better. I want to create a grid of 160x120 cubes, each cube displaying a color of the 160x120 webcam-feed. Understanding the problem mentioned above would be a first step to achieve this I figured...
    Thanks again!
  4. MisterMe

    MisterMe

    New Member

    Joined:
    Feb 3, 2012
    Messages:
    3
    Got some help from a friend, in case other people want to know:

    cube.transform.renderer.material.color = Color(data[0]);
    should be:
    cube.transform.renderer.material.color = data[0];

    Works fine now!
  5. i_allways_forget

    i_allways_forget

    New Member

    Joined:
    Mar 12, 2012
    Messages:
    1
    If you're simply looking for the color of one pixel, it will be more efficient to use the WebCamTexture.GetPixel function. This will return the color of the specified pixel.
  6. Minassian

    Minassian

    New Member

    Joined:
    Nov 22, 2010
    Messages:
    40
    It was as simple as setting the texture and stop the device.
    Thank you dreamora I was looking for that info.