I am trying to reproduce this technique:
http://forum.unity3d.com/threads/10682-Did-anyone-get-TexturePlugin-demo-to-work-on-Windows
But optimized with the use of SetPixels32, so that i dont have to send over all my RGBA in a float per channel format.
Here is what I am doing in my c++ plugin:
void EXPORT_API UpdateTexture( void* colors, int width, int height, float time )
{
// safeguard - array must be not null
if( !colors )
return;
// Color structure in Unity is four RGBA floats
unsigned char* data = reinterpret_cast<unsigned char*>(colors);
// Generate the texture data in funky ways
// Or could just stream it from a webcam or however you like.
for (int y=0;y<height;y++)
{
for (int x=0;x<width;x++)
{
unsigned char* pixel = data + (y * width + x) * 4;
// just a very simple scrolling sine waves pattern
pixel[0] = (unsigned char)((sinf( x * 0.13f + time * 2.1f ) * 0.5f + 0.5f) * 255.0f); // R
pixel[1] = (unsigned char)((sinf( y * 0.17f + time * 1.9f ) * 0.5f + 0.5f) * 255.0f); // G
pixel[2] = (unsigned char)((cosf( x * 0.21f + time * 2.3f ) * 0.5f + 0.5f) * 255.0f); // B
pixel[3] = 255; // A
}
}
}
and this is what I am doing in my C# script
public int width = 640;
public int height = 480;
protected Texture2D m_Texture;
protected Color32[] m_Pixels;
protected GCHandle m_PixelsHandle;
void setupImageUpdate()
{
m_Texture = new Texture2D (width, height, TextureFormat.RGBA32, false);
m_Pixels = m_Texture.GetPixels32 (0);
m_PixelsHandle = GCHandle.Alloc(m_Pixels, GCHandleType.Pinned);
m_ImagePlaneRenderer.material.mainTexture = m_Texture;
}
// Use this for initialization
void Start ()
{
setupImageUpdate();
}
[DllImport ("UnityTexture")]
private static extern void UpdateTexture (IntPtr colors, int width, int height, float time);
// Update is called once per frame
void Update ()
{
//UpdateTexture(m_PixelsHandle.AddrOfPinnedObject(), m_Texture.width, m_Texture.height, Time.time);
m_Texture.SetPixels32(m_Pixels, 0);
m_Texture.Apply(false,false);
}
I get an immediate crash (the unity gui crashes) when i try to run the system doing this. Anyone have a clue as to what I might be doing wrong? This works fine with the SetPixels method. Thanks for any help.