- Home /
Question by
PaulUsul · Jun 05, 2011 at 09:45 AM ·
textureisreadable
How can I bypass texture isReadable
Is there a way to copy a texture from the gfx card to system memory, thereby setting isReadable to true.
Comment
Best Answer
Answer by PaulUsul · Jun 19, 2011 at 03:22 PM
It is possible when using opengl.
in c
void EXPORT_API SetTexture(int textureID)
{
freopen("debug.txt", "a", stdout);
printf("SetTexture id:%d\n" ,textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &textureWidth);
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &textureHeight);
printf("w:%d h:%d\n" ,textureWidth ,textureHeight);
buffer = (GLfloat *)malloc(textureWidth * textureHeight * (sizeof(GLfloat) * 4));
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_FLOAT, buffer);
}
bool EXPORT_API GetTexture(float* resultPixels, int* resultPixelLength)
{
*resultPixelLength = (textureWidth * textureHeight) * 4;
printf("GetTexture pixels%d \n", resultPixelLength);
for(int i = 0; i < *resultPixelLength; i++)
{
resultPixels[+i] = (float) buffer[i];
}
count++;
return true;
}
void EXPORT_API FreeMemory()
{
printf("\n");
//delete(buffer);
free(buffer);
//free(bufferTest);
}
void EXPORT_API Close()
{
fclose (stdout);
}
in c#
[DllImport("Unity3dTextureSteal")]
private static extern void SetTexture(int textureId);
[DllImport("Unity3dTextureSteal")]
private static extern bool GetTexture(System.IntPtr ptrResultVerts, ref int resultVertLength);
[DllImport("Unity3dTextureSteal")]
private static extern void FreeMemory();
[DllImport("Unity3dTextureSteal")]
private static extern void Close();
public static Texture2D FindTexture(Texture2D tex)
{
//Debug.Log(tex.name + " " + tex.format);
SetTexture(tex.GetNativeTextureID());
float[] pix = new float[tex.width * tex.height * 4];
int resultVertLength = 0;
GCHandle pArray = GCHandle.Alloc(pix, GCHandleType.Pinned);
if (GetTexture(pArray.AddrOfPinnedObject(), ref resultVertLength))
{
pArray.Free();
Color[] colors = new Color[tex.width * tex.height];
//Debug.Log(pix.Length + " " + resultVertLength);
int i, c;
for (c = 0, i = 0; c < colors.Length; c++)
{
Color co = new Color();
co[0] = pix[i];
i++;
co[1] = pix[i];
i++;
co[2] = pix[i];
i++;
co[3] = pix[i];
i++;
colors[c] = co;
}
Texture2D texture = new Texture2D(tex.width, tex.height, TextureFormat.RGBA32, false);
texture.SetPixels(colors, 0);
texture.Apply(false);
FreeMemory();
return texture;
}
return null;
}