- Home /
Crop texture dynamicly with a shader
Hi everyone, I'm using kinect v2 for face detection. The main goal is to get the face of a person and drawing it over a mesh. I have a Rect corresponding to the coordinate and size of the head in the kinect video stream. This rect has a size and coordinates changing every frame. What is the most efficient way to crop the full texture to draw the rect over a mesh? When i use texture2D.readPixels (from source) / texture2D.setPixels (to new texture) I have a memory leak, and it takes a lot of cpu. Is there anyway to do it with the scale and tiling parameters of the texture, or with a shader?
Thanks and sorry for the bad English.
Just a little precision : When i crop the texture with a simple Texture2D.getPixels(), i have a memory leak when I apply the texture
Texture2D $$anonymous$$inectVideoTexture; // Full video texture
Texture2D FaceTexture; // Face texture
void updateFaceTexture(Rect texRect)
{
FaceTexture.Resize(texRect.width, texRect.height);
FaceTexture.SetPixels($$anonymous$$inectVideoTexture.GetPixels(texRect.x, texRect.y, texRect.width, texRect.height));
FaceTexture.Apply(); // $$anonymous$$emory leak occurs here
headObject.renderer.material.mainTexture = FaceTexture;
}
How do you initialize FaceTexture?
You should be creating it with enough space for all pixels you want to set there:
Texture2D FaceTexture= new Texture2D(texRect.width, texRect.height);
Your answer