- Home /
Fade Out Background Based On Movement or Lack Thereof (Webcam Texture)
Hi Unity!
So I've been tasked with creating a system that fades out the background of a webcam texture based on movement, or lack thereof. The user will be holding a cellphone and using the camera facing them, whilst spinning around slowly. This means that while their background will change, their position in the image will not.
So here is my code, which fails to do much of anything. I'm new to Get/SetPixels though - so go easy on me. Any thoughts? I would obviously need to include a lot more refinement, but I thought this would at least give me the basic effect.
public RawImage webTexImage, mask; //The webTexImage is the image we will project our webcamtexture onto. The mask is the texture that will be infront of it to mask it.
public float threshold; //The threshold of color variation
void Start()
{
WebCamTexture webTex = new WebCamTexture(Screen.width, Screen.height); //Initialize our webcam texture
webTexImage.texture = webTex; //Set our webcamtexture to the proper component's texture
webTex.Play(); //play our webcam texture
StartCoroutine(GetMask()); //Start getting the mask to mask our webcam texture
}
IEnumerator GetMask()
{
//Create a new texture for us to assign to our mask later
Texture2D texture = new Texture2D(webTexImage.mainTexture.width / 100, webTexImage.mainTexture.height / 100, TextureFormat.ARGB32, false);
//Define the "old" web texture
Texture2D oldWebTex = new Texture2D(webTexImage.mainTexture.width, webTexImage.mainTexture.height, TextureFormat.ARGB32, false) as Texture2D;
//Wait a frame so that we can get a more recent view of our web texture
yield return new WaitForEndOfFrame();
//Define the "new" web texture
Texture2D newWebTex = new Texture2D(webTexImage.mainTexture.width, webTexImage.mainTexture.height, TextureFormat.ARGB32, false) as Texture2D;
//Iterate through the entire mask texture and compare each "new" pixel to it's respective "old" pixel
//If the new pixel's color is close enough to it's respective old pixel, set the mask to black (in mask terms, this means show the pixel).
//If the new pixel's color is not close enough to it's repective old pixel, set the mask pixel to clear (in mask terms, this means do not show the pixel).
for (int x = 0; x < texture.width; x++)
{
for (int y = 0; y < texture.height; y++)
{
Texture2D o_tex = oldWebTex, n_tex = newWebTex;
float old_r = o_tex.GetPixel(x, y).r, old_g = o_tex.GetPixel(x, y).g, old_b = o_tex.GetPixel(x, y).b;
float new_r = n_tex.GetPixel(x, y).r, new_g = n_tex.GetPixel(x, y).g, new_b = n_tex.GetPixel(x, y).b;
if (old_r > (new_r - threshold) && old_r < (new_r + threshold) &&
old_g > (new_g - threshold) && old_g < (old_g + threshold) &&
old_b > (new_b - threshold) && old_b < (old_b + threshold))
{
texture.SetPixel(x, y, Color.black);
}
else
{
texture.SetPixel(x, y, Color.clear);
}
}
}
// Apply all SetPixel calls
texture.Apply();
//Set the texture to the mask
mask.texture = texture;
//Repeat the process for the next frame
StartCoroutine(GetMask());
}
I've rewritten the script to use GetPixels32 (which I'm still a bit hazy on, but sounds like it uses averaged color values ins$$anonymous$$d of literal pixel color values?)
However, this doesn't seem to increase efficiency at all - and definitely doesn't achieve the effect, since the game still crashes right off the bat.
Your answer
Follow this Question
Related Questions
How do I set pixels with WebCamTexture.GetPixels 1 Answer
Painting stencil on a surface. 6 Answers
How to paint color on plane in runtime on mouse position? 0 Answers
Setpixels leads to a blank texture? 1 Answer
Setting pixels on a texture? 2 Answers