Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Addyarb · Feb 13, 2016 at 03:30 AM · webcamtexturesetpixelsgetpixels

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());
     }

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Addyarb · Feb 14, 2016 at 04:35 AM 0
Share

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.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

34 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges