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 /
  • Help Room /
avatar image
0
Question by pouria77 · Feb 09, 2017 at 04:03 PM · spriteshaderstransparencyblendingalpha-channel

(using shader blending to) remove white background from an image at runtime

I am working on a unity application for children.

The idea is that the kids draw an image (like a fish) on a white surface, which is then stored as an image file on a folder. The application has to import these at runtime and then move them around in a 2d space.

The problem is the white background. How can I dynamically change the white part to transparent? I was playing with the current shaders in Unity and noticed that if I import an image with a white background manually and assign a shader of type "Particles/multiply", the white part is removed, but the image also looks way darker than it should, like the colors are not exactly right. How can I do this with code at runtime? and also is this the only way? because I don't want the image to loose color.

So in short, how do I remove the white background dynamically? Also, worth mentioning is that only the outer background should be removed, so if the fish has for example white eyes, that should not turn into transparent. so detecting the outer part is also part of the issue for me. Thanks in advance

Edit : I started playin with different shaders and shader code, and progressed a little. So I imported an sprite, added some default sprite shader on it, and in the shader code, added this line : Blend Zero SrcColor. As is visible in the attached image, it produces the needed effect of removing the white background. Only problem is , now the fish is also transparent a little. All I want to do is to keep the fish normal as the left image, but remove the background , like the right one!

alt text

fsh.png (367.4 kB)
Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by FortisVenaliter · Feb 09, 2017 at 04:03 PM

Have them draw it on a transparent surface, and put a white surface behind it. Then it will look the same while drawing, but you'll get a transparent image by default.

Comment
Add comment · Show 3 · Share
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 pouria77 · Feb 09, 2017 at 04:06 PM 0
Share

Hi

I have no control over where they draw it unfortunately, as the files come from a secondary web application. I just receive them as image files with a white background.

avatar image FortisVenaliter pouria77 · Feb 09, 2017 at 10:15 PM 0
Share

Ah.... that's difficult to do procedurally, then. Even the best graphics programs can't do it properly on their own... it requires an artist's touch. Sorry, but you may be out of luck then.

Best you could do is convert anything that is close enough to white to transparent, but then you'll have ugly edges.

avatar image pouria77 FortisVenaliter · Feb 10, 2017 at 07:06 AM 0
Share

hm.. there's already a shader in Unity that does it as i mentioned, but the thing is it just makes the whole image a little dark , so I'm not sure if I'm out of luck :) I just need to figure out the write way, maybe a special shader.

avatar image
0

Answer by pouria77 · Feb 15, 2017 at 01:08 PM

Hi

Thanks for the feedbacks. I solved it using the floodfill algorithm If anyone is interested :

FloodFill - Wikipedia

I used the recursive 4 direction method. I think it's also much more efficient that using a shader (if there was any way to do that) because unlike shaders, this method can be used only once in the beginning after loading the texture.

Edit : here's the code :

 //--------------------------------------------------------
     void removeTextureBackground()
     {
         if (_textureHolder != null)
         {
             _colorHolder = _textureHolder.GetPixel (0, 0);
             floodFill (0, 0);
             floodFill (0, _textureHolder.height - 1);
             floodFill (_textureHolder.width - 1, 0);
             floodFill (_textureHolder.width - 1, _textureHolder.height - 1);
             _textureHolder.Apply ();
         }
     }
     //--------------------------------------------------------------------
     void floodFill(int x, int y)
     {
         if (x < 0 || y < 0 || x > _textureHolder.width || y > _textureHolder.height)
             return;
         Color color = _textureHolder.GetPixel (x, y);
         if (isWhite (color) && color.a>0)
         {
             color.a = 0;
             _textureHolder.SetPixel (x, y, color);
             floodFill (x - 1, y);
             floodFill (x + 1, y);
             floodFill (x, y - 1);
             floodFill (x, y + 1);
         }
         else
             return;
     }
     //--------------------------------------------------------------
     bool isWhite(Color color)
     {
         float threshold = 1f;
         bool r = Mathf.Abs (color.r - _colorHolder.r) < threshold;
         bool g = Mathf.Abs (color.g - _colorHolder.g) < threshold;
         bool b = Mathf.Abs (color.b - _colorHolder.b) < threshold;
         if (r && g && b)
             return true;
         else
             return false;
     }


Comment
Add comment · Show 2 · Share
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 govind · Apr 01, 2017 at 12:25 PM 0
Share

can u help me......same problem i m facing right now.....how to solve help me to solve to remove white bg ....can u send me demo project....on my id ashwani.shinde91@gmail.com

avatar image valeria159 · Apr 17, 2017 at 09:26 AM 0
Share

Hello hellhammer, can you please share your code? I am having the same problem. Thank you. oufunity@gmail.com

avatar image
0

Answer by ntieshprismetric · Apr 17, 2017 at 11:13 AM

hello hellhammer please,can you share your code? nitesh@prismetric.com i am facing same problem

thank you.......

Comment
Add comment · Share
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

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

9 People are following this question.

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

Related Questions

Why Won't My Sprite Go Transparent? 2 Answers

Alpha blended shader - Show only the first alpha 2 Answers

How can I make this shader transparent? (add alpha option) 0 Answers

Lighting and shadows for 2D sprites. 0 Answers

How to create a mesh for 2D maps with transparent parts, which is generated by software? 0 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