Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Bypp · Jun 04, 2014 at 03:43 PM · filltransparent-texture

Is it possible to fill transparency with white in a texture in code ?

I have some textures containing some transparency parts (a donut, for example, which would show a transparent center). What I want to do is fill the middle of the donut (or anything else) with a plain white, in code (I don't want to have a double of all my assets that need this tweak in one part of my game).

Is there a way to do this ? Or do I really have to have 2 of each of my assets ?

Thanks for the help !

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
2

Answer by Owen-Reynolds · Jun 04, 2014 at 08:46 PM

Yes, this is simple (assuming you know loops and color formats.) Texture2D should have all the info you need (getPixels, setPixels... ) to read the old texture, and create/write/save the new. Reading alpha is no different than reading RGB.

Probably use: Color c2=Color.Lerp(c1, Color.White, c1.alpha); c2.a=1; to get the white to fade in with the alpha.

Or, if you just happen to have alpha==white in the original image, simply use a non-transparent shader (with the original image.) It will show all the RGB, ignoring the alpha.

Comment
Add comment · Show 5 · 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 Bypp · Jun 04, 2014 at 09:48 PM 0
Share

But this will make all of my alpha become white right ? I want only the middle of said donut to be white ! This will make the whole "square" of my texture white, which is something I don't want. Am I missing something else here ?

avatar image tanoshimi · Jun 04, 2014 at 10:22 PM 0
Share

No, it will make the transparent parts to white, and leave the nontransparent pixels as they were. Isn't that what you wanted?

avatar image Owen-Reynolds · Jun 05, 2014 at 12:18 AM 1
Share

Well, sure, if the inside and edges are transparent, this would change all. A cheap "center only" is to hand-do math to say "if within a radius of the center." Can use (x-halfPixelWd)/pixelWd to get a -0.5 to 0.5 for how far a pixel on x is from the center. Can probably also look up how to make a circle texture, and s$$anonymous$$l that.

avatar image tn7111 · Sep 03, 2021 at 01:56 PM 0
Share

Umm, I'd say rather something like that:

 Color c2 = Color.Lerp(Color.White, c1, c1.alpha/255f);
 c2.a=255;

then you get an image as if you put png onto totally white background layer in Photoshop or similar soft.

avatar image Owen-Reynolds tn7111 · Sep 03, 2021 at 03:12 PM 1
Share

No need to divide by 255. Colors in Unity are scaled into range 0-1. Color.white is (1,1,1,1), ca.alpha is 1 (for a solid image), and so on.

avatar image
0

Answer by Nerevar · Jun 04, 2014 at 04:06 PM

Hello,

I think you would need to write an editor script for that.

using a custom TextureImporter

Then you may be able to access variables you need

alt text

if you can set AlphaFromGrayscale and AlpaIsTransparency booleans (which I did no test) problem would be solved.

regards


alpha.png (15.0 kB)
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 Bypp · Jun 04, 2014 at 04:58 PM 0
Share

Thing is, I need to do this on the fly (in code), not change the texture or import settings !

avatar image Nerevar · Jun 05, 2014 at 08:27 AM 0
Share

Yes well you can do it with Texture importer on an editor script

avatar image
0

Answer by Jeff-Kesselman · Jun 04, 2014 at 09:10 PM

Another way to do it is to create an all white RenderTexture of the same size and then use http://docs.unity3d.com/ScriptReference/Graphics.Blit.html to copy your original ontop of it.

Comment
Add comment · Show 1 · 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 Bypp · Jun 04, 2014 at 09:49 PM 0
Share

I only want to fill the middle part of my texture, not the entire thing !

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

25 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

Related Questions

Filling array with gameObjects 2 Answers

What is the best way to evenly distribute objects to fill a curved space? 1 Answer

How do I soften the edges of a transparent Plane texture 1 Answer

Mobile shader with transparent rendering mode 1 Answer

How to fill circle in Eric5h5's TextureDrawCircle function 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