Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 jetjaguar · Jun 27, 2013 at 06:12 PM · texturepixelurl

Change pixel color of Material from URL

Hello everybody, I'm in the process of learning Unity3D and I'm a bit stuck on something. I have a an object with a PNG material that is pulled from a url. To do this I used the C# code here: http://docs.unity3d.com/Documentation/ScriptReference/WWW.html and it works just fine.

Now what I'd like to do is change the black pixels in the PNG image I'm grabbing to be transparent. I think I need get/set pixels to do this, and the image is coming from NASA, so it should be safe, but I'm having trouble because (for example) the setpixels code on Unity's site is only in javascript...

Any help would be greatly appreciated. Thank you!

Comment
Add comment · Show 3
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 jetjaguar · Jun 29, 2013 at 08:13 PM 0
Share

Hello, I'm wondering if my last response is lost in the moderation machine? It's been a couple days.

With set and getpixel I can change the tint of an entire texture, but I cannot find any examples of people that have changed only specific texture pixel colors. $$anonymous$$aybe it's not possible, especially with an image co$$anonymous$$g straight from a URL. Does anyone know any better?

Here is example of an image that I'd use: http://neo.sci.gsfc.nasa.gov/RenderData?si=1561252&cs=rgb&format=PNG . I would then wrap this image around a sphere with another slightly smaller sphere inside of that one, that is in turn wrapped with a true-color photo of the earth.

This is all for a museum exhibit in case you are wondering.

Thank you so much in advance!

avatar image robertbu · Jun 29, 2013 at 08:33 PM 0
Share

Untested, but replace line 15 in the code posted by @iwaldrop below with the following three lines:

 color c = cols[i];
 if (c.r < 0.01 && c.g < 0.01 && c.b < 0.01)
   cols[i].a = 0.0;

This will set the alpha for any pixels that are nearly black to 0.0. Note your shader will need to support transparency for this to work. In addition, you would be better off both performance wise and memory usage wise if you use Texture2D.GetPixels32() and Texture2D.SetPixels32(). These methods use Color32 structures which has a byte for each of r,g,b, rather than floats used in Color structure.

avatar image jetjaguar · Jun 29, 2013 at 10:51 PM 0
Share

I had to change "color c" to "Color c" and change the shader to transparent + diffuse. After all that, it worked great. I'll try the 32 method as you suggested.

Thank you iwaldrop and robertbu for both helping me with this. I greatly appreciate it.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by iwaldrop · Jun 27, 2013 at 06:17 PM

Translating UnityScript to C# is really quite easy. You should try it yourself next time you encounter one of those pages in the Unity docs that are only in US. For instance, the code on the Texture2D.SetPixels page, translated to C# looks like this:

 void Start ()
 {
     Texture2D texture = Instantiate(renderer.material.mainTexture);
     renderer.material.mainTexture = texture;
     Color[] colors = new Color[3];
     colors[0] = Color.red;
     colors[1] = Color.green;
     colors[2] = Color.blue;
     int mipCount = Mathf.Min( 3, texture.mipmapCount );
     for(int mip = 0; mip < mipCount; ++mip)
     {
         Color[] cols = texture.GetPixels(mip);
         for(int i = 0; i < cols.Length; ++i)
         {
             cols[i] = Color.Lerp(cols[i], colors[mip], 0.33 );
         }
         texture.SetPixels( cols, mip );
     }
     texture.Apply( false );
 }


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

16 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

Related Questions

How to fill texture that would be avaliable to ither shaders? 1 Answer

Suggestion about a photo hunt game 1 Answer

Assigning UV Map to model at runtime 0 Answers

Check if connected to internet or not 3 Answers

Best way to determine WWW timeout/404/etc.? 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