- Home /
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!
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!
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.
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.
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 );
}