Texture tinting / performance

Hi

I’m new to unity so please forgive any stupidity I’m doing or saying :wink:

We want to tint one of the characters of our game based on alpha values of the texture. Say, if the pixel values are

R, G, B, A
255, 255, 255, 0 → this will be tinted
255, 255, 255, 255 → this will not be tinted
255, 255, 255, 127 → this will be blended with the tint value

I was looking at the docs, and I saw the Texture2D class. I noticed that it allows loading from a .png file, so, loading the file and processing the image and calling the Apply method seems to be a way of achieving the desired effect.

Questions:

  • Is this the best way of achieving what we want?
  • How about powervr texture compression? Will we lose on performance because the texture is a .png?

Any posts will be appreciated.
Cheers

I don’t think so. I think you’d be better off scripting values that will be sent to the shader. (Try using the shader called “Diffuse”, that comes with Unity, and choose a “Main Color”, for example. It’s possible that you will get a performance benefit from modifying the shader to only take a grayscale value for “Main Color” instead of RGB, but I’m not sure about that.)

Format doesn’t matter once you get it into Unity. Right-click on your texture in the Project pane, and select “Import Settings…” to find out what’s really going on.

Thanks for answering Jessy,

Okay, wouldn’t that tint the entire material (we want only the parts of the texture marked with alpha values < 1.0 to be tinted)?
I thought about using a lightmap, or self ilumination material, but doesn’t that combine two textures, and wouldn’t that be less efficient performance wise?

Format doesn’t matter once you get it into Unity. Right-click on your texture in the Project pane, and select “Import Settings…” to find out what’s really going on.[/quote]

Okay, so do you mean that once I Apply() my texture changes Unity will convert the texture to use pvrt texture compression automatically? Or will it keep it as just plain texture data in memory?

Sorry for the confusion. Thanks!

no

pvrtc are generated in the editor / on the mac, not in realtime.
modifyable textures can’t be pvrtc, they remain uncompressed.

I thought this code would work, but it doesn’t run as-in on the iPhone hardware. Anybody know why this is?

Shader "Tinted using Alpha" { 

Properties 
{ 
	_Color ("Tint Color", Color) = (1,1,1) 
	_MainTex ("Texture", 2D) = "white" 
} 

SubShader
{ 
	Pass 
	{		
		SetTexture [_MainTex] 
		{ 
			ConstantColor (0,0,0,0) 
			combine texture * one - texture + constant 
		} 
		
		SetTexture [_Color] 
		{ 
			ConstantColor [_Color] 
			combine constant * previous 
		}
		
		SetTexture [_MainTex] 
		{ 
			combine texture * texture + previous 
		}
	} 
}

}

Okay, this is shorter code and actually runs on the iPhone. :stuck_out_tongue:

Shader "Tinted using alpha" { 
    
Properties 
{ 
   _Color ("Tint Color", Color) = (1,1,1) 
   _MainTex ("Texture", 2D) = "white" 
} 

SubShader 
{	
	Pass
	{
		SetTexture [_MainTex] 
		{
			ConstantColor [_Color]
			combine texture * constant 
		} 

		SetTexture [_MainTex] 
		{
			combine texture lerp(texture) previous
		}
	}
} 

}

Hey Jessy,

Awesome, I’ve just tried this. It works!

Thanks a lot

No problem. You may want to check out this other thread, which explains why I think it might make more sense to use an a variant of this shader that takes an inverted alpha channel:

http://forum.unity3d.com/viewtopic.php?t=30702