# Texture tinting / performance

**URL:** <https://discussions.unity.com/t/texture-tinting-performance/396171>\
**Category:** Mobile\
**Tags:** iOS\
**Created:** [August 9, 2009, 12:10pm UTC](https://discussions.unity.com/t/texture-tinting-performance/396171 "2009-08-09T12:10:44Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon\_77707511](https://avatars.discourse-cdn.com/v4/letter/a/5f9b8f/32.png) [@anon\_77707511](https://discussions.unity.com/u/anon_77707511)\
**Post date:** [August 9, 2009, 12:10pm UTC](https://discussions.unity.com/t/texture-tinting-performance/396171/1 "2009-08-09T12:10:44Z")

</div>

Hi

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

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

---

<div class="post-metadata">

**Author:** ![Jessy](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/jessy/32/465975_2.png) [@Jessy](https://discussions.unity.com/u/Jessy)\
**Post date:** [August 9, 2009, 12:56pm UTC](https://discussions.unity.com/t/texture-tinting-performance/396171/2 "2009-08-09T12:56:36Z")

</div>

> [@""](#):
>
> - Is this the best way of achieving what we want?

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.)

> [@""](#):
>
> - How about powervr texture compression? Will we lose on performance because the texture is a .png?

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.

---

<div class="post-metadata">

**Author:** ![anon\_77707511](https://avatars.discourse-cdn.com/v4/letter/a/5f9b8f/32.png) [@anon\_77707511](https://discussions.unity.com/u/anon_77707511)\
**Post date:** [August 9, 2009, 1:37pm UTC](https://discussions.unity.com/t/texture-tinting-performance/396171/3 "2009-08-09T13:37:51Z")

</div>

Thanks for answering Jessy,

> [@""](#):
>
> 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.

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?

> [@""](#):
>
> - How about powervr texture compression? Will we lose on performance because the texture is a .png?

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!

---

<div class="post-metadata">

**Author:** ![Dreamora](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/dreamora/32/91058_2.png) [@Dreamora](https://discussions.unity.com/u/Dreamora)\
**Post date:** [August 9, 2009, 2:37pm UTC](https://discussions.unity.com/t/texture-tinting-performance/396171/4 "2009-08-09T14:37:50Z")

</div>

no

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

---

<div class="post-metadata">

**Author:** ![Jessy](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/jessy/32/465975_2.png) [@Jessy](https://discussions.unity.com/u/Jessy)\
**Post date:** [August 9, 2009, 5:21pm UTC](https://discussions.unity.com/t/texture-tinting-performance/396171/5 "2009-08-09T17:21:22Z")

</div>

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

```auto
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 
		}
	} 
}

}

```

---

<div class="post-metadata">

**Author:** ![Jessy](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/jessy/32/465975_2.png) [@Jessy](https://discussions.unity.com/u/Jessy)\
**Post date:** [August 9, 2009, 9:53pm UTC](https://discussions.unity.com/t/texture-tinting-performance/396171/6 "2009-08-09T21:53:34Z")

</div>

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

```auto
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
		}
	}
} 

}

```

---

<div class="post-metadata">

**Author:** ![anon\_77707511](https://avatars.discourse-cdn.com/v4/letter/a/5f9b8f/32.png) [@anon\_77707511](https://discussions.unity.com/u/anon_77707511)\
**Post date:** [August 28, 2009, 10:51am UTC](https://discussions.unity.com/t/texture-tinting-performance/396171/7 "2009-08-28T10:51:53Z")

</div>

Hey Jessy,

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

Thanks a lot

---

<div class="post-metadata">

**Author:** ![Jessy](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/jessy/32/465975_2.png) [@Jessy](https://discussions.unity.com/u/Jessy)\
**Post date:** [August 28, 2009, 11:08am UTC](https://discussions.unity.com/t/texture-tinting-performance/396171/8 "2009-08-28T11:08:40Z")

</div>

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](http://forum.unity3d.com/viewtopic.php?t=30702)
