Masked Tint

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m (Description)
Line 6: Line 6:
 
These shaders tint the model's texture based on a mask, so that you can selectively colorize portions of your texture.  The '''Masked Tint - Vertex Colors''' shader allows you to have many different color variations for the tinted area, on many different instances of your model, but only use one draw call.
 
These shaders tint the model's texture based on a mask, so that you can selectively colorize portions of your texture.  The '''Masked Tint - Vertex Colors''' shader allows you to have many different color variations for the tinted area, on many different instances of your model, but only use one draw call.
  
'''Masked Tint''' will allow you to save memory, by not having to store a different mesh for every color you want to use, but does not offer the batching power of '''Masked Tint - Vertex Colors'''.  This shader may be a good choice if you only want to colorize using a single color, and your meshes have over 300 vertices, removing the ability to use dynamic batching.
+
'''Masked Tint''' will allow you to save memory, by not having to store a different mesh for every color you want to use, but does not offer the batching power of '''Masked Tint - Vertex Colors'''.  '''Masked Tint''' may be a good choice if you only want to colorize using a single color, and your meshes have over 300 vertices, removing the ability to use dynamic batching.
 
+
Finally, '''Masked Tint - Color from UV 2''' is an alternative to '''Masked Tint - Vertex Colors''', and is basically workaround, in the case where you don't have the ability to export vertex colors.
+
  
 
==Usage==
 
==Usage==
Line 19: Line 17:
  
  
'''Masked Tint - Color from UV 2''' requires a little more work.  In your texture map, you need to reserve some room for areas of color that you will want to use to tint your model.  Ideally, you could just use one pixel for every color you will want to use, but using compression, or downsampling your image, will completely ruin your color scheme.  Therefore, it's best to make each "colorizing area" as large as possible, with each one being about the same size if possible.
+
[[Media:Masked Tint.zip|Click here for a zip file that includes these shaders.]]
 
+
As with '''Masked Tint - Vertex Colors''', you will need to create one mesh for each color you want to use.  The first UV map of these meshes should be created "as normal", but you will have to take the colorizing areas into account, of course.  For the second UV map, the easiest thing to do, is to select all of your vertices, scale to zero, and move that point to the center of the colorizing area you want to use for that mesh.
+
 
+
One benefit of this shader over '''Masked Tint - Vertex Colors''', is that you can avoid interpolation from occurring along UV seams.  However, having to use colorizing areas is tedious, and limits flexibility, and this shader adds a draw call for pre-3GS hardware, so I don't recommend it unless absolutely necessary.
+
 
+
 
+
[[Media:Masked Tint.zip|Click here for a zip file that includes these three shaders.]]
+
  
 
==ShaderLab - Masked Tint.shader==
 
==ShaderLab - Masked Tint.shader==
 
<shaderlab>Shader "Masked Tint" {
 
<shaderlab>Shader "Masked Tint" {
  
 
+
Properties {
Properties
+
{
+
 
_Color ("Tint Color", Color) = (1,1,1)
 
_Color ("Tint Color", Color) = (1,1,1)
 
_MainTex ("Texture  (A = Tint Mask)", 2D) = ""
 
_MainTex ("Texture  (A = Tint Mask)", 2D) = ""
 
}
 
}
  
SubShader  
+
SubShader {Pass { // iPhone 3GS and later
{
+
GLSLPROGRAM
Pass
+
varying mediump vec2 uv;
{
+
// tint the texture
+
#ifdef VERTEX
SetTexture [_MainTex]
+
void main() {
{
+
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
ConstantColor [_Color]
+
uv = gl_MultiTexCoord0.xy;
combine texture * constant
+
}
+
+
// alpha blend between untinted and tinted
+
SetTexture [_MainTex]
+
{
+
combine previous lerp(texture) texture
+
}
+
 
}
 
}
}
+
#endif
 +
 +
#ifdef FRAGMENT
 +
uniform lowp sampler2D _MainTex;
 +
uniform lowp vec3 _Color;
 +
void main() {
 +
vec4 texture = texture2D(_MainTex, uv);
 +
gl_FragColor = vec4(texture.rgb * (_Color * texture.a + (1. - texture.a)), 1);
 +
}
 +
#endif
 +
ENDGLSL
 +
}}
 +
 
 +
SubShader {Pass { // pre-3GS devices, including the September 2009 8GB iPod touch
 +
SetTexture[_MainTex] {Combine texture alpha * one - constant ConstantColor[_Color]}
 +
SetTexture[_MainTex] {Combine texture * one - previous}
 +
}}
  
 
 
}</shaderlab>
 
}</shaderlab>
  
Line 63: Line 59:
 
<shaderlab>Shader "Masked Tint - Vertex Colors" {
 
<shaderlab>Shader "Masked Tint - Vertex Colors" {
  
 
+
Properties {
Properties
+
{
+
 
_MainTex ("Texture  (A = Tint Mask)", 2D) = ""
 
_MainTex ("Texture  (A = Tint Mask)", 2D) = ""
 
}
 
}
  
SubShader  
+
SubShader {Pass { // iPhone 3GS and later
{
+
GLSLPROGRAM
Pass
+
varying mediump vec2 uv;
{
+
varying lowp vec3 color;
BindChannels
+
{
+
#ifdef VERTEX
Bind "Vertex", vertex
+
void main() {
Bind "Texcoord", texcoord
+
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
Bind "Color", color
+
uv = gl_MultiTexCoord0.xy;
}
+
color = gl_Color.rgb;
+
// tint by the vertex colors
+
SetTexture [_MainTex]
+
{
+
combine primary * texture
+
}
+
+
// alpha blend between untinted and tinted
+
SetTexture [_MainTex]
+
{
+
combine previous lerp(texture) texture
+
}
+
 
}
 
}
}
+
#endif
 
+
+
#ifdef FRAGMENT
}</shaderlab>
+
uniform lowp sampler2D _MainTex;
 
+
void main() {
==ShaderLab - Masked Tint - Color from UV2.shader==
+
vec4 texture = texture2D(_MainTex, uv);
<shaderlab>Shader "Masked Tint - Color from UV2" {
+
gl_FragColor = vec4(texture.rgb * (color * texture.a + (1. - texture.a)), 1);
 
+
 
+
Properties
+
{
+
_MainTex ("Texture  (A = Tint Mask)", 2D) = ""
+
}
+
 
+
// iPhone 3GS and later
+
SubShader
+
{
+
Pass
+
{
+
BindChannels
+
{
+
Bind "Vertex", vertex
+
Bind "texcoord1", texcoord0
+
Bind "texcoord", texcoord1
+
Bind "texcoord", texcoord2
+
}
+
+
// grab the tint color
+
SetTexture [_MainTex]
+
+
// tint the texture
+
SetTexture [_MainTex]
+
{
+
combine texture * previous
+
}
+
+
// alpha blend between untinted and tinted
+
SetTexture [_MainTex]
+
{
+
combine previous lerp(texture) texture
+
}
+
 
}
 
}
}
+
#endif
 +
ENDGLSL
 +
}}
  
// pre-3GS devices, including the September 2009 8GB iPod touch
+
SubShader {Pass { // pre-3GS devices, including the September 2009 8GB iPod touch
SubShader
+
BindChannels {
{
+
Bind "vertex", vertex
Pass
+
Bind "color", color
{
+
Bind "texcoord", texcoord
BindChannels
+
{
+
Bind "Vertex", vertex
+
Bind "texcoord1", texcoord0
+
Bind "texcoord", texcoord1
+
}
+
+
// grab the tint color
+
SetTexture [_MainTex]
+
+
// tint the texture
+
SetTexture [_MainTex]
+
{
+
combine texture * previous
+
}
+
 
}
 
}
+
SetTexture[_MainTex] {Combine texture alpha * one - primary}
// alpha blend between untinted and tinted
+
SetTexture[_MainTex] {Combine texture * one - previous}
Pass
+
}}
{
+
Blend SrcAlpha OneMinusSrcAlpha
+
+
SetTexture [_MainTex]
+
{
+
combine texture, one - texture
+
}
+
}
+
}
+
 
+
 
   
 
   
 
}</shaderlab>
 
}</shaderlab>

Revision as of 19:25, 12 April 2011

Author: Jessy

Contents

Description

These nine posh butterfly shower curtains require only one draw call.

These shaders tint the model's texture based on a mask, so that you can selectively colorize portions of your texture. The Masked Tint - Vertex Colors shader allows you to have many different color variations for the tinted area, on many different instances of your model, but only use one draw call.

Masked Tint will allow you to save memory, by not having to store a different mesh for every color you want to use, but does not offer the batching power of Masked Tint - Vertex Colors. Masked Tint may be a good choice if you only want to colorize using a single color, and your meshes have over 300 vertices, removing the ability to use dynamic batching.

Usage

Create a texture, with RGB colors where you want to share colors between all instances of the mesh. For the areas that you will want to colorize differently, I recommend using grayscale. Create a mask, stored in the alpha channel, that gets whiter as you want more colorization. I assume that for most usage, you will want to mainly use pure white or black, with the gray antialiased edges between making transitions look good.

If you're using Masked Tint, then all you need to do is drag the RGBA texture onto the material's only variable slot, and choose a Tint Color. You'll need a different Unity Material for every different color you want to use.

Masked Tint - Vertex Colors requires the use of only one material for all similar objects, but you will need to create a separate mesh for each color scheme. As the name implies, you paint the tint colors into the vertex colors.


Click here for a zip file that includes these shaders.

ShaderLab - Masked Tint.shader

<shaderlab>Shader "Masked Tint" {

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

SubShader {Pass { // iPhone 3GS and later GLSLPROGRAM varying mediump vec2 uv;

#ifdef VERTEX void main() { gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; uv = gl_MultiTexCoord0.xy; } #endif

#ifdef FRAGMENT uniform lowp sampler2D _MainTex; uniform lowp vec3 _Color; void main() { vec4 texture = texture2D(_MainTex, uv); gl_FragColor = vec4(texture.rgb * (_Color * texture.a + (1. - texture.a)), 1); } #endif ENDGLSL }}

SubShader {Pass { // pre-3GS devices, including the September 2009 8GB iPod touch SetTexture[_MainTex] {Combine texture alpha * one - constant ConstantColor[_Color]} SetTexture[_MainTex] {Combine texture * one - previous} }}

}</shaderlab>

ShaderLab - Masked Tint - Vertex Colors.shader

<shaderlab>Shader "Masked Tint - Vertex Colors" {

Properties { _MainTex ("Texture (A = Tint Mask)", 2D) = "" }

SubShader {Pass { // iPhone 3GS and later GLSLPROGRAM varying mediump vec2 uv; varying lowp vec3 color;

#ifdef VERTEX void main() { gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; uv = gl_MultiTexCoord0.xy; color = gl_Color.rgb; } #endif

#ifdef FRAGMENT uniform lowp sampler2D _MainTex; void main() { vec4 texture = texture2D(_MainTex, uv); gl_FragColor = vec4(texture.rgb * (color * texture.a + (1. - texture.a)), 1); } #endif ENDGLSL }}

SubShader {Pass { // pre-3GS devices, including the September 2009 8GB iPod touch BindChannels { Bind "vertex", vertex Bind "color", color Bind "texcoord", texcoord } SetTexture[_MainTex] {Combine texture alpha * one - primary} SetTexture[_MainTex] {Combine texture * one - previous} }}

}</shaderlab>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox