Masked Tint

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(New page: Category:iPhone Author: Jessy ==Description== [[Image:Masked Tint.png|thumb|200px|These seven posh butterfly shower curtains require only one draw call on the 3GS; two on the old stuf...)
 
(ShaderLab - Masked Tint - Color from UV2.shader)
Line 122: Line 122:
 
SetTexture [_MainTex]
 
SetTexture [_MainTex]
 
{
 
{
combine texture, one - texture alpha
+
combine texture, one - texture
 
}
 
}
 
}
 
}

Revision as of 21:33, 14 November 2009

Author: Jessy

Contents

Description

These seven posh butterfly shower curtains require only one draw call on the 3GS; two on the old stuff.

These shaders tint the model's texture based on a mask, so that you can selectively colorize only the portions of your texture that you want to. The Masked Tint - Color from UV 2 shader can allow you to have many different color variations for the tinted area, on many different instances of your model, but only use one (3GS & later) or two (pre-3GS) draw calls. However, it is otherwise limited in flexibility compared to Masked Tint, which will, however, require a draw call for each different tint color, using batching.

In the cases where you have dynamic objects that have more than 300 vertices, Masked Tint will be easier to use, and will even be faster on pre-3GS hardware. However, otherwise, as long as you are using more than two tint colors at once for instances of the same mesh, using Masked Tint - Color from UV 2 can yield better performance.

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.


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.

You will also 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.


Click here for a zip file that includes both of 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 { // tint the texture SetTexture [_MainTex] { ConstantColor [_Color] combine texture * constant }

// alpha blend between untinted and tinted SetTexture [_MainTex] { combine previous lerp(texture) texture } } }


}</shaderlab>

ShaderLab - Masked Tint - Color from UV2.shader

<shaderlab>Shader "Masked Tint - Color from UV2" {


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

// pre-3GS devices, including the September 2009 8GB iPod touch SubShader { Pass { BindChannels { Bind "Vertex", vertex Bind "texcoord1", texcoord0 Bind "texcoord", texcoord1 }

// grab the tint color SetTexture [_MainTex]

// tint the texture SetTexture [_MainTex] { combine texture * previous } }

// alpha blend between untinted and tinted Pass { Blend SrcAlpha OneMinusSrcAlpha

SetTexture [_MainTex] { combine texture, one - texture } } }


}</shaderlab>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox