Masked Tint

From Unify Community Wiki
Revision as of 05:22, 15 November 2009 by Jessy (Talk | contribs)

Jump to: navigation, search

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

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. (As far as I know, this is true of modo, for example.)

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.


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.

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.


Click here for a zip file that includes these three 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 - Vertex Colors.shader

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


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

SubShader { Pass { BindChannels { Bind "Vertex", vertex Bind "Texcoord", texcoord Bind "Color", color }

// tint by the vertex colors SetTexture [_MainTex] { combine primary * texture }

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