Masked Tint

From Unify Community Wiki
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. 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

Invalid language.

You need to specify a language like this: <source lang="html4strict">...</source>

Supported languages for syntax highlighting:

4cs, 6502acme, 6502kickass, 6502tasm, 68000devpac, abap, actionscript, actionscript3, ada, algol68, apache, applescript, apt_sources, asm, asp, autoconf, autohotkey, autoit, avisynth, awk, bascomavr, bash, basic4gl, bf, bibtex, blitzbasic, bnf, boo, c, c_loadrunner, c_mac, caddcl, cadlisp, cfdg, cfm, chaiscript, cil, clojure, cmake, cobol, coffeescript, cpp, cpp-qt, csharp, css, cuesheet, d, dcs, delphi, diff, div, dos, dot, e, ecmascript, eiffel, email, epc, erlang, euphoria, f1, falcon, fo, fortran, freebasic, fsharp, gambas, gdb, genero, genie, gettext, glsl, gml, gnuplot, go, groovy, gwbasic, haskell, hicest, hq9plus, html4strict, html5, icon, idl, ini, inno, intercal, io, j, java, java5, javascript, jquery, kixtart, klonec, klonecpp, latex, lb, lisp, llvm, locobasic, logtalk, lolcode, lotusformulas, lotusscript, lscript, lsl2, lua, m68k, magiksf, make, mapbasic, matlab, mirc, mmix, modula2, modula3, mpasm, mxml, mysql, newlisp, nsis, oberon2, objc, objeck, ocaml, ocaml-brief, oobas, oracle11, oracle8, oxygene, oz, pascal, pcre, per, perl, perl6, pf, php, php-brief, pic16, pike, pixelbender, pli, plsql, postgresql, povray, powerbuilder, powershell, proftpd, progress, prolog, properties, providex, purebasic, pycon, python, q, qbasic, rails, rebol, reg, robots, rpmspec, rsplus, ruby, sas, scala, scheme, scilab, sdlbasic, smalltalk, smarty, sql, systemverilog, tcl, teraterm, text, thinbasic, tsql, typoscript, unicon, uscript, vala, vb, vbnet, verilog, vhdl, vim, visualfoxpro, visualprolog, whitespace, whois, winbatch, xbasic, xml, xorg_conf, xpp, yaml, z80, zxbasic


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 - Masked Tint - Vertex Colors.shader

Invalid language.

You need to specify a language like this: <source lang="html4strict">...</source>

Supported languages for syntax highlighting:

4cs, 6502acme, 6502kickass, 6502tasm, 68000devpac, abap, actionscript, actionscript3, ada, algol68, apache, applescript, apt_sources, asm, asp, autoconf, autohotkey, autoit, avisynth, awk, bascomavr, bash, basic4gl, bf, bibtex, blitzbasic, bnf, boo, c, c_loadrunner, c_mac, caddcl, cadlisp, cfdg, cfm, chaiscript, cil, clojure, cmake, cobol, coffeescript, cpp, cpp-qt, csharp, css, cuesheet, d, dcs, delphi, diff, div, dos, dot, e, ecmascript, eiffel, email, epc, erlang, euphoria, f1, falcon, fo, fortran, freebasic, fsharp, gambas, gdb, genero, genie, gettext, glsl, gml, gnuplot, go, groovy, gwbasic, haskell, hicest, hq9plus, html4strict, html5, icon, idl, ini, inno, intercal, io, j, java, java5, javascript, jquery, kixtart, klonec, klonecpp, latex, lb, lisp, llvm, locobasic, logtalk, lolcode, lotusformulas, lotusscript, lscript, lsl2, lua, m68k, magiksf, make, mapbasic, matlab, mirc, mmix, modula2, modula3, mpasm, mxml, mysql, newlisp, nsis, oberon2, objc, objeck, ocaml, ocaml-brief, oobas, oracle11, oracle8, oxygene, oz, pascal, pcre, per, perl, perl6, pf, php, php-brief, pic16, pike, pixelbender, pli, plsql, postgresql, povray, powerbuilder, powershell, proftpd, progress, prolog, properties, providex, purebasic, pycon, python, q, qbasic, rails, rebol, reg, robots, rpmspec, rsplus, ruby, sas, scala, scheme, scilab, sdlbasic, smalltalk, smarty, sql, systemverilog, tcl, teraterm, text, thinbasic, tsql, typoscript, unicon, uscript, vala, vb, vbnet, verilog, vhdl, vim, visualfoxpro, visualprolog, whitespace, whois, winbatch, xbasic, xml, xorg_conf, xpp, yaml, z80, zxbasic


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}
}}
 
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Tools