Blend 2 Textures by Lightmap Alpha

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m
m (Text replace - "</shaderlab>" to "</syntaxhighlight>")
 
(4 intermediate revisions by 2 users not shown)
Line 11: Line 11:
  
 
The blend map is stored in the alpha channel of the lightmap.  The two textures mix as if, in Photoshop, you had ''Texture 1'' in a layer above ''Texture 2'', and ''Texture 1'' used the blend map as a layer mask.  i.e. white = ''Texture 1'', black = ''Texture 2''.  In my usage so far, I've found that it's important to have the lightmap/splatmap be at a high resolution, to preserve the shape of fine splat details, but that it's okay to use PVRTC 2 bpp compression, as the color detail is mainly coming from ''Texture 1'' and ''Texture 2''.
 
The blend map is stored in the alpha channel of the lightmap.  The two textures mix as if, in Photoshop, you had ''Texture 1'' in a layer above ''Texture 2'', and ''Texture 1'' used the blend map as a layer mask.  i.e. white = ''Texture 1'', black = ''Texture 2''.  In my usage so far, I've found that it's important to have the lightmap/splatmap be at a high resolution, to preserve the shape of fine splat details, but that it's okay to use PVRTC 2 bpp compression, as the color detail is mainly coming from ''Texture 1'' and ''Texture 2''.
 
 
[[Media:Blend 2 Textures by Lightmap Alpha.zip|Click here for a zip file that includes this shader.]]
 
  
 
==ShaderLab - Blend 2 Textures by Lightmap Alpha.shader==
 
==ShaderLab - Blend 2 Textures by Lightmap Alpha.shader==
<shaderlab>Shader "Blend 2 Textures by Lightmap Alpha" {
+
<syntaxhighlight lang="shaderlab">Shader "Blend 2 Textures by Lightmap Alpha" {
  
 
Properties {
 
Properties {
_MainTex ("Texture 1 (RGB)", 2D) = ""
+
_MainTex ("Texture 1 (RGB)", 2D) = ""
_Texture2 ("Texture 2 (RGB)", 2D) = ""
+
_Tex2 ("Texture 2 (RGB)", 2D) = ""
_LightMap ("Lightmap (A = Blend)", 2D) = ""
+
_Lightmap ("Lightmap (A=Blend)", 2D) = ""
 
}
 
}
  
Category {
+
// iPhone 3GS and later
 +
SubShader {Pass {
 +
GLSLPROGRAM
 +
varying mediump vec2 mainTexUV, tex2UV, lightmapUV;
 +
 +
#ifdef VERTEX
 +
uniform mediump vec4 _MainTex_ST, _Tex2_ST;
 +
void main() {
 +
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
 +
mainTexUV = gl_MultiTexCoord0.xy * _MainTex_ST.xy + _MainTex_ST.zw;
 +
tex2UV = gl_MultiTexCoord0.xy * _Tex2_ST.xy + _Tex2_ST.zw;
 +
lightmapUV = gl_MultiTexCoord1.xy;
 +
}
 +
#endif
 +
 +
#ifdef FRAGMENT
 +
uniform lowp sampler2D _MainTex, _Tex2, _Lightmap;
 +
void main() {
 +
lowp vec4 lightmap_mix = texture2D(_Lightmap, lightmapUV);
 +
gl_FragColor = lightmap_mix *
 +
mix(texture2D(_Tex2, tex2UV), texture2D(_MainTex, mainTexUV), lightmap_mix.a);
 +
}
 +
#endif 
 +
ENDGLSL
 +
}}
 +
 
 +
// pre-3GS devices, including the September 2009 8GB iPod touch
 +
SubShader {
 
BindChannels {
 
BindChannels {
 
Bind "vertex", vertex
 
Bind "vertex", vertex
+
Bind "texcoord", texcoord0 // 1st UV - tiling textures
// 1st UV - tiling textures
+
Bind "texcoord1", texcoord1 // 2nd UV - lightmap and blend map
Bind "texcoord", texcoord0
+
Bind "texcoord", texcoord2 // 1st SubShader only
+
+
// 2nd UV - lightmap and blend map
+
Bind "texcoord1", texcoord1
+
Bind "texcoord1", texcoord3 // 1st SubShader only
+
 
}
 
}
 
+
Pass {
// iPhone 3GS and later
+
SetTexture[_Tex2]
SubShader {Pass {
+
SetTexture[_Lightmap] {Combine previous * texture}
// tile Texture 2
+
}
SetTexture[_Texture2]
+
Pass {
 
+
Blend SrcAlpha OneMinusSrcAlpha
// put the splat map into the alpha channel
+
SetTexture[_MainTex]
SetTexture[_LightMap] {Combine previous, texture}
+
// Apply the lightmap to Texture 1  
+
// and blend in the result, using the alpha map
// tile Texture 1 and combine it with Texture 2 based on the splat map
+
SetTexture[_Lightmap] {Combine previous * texture, texture}
SetTexture[_MainTex] {Combine texture lerp(previous) previous}
+
+
// apply the lightmap
+
SetTexture[_LightMap] {Combine previous * texture}
+
}}
+
 
+
// pre-3GS devices, including the September 2009 8GB iPod touch
+
SubShader {
+
Pass {
+
// tile Texture 2
+
SetTexture[_Texture2]
+
+
// apply the lightmap to Texture 2
+
SetTexture[_LightMap] {Combine previous * texture}
+
}
+
Pass {
+
Blend SrcAlpha OneMinusSrcAlpha
+
+
// tile Texture 1
+
SetTexture[_MainTex]
+
+
// apply the lightmap to Texture 1  
+
// and blend in the result, using the splat map
+
SetTexture[_LightMap] {Combine previous * texture, texture}
+
}
+
 
}
 
}
 
}
 
}
 
   
 
   
}</shaderlab>
+
}</syntaxhighlight>

Latest revision as of 21:56, 17 January 2012

Author: Jessy

[edit] Description

A textured quad that uses this shader.

This shader mixes two tiling textures based on an alpha blending map, and uses a lightmap instead of real-time lighting.

[edit] Usage

In your modeling app, you create two UV sets - one for Texture 1 and Texture 2, and another for the lightmap and blending map. The idea is that you will tile the textures using UV1, but not in UV2 - standard lightmapping practice. By default, this means that your two main textures will tile at the same "rate", but you can use the Offset and/or Tiling controls in the Material, if you think it will add visual interest.

The blend map is stored in the alpha channel of the lightmap. The two textures mix as if, in Photoshop, you had Texture 1 in a layer above Texture 2, and Texture 1 used the blend map as a layer mask. i.e. white = Texture 1, black = Texture 2. In my usage so far, I've found that it's important to have the lightmap/splatmap be at a high resolution, to preserve the shape of fine splat details, but that it's okay to use PVRTC 2 bpp compression, as the color detail is mainly coming from Texture 1 and Texture 2.

[edit] ShaderLab - Blend 2 Textures by Lightmap Alpha.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 "Blend 2 Textures by Lightmap Alpha" {

Properties {
	_MainTex ("Texture 1 (RGB)", 2D) = ""
	_Tex2 ("Texture 2 (RGB)", 2D) = ""
	_Lightmap ("Lightmap (A=Blend)", 2D) = ""
}

// iPhone 3GS and later
SubShader {Pass {
	GLSLPROGRAM
	varying mediump vec2 mainTexUV, tex2UV, lightmapUV;
			
	#ifdef VERTEX		
	uniform mediump vec4 _MainTex_ST, _Tex2_ST;
	void main() {
		gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
		mainTexUV = gl_MultiTexCoord0.xy * _MainTex_ST.xy + _MainTex_ST.zw;
		tex2UV = gl_MultiTexCoord0.xy * _Tex2_ST.xy + _Tex2_ST.zw;
		lightmapUV = gl_MultiTexCoord1.xy;		
	}
	#endif
	
	#ifdef FRAGMENT
	uniform lowp sampler2D _MainTex, _Tex2, _Lightmap;
	void main() {
		lowp vec4 lightmap_mix = texture2D(_Lightmap, lightmapUV);
		gl_FragColor = lightmap_mix *
			mix(texture2D(_Tex2, tex2UV), texture2D(_MainTex, mainTexUV), lightmap_mix.a);
	}
	#endif  
	ENDGLSL
}}

// pre-3GS devices, including the September 2009 8GB iPod touch
SubShader {
	BindChannels {
		Bind "vertex", vertex
		Bind "texcoord", texcoord0	// 1st UV - tiling textures		
		Bind "texcoord1", texcoord1	// 2nd UV - lightmap and blend map
	}
	Pass {			
		SetTexture[_Tex2]
		SetTexture[_Lightmap] {Combine previous * texture}
	}
	Pass {
		Blend SrcAlpha OneMinusSrcAlpha	
		SetTexture[_MainTex]
		// Apply the lightmap to Texture 1 
		// and blend in the result, using the alpha map
		SetTexture[_Lightmap] {Combine previous * texture, texture}
	}
}
 
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox