Blend 2 Textures by Lightmap Alpha

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Description)
m
Line 4: Line 4:
 
==Description==
 
==Description==
 
[[Image:MainBonsterWall.jpg|thumb|200px|A textured quad that uses this shader.]]
 
[[Image:MainBonsterWall.jpg|thumb|200px|A textured quad that uses this shader.]]
This shader blends between two tiling textures based on an alpha map, and uses a lightmap instead of real-time lighting.
+
This shader mixes two tiling textures based on an alpha blending map, and uses a lightmap instead of real-time lighting.
  
 
==Usage==
 
==Usage==
  
In your modeling app, you create two UV maps - one for ''Texture 1'' and ''Texture 2'', and another for the lightmap and splatmap.  The idea is that you will either extend the UVs of the model outside the 0-1 range, overlap them, or both, in the first map,  and not do any of that in the second - 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.
+
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 splatmap 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 splatmap 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''.
  
  
Line 18: Line 18:
 
<shaderlab>Shader "Blend 2 Textures by Lightmap Alpha" {
 
<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) = ""
 
_Texture2 ("Texture 2  (RGB)", 2D) = ""
_LightMap ("Lightmap  (A = Splat)", 2D) = ""
+
_LightMap ("Lightmap  (A = Blend)", 2D) = ""
 
}
 
}
  
// iPhone 3GS and later
+
Category {
SubShader
+
BindChannels {
{
+
Bind "vertex", vertex
Pass
+
{
+
BindChannels
+
{
+
Bind "Vertex", vertex
+
 
 
// 1st UV - tiling textures
+
// 1st UV - tiling textures
Bind "texcoord", texcoord0
+
Bind "texcoord", texcoord0
Bind "texcoord", texcoord2
+
Bind "texcoord", texcoord2 // 1st SubShader only
+
// 2nd UV - lightmap and splatmap
+
Bind "texcoord1", texcoord1
+
Bind "texcoord1", texcoord3
+
}
+
 
 
 +
// 2nd UV - lightmap and blend map
 +
Bind "texcoord1", texcoord1
 +
Bind "texcoord1", texcoord3 // 1st SubShader only
 +
}
 +
 +
// iPhone 3GS and later
 +
SubShader {Pass {
 
// tile Texture 2
 
// tile Texture 2
SetTexture [_Texture2]
+
SetTexture[_Texture2]
+
 
 
// put the splat map into the alpha channel
 
// put the splat map into the alpha channel
SetTexture [_LightMap]
+
SetTexture[_LightMap] {Combine previous, texture}
{
+
combine previous, texture
+
}
+
 
 
 
// tile Texture 1 and combine it with Texture 2 based on the splat map
 
// tile Texture 1 and combine it with Texture 2 based on the splat map
SetTexture [_MainTex]
+
SetTexture[_MainTex] {Combine texture lerp(previous) previous}
{
+
combine texture lerp(previous) previous
+
}
+
 
 
 
// apply the lightmap
 
// apply the lightmap
SetTexture [_LightMap]
+
SetTexture[_LightMap] {Combine previous * texture}
{
+
}}
combine previous * texture
+
}
+
}
+
}
+
  
// pre-3GS devices, including the September 2009 8GB iPod touch
+
// pre-3GS devices, including the September 2009 8GB iPod touch
SubShader  
+
SubShader {
{
+
Pass {
Pass
+
// tile Texture 2
{
+
SetTexture[_Texture2]
BindChannels
+
{
+
Bind "Vertex", vertex
+
Bind "texcoord", texcoord0
+
Bind "texcoord1", texcoord1
+
}
+
+
// tile Texture 2
+
SetTexture [_Texture2]
+
 
 
// apply the lightmap to Texture 2
+
// apply the lightmap to Texture 2
SetTexture [_LightMap]
+
SetTexture[_LightMap] {Combine previous * texture}
{
+
combine previous * texture
+
 
}
 
}
}
+
Pass {
+
Blend SrcAlpha OneMinusSrcAlpha
Pass
+
{
+
// tile Texture 1
Blend SrcAlpha OneMinusSrcAlpha
+
SetTexture[_MainTex]
 
 
BindChannels
+
// apply the lightmap to Texture 1  
{
+
// and blend in the result, using the splat map
Bind "Vertex", vertex
+
SetTexture[_LightMap] {Combine previous * texture, texture}
Bind "texcoord", texcoord0
+
Bind "texcoord1", texcoord1
+
}
+
+
// 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>
 
}</shaderlab>

Revision as of 14:25, 3 February 2011

Author: Jessy

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.

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.


Click here for a zip file that includes this shader.

ShaderLab - Blend 2 Textures by Lightmap Alpha.shader

<shaderlab>Shader "Blend 2 Textures by Lightmap Alpha" {

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

Category { BindChannels { Bind "vertex", vertex

// 1st UV - tiling textures Bind "texcoord", texcoord0 Bind "texcoord", texcoord2 // 1st SubShader only

// 2nd UV - lightmap and blend map Bind "texcoord1", texcoord1 Bind "texcoord1", texcoord3 // 1st SubShader only }

// iPhone 3GS and later SubShader {Pass { // tile Texture 2 SetTexture[_Texture2]

// put the splat map into the alpha channel SetTexture[_LightMap] {Combine previous, texture}

// tile Texture 1 and combine it with Texture 2 based on the splat map 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>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox