iPhone Shaders: Simple diffuse with alpha and Lightmap

Hi all,

To make sure we’re as optimized as possible, we’ve put together all of our own shaders for the iPhone. Sometimes the layers of fallback shaders make it hard to know if you’re getting the most efficient shader for the iPhone. These are cobbled together from various samples, so I didn’t create them from scratch.

One is a simple diffuse texture for unlit characters with baked lighting in their diffuse texture that supports alpha testing. The other is a lightmap shader.

I’m struggling to write a shader for our lightmapped objects that require alpha testing or alpha blending. The lightmap alpha testing shader would be a combination of these two shaders below. Any help would be appreciated.

Otherwise, I’ve seen requests for these two shaders elsewhere. Enjoy.

Here is our simple “no lighting” diffuse texture shader that supports alpha testing (cutout).

Shader "Unlit Diffuse Alpha" {
Properties {
    _Color ("Color Tint", Color) = (1,1,1,1)
    _MainTex ("Base (RGB) Alpha (A)", 2D) = "white" {}
	_Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
}

SubShader {
	Cull Off
	Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="TransparentCutoff"}
	Pass {
		Alphatest Greater [_Cutoff]
		AlphaToMask True
		ColorMask RGB

		SetTexture [_MainTex] {
			Combine texture, texture 
		} 
	}
}

}

And here is our simple lightmap diffuse shader that we use for lightmaping our scenes. This is a two pass shader that multiplies the lightmap texture with the color constant and the diffuse text.

Shader "Unlit Lightmap Diffuse" {

Properties {
	_Color ("Main Color", Color) = (1,1,1,1) 
	_MainTex ("Base (RGB)", 2D) = "white"
	_LightMap ("Lightmap (RGB)", 2D) = "white"
}
   
SubShader {
	Cull Back
	
   // Base pass: lightmap
   Pass {
      Name "LIGHTMAP"
      Tags {"LightMode" = "Always"}
      BindChannels {
         Bind "Vertex", vertex
         Bind "texcoord1", texcoord0 // lightmap uses 2nd uv
      }
      SetTexture [_LightMap] { constantColor [_Color] combine texture * constant DOUBLE }
   }
   
   // Second pass: modulate with texture
   Pass {
      Name "BASE"
      Tags {"LightMode" = "Always"}
      BindChannels {
         Bind "Vertex", vertex
         Bind "texcoord", texcoord0 // main uses 1st uv
      }
      Blend Zero SrcColor
      SetTexture [_MainTex] { combine texture }
   }
}  
}

Thanks,
Matthew Scott
Manifest Games

thank you a lot!
are those on unitedWiki? If not… it may be a better place for the resource!

I didn’t go deep enough into shader lab stuff, so this might sound noobish:
Thats about the 3rd shader I’ve seen that uses two passes for lightmap. I’ve worked with DX7 and FFP for a long time and there you could put that all into a single pass.

The iphone has 2 texture units which allows 2 textures in parallel so something like lightmapping theoretically should work in 1 pass shouldn’t it? (don’t see how the color influences it as it ends on the vertex color, at least I really hope that it does not introduce a virtual 1x1 texture to tint)

Am I missing something on that thinking?

You’re right. There are 2 texture units.
But I don’t know if you can have two different blend settings in the same pass. You’ll notice that the 2nd pass uses Blend Zero SrcColor to blend the main texture with the lightmap.

I’ll see if I can optimize it, but so far I’m not taking a rendering hit on the device.

Any ideas for how to do the lightmap shader with alpha testing?

Thanks,
Matthew Scott
Manifest Games

Don’t use alpha testing on the iphone but alpha blending.
Alpha testing is considerably slower than blending.

As for the rendering hit: each pass renders the object once again, which means another draw call and another time its geometry. But you might be low enough on both to not feel the impact or not have one of the normal devices but a high speed one (3GS or itouch 2nd gen)

Actually, you are forced to use Alpha testing, because Alpha blending can’t manage depth properly.

In short, with alpha blending, you’ll have perfect alpha blends, with no “cut edges” on your textures, but all the objects using semi-transparency will mask other parts of the same texture if it’s placed behind.

Here is the doc explaining it : Unity - Manual: ShaderLab: legacy alpha testing

That’s why in order to use lightmaps, or self illuminated maps, with integrated semi transparent
zones, you’ll have to make it 2 pass, alpha test. :slight_smile:

Here is one I wrote, putting 2 pass self illum + alpha, based on Aras ones :

Shader "_SelfIllumAlpha" {
  Properties {
        _Color ("Main Color", Color) = (1,1,1,1)
   _SpecColor ("Spec Color", Color) = (1,1,1,1)
   _Shininess ("Shininess", Range (0.1, 1)) = 0.8
   _MainTex ("Base (RGB)", 2D) = "white" {}
   _SelfIllum ("Illumin (A)", 2D) = "bump" {}
   _Cutoff ("Base Alpha cutoff", Range (0,.9)) = .95
    }

  SubShader {
  Tags {"RenderType"="Transparent" "Queue"="Transparent" "IgnoreProjector"="True"}
       SeparateSpecular On
      Lighting On

      
   // Vertex lights
   Pass {
     
      Tags {"LightMode" = "Vertex"}
      Material {
         Diffuse [_Color]
         Emission [_PPLAmbient]
         Shininess [_Shininess]
         Specular [_SpecColor]
      }
     
      ZWrite On
      AlphaTest GEqual [_Cutoff]

      SetTexture [_SelfIllum] {
         constantColor (.5,.5,.5)
         combine constant lerp (texture) primary
      }
      SetTexture [_MainTex] {
         Combine texture * previous DOUBLE, texture
      }
   }
   
   Pass {
        
         ZWrite off
         AlphaTest Less [_Cutoff]

         // Set up alpha blending
         Blend SrcAlpha OneMinusSrcAlpha

         
     SetTexture [_SelfIllum] {
         constantColor (.5,.5,.5)
         combine constant lerp (texture) primary
      }
      SetTexture [_MainTex] {
         Combine texture * previous DOUBLE, texture
      }

      }
}

 

}

My selfillum process takes the alpha layer of a separate map, but could work with black white, using addition instead of lerping in the selfillum texture pass. But I found lerping more realistic than black white.

Another difference is this shader don’t use 2 UVs, as the selfillum map don’t require high pixel precision, it can be based on the base texture positions, and resized to 256x256 pixels.
I prefer this method because of the faster workflow, but it could be better for some other cases to set 2 separate UVs.

I can’t access my home macbook now, but I remember having jumped to a more optimized than this one. I will update it tonight.

Still, it works.

But actually, if a better solution exists, I’m really interested !

Hi, all. First of all, I’m a newbie and thanks to MatthewScott.
I needed a shader of lightmap + cutoff for trees, buildings, etc.
The alpha value is in main texture.
I mixed a shader and It seems to work.
But I’m worried about the shader’s performance and properness.
Or another good solution is welcome.

Shader "Tree" {
	Properties {
		_Color ("Main Color", Color) = (1,1,1,1)
		_MainTex ("Base (RGB)", 2D) = "white" {}
		_Cutoff ("Alpha Cutoff", Range(0,1)) = 0.5
		_LightMap ("Lightmap (RGB)", 2D) = "white" {}
	}
	SubShader {
		Cull Off 
		Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="TransparentCutoff"} 

		Pass {
	      Name "LIGHTMAP" 
	      Tags {"LightMode" = "Always"} 
	      BindChannels { 
	         Bind "Vertex", vertex 
	         Bind "texcoord1", texcoord0 // lightmap uses 2nd uv 
	         Bind "texcoord", texcoord1 // alphamap uses 1nd uv 
	      } 
			
	      Alphatest Greater [_Cutoff]
	      AlphaToMask True 
	      ColorMask RGB 
		  
	      SetTexture [_LightMap] { constantColor [_Color] combine texture * constant DOUBLE }
	      SetTexture [_MainTex] { combine previous * texture, texture }
	      
		}
	}
}

AlphaTest generally isn’t a good idea unless its really needed because the iphone gpu is opted for alpha blending, not testing.

Also the Lightmode Tag isn’t required as you only have vertex.

Thanks dreamora.
I removed Lightmode. and tried removing AlphaTest.
But because of depth, trees seems to need AlphaTest.
So, I decided separate the shader.
Basically most uses AlphaBlend shader, and the rest uses AlphaTest shader.
How about this?

Shader "Tree" {
	Properties {
		_Color ("Main Color", Color) = (1,1,1,1)
		_MainTex ("Base (RGB)", 2D) = "white" {}
		_Cutoff ("Alpha Cutoff", Range(0,1)) = 0.5
		_LightMap ("Lightmap (RGB)", 2D) = "white" {}
	}
	SubShader {
		Cull Off 
		Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="TransparentCutoff"} 
		Pass {
	      BindChannels { 
	         Bind "Vertex", vertex 
	         Bind "texcoord1", texcoord0 // lightmap uses 2nd uv 
	         Bind "texcoord", texcoord1 // alphamap uses 1nd uv 
	      } 
			
	      Alphatest Greater [_Cutoff]
	      AlphaToMask True 
	      ColorMask RGB 
		  
	      SetTexture [_LightMap] { constantColor [_Color] combine texture * constant DOUBLE }
	      SetTexture [_MainTex] { combine previous * texture, texture }
	      
		}
	}
}
Shader "Building" {
	Properties {
		_Color ("Main Color", Color) = (1,1,1,1)
		_MainTex ("Base (RGB)", 2D) = "white" {}
		_LightMap ("Lightmap (RGB)", 2D) = "white" {}
	}
	SubShader {
		Tags {"Queue"="Transparent" "IgnoreProjector"="True"} 
		Pass {
	      BindChannels { 
	         Bind "Vertex", vertex 
	         Bind "texcoord1", texcoord0 // lightmap uses 2nd uv 
	         Bind "texcoord", texcoord1 // alphamap uses 1nd uv 
	      } 
			
		  Blend SrcAlpha OneMinusSrcAlpha
		  
	      SetTexture [_LightMap] { constantColor [_Color] combine texture * constant DOUBLE }
	      SetTexture [_MainTex] { combine previous * texture, texture }
		}
	}
}