[iPhone] Self Illum shader with transparency based on color

Hello,

For optimization purpose, I’m trying to build a shader that would do Self Illumination + texturing (so 2 textures limit would be reached), and that additionally would create transparency based on a particular color in the lightmap texture.

For example, color #785634 would render its mainTex to alpha 0.5

Is it possible ?

This feature would be actually vital for iPhone environment rendering, as transparency is needed in most modern scenes (glass, water, etc), coupled with selfillum for performance saving. But the 2 textures shader limit puts a brake on it.

The solution would be to use a RGBA mainTex, but that would result in huge memory allocations, so that particular color solution would be perfect.

Any idea ?

You can only do alpha blending based on alpha values on the iPhone. This means you will need an explicit alpha channel.

too bad :frowning:

So there is no shader for the iPhone that can support Alpha + Self Illumination + VertexLit ?

My bad, I mixed Lightmap with Self Illumination …

Changed the posts, but still, the problem is the same.

Haha, I’m sorry to make this thread a monologue, but I finally found how to render Self Illum + Alpha + Vertex Lit.

Just change Blend AppSrcAdd AppDstAdd to Blend SrcAlpha OneMinusSrcAlpha in the builtin shader Illumin/VertexLit.

Yay.

Can you post the shader source? I have a feeling that won’t work completely.

I won’t be able to be in front of my computer until tomorrow, but it’s just the Illum-VertexLit Shader in this link.

I was thinking the same as you when I downloaded it, but still tried, and bam it works.

In fact, the illum is different from the lightmap as it doesn’t use color, but alpha map.

So I just put my white lights on a transparent background in a PNG, import it under the self illum texture, and job’s done.

I tested it in the editor, with different directional light intensities, and then without the self illum map. Got it on a scene with window lit buildings and neons, coupled with a glass texture on a shop.
Everything works perfectly, even on iPhone.

Will put a screenshot tomorrow.

Ok, that looks like it should work. I’d forgotten that there would only be one pass. If you did the same thing with a pixel shader, you’d get strange looking lighting.

In fact, the built-in shader has two passes (ambient + vertex lights), but I strippped it to one (vertex lights).

I’m a real newbie in term of shaders, so I can’t seize how much pixel light could enhance visuals. But as I see on the iPhone screen, there’s not much that difference between pixel and vertex lightings.

On another hand, I wish we could use CG with iPhone shaders, because having control over unlight zones would be cool. Actually, it puts too much black, unless we use several lights or some emission. But emission is not so elegant :confused:

The built-in shader only uses one pass. It has two different passes defined, but one is used when there are pixel lights or no lights, and the other is used when there are vertex lights.

Ha, my bad. Thank you.

updated :

I found that technique I mentionned above is not efficient, as it overwrites Z buffer.

So here is a correct shader for Self Illumination + Alpha + VertexLit + Alpha Cutoff, based on some posted by the almighty Aras.

2 passes, works great on OpenGL ES 1.1 (iPhone) with no FPS loss from a basic VertexLit shader (frameTime in the Profiler is exactly the same).

Shader "_iSelfIllumAlpha" {
  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" {}
	_BumpMap ("Illumin (A)", 2D) = "bump" {}
	_Cutoff ("Base Alpha cutoff", Range (0,.9)) = .95
    }

  SubShader {
  Tags {"RenderType"="Transparent" "Queue"="Transparent" "IgnoreProjector"="True"}
		

		
	// Vertex lights
	Pass {
		Name "BASE"
		Tags {"LightMode" = "Vertex"}
		Material {
			Diffuse [_Color]
			Emission [_PPLAmbient]
			Shininess [_Shininess]
			Specular [_SpecColor]
		}
		SeparateSpecular On
		Lighting On
		ZWrite On
		AlphaTest Greater [_Cutoff]
		SetTexture [_BumpMap] {
			constantColor (.5,.5,.5)
			combine constant lerp (texture) primary
		}
		SetTexture [_MainTex] {
			Combine texture * previous DOUBLE, texture*primary
		}
	}
	
	Pass {
			// Dont write to the depth buffer
			ZWrite off
			// Don't write pixels we have already written.
			ZTest Less
			// Only render pixels less or equal to the value
			AlphaTest LEqual [_Cutoff]

			// Set up alpha blending
			Blend SrcAlpha OneMinusSrcAlpha

			
		SetTexture [_MainTex] {
			constantColor (1,1,1)
			Combine texture * constant DOUBLE, texture*primary
		}

		}
}

  

}