Adjusting a shader to fade with distance

I’m using this shader for some gui elements that are being rendered with an orthographic camera.

Shader "iPhone/Particles/Additive Culled" {
Properties {
	_TintColor ("Tint Color", Color) = (0.5,0.5,0.5,0.5)
	_MainTex ("Particle Texture", 2D) = "white" {}
}

Category {
	Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
	Blend SrcAlpha One
	AlphaTest Greater .01
	ColorMask RGB
	Cull Back Lighting Off ZWrite Off Fog { Color (0,0,0,0) }
	BindChannels {
		Bind "Color", color
		Bind "Vertex", vertex
		Bind "TexCoord", texcoord
	}
	
	// ---- Dual texture cards
	SubShader {
		Pass {
			SetTexture [_MainTex] {
				constantColor [_TintColor]
				combine constant * primary
			}
			SetTexture [_MainTex] {
				combine texture * previous DOUBLE
			}
		}
	}
	
	// ---- Single texture cards (does not do color tint)
	SubShader {
		Pass {
			SetTexture [_MainTex] {
				combine texture * primary
			}
		}
	}
}
}

I need some of these elements to fade in or out. Is there any way to adjust this shader so that the texture value is multiplied by the object’s distance from camera.

I would usually just use SetColor of the material, but I want the elements to batch.

Try checking out the shaders of PuzzleBloom at http://robert.cupisz.eu/.
It’s a great example of shaders that do what you want. They have a part where the blooming part starts from one point in the scene and goes out based on the distance from that point.
They are for the “regular” Unity editor and not the iPhone version, but it should work.
I think… :confused:

So why not use SetColor on the Shared Material ? do you what them to fade at different times and still batch?

Sorry I didn’t reply to this, must not have turned on notify.

cyb3rmaniak, that link appears dead :(, at least I can’t get to it.
Edit: Now it’s working, I’ll check out those shaders tonight.

UVRadiation, you are correct, I want them to fade in/out at different times. Maybe there is another way to do it?

If these elements are 2D, SpriteManager 2 yields this functionality (ability to set the color/alpha arbitrarily) without breaking batching. You can find out more here:
http://www.anbsoft.com/middleware/sm2

Apart from that, I’m not sure it is possible to do this using a shader on iPhone as the iPhone only has a fixed function pipeline. Then again, I’m not a shader expert, so perhaps it can be done using fixed function?

I am not sure how this can be made but there is a builtin var inside shaderlab: _ObjectSpaceCameraPos
which is the Camera’s position in object space
that you can access inside your shader, so I would somehow get the distance from that vector and use it for whatever you like…

I know that to get the magnitude of a vector you need to get the sqaure root of xx+yy+z*z but I don’t know how you can do it inside an iPhone shader.

I would only need the z component of _ObjectSpaceCameraPos and multiply the texture values by that so no need for calculations. I just have no idea how this could be applied to this shader.

I was planning on getting Sprite Manager 2 for my next game when its released, but for this game the interface is almost done, I just wanted some stuff to fade in and out.