Absolutely new to shaders (Need help with transparency)

I’ve been working on a fog particle for the past day or so, and it looks fine as far as texture and such, problem is, the particles look the same even at a distance.

What I need it to do is fade into the distance (the further away you get, the lest likely you are to see it, aka the more transparent it is.

Problem is, while I know I should be able to do this with a shader (either by shader+script to change it based on distance, or just as a shader, I’m not even 10% sure how shaders work really)

So, I need some help XD.

I’ve downloaded the built-in shaders script package, and I have the original particle additive soft shader that I am currently using.

Shader "Particles/Additive (Soft)" {
Properties {
	_MainTex ("Particle Texture", 2D) = "white" {}
}

Category {
	Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
	Blend One OneMinusSrcColor
	ColorMask RGB
	Cull Off 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] {
				combine texture * primary
			}
			SetTexture [_MainTex] {
				combine previous * previous alpha, previous
			}
		}
	}
	
	// ---- Single texture cards (does not do particle colors)
	SubShader {
		Pass {
			SetTexture [_MainTex] {
				combine texture * texture alpha, texture
			}
		}
	}
}
}

So how do I add something like alpha transparency that can be changed by a javascript or c# script.?

I don’t know what the exact syntax is off-hand, you’ll have to look at how some other shaders to it, but you basically need to add another variable in the Properties section which defines a range from 0…1. name it distance. Then later on in your SetTexture steps, somewhere, you either need to add that value to the alpha value or multiply it by the alpha value, to allow you to scale the alpha depending on distance.

Also I think in your first SetTexture of the dual-texture-unit version, you need to change your line from

SetTexture [_MainTex] {
combine texture * primary
}

to

SetTexture [_MainTex] {
combine texture * primary, texture
}

so that it will pull the alpha channel from the texture?

Then what you need to do is mix in that distance value, which you could do like:

SetTexture [_MainTex] {
combine texture * primary, texture * distance
}

as the first step.

Once you have the shader set up to accept the range variable and to multiply it by the alpha value, you then would need to use a script to modify the distance variable via the material that contains the shader, like myGameObject.renderer.material.distance=0.3; (I might be wrong on that syntax). You’d have to set it for every object individually based on distance, and force the object to be drawn before moving on to the next one.