Fade out additive shader near camera


Say goodbye to flying through ugly clipped particle planes.

A modification of the standard additive shader that fades out near the camera.

This is the additive shader I used in Epic Proportions Space Game.I found the code somewhere in Unity Answers and made some small tweaks to make it work for me.

ParticleFade.zip (134 downloads)

Or just copy and paste from below.

Shader "Particles/ParticleFade" {
		_TintColor("Tint Color", Color) = (0.5,0.5,0.5,0.5)
		_MainTex("Particle Texture", 2D) = "white" {}
		_FadeDistance("Fade Start Distance", float) = 0.5

		Tags{ "Queue" = "Transparent" }
		Blend SrcAlpha One
		AlphaTest Greater .01
		ColorMask RGB
		Lighting Off
		Cull Off
		ZWrite Off
		Fog{ Color(0,0,0,0) }
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_builtin
#pragma fragmentoption ARB_fog_exp2
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"

		uniform float4    _MainTex_ST,
	uniform float _FadeDistance;

	struct appdata_vert {
		float4 vertex : POSITION;
		float4 texcoord : TEXCOORD0;
		float4 color : COLOR;

	uniform sampler2D _MainTex;

	struct v2f {
		float4 pos : SV_POSITION;
		float2 uv : TEXCOORD0;
		float4 color : COLOR;

	v2f vert(appdata_vert v) {
		v2f o;
		o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
		o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
		float4 viewPos = mul(UNITY_MATRIX_MV, v.vertex);
		float alpha = (-viewPos.z - _ProjectionParams.y) / _FadeDistance;
		o.color = float4(v.color.rgb, v.color.a*alpha);
		return o;

	float4 frag(v2f i) : COLOR{
		half4 texcol = tex2D(_MainTex, i.uv);

		return texcol*i.color;

		Fallback "Particles/Additive"

Leave a Reply

Your email address will not be published.