AlphaClipsafe

From Unify Community Wiki
Jump to: navigation, search
Note how the objects fade out as they get close to the camera

Author: Daniel Brauer

Contents

Description

These versions of the built-in alpha blended and additive shaders fade out as vertices come within Fade Start Distance from the near clip plane, which needs to be set in the material. The result is a smooth transition to the near clipping plane rather than simply disappearing or being cut off.

Usage

Use for additive or alpha blended particles or convex geometry that are likely to be very close to the camera. Aside from fading when they get close to the camera, they will behave exactly like the built-in shader.

ShaderLab - AlphaBlendClipsafe.shader

Invalid language.

You need to specify a language like this: <source lang="html4strict">...</source>

Supported languages for syntax highlighting:

4cs, 6502acme, 6502kickass, 6502tasm, 68000devpac, abap, actionscript, actionscript3, ada, algol68, apache, applescript, apt_sources, asm, asp, autoconf, autohotkey, autoit, avisynth, awk, bascomavr, bash, basic4gl, bf, bibtex, blitzbasic, bnf, boo, c, c_loadrunner, c_mac, caddcl, cadlisp, cfdg, cfm, chaiscript, cil, clojure, cmake, cobol, coffeescript, cpp, cpp-qt, csharp, css, cuesheet, d, dcs, delphi, diff, div, dos, dot, e, ecmascript, eiffel, email, epc, erlang, euphoria, f1, falcon, fo, fortran, freebasic, fsharp, gambas, gdb, genero, genie, gettext, glsl, gml, gnuplot, go, groovy, gwbasic, haskell, hicest, hq9plus, html4strict, html5, icon, idl, ini, inno, intercal, io, j, java, java5, javascript, jquery, kixtart, klonec, klonecpp, latex, lb, lisp, llvm, locobasic, logtalk, lolcode, lotusformulas, lotusscript, lscript, lsl2, lua, m68k, magiksf, make, mapbasic, matlab, mirc, mmix, modula2, modula3, mpasm, mxml, mysql, newlisp, nsis, oberon2, objc, objeck, ocaml, ocaml-brief, oobas, oracle11, oracle8, oxygene, oz, pascal, pcre, per, perl, perl6, pf, php, php-brief, pic16, pike, pixelbender, pli, plsql, postgresql, povray, powerbuilder, powershell, proftpd, progress, prolog, properties, providex, purebasic, pycon, python, q, qbasic, rails, rebol, reg, robots, rpmspec, rsplus, ruby, sas, scala, scheme, scilab, sdlbasic, smalltalk, smarty, sql, systemverilog, tcl, teraterm, text, thinbasic, tsql, typoscript, unicon, uscript, vala, vb, vbnet, verilog, vhdl, vim, visualfoxpro, visualprolog, whitespace, whois, winbatch, xbasic, xml, xorg_conf, xpp, yaml, z80, zxbasic


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

	SubShader {
		Tags { "Queue" = "Transparent" }
		Blend SrcAlpha OneMinusSrcAlpha
		AlphaTest Greater .01
		ColorMask RGB
		Lighting Off
		ZWrite Off
		Fog { Color (0,0,0,0) }
		Pass {
			CGPROGRAM
				#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,
								_TintColor;
				uniform float _FadeDistance;
				
				struct appdata_vert {
					float4 vertex : POSITION;
					float4 texcoord : TEXCOORD0;
					float4 color : COLOR;
				};
				
				uniform sampler2D _MainTex;
				
				struct v2f {
					V2F_POS_FOG;
					float2	uv;
					float4 color;
				};
				
				v2f vert (appdata_vert v) {
					v2f o;
					PositionFog( v.vertex, o.pos, o.fog );
					o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
					float4 viewPos = mul(glstate.matrix.modelview[0], v.vertex);
					float alpha = (-viewPos.z - _ProjectionParams.y)/_FadeDistance;
					alpha = min(alpha, 1);
					o.color = float4(v.color.rgb, v.color.a*alpha);
					o.color *= _TintColor*2;
					return o;
				}
				
				float4 frag (v2f i) : COLOR {
					half4 texcol = tex2D( _MainTex, i.uv );
					
					return texcol*i.color;
				}
			ENDCG
		}
	}
	
	Fallback "Particles/Alpha Blended"
}

ShaderLab - AddClipsafe.shader

Invalid language.

You need to specify a language like this: <source lang="html4strict">...</source>

Supported languages for syntax highlighting:

4cs, 6502acme, 6502kickass, 6502tasm, 68000devpac, abap, actionscript, actionscript3, ada, algol68, apache, applescript, apt_sources, asm, asp, autoconf, autohotkey, autoit, avisynth, awk, bascomavr, bash, basic4gl, bf, bibtex, blitzbasic, bnf, boo, c, c_loadrunner, c_mac, caddcl, cadlisp, cfdg, cfm, chaiscript, cil, clojure, cmake, cobol, coffeescript, cpp, cpp-qt, csharp, css, cuesheet, d, dcs, delphi, diff, div, dos, dot, e, ecmascript, eiffel, email, epc, erlang, euphoria, f1, falcon, fo, fortran, freebasic, fsharp, gambas, gdb, genero, genie, gettext, glsl, gml, gnuplot, go, groovy, gwbasic, haskell, hicest, hq9plus, html4strict, html5, icon, idl, ini, inno, intercal, io, j, java, java5, javascript, jquery, kixtart, klonec, klonecpp, latex, lb, lisp, llvm, locobasic, logtalk, lolcode, lotusformulas, lotusscript, lscript, lsl2, lua, m68k, magiksf, make, mapbasic, matlab, mirc, mmix, modula2, modula3, mpasm, mxml, mysql, newlisp, nsis, oberon2, objc, objeck, ocaml, ocaml-brief, oobas, oracle11, oracle8, oxygene, oz, pascal, pcre, per, perl, perl6, pf, php, php-brief, pic16, pike, pixelbender, pli, plsql, postgresql, povray, powerbuilder, powershell, proftpd, progress, prolog, properties, providex, purebasic, pycon, python, q, qbasic, rails, rebol, reg, robots, rpmspec, rsplus, ruby, sas, scala, scheme, scilab, sdlbasic, smalltalk, smarty, sql, systemverilog, tcl, teraterm, text, thinbasic, tsql, typoscript, unicon, uscript, vala, vb, vbnet, verilog, vhdl, vim, visualfoxpro, visualprolog, whitespace, whois, winbatch, xbasic, xml, xorg_conf, xpp, yaml, z80, zxbasic


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

	SubShader {
		Tags { "Queue" = "Transparent" }
		Blend SrcAlpha One
		AlphaTest Greater .01
		ColorMask RGB
		Lighting Off
		ZWrite Off
		Fog { Color (0,0,0,0) }
		Pass {
			CGPROGRAM
				#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,
								_TintColor;
				uniform float _FadeDistance;
				
				struct appdata_vert {
					float4 vertex : POSITION;
					float4 texcoord : TEXCOORD0;
					float4 color : COLOR;
				};
				
				uniform sampler2D _MainTex;
				
				struct v2f {
					V2F_POS_FOG;
					float2	uv;
					float4 color;
				};
				
				v2f vert (appdata_vert v) {
					v2f o;
					PositionFog( v.vertex, o.pos, o.fog );
					o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
					float4 viewPos = mul(glstate.matrix.modelview[0], v.vertex);
					float alpha = (-viewPos.z - _ProjectionParams.y)/_FadeDistance;
					alpha = min(alpha, 1);
					o.color = float4(v.color.rgb, v.color.a*alpha);
					o.color *= _TintColor*2;
					return o;
				}
				
				float4 frag (v2f i) : COLOR {
					half4 texcol = tex2D( _MainTex, i.uv );
					
					return texcol*i.color;
				}
			ENDCG
		}
	}
	
	Fallback "Particles/Additive"
}

License

The MIT License (MIT)

Copyright (c) 2009 Daniel Brauer

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Tools