AlphaClipsafe

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m (ShaderLab - AlphaSelfIllum.shader: Added Tint, which was declared but ignored before.)
(ShaderLab - AlphaSelfIllum.shader: Fixed alpha clamping)
Line 22: Line 22:
 
Tags { "Queue" = "Transparent" }
 
Tags { "Queue" = "Transparent" }
 
Blend SrcAlpha OneMinusSrcAlpha
 
Blend SrcAlpha OneMinusSrcAlpha
 +
AlphaTest Greater .01
 
ColorMask RGB
 
ColorMask RGB
 
Lighting Off
 
Lighting Off
 
ZWrite Off
 
ZWrite Off
Fog { Color (0,0,0,1) }
+
Fog { Color (0,0,0,0) }
 
 
 
SubShader {
 
SubShader {
Line 37: Line 38:
 
#include "UnityCG.cginc"
 
#include "UnityCG.cginc"
 
 
uniform float4 _MainTex_ST;
+
uniform float4 _MainTex_ST,
uniform float4 _TintColor;
+
_TintColor;
 
uniform float _NearClip,
 
uniform float _NearClip,
 
_FadeDistance;
 
_FadeDistance;
Line 62: Line 63:
 
float4 viewPos = mul(glstate.matrix.modelview[0], v.vertex);
 
float4 viewPos = mul(glstate.matrix.modelview[0], v.vertex);
 
float alpha = (-viewPos.z - _NearClip)/_FadeDistance;
 
float alpha = (-viewPos.z - _NearClip)/_FadeDistance;
clamp(alpha, 0, 1);
+
alpha = clamp(alpha, -10000000, 1);
 
o.color = float4(v.color.rgb, v.color.a*alpha);
 
o.color = float4(v.color.rgb, v.color.a*alpha);
o.color *= _TintColor;
+
o.color *= _TintColor*2;
 
return o;
 
return o;
 
}
 
}

Revision as of 12:59, 30 June 2009

Note how the objects fade out as they get close to the camera

Author: Daniel Brauer

Description

This shader fades out as vertices move from Fade Start Distance to Near Clip Distance, both of which need to be set in the material.

Usage

Good for particles or convex geometry that are likely to be very close to the camera.

ShaderLab - AlphaSelfIllum.shader

<shaderlab> 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 _NearClip ("Near Clip Distance", float) = 0.3 }

Category { Tags { "Queue" = "Transparent" } Blend SrcAlpha OneMinusSrcAlpha AlphaTest Greater .01 ColorMask RGB Lighting Off ZWrite Off Fog { Color (0,0,0,0) }

SubShader { 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 _NearClip, _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 - _NearClip)/_FadeDistance; alpha = clamp(alpha, -10000000, 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 } } } } </shaderlab>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox