AlphaClipsafe

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Removing all content from page)
m (Reverted edits by Joseph05408 (Talk); changed back to last version by Danielbrauer)
Line 1: Line 1:
 +
[[Image:AlphaClipsafe.jpg|thumb|200px|Note how the objects fade out as they get close to the camera]]
  
 +
Author: [[User:Danielbrauer|Daniel Brauer]]
 +
 +
== 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 ==
 +
<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
 +
}
 +
 +
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>
 +
 +
== ShaderLab - AddClipsafe.shader ==
 +
<shaderlab>
 +
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"
 +
}
 +
</shaderlab>

Revision as of 19:29, 19 October 2009

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

<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 }

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>

ShaderLab - AddClipsafe.shader

<shaderlab> 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" } </shaderlab>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox