Skip to content

Instantly share code, notes, and snippets.

@artm
Created June 25, 2011 09:49
Show Gist options
  • Save artm/1046323 to your computer and use it in GitHub Desktop.
Save artm/1046323 to your computer and use it in GitHub Desktop.
Manual Billboarding
// Shift a billboard corner size pixels along its corner
inline void Billboard(inout float4 pos, in float2 corner, float size)
{
pos.xy += (size * 2.0 / _ScreenParams.xy) * corner * pos.w;
}
// structures used to pass input output between shader stages
struct PointVIn {
float4 vertex : POSITION;
float4 texcoord : TEXCOORD;
float4 color : COLOR;
};
struct PointV2F {
float4 pos : POSITION;
float4 color : COLOR;
};
struct TexPointV2F {
float4 pos : POSITION;
float4 color : COLOR;
float4 texcoord : TEXCOORD;
};
Shader "Textured Point (additive)" {
Properties {
_Size("Point size", float) = 1.0
_MainTex ("Main texture", 2D) = "white" {}
_Dimmer("Dim factor", float) = 1.0
}
SubShader {
Tags { "Queue" = "Transparent" }
Pass {
Lighting Off
// don't write to zbuffer
ZWrite Off
Blend SrcColor One
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
#include "PointLib.cginc"
float _Size;
TexPointV2F vert (PointVIn v)
{
TexPointV2F o;
o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
// billboard...
Billboard( o.pos, v.texcoord.xy, _Size );
// pass the color along...
o.color = v.color;
o.texcoord = v.texcoord;
return o;
}
sampler2D _MainTex;
float _Dimmer;
half4 frag( TexPointV2F i ) : COLOR {
half4 o = i.color;
o.rgb *= tex2D( _MainTex, i.texcoord.xy ).a * _Dimmer;
return o;
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment