Skip to content

Instantly share code, notes, and snippets.

@whaison
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whaison/ca1a22bbfd0a6c804c45 to your computer and use it in GitHub Desktop.
Save whaison/ca1a22bbfd0a6c804c45 to your computer and use it in GitHub Desktop.
Shader "Sprites//CicleMask"
{
Properties
{
[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
_CenterPosX( "CenterPosX", Range(0.0,1.0)) = 0.5 // 円の中心となるX座標
_CenterPosY( "CenterPosY", Range(0.0,1.0)) = 0.5 // 円の中心となるY座標
_InnerRadius ("Inner Radius", Range (0.0, 1.0)) = 0.3 // 描画する範囲
_FadeRadius("Fade Radisu", Range(0.0,1.0)) = 0.05 // フェード範囲
_HWRate("Height / Width Rate", float) = 1 // テクスチャの縦/横の比率
}
SubShader
{
Tags
{
"Queue"="Transparent"
"IgnoreProjector"="True"
"RenderType"="Transparent"
"PreviewType"="Plane"
"CanUseSpriteAtlas"="True"
}
Cull Off
Lighting Off
ZWrite Off
Blend One OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile _ PIXELSNAP_ON
#include "UnityCG.cginc"
struct appdata_t
{
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
};
struct v2f
{
float4 vertex : SV_POSITION;
half2 texcoord : TEXCOORD0;
};
fixed4 _Color;
uniform float _CenterPosX;
uniform float _CenterPosY;
uniform float _InnerRadius;
uniform float _FadeRadius;
uniform float _HWRate;
v2f vert(appdata_t IN)
{
v2f OUT;
OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
OUT.texcoord = IN.texcoord;
return OUT;
}
sampler2D _MainTex;
fixed4 frag(v2f IN) : SV_Target
{
// メインテクスチャから指定された座標の色を取得
fixed4 c = tex2D(_MainTex, IN.texcoord);
// アルファが0なら描画しない
// うまく行かなかったのはこれが原因だと思われます
// 後の処理でアルファを上書きしているので、アルファが0の部分も表示されてしまっていたのだと思います。
if( c.a == 0 )
{
clip(-1);
}
// 座標を保存
float2 pos = IN.texcoord;
// 中心からの距離を補正
pos.y -= _CenterPosY;
pos.y *= _HWRate;
pos.y += _CenterPosY;
// 中心からの距離を算出
float dist = distance(pos, float2(_CenterPosX,_CenterPosY));
// 描画する範囲より離れているかチェック
if( dist >_InnerRadius )
{
// 描画しない
clip(-1);
}
// フェードさせる範囲内かチェック
else if( dist > _InnerRadius - _FadeRadius)
{
// 距離に応じてアルファを補正
c.a *= (dist - _InnerRadius) / -_FadeRadius;
}
c.rgb *= c.a;
return c;
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment