-->


Shader "UI/XAxisWave"
{
Properties
{
//使用する波形をトグルとして表示、実際はfloat値の0か1が保存される
[Toggle] _UseSin1("UseSin1", float) = 1
[Toggle] _UseSin2("UseSin2", float) = 1
[Toggle] _UseCos1("UseCos1", float) = 1
[Toggle] _UseCos2("UseCos2", float) = 1
[Space(10)]
_Twist ("Twist", Range(0, 100)) = 0 //波形の細かさ
_SinWave("SinWave", Range(1, 50)) = 5 //サイン波の波形の振れ幅の抑制値
_CosWave("CosWave", Range(1, 50)) = 10 //コサイン波の波形の振れ幅の抑制値
}
SubShader
{
Tags
{
"Queue" = "Transparent"
"RenderType"="Transparent"
}
Cull Off
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
GrabPass{}
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _GrabTexture;
float _UseSin1;
float _UseSin2;
float _UseCos1;
float _UseCos2;
float _Twist;
float _SinWave;
float _CosWave;
struct appdata
{
fixed4 vertex : POSITION;
fixed4 uv : TEXCOORD0;
};
struct v2f
{
fixed4 vertex : SV_POSITION;
fixed2 uv : TEXCOORD0;
};
v2f vert(appdata i)
{
v2f o;
o.vertex = UnityObjectToClipPos(i.vertex);
o.uv = ComputeGrabScreenPos(o.vertex);
return o;
}
//プラスのサイン波を使ってy軸座標からx軸の修正値を返す
fixed2 sinUv1(v2f i)
{
fixed twist = sin(i.uv.y * _Twist) / _SinWave;
fixed resultX = i.uv.x + twist;
fixed2 fixUv = fixed2(resultX , i.uv.y);
return fixUv;
}
//マイナスのサイン波を使ってy軸座標からx軸の修正値を返す
fixed2 sinUv2(v2f i)
{
fixed twist = -sin(i.uv.y * _Twist) / _SinWave;
fixed resultX = i.uv.x + twist;
fixed2 fixUv = fixed2(resultX , i.uv.y);
return fixUv;
}
//プラスのコサイン波を使ってy軸座標からx軸の修正値を返す
fixed2 cosUv1(v2f i)
{
fixed twist = cos(i.uv.y * _Twist) / _CosWave - (1 / _CosWave);
fixed resultX = i.uv.x + twist;
fixed2 fixUv = fixed2(resultX , i.uv.y);
return fixUv;
}
//マイナスのコサイン波を使ってy軸座標からx軸の修正値を返す
fixed2 cosUv2(v2f i)
{
fixed twist = -cos(i.uv.y * _Twist) / _CosWave + (1 / _CosWave);
fixed resultX = i.uv.x + twist;
fixed2 fixUv = fixed2(resultX , i.uv.y);
return fixUv;
}
fixed4 frag(v2f i) : SV_Target
{
//4つの波形のx軸修正値を取得、ただし対応する_Use***が0の場合0を取得
fixed4 col1 = tex2D(_GrabTexture, sinUv1(i)) * _UseSin1;
fixed4 col2 = tex2D(_GrabTexture, sinUv2(i)) * _UseSin2;
fixed4 col3 = tex2D(_GrabTexture, cosUv1(i)) * _UseCos1;
fixed4 col4 = tex2D(_GrabTexture, cosUv2(i)) * _UseCos2;
//それぞれの波系の合成率を取得、2種類使ってれば0.5を、4種類使ってれば0.25を返す
fixed division = 1 / (_UseSin1 + _UseSin2 + _UseCos1 + _UseCos2);
//それぞれの波形に合成率を掛ける
col1 = col1 * division;
col2 = col2 * division;
col3 = col3 * division;
col4 = col4 * division;
//波形を合成して返す
return col1 + col2 + col3 + col4;
}
ENDCG
}
}
}




//プラスのサイン波を使ってy軸座標からx軸の修正値を返す
fixed2 sinUv1(v2f i)
{
fixed twist = sin(i.uv.y * _Twist) / _SinWave;
fixed resultX = i.uv.x + twist;
fixed2 fixUv = fixed2(resultX , i.uv.y);
return fixUv;
}
//マイナスのサイン波を使ってy軸座標からx軸の修正値を返す
fixed2 sinUv2(v2f i)
{
fixed twist = -sin(i.uv.y * _Twist) / _SinWave;
fixed resultX = i.uv.x + twist;
fixed2 fixUv = fixed2(resultX , i.uv.y);
return fixUv;
}
//プラスのコサイン波を使ってy軸座標からx軸の修正値を返す
fixed2 cosUv1(v2f i)
{
fixed twist = cos(i.uv.y * _Twist) / _CosWave - (1 / _CosWave);
fixed resultX = i.uv.x + twist;
fixed2 fixUv = fixed2(resultX , i.uv.y);
return fixUv;
}
//マイナスのコサイン波を使ってy軸座標からx軸の修正値を返す
fixed2 cosUv2(v2f i)
{
fixed twist = -cos(i.uv.y * _Twist) / _CosWave + (1 / _CosWave);
fixed resultX = i.uv.x + twist;
fixed2 fixUv = fixed2(resultX , i.uv.y);
return fixUv;
}