応用:Y座標特化した限定範囲でのTransition実装
2018/7/8
対象:Unity 2018.1.1f1
応用:Y座標特化した限定範囲でのTransition実装
確認が出来たので放置していたThresholdとの組み合わせを行う。
Threshold付近にだけルール画像を適用したいので、その範囲を指定するパラメータ_TransWidthをProperties追加する。
code:TransWidthを追加.shader
Properties
{
_BeforeTex ("Before Texture", 2D) = "red" {}
_AfterTex ("After Texture", 2D) = "white" {}
_RuleTex ("Rule Texture", 2D) = "gray" {}
_Threshold ("Threshold", Range(0, 1)) = 0.5
_TransWidth ("Transition Width", Range(0,1)) = 0.2
}
...
float _TransWidth;
v2f vert (appdata v){
...
考え方としては
_Thresholdを中心として_TransWidth幅の範囲外なら前画像、後画像どちらかの色をそのまま表示
_TransWidthの範囲内なら中心とした_TransWidth内でのuv.yを元にrule画像のy座標を計算して、対象の色を求めてlerpパラメータとして利用する。
ひとまずわかりやすくif文で記述する。
code:TranWidth内のみrule適用.shader
fixed4 frag (v2f i) : SV_Target
{
// sample the texture
//fixed4 col = (i.uv.y > _Threshold) ? tex2D(_BeforeTex, i.uv) : tex2D(_AfterTex, i.uv);
fixed4 col1 = tex2D(_BeforeTex, i.uv);
fixed4 col2 = tex2D(_AfterTex, i.uv);
fixed4 col;
float halfWidth = _TransWidth / 2;
float t1 = _Threshold - halfWidth;
float t2 = _Threshold + halfWidth;
if (i.uv.y > t1){
col = col1;
}
else if (i.uv.y < t2){
col = col2;
}
else{
float ruleY = (t2 - i.uv.y) / _TransWidth;
fixed4 col3 = tex2D(_RuleTex, float2(i.uv.x, ruleY));
col = lerp(col1, col2, col3.r);
}
// apply fog
UNITY_APPLY_FOG(i.fogCoord, col);
return col;
}
https://gyazo.com/a673c6203d35c6325ee7672673fd3fa5
ここで、Thresholdの範囲が0〜1だとTransWidthの幅の分、いきなり合成がかかってしまうのがわかったので範囲を-1〜2に変更する。
code:Threshold範囲変更.shader
Properties{
...
_Threshold ("Threshold", Range(-1, 2)) = 0.5
...
}
最終的にこれを全天球に適用すると冒頭の切り替えが可能となる。
https://gyazo.com/47abfb14f43087f3bd84979993047184
最終的なソースコードは以下になる
code:RuleTransition.shader
Shader "Unlit/RuleTransition"
{
Properties
{
_BeforeTex ("Before Texture", 2D) = "red" {}
_AfterTex ("After Texture", 2D) = "white" {}
_RuleTex ("Rule Texture", 2D) = "gray" {}
_Threshold ("Threshold", Range(-1, 2)) = 0.5
_TransWidth ("Transition Width", Range(0,1)) = 0.2
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
// make fog work
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
};
sampler2D _BeforeTex;
float4 _BeforeTex_ST;
sampler2D _AfterTex;
float4 _AfterTex_ST;
sampler2D _RuleTex;
float4 _RuleTex_ST;
float _Threshold;
float _TransWidth;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _BeforeTex);
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
// sample the texture
//fixed4 col = (i.uv.y > _Threshold) ? tex2D(_BeforeTex, i.uv) : tex2D(_AfterTex, i.uv);
fixed4 col1 = tex2D(_BeforeTex, i.uv);
fixed4 col2 = tex2D(_AfterTex, i.uv);
fixed4 col;
float halfWidth = _TransWidth / 2;
float t1 = _Threshold - halfWidth;
float t2 = _Threshold + halfWidth;
if (i.uv.y < t1){
col = col2;
}
else if (i.uv.y > t2){
col = col1;
}
else{
float ruleY = (t2 - i.uv.y) / _TransWidth;
fixed4 col3 = tex2D(_RuleTex, float2(i.uv.x, ruleY));
col = lerp(col1, col2, col3.r);
}
// apply fog
UNITY_APPLY_FOG(i.fogCoord, col);
return col;
}
ENDCG
}
}
}