一个解决背景切换的shader

最近项目为了优化,节省资源,去掉了很多资源,例如该有的动画素材没有了,场景背景切换就比较麻烦。

最后想到用场景融合的方式去换场景,需要写个边缘alpha混合的shader,效果也不错。 运行的时候,无论左刷屏还是右刷屏 也不会有什么问题。

shader如下:


Shader "Custom/LeftAlpha" {
Properties 
{
_MainTex ("Base (RGB)", 2D) = "white" {}
}

SubShader 
{
Tags { "Queue" = "Transparent+5000" "IgnoreProjector" = "true" "RenderType"="Transparent" }
ZWrite Off Lighting Off Cull Off Fog {Mode Off} Blend SrcAlpha OneMinusSrcAlpha
//Blend DstColor One
LOD 110

Pass
{
CGPROGRAM
#pragma vertex vert_vct 
#pragma fragment frag_mult
//#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"

sampler2D _MainTex;
float4 _MainTex_ST;

struct vin_vct
{
float4 vertex : POSITION;
float4 color : COLOR;
float2 texcoord : TEXCOORD;
};

struct v2f_vct
{
float4 vertex : POSITION;
float4 color : COLOR;
float2 texcoord : TEXCOORD;
};

v2f_vct  vert_vct(vin_vct v)
{
v2f_vct o;
o.vertex = mul(UNITY_MATRIX_MVP,v.vertex);
o.color = v.color;
//o.texcoord = v.texcoord;
o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
return o;
}

fixed4 frag_mult(v2f_vct i):COLOR
{
fixed4 col = tex2D(_MainTex,i.texcoord);


float AlphaRitio = 0.2; //图片alpha融合的比例

if(_MainTex_ST.z < 0)   //如果反过来偏移
{
if(i.texcoord.x > (1 + _MainTex_ST.z - AlphaRitio))
{
col = col * (1 + _MainTex_ST.z - i.texcoord.x)* 1.0 / AlphaRitio;
}
}
else //正向偏移
{
if(i.texcoord.x < _MainTex_ST.z + AlphaRitio)
{
col = col * (i.texcoord.x - _MainTex_ST.z)* 1.0 / AlphaRitio;
}
}
return col;
}

ENDCG
}



}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值