您的位置:首页 > 移动开发 > Unity3D

一个解决背景切换的shader

2016-07-12 15:44 309 查看
最近项目为了优化,节省资源,去掉了很多资源,例如该有的动画素材没有了,场景背景切换就比较麻烦。

最后想到用场景融合的方式去换场景,需要写个边缘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
}


}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  unity3d shader