之前做过一个流光效果(http://www.cnblogs.com/jietian331/p/4748644.html)。
现将其改进一下,与NGUI结合起来,提供一个具有流光效果的组件:UIWalkLightSprite。
效果如图:
首先是此组件的源码,如下:
using System;
using UnityEngine; /// <summary>
/// 具有流光效果的 UISprite。
/// 注意:
/// 1. 流光图片需命名为"Special_WalkLight"(待改善)
/// 2. 流光的 UISprite 暂不支持 UIPanel 剪裁(待改善)
/// 3. 设置 UISprite 的 color 将无效
/// </summary>
public class UIWalkLightSprite : UISprite
{
const string WalkLightShaderName = "Bleach/Walk Light Colored"; float m_duration = 2f;
float m_widthLight2Sprite;
float m_timer;
float m_t1;
bool m_initedMat = true;
Vector2 m_lightURange; void Awake()
{
// replace shader
if (base.atlas.spriteMaterial.shader.name != WalkLightShaderName)
{
string tempAtlasName = string.Format("[WalkLightTempAtlas_{0}]", atlas.name);
Transform t = transform.Find(tempAtlasName);
if (t == null)
t = ((GameObject)GameObject.Instantiate(atlas.gameObject)).transform; t.gameObject.SetActive(false);
t.name = tempAtlasName;
t.parent = transform; var tempAtlas = t.GetComponent<UIAtlas>();
if (tempAtlas.spriteMaterial.name != WalkLightShaderName)
{
Material mat = (Material)GameObject.Instantiate(atlas.spriteMaterial);
mat.shader = Shader.Find(WalkLightShaderName);
tempAtlas.spriteMaterial = mat;
} base.atlas = tempAtlas;
}
} public override void OnFill(BetterList<Vector3> verts, BetterList<Vector2> uvs, BetterList<Color32> cols)
{
base.OnFill(verts, uvs, cols); // set uv2 to vertex
var lightSprite = atlas.GetSprite("Special_WalkLight");
Rect lightOuter = new Rect(lightSprite.x, lightSprite.y, lightSprite.width, lightSprite.height);
lightOuter = NGUIMath.ConvertToTexCoords(lightOuter, mainTexture.width, mainTexture.height); // 用 color 把流光图片的 uv 地址传到 shader 中,故会导致设置 color 无效
cols.Clear();
cols.Add(new Color(lightOuter.xMin, lightOuter.yMin, ));
cols.Add(new Color(lightOuter.xMin, lightOuter.yMax, ));
cols.Add(new Color(lightOuter.xMax, lightOuter.yMax, ));
cols.Add(new Color(lightOuter.xMax, lightOuter.yMin, )); // data set to shader
m_initedMat = false;
m_lightURange = new Vector2(lightOuter.xMin, lightOuter.xMax); Rect spriteOuter = new Rect(mSprite.x, mSprite.y, mSprite.width, mSprite.height);
spriteOuter = NGUIMath.ConvertToTexCoords(spriteOuter, mainTexture.width, mainTexture.height);
m_widthLight2Sprite = (lightOuter.width * spriteOuter.height) / (lightOuter.height * spriteOuter.width);
m_t1 = ( - m_widthLight2Sprite) * m_duration;
} protected override void OnUpdate()
{
base.OnUpdate(); if (RendererMat != null)
{
if (!m_initedMat)
{
m_initedMat = true; // init
RendererMat.SetFloat("_LightWidthToMain", m_widthLight2Sprite);
RendererMat.SetVector("_LightURange", m_lightURange);
} // 驱动流光动画
RendererMat.SetFloat("_TimeRate", m_timer / m_duration); m_timer += Time.deltaTime; if (m_timer > m_duration)
m_timer = ;
else if (m_timer > m_t1)
RendererMat.SetFloat("_ReachBoundary", );
else
RendererMat.SetFloat("_ReachBoundary", -);
}
} Material RendererMat
{
get { return base.Renderer != null ? base.Renderer.sharedMaterial : null; }
} public float Duration
{
set
{
if (value <= )
throw new ArgumentException("value <= 0"); m_duration = value;
m_t1 = ( - m_widthLight2Sprite) * m_duration;
}
}
}
UISprite
对应的 shader 如下:
Shader "Bleach/Walk Light Colored"
{
Properties
{
_MainTex ("RGB", 2D) = "black" {}
_AlphaTex ("Alpha", 2D) = "black" {}
} SubShader
{
LOD Tags
{
"Queue" = "Transparent"
"IgnoreProjector" = "True"
"RenderType" = "Transparent"
} Pass
{
Cull Off
Lighting Off
ZWrite Off
Fog { Mode Off }
Offset -, -
Blend SrcAlpha OneMinusSrcAlpha CGPROGRAM
#pragma vertex vert
#pragma fragment frag sampler2D _MainTex;
sampler2D _AlphaTex; uniform fixed2 _LightURange; // 光图片在 u 上的范围
uniform fixed _LightWidthToMain; // 光图片宽度占主图片宽度的比例
uniform fixed _ReachBoundary; // 小于0表示未到边界,大于0表示到达边界
uniform fixed _TimeRate; // 时间/周期 struct appdata_t
{
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
fixed4 color : COLOR;
}; struct v2f
{
float4 vertex : SV_POSITION;
half2 texcoord : TEXCOORD0;
half2 uv2 : TEXCOORD1; // 流光图的 uv 坐标
}; v2f vert (appdata_t v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.texcoord = v.texcoord;
fixed fixedU=(v.color.x - _LightURange.x) / (_LightURange.y - _LightURange.x); // 把 u 坐标转到[0,1]范围
o.uv2 = fixed2(fixedU,v.color.y);
return o;
} fixed4 frag (v2f IN) : COLOR
{
fixed4 main;
main.rgb = tex2D(_MainTex, IN.texcoord).rgb;
main.a = tex2D(_AlphaTex, IN.texcoord).a; // 流光
fixed x = _ReachBoundary > && IN.uv2.x < _LightWidthToMain ? IN.uv2.x+ : IN.uv2.x;
fixed lightU = (x - _TimeRate) / _LightWidthToMain;
lightU = (_LightURange.y - _LightURange.x) * lightU + _LightURange.x; // 把 u 坐标从[0,1]范围转回来
lightU = clamp(lightU, _LightURange.x, _LightURange.y);
fixed2 lightUV = fixed2(lightU, IN.uv2.y);
fixed lightA = tex2D(_AlphaTex, lightUV).a; // 融合
fixed3 col = main.rgb * ( + lightA * 1.5);
return fixed4(col, main.a);
}
ENDCG
}
} SubShader
{
LOD Tags
{
"Queue" = "Transparent"
"IgnoreProjector" = "True"
"RenderType" = "Transparent"
} Pass
{
Cull Off
Lighting Off
ZWrite Off
Fog { Mode Off }
Offset -, -
ColorMask RGB
Blend SrcAlpha OneMinusSrcAlpha
ColorMaterial AmbientAndDiffuse SetTexture [_MainTex]
{
Combine Texture * Primary
} SetTexture [_AlphaTex]
{
Combine previous, texture * primary
}
}
}
}
Shader