Unity Sharder帧动画
2015-11-14 14:41
190 查看
如果有看不懂的参数,请参看http://blog.csdn.net/u011244737/article/details/49819751
Shader "Custom/UVAnim_Shader"
{
Properties
{
_Color("Base Color", Color) = (1,1,1,1)
_MainTex("Base(RGB)", 2D) = "white" {}
}
SubShader
{
tags{"Queue" = "Transparent" "RenderType" = "Transparent" "IgnoreProjector" = "True"}
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
float4 _Color;
sampler2D _MainTex;
struct v2f
{
float4 pos:POSITION;
float2 uv:TEXCOORD0;
};
//封装的函数 实现帧移动
float2 moveUV(float2 vertUV)
{
//一共有几帧
float textureNum = 12.0;
//加速播放时间时使用
float timePerFrame = 100;
//frac 返回小数部分 随着时间增加而循环
float index = frac(_Time.x / textureNum * timePerFrame);
//初始化为第一帧
float2 uvScale = float2(1 / textureNum, 1);
if(index <= uvScale.x)
return vertUV * uvScale;
else if(index <= 2 * uvScale.x)
return vertUV * uvScale + float2(uvScale.x, 0.0);
else if(index <= 3 * uvScale.x)
return vertUV * uvScale + float2(2 * uvScale.x, 0.0);
else if(index <= 4 * uvScale.x)
return vertUV * uvScale + float2(3 * uvScale.x, 0.0);
else if(index <= 5 * uvScale.x)
return vertUV * uvScale + float2(4 * uvScale.x, 0.0);
else if(index <= 6 * uvScale.x)
return vertUV * uvScale + float2(5 * uvScale.x, 0.0);
else if(index <= 7 * uvScale.x)
return vertUV * uvScale + float2(6 * uvScale.x, 0.0);
else if(index <= 8 * uvScale.x)
return vertUV * uvScale + float2(7 * uvScale.x, 0.0);
else if(index <= 9 * uvScale.x)
return vertUV * uvScale + float2(8 * uvScale.x, 0.0);
else if(index <= 10 * uvScale.x)
return vertUV * uvScale + float2(9 * uvScale.x, 0.0);
else if(index <= 11 * uvScale.x)
return vertUV * uvScale + float2(10 * uvScale.x, 0.0);
else
return vertUV * uvScale + float2(11 * uvScale.x, 0.0);
}
v2f vert(appdata_base v)
{
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = moveUV(v.texcoord.xy);
//o.uv = v.texcoord.xy * float2(1.0 / 12.0, 1.0);
return o;
}
half4 frag(v2f i):COLOR
{
half4 c = tex2D(_MainTex , i.uv) * _Color;
return c;
}
ENDCG
}
}
}
Shader "Custom/UVAnim_Shader"
{
Properties
{
_Color("Base Color", Color) = (1,1,1,1)
_MainTex("Base(RGB)", 2D) = "white" {}
}
SubShader
{
tags{"Queue" = "Transparent" "RenderType" = "Transparent" "IgnoreProjector" = "True"}
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
float4 _Color;
sampler2D _MainTex;
struct v2f
{
float4 pos:POSITION;
float2 uv:TEXCOORD0;
};
//封装的函数 实现帧移动
float2 moveUV(float2 vertUV)
{
//一共有几帧
float textureNum = 12.0;
//加速播放时间时使用
float timePerFrame = 100;
//frac 返回小数部分 随着时间增加而循环
float index = frac(_Time.x / textureNum * timePerFrame);
//初始化为第一帧
float2 uvScale = float2(1 / textureNum, 1);
if(index <= uvScale.x)
return vertUV * uvScale;
else if(index <= 2 * uvScale.x)
return vertUV * uvScale + float2(uvScale.x, 0.0);
else if(index <= 3 * uvScale.x)
return vertUV * uvScale + float2(2 * uvScale.x, 0.0);
else if(index <= 4 * uvScale.x)
return vertUV * uvScale + float2(3 * uvScale.x, 0.0);
else if(index <= 5 * uvScale.x)
return vertUV * uvScale + float2(4 * uvScale.x, 0.0);
else if(index <= 6 * uvScale.x)
return vertUV * uvScale + float2(5 * uvScale.x, 0.0);
else if(index <= 7 * uvScale.x)
return vertUV * uvScale + float2(6 * uvScale.x, 0.0);
else if(index <= 8 * uvScale.x)
return vertUV * uvScale + float2(7 * uvScale.x, 0.0);
else if(index <= 9 * uvScale.x)
return vertUV * uvScale + float2(8 * uvScale.x, 0.0);
else if(index <= 10 * uvScale.x)
return vertUV * uvScale + float2(9 * uvScale.x, 0.0);
else if(index <= 11 * uvScale.x)
return vertUV * uvScale + float2(10 * uvScale.x, 0.0);
else
return vertUV * uvScale + float2(11 * uvScale.x, 0.0);
}
v2f vert(appdata_base v)
{
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = moveUV(v.texcoord.xy);
//o.uv = v.texcoord.xy * float2(1.0 / 12.0, 1.0);
return o;
}
half4 frag(v2f i):COLOR
{
half4 c = tex2D(_MainTex , i.uv) * _Color;
return c;
}
ENDCG
}
}
}
相关文章推荐
- Unity编译指令
- Unity里面的自动寻路(二)
- unity3d问题集 <1> FixedUpdate()与Update()的异同
- unity3d问题集 <3> 对Physics.Raycast和Ray(射线)的理解
- Unity Sharder墙面应用
- 【Unity开发】A星算法(怪物终点反复改变的解决办法)
- Unity用sharder实现重复贴图
- 【Unity实用小方法】开启游戏时播放一段动画
- Unity一个旋转的Sharder
- unity 3D 里的刚体属性
- [unity3d]屏幕坐标跟世界坐标的转换
- 一种Unity2D多分辨率屏幕适配方案
- Unity3D共享材质巧妙用法
- 全面理解Unity加载和内存管理
- Unity3d 鼠标拣选小功能集合
- 全面理解Unity加载和内存管理机制之二:进一步深入和细节
- Unity矩阵说明
- Unity3d调用android里的.so
- unity Sharder的格式与参数
- unity5 新布料系统 分析