Created
March 11, 2020 10:48
-
-
Save stilllisisi/6122bf006358d2ce85b42017cdea3670 to your computer and use it in GitHub Desktop.
【游戏-shader】Shader序列帧动画效果。序列帧动画属于纹理动画的其中之一,主要的实现原理是设置显示UV纹理的大小,并逐帧修改图片的UV坐标
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shader "Image Sequence Animation" { | |
Properties { | |
_Color ("Color Tint", Color) = (1, 1, 1, 1) | |
_MainTex ("Image Sequence", 2D) = "white" {} | |
_HorizontalAmount ("Horizontal Amount", Float) = 4 | |
_VerticalAmount ("Vertical Amount", Float) = 4 | |
_Speed ("Speed", Range(1, 100)) = 30 | |
} | |
SubShader { | |
// 设置成透明是希望画面特效能在其他物体都绘制完之后再显示 | |
// 忽略投影器 | |
// 渲染类型是透明 | |
Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"} | |
Pass { | |
Tags { "LightMode"="ForwardBase" } | |
// 半透明的处理要关闭深度写入 | |
ZWrite Off | |
// 使用透明度混合 | |
Blend SrcAlpha OneMinusSrcAlpha | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
#include "UnityCG.cginc" | |
fixed4 _Color; | |
sampler2D _MainTex; | |
float4 _MainTex_ST; | |
// 横向和纵向包含的图像个数 | |
float _HorizontalAmount; | |
float _VerticalAmount; | |
float _Speed; | |
struct a2v { | |
float4 vertex : POSITION; | |
float2 texcoord : TEXCOORD0; | |
}; | |
struct v2f { | |
float4 pos : SV_POSITION; | |
float2 uv : TEXCOORD0; | |
}; | |
v2f vert (a2v v) { | |
v2f o; | |
o.pos = mul(UNITY_MATRIX_MVP, v.vertex); | |
o.uv = TRANSFORM_TEX(v.texcoord, _MainTex); | |
return o; | |
} | |
fixed4 frag (v2f i) : SV_Target { | |
// _Time.y 场景加载后到现在所经历的时间 | |
// floor取整数 | |
float time = floor(_Time.y * _Speed); | |
// 行(商) | |
float row = floor(time / _HorizontalAmount); | |
// 列(余数) | |
float column = time - row * _HorizontalAmount; | |
// half2 uv = float2(i.uv.x /_HorizontalAmount, i.uv.y / _VerticalAmount); | |
// uv.x += column / _HorizontalAmount; | |
// uv.y -= row / _VerticalAmount; | |
half2 uv = i.uv + half2( column,-row); | |
// 每次只显示宽度的1/8和高度的1/8 | |
uv.x /= _HorizontalAmount; | |
uv.y /= _VerticalAmount; | |
fixed4 c = tex2D(_MainTex,uv); | |
c.rgb *= _Color; | |
return c; | |
} | |
ENDCG | |
} | |
} | |
FallBack "Transparent/VertexLit" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://gameinstitute.qq.com/community/detail/128632