前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >UnityShader实例06:UV动画

UnityShader实例06:UV动画

作者头像
bering
发布2019-12-03 14:52:07
1.2K0
发布2019-12-03 14:52:07
举报
文章被收录于专栏:游戏开发之旅

UV动画

UV动画,顾名思义,就是针对UV做的动画。在游戏中,一些动态水面,飞流直下的瀑布,流动的岩浆,跳动的火焰等等,很多都是通过操作UV做的动画。在unity中我可以实用挂载脚本或者直接针对UV key动画帧做动画操作,而在本文中将通过shader编写实现三个比较常见的UV动画方式:

1.UV位移动画

2.UV序列帧动画

3.UV旋转动画

先从UV位移动画开始

我们将做一个流动岩浆的效果,在开始前,我们需要介绍下Unity内置变量_Time

float4 _Time : Time (t/20, t, t*2, t*3)

这是个随时间变化的增量,从函数的定义我们可以知道这个变量的4个分量速率比 _Time=20*_Time.x=_Time.y=_Time.z/2=_Time.w/3.

为了方便控制位移动画的速率和方向我们定义一个变量

[csharp] view plain copy

print?

  1. _ScrollingSpeed("Scrolling speed", Vector) = (0,0,0,0)

在顶点函数中将uv坐标乘以_Time变量和_ScrollingSpeed,下面为关键代码

[csharp] view plain copy

print?

  1. o.uvScroll = TRANSFORM_TEX((v.texcoord.xy+_Time.x*_ScrollingSpeed.xy), _Tex);// 这里只使用了xy上的两个分量对应uv

VF版本代码01:

[csharp] view plain copy

print?

  1. Shader "PengLu/Self-Illumin/IlluminDiffuseScrollVF"
  2. {
  3. Properties
  4. {
  5. _Color ("Main Color", Color) = (1,1,1,1)
  6. _MainTex ("Base (RGB)", 2D) = "white" {}
  7. _Illum ("Illumin (A)", 2D) = "white" {}
  8. _Tex("Scroll Tex (RGB)", 2D)= "white" {}
  9. _ScrollingSpeed("Scrolling speed", Vector) = (0,0,0,0)
  10. }
  11. SubShader
  12. {
  13. Tags { "RenderType"="Opaque" }
  14. LOD 200
  15. pass
  16. {
  17. CGPROGRAM
  18. #pragma vertex vert
  19. #pragma fragment frag
  20. #include "UnityCG.cginc"
  21. sampler2D _MainTex,_Illum,_Tex;
  22. float4 _MainTex_ST,_Illum_ST,_Tex_ST,_Color,_ScrollingSpeed;
  23. struct appdata {
  24. float4 vertex : POSITION;
  25. float2 texcoord : TEXCOORD0;
  26. float2 texcoord1 : TEXCOORD1;
  27. };
  28. struct v2f {
  29. float4 pos : POSITION;
  30. float2 uv_MainTex : TEXCOORD0;
  31. float2 uv_Illum : TEXCOORD1;
  32. float2 uvLM : TEXCOORD2;
  33. float2 uvScroll : TEXCOORD3;
  34. };
  35. v2f vert (appdata v)
  36. {
  37. v2f o;
  38. o.pos = mul(UNITY_MATRIX_MVP,v.vertex)*_Color;
  39. o.uv_MainTex = TRANSFORM_TEX(v.texcoord, _MainTex);
  40. o.uv_Illum = TRANSFORM_TEX(v.texcoord, _Illum);
  41. o.uvScroll = TRANSFORM_TEX((v.texcoord.xy+_Time.x*_ScrollingSpeed.xy), _Tex);
  42. o.uvLM = v.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
  43. return o;
  44. }
  45. float4 frag (v2f i) : COLOR
  46. {
  47. float4 texCol = tex2D(_MainTex, i.uv_MainTex);
  48. float4 IllumTex = tex2D(_Illum,i.uv_Illum);
  49. float3 lm = DecodeLightmap (UNITY_SAMPLE_TEX2D(unity_Lightmap, i.uvLM.xy));
  50. IllumTex.rgb+=tex2D(_Tex,i.uvScroll).rgb;
  51. IllumTex*=IllumTex.a;
  52. texCol+=IllumTex;
  53. texCol.rgb*=lm;
  54. return texCol;
  55. }
  56. ENDCG
  57. }
  58. }
  59. FallBack "Diffuse"
  60. }

VF版本代码01效果:

UV序列帧动画

序列帧动画是游戏比较常用的一种动画形式,在unity自带的粒子系统中就可以设置序列帧动画(下图),但是这个只能用于粒子系统的粒子效果,如果是自己做的模型就要使用序列帧动画就得自己写脚本或shader,下面我们将用shader实现序列帧动画效果。

首先得准备一张序列帧的贴图,如下图这样的:

然后我们需要声明三个变量:

[csharp] view plain copy

print?

  1. _SizeX ("SizeX", Float) = 4//列数
  2. _SizeY ("SizeY", Float) = 4//行数
  3. _Speed ("Speed", Float) = 200//动画的播放速度

在frag函数里面处理动画

[csharp] view plain copy

print?

  1. int indexX=fmod (_Time.x*_Speed,_SizeX);//获得列数的循环
  2. int indexY=fmod ((_Time.x*_Speed)/_SizeX,_SizeY);//获得行数的循环

fmod函数是取余函数,int用来强制取整,当然也可以用floor函数来取整

为了直观解释,将用下面的列表列出上面变量的值的对应关系,假设_SizeX=_SizeY=4

_Time.x*_Speed

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

indexX

0

1

2

3

0

1

2

3

0

1

2

3

0

1

2

3

indexY

0

0

0

0

1

1

1

1

2

2

2

2

3

3

3

3

_Time.x*_Speed

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

indexX

0

1

2

3

0

1

2

3

0

1

2

3

0

1

2

3

indexY

0

0

0

0

1

1

1

1

2

2

2

2

3

3

3

3

参考网络上的例子我还有写了另外一种获得循环动画的代码(我是孔乙己啦),和上面两句功能是一样的,貌似消耗稍微少一点

[csharp] view plain copy

print?

  1. int index = floor(_Time .x * _Speed);
  2. int indexY = index/_SizeX;
  3. int indexX = index - indexY*_SizeX;

通过列表对应,可以看出获取循环数的差别

index

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

indexX

0

1

2

3

0

1

2

3

0

1

2

3

0

1

2

3

indexY

0

0

0

0

1

1

1

1

2

2

2

2

3

3

3

3

index

16

17

18

19

20

21

22

23

24

25

25

27

28

29

30

31

indexX

0

1

2

3

0

1

2

3

0

1

2

3

0

1

2

3

indexY

4

4

4

4

5

5

5

5

6

6

6

6

7

7

7

7

接下来将获得的循环数与uv贴图的UV进行操作

[csharp] view plain copy

print?

  1. fixed2 seqUV = float2((i.texcoord.x) /_SizeX, (i.texcoord.y)/_SizeY);//将uv切分
  2. seqUV.x += indexX/_SizeX;//U方向上循环
  3. seqUV.y -= indexY/_SizeY;//V方向上循环

VF版本代码02:

[csharp] view plain copy

print?

  1. Shader "PengLu/Particle/SequenceAdd" {
  2. Properties {
  3. _TintColor ("Tint Color", Color) = (0.5,0.5,0.5,0.5)
  4. _MainTex ("Particle Texture", 2D) = "white" {}
  5. _InvFade ("Soft Particles Factor", Range(0.01,3.0)) = 1.0
  6. _SizeX ("SizeX", Float) = 4
  7. _SizeY ("SizeY", Float) = 4
  8. _Speed ("Speed", Float) = 200
  9. }
  10. Category {
  11. Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
  12. Blend SrcAlpha One
  13. AlphaTest Greater .01
  14. ColorMask RGB
  15. Cull Off Lighting Off ZWrite Off Fog { Color (0,0,0,0) }
  16. SubShader {
  17. Pass {
  18. CGPROGRAM
  19. #pragma vertex vert
  20. #pragma fragment frag
  21. #pragma multi_compile_particles
  22. #include "UnityCG.cginc"
  23. sampler2D _MainTex;
  24. fixed4 _TintColor;
  25. fixed _SizeX;
  26. fixed _SizeY;
  27. fixed _Speed;
  28. struct appdata_t {
  29. float4 vertex : POSITION;
  30. fixed4 color : COLOR;
  31. float2 texcoord : TEXCOORD0;
  32. };
  33. struct v2f {
  34. float4 vertex : POSITION;
  35. fixed4 color : COLOR;
  36. float2 texcoord : TEXCOORD0;
  37. #ifdef SOFTPARTICLES_ON
  38. float4 projPos : TEXCOORD1;
  39. #endif
  40. };
  41. float4 _MainTex_ST;
  42. v2f vert (appdata_t v)
  43. {
  44. v2f o;
  45. o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
  46. #ifdef SOFTPARTICLES_ON
  47. o.projPos = ComputeScreenPos (o.vertex);
  48. COMPUTE_EYEDEPTH(o.projPos.z);
  49. #endif
  50. o.color = v.color;
  51. o.texcoord = TRANSFORM_TEX(v.texcoord,_MainTex);
  52. return o;
  53. }
  54. sampler2D _CameraDepthTexture;
  55. float _InvFade;
  56. fixed4 frag (v2f i) : COLOR
  57. {
  58. #ifdef SOFTPARTICLES_ON
  59. float sceneZ = LinearEyeDepth (UNITY_SAMPLE_DEPTH(tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(i.projPos))));
  60. float partZ = i.projPos.z;
  61. float fade = saturate (_InvFade * (sceneZ-partZ));
  62. i.color.a *= fade;
  63. #endif
  64. int indexX=fmod (_Time.x*_Speed,_SizeX);
  65. int indexY=fmod ((_Time.x*_Speed)/_SizeX,_SizeY);
  66. // 以下三行代码和之前两行代码功能一样
  67. // int index = floor(_Time .x * _Speed);
  68. // int indexY = index/_SizeX;
  69. // int indexX = index-indexY*_SizeX;
  70. fixed2 seqUV = float2((i.texcoord.x) /_SizeX, (i.texcoord.y)/_SizeY);
  71. seqUV.x += indexX/_SizeX;
  72. seqUV.y -= indexY/_SizeY;
  73. return 2.0f * i.color * _TintColor * tex2D(_MainTex, seqUV);
  74. }
  75. ENDCG
  76. }
  77. }
  78. }
  79. }

VF版本代码02效果:

UV旋转动画

UV旋转动画在游戏开发中用得相对比较少,特效师一般会采用其他方式代替,这里将用shader实现一个UV旋转的动画。UV旋转实际上一个2D旋转,有关2D旋转的理论在这里。这边文章里讲得比较透彻,在这里我只需要拿到最终结果公式:

x' = r*cosα*cosθ - r*sinα*sinθ = x * cos θ – y * sin θ

y' = r*sinα*cosθ +r*cosα*sinθ = y * cos θ + x * sin θ

那么开始,同意需要声明一个变量来控制旋转方向和速度;

[csharp] view plain copy

print?

  1. _Speed ("Speed", Float) = 200

接下来就要在frag函数里面操作UV,关键代码如下:

[csharp] view plain copy

print?

  1. //将uv偏移0.5,使旋转中心到贴图中心
  2. float2 uv=i.texcoord-0.5;
  3. //定义一个二元变量,存储时间变量的正弦和余弦值
  4. float2 rotate = float2(cos(_Speed*_Time.x),sin(_Speed*_Time.x));
  5. //获得旋转后的uv坐标值
  6. uv=float2((uv.x*rotate.x-uv.y*rotate.y),(uv.x*rotate.y+uv.y*rotate.x));
  7. //将偏移的uv偏移回来
  8. uv+=0.5;

VF版本代码03

[csharp] view plain copy

print?

  1. Shader "PengLu/Particle/RotationAdd" {
  2. Properties {
  3. _TintColor ("Tint Color", Color) = (0.5,0.5,0.5,0.5)
  4. _MainTex ("Particle Texture", 2D) = "white" {}
  5. _InvFade ("Soft Particles Factor", Range(0.01,3.0)) = 1.0
  6. _Speed ("Speed", Float) = 200
  7. }
  8. Category {
  9. Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
  10. Blend SrcAlpha One
  11. AlphaTest Greater .01
  12. ColorMask RGB
  13. Cull Off Lighting Off ZWrite Off Fog { Color (0,0,0,0) }
  14. SubShader {
  15. Pass {
  16. CGPROGRAM
  17. #pragma vertex vert
  18. #pragma fragment frag
  19. #pragma multi_compile_particles
  20. #include "UnityCG.cginc"
  21. sampler2D _MainTex;
  22. fixed4 _TintColor;
  23. fixed _Speed;
  24. struct appdata_t {
  25. float4 vertex : POSITION;
  26. fixed4 color : COLOR;
  27. float2 texcoord : TEXCOORD0;
  28. };
  29. struct v2f {
  30. float4 vertex : POSITION;
  31. fixed4 color : COLOR;
  32. float2 texcoord : TEXCOORD0;
  33. #ifdef SOFTPARTICLES_ON
  34. float4 projPos : TEXCOORD1;
  35. #endif
  36. };
  37. float4 _MainTex_ST;
  38. v2f vert (appdata_t v)
  39. {
  40. v2f o;
  41. o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
  42. #ifdef SOFTPARTICLES_ON
  43. o.projPos = ComputeScreenPos (o.vertex);
  44. COMPUTE_EYEDEPTH(o.projPos.z);
  45. #endif
  46. o.color = v.color;
  47. o.texcoord = TRANSFORM_TEX(v.texcoord,_MainTex);
  48. return o;
  49. }
  50. sampler2D _CameraDepthTexture;
  51. float _InvFade;
  52. fixed4 frag (v2f i) : COLOR
  53. {
  54. #ifdef SOFTPARTICLES_ON
  55. float sceneZ = LinearEyeDepth (UNITY_SAMPLE_DEPTH(tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(i.projPos))));
  56. float partZ = i.projPos.z;
  57. float fade = saturate (_InvFade * (sceneZ-partZ));
  58. i.color.a *= fade;
  59. #endif
  60. float2 uv=i.texcoord-0.5;
  61. float2 rotate = float2(cos(_Speed*_Time.x),sin(_Speed*_Time.x));
  62. uv=float2((uv.x*rotate.x-uv.y*rotate.y),(uv.x*rotate.y+uv.y*rotate.x));
  63. uv+=0.5;
  64. return 2.0f * i.color * _TintColor * tex2D(_MainTex, uv);
  65. }
  66. ENDCG
  67. }
  68. }
  69. }
  70. }

VF版本代码03效果:

PS:贴图的Wrap Mode要设置成Clamp,原因你懂的 ^_^

原文链接:http://blog.csdn.net/u011047171/article/details/46776713

添加一个浮动的Shader实现:

代码语言:javascript
复制
Shader "Custom/WaveShader" {
	Properties{
		_WaveSpeed("WaveSpeed",float)=4
		_MaxHight("MaxHeight(峰值)",float)=3
		_Frenquncy("WaveF(频率)",float)=3
	}
		SubShader{
			Tags{ "Queue" = "Transparent" }
			Pass{
			CGPROGRAM
	#pragma vertex vert
	#pragma fragment frag
	#include "UnityCG.cginc"
			float _WaveSpeed;
		float _MaxHight;
		float _Frenquncy;
		struct a2v {
			fixed4 vertex : POSITION;

		};
		struct v2f {
			fixed4 pos : SV_POSITION;

		};
		v2f vert(a2v v)
		{
			v2f o;
			v.vertex.y += _MaxHight*sin(v.vertex.x*_Frenquncy + _Time.y*_WaveSpeed);
			o.pos = mul(UNITY_MATRIX_MVP,v.vertex);
			return o;
		}
		fixed4 frag(v2f v) :SV_Target
		{
			return fixed4(1,1,1,1);
		}
			ENDCG
		}
	}
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017/10/27 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
对象存储
对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档