Unity Shader 之 uv动画
2017-12-01 11:14
816 查看
Unity 动画
Unity Shader 内置时间变量
引入时间变量名称 | 类型 | 描述 |
---|---|---|
_Time | float4 | t是自该场景加载开始所经过的时间,4个分量分别是(t/20, t, 2t, 3t) |
_SinTime | float4 | t是时间的正弦值,(t/8, t/4, t/2, t) |
_CosTime | float4 | t是时间的余弦值,(t/8, t/4, t/2, t) |
unity_DeltaTime | float4 | dt是时间增量,(dt, 1/dt, smoothDt, 1/smoothDt) |
纹理动画
使用一张纹理实现的动画,用于代替复杂的粒子系统来模拟动画效果。u方向动画与v方向动画对比
序列帧动画
代码:
Shader "Unity Shaders Book/Chapter 11/Image Sequence Animation" { Properties { _Color ("Color Tint", Color) = (1, 1, 1, 1) _MainTex ("Image Sequence", 2D) = "white" {} _HorizontalAmount ("Horizontal Amount", Float) = 8 _VerticalAmount ("Vertical Amount", Float) = 8 _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.texcoord = v.texcoord.xy *_MainTex_ST.xy+_MainTex_ST.zw //将纹理坐标映射到顶点上以及zw偏移 o.uv = TRANSFORM_TEX(v.texcoord, _MainTex); return o; } fixed4 frag (v2f i) : SV_Target { float time = floor(_Time.y * _Speed); float row = floor(time / _HorizontalAmount); // /运算获取当前行 float column = time - row * _HorizontalAmount; // %运算获取当前列 //首先把原纹理坐标i.uv按行数和列数进行等分,然后使用当前的行列进行偏移 half2 uv = i.uv + half2(column, -row); uv.x /= _HorizontalAmount; uv.y /= _VerticalAmount; //纹理采样 fixed4 c = tex2D(_MainTex, uv); c.rgb *= _Color; return c; } ENDCG } } //FallBack "Transparent/VertexLit" }
效果:
滚动的背景
Shader "Unity Shaders Book/Chapter 11/Scrolling Background" { Properties { _MainTex ("Base Layer (RGB)", 2D) = "white" {} _DetailTex ("2nd Layer (RGB)", 2D) = "white" {} _ScrollX ("Base layer Scroll Speed", Float) = 1.0 _Scroll2X ("2nd layer Scroll Speed", Float) = 1.0 _Multiplier ("Layer Multiplier", Float) = 1 } SubShader { Tags { "RenderType"="Opaque" "Queue"="Geometry"} Pass { Tags { "LightMode"="ForwardBase" } CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" sampler2D _MainTex; sampler2D _DetailTex; float4 _MainTex_ST; float4 _DetailTex_ST; float _ScrollX; float _Scroll2X; float _Multiplier; struct a2v { float4 vertex : POSITION; float4 texcoord : TEXCOORD0; }; struct v2f { float4 pos : SV_POSITION; float4 uv : TEXCOORD0; }; v2f vert (a2v v) { v2f o; //将顶点坐标从模型空间转换到裁剪空间 o.pos = mul(UNITY_MATRIX_MVP, v.vertex); ////将纹理坐标映射到顶点上以及zw偏移,并用ScrollX对x轴坐标进行偏移 o.uv.xy = TRANSFORM_TEX(v.texcoord, _MainTex) + frac(float2(_ScrollX, 0.0) * _Time.y); o.uv.zw = TRANSFORM_TEX(v.texcoord, _DetailTex) + frac(float2(_Scroll2X, 0.0) * _Time.y); return o; } fixed4 frag (v2f i) : SV_Target { //纹理采样 fixed4 firstLayer = tex2D(_MainTex, i.uv.xy); fixed4 secondLayer = tex2D(_DetailTex, i.uv.zw); //纹理混合 fixed4 c = lerp(firstLayer, secondLayer, secondLayer.a); c.rgb *= _Multiplier; return c; } ENDCG } } FallBack "VertexLit" }
效果:
顶点动画
效果:Shader "Unity Shaders Book/Chapter 11/Water" { Properties { _MainTex ("Main Tex", 2D) = "white" {} _Color ("Color Tint", Color) = (1, 1, 1, 1) _Magnitude ("Distortion Magnitude", Float) = 1 _Frequency ("Distortion Frequency", Float) = 1 _InvWaveLength ("Distortion Inverse Wave Length", Float) = 10 _Speed ("Speed", Float) = 0.5 } SubShader { // Need to disable batching because of the vertex animation Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" "DisableBatching"="True"} Pass { Tags { "LightMode"="ForwardBase" } ZWrite Off Blend SrcAlpha OneMinusSrcAlpha Cull Off CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" sampler2D _MainTex; float4 _MainTex_ST; fixed4 _Color; float _Magnitude; float _Frequency; float _InvWaveLength; float _Speed; struct a2v { float4 vertex : POSITION; float4 texcoord : TEXCOORD0; }; struct v2f { float4 pos : SV_POSITION; float2 uv : TEXCOORD0; }; v2f vert(a2v v) { v2f o; //顶点偏移量,只对x偏移 float4 offset; offset.yzw = float3(0.0, 0.0, 0.0); //Frequency控制频率f //InvWaveLength控制波长L //Magnitude控制幅度k offset.x = sin(_Frequency * _Time.y + v.vertex.x * _InvWaveLength + v.vertex.y * _InvWaveLength + v.vertex.z * _InvWaveLength) * _Magnitude; o.pos = mul(UNITY_MATRIX_MVP, v.vertex + offset); //纹理采样 o.uv = TRANSFORM_TEX(v.texcoord, _MainTex); //对v偏移,横向 o.uv += float2(0.0, _Time.y * _Speed); return o; } fixed4 frag(v2f i) : SV_Target { fixed4 c = tex2D(_MainTex, i.uv); c.rgb *= _Color.rgb; return c; } ENDCG } } FallBack "Transparent/VertexLit" }
效果:
相关文章推荐
- Unity Shader 纹理采样UV动画 帧动画
- Unity Shader uv 动画
- UV动画:UV映射 + UV旋转 + UV偏移 实现 太空旅行、烟云内旋,旋涡效果
- Cg入门25: Fragment shader – UV动画(模糊)
- unity3d uv动画
- 使用Unity实现一个简单的UV动画
- UV-Sprite动画
- Unity Shader(一) Lowpoly动态低多边形 (QQ登录界面低边动画)
- UV动画
- Unity Shader 学习笔记(九) UV动画Shader实例
- Unity3D UV动画
- 【猫猫的Unity Shader之旅】之UV动画
- UV动画轻松实现 - 无限背景循环效果
- UV旋转动画Shader
- unity基础开发----unity中的UV动画实现代码
- Unity Shaders and Effects Cookbook (2-2) 修改 UV 坐标实现 帧动画
- Unity-Shader——用UV动画模拟模型在水下的场景
- uv切换动画实现-shader
- Unity3D手动实现UV动画教程
- 【原】UV动画