首页 > 代码库 > 树叶婆娑 shader

树叶婆娑 shader

效果是树叶摇曳:

 1 Shader "LeafAnim" {
 2 Properties {
 3     _MainTex ("Base (RGB)", 2D) = "white" {}
 4     _Dist ("_Dist", float) = 0.04
 5     _Speed ("_Speed", float) = 0.2
 6 }
 7 
 8 SubShader {
 9     Tags {"IgnoreProjector"="True" "RenderType"="Transparent"}
10     LOD 350
11 
12 CGPROGRAM
13 #pragma surface surf Lambert vertex:vert  
14 
15 sampler2D _MainTex;
16 
17 struct Input {
18     half2 uv_MainTex;
19 };
20 
21 fixed _Dist;
22 fixed _Speed;
23 
24 struct appdata_t {
25     float4 vertex : POSITION;
26     float2 texcoord : TEXCOORD0;
27     float3 normal : NORMAL;
28 };
29 
30 void vert (inout appdata_t v)
31 {
32     float4 vertex = mul(v.vertex, _Object2World);
33     float a = vertex.x * vertex.z;//沿x和z轴距离作为偏离的一个依据,这样各个地方的叶子偏离的大小就不同,就“摇曳”起来了。
    //通过偏移顶点让叶子摇动起来
34 v.vertex.xyz += float3(1,0,1) * _Dist * sin(_Time.w * (_Speed)+a);//dist是整体偏离放大缩小倍数,用Time.w做循环,sin做距离约束 35 }   void surf (Input IN, inout SurfaceOutput o)
  { fixed4 c
= tex2D(_MainTex, IN.uv_MainTex); o.Albedo = c;   } 36 ENDCG 37 } 38 39 Fallback "Transparent/VertexLit" 40 }

效果肯定是gif才看得到,所以这里就不贴图了,但叶子摇曳的原理依据讲了,并且简单,读者一试便知。

树叶婆娑 shader