UnityShader—使用shader完成一个水面波动的效果

Shader "Custom/SinShader"  
{  
    Properties  
    {  
        _MainTex ("Texture", 2D) = "white" {}  
    }  
    SubShader  
    {  
        Tags { "RenderType"="Opaque" }  
        LOD 100  
  
        Pass  
        {  
            CGPROGRAM  
            #pragma vertex vert  
            #pragma fragment frag  
            #include "UnityCG.cginc"  
              
            // make fog work  
            #pragma multi_compile_fog  
            #include "UnityCG.cginc"  
  
           // 该结构体为传递给顶点shader所需的信息
            struct appdata  
            {  
                // 绑定什么语义,就会把什么上一个顶点的什么属性传过来   
                float4 vertex : POSITION;  
                float2 uv : TEXCOORD0;  
            };  
             // 该结构体为传递给片元shader所需的信息
            struct v2f  
            {  
                float2 uv : TEXCOORD0;  
                float4 vertex : POSITION ;  
            };  
  
            sampler2D _MainTex;  
            float4 _MainTex_ST;  
              
  
            v2f vert (appdata v)  
            {  
                v2f o;  
                // 计算当前的点和原点的距离
                float dist = distance(v.vertex.xyz, float3(0,0,0));   
                // 根据当前的点,加时间延时,作为一个符合sin变化的参数,将该值作为起伏的高度
                float height = sin(dist * 2 + _Time.z); 
                 // 把当前的点变换到世界座标中 
                o.vertex = mul(unity_ObjectToWorld, v.vertex);  
               // 更改世界座标中的y值,也就是高度值
                o.vertex.y = height;  
               // 将顶点的座标返回到局部座标
                o.vertex = mul(unity_WorldToObject,o.vertex);  
                // 将局部座标设置乘以MVP矩阵
                o.vertex = UnityObjectToClipPos(o.vertex);  
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);  
  
                return o;  
            }  
  
            fixed4 frag (v2f i) : SV_Target  
            {  
                // sample the texture  
                fixed4 col = tex2D(_MainTex, i.uv);  
                return col;  
            }  
            ENDCG  
        }  
    }  
}  

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章