(5.3) Example of Unity Vertex Fragment Shader in Computer Graphics

 The general flow of the GPU pipeline pipeline:

float4 fixed4 Time

1: float4 is a built-in vector (x, yz, w): tloat4 a; access individual members ax, ay, az, aw; 2: fixed4 is a built-in
vector (r, gb, a); fxed4 c; color.r, color .B, colorb, color.a;
3:float3 is the built-in vector (x,y,2);
4:fixed3 is the built-in vector (r,g,b);
5:float2 is the built-in vector (x,w);
_Time: the elapsed time from the beginning of the white field loading, the four components are (t/20,t, t*2, t*3); SinTime: t is the sine value of time, the four components are (
t /8,t/4,t/2,t;
8: CosTime:t is the cosine value of time, and
the 4 components are (t/8,t/4, t/2,t);
9: unity DeltaTime: dt is the time increment, the value of 4 components (dt, 1/dt, smoothDt,
1/smoothDt), smoothing the time, preventing the time interval from fluctuating too much; 

There is no debugging and no log when writing Shader. You can only rely on carefulness and write slowly bit by bit. Start writing from the simple, and slowly debug in depth. You must be clear about the usage of functions and structures in the shader. Especially when using Unity's built-in structure, if there is a problem, be sure to check the cg file that comes with Unity to see if there is an error in the use), cg file directory: Unity --> Edit --> Data --> CGlncludes --> UnityCG .ceinc file.

sine wave

water surface sine wave

Shader "GFStudy/sinShader"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

			//  v2f 就是 vert 传给 frag
            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : POSITION;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;

            v2f vert (appdata v)
            {
                v2f o;
                // 有了点的距离
                float dist = distance(v.vertex,float3(0,0,0));
                // 计算高度
                float h = sin(dist*2+_Time.z)/5;// _Time.z 是过去时间的两倍 (随着时间 越来越大)
                // dist*2 使波峰在多一点
                // /5 让高度在平缓一点

                // 把顶点坐标转换到世界坐标
                o.vertex = mul(unity_ObjectToWorld,v.vertex);
                // 让世界坐标的y 等于h  ,这样h改了世界坐标就改了
                o.vertex.y = h;
                // 再把世界坐标转回到顶点坐标
                o.vertex = mul(unity_WorldToObject,o.vertex);
                // 再把坐标转换到透视坐标
                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
        }
    }
}

uv animation- water ripple

water ripple basemap

Unity-uvShader, body water ripple overlay

Shader "GFStudy/shuibowenuv"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _SubTex ("Texture", 2D) = "white" {}
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            sampler2D _SubTex;
            sampler2D _MainTex;
            float4 _MainTex_ST;

            // 贴图寻址
            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                return o;
            }

            // 开始着色
            fixed4 frag (v2f i) : SV_Target
            {
                // uv偏移值 
                float2 uv_offset = float2(0,0);
                uv_offset.x = _Time.y*0.24; // 这里_Time.y 使用的是_Time的第二个分量, 4个分量分别是 (t/20,t, t*2, t*3),对应的(x,y,z,w);
                uv_offset.y = _Time.y*0.24; // 时间 乘以 一个速度 就能实现 不断的修改uv坐标
                // sample the texture
                fixed4 linght_color = tex2D(_SubTex,i.uv+uv_offset); // 一直贴uv坐标 就会动起来了
                fixed4 col = tex2D(_MainTex, i.uv)+linght_color; // 注意这里用的时颜色的加法,所有水波纹的贴图要是黑底加波纹的形式
                // 因为黑色的颜色值是0 ,加处理后,还是原来的颜色,这样就只有波纹加处理了,波纹就到物体上了(产生叠加了)

                return col;
            }
            ENDCG
        }
    }
}

Guess you like

Origin blog.csdn.net/u013774978/article/details/130095757