UnityShader片段着色器之河流、旋轉、outLine效果 筆記

紋理流動

1.讓紋理動起來

 

什麼是UV?

貼圖影射到模型表面的對應數據。

完整是UVW(相當xyz)。U和V分別是圖片在屏幕水平、垂直方向上的座標,取值一般都是0~1

W深度。(3D貼圖用到)

 

wrap Mode

Clamp 表示UV座標超過1,永遠取1,比0小永遠是0;

Repeat:UV 超過1 ,取小數部分 和重複(0-1);

 fixed4 frag (v2f i) : SV_Target
            {
				float2 tmpUV=i.uv;
				tmpUV.x+=_Time.x;
				//temUV.y+=_Timey;

                fixed4 col = tex2D(_MainTex, tmpUV);
				//_MainTex:一張圖片
				//i.uv:表示比例 UV座標(0—1);
                // just invert the colors
                ///col.rgb = 1 - col.rgb;//反色效果

				//col.x*=99; 使紋理變紅

				
                return col;
            }
      

 

紋理旋轉

Loding 做法:

Cpu:

1.多個圖片切換

GPU

1.紋理旋轉:Uv旋轉起來

2.UV用矩陣的方式旋轉

 

實現思路

1.物體平移到原點。(使UV旋轉軸心在中心)

2.實現旋轉

3.物體平移到原來的位置

     sampler2D _MainTex;
			float _Speed;
            fixed4 frag (v2f i) : SV_Target
            {
				float2 tmpUV=i.uv;
				tmpUV-=float2(0.5,0.5);//平移軸心
				if(length(tmpUV)>0.5)
				{
					return fixed4(0,0,0,0);
				}
				float2 finalUV=0;
				float angle=_Time.x*_Speed;
				finalUV.x=tmpUV.x*cos(angle)-tmpUV.y*sin(angle);
				finalUV.y=tmpUV.x*sin(angle)+tmpUV.y*cos(angle);
				
				finalUV+=float2(0.5,0.5);//平移回去
                fixed4 col = tex2D(_MainTex,finalUV);

				
                // just invert the colors
               // col.rgb = 1 - col.rgb;
                return col;
            }
            ENDCG

 

OutLine效果

輪廓:

1.直接渲染兩個物體

一個大,一個小。大的就是輪廓

一個Pass{}就是渲染一遍;

2.渲染一個物體

1)找到邊緣

2)着色

3)非邊緣地帶正常紋理採樣

Shader "Hidden/OutLine"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
		_OutLineWidth("width",float)=1.2
    }
    SubShader
    {
        // No culling or depth
       // Cull Off ZWrite Off ZTest Always
	     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;
            };
			float _OutLineWidth;
            v2f vert (appdata v)
            {
                v2f o;
				v.vertex.xy*=_OutLineWidth;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                return o;
            }

            sampler2D _MainTex;

            fixed4 frag (v2f i) : SV_Target
            {
                fixed4 col = tex2D(_MainTex, i.uv);
                // just invert the colors
               // col.rgb = 1 - col.rgb;
			   col=fixed4(1,1,0,0);
                return col;
            }
            ENDCG
        }
        Pass
        {	ZTest Always//因爲渲染的兩個物體深度度一樣,所
            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;
            };



			
            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                return o;
            }

            sampler2D _MainTex;

            fixed4 frag (v2f i) : SV_Target
            {
                fixed4 col = tex2D(_MainTex, i.uv);
                // just invert the colors
               // col.rgb = 1 - col.rgb;
                return col;
            }
            ENDCG
        }

		
      
    }

 

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