Fragment shader---UV動畫3



1、對UV進行多次采樣,每次都對U或V進行稍微的偏轉,然后再做一個疊加


Shader "Custom/s_mohu" {


	Properties{
		_MainTex("MainTex", 2D) = ""{}
		_F("F",range(1,30)) = 10
		_A("A",range(0,0.1)) = 0.01
		_R("R",range(0,1)) = 0
	}

	SubShader{
		pass {
		CGPROGRAM
		#pragma vertex vert
		#pragma fragment frag
		#include "unitycg.cginc"


		float tiling_x;
		float tiling_y;
		float offset_x;
		float offset_y;

		sampler2D _MainTex;
		//unity總是會幫我們傳遞這樣一個值
		float4 _MainTex_ST;
		float _F;
		float _A;
		float _R;

		struct v2f {
			float4 pos:POSITION;
			float2 uv:TEXCOORD0;
		};


		v2f vert(appdata_base v) {
			v2f o;
			o.pos = mul(UNITY_MATRIX_MVP,v.vertex);
			o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
			return o;
		}

		fixed4 frag(v2f IN) :COLOR{
			float offset_uv = 0.01;

			float2 uv = IN.uv;
			fixed4 color = tex2D(_MainTex, uv);
			//采樣后稍微偏移
			uv = IN.uv.x + offset_uv;
			//采樣疊加
			color.rgb += tex2D(_MainTex,uv);
			uv = IN.uv.x - offset_uv;
			color.rgb += tex2D(_MainTex, uv);

			uv = IN.uv.y + offset_uv;
			//采樣疊加
			color.rgb += tex2D(_MainTex, uv);
			uv = IN.uv.y - offset_uv;
			color.rgb += tex2D(_MainTex, uv);


			//疊加后顏色加深,所以除以總的疊加次數
			color.rgb /= 5;

			return color;	
		}
		ENDCG
	}
	}
}



多次采樣后的結果:




2、通過導數的形式進行模糊


Shader "Custom/s_mohu" {


	Properties{
		_MainTex("MainTex", 2D) = ""{}
		_F("F",range(1,30)) = 10
		_A("A",range(0,0.1)) = 0.01
		_R("R",range(0,1)) = 0
	}

	SubShader{
		pass {
		CGPROGRAM
		#pragma vertex vert
		#pragma fragment frag
		#include "unitycg.cginc"
		#pragma target 3.0


		float tiling_x;
		float tiling_y;
		float offset_x;
		float offset_y;

		sampler2D _MainTex;
		//unity總是會幫我們傳遞這樣一個值
		float4 _MainTex_ST;
		float _F;
		float _A;
		float _R;

		struct v2f {
			float4 pos:POSITION;
			float2 uv:TEXCOORD0;
		};


		v2f vert(appdata_base v) {
			v2f o;
			o.pos = mul(UNITY_MATRIX_MVP,v.vertex);
			o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
			return o;
		}

		fixed4 frag(v2f IN) :COLOR{
			fixed4 color = tex2D(_MainTex,IN.uv,float2(0.01,0.01),float2(0.01,0.01));
			return color;	
		}
		ENDCG
	}
	}
}




3、使用ddx ddy導數進行偏導

Shader "Custom/s_mohu" {


	Properties{
		_MainTex("MainTex", 2D) = ""{}
		_F("F",range(1,30)) = 10
		_A("A",range(0,0.1)) = 0.01
		_R("R",range(0,1)) = 0
	}

	SubShader{
		pass {
		CGPROGRAM
		#pragma vertex vert
		#pragma fragment frag
		#include "unitycg.cginc"
		#pragma target 3.0


		float tiling_x;
		float tiling_y;
		float offset_x;
		float offset_y;

		sampler2D _MainTex;
		//unity總是會幫我們傳遞這樣一個值
		float4 _MainTex_ST;
		float _F;
		float _A;
		float _R;

		struct v2f {
			float4 pos:POSITION;
			float2 uv:TEXCOORD0;
		};


		v2f vert(appdata_base v) {
			v2f o;
			o.pos = mul(UNITY_MATRIX_MVP,v.vertex);
			o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
			return o;
		}

		fixed4 frag(v2f IN) :COLOR{
			float dx = ddx(IN.uv.x)*10;
			float2 dsdx = float2(dx,dx);

			float dy = ddx(IN.uv.y) * 10;
			float2 dsdy = float2(dy, dy);
			//ddx /ddy
			fixed4 color = tex2D(_MainTex,IN.uv,dsdx,dsdy);
			return color;	
		}
		ENDCG
	}
	}
}



4、當轉到正面的時候很清楚,轉到其它面的時候很模糊


Shader "Custom/s_mohu" {


	Properties{
		_MainTex("MainTex", 2D) = ""{}
		_F("F",range(1,30)) = 10
		_A("A",range(0,0.1)) = 0.01
		_R("R",range(0,1)) = 0
	}

	SubShader{
		pass {
		CGPROGRAM
		#pragma vertex vert
		#pragma fragment frag
		#include "unitycg.cginc"
		#pragma target 3.0


		float tiling_x;
		float tiling_y;
		float offset_x;
		float offset_y;

		sampler2D _MainTex;
		//unity總是會幫我們傳遞這樣一個值
		float4 _MainTex_ST;
		float _F;
		float _A;
		float _R;

		struct v2f {
			float4 pos:POSITION;
			float2 uv:TEXCOORD0;
			float z : TEXCOORD1;
		};


		v2f vert(appdata_base v) {
			v2f o;
			o.pos = mul(UNITY_MATRIX_MVP,v.vertex);
			o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
			o.z = mul(_Object2World,v.vertex).z;
			return o;
		}

		fixed4 frag(v2f IN) :COLOR{
			float2 dsdx=ddx(IN.z)*10;
			float2 dsdy = ddy(IN.z)*10;
			fixed4 color = tex2D(_MainTex,IN.uv,dsdx,dsdy);
			return color;	
		}
		ENDCG
	}
	}
}

正面:清晰



稍微旋轉:模糊






注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
  © 2014-2022 ITdaan.com