1. 程式人生 > >Shader學習筆記(四)實現積雪效果

Shader學習筆記(四)實現積雪效果

shader實現積雪效果

Shader "Custom/Snow" {
	Properties {
		_MainTex("Base",2D)="white"{}
		_Bump("Bump",2D)="bump"{}
		_Snow("Snow Level",Range(0,1))=0
		_SnowColor("Snow Color",Color)=(1.0,1.0,1.0,1.0)
		_SnowDirection("Snow Direction",Vector)=(0,1,0)
	}
	SubShader {
		Tags { "RenderType"="Opaque" }
		LOD 200
		
		CGPROGRAM
		// Physically based Standard lighting model, and enable shadows on all light types
		#pragma surface surf Lambert

		// Use shader model 3.0 target, to get nicer looking lighting
		#pragma target 3.0

		sampler2D _MainTex;

		struct Input {
			float2 uv_MainTex;
			float2 uv_Bump;
			float3 worldNormal;//Unity內建資料,如果表面shader為SurfaceOutput結構中的Normal賦值了,它會包含世界空間座標中法線的向量。
					  //通過呼叫WorldNormalVector(IN,o.Normal)得到此值。如果沒有為Normal賦值,直接呼叫IN.worldNormal得到此值
					 //這個方法把法線從:切空間→模型空間→世界空間,o.Normal是在切空間中的向量</span>
			INTERNAL_DATA<span style="color:#009900;">//通過在Input中新增INTERNAL_DATA,就可以訪問由法線貼圖修改後的表面法線。</span>
		};

		sampler2D _Bump;
		float _Snow;
		float4 _SnowColor;
		float4 _SnowDirection;

		void surf (Input IN, inout SurfaceOutput o) {
			// Albedo comes from a texture tinted by color
			fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
			o.Normal=UnpackNormal(tex2D(_Bump,IN.uv_Bump));//從法線貼圖中提取法線資訊,接受一個fixed4的輸入,並將其轉換為所對應的法線值(fixed3)

			if((dot(WorldNormalVector(IN,o.Normal),_SnowDirection.xyz))>lerp(1,-1,_Snow)){
			o.Albedo=_SnowColor.rgb;
			}else{
			o.Albedo=c.rgb;
			}
			o.Alpha = c.a;
		}
		ENDCG
	}
	FallBack "Diffuse"
}



inspector中的Tiling與Offset


Tiling是uv座標的縮放倍數

Offset是uv座標的起始位置

uv座標乘某上個數相當於修改Tiling

uv座標加上某個數相當於修改Offset