一個簡單的邊緣發光的例子(vf 語法)

 

Shader "Custom/BasicRimShader001"
{
	Properties{
		//主顏色
		_MainColor("MainColor",color) = (0.5,0.5,0.5,1)

		//漫反射紋理
		_TextureDiffuse("Texture Diffuse",2D) = "white" {}

		//邊緣發光的顏色
		_RimColor("Rim color:",color) = (0.2,1,1,1)

		//邊緣光的強度
		_RimColorPower("RimColor Power",Range(0.0,36.0)) = 0.1

		//邊緣發光強度係數
		_RimIntensity("Rim Intensity",Range(0.0,100.0)) = 3
	}
	SubShader{
		//渲染類型爲Opaque,不透明 || RenderType Opaque
		Tags
		{
			"RenderType" = "Opaque"
		}

		pass{

			//設置通道名稱 
			Name "ForwardBase"

			//設置光照的模式
			Tags{
				"LightMode" = "ForwardBase"
			}

			CGPROGRAM


			#pragma vertex vert
			#pragma fragment frag

			//包含的頭文件
			#include "AutoLight.cginc"
			#include "UnityCG.cginc"

			 float4 _MainColor;
			 float4 _LightColor0;
			 float4 _RimColor;
			 float4 _TextureDiffuse_ST;
			 float _RimColorPower;
			 float _RimIntensity; 
			 sampler2D _TextureDiffuse;

			//定數輸入的結構
			struct VertexInput{
				float4 vertex : POSITION;
				float3 normal : NORMAL;
				float4 texcoord : TEXCOORD0; //設置一級紋理信息
			};

			//設置頂點輸出的結構
			struct VertexOutput{
				//像素位置 
				float4 pos:SV_POSITION;

				//一級紋理
				float4 texcoord : TEXCOORD0;

				//法向量
				float3 normal:NORMAL;

				//世界空間的座標
				float4 worldPos : TEXCOORD1;

				//創制光源座標,用於內置的光照
				LIGHTING_COORDS(3,4)
			};

			//頂點着色氣
			VertexOutput vert(VertexInput Input){

				VertexOutput OutPut;

				OutPut.pos = mul(UNITY_MATRIX_MVP,Input.vertex);
				OutPut.texcoord = Input.texcoord;

				//法線要在世界座標系的位置
				OutPut.normal = mul(float4(Input.normal,1.0), unity_WorldToObject) .xyz;

				//獲取頂點在世界座標中的位置
				OutPut.worldPos = mul(unity_ObjectToWorld,Input.vertex);

				return OutPut;
			}

			//片段着色氣
			float4 frag(VertexOutput i) : COLOR{
				//視角的方向
				float3 ViewDirection = normalize(_WorldSpaceCameraPos.xyz - i.worldPos.xyz);

				// 法線的方向
				float NormalDirection = normalize(i.normal);

				//光照的方向
				float3 LightDirection = normalize(_WorldSpaceLightPos0.xyz);

				//計算光照的衰減 
				//衰減值
				float Attenuation = LIGHT_ATTENUATION(i);
				//衰減後的顏色值
				float3 AttenColor = Attenuation * _LightColor0;

				//計算漫反射值
				float Ndot = dot(NormalDirection,LightDirection);
				float3 Diffuse = max(0.0,Ndot)  * AttenColor + UNITY_LIGHTMODEL_AMBIENT.xyz;

				//計算自發光參數
				//計算邊緣強度
				half Rim = 1.0 - max(0,dot(i.normal,ViewDirection));
				//計算出邊緣自發光強度
				float3 Emissive = _RimColor.rgb * pow(Rim,_RimColorPower) * _RimIntensity;

				//【8.5】計在最終顏色中加入自發光顏色 || Calculate the final color
				//最終顏色 = (漫反射係數 x 紋理顏色 x rgb顏色)+自發光顏色 || Final Color=(Diffuse x Texture x rgbColor)+Emissive
				float3 finalColor = Diffuse * (tex2D(_TextureDiffuse,TRANSFORM_TEX(i.texcoord.rgb, _TextureDiffuse)).rgb*_MainColor.rgb) + Emissive;
			
				//【8.6】返回最終顏色 || Return final color
				return fixed4(finalColor,1);

			}
		ENDCG

	   }
	}
}



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