Unity着色器和屏幕特效开发秘籍 顶点-片元着色器实现系列-Phong

表面着色器实现:

Shader "Phong"
{
	Properties
	{
		_MainTex("Base(RGB)",2D) = ""{}
		_MainTint("Main Tint",Color) = (1,1,1,1)
		_SpecularColor("Specular Color",Color) = (1,1,1,1)
		_SpecPower("Specular Power",Range(0,30)) = 1
	}
	SubShader
	{
		Tags{"RenderType" = "Opaque"}
		LOD 200
		CGPROGRAM
		#pragma surface surf Phong
		sampler2D _MainTex;
		fixed4 _MainTint;
		fixed4 _SpecularColor;
		fixed _SpecPower;
		struct Input
		{
			half2 uv_MainTex;
		};
		void surf(Input IN,inout SurfaceOutput o)
		{
			fixed4 col = tex2D(_MainTex,IN.uv_MainTex);
			o.Albedo = col.rgb;
			o.Alpha = col.a;
		}
		inline fixed4 LightingPhong(SurfaceOutput s,fixed3 lightDir,fixed3 viewDir,fixed atten)
		{
			fixed3 normal = normalize(s.Normal);
			fixed NdotL = dot(normal,lightDir);
			fixed3 reflectionDir = normalize(2 * normal * NdotL - lightDir);
			//fixed3 halfDir = normalize(lightDir + viewDir);
			//fixed NdotH = max(0,dot(normal,halfDir));
			fixed spec = pow(max(0,dot(reflectionDir,viewDir)),_SpecPower);
			fixed3 diffuse = _LightColor0.rgb * s.Albedo * max(0,NdotL);
			fixed3 specular = _LightColor0.rgb * _SpecularColor.rgb * pow(max(0,dot(reflectionDir,viewDir)),_SpecPower);
			//fixed3 specular = _LightColor0.rgb * _SpecularColor.rgb * pow(NdotH,_SpecPower);
			return fixed4(diffuse+specular,1.0);
		}
		ENDCG
	}
	FallBack "Diffuse"
}

顶点-片元实现:

Shader "Phong"
{
	Properties
	{
		_MainTex("Base(RGB)",2D) = ""{}
		_MainTint("Main Tint",Color) = (1,1,1,1)
		_SpecularColor("Specular Color",Color) = (1,1,1,1)
		_SpecPower("Specular Power",Range(0,30)) = 1
	}
	SubShader
	{
		Pass
		{
			Tags{"LightMode" = "ForwardBase"}
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#include "Lighting.cginc"
			sampler2D _MainTex;
			fixed4 _MainTex_ST;
			fixed4 _MainTint;
			fixed4 _SpecularColor;
			fixed _SpecPower;
			struct a2v
			{
				float4 vertex:POSITION;
				float3 normal:NORMAL;
				float4 texcoord:TEXCOORD0;
			};
			struct v2f
			{
				float4 pos:SV_POSITION;
				half2 uv:TEXCOORD0;
				float3 worldNormal:TEXCOORD1;
				float3 worldPos:TEXCOORD2;
			};
			v2f vert(a2v v)
			{
				v2f o;
				o.pos = mul(UNITY_MATRIX_MVP,v.vertex);
				o.uv = TRANSFORM_TEX(v.texcoord,_MainTex);
				o.worldNormal = mul(unity_ObjectToWorld,v.normal);
				o.worldPos = mul(unity_ObjectToWorld,v.vertex).xyz;
				return o;
			}
			fixed4 frag(v2f i):SV_Target
			{
				fixed3 worldNormal = normalize(i.worldNormal);
				fixed3 lightDir = normalize(UnityWorldSpaceLightDir(i.worldPos));
				fixed3 viewDir = normalize(UnityWorldSpaceViewDir(i.worldPos));
				fixed NdotL = dot(worldNormal,lightDir);
				fixed3 reflectionDir = normalize(2 * worldNormal * NdotL - lightDir);
				//fixed3 halfDir = normalize(lightDir + viewDir);
				//fixed NdotH = max(0,dot(worldNormal,halfDir));
				fixed3 albedo = tex2D(_MainTex,i.uv).rgb * _MainTint.rgb;
				fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo;
				fixed3 diffuse = _LightColor0.rgb * albedo * max(0,NdotL);
				fixed3 specular = _LightColor0.rgb * _SpecularColor.rgb * pow(max(0,dot(reflectionDir,viewDir)),_SpecPower);
				//fixed3 specular = _LightColor0.rgb * _SpecularColor.rgb * pow(NdotH,_SpecPower);
				return fixed4(ambient + diffuse + specular,1.0);
			}
			ENDCG
		}
	}
	FallBack "Diffuse"
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值