镜面反射

Shader "Custom/CustomSpecular" {
	Properties {
		_Color ("Color", Color) = (1,1,1,1)
		_SpeColor("Specular Color", Color) = (1,1,1,1)
		_Shininess("Shininess", Float) = 10
	}
	SubShader {
		Pass
		{
			Tags { "LightMode" = "ForwardBase" }
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#include "UnityCG.cginc"
			#include "Lighting.cginc"
			fixed4 _Color;
			fixed4 _SpeColor;
			float _Shininess;
			
			struct v2f
			{
				float4 pos : SV_POSITION;
				float4 col : COLOR;
			};
			
			v2f vert(appdata_base v)
			{
				v2f o;
				float3 normalDirection = normalize(mul(float4(v.normal, 0.0), _World2Object).xyz);
				float3 lightDirection = normalize(_WorldSpaceLightPos0.xyz);
				float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - mul(_Object2World, v.vertex));
				float3 specularReflection = _LightColor0.rgb*_SpeColor.rgb*pow(max(0.0,dot(reflect(-lightDirection, normalDirection),viewDirection)),_Shininess);
				float3 diffuseReflection = _LightColor0.rgb * _Color.rgb* max(0.0, dot(normalDirection, lightDirection));
				float3 ambientLighting = UNITY_LIGHTMODEL_AMBIENT.rgb * _Color.rgb;
				o.col = float4(ambientLighting + diffuseReflection + specularReflection, 1.0);
				o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
				return o;
			}
			
			fixed4 frag(v2f i) : COLOR
			{
				return i.col;
			}
			ENDCG
		}
	} 
	FallBack "Diffuse"
}

<pre name="code" class="plain">Shader "Custom/SpecularTex" {
	Properties {
		_MainTex ("Albedo (RGB)", 2D) = "white" {}
		_Color ("Color", Color) = (1,1,1,1)
		_SpeColor("Specular Color", Color) = (1,1,1,1)
		_Shininess("Shininess", Float) = 10
	}
	SubShader {
		Pass
		{
			Tags { "LightMode" = "ForwardBase" }
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#include "UnityCG.cginc"
			#include "Lighting.cginc"
			sampler2D _MainTex;
			fixed4 _Color;
			fixed4 _SpeColor;
			float _Shininess;
			
			struct v2f
			{
				float4 pos : SV_POSITION;
				float4 tex : TEXCOORD0;
				float4 posWorld : TEXCOORD1;
				float4 normalDir : TEXCOORD2;
			};
			
			v2f vert(appdata_base v)
			{
				v2f o;			
				o.posWorld = mul(_Object2World,  v.vertex);
				o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
				o.tex = v.texcoord;
				o.normalDir = normalize(mul(float4(v.normal, 0.0), _World2Object));
				return o;
			}
			
			fixed4 frag(v2f i) : COLOR
			{
				float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - i.posWorld);
				float3 lightDirection = normalize(_WorldSpaceLightPos0.xyz);
				float3 specularReflection = _LightColor0.rgb*_SpeColor.rgb*pow(max(0.0,dot(reflect(-lightDirection, i.normalDir.xyz),viewDirection)),_Shininess);
				float3 diffuseReflection = _LightColor0.rgb * _Color.rgb* max(0.0, dot(i.normalDir.xyz, lightDirection));
				float3 ambientLighting = UNITY_LIGHTMODEL_AMBIENT.rgb * _Color.rgb;
				return float4(ambientLighting + diffuseReflection * tex2D(_MainTex, i.tex.xy) + specularReflection, 1.0);
			}
			ENDCG
		}
	} 
	FallBack "Diffuse"
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值