您的位置:首页 > 移动开发 > Unity3D

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

2018-03-07 17:00 579 查看
表面着色器实现: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
4000
);
//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"
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐