Phong光照模型
进入摄像机的光线分为四个部分:
环境光(ambient)
其他所有间接光照
自发光(emissive)
给定一个方向,模型表面本身向该方向发射多少辐射量
漫反射(diffuse)
当光线从光源照射到模型表面时,会向所有方向散射多少辐射量
高光反射(specular)
当光线从光源照射到模型表面时,会向镜面反射方向散射多少辐射量
Blinn-Phong模型:引入半程向量来计算高光反射,能得到更好的结果
Blinn-Phong模型是各向同性(isotropic)的,固定视角和光源方向旋转表面时,反射不会发生变化。
逐像素光照:Phong着色(Phong shading)
使用每个像素的法线(对顶点法线插值得到 或法线纹理中采样得到)进行光照模型的计算
逐顶点光照:高洛德着色(Gouraud shading)
只在顶点上计算光照,将结果进行线性插值得到像素颜色,无法计算高光反射
且会导致图元内部的颜色暗于顶点处的最高颜色值,可能会产生棱角现象
但是计算量很小。
逐顶点光照 (Gouraud shading)
Shader "Custom/DiffuseVertex"
{
Properties
{
_Diffuse ("Diffuse", Color) = (1,1,1,1)
}
SubShader
{
Pass{
Tags { "RenderType"="ForwardBase" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Lighting.cginc"
struct a2v{
float4 vertex: POSITION;
float3 normal:NORMAL;
};
struct v2f{
float4 pos:SV_POSITION;
float3 color:COLOR;
};
fixed4 _Diffuse;
v2f vert(a2v v)
{
v2f o;
o.pos=UnityObjectToClipPos(v.vertex);
fixed3 ambient=UNITY_LIGHTMODEL_AMBIENT.xyz;
fixed3 worldNormal=normalize(mul(v.normal,(float3x3)unity_WorldToObject));
fixed3 worldLight=normalize(_WorldSpaceLightPos0.xyz);
fixed3 diffuse=_LightColor0.rgb * _Diffuse.rgb * saturate(dot(worldNormal,worldLight));
o.color=ambient+diffuse;
return o;
}
fixed4 frag(v2f i):SV_TARGET
{
return fixed4(i.color,1.0);
}
ENDCG
}
}
FallBack "Diffuse"
}
逐像素光照 (Phong shading)
Shader "Custom/DiffusePixel"
{
Properties
{
_Diffuse ("Diffuse", Color) = (1,1,1,1)
_Specular("Specular",Color)=(1,1,1,1)
_Gloss("Gloss",Range(8.0,256))=20
}
SubShader
{
Pass{
Tags { "RenderType"="ForwardBase" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Lighting.cginc"
struct a2v{
float4 vertex: POSITION;
float3 normal:NORMAL;
};
struct v2f{
float4 pos:SV_POSITION;
float3 worldNormal:TEXCOORD0;
float3 worldPos:TEXCOORD1;
};
fixed4 _Diffuse;
fixed4 _Specular;
float _Gloss;
v2f vert(a2v v)
{
v2f o;
o.pos=UnityObjectToClipPos(v.vertex);
o.worldNormal=mul(v.normal,(float3x3)unity_WorldToObject);
o.worldPos=mul(unity_ObjectToWorld,v.vertex).xyz;
return o;
}
fixed4 frag(v2f i):SV_TARGET
{
fixed3 worldNormal=normalize(i.worldNormal);
fixed3 ambient=UNITY_LIGHTMODEL_AMBIENT.xyz;
fixed3 worldLight=normalize(_WorldSpaceLightPos0.xyz);
fixed lambert=saturate(dot(worldNormal,worldLight));
float halfLambert=dot(worldNormal,worldLight)*0.5+0.5;
fixed3 diffuse=_LightColor0.rgb * _Diffuse.rgb * halfLambert;
return fixed4(ambient+diffuse,1.0);
}
ENDCG
}
}
FallBack "Diffuse"
}
镜面反射(高光反射)
在上述基础上增加了specular分量的计算,使用半程向量,可以不用计算反射光线。同时也给出了计算反射光线的方法。
逐像素光照
Shader "Custom/DiffusePixel"
{
Properties
{
_Diffuse ("Diffuse", Color) = (1,1,1,1)
_Specular("Specular",Color)=(1,1,1,1)
_Gloss("Gloss",Range(8.0,256))=20
}
SubShader
{
Pass{
Tags { "RenderType"="ForwardBase" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Lighting.cginc"
struct a2v{
float4 vertex: POSITION;
float3 normal:NORMAL;
};
struct v2f{
float4 pos:SV_POSITION;
float3 worldNormal:TEXCOORD0;
float3 worldPos:TEXCOORD1;
};
fixed4 _Diffuse;
fixed4 _Specular;
float _Gloss;
v2f vert(a2v v)
{
v2f o;
o.pos=UnityObjectToClipPos(v.vertex);
o.worldNormal=mul(v.normal,(float3x3)unity_WorldToObject);
o.worldPos=mul(unity_ObjectToWorld,v.vertex).xyz;
return o;
}
fixed4 frag(v2f i):SV_TARGET
{
fixed3 worldNormal=normalize(i.worldNormal);
fixed3 ambient=UNITY_LIGHTMODEL_AMBIENT.xyz;
fixed3 worldLight=normalize(_WorldSpaceLightPos0.xyz);
fixed lambert=saturate(dot(worldNormal,worldLight));
float halfLambert=dot(worldNormal,worldLight)*0.5+0.5;
fixed3 diffuse=_LightColor0.rgb * _Diffuse.rgb * halfLambert;
fixed3 viewDir=normalize(_WorldSpaceCameraPos.xyz - i.worldPos.xyz);
fixed3 reflectDir=normalize(reflect(-worldLight,worldNormal));
fixed3 halfDir=normalize(worldLight+viewDir);
fixed3 specular_phong = _LightColor0.rgb * _Specular.rgb * pow(saturate(dot(reflectDir,viewDir)),_Gloss);
fixed3 specular_blinn = _LightColor0.rgb * _Specular.rgb * pow(saturate(dot(halfDir,worldNormal)),_Gloss);
return fixed4(ambient+diffuse+specular_blinn,1.0);
}
ENDCG
}
}
FallBack "Diffuse"
}
纹理
单一纹理贴图
Shader "Custom/SingleTexture"
{
Properties
{
_Color ("Color", Color) = (1,1,1,1)
_Specular ("Specular",Color) = (1,1,1,1)
_Gloss ("Gloss",Range(8.0,256)) = 20
_MainTex ("Main Tex",2D)="white" {}
}
SubShader
{
Pass{
Tags { "LightMode"="ForwardBase" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Lighting.cginc"
struct a2v{
float4 vertex: POSITION;
float3 normal:NORMAL;
float4 texcoord:TEXCOORD0;
};
struct v2f{
float4 pos:SV_POSITION;
float3 worldNormal:TEXCOORD0;
float3 worldPos:TEXCOORD1;
float2 uv:TEXCOORD2;
};
fixed4 _Color;
fixed4 _Specular;
float _Gloss;
sampler2D _MainTex;
float4 _MainTex_ST;
v2f vert(a2v v)
{
v2f o;
o.pos=UnityObjectToClipPos(v.vertex);
o.worldNormal=UnityObjectToWorldNormal(v.normal);
o.worldPos=mul(unity_ObjectToWorld,v.vertex).xyz;
o.uv=v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;
return o;
}
fixed4 frag(v2f i):SV_TARGET
{
fixed3 worldNormal=normalize(i.worldNormal);
fixed3 worldLightDir=normalize(UnityWorldSpaceLightDir(i.worldPos));
fixed3 albedo=tex2D(_MainTex,i.uv).rgb * _Color.rgb;
fixed3 diffuse=albedo*_LightColor0.rgb*max(0,dot(worldNormal,worldLightDir));
fixed3 viewDir=normalize(UnityWorldSpaceViewDir(i.worldPos));
fixed3 halfDir=normalize(worldLightDir+viewDir);
fixed3 specular=_LightColor0.rgb * _Specular.rgb *pow(max(0,dot(worldNormal,halfDir)),_Gloss);
fixed3 ambient=UNITY_LIGHTMODEL_AMBIENT.xyz * albedo;
return fixed4(ambient+diffuse+specular,1.0);
}
ENDCG
}
}
FallBack "Specular"
}
法线贴图
在顶点的切线空间中计算
Shader "Custom/NormalMap_TangentSpace"
{
Properties
{
_Color ("Color Tint", Color) = (1,1,1,1)
_MainTex ("Main Tex", 2D) = "white" {}
_BumpMap ("Normal Map",2D)="bump" {}
_BumpScale ("Bump Scale",Float)=1.0
_Specular ("Specular", Color)=(1,1,1,1)
_Gloss ("Gloss", Range(0,255)) = 10
}
SubShader
{
Pass{
Tags { "LightMode"="ForwardBase" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Lighting.cginc"
#include "UnityCG.cginc"
fixed4 _Color;
fixed4 _Specular;
float _Gloss;
sampler2D _MainTex;
float4 _MainTex_ST;
sampler2D _BumpMap;
float4 _BumpMap_ST;
float _BumpScale;
struct a2v{
float4 vertex: POSITION;
float3 normal:NORMAL;
float4 tangent:TANGENT;
float4 texcoord:TEXCOORD0;
};
struct v2f{
float4 pos:SV_POSITION;
float4 uv:TEXCOORD0;
float3 lightDir:TEXCOORD1;
float3 viewDir:TEXCOORD2;
};
v2f vert(a2v v)
{
v2f o;
o.pos=UnityObjectToClipPos(v.vertex);
o.uv.xy=v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;
o.uv.zw=v.texcoord.xy * _BumpMap_ST.xy + _BumpMap_ST.zw;
float3 binormal=cross(normalize(v.normal),normalize(v.tangent.xyz)) * v.tangent.w;
float3x3 rotation=float3x3(v.tangent.xyz,binormal,v.normal);
o.lightDir=mul(rotation,ObjSpaceLightDir(v.vertex));
o.viewDir=mul(rotation,ObjSpaceViewDir(v.vertex));
return o;
}
fixed4 frag(v2f i):SV_TARGET
{
fixed3 tangentLightDir=normalize(i.lightDir);
fixed3 tangentViewDir=normalize(i.viewDir);
fixed4 packedNormal=tex2D(_BumpMap,i.uv.zw);
fixed3 tangentNormal;
tangentNormal.xy=(packedNormal.xy*2-1)*_BumpScale;
tangentNormal.z=sqrt(1.0-saturate(dot(tangentNormal.xy,tangentNormal.xy)));
fixed3 albedo=tex2D(_MainTex,i.uv).rgb * _Color.rgb;
fixed3 ambient=UNITY_LIGHTMODEL_AMBIENT.xyz * albedo;
fixed3 diffuse=albedo*_LightColor0.rgb*max(0,dot(tangentNormal,tangentLightDir));
fixed3 halfDir=normalize(tangentLightDir+tangentViewDir);
fixed3 specular=_LightColor0.rgb * _Specular.rgb *pow(max(0,dot(tangentNormal,halfDir)),_Gloss);
return fixed4(ambient+diffuse+specular,1.0);
}
ENDCG
}
}
FallBack "Specular"
}
在世界空间中计算(推荐)
Shader "Custom/NormalMap_WorldSpace"
{
Properties
{
_Color ("Color Tint", Color) = (1,1,1,1)
_MainTex ("Main Tex", 2D) = "white" {}
_BumpMap ("Normal Map",2D)="bump" {}
_BumpScale ("Bump Scale",Float)=1.0
_Specular ("Specular", Color)=(1,1,1,1)
_Gloss ("Gloss", Range(0,255)) = 10
}
SubShader
{
Pass{
Tags { "LightMode"="ForwardBase" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Lighting.cginc"
#include "UnityCG.cginc"
fixed4 _Color;
fixed4 _Specular;
float _Gloss;
sampler2D _MainTex;
float4 _MainTex_ST;
sampler2D _BumpMap;
float4 _BumpMap_ST;
float _BumpScale;
struct a2v{
float4 vertex: POSITION;
float3 normal:NORMAL;
float4 tangent:TANGENT;
float4 texcoord:TEXCOORD0;
};
struct v2f{
float4 pos:SV_POSITION;
float4 uv:TEXCOORD0;
float4 TtoW0: TEXCOORD1;
float4 TtoW1: TEXCOORD2;
float4 TtoW2: TEXCOORD3;
};
v2f vert(a2v v)
{
v2f o;
o.pos=UnityObjectToClipPos(v.vertex);
o.uv.xy=v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;
o.uv.zw=v.texcoord.xy * _BumpMap_ST.xy + _BumpMap_ST.zw;
float3 worldPos=mul(unity_ObjectToWorld,v.vertex);
fixed3 worldNormal=UnityObjectToWorldNormal(v.normal);
fixed3 worldTangent=UnityObjectToWorldDir(v.tangent.xyz);
fixed3 worldBinormal=cross(worldNormal,worldTangent) * v.tangent.w;
o.TtoW0=float4(worldTangent.x,worldBinormal.x,worldNormal.x,worldPos.x);
o.TtoW1=float4(worldTangent.y,worldBinormal.y,worldNormal.y,worldPos.y);
o.TtoW2=float4(worldTangent.z,worldBinormal.z,worldNormal.z,worldPos.z);
return o;
}
fixed4 frag(v2f i):SV_TARGET
{
float3 worldPos=float3(i.TtoW0.w,i.TtoW1.w,i.TtoW2.w);
fixed3 lightDir=normalize(UnityWorldSpaceLightDir(worldPos));
fixed3 viewDir=normalize(UnityWorldSpaceViewDir(worldPos));
fixed3 bump=UnpackNormal(tex2D(_BumpMap,i.uv.zw));
bump.xy*=_BumpScale;
bump.z=sqrt(1.0-saturate(dot(bump.xy,bump.xy)));
bump=normalize(half3(dot(i.TtoW0.xyz,bump),dot(i.TtoW1.xyz,bump),dot(i.TtoW2.xyz,bump)));
fixed3 albedo=tex2D(_MainTex,i.uv).rgb * _Color.rgb;
fixed3 ambient=UNITY_LIGHTMODEL_AMBIENT.xyz * albedo;
fixed3 diffuse=albedo*_LightColor0.rgb*max(0,dot(bump,lightDir));
fixed3 halfDir=normalize(lightDir+viewDir);
fixed3 specular=_LightColor0.rgb * _Specular.rgb *pow(max(0,dot(bump,halfDir)),_Gloss);
return fixed4(ambient+diffuse+specular,1.0);
}
ENDCG
}
}
FallBack "Specular"
}
参考
- 冯乐乐 《Unity Shader 入门精要》
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)