歡迎前往個人博客 駑馬點滴 和視頻空間 嗶哩嗶哩-《挨踢日志》
6.1 我們是如何看到這個世界的
- 需要有光源踏施;
- 光源發(fā)出的光有幾種情況:
a) 被別的物體吸收
b) 散射出去(反射和折射) - 攝像機接收的光,產(chǎn)生了圖片狸相;
一些名詞:
散射(scattering)
吸收(absorption)
折射(reflection)或者透射(transmission)
漫反射(diffuse)
高光反射(specular)
著色(shading)
6.2 什么是標準光照模型
標準光照模型是由著名學(xué)者裴祥風在1973年提出的。標準光照模型只關(guān)心直接光照(direct light)香璃,就是那些直接從光源發(fā)射出來照射到物體表面后哨毁,經(jīng)過物體表面到一次反射直接進入攝像機的光線钓试。
它的基本方法是螟蝙,將進入攝像機的光線分為 4 個部分恢恼,每個部分使用一種方法來計算其貢獻度。這 4 個部分分別是:
自發(fā)光(emissive)
直接使用材質(zhì)的自發(fā)光顏色胰默;c(emissive) = m(emissive)漫反射(diffuse)
漫反射光照符合蘭伯特定律(Lambert‘s Law):反射光線的強度與表面法線和光源方向之間夾角的余弦值成正比场斑,因此計算公式如下:
c(diffuse) = ( c(light) * m(diffuse) ) * max(0, n·l)高光反射(specular)
反射光 r = 2(l·n)n-l
c(specular) = c(light)m(specular)(max(0, v·r))^m(gloss)
上述模型為 Phong 模型,Blinn 模型引入一個新的矢量: h = (v+l)/|v+l|
光照模型為 c(specular) = c(light)m(specular)(max(0, v·h))^m(gloss)
Blinn 模型在 v 和 l 都是定值的時候牵署,會比較快漏隐,否者還是 Phong 比較快一些。環(huán)境光(ambient)碟刺,通常是一個全局變量 c(ambient) = g(ambient)
最后幾點:
- 光照模型可以用在頂點著色階段或者片元著色階段進行計算锁保,亦是逐頂點光照或者逐像素光照薯酝;
- 上述的模型都是經(jīng)驗?zāi)P桶牍粒瑘D形學(xué)第一定律:如果它看起來是對的,那么它就是對的吴菠;
- 標準光照模型又稱為 Phong 光照模型者填;
- 更改標準光照模型中的高光反射計算模型為 Blinn 算法的光照模型,稱為 Blinn-Phong 光照模型做葵;
6.3 Unity 中的環(huán)境光和自發(fā)光
暫時忽略占哟,未看到有環(huán)境光和自發(fā)光相關(guān)的設(shè)置;
6.4 如何在 Unity 中實現(xiàn)漫反射光照模型
Shader "Unity Shaders Book/Chapter 6/Diffuse Vertex-Level" {
Properties {
_Diffuse ("Diffuse", Color) = (1.0, 1.0, 1.0, 1.0)
}
SubShader {
Pass {
Tags {"LightMode"="ForwardBase"}
CGPROGRAM
#include "Lighting.cginc"
#pragma vertex vert
#pragma fragment frag
fixed4 _Diffuse;
struct a2v {
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct v2f {
float4 pos : SV_POSITION;
fixed3 color : COLOR0;
};
v2f vert(a2v v) {
v2f o;
// o.pos = mul(UNITY_MATRIX_MVP, v.vertex)
o.pos = UnityObjectToClipPos(v.vertex);
// Lighting
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
// fixed3 worldNormal = normalize(mul(v.normal, (float3x3)unity_WorldToObject));
fixed3 worldNormal = UnityObjectToWorldNormal(v.normal);
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 {
fixed3 c = i.color;
return fixed4(c, 1.0);
}
ENDCG
}
}
Fallback "Diffuse"
}
語法上的變動:
// Upgrade NOTE: replaced '_World2Object' with 'unity_WorldToObject'
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
- _World2Object 被替換為 unity_WorldToObject
- mul(UNITY_MATRIX_MVP,) 被替換為 UnityObjectToClipPos()
- Object Space 中的法線變換到 World Space 中時酿矢,Unity 提供了 UnityObjectToWorldNormal( in float3 norm ) 函數(shù)榨乎;
逐頂點計算更改為逐像素計算
將上述逐頂點的計算更改為逐像素計算:
// Upgrade NOTE: replaced '_World2Object' with 'unity_WorldToObject'
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
Shader "Unity Shaders Book/Chapter 6/Diffuse Pixel-Level" {
Properties {
_Diffuse ("Diffuse", Color) = (1.0, 1.0, 1.0, 1.0)
}
SubShader {
Pass {
Tags {"LightMode"="ForwardBase"}
CGPROGRAM
#include "Lighting.cginc"
#pragma vertex vert
#pragma fragment frag
fixed4 _Diffuse;
struct a2v {
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct v2f {
float4 pos : SV_POSITION;
fixed3 worldNormal : TEXCOORD0;
};
v2f vert(a2v v) {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.worldNormal = mul(v.normal, (float3x3)unity_WorldToObject);
return o;
}
fixed4 frag(v2f i): SV_Target {
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
fixed3 worldNormal = normalize(i.worldNormal);
fixed3 worldLight = normalize(_WorldSpaceLightPos0.xyz);
fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * saturate(dot(worldNormal, worldLight));
fixed3 c = ambient + diffuse;
return fixed4(c, 1.0);
}
ENDCG
}
}
Fallback "Diffuse"
}
上述使用的蘭伯特光照模型,在光照無法到達的地方瘫筐,會失去模型表現(xiàn)細節(jié)蜜暑,出現(xiàn)全黑的情況。有一種改善技術(shù)被提出來:半蘭伯特光照模型策肝。其公式為:
c(diffuse) = c(light)*m(diffuse)*(alpha*n·l + beta)
通常 alpha 和 beta 均取 0.5 肛捍。
// Upgrade NOTE: replaced '_World2Object' with 'unity_WorldToObject'
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
Shader "Unity Shaders Book/Chapter 6/Diffuse Pixel-Level Half Lambert" {
Properties {
_Diffuse ("Diffuse", Color) = (1.0, 1.0, 1.0, 1.0)
}
SubShader {
Pass {
Tags {"LightMode"="ForwardBase"}
CGPROGRAM
#include "Lighting.cginc"
#pragma vertex vert
#pragma fragment frag
fixed4 _Diffuse;
struct a2v {
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct v2f {
float4 pos : SV_POSITION;
fixed3 worldNormal : TEXCOORD0;
};
v2f vert(a2v v) {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.worldNormal = mul(v.normal, (float3x3)unity_WorldToObject);
return o;
}
fixed4 frag(v2f i): SV_Target {
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
fixed3 worldNormal = normalize(i.worldNormal);
fixed3 worldLight = normalize(_WorldSpaceLightPos0.xyz);
fixed halfLambert = dot(worldNormal, worldLight)*0.5 + 0.5;
fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * halfLambert;
fixed3 c = ambient + diffuse;
return fixed4(c, 1.0);
}
ENDCG
}
}
Fallback "Diffuse"
}
6.5 如何在 Unity 中實現(xiàn)高光反射模型
高光反射模型的計算公式為:
c(specular) = c(light)*m(specular)*(max(0, r·v))^m(gloss)
其中:
- c(light) 由 _LightColor0.rgb 獲得
- m(specular) 通過材質(zhì)面板獲取
- r 反射光線可以由 2·(l·n) - l 計算出來隐绵,其中 l 是光源方向(-l 是光照方向,注意二者的區(qū)別:光源方向是指向光源拙毫,光照方向是從光源發(fā)出的方向)依许,Unity 的 Cg 提供了計算反射光的函數(shù) reflect(i, n) 其中, i 是入射方向缀蹄,n 是法線方向峭跳;
- v 是視角方向;
對應(yīng)的 Shader:
// Upgrade NOTE: replaced '_World2Object' with 'unity_WorldToObject'
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
Shader "Unity Shaders Book/Chapter 6/Specular Vertex-Level" {
Properties {
_Diffuse ("Diffuse", Color) = (1.0, 1.0, 1.0, 1.0)
_Specular ("Specular", Color) = (1.0, 1.0, 1.0, 1.0)
_Gloss ("Gloss", Range(8.0, 256)) = 20
}
SubShader {
Pass {
Tags {"LightMode"="ForwardBase"}
CGPROGRAM
#include "Lighting.cginc"
#pragma vertex vert
#pragma fragment frag
fixed4 _Diffuse;
fixed4 _Specular;
fixed _Gloss;
struct a2v {
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct v2f {
float4 pos : SV_POSITION;
fixed3 color : COLOR0;
};
v2f vert(a2v v) {
v2f o;
// o.pos = mul(UNITY_MATRIX_MVP, v.vertex)
o.pos = UnityObjectToClipPos(v.vertex);
// Common Variables
fixed3 worldNormal = UnityObjectToWorldNormal(v.normal);
fixed3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz);
// Ambient Lighting
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
// Diffuse Lighting
fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * saturate(dot(worldNormal, worldLightDir));
// Specular Lighting
fixed3 reflectDir = normalize(reflect(-worldLightDir, worldNormal));
fixed3 viewDir = normalize(_WorldSpaceCameraPos.xyz - mul(unity_ObjectToWorld, v.vertex));
fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(saturate(dot(reflectDir, viewDir)), _Gloss);
// Summery of Lightings
o.color = ambient + diffuse + specular;
return o;
}
fixed4 frag(v2f i): SV_Target {
fixed3 c = i.color;
return fixed4(c, 1.0);
}
ENDCG
}
}
Fallback "Specular"
}
逐像素高光反射
// Upgrade NOTE: replaced '_World2Object' with 'unity_WorldToObject'
// Upgrade NOTE: replaced '_World2Object' with 'unity_WorldToObject'
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
Shader "Unity Shaders Book/Chapter 6/Specular Pixel-Level" {
Properties {
_Diffuse ("Diffuse", Color) = (1.0, 1.0, 1.0, 1.0)
_Specular ("Specular", Color) = (1.0, 1.0, 1.0, 1.0)
_Gloss ("Gloss", Range(8.0, 256)) = 20
}
SubShader {
Pass {
Tags {"LightMode"="ForwardBase"}
CGPROGRAM
#include "Lighting.cginc"
#pragma vertex vert
#pragma fragment frag
fixed4 _Diffuse;
fixed4 _Specular;
fixed _Gloss;
struct a2v {
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct v2f {
float4 pos : SV_POSITION;
float3 worldNormal: TEXCOORD0;
float3 worldPos: TEXCOORD1;
};
v2f vert(a2v v) {
v2f o;
// o.pos = mul(UNITY_MATRIX_MVP, v.vertex)
o.pos = UnityObjectToClipPos(v.vertex);
o.worldNormal = mul(v.normal, (float3x3)unity_WorldToObject);
// o.worldNormal = mul((float3x3)unity_ObjectToWorld, v.normal);
o.worldPos = mul(unity_ObjectToWorld, v.vertex);
return o;
}
fixed4 frag(v2f i): SV_Target {
// Common Variables
fixed3 worldNormal = normalize(i.worldNormal);
fixed3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz);
// Ambient Lighting
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
// Diffuse Lighting
fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * saturate(dot(worldNormal, worldLightDir));
// Specular Lighting
fixed3 reflectDir = normalize(reflect(-worldLightDir, worldNormal));
fixed3 viewDir = normalize(_WorldSpaceCameraPos.xyz - i.worldPos.xyz);
fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(saturate(dot(reflectDir, viewDir)), _Gloss);
// Summery of Lightings
fixed3 c = ambient + diffuse + specular;
return fixed4(c, 1.0);
}
ENDCG
}
}
Fallback "Specular"
}
Blinn-Phong 高光反射模型
// Upgrade NOTE: replaced '_World2Object' with 'unity_WorldToObject'
// Upgrade NOTE: replaced '_World2Object' with 'unity_WorldToObject'
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
Shader "Unity Shaders Book/Chapter 6/Specular Pixel-Level Blinn-Phong" {
Properties {
_Diffuse ("Diffuse", Color) = (1.0, 1.0, 1.0, 1.0)
_Specular ("Specular", Color) = (1.0, 1.0, 1.0, 1.0)
_Gloss ("Gloss", Range(8.0, 256)) = 20
}
SubShader {
Pass {
Tags {"LightMode"="ForwardBase"}
CGPROGRAM
#include "Lighting.cginc"
#pragma vertex vert
#pragma fragment frag
fixed4 _Diffuse;
fixed4 _Specular;
fixed _Gloss;
struct a2v {
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct v2f {
float4 pos : SV_POSITION;
float3 worldNormal: TEXCOORD0;
float3 worldPos: TEXCOORD1;
};
v2f vert(a2v v) {
v2f o;
// o.pos = mul(UNITY_MATRIX_MVP, v.vertex)
o.pos = UnityObjectToClipPos(v.vertex);
o.worldNormal = mul(v.normal, (float3x3)unity_WorldToObject);
// o.worldNormal = mul((float3x3)unity_ObjectToWorld, v.normal);
o.worldPos = mul(unity_ObjectToWorld, v.vertex);
return o;
}
fixed4 frag(v2f i): SV_Target {
// Common Variables
fixed3 worldNormal = normalize(i.worldNormal);
fixed3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz);
// Ambient Lighting
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
// Diffuse Lighting
fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * saturate(dot(worldNormal, worldLightDir));
// Specular Lighting
// fixed3 reflectDir = normalize(reflect(-worldLightDir, worldNormal));
fixed3 viewDir = normalize(_WorldSpaceCameraPos.xyz - i.worldPos.xyz);
fixed3 halfDir = normalize(worldLightDir + viewDir);
fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(saturate(dot(worldNormal, halfDir)), _Gloss);
// Summery of Lightings
fixed3 c = ambient + diffuse + specular;
return fixed4(c, 1.0);
}
ENDCG
}
}
Fallback "Specular"
}
6.6 使用 Unity 內(nèi)置函數(shù)
UnityCG.cginc 中一些常用的幫助函數(shù)
輸入:模型空間中的頂點
輸出:世界空間中從該點到攝像機的方向
inline float3 WorldSpaceViewDir( in float4 localPos )
{
float3 worldPos = mul(unity_ObjectToWorld, localPos).xyz;
return UnityWorldSpaceViewDir(worldPos);
}
輸入: 世界空間中的頂點
輸出:世界空間中從該點到攝像機的方向
inline float3 UnityWorldSpaceViewDir( in float3 worldPos )
{
return _WorldSpaceCameraPos.xyz - worldPos;
}
輸入:模型空間中到頂點
輸出:模型空間中從該點到攝像機的方向
inline float3 ObjSpaceViewDir( in float4 v )
{
float3 objSpaceCameraPos = mul(unity_WorldToObject, float4(_WorldSpaceCameraPos.xyz, 1)).xyz;
return objSpaceCameraPos - v.xyz;
}
輸入:模型空間中的頂點坐標
輸出:世界空間中袍患,從該點到光源的方向
inline float3 WorldSpaceLightDir( in float4 localPos )
{
float3 worldPos = mul(unity_ObjectToWorld, localPos).xyz;
return UnityWorldSpaceLightDir(worldPos);
}
備注:僅可用于向前渲染中坦康,沒有歸一化。
輸入:世界空間中的頂點坐標
輸出:世界空間中從該點到光源到方向
inline float3 UnityWorldSpaceLightDir( in float3 worldPos )
{
#ifndef USING_LIGHT_MULTI_COMPILE
return _WorldSpaceLightPos0.xyz - worldPos * _WorldSpaceLightPos0.w;
#else
#ifndef USING_DIRECTIONAL_LIGHT
return _WorldSpaceLightPos0.xyz - worldPos;
#else
return _WorldSpaceLightPos0.xyz;
#endif
#endif
}
備注:僅可用于向前渲染中诡延,沒有歸一化滞欠。
輸入:模型空間中的頂點坐標
輸出:模型空間中從該點到光源的方向
inline float3 ObjSpaceLightDir( in float4 v )
{
float3 objSpaceLightPos = mul(unity_WorldToObject, _WorldSpaceLightPos0).xyz;
#ifndef USING_LIGHT_MULTI_COMPILE
return objSpaceLightPos.xyz - v.xyz * _WorldSpaceLightPos0.w;
#else
#ifndef USING_DIRECTIONAL_LIGHT
return objSpaceLightPos.xyz - v.xyz;
#else
return objSpaceLightPos.xyz;
#endif
#endif
}
備注:僅可用于向前渲染中,沒有歸一化肆良。
歡迎前往個人博客 駑馬點滴 和視頻空間 嗶哩嗶哩-《挨踢日志》